* lisp/subr.el (define-error): New function.
[emacs.git] / src / xfaces.c
blobacd2d2b11167844b1d3cee3cbba6764164782f5b
1 /* xfaces.c -- "Face" primitives.
3 Copyright (C) 1993-1994, 1998-2013 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 HAVE_X_WINDOWS
215 #include "xterm.h"
216 #ifdef USE_MOTIF
217 #include <Xm/Xm.h>
218 #include <Xm/XmStrDefs.h>
219 #endif /* USE_MOTIF */
220 #endif /* HAVE_X_WINDOWS */
222 #ifdef MSDOS
223 #include "dosfns.h"
224 #endif
226 #ifdef HAVE_WINDOW_SYSTEM
227 #include TERM_HEADER
228 #include "fontset.h"
229 #ifdef HAVE_NTGUI
230 #undef FRAME_X_DISPLAY_INFO
231 #define FRAME_X_DISPLAY_INFO FRAME_W32_DISPLAY_INFO
232 #define x_display_info w32_display_info
233 #define GCGraphicsExposures 0
234 #endif /* HAVE_NTGUI */
236 #ifdef HAVE_NS
237 #undef FRAME_X_DISPLAY_INFO
238 #define FRAME_X_DISPLAY_INFO FRAME_NS_DISPLAY_INFO
239 #define x_display_info ns_display_info
240 #define GCGraphicsExposures 0
241 #endif /* HAVE_NS */
242 #endif /* HAVE_WINDOW_SYSTEM */
244 #include "buffer.h"
245 #include "dispextern.h"
246 #include "blockinput.h"
247 #include "window.h"
248 #include "intervals.h"
249 #include "termchar.h"
251 #include "font.h"
253 #ifdef HAVE_X_WINDOWS
255 /* Compensate for a bug in Xos.h on some systems, on which it requires
256 time.h. On some such systems, Xos.h tries to redefine struct
257 timeval and struct timezone if USG is #defined while it is
258 #included. */
260 #ifdef XOS_NEEDS_TIME_H
261 #include <time.h>
262 #undef USG
263 #include <X11/Xos.h>
264 #define USG
265 #define __TIMEVAL__
266 #if defined USG || defined __TIMEVAL__ /* Don't warn about unused macros. */
267 #endif
268 #else /* not XOS_NEEDS_TIME_H */
269 #include <X11/Xos.h>
270 #endif /* not XOS_NEEDS_TIME_H */
272 #endif /* HAVE_X_WINDOWS */
274 #include <c-ctype.h>
276 /* Number of pt per inch (from the TeXbook). */
278 #define PT_PER_INCH 72.27
280 /* Non-zero if face attribute ATTR is unspecified. */
282 #define UNSPECIFIEDP(ATTR) EQ ((ATTR), Qunspecified)
284 /* Non-zero if face attribute ATTR is `ignore-defface'. */
286 #define IGNORE_DEFFACE_P(ATTR) EQ ((ATTR), QCignore_defface)
288 /* Value is the number of elements of VECTOR. */
290 #define DIM(VECTOR) (sizeof (VECTOR) / sizeof *(VECTOR))
292 /* Size of hash table of realized faces in face caches (should be a
293 prime number). */
295 #define FACE_CACHE_BUCKETS_SIZE 1001
297 /* Keyword symbols used for face attribute names. */
299 Lisp_Object QCfamily, QCheight, QCweight, QCslant;
300 static Lisp_Object QCunderline;
301 static Lisp_Object QCinverse_video, QCstipple;
302 Lisp_Object QCforeground, QCbackground;
303 Lisp_Object QCwidth;
304 static Lisp_Object QCfont, QCbold, QCitalic;
305 static Lisp_Object QCreverse_video;
306 static Lisp_Object QCoverline, QCstrike_through, QCbox, QCinherit;
307 static Lisp_Object QCfontset;
309 /* Symbols used for attribute values. */
311 Lisp_Object Qnormal;
312 Lisp_Object Qbold;
313 static Lisp_Object Qline, Qwave;
314 Lisp_Object Qextra_light, Qlight;
315 Lisp_Object Qsemi_light, Qsemi_bold, Qextra_bold, Qultra_bold;
316 Lisp_Object Qoblique;
317 Lisp_Object Qitalic;
318 static Lisp_Object Qreleased_button, Qpressed_button;
319 static Lisp_Object QCstyle, QCcolor, QCline_width;
320 Lisp_Object Qunspecified; /* used in dosfns.c */
321 static Lisp_Object QCignore_defface;
323 char unspecified_fg[] = "unspecified-fg", unspecified_bg[] = "unspecified-bg";
325 /* The name of the function to call when the background of the frame
326 has changed, frame_set_background_mode. */
328 static Lisp_Object Qframe_set_background_mode;
330 /* Names of basic faces. */
332 Lisp_Object Qdefault, Qtool_bar, Qfringe;
333 static Lisp_Object Qregion;
334 Lisp_Object Qheader_line, Qscroll_bar, Qcursor;
335 static Lisp_Object Qborder, Qmouse, Qmenu;
336 Lisp_Object Qmode_line_inactive;
337 static Lisp_Object Qvertical_border;
339 /* The symbol `face-alias'. A symbols having that property is an
340 alias for another face. Value of the property is the name of
341 the aliased face. */
343 static Lisp_Object Qface_alias;
345 /* Alist of alternative font families. Each element is of the form
346 (FAMILY FAMILY1 FAMILY2 ...). If fonts of FAMILY can't be loaded,
347 try FAMILY1, then FAMILY2, ... */
349 Lisp_Object Vface_alternative_font_family_alist;
351 /* Alist of alternative font registries. Each element is of the form
352 (REGISTRY REGISTRY1 REGISTRY2...). If fonts of REGISTRY can't be
353 loaded, try REGISTRY1, then REGISTRY2, ... */
355 Lisp_Object Vface_alternative_font_registry_alist;
357 /* Allowed scalable fonts. A value of nil means don't allow any
358 scalable fonts. A value of t means allow the use of any scalable
359 font. Otherwise, value must be a list of regular expressions. A
360 font may be scaled if its name matches a regular expression in the
361 list. */
363 static Lisp_Object Qscalable_fonts_allowed;
365 /* The symbols `foreground-color' and `background-color' which can be
366 used as part of a `face' property. This is for compatibility with
367 Emacs 20.2. */
369 Lisp_Object Qforeground_color, Qbackground_color;
371 /* The symbols `face' and `mouse-face' used as text properties. */
373 Lisp_Object Qface;
375 /* Property for basic faces which other faces cannot inherit. */
377 static Lisp_Object Qface_no_inherit;
379 /* Error symbol for wrong_type_argument in load_pixmap. */
381 static Lisp_Object Qbitmap_spec_p;
383 /* The next ID to assign to Lisp faces. */
385 static int next_lface_id;
387 /* A vector mapping Lisp face Id's to face names. */
389 static Lisp_Object *lface_id_to_name;
390 static ptrdiff_t lface_id_to_name_size;
392 /* TTY color-related functions (defined in tty-colors.el). */
394 static Lisp_Object Qtty_color_desc, Qtty_color_by_index, Qtty_color_standard_values;
396 /* The name of the function used to compute colors on TTYs. */
398 static Lisp_Object Qtty_color_alist;
400 /* Counter for calls to clear_face_cache. If this counter reaches
401 CLEAR_FONT_TABLE_COUNT, and a frame has more than
402 CLEAR_FONT_TABLE_NFONTS load, unused fonts are freed. */
404 static int clear_font_table_count;
405 #define CLEAR_FONT_TABLE_COUNT 100
406 #define CLEAR_FONT_TABLE_NFONTS 10
408 /* Non-zero means face attributes have been changed since the last
409 redisplay. Used in redisplay_internal. */
411 int face_change_count;
413 /* Non-zero means don't display bold text if a face's foreground
414 and background colors are the inverse of the default colors of the
415 display. This is a kluge to suppress `bold black' foreground text
416 which is hard to read on an LCD monitor. */
418 static int tty_suppress_bold_inverse_default_colors_p;
420 /* A list of the form `((x . y))' used to avoid consing in
421 Finternal_set_lisp_face_attribute. */
423 static Lisp_Object Vparam_value_alist;
425 /* The total number of colors currently allocated. */
427 #ifdef GLYPH_DEBUG
428 static int ncolors_allocated;
429 static int npixmaps_allocated;
430 static int ngcs;
431 #endif
433 /* Non-zero means the definition of the `menu' face for new frames has
434 been changed. */
436 static int menu_face_changed_default;
439 /* Function prototypes. */
441 struct table_entry;
442 struct named_merge_point;
444 static void set_font_frame_param (Lisp_Object, Lisp_Object);
445 static struct face *realize_face (struct face_cache *, Lisp_Object *,
446 int);
447 static struct face *realize_non_ascii_face (struct frame *, Lisp_Object,
448 struct face *);
449 static struct face *realize_x_face (struct face_cache *, Lisp_Object *);
450 static struct face *realize_tty_face (struct face_cache *, Lisp_Object *);
451 static bool realize_basic_faces (struct frame *);
452 static bool realize_default_face (struct frame *);
453 static void realize_named_face (struct frame *, Lisp_Object, int);
454 static struct face_cache *make_face_cache (struct frame *);
455 static void clear_face_gcs (struct face_cache *);
456 static void free_face_cache (struct face_cache *);
457 static int merge_face_ref (struct frame *, Lisp_Object, Lisp_Object *,
458 int, struct named_merge_point *);
461 /***********************************************************************
462 Utilities
463 ***********************************************************************/
465 #ifdef HAVE_X_WINDOWS
467 #ifdef DEBUG_X_COLORS
469 /* The following is a poor mans infrastructure for debugging X color
470 allocation problems on displays with PseudoColor-8. Some X servers
471 like 3.3.5 XF86_SVGA with Matrox cards apparently don't implement
472 color reference counts completely so that they don't signal an
473 error when a color is freed whose reference count is already 0.
474 Other X servers do. To help me debug this, the following code
475 implements a simple reference counting schema of its own, for a
476 single display/screen. --gerd. */
478 /* Reference counts for pixel colors. */
480 int color_count[256];
482 /* Register color PIXEL as allocated. */
484 void
485 register_color (unsigned long pixel)
487 eassert (pixel < 256);
488 ++color_count[pixel];
492 /* Register color PIXEL as deallocated. */
494 void
495 unregister_color (unsigned long pixel)
497 eassert (pixel < 256);
498 if (color_count[pixel] > 0)
499 --color_count[pixel];
500 else
501 emacs_abort ();
505 /* Register N colors from PIXELS as deallocated. */
507 void
508 unregister_colors (unsigned long *pixels, int n)
510 int i;
511 for (i = 0; i < n; ++i)
512 unregister_color (pixels[i]);
516 DEFUN ("dump-colors", Fdump_colors, Sdump_colors, 0, 0, 0,
517 doc: /* Dump currently allocated colors to stderr. */)
518 (void)
520 int i, n;
522 fputc ('\n', stderr);
524 for (i = n = 0; i < sizeof color_count / sizeof color_count[0]; ++i)
525 if (color_count[i])
527 fprintf (stderr, "%3d: %5d", i, color_count[i]);
528 ++n;
529 if (n % 5 == 0)
530 fputc ('\n', stderr);
531 else
532 fputc ('\t', stderr);
535 if (n % 5 != 0)
536 fputc ('\n', stderr);
537 return Qnil;
540 #endif /* DEBUG_X_COLORS */
543 /* Free colors used on frame F. PIXELS is an array of NPIXELS pixel
544 color values. Interrupt input must be blocked when this function
545 is called. */
547 void
548 x_free_colors (struct frame *f, long unsigned int *pixels, int npixels)
550 int class = FRAME_X_DISPLAY_INFO (f)->visual->class;
552 /* If display has an immutable color map, freeing colors is not
553 necessary and some servers don't allow it. So don't do it. */
554 if (class != StaticColor && class != StaticGray && class != TrueColor)
556 #ifdef DEBUG_X_COLORS
557 unregister_colors (pixels, npixels);
558 #endif
559 XFreeColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
560 pixels, npixels, 0);
565 #ifdef USE_X_TOOLKIT
567 /* Free colors used on frame F. PIXELS is an array of NPIXELS pixel
568 color values. Interrupt input must be blocked when this function
569 is called. */
571 void
572 x_free_dpy_colors (Display *dpy, Screen *screen, Colormap cmap,
573 long unsigned int *pixels, int npixels)
575 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
576 int class = dpyinfo->visual->class;
578 /* If display has an immutable color map, freeing colors is not
579 necessary and some servers don't allow it. So don't do it. */
580 if (class != StaticColor && class != StaticGray && class != TrueColor)
582 #ifdef DEBUG_X_COLORS
583 unregister_colors (pixels, npixels);
584 #endif
585 XFreeColors (dpy, cmap, pixels, npixels, 0);
588 #endif /* USE_X_TOOLKIT */
590 /* Create and return a GC for use on frame F. GC values and mask
591 are given by XGCV and MASK. */
593 static GC
594 x_create_gc (struct frame *f, long unsigned int mask, XGCValues *xgcv)
596 GC gc;
597 block_input ();
598 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), mask, xgcv);
599 unblock_input ();
600 IF_DEBUG (++ngcs);
601 return gc;
605 /* Free GC which was used on frame F. */
607 static void
608 x_free_gc (struct frame *f, GC gc)
610 eassert (input_blocked_p ());
611 IF_DEBUG (eassert (--ngcs >= 0));
612 XFreeGC (FRAME_X_DISPLAY (f), gc);
615 #endif /* HAVE_X_WINDOWS */
617 #ifdef HAVE_NTGUI
618 /* W32 emulation of GCs */
620 static GC
621 x_create_gc (struct frame *f, unsigned long mask, XGCValues *xgcv)
623 GC gc;
624 block_input ();
625 gc = XCreateGC (NULL, FRAME_W32_WINDOW (f), mask, xgcv);
626 unblock_input ();
627 IF_DEBUG (++ngcs);
628 return gc;
632 /* Free GC which was used on frame F. */
634 static void
635 x_free_gc (struct frame *f, GC gc)
637 IF_DEBUG (eassert (--ngcs >= 0));
638 xfree (gc);
641 #endif /* HAVE_NTGUI */
643 #ifdef HAVE_NS
644 /* NS emulation of GCs */
646 static GC
647 x_create_gc (struct frame *f,
648 unsigned long mask,
649 XGCValues *xgcv)
651 GC gc = xmalloc (sizeof *gc);
652 *gc = *xgcv;
653 return gc;
656 static void
657 x_free_gc (struct frame *f, GC gc)
659 xfree (gc);
661 #endif /* HAVE_NS */
663 /***********************************************************************
664 Frames and faces
665 ***********************************************************************/
667 /* Initialize face cache and basic faces for frame F. */
669 void
670 init_frame_faces (struct frame *f)
672 /* Make a face cache, if F doesn't have one. */
673 if (FRAME_FACE_CACHE (f) == NULL)
674 FRAME_FACE_CACHE (f) = make_face_cache (f);
676 #ifdef HAVE_WINDOW_SYSTEM
677 /* Make the image cache. */
678 if (FRAME_WINDOW_P (f))
680 /* We initialize the image cache when creating the first frame
681 on a terminal, and not during terminal creation. This way,
682 `x-open-connection' on a tty won't create an image cache. */
683 if (FRAME_IMAGE_CACHE (f) == NULL)
684 FRAME_IMAGE_CACHE (f) = make_image_cache ();
685 ++FRAME_IMAGE_CACHE (f)->refcount;
687 #endif /* HAVE_WINDOW_SYSTEM */
689 /* Realize basic faces. Must have enough information in frame
690 parameters to realize basic faces at this point. */
691 #ifdef HAVE_X_WINDOWS
692 if (!FRAME_X_P (f) || FRAME_X_WINDOW (f))
693 #endif
694 #ifdef HAVE_NTGUI
695 if (!FRAME_WINDOW_P (f) || FRAME_W32_WINDOW (f))
696 #endif
697 #ifdef HAVE_NS
698 if (!FRAME_NS_P (f) || FRAME_NS_WINDOW (f))
699 #endif
700 if (!realize_basic_faces (f))
701 emacs_abort ();
705 /* Free face cache of frame F. Called from delete_frame. */
707 void
708 free_frame_faces (struct frame *f)
710 struct face_cache *face_cache = FRAME_FACE_CACHE (f);
712 if (face_cache)
714 free_face_cache (face_cache);
715 FRAME_FACE_CACHE (f) = NULL;
718 #ifdef HAVE_WINDOW_SYSTEM
719 if (FRAME_WINDOW_P (f))
721 struct image_cache *image_cache = FRAME_IMAGE_CACHE (f);
722 if (image_cache)
724 --image_cache->refcount;
725 if (image_cache->refcount == 0)
726 free_image_cache (f);
729 #endif /* HAVE_WINDOW_SYSTEM */
733 /* Clear face caches, and recompute basic faces for frame F. Call
734 this after changing frame parameters on which those faces depend,
735 or when realized faces have been freed due to changing attributes
736 of named faces. */
738 void
739 recompute_basic_faces (struct frame *f)
741 if (FRAME_FACE_CACHE (f))
743 clear_face_cache (0);
744 if (!realize_basic_faces (f))
745 emacs_abort ();
750 /* Clear the face caches of all frames. CLEAR_FONTS_P non-zero means
751 try to free unused fonts, too. */
753 void
754 clear_face_cache (int clear_fonts_p)
756 #ifdef HAVE_WINDOW_SYSTEM
757 Lisp_Object tail, frame;
759 if (clear_fonts_p
760 || ++clear_font_table_count == CLEAR_FONT_TABLE_COUNT)
762 #if 0
763 /* Not yet implemented. */
764 clear_font_cache (frame);
765 #endif
767 /* From time to time see if we can unload some fonts. This also
768 frees all realized faces on all frames. Fonts needed by
769 faces will be loaded again when faces are realized again. */
770 clear_font_table_count = 0;
772 FOR_EACH_FRAME (tail, frame)
774 struct frame *f = XFRAME (frame);
775 if (FRAME_WINDOW_P (f)
776 && FRAME_X_DISPLAY_INFO (f)->n_fonts > CLEAR_FONT_TABLE_NFONTS)
777 free_all_realized_faces (frame);
780 else
782 /* Clear GCs of realized faces. */
783 FOR_EACH_FRAME (tail, frame)
785 struct frame *f = XFRAME (frame);
786 if (FRAME_WINDOW_P (f))
787 clear_face_gcs (FRAME_FACE_CACHE (f));
789 clear_image_caches (Qnil);
791 #endif /* HAVE_WINDOW_SYSTEM */
795 DEFUN ("clear-face-cache", Fclear_face_cache, Sclear_face_cache, 0, 1, 0,
796 doc: /* Clear face caches on all frames.
797 Optional THOROUGHLY non-nil means try to free unused fonts, too. */)
798 (Lisp_Object thoroughly)
800 clear_face_cache (!NILP (thoroughly));
801 ++face_change_count;
802 ++windows_or_buffers_changed;
803 return Qnil;
807 /***********************************************************************
808 X Pixmaps
809 ***********************************************************************/
811 #ifdef HAVE_WINDOW_SYSTEM
813 DEFUN ("bitmap-spec-p", Fbitmap_spec_p, Sbitmap_spec_p, 1, 1, 0,
814 doc: /* Value is non-nil if OBJECT is a valid bitmap specification.
815 A bitmap specification is either a string, a file name, or a list
816 \(WIDTH HEIGHT DATA) where WIDTH is the pixel width of the bitmap,
817 HEIGHT is its height, and DATA is a string containing the bits of
818 the pixmap. Bits are stored row by row, each row occupies
819 \(WIDTH + 7)/8 bytes. */)
820 (Lisp_Object object)
822 bool pixmap_p = 0;
824 if (STRINGP (object))
825 /* If OBJECT is a string, it's a file name. */
826 pixmap_p = 1;
827 else if (CONSP (object))
829 /* Otherwise OBJECT must be (WIDTH HEIGHT DATA), WIDTH and
830 HEIGHT must be ints > 0, and DATA must be string large
831 enough to hold a bitmap of the specified size. */
832 Lisp_Object width, height, data;
834 height = width = data = Qnil;
836 if (CONSP (object))
838 width = XCAR (object);
839 object = XCDR (object);
840 if (CONSP (object))
842 height = XCAR (object);
843 object = XCDR (object);
844 if (CONSP (object))
845 data = XCAR (object);
849 if (STRINGP (data)
850 && RANGED_INTEGERP (1, width, INT_MAX)
851 && RANGED_INTEGERP (1, height, INT_MAX))
853 int bytes_per_row = ((XINT (width) + BITS_PER_CHAR - 1)
854 / BITS_PER_CHAR);
855 if (XINT (height) <= SBYTES (data) / bytes_per_row)
856 pixmap_p = 1;
860 return pixmap_p ? Qt : Qnil;
864 /* Load a bitmap according to NAME (which is either a file name or a
865 pixmap spec) for use on frame F. Value is the bitmap_id (see
866 xfns.c). If NAME is nil, return with a bitmap id of zero. If
867 bitmap cannot be loaded, display a message saying so, and return
868 zero. Store the bitmap width in *W_PTR and its height in *H_PTR,
869 if these pointers are not null. */
871 static ptrdiff_t
872 load_pixmap (struct frame *f, Lisp_Object name, unsigned int *w_ptr,
873 unsigned int *h_ptr)
875 ptrdiff_t bitmap_id;
877 if (NILP (name))
878 return 0;
880 CHECK_TYPE (!NILP (Fbitmap_spec_p (name)), Qbitmap_spec_p, name);
882 block_input ();
883 if (CONSP (name))
885 /* Decode a bitmap spec into a bitmap. */
887 int h, w;
888 Lisp_Object bits;
890 w = XINT (Fcar (name));
891 h = XINT (Fcar (Fcdr (name)));
892 bits = Fcar (Fcdr (Fcdr (name)));
894 bitmap_id = x_create_bitmap_from_data (f, SSDATA (bits),
895 w, h);
897 else
899 /* It must be a string -- a file name. */
900 bitmap_id = x_create_bitmap_from_file (f, name);
902 unblock_input ();
904 if (bitmap_id < 0)
906 add_to_log ("Invalid or undefined bitmap `%s'", name, Qnil);
907 bitmap_id = 0;
909 if (w_ptr)
910 *w_ptr = 0;
911 if (h_ptr)
912 *h_ptr = 0;
914 else
916 #ifdef GLYPH_DEBUG
917 ++npixmaps_allocated;
918 #endif
919 if (w_ptr)
920 *w_ptr = x_bitmap_width (f, bitmap_id);
922 if (h_ptr)
923 *h_ptr = x_bitmap_height (f, bitmap_id);
926 return bitmap_id;
929 #endif /* HAVE_WINDOW_SYSTEM */
933 /***********************************************************************
934 X Colors
935 ***********************************************************************/
937 /* Parse RGB_LIST, and fill in the RGB fields of COLOR.
938 RGB_LIST should contain (at least) 3 lisp integers.
939 Return 0 if there's a problem with RGB_LIST, otherwise return 1. */
941 static int
942 parse_rgb_list (Lisp_Object rgb_list, XColor *color)
944 #define PARSE_RGB_LIST_FIELD(field) \
945 if (CONSP (rgb_list) && INTEGERP (XCAR (rgb_list))) \
947 color->field = XINT (XCAR (rgb_list)); \
948 rgb_list = XCDR (rgb_list); \
950 else \
951 return 0;
953 PARSE_RGB_LIST_FIELD (red);
954 PARSE_RGB_LIST_FIELD (green);
955 PARSE_RGB_LIST_FIELD (blue);
957 return 1;
961 /* Lookup on frame F the color described by the lisp string COLOR.
962 The resulting tty color is returned in TTY_COLOR; if STD_COLOR is
963 non-zero, then the `standard' definition of the same color is
964 returned in it. */
966 static bool
967 tty_lookup_color (struct frame *f, Lisp_Object color, XColor *tty_color,
968 XColor *std_color)
970 Lisp_Object frame, color_desc;
972 if (!STRINGP (color) || NILP (Ffboundp (Qtty_color_desc)))
973 return 0;
975 XSETFRAME (frame, f);
977 color_desc = call2 (Qtty_color_desc, color, frame);
978 if (CONSP (color_desc) && CONSP (XCDR (color_desc)))
980 Lisp_Object rgb;
982 if (! INTEGERP (XCAR (XCDR (color_desc))))
983 return 0;
985 tty_color->pixel = XINT (XCAR (XCDR (color_desc)));
987 rgb = XCDR (XCDR (color_desc));
988 if (! parse_rgb_list (rgb, tty_color))
989 return 0;
991 /* Should we fill in STD_COLOR too? */
992 if (std_color)
994 /* Default STD_COLOR to the same as TTY_COLOR. */
995 *std_color = *tty_color;
997 /* Do a quick check to see if the returned descriptor is
998 actually _exactly_ equal to COLOR, otherwise we have to
999 lookup STD_COLOR separately. If it's impossible to lookup
1000 a standard color, we just give up and use TTY_COLOR. */
1001 if ((!STRINGP (XCAR (color_desc))
1002 || NILP (Fstring_equal (color, XCAR (color_desc))))
1003 && !NILP (Ffboundp (Qtty_color_standard_values)))
1005 /* Look up STD_COLOR separately. */
1006 rgb = call1 (Qtty_color_standard_values, color);
1007 if (! parse_rgb_list (rgb, std_color))
1008 return 0;
1012 return 1;
1014 else if (NILP (Fsymbol_value (intern ("tty-defined-color-alist"))))
1015 /* We were called early during startup, and the colors are not
1016 yet set up in tty-defined-color-alist. Don't return a failure
1017 indication, since this produces the annoying "Unable to
1018 load color" messages in the *Messages* buffer. */
1019 return 1;
1020 else
1021 /* tty-color-desc seems to have returned a bad value. */
1022 return 0;
1025 /* A version of defined_color for non-X frames. */
1027 static bool
1028 tty_defined_color (struct frame *f, const char *color_name,
1029 XColor *color_def, bool alloc)
1031 bool status = 1;
1033 /* Defaults. */
1034 color_def->pixel = FACE_TTY_DEFAULT_COLOR;
1035 color_def->red = 0;
1036 color_def->blue = 0;
1037 color_def->green = 0;
1039 if (*color_name)
1040 status = tty_lookup_color (f, build_string (color_name), color_def, NULL);
1042 if (color_def->pixel == FACE_TTY_DEFAULT_COLOR && *color_name)
1044 if (strcmp (color_name, "unspecified-fg") == 0)
1045 color_def->pixel = FACE_TTY_DEFAULT_FG_COLOR;
1046 else if (strcmp (color_name, "unspecified-bg") == 0)
1047 color_def->pixel = FACE_TTY_DEFAULT_BG_COLOR;
1050 if (color_def->pixel != FACE_TTY_DEFAULT_COLOR)
1051 status = 1;
1053 return status;
1057 /* Decide if color named COLOR_NAME is valid for the display
1058 associated with the frame F; if so, return the rgb values in
1059 COLOR_DEF. If ALLOC, allocate a new colormap cell.
1061 This does the right thing for any type of frame. */
1063 static bool
1064 defined_color (struct frame *f, const char *color_name, XColor *color_def,
1065 bool alloc)
1067 if (!FRAME_WINDOW_P (f))
1068 return tty_defined_color (f, color_name, color_def, alloc);
1069 #ifdef HAVE_X_WINDOWS
1070 else if (FRAME_X_P (f))
1071 return x_defined_color (f, color_name, color_def, alloc);
1072 #endif
1073 #ifdef HAVE_NTGUI
1074 else if (FRAME_W32_P (f))
1075 return w32_defined_color (f, color_name, color_def, alloc);
1076 #endif
1077 #ifdef HAVE_NS
1078 else if (FRAME_NS_P (f))
1079 return ns_defined_color (f, color_name, color_def, alloc, 1);
1080 #endif
1081 else
1082 emacs_abort ();
1086 /* Given the index IDX of a tty color on frame F, return its name, a
1087 Lisp string. */
1089 Lisp_Object
1090 tty_color_name (struct frame *f, int idx)
1092 if (idx >= 0 && !NILP (Ffboundp (Qtty_color_by_index)))
1094 Lisp_Object frame;
1095 Lisp_Object coldesc;
1097 XSETFRAME (frame, f);
1098 coldesc = call2 (Qtty_color_by_index, make_number (idx), frame);
1100 if (!NILP (coldesc))
1101 return XCAR (coldesc);
1103 #ifdef MSDOS
1104 /* We can have an MSDOG frame under -nw for a short window of
1105 opportunity before internal_terminal_init is called. DTRT. */
1106 if (FRAME_MSDOS_P (f) && !inhibit_window_system)
1107 return msdos_stdcolor_name (idx);
1108 #endif
1110 if (idx == FACE_TTY_DEFAULT_FG_COLOR)
1111 return build_string (unspecified_fg);
1112 if (idx == FACE_TTY_DEFAULT_BG_COLOR)
1113 return build_string (unspecified_bg);
1115 return Qunspecified;
1119 /* Return non-zero if COLOR_NAME is a shade of gray (or white or
1120 black) on frame F.
1122 The criterion implemented here is not a terribly sophisticated one. */
1124 static int
1125 face_color_gray_p (struct frame *f, const char *color_name)
1127 XColor color;
1128 int gray_p;
1130 if (defined_color (f, color_name, &color, 0))
1131 gray_p = (/* Any color sufficiently close to black counts as gray. */
1132 (color.red < 5000 && color.green < 5000 && color.blue < 5000)
1134 ((eabs (color.red - color.green)
1135 < max (color.red, color.green) / 20)
1136 && (eabs (color.green - color.blue)
1137 < max (color.green, color.blue) / 20)
1138 && (eabs (color.blue - color.red)
1139 < max (color.blue, color.red) / 20)));
1140 else
1141 gray_p = 0;
1143 return gray_p;
1147 /* Return non-zero if color COLOR_NAME can be displayed on frame F.
1148 BACKGROUND_P non-zero means the color will be used as background
1149 color. */
1151 static int
1152 face_color_supported_p (struct frame *f, const char *color_name,
1153 int background_p)
1155 Lisp_Object frame;
1156 XColor not_used;
1158 XSETFRAME (frame, f);
1159 return
1160 #ifdef HAVE_WINDOW_SYSTEM
1161 FRAME_WINDOW_P (f)
1162 ? (!NILP (Fxw_display_color_p (frame))
1163 || xstrcasecmp (color_name, "black") == 0
1164 || xstrcasecmp (color_name, "white") == 0
1165 || (background_p
1166 && face_color_gray_p (f, color_name))
1167 || (!NILP (Fx_display_grayscale_p (frame))
1168 && face_color_gray_p (f, color_name)))
1170 #endif
1171 tty_defined_color (f, color_name, &not_used, 0);
1175 DEFUN ("color-gray-p", Fcolor_gray_p, Scolor_gray_p, 1, 2, 0,
1176 doc: /* Return non-nil if COLOR is a shade of gray (or white or black).
1177 FRAME specifies the frame and thus the display for interpreting COLOR.
1178 If FRAME is nil or omitted, use the selected frame. */)
1179 (Lisp_Object color, Lisp_Object frame)
1181 CHECK_STRING (color);
1182 return (face_color_gray_p (decode_any_frame (frame), SSDATA (color))
1183 ? Qt : Qnil);
1187 DEFUN ("color-supported-p", Fcolor_supported_p,
1188 Scolor_supported_p, 1, 3, 0,
1189 doc: /* Return non-nil if COLOR can be displayed on FRAME.
1190 BACKGROUND-P non-nil means COLOR is used as a background.
1191 Otherwise, this function tells whether it can be used as a foreground.
1192 If FRAME is nil or omitted, use the selected frame.
1193 COLOR must be a valid color name. */)
1194 (Lisp_Object color, Lisp_Object frame, Lisp_Object background_p)
1196 CHECK_STRING (color);
1197 return (face_color_supported_p (decode_any_frame (frame),
1198 SSDATA (color), !NILP (background_p))
1199 ? Qt : Qnil);
1203 /* Load color with name NAME for use by face FACE on frame F.
1204 TARGET_INDEX must be one of LFACE_FOREGROUND_INDEX,
1205 LFACE_BACKGROUND_INDEX, LFACE_UNDERLINE_INDEX, LFACE_OVERLINE_INDEX,
1206 LFACE_STRIKE_THROUGH_INDEX, or LFACE_BOX_INDEX. Value is the
1207 pixel color. If color cannot be loaded, display a message, and
1208 return the foreground, background or underline color of F, but
1209 record that fact in flags of the face so that we don't try to free
1210 these colors. */
1212 unsigned long
1213 load_color (struct frame *f, struct face *face, Lisp_Object name,
1214 enum lface_attribute_index target_index)
1216 XColor color;
1218 eassert (STRINGP (name));
1219 eassert (target_index == LFACE_FOREGROUND_INDEX
1220 || target_index == LFACE_BACKGROUND_INDEX
1221 || target_index == LFACE_UNDERLINE_INDEX
1222 || target_index == LFACE_OVERLINE_INDEX
1223 || target_index == LFACE_STRIKE_THROUGH_INDEX
1224 || target_index == LFACE_BOX_INDEX);
1226 /* if the color map is full, defined_color will return a best match
1227 to the values in an existing cell. */
1228 if (!defined_color (f, SSDATA (name), &color, 1))
1230 add_to_log ("Unable to load color \"%s\"", name, Qnil);
1232 switch (target_index)
1234 case LFACE_FOREGROUND_INDEX:
1235 face->foreground_defaulted_p = 1;
1236 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1237 break;
1239 case LFACE_BACKGROUND_INDEX:
1240 face->background_defaulted_p = 1;
1241 color.pixel = FRAME_BACKGROUND_PIXEL (f);
1242 break;
1244 case LFACE_UNDERLINE_INDEX:
1245 face->underline_defaulted_p = 1;
1246 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1247 break;
1249 case LFACE_OVERLINE_INDEX:
1250 face->overline_color_defaulted_p = 1;
1251 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1252 break;
1254 case LFACE_STRIKE_THROUGH_INDEX:
1255 face->strike_through_color_defaulted_p = 1;
1256 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1257 break;
1259 case LFACE_BOX_INDEX:
1260 face->box_color_defaulted_p = 1;
1261 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1262 break;
1264 default:
1265 emacs_abort ();
1268 #ifdef GLYPH_DEBUG
1269 else
1270 ++ncolors_allocated;
1271 #endif
1273 return color.pixel;
1277 #ifdef HAVE_WINDOW_SYSTEM
1279 /* Load colors for face FACE which is used on frame F. Colors are
1280 specified by slots LFACE_BACKGROUND_INDEX and LFACE_FOREGROUND_INDEX
1281 of ATTRS. If the background color specified is not supported on F,
1282 try to emulate gray colors with a stipple from Vface_default_stipple. */
1284 static void
1285 load_face_colors (struct frame *f, struct face *face,
1286 Lisp_Object attrs[LFACE_VECTOR_SIZE])
1288 Lisp_Object fg, bg;
1290 bg = attrs[LFACE_BACKGROUND_INDEX];
1291 fg = attrs[LFACE_FOREGROUND_INDEX];
1293 /* Swap colors if face is inverse-video. */
1294 if (EQ (attrs[LFACE_INVERSE_INDEX], Qt))
1296 Lisp_Object tmp;
1297 tmp = fg;
1298 fg = bg;
1299 bg = tmp;
1302 /* Check for support for foreground, not for background because
1303 face_color_supported_p is smart enough to know that grays are
1304 "supported" as background because we are supposed to use stipple
1305 for them. */
1306 if (!face_color_supported_p (f, SSDATA (bg), 0)
1307 && !NILP (Fbitmap_spec_p (Vface_default_stipple)))
1309 x_destroy_bitmap (f, face->stipple);
1310 face->stipple = load_pixmap (f, Vface_default_stipple,
1311 &face->pixmap_w, &face->pixmap_h);
1314 face->background = load_color (f, face, bg, LFACE_BACKGROUND_INDEX);
1315 face->foreground = load_color (f, face, fg, LFACE_FOREGROUND_INDEX);
1319 /* Free color PIXEL on frame F. */
1321 void
1322 unload_color (struct frame *f, long unsigned int pixel)
1324 #ifdef HAVE_X_WINDOWS
1325 if (pixel != -1)
1327 block_input ();
1328 x_free_colors (f, &pixel, 1);
1329 unblock_input ();
1331 #endif
1335 /* Free colors allocated for FACE. */
1337 static void
1338 free_face_colors (struct frame *f, struct face *face)
1340 /* PENDING(NS): need to do something here? */
1341 #ifdef HAVE_X_WINDOWS
1342 if (face->colors_copied_bitwise_p)
1343 return;
1345 block_input ();
1347 if (!face->foreground_defaulted_p)
1349 x_free_colors (f, &face->foreground, 1);
1350 IF_DEBUG (--ncolors_allocated);
1353 if (!face->background_defaulted_p)
1355 x_free_colors (f, &face->background, 1);
1356 IF_DEBUG (--ncolors_allocated);
1359 if (face->underline_p
1360 && !face->underline_defaulted_p)
1362 x_free_colors (f, &face->underline_color, 1);
1363 IF_DEBUG (--ncolors_allocated);
1366 if (face->overline_p
1367 && !face->overline_color_defaulted_p)
1369 x_free_colors (f, &face->overline_color, 1);
1370 IF_DEBUG (--ncolors_allocated);
1373 if (face->strike_through_p
1374 && !face->strike_through_color_defaulted_p)
1376 x_free_colors (f, &face->strike_through_color, 1);
1377 IF_DEBUG (--ncolors_allocated);
1380 if (face->box != FACE_NO_BOX
1381 && !face->box_color_defaulted_p)
1383 x_free_colors (f, &face->box_color, 1);
1384 IF_DEBUG (--ncolors_allocated);
1387 unblock_input ();
1388 #endif /* HAVE_X_WINDOWS */
1391 #endif /* HAVE_WINDOW_SYSTEM */
1395 /***********************************************************************
1396 XLFD Font Names
1397 ***********************************************************************/
1399 /* An enumerator for each field of an XLFD font name. */
1401 enum xlfd_field
1403 XLFD_FOUNDRY,
1404 XLFD_FAMILY,
1405 XLFD_WEIGHT,
1406 XLFD_SLANT,
1407 XLFD_SWIDTH,
1408 XLFD_ADSTYLE,
1409 XLFD_PIXEL_SIZE,
1410 XLFD_POINT_SIZE,
1411 XLFD_RESX,
1412 XLFD_RESY,
1413 XLFD_SPACING,
1414 XLFD_AVGWIDTH,
1415 XLFD_REGISTRY,
1416 XLFD_ENCODING,
1417 XLFD_LAST
1420 /* An enumerator for each possible slant value of a font. Taken from
1421 the XLFD specification. */
1423 enum xlfd_slant
1425 XLFD_SLANT_UNKNOWN,
1426 XLFD_SLANT_ROMAN,
1427 XLFD_SLANT_ITALIC,
1428 XLFD_SLANT_OBLIQUE,
1429 XLFD_SLANT_REVERSE_ITALIC,
1430 XLFD_SLANT_REVERSE_OBLIQUE,
1431 XLFD_SLANT_OTHER
1434 /* Relative font weight according to XLFD documentation. */
1436 enum xlfd_weight
1438 XLFD_WEIGHT_UNKNOWN,
1439 XLFD_WEIGHT_ULTRA_LIGHT, /* 10 */
1440 XLFD_WEIGHT_EXTRA_LIGHT, /* 20 */
1441 XLFD_WEIGHT_LIGHT, /* 30 */
1442 XLFD_WEIGHT_SEMI_LIGHT, /* 40: SemiLight, Book, ... */
1443 XLFD_WEIGHT_MEDIUM, /* 50: Medium, Normal, Regular, ... */
1444 XLFD_WEIGHT_SEMI_BOLD, /* 60: SemiBold, DemiBold, ... */
1445 XLFD_WEIGHT_BOLD, /* 70: Bold, ... */
1446 XLFD_WEIGHT_EXTRA_BOLD, /* 80: ExtraBold, Heavy, ... */
1447 XLFD_WEIGHT_ULTRA_BOLD /* 90: UltraBold, Black, ... */
1450 /* Relative proportionate width. */
1452 enum xlfd_swidth
1454 XLFD_SWIDTH_UNKNOWN,
1455 XLFD_SWIDTH_ULTRA_CONDENSED, /* 10 */
1456 XLFD_SWIDTH_EXTRA_CONDENSED, /* 20 */
1457 XLFD_SWIDTH_CONDENSED, /* 30: Condensed, Narrow, Compressed, ... */
1458 XLFD_SWIDTH_SEMI_CONDENSED, /* 40: semicondensed */
1459 XLFD_SWIDTH_MEDIUM, /* 50: Medium, Normal, Regular, ... */
1460 XLFD_SWIDTH_SEMI_EXPANDED, /* 60: SemiExpanded, DemiExpanded, ... */
1461 XLFD_SWIDTH_EXPANDED, /* 70: Expanded... */
1462 XLFD_SWIDTH_EXTRA_EXPANDED, /* 80: ExtraExpanded, Wide... */
1463 XLFD_SWIDTH_ULTRA_EXPANDED /* 90: UltraExpanded... */
1466 /* Order by which font selection chooses fonts. The default values
1467 mean `first, find a best match for the font width, then for the
1468 font height, then for weight, then for slant.' This variable can be
1469 set via set-face-font-sort-order. */
1471 static int font_sort_order[4];
1473 #ifdef HAVE_WINDOW_SYSTEM
1475 static enum font_property_index font_props_for_sorting[FONT_SIZE_INDEX];
1477 static int
1478 compare_fonts_by_sort_order (const void *v1, const void *v2)
1480 Lisp_Object const *p1 = v1;
1481 Lisp_Object const *p2 = v2;
1482 Lisp_Object font1 = *p1;
1483 Lisp_Object font2 = *p2;
1484 int i;
1486 for (i = 0; i < FONT_SIZE_INDEX; i++)
1488 enum font_property_index idx = font_props_for_sorting[i];
1489 Lisp_Object val1 = AREF (font1, idx), val2 = AREF (font2, idx);
1490 int result;
1492 if (idx <= FONT_REGISTRY_INDEX)
1494 if (STRINGP (val1))
1495 result = STRINGP (val2) ? strcmp (SSDATA (val1), SSDATA (val2)) : -1;
1496 else
1497 result = STRINGP (val2) ? 1 : 0;
1499 else
1501 if (INTEGERP (val1))
1502 result = (INTEGERP (val2) && XINT (val1) >= XINT (val2)
1503 ? XINT (val1) > XINT (val2)
1504 : -1);
1505 else
1506 result = INTEGERP (val2) ? 1 : 0;
1508 if (result)
1509 return result;
1511 return 0;
1514 DEFUN ("x-family-fonts", Fx_family_fonts, Sx_family_fonts, 0, 2, 0,
1515 doc: /* Return a list of available fonts of family FAMILY on FRAME.
1516 If FAMILY is omitted or nil, list all families.
1517 Otherwise, FAMILY must be a string, possibly containing wildcards
1518 `?' and `*'.
1519 If FRAME is omitted or nil, use the selected frame.
1520 Each element of the result is a vector [FAMILY WIDTH POINT-SIZE WEIGHT
1521 SLANT FIXED-P FULL REGISTRY-AND-ENCODING].
1522 FAMILY is the font family name. POINT-SIZE is the size of the
1523 font in 1/10 pt. WIDTH, WEIGHT, and SLANT are symbols describing the
1524 width, weight and slant of the font. These symbols are the same as for
1525 face attributes. FIXED-P is non-nil if the font is fixed-pitch.
1526 FULL is the full name of the font, and REGISTRY-AND-ENCODING is a string
1527 giving the registry and encoding of the font.
1528 The result list is sorted according to the current setting of
1529 the face font sort order. */)
1530 (Lisp_Object family, Lisp_Object frame)
1532 Lisp_Object font_spec, list, *drivers, vec;
1533 struct frame *f = decode_live_frame (frame);
1534 ptrdiff_t i, nfonts;
1535 EMACS_INT ndrivers;
1536 Lisp_Object result;
1537 USE_SAFE_ALLOCA;
1539 font_spec = Ffont_spec (0, NULL);
1540 if (!NILP (family))
1542 CHECK_STRING (family);
1543 font_parse_family_registry (family, Qnil, font_spec);
1546 list = font_list_entities (f, font_spec);
1547 if (NILP (list))
1548 return Qnil;
1550 /* Sort the font entities. */
1551 for (i = 0; i < 4; i++)
1552 switch (font_sort_order[i])
1554 case XLFD_SWIDTH:
1555 font_props_for_sorting[i] = FONT_WIDTH_INDEX; break;
1556 case XLFD_POINT_SIZE:
1557 font_props_for_sorting[i] = FONT_SIZE_INDEX; break;
1558 case XLFD_WEIGHT:
1559 font_props_for_sorting[i] = FONT_WEIGHT_INDEX; break;
1560 default:
1561 font_props_for_sorting[i] = FONT_SLANT_INDEX; break;
1563 font_props_for_sorting[i++] = FONT_FAMILY_INDEX;
1564 font_props_for_sorting[i++] = FONT_FOUNDRY_INDEX;
1565 font_props_for_sorting[i++] = FONT_ADSTYLE_INDEX;
1566 font_props_for_sorting[i++] = FONT_REGISTRY_INDEX;
1568 ndrivers = XINT (Flength (list));
1569 SAFE_ALLOCA_LISP (drivers, ndrivers);
1570 for (i = 0; i < ndrivers; i++, list = XCDR (list))
1571 drivers[i] = XCAR (list);
1572 vec = Fvconcat (ndrivers, drivers);
1573 nfonts = ASIZE (vec);
1575 qsort (XVECTOR (vec)->contents, nfonts, word_size,
1576 compare_fonts_by_sort_order);
1578 result = Qnil;
1579 for (i = nfonts - 1; i >= 0; --i)
1581 Lisp_Object font = AREF (vec, i);
1582 Lisp_Object v = make_uninit_vector (8);
1583 int point;
1584 Lisp_Object spacing;
1586 ASET (v, 0, AREF (font, FONT_FAMILY_INDEX));
1587 ASET (v, 1, FONT_WIDTH_SYMBOLIC (font));
1588 point = PIXEL_TO_POINT (XINT (AREF (font, FONT_SIZE_INDEX)) * 10,
1589 FRAME_RES_Y (f));
1590 ASET (v, 2, make_number (point));
1591 ASET (v, 3, FONT_WEIGHT_SYMBOLIC (font));
1592 ASET (v, 4, FONT_SLANT_SYMBOLIC (font));
1593 spacing = Ffont_get (font, QCspacing);
1594 ASET (v, 5, (NILP (spacing) || EQ (spacing, Qp)) ? Qnil : Qt);
1595 ASET (v, 6, Ffont_xlfd_name (font, Qnil));
1596 ASET (v, 7, AREF (font, FONT_REGISTRY_INDEX));
1598 result = Fcons (v, result);
1601 SAFE_FREE ();
1602 return result;
1605 DEFUN ("x-list-fonts", Fx_list_fonts, Sx_list_fonts, 1, 5, 0,
1606 doc: /* Return a list of the names of available fonts matching PATTERN.
1607 If optional arguments FACE and FRAME are specified, return only fonts
1608 the same size as FACE on FRAME.
1610 PATTERN should be a string containing a font name in the XLFD,
1611 Fontconfig, or GTK format. A font name given in the XLFD format may
1612 contain wildcard characters:
1613 the * character matches any substring, and
1614 the ? character matches any single character.
1615 PATTERN is case-insensitive.
1617 The return value is a list of strings, suitable as arguments to
1618 `set-face-font'.
1620 Fonts Emacs can't use may or may not be excluded
1621 even if they match PATTERN and FACE.
1622 The optional fourth argument MAXIMUM sets a limit on how many
1623 fonts to match. The first MAXIMUM fonts are reported.
1624 The optional fifth argument WIDTH, if specified, is a number of columns
1625 occupied by a character of a font. In that case, return only fonts
1626 the WIDTH times as wide as FACE on FRAME. */)
1627 (Lisp_Object pattern, Lisp_Object face, Lisp_Object frame,
1628 Lisp_Object maximum, Lisp_Object width)
1630 struct frame *f;
1631 int size, avgwidth IF_LINT (= 0);
1633 check_window_system (NULL);
1634 CHECK_STRING (pattern);
1636 if (! NILP (maximum))
1637 CHECK_NATNUM (maximum);
1639 if (!NILP (width))
1640 CHECK_NUMBER (width);
1642 /* We can't simply call decode_window_system_frame because
1643 this function may be called before any frame is created. */
1644 f = decode_live_frame (frame);
1645 if (! FRAME_WINDOW_P (f))
1647 /* Perhaps we have not yet created any frame. */
1648 f = NULL;
1649 frame = Qnil;
1650 face = Qnil;
1652 else
1653 XSETFRAME (frame, f);
1655 /* Determine the width standard for comparison with the fonts we find. */
1657 if (NILP (face))
1658 size = 0;
1659 else
1661 /* This is of limited utility since it works with character
1662 widths. Keep it for compatibility. --gerd. */
1663 int face_id = lookup_named_face (f, face, 0);
1664 struct face *width_face = (face_id < 0
1665 ? NULL
1666 : FACE_FROM_ID (f, face_id));
1668 if (width_face && width_face->font)
1670 size = width_face->font->pixel_size;
1671 avgwidth = width_face->font->average_width;
1673 else
1675 size = FRAME_FONT (f)->pixel_size;
1676 avgwidth = FRAME_FONT (f)->average_width;
1678 if (!NILP (width))
1679 avgwidth *= XINT (width);
1683 Lisp_Object font_spec;
1684 Lisp_Object args[2], tail;
1686 font_spec = font_spec_from_name (pattern);
1687 if (!FONTP (font_spec))
1688 signal_error ("Invalid font name", pattern);
1690 if (size)
1692 Ffont_put (font_spec, QCsize, make_number (size));
1693 Ffont_put (font_spec, QCavgwidth, make_number (avgwidth));
1695 args[0] = Flist_fonts (font_spec, frame, maximum, font_spec);
1696 for (tail = args[0]; CONSP (tail); tail = XCDR (tail))
1698 Lisp_Object font_entity;
1700 font_entity = XCAR (tail);
1701 if ((NILP (AREF (font_entity, FONT_SIZE_INDEX))
1702 || XINT (AREF (font_entity, FONT_SIZE_INDEX)) == 0)
1703 && ! NILP (AREF (font_spec, FONT_SIZE_INDEX)))
1705 /* This is a scalable font. For backward compatibility,
1706 we set the specified size. */
1707 font_entity = copy_font_spec (font_entity);
1708 ASET (font_entity, FONT_SIZE_INDEX,
1709 AREF (font_spec, FONT_SIZE_INDEX));
1711 XSETCAR (tail, Ffont_xlfd_name (font_entity, Qnil));
1713 if (NILP (frame))
1714 /* We don't have to check fontsets. */
1715 return args[0];
1716 args[1] = list_fontsets (f, pattern, size);
1717 return Fnconc (2, args);
1721 #endif /* HAVE_WINDOW_SYSTEM */
1724 /***********************************************************************
1725 Lisp Faces
1726 ***********************************************************************/
1728 /* Access face attributes of face LFACE, a Lisp vector. */
1730 #define LFACE_FAMILY(LFACE) AREF ((LFACE), LFACE_FAMILY_INDEX)
1731 #define LFACE_FOUNDRY(LFACE) AREF ((LFACE), LFACE_FOUNDRY_INDEX)
1732 #define LFACE_HEIGHT(LFACE) AREF ((LFACE), LFACE_HEIGHT_INDEX)
1733 #define LFACE_WEIGHT(LFACE) AREF ((LFACE), LFACE_WEIGHT_INDEX)
1734 #define LFACE_SLANT(LFACE) AREF ((LFACE), LFACE_SLANT_INDEX)
1735 #define LFACE_UNDERLINE(LFACE) AREF ((LFACE), LFACE_UNDERLINE_INDEX)
1736 #define LFACE_INVERSE(LFACE) AREF ((LFACE), LFACE_INVERSE_INDEX)
1737 #define LFACE_FOREGROUND(LFACE) AREF ((LFACE), LFACE_FOREGROUND_INDEX)
1738 #define LFACE_BACKGROUND(LFACE) AREF ((LFACE), LFACE_BACKGROUND_INDEX)
1739 #define LFACE_STIPPLE(LFACE) AREF ((LFACE), LFACE_STIPPLE_INDEX)
1740 #define LFACE_SWIDTH(LFACE) AREF ((LFACE), LFACE_SWIDTH_INDEX)
1741 #define LFACE_OVERLINE(LFACE) AREF ((LFACE), LFACE_OVERLINE_INDEX)
1742 #define LFACE_STRIKE_THROUGH(LFACE) AREF ((LFACE), LFACE_STRIKE_THROUGH_INDEX)
1743 #define LFACE_BOX(LFACE) AREF ((LFACE), LFACE_BOX_INDEX)
1744 #define LFACE_FONT(LFACE) AREF ((LFACE), LFACE_FONT_INDEX)
1745 #define LFACE_INHERIT(LFACE) AREF ((LFACE), LFACE_INHERIT_INDEX)
1746 #define LFACE_FONTSET(LFACE) AREF ((LFACE), LFACE_FONTSET_INDEX)
1748 /* Non-zero if LFACE is a Lisp face. A Lisp face is a vector of size
1749 LFACE_VECTOR_SIZE which has the symbol `face' in slot 0. */
1751 #define LFACEP(LFACE) \
1752 (VECTORP (LFACE) \
1753 && ASIZE (LFACE) == LFACE_VECTOR_SIZE \
1754 && EQ (AREF (LFACE, 0), Qface))
1757 #ifdef GLYPH_DEBUG
1759 /* Check consistency of Lisp face attribute vector ATTRS. */
1761 static void
1762 check_lface_attrs (Lisp_Object attrs[LFACE_VECTOR_SIZE])
1764 eassert (UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
1765 || IGNORE_DEFFACE_P (attrs[LFACE_FAMILY_INDEX])
1766 || STRINGP (attrs[LFACE_FAMILY_INDEX]));
1767 eassert (UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
1768 || IGNORE_DEFFACE_P (attrs[LFACE_FOUNDRY_INDEX])
1769 || STRINGP (attrs[LFACE_FOUNDRY_INDEX]));
1770 eassert (UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
1771 || IGNORE_DEFFACE_P (attrs[LFACE_SWIDTH_INDEX])
1772 || SYMBOLP (attrs[LFACE_SWIDTH_INDEX]));
1773 eassert (UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
1774 || IGNORE_DEFFACE_P (attrs[LFACE_HEIGHT_INDEX])
1775 || INTEGERP (attrs[LFACE_HEIGHT_INDEX])
1776 || FLOATP (attrs[LFACE_HEIGHT_INDEX])
1777 || FUNCTIONP (attrs[LFACE_HEIGHT_INDEX]));
1778 eassert (UNSPECIFIEDP (attrs[LFACE_WEIGHT_INDEX])
1779 || IGNORE_DEFFACE_P (attrs[LFACE_WEIGHT_INDEX])
1780 || SYMBOLP (attrs[LFACE_WEIGHT_INDEX]));
1781 eassert (UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX])
1782 || IGNORE_DEFFACE_P (attrs[LFACE_SLANT_INDEX])
1783 || SYMBOLP (attrs[LFACE_SLANT_INDEX]));
1784 eassert (UNSPECIFIEDP (attrs[LFACE_UNDERLINE_INDEX])
1785 || IGNORE_DEFFACE_P (attrs[LFACE_UNDERLINE_INDEX])
1786 || SYMBOLP (attrs[LFACE_UNDERLINE_INDEX])
1787 || STRINGP (attrs[LFACE_UNDERLINE_INDEX])
1788 || CONSP (attrs[LFACE_UNDERLINE_INDEX]));
1789 eassert (UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
1790 || IGNORE_DEFFACE_P (attrs[LFACE_OVERLINE_INDEX])
1791 || SYMBOLP (attrs[LFACE_OVERLINE_INDEX])
1792 || STRINGP (attrs[LFACE_OVERLINE_INDEX]));
1793 eassert (UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
1794 || IGNORE_DEFFACE_P (attrs[LFACE_STRIKE_THROUGH_INDEX])
1795 || SYMBOLP (attrs[LFACE_STRIKE_THROUGH_INDEX])
1796 || STRINGP (attrs[LFACE_STRIKE_THROUGH_INDEX]));
1797 eassert (UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
1798 || IGNORE_DEFFACE_P (attrs[LFACE_BOX_INDEX])
1799 || SYMBOLP (attrs[LFACE_BOX_INDEX])
1800 || STRINGP (attrs[LFACE_BOX_INDEX])
1801 || INTEGERP (attrs[LFACE_BOX_INDEX])
1802 || CONSP (attrs[LFACE_BOX_INDEX]));
1803 eassert (UNSPECIFIEDP (attrs[LFACE_INVERSE_INDEX])
1804 || IGNORE_DEFFACE_P (attrs[LFACE_INVERSE_INDEX])
1805 || SYMBOLP (attrs[LFACE_INVERSE_INDEX]));
1806 eassert (UNSPECIFIEDP (attrs[LFACE_FOREGROUND_INDEX])
1807 || IGNORE_DEFFACE_P (attrs[LFACE_FOREGROUND_INDEX])
1808 || STRINGP (attrs[LFACE_FOREGROUND_INDEX]));
1809 eassert (UNSPECIFIEDP (attrs[LFACE_BACKGROUND_INDEX])
1810 || IGNORE_DEFFACE_P (attrs[LFACE_BACKGROUND_INDEX])
1811 || STRINGP (attrs[LFACE_BACKGROUND_INDEX]));
1812 eassert (UNSPECIFIEDP (attrs[LFACE_INHERIT_INDEX])
1813 || IGNORE_DEFFACE_P (attrs[LFACE_INHERIT_INDEX])
1814 || NILP (attrs[LFACE_INHERIT_INDEX])
1815 || SYMBOLP (attrs[LFACE_INHERIT_INDEX])
1816 || CONSP (attrs[LFACE_INHERIT_INDEX]));
1817 #ifdef HAVE_WINDOW_SYSTEM
1818 eassert (UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
1819 || IGNORE_DEFFACE_P (attrs[LFACE_STIPPLE_INDEX])
1820 || SYMBOLP (attrs[LFACE_STIPPLE_INDEX])
1821 || !NILP (Fbitmap_spec_p (attrs[LFACE_STIPPLE_INDEX])));
1822 eassert (UNSPECIFIEDP (attrs[LFACE_FONT_INDEX])
1823 || IGNORE_DEFFACE_P (attrs[LFACE_FONT_INDEX])
1824 || FONTP (attrs[LFACE_FONT_INDEX]));
1825 eassert (UNSPECIFIEDP (attrs[LFACE_FONTSET_INDEX])
1826 || STRINGP (attrs[LFACE_FONTSET_INDEX])
1827 || NILP (attrs[LFACE_FONTSET_INDEX]));
1828 #endif
1832 /* Check consistency of attributes of Lisp face LFACE (a Lisp vector). */
1834 static void
1835 check_lface (Lisp_Object lface)
1837 if (!NILP (lface))
1839 eassert (LFACEP (lface));
1840 check_lface_attrs (XVECTOR (lface)->contents);
1844 #else /* not GLYPH_DEBUG */
1846 #define check_lface_attrs(attrs) (void) 0
1847 #define check_lface(lface) (void) 0
1849 #endif /* GLYPH_DEBUG */
1853 /* Face-merge cycle checking. */
1855 enum named_merge_point_kind
1857 NAMED_MERGE_POINT_NORMAL,
1858 NAMED_MERGE_POINT_REMAP
1861 /* A `named merge point' is simply a point during face-merging where we
1862 look up a face by name. We keep a stack of which named lookups we're
1863 currently processing so that we can easily detect cycles, using a
1864 linked- list of struct named_merge_point structures, typically
1865 allocated on the stack frame of the named lookup functions which are
1866 active (so no consing is required). */
1867 struct named_merge_point
1869 Lisp_Object face_name;
1870 enum named_merge_point_kind named_merge_point_kind;
1871 struct named_merge_point *prev;
1875 /* If a face merging cycle is detected for FACE_NAME, return 0,
1876 otherwise add NEW_NAMED_MERGE_POINT, which is initialized using
1877 FACE_NAME and NAMED_MERGE_POINT_KIND, as the head of the linked list
1878 pointed to by NAMED_MERGE_POINTS, and return 1. */
1880 static int
1881 push_named_merge_point (struct named_merge_point *new_named_merge_point,
1882 Lisp_Object face_name,
1883 enum named_merge_point_kind named_merge_point_kind,
1884 struct named_merge_point **named_merge_points)
1886 struct named_merge_point *prev;
1888 for (prev = *named_merge_points; prev; prev = prev->prev)
1889 if (EQ (face_name, prev->face_name))
1891 if (prev->named_merge_point_kind == named_merge_point_kind)
1892 /* A cycle, so fail. */
1893 return 0;
1894 else if (prev->named_merge_point_kind == NAMED_MERGE_POINT_REMAP)
1895 /* A remap `hides ' any previous normal merge points
1896 (because the remap means that it's actually different face),
1897 so as we know the current merge point must be normal, we
1898 can just assume it's OK. */
1899 break;
1902 new_named_merge_point->face_name = face_name;
1903 new_named_merge_point->named_merge_point_kind = named_merge_point_kind;
1904 new_named_merge_point->prev = *named_merge_points;
1906 *named_merge_points = new_named_merge_point;
1908 return 1;
1912 /* Resolve face name FACE_NAME. If FACE_NAME is a string, intern it
1913 to make it a symbol. If FACE_NAME is an alias for another face,
1914 return that face's name.
1916 Return default face in case of errors. */
1918 static Lisp_Object
1919 resolve_face_name (Lisp_Object face_name, int signal_p)
1921 Lisp_Object orig_face;
1922 Lisp_Object tortoise, hare;
1924 if (STRINGP (face_name))
1925 face_name = intern (SSDATA (face_name));
1927 if (NILP (face_name) || !SYMBOLP (face_name))
1928 return face_name;
1930 orig_face = face_name;
1931 tortoise = hare = face_name;
1933 while (1)
1935 face_name = hare;
1936 hare = Fget (hare, Qface_alias);
1937 if (NILP (hare) || !SYMBOLP (hare))
1938 break;
1940 face_name = hare;
1941 hare = Fget (hare, Qface_alias);
1942 if (NILP (hare) || !SYMBOLP (hare))
1943 break;
1945 tortoise = Fget (tortoise, Qface_alias);
1946 if (EQ (hare, tortoise))
1948 if (signal_p)
1949 xsignal1 (Qcircular_list, orig_face);
1950 return Qdefault;
1954 return face_name;
1958 /* Return the face definition of FACE_NAME on frame F. F null means
1959 return the definition for new frames. FACE_NAME may be a string or
1960 a symbol (apparently Emacs 20.2 allowed strings as face names in
1961 face text properties; Ediff uses that). If SIGNAL_P is non-zero,
1962 signal an error if FACE_NAME is not a valid face name. If SIGNAL_P
1963 is zero, value is nil if FACE_NAME is not a valid face name. */
1964 static Lisp_Object
1965 lface_from_face_name_no_resolve (struct frame *f, Lisp_Object face_name,
1966 int signal_p)
1968 Lisp_Object lface;
1970 if (f)
1971 lface = assq_no_quit (face_name, f->face_alist);
1972 else
1973 lface = assq_no_quit (face_name, Vface_new_frame_defaults);
1975 if (CONSP (lface))
1976 lface = XCDR (lface);
1977 else if (signal_p)
1978 signal_error ("Invalid face", face_name);
1980 check_lface (lface);
1982 return lface;
1985 /* Return the face definition of FACE_NAME on frame F. F null means
1986 return the definition for new frames. FACE_NAME may be a string or
1987 a symbol (apparently Emacs 20.2 allowed strings as face names in
1988 face text properties; Ediff uses that). If FACE_NAME is an alias
1989 for another face, return that face's definition. If SIGNAL_P is
1990 non-zero, signal an error if FACE_NAME is not a valid face name.
1991 If SIGNAL_P is zero, value is nil if FACE_NAME is not a valid face
1992 name. */
1993 static Lisp_Object
1994 lface_from_face_name (struct frame *f, Lisp_Object face_name, int signal_p)
1996 face_name = resolve_face_name (face_name, signal_p);
1997 return lface_from_face_name_no_resolve (f, face_name, signal_p);
2001 /* Get face attributes of face FACE_NAME from frame-local faces on
2002 frame F. Store the resulting attributes in ATTRS which must point
2003 to a vector of Lisp_Objects of size LFACE_VECTOR_SIZE. If SIGNAL_P
2004 is non-zero, signal an error if FACE_NAME does not name a face.
2005 Otherwise, value is zero if FACE_NAME is not a face. */
2007 static int
2008 get_lface_attributes_no_remap (struct frame *f, Lisp_Object face_name,
2009 Lisp_Object attrs[LFACE_VECTOR_SIZE],
2010 int signal_p)
2012 Lisp_Object lface;
2014 lface = lface_from_face_name_no_resolve (f, face_name, signal_p);
2016 if (! NILP (lface))
2017 memcpy (attrs, XVECTOR (lface)->contents,
2018 LFACE_VECTOR_SIZE * sizeof *attrs);
2020 return !NILP (lface);
2023 /* Get face attributes of face FACE_NAME from frame-local faces on frame
2024 F. Store the resulting attributes in ATTRS which must point to a
2025 vector of Lisp_Objects of size LFACE_VECTOR_SIZE. If FACE_NAME is an
2026 alias for another face, use that face's definition. If SIGNAL_P is
2027 non-zero, signal an error if FACE_NAME does not name a face.
2028 Otherwise, value is zero if FACE_NAME is not a face. */
2030 static int
2031 get_lface_attributes (struct frame *f, Lisp_Object face_name,
2032 Lisp_Object attrs[LFACE_VECTOR_SIZE], int signal_p,
2033 struct named_merge_point *named_merge_points)
2035 Lisp_Object face_remapping;
2037 face_name = resolve_face_name (face_name, signal_p);
2039 /* See if SYMBOL has been remapped to some other face (usually this
2040 is done buffer-locally). */
2041 face_remapping = assq_no_quit (face_name, Vface_remapping_alist);
2042 if (CONSP (face_remapping))
2044 struct named_merge_point named_merge_point;
2046 if (push_named_merge_point (&named_merge_point,
2047 face_name, NAMED_MERGE_POINT_REMAP,
2048 &named_merge_points))
2050 int i;
2052 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2053 attrs[i] = Qunspecified;
2055 return merge_face_ref (f, XCDR (face_remapping), attrs,
2056 signal_p, named_merge_points);
2060 /* Default case, no remapping. */
2061 return get_lface_attributes_no_remap (f, face_name, attrs, signal_p);
2065 /* Non-zero if all attributes in face attribute vector ATTRS are
2066 specified, i.e. are non-nil. */
2068 static int
2069 lface_fully_specified_p (Lisp_Object attrs[LFACE_VECTOR_SIZE])
2071 int i;
2073 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2074 if (i != LFACE_FONT_INDEX && i != LFACE_INHERIT_INDEX)
2075 if ((UNSPECIFIEDP (attrs[i]) || IGNORE_DEFFACE_P (attrs[i])))
2076 break;
2078 return i == LFACE_VECTOR_SIZE;
2081 #ifdef HAVE_WINDOW_SYSTEM
2083 /* Set font-related attributes of Lisp face LFACE from FONT-OBJECT.
2084 If FORCE_P is zero, set only unspecified attributes of LFACE. The
2085 exception is `font' attribute. It is set to FONT_OBJECT regardless
2086 of FORCE_P. */
2088 static int
2089 set_lface_from_font (struct frame *f, Lisp_Object lface,
2090 Lisp_Object font_object, int force_p)
2092 Lisp_Object val;
2093 struct font *font = XFONT_OBJECT (font_object);
2095 /* Set attributes only if unspecified, otherwise face defaults for
2096 new frames would never take effect. If the font doesn't have a
2097 specific property, set a normal value for that. */
2099 if (force_p || UNSPECIFIEDP (LFACE_FAMILY (lface)))
2101 Lisp_Object family = AREF (font_object, FONT_FAMILY_INDEX);
2103 ASET (lface, LFACE_FAMILY_INDEX, SYMBOL_NAME (family));
2106 if (force_p || UNSPECIFIEDP (LFACE_FOUNDRY (lface)))
2108 Lisp_Object foundry = AREF (font_object, FONT_FOUNDRY_INDEX);
2110 ASET (lface, LFACE_FOUNDRY_INDEX, SYMBOL_NAME (foundry));
2113 if (force_p || UNSPECIFIEDP (LFACE_HEIGHT (lface)))
2115 int pt = PIXEL_TO_POINT (font->pixel_size * 10, FRAME_RES_Y (f));
2117 eassert (pt > 0);
2118 ASET (lface, LFACE_HEIGHT_INDEX, make_number (pt));
2121 if (force_p || UNSPECIFIEDP (LFACE_WEIGHT (lface)))
2123 val = FONT_WEIGHT_FOR_FACE (font_object);
2124 ASET (lface, LFACE_WEIGHT_INDEX, ! NILP (val) ? val :Qnormal);
2126 if (force_p || UNSPECIFIEDP (LFACE_SLANT (lface)))
2128 val = FONT_SLANT_FOR_FACE (font_object);
2129 ASET (lface, LFACE_SLANT_INDEX, ! NILP (val) ? val : Qnormal);
2131 if (force_p || UNSPECIFIEDP (LFACE_SWIDTH (lface)))
2133 val = FONT_WIDTH_FOR_FACE (font_object);
2134 ASET (lface, LFACE_SWIDTH_INDEX, ! NILP (val) ? val : Qnormal);
2137 ASET (lface, LFACE_FONT_INDEX, font_object);
2138 return 1;
2141 #endif /* HAVE_WINDOW_SYSTEM */
2144 /* Merges the face height FROM with the face height TO, and returns the
2145 merged height. If FROM is an invalid height, then INVALID is
2146 returned instead. FROM and TO may be either absolute face heights or
2147 `relative' heights; the returned value is always an absolute height
2148 unless both FROM and TO are relative. */
2150 static Lisp_Object
2151 merge_face_heights (Lisp_Object from, Lisp_Object to, Lisp_Object invalid)
2153 Lisp_Object result = invalid;
2155 if (INTEGERP (from))
2156 /* FROM is absolute, just use it as is. */
2157 result = from;
2158 else if (FLOATP (from))
2159 /* FROM is a scale, use it to adjust TO. */
2161 if (INTEGERP (to))
2162 /* relative X absolute => absolute */
2163 result = make_number (XFLOAT_DATA (from) * XINT (to));
2164 else if (FLOATP (to))
2165 /* relative X relative => relative */
2166 result = make_float (XFLOAT_DATA (from) * XFLOAT_DATA (to));
2167 else if (UNSPECIFIEDP (to))
2168 result = from;
2170 else if (FUNCTIONP (from))
2171 /* FROM is a function, which use to adjust TO. */
2173 /* Call function with current height as argument.
2174 From is the new height. */
2175 result = safe_call1 (from, to);
2177 /* Ensure that if TO was absolute, so is the result. */
2178 if (INTEGERP (to) && !INTEGERP (result))
2179 result = invalid;
2182 return result;
2186 /* Merge two Lisp face attribute vectors on frame F, FROM and TO, and
2187 store the resulting attributes in TO, which must be already be
2188 completely specified and contain only absolute attributes. Every
2189 specified attribute of FROM overrides the corresponding attribute of
2190 TO; relative attributes in FROM are merged with the absolute value in
2191 TO and replace it. NAMED_MERGE_POINTS is used internally to detect
2192 loops in face inheritance/remapping; it should be 0 when called from
2193 other places. */
2195 static void
2196 merge_face_vectors (struct frame *f, Lisp_Object *from, Lisp_Object *to,
2197 struct named_merge_point *named_merge_points)
2199 int i;
2200 Lisp_Object font = Qnil;
2202 /* If FROM inherits from some other faces, merge their attributes into
2203 TO before merging FROM's direct attributes. Note that an :inherit
2204 attribute of `unspecified' is the same as one of nil; we never
2205 merge :inherit attributes, so nil is more correct, but lots of
2206 other code uses `unspecified' as a generic value for face attributes. */
2207 if (!UNSPECIFIEDP (from[LFACE_INHERIT_INDEX])
2208 && !NILP (from[LFACE_INHERIT_INDEX]))
2209 merge_face_ref (f, from[LFACE_INHERIT_INDEX], to, 0, named_merge_points);
2211 if (FONT_SPEC_P (from[LFACE_FONT_INDEX]))
2213 if (!UNSPECIFIEDP (to[LFACE_FONT_INDEX]))
2214 font = merge_font_spec (from[LFACE_FONT_INDEX], to[LFACE_FONT_INDEX]);
2215 else
2216 font = copy_font_spec (from[LFACE_FONT_INDEX]);
2217 to[LFACE_FONT_INDEX] = font;
2220 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2221 if (!UNSPECIFIEDP (from[i]))
2223 if (i == LFACE_HEIGHT_INDEX && !INTEGERP (from[i]))
2225 to[i] = merge_face_heights (from[i], to[i], to[i]);
2226 font_clear_prop (to, FONT_SIZE_INDEX);
2228 else if (i != LFACE_FONT_INDEX && ! EQ (to[i], from[i]))
2230 to[i] = from[i];
2231 if (i >= LFACE_FAMILY_INDEX && i <=LFACE_SLANT_INDEX)
2232 font_clear_prop (to,
2233 (i == LFACE_FAMILY_INDEX ? FONT_FAMILY_INDEX
2234 : i == LFACE_FOUNDRY_INDEX ? FONT_FOUNDRY_INDEX
2235 : i == LFACE_SWIDTH_INDEX ? FONT_WIDTH_INDEX
2236 : i == LFACE_HEIGHT_INDEX ? FONT_SIZE_INDEX
2237 : i == LFACE_WEIGHT_INDEX ? FONT_WEIGHT_INDEX
2238 : FONT_SLANT_INDEX));
2242 /* If FROM specifies a font spec, make its contents take precedence
2243 over :family and other attributes. This is needed for face
2244 remapping using :font to work. */
2246 if (!NILP (font))
2248 if (! NILP (AREF (font, FONT_FOUNDRY_INDEX)))
2249 to[LFACE_FOUNDRY_INDEX] = SYMBOL_NAME (AREF (font, FONT_FOUNDRY_INDEX));
2250 if (! NILP (AREF (font, FONT_FAMILY_INDEX)))
2251 to[LFACE_FAMILY_INDEX] = SYMBOL_NAME (AREF (font, FONT_FAMILY_INDEX));
2252 if (! NILP (AREF (font, FONT_WEIGHT_INDEX)))
2253 to[LFACE_WEIGHT_INDEX] = FONT_WEIGHT_FOR_FACE (font);
2254 if (! NILP (AREF (font, FONT_SLANT_INDEX)))
2255 to[LFACE_SLANT_INDEX] = FONT_SLANT_FOR_FACE (font);
2256 if (! NILP (AREF (font, FONT_WIDTH_INDEX)))
2257 to[LFACE_SWIDTH_INDEX] = FONT_WIDTH_FOR_FACE (font);
2258 ASET (font, FONT_SIZE_INDEX, Qnil);
2261 /* TO is always an absolute face, which should inherit from nothing.
2262 We blindly copy the :inherit attribute above and fix it up here. */
2263 to[LFACE_INHERIT_INDEX] = Qnil;
2266 /* Merge the named face FACE_NAME on frame F, into the vector of face
2267 attributes TO. NAMED_MERGE_POINTS is used to detect loops in face
2268 inheritance. Returns true if FACE_NAME is a valid face name and
2269 merging succeeded. */
2271 static int
2272 merge_named_face (struct frame *f, Lisp_Object face_name, Lisp_Object *to,
2273 struct named_merge_point *named_merge_points)
2275 struct named_merge_point named_merge_point;
2277 if (push_named_merge_point (&named_merge_point,
2278 face_name, NAMED_MERGE_POINT_NORMAL,
2279 &named_merge_points))
2281 struct gcpro gcpro1;
2282 Lisp_Object from[LFACE_VECTOR_SIZE];
2283 int ok = get_lface_attributes (f, face_name, from, 0, named_merge_points);
2285 if (ok)
2287 GCPRO1 (named_merge_point.face_name);
2288 merge_face_vectors (f, from, to, named_merge_points);
2289 UNGCPRO;
2292 return ok;
2294 else
2295 return 0;
2299 /* Merge face attributes from the lisp `face reference' FACE_REF on
2300 frame F into the face attribute vector TO. If ERR_MSGS is non-zero,
2301 problems with FACE_REF cause an error message to be shown. Return
2302 non-zero if no errors occurred (regardless of the value of ERR_MSGS).
2303 NAMED_MERGE_POINTS is used to detect loops in face inheritance or
2304 list structure; it may be 0 for most callers.
2306 FACE_REF may be a single face specification or a list of such
2307 specifications. Each face specification can be:
2309 1. A symbol or string naming a Lisp face.
2311 2. A property list of the form (KEYWORD VALUE ...) where each
2312 KEYWORD is a face attribute name, and value is an appropriate value
2313 for that attribute.
2315 3. Conses or the form (FOREGROUND-COLOR . COLOR) or
2316 (BACKGROUND-COLOR . COLOR) where COLOR is a color name. This is
2317 for compatibility with 20.2.
2319 Face specifications earlier in lists take precedence over later
2320 specifications. */
2322 static int
2323 merge_face_ref (struct frame *f, Lisp_Object face_ref, Lisp_Object *to,
2324 int err_msgs, struct named_merge_point *named_merge_points)
2326 int ok = 1; /* Succeed without an error? */
2328 if (CONSP (face_ref))
2330 Lisp_Object first = XCAR (face_ref);
2332 if (EQ (first, Qforeground_color)
2333 || EQ (first, Qbackground_color))
2335 /* One of (FOREGROUND-COLOR . COLOR) or (BACKGROUND-COLOR
2336 . COLOR). COLOR must be a string. */
2337 Lisp_Object color_name = XCDR (face_ref);
2338 Lisp_Object color = first;
2340 if (STRINGP (color_name))
2342 if (EQ (color, Qforeground_color))
2343 to[LFACE_FOREGROUND_INDEX] = color_name;
2344 else
2345 to[LFACE_BACKGROUND_INDEX] = color_name;
2347 else
2349 if (err_msgs)
2350 add_to_log ("Invalid face color", color_name, Qnil);
2351 ok = 0;
2354 else if (SYMBOLP (first)
2355 && *SDATA (SYMBOL_NAME (first)) == ':')
2357 /* Assume this is the property list form. */
2358 while (CONSP (face_ref) && CONSP (XCDR (face_ref)))
2360 Lisp_Object keyword = XCAR (face_ref);
2361 Lisp_Object value = XCAR (XCDR (face_ref));
2362 int err = 0;
2364 /* Specifying `unspecified' is a no-op. */
2365 if (EQ (value, Qunspecified))
2367 else if (EQ (keyword, QCfamily))
2369 if (STRINGP (value))
2371 to[LFACE_FAMILY_INDEX] = value;
2372 font_clear_prop (to, FONT_FAMILY_INDEX);
2374 else
2375 err = 1;
2377 else if (EQ (keyword, QCfoundry))
2379 if (STRINGP (value))
2381 to[LFACE_FOUNDRY_INDEX] = value;
2382 font_clear_prop (to, FONT_FOUNDRY_INDEX);
2384 else
2385 err = 1;
2387 else if (EQ (keyword, QCheight))
2389 Lisp_Object new_height =
2390 merge_face_heights (value, to[LFACE_HEIGHT_INDEX], Qnil);
2392 if (! NILP (new_height))
2394 to[LFACE_HEIGHT_INDEX] = new_height;
2395 font_clear_prop (to, FONT_SIZE_INDEX);
2397 else
2398 err = 1;
2400 else if (EQ (keyword, QCweight))
2402 if (SYMBOLP (value) && FONT_WEIGHT_NAME_NUMERIC (value) >= 0)
2404 to[LFACE_WEIGHT_INDEX] = value;
2405 font_clear_prop (to, FONT_WEIGHT_INDEX);
2407 else
2408 err = 1;
2410 else if (EQ (keyword, QCslant))
2412 if (SYMBOLP (value) && FONT_SLANT_NAME_NUMERIC (value) >= 0)
2414 to[LFACE_SLANT_INDEX] = value;
2415 font_clear_prop (to, FONT_SLANT_INDEX);
2417 else
2418 err = 1;
2420 else if (EQ (keyword, QCunderline))
2422 if (EQ (value, Qt)
2423 || NILP (value)
2424 || STRINGP (value)
2425 || CONSP (value))
2426 to[LFACE_UNDERLINE_INDEX] = value;
2427 else
2428 err = 1;
2430 else if (EQ (keyword, QCoverline))
2432 if (EQ (value, Qt)
2433 || NILP (value)
2434 || STRINGP (value))
2435 to[LFACE_OVERLINE_INDEX] = value;
2436 else
2437 err = 1;
2439 else if (EQ (keyword, QCstrike_through))
2441 if (EQ (value, Qt)
2442 || NILP (value)
2443 || STRINGP (value))
2444 to[LFACE_STRIKE_THROUGH_INDEX] = value;
2445 else
2446 err = 1;
2448 else if (EQ (keyword, QCbox))
2450 if (EQ (value, Qt))
2451 value = make_number (1);
2452 if (INTEGERP (value)
2453 || STRINGP (value)
2454 || CONSP (value)
2455 || NILP (value))
2456 to[LFACE_BOX_INDEX] = value;
2457 else
2458 err = 1;
2460 else if (EQ (keyword, QCinverse_video)
2461 || EQ (keyword, QCreverse_video))
2463 if (EQ (value, Qt) || NILP (value))
2464 to[LFACE_INVERSE_INDEX] = value;
2465 else
2466 err = 1;
2468 else if (EQ (keyword, QCforeground))
2470 if (STRINGP (value))
2471 to[LFACE_FOREGROUND_INDEX] = value;
2472 else
2473 err = 1;
2475 else if (EQ (keyword, QCbackground))
2477 if (STRINGP (value))
2478 to[LFACE_BACKGROUND_INDEX] = value;
2479 else
2480 err = 1;
2482 else if (EQ (keyword, QCstipple))
2484 #if defined (HAVE_WINDOW_SYSTEM)
2485 Lisp_Object pixmap_p = Fbitmap_spec_p (value);
2486 if (!NILP (pixmap_p))
2487 to[LFACE_STIPPLE_INDEX] = value;
2488 else
2489 err = 1;
2490 #endif /* HAVE_WINDOW_SYSTEM */
2492 else if (EQ (keyword, QCwidth))
2494 if (SYMBOLP (value) && FONT_WIDTH_NAME_NUMERIC (value) >= 0)
2496 to[LFACE_SWIDTH_INDEX] = value;
2497 font_clear_prop (to, FONT_WIDTH_INDEX);
2499 else
2500 err = 1;
2502 else if (EQ (keyword, QCfont))
2504 if (FONTP (value))
2505 to[LFACE_FONT_INDEX] = value;
2506 else
2507 err = 1;
2509 else if (EQ (keyword, QCinherit))
2511 /* This is not really very useful; it's just like a
2512 normal face reference. */
2513 if (! merge_face_ref (f, value, to,
2514 err_msgs, named_merge_points))
2515 err = 1;
2517 else
2518 err = 1;
2520 if (err)
2522 add_to_log ("Invalid face attribute %S %S", keyword, value);
2523 ok = 0;
2526 face_ref = XCDR (XCDR (face_ref));
2529 else
2531 /* This is a list of face refs. Those at the beginning of the
2532 list take precedence over what follows, so we have to merge
2533 from the end backwards. */
2534 Lisp_Object next = XCDR (face_ref);
2536 if (! NILP (next))
2537 ok = merge_face_ref (f, next, to, err_msgs, named_merge_points);
2539 if (! merge_face_ref (f, first, to, err_msgs, named_merge_points))
2540 ok = 0;
2543 else
2545 /* FACE_REF ought to be a face name. */
2546 ok = merge_named_face (f, face_ref, to, named_merge_points);
2547 if (!ok && err_msgs)
2548 add_to_log ("Invalid face reference: %s", face_ref, Qnil);
2551 return ok;
2555 DEFUN ("internal-make-lisp-face", Finternal_make_lisp_face,
2556 Sinternal_make_lisp_face, 1, 2, 0,
2557 doc: /* Make FACE, a symbol, a Lisp face with all attributes nil.
2558 If FACE was not known as a face before, create a new one.
2559 If optional argument FRAME is specified, make a frame-local face
2560 for that frame. Otherwise operate on the global face definition.
2561 Value is a vector of face attributes. */)
2562 (Lisp_Object face, Lisp_Object frame)
2564 Lisp_Object global_lface, lface;
2565 struct frame *f;
2566 int i;
2568 CHECK_SYMBOL (face);
2569 global_lface = lface_from_face_name (NULL, face, 0);
2571 if (!NILP (frame))
2573 CHECK_LIVE_FRAME (frame);
2574 f = XFRAME (frame);
2575 lface = lface_from_face_name (f, face, 0);
2577 else
2578 f = NULL, lface = Qnil;
2580 /* Add a global definition if there is none. */
2581 if (NILP (global_lface))
2583 global_lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
2584 Qunspecified);
2585 ASET (global_lface, 0, Qface);
2586 Vface_new_frame_defaults = Fcons (Fcons (face, global_lface),
2587 Vface_new_frame_defaults);
2589 /* Assign the new Lisp face a unique ID. The mapping from Lisp
2590 face id to Lisp face is given by the vector lface_id_to_name.
2591 The mapping from Lisp face to Lisp face id is given by the
2592 property `face' of the Lisp face name. */
2593 if (next_lface_id == lface_id_to_name_size)
2594 lface_id_to_name =
2595 xpalloc (lface_id_to_name, &lface_id_to_name_size, 1, MAX_FACE_ID,
2596 sizeof *lface_id_to_name);
2598 lface_id_to_name[next_lface_id] = face;
2599 Fput (face, Qface, make_number (next_lface_id));
2600 ++next_lface_id;
2602 else if (f == NULL)
2603 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2604 ASET (global_lface, i, Qunspecified);
2606 /* Add a frame-local definition. */
2607 if (f)
2609 if (NILP (lface))
2611 lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
2612 Qunspecified);
2613 ASET (lface, 0, Qface);
2614 fset_face_alist (f, Fcons (Fcons (face, lface), f->face_alist));
2616 else
2617 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2618 ASET (lface, i, Qunspecified);
2620 else
2621 lface = global_lface;
2623 /* Changing a named face means that all realized faces depending on
2624 that face are invalid. Since we cannot tell which realized faces
2625 depend on the face, make sure they are all removed. This is done
2626 by incrementing face_change_count. The next call to
2627 init_iterator will then free realized faces. */
2628 if (NILP (Fget (face, Qface_no_inherit)))
2630 ++face_change_count;
2631 ++windows_or_buffers_changed;
2634 eassert (LFACEP (lface));
2635 check_lface (lface);
2636 return lface;
2640 DEFUN ("internal-lisp-face-p", Finternal_lisp_face_p,
2641 Sinternal_lisp_face_p, 1, 2, 0,
2642 doc: /* Return non-nil if FACE names a face.
2643 FACE should be a symbol or string.
2644 If optional second argument FRAME is non-nil, check for the
2645 existence of a frame-local face with name FACE on that frame.
2646 Otherwise check for the existence of a global face. */)
2647 (Lisp_Object face, Lisp_Object frame)
2649 Lisp_Object lface;
2651 face = resolve_face_name (face, 1);
2653 if (!NILP (frame))
2655 CHECK_LIVE_FRAME (frame);
2656 lface = lface_from_face_name (XFRAME (frame), face, 0);
2658 else
2659 lface = lface_from_face_name (NULL, face, 0);
2661 return lface;
2665 DEFUN ("internal-copy-lisp-face", Finternal_copy_lisp_face,
2666 Sinternal_copy_lisp_face, 4, 4, 0,
2667 doc: /* Copy face FROM to TO.
2668 If FRAME is t, copy the global face definition of FROM.
2669 Otherwise, copy the frame-local definition of FROM on FRAME.
2670 If NEW-FRAME is a frame, copy that data into the frame-local
2671 definition of TO on NEW-FRAME. If NEW-FRAME is nil,
2672 FRAME controls where the data is copied to.
2674 The value is TO. */)
2675 (Lisp_Object from, Lisp_Object to, Lisp_Object frame, Lisp_Object new_frame)
2677 Lisp_Object lface, copy;
2679 CHECK_SYMBOL (from);
2680 CHECK_SYMBOL (to);
2682 if (EQ (frame, Qt))
2684 /* Copy global definition of FROM. We don't make copies of
2685 strings etc. because 20.2 didn't do it either. */
2686 lface = lface_from_face_name (NULL, from, 1);
2687 copy = Finternal_make_lisp_face (to, Qnil);
2689 else
2691 /* Copy frame-local definition of FROM. */
2692 if (NILP (new_frame))
2693 new_frame = frame;
2694 CHECK_LIVE_FRAME (frame);
2695 CHECK_LIVE_FRAME (new_frame);
2696 lface = lface_from_face_name (XFRAME (frame), from, 1);
2697 copy = Finternal_make_lisp_face (to, new_frame);
2700 vcopy (copy, 0, XVECTOR (lface)->contents, LFACE_VECTOR_SIZE);
2702 /* Changing a named face means that all realized faces depending on
2703 that face are invalid. Since we cannot tell which realized faces
2704 depend on the face, make sure they are all removed. This is done
2705 by incrementing face_change_count. The next call to
2706 init_iterator will then free realized faces. */
2707 if (NILP (Fget (to, Qface_no_inherit)))
2709 ++face_change_count;
2710 ++windows_or_buffers_changed;
2713 return to;
2717 DEFUN ("internal-set-lisp-face-attribute", Finternal_set_lisp_face_attribute,
2718 Sinternal_set_lisp_face_attribute, 3, 4, 0,
2719 doc: /* Set attribute ATTR of FACE to VALUE.
2720 FRAME being a frame means change the face on that frame.
2721 FRAME nil means change the face of the selected frame.
2722 FRAME t means change the default for new frames.
2723 FRAME 0 means change the face on all frames, and change the default
2724 for new frames. */)
2725 (Lisp_Object face, Lisp_Object attr, Lisp_Object value, Lisp_Object frame)
2727 Lisp_Object lface;
2728 Lisp_Object old_value = Qnil;
2729 /* Set one of enum font_property_index (> 0) if ATTR is one of
2730 font-related attributes other than QCfont and QCfontset. */
2731 enum font_property_index prop_index = 0;
2733 CHECK_SYMBOL (face);
2734 CHECK_SYMBOL (attr);
2736 face = resolve_face_name (face, 1);
2738 /* If FRAME is 0, change face on all frames, and change the
2739 default for new frames. */
2740 if (INTEGERP (frame) && XINT (frame) == 0)
2742 Lisp_Object tail;
2743 Finternal_set_lisp_face_attribute (face, attr, value, Qt);
2744 FOR_EACH_FRAME (tail, frame)
2745 Finternal_set_lisp_face_attribute (face, attr, value, frame);
2746 return face;
2749 /* Set lface to the Lisp attribute vector of FACE. */
2750 if (EQ (frame, Qt))
2752 lface = lface_from_face_name (NULL, face, 1);
2754 /* When updating face-new-frame-defaults, we put :ignore-defface
2755 where the caller wants `unspecified'. This forces the frame
2756 defaults to ignore the defface value. Otherwise, the defface
2757 will take effect, which is generally not what is intended.
2758 The value of that attribute will be inherited from some other
2759 face during face merging. See internal_merge_in_global_face. */
2760 if (UNSPECIFIEDP (value))
2761 value = QCignore_defface;
2763 else
2765 if (NILP (frame))
2766 frame = selected_frame;
2768 CHECK_LIVE_FRAME (frame);
2769 lface = lface_from_face_name (XFRAME (frame), face, 0);
2771 /* If a frame-local face doesn't exist yet, create one. */
2772 if (NILP (lface))
2773 lface = Finternal_make_lisp_face (face, frame);
2776 if (EQ (attr, QCfamily))
2778 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2780 CHECK_STRING (value);
2781 if (SCHARS (value) == 0)
2782 signal_error ("Invalid face family", value);
2784 old_value = LFACE_FAMILY (lface);
2785 ASET (lface, LFACE_FAMILY_INDEX, value);
2786 prop_index = FONT_FAMILY_INDEX;
2788 else if (EQ (attr, QCfoundry))
2790 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2792 CHECK_STRING (value);
2793 if (SCHARS (value) == 0)
2794 signal_error ("Invalid face foundry", value);
2796 old_value = LFACE_FOUNDRY (lface);
2797 ASET (lface, LFACE_FOUNDRY_INDEX, value);
2798 prop_index = FONT_FOUNDRY_INDEX;
2800 else if (EQ (attr, QCheight))
2802 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2804 if (EQ (face, Qdefault))
2806 /* The default face must have an absolute size. */
2807 if (!INTEGERP (value) || XINT (value) <= 0)
2808 signal_error ("Default face height not absolute and positive",
2809 value);
2811 else
2813 /* For non-default faces, do a test merge with a random
2814 height to see if VALUE's ok. */
2815 Lisp_Object test = merge_face_heights (value,
2816 make_number (10),
2817 Qnil);
2818 if (!INTEGERP (test) || XINT (test) <= 0)
2819 signal_error ("Face height does not produce a positive integer",
2820 value);
2824 old_value = LFACE_HEIGHT (lface);
2825 ASET (lface, LFACE_HEIGHT_INDEX, value);
2826 prop_index = FONT_SIZE_INDEX;
2828 else if (EQ (attr, QCweight))
2830 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2832 CHECK_SYMBOL (value);
2833 if (FONT_WEIGHT_NAME_NUMERIC (value) < 0)
2834 signal_error ("Invalid face weight", value);
2836 old_value = LFACE_WEIGHT (lface);
2837 ASET (lface, LFACE_WEIGHT_INDEX, value);
2838 prop_index = FONT_WEIGHT_INDEX;
2840 else if (EQ (attr, QCslant))
2842 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2844 CHECK_SYMBOL (value);
2845 if (FONT_SLANT_NAME_NUMERIC (value) < 0)
2846 signal_error ("Invalid face slant", value);
2848 old_value = LFACE_SLANT (lface);
2849 ASET (lface, LFACE_SLANT_INDEX, value);
2850 prop_index = FONT_SLANT_INDEX;
2852 else if (EQ (attr, QCunderline))
2854 bool valid_p = 0;
2856 if (UNSPECIFIEDP (value) || IGNORE_DEFFACE_P (value))
2857 valid_p = 1;
2858 else if (NILP (value) || EQ (value, Qt))
2859 valid_p = 1;
2860 else if (STRINGP (value) && SCHARS (value) > 0)
2861 valid_p = 1;
2862 else if (CONSP (value))
2864 Lisp_Object key, val, list;
2866 list = value;
2867 /* FIXME? This errs on the side of acceptance. Eg it accepts:
2868 (defface foo '((t :underline 'foo) "doc")
2869 Maybe this is intentional, maybe it isn't.
2870 Non-nil symbols other than t are not documented as being valid.
2871 Eg compare with inverse-video, which explicitly rejects them.
2873 valid_p = 1;
2875 while (!NILP (CAR_SAFE(list)))
2877 key = CAR_SAFE (list);
2878 list = CDR_SAFE (list);
2879 val = CAR_SAFE (list);
2880 list = CDR_SAFE (list);
2882 if (NILP (key) || NILP (val))
2884 valid_p = 0;
2885 break;
2888 else if (EQ (key, QCcolor)
2889 && !(EQ (val, Qforeground_color)
2890 || (STRINGP (val) && SCHARS (val) > 0)))
2892 valid_p = 0;
2893 break;
2896 else if (EQ (key, QCstyle)
2897 && !(EQ (val, Qline) || EQ (val, Qwave)))
2899 valid_p = 0;
2900 break;
2905 if (!valid_p)
2906 signal_error ("Invalid face underline", value);
2908 old_value = LFACE_UNDERLINE (lface);
2909 ASET (lface, LFACE_UNDERLINE_INDEX, value);
2911 else if (EQ (attr, QCoverline))
2913 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2914 if ((SYMBOLP (value)
2915 && !EQ (value, Qt)
2916 && !EQ (value, Qnil))
2917 /* Overline color. */
2918 || (STRINGP (value)
2919 && SCHARS (value) == 0))
2920 signal_error ("Invalid face overline", value);
2922 old_value = LFACE_OVERLINE (lface);
2923 ASET (lface, LFACE_OVERLINE_INDEX, value);
2925 else if (EQ (attr, QCstrike_through))
2927 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2928 if ((SYMBOLP (value)
2929 && !EQ (value, Qt)
2930 && !EQ (value, Qnil))
2931 /* Strike-through color. */
2932 || (STRINGP (value)
2933 && SCHARS (value) == 0))
2934 signal_error ("Invalid face strike-through", value);
2936 old_value = LFACE_STRIKE_THROUGH (lface);
2937 ASET (lface, LFACE_STRIKE_THROUGH_INDEX, value);
2939 else if (EQ (attr, QCbox))
2941 bool valid_p;
2943 /* Allow t meaning a simple box of width 1 in foreground color
2944 of the face. */
2945 if (EQ (value, Qt))
2946 value = make_number (1);
2948 if (UNSPECIFIEDP (value) || IGNORE_DEFFACE_P (value))
2949 valid_p = 1;
2950 else if (NILP (value))
2951 valid_p = 1;
2952 else if (INTEGERP (value))
2953 valid_p = XINT (value) != 0;
2954 else if (STRINGP (value))
2955 valid_p = SCHARS (value) > 0;
2956 else if (CONSP (value))
2958 Lisp_Object tem;
2960 tem = value;
2961 while (CONSP (tem))
2963 Lisp_Object k, v;
2965 k = XCAR (tem);
2966 tem = XCDR (tem);
2967 if (!CONSP (tem))
2968 break;
2969 v = XCAR (tem);
2970 tem = XCDR (tem);
2972 if (EQ (k, QCline_width))
2974 if (!INTEGERP (v) || XINT (v) == 0)
2975 break;
2977 else if (EQ (k, QCcolor))
2979 if (!NILP (v) && (!STRINGP (v) || SCHARS (v) == 0))
2980 break;
2982 else if (EQ (k, QCstyle))
2984 if (!EQ (v, Qpressed_button) && !EQ (v, Qreleased_button))
2985 break;
2987 else
2988 break;
2991 valid_p = NILP (tem);
2993 else
2994 valid_p = 0;
2996 if (!valid_p)
2997 signal_error ("Invalid face box", value);
2999 old_value = LFACE_BOX (lface);
3000 ASET (lface, LFACE_BOX_INDEX, value);
3002 else if (EQ (attr, QCinverse_video)
3003 || EQ (attr, QCreverse_video))
3005 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3007 CHECK_SYMBOL (value);
3008 if (!EQ (value, Qt) && !NILP (value))
3009 signal_error ("Invalid inverse-video face attribute value", value);
3011 old_value = LFACE_INVERSE (lface);
3012 ASET (lface, LFACE_INVERSE_INDEX, value);
3014 else if (EQ (attr, QCforeground))
3016 /* Compatibility with 20.x. */
3017 if (NILP (value))
3018 value = Qunspecified;
3019 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3021 /* Don't check for valid color names here because it depends
3022 on the frame (display) whether the color will be valid
3023 when the face is realized. */
3024 CHECK_STRING (value);
3025 if (SCHARS (value) == 0)
3026 signal_error ("Empty foreground color value", value);
3028 old_value = LFACE_FOREGROUND (lface);
3029 ASET (lface, LFACE_FOREGROUND_INDEX, value);
3031 else if (EQ (attr, QCbackground))
3033 /* Compatibility with 20.x. */
3034 if (NILP (value))
3035 value = Qunspecified;
3036 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3038 /* Don't check for valid color names here because it depends
3039 on the frame (display) whether the color will be valid
3040 when the face is realized. */
3041 CHECK_STRING (value);
3042 if (SCHARS (value) == 0)
3043 signal_error ("Empty background color value", value);
3045 old_value = LFACE_BACKGROUND (lface);
3046 ASET (lface, LFACE_BACKGROUND_INDEX, value);
3048 else if (EQ (attr, QCstipple))
3050 #if defined (HAVE_WINDOW_SYSTEM)
3051 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value)
3052 && !NILP (value)
3053 && NILP (Fbitmap_spec_p (value)))
3054 signal_error ("Invalid stipple attribute", value);
3055 old_value = LFACE_STIPPLE (lface);
3056 ASET (lface, LFACE_STIPPLE_INDEX, value);
3057 #endif /* HAVE_WINDOW_SYSTEM */
3059 else if (EQ (attr, QCwidth))
3061 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3063 CHECK_SYMBOL (value);
3064 if (FONT_WIDTH_NAME_NUMERIC (value) < 0)
3065 signal_error ("Invalid face width", value);
3067 old_value = LFACE_SWIDTH (lface);
3068 ASET (lface, LFACE_SWIDTH_INDEX, value);
3069 prop_index = FONT_WIDTH_INDEX;
3071 else if (EQ (attr, QCfont))
3073 #ifdef HAVE_WINDOW_SYSTEM
3074 if (EQ (frame, Qt) || FRAME_WINDOW_P (XFRAME (frame)))
3076 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3078 struct frame *f;
3080 old_value = LFACE_FONT (lface);
3081 if (! FONTP (value))
3083 if (STRINGP (value))
3085 Lisp_Object name = value;
3086 int fontset = fs_query_fontset (name, 0);
3088 if (fontset >= 0)
3089 name = fontset_ascii (fontset);
3090 value = font_spec_from_name (name);
3091 if (!FONTP (value))
3092 signal_error ("Invalid font name", name);
3094 else
3095 signal_error ("Invalid font or font-spec", value);
3097 if (EQ (frame, Qt))
3098 f = XFRAME (selected_frame);
3099 else
3100 f = XFRAME (frame);
3101 if (! FONT_OBJECT_P (value))
3103 Lisp_Object *attrs = XVECTOR (lface)->contents;
3104 Lisp_Object font_object;
3106 font_object = font_load_for_lface (f, attrs, value);
3107 if (NILP (font_object))
3108 signal_error ("Font not available", value);
3109 value = font_object;
3111 set_lface_from_font (f, lface, value, 1);
3113 else
3114 ASET (lface, LFACE_FONT_INDEX, value);
3116 #endif /* HAVE_WINDOW_SYSTEM */
3118 else if (EQ (attr, QCfontset))
3120 #ifdef HAVE_WINDOW_SYSTEM
3121 if (EQ (frame, Qt) || FRAME_WINDOW_P (XFRAME (frame)))
3123 Lisp_Object tmp;
3125 old_value = LFACE_FONTSET (lface);
3126 tmp = Fquery_fontset (value, Qnil);
3127 if (NILP (tmp))
3128 signal_error ("Invalid fontset name", value);
3129 ASET (lface, LFACE_FONTSET_INDEX, value = tmp);
3131 #endif /* HAVE_WINDOW_SYSTEM */
3133 else if (EQ (attr, QCinherit))
3135 Lisp_Object tail;
3136 if (SYMBOLP (value))
3137 tail = Qnil;
3138 else
3139 for (tail = value; CONSP (tail); tail = XCDR (tail))
3140 if (!SYMBOLP (XCAR (tail)))
3141 break;
3142 if (NILP (tail))
3143 ASET (lface, LFACE_INHERIT_INDEX, value);
3144 else
3145 signal_error ("Invalid face inheritance", value);
3147 else if (EQ (attr, QCbold))
3149 old_value = LFACE_WEIGHT (lface);
3150 ASET (lface, LFACE_WEIGHT_INDEX, NILP (value) ? Qnormal : Qbold);
3151 prop_index = FONT_WEIGHT_INDEX;
3153 else if (EQ (attr, QCitalic))
3155 attr = QCslant;
3156 old_value = LFACE_SLANT (lface);
3157 ASET (lface, LFACE_SLANT_INDEX, NILP (value) ? Qnormal : Qitalic);
3158 prop_index = FONT_SLANT_INDEX;
3160 else
3161 signal_error ("Invalid face attribute name", attr);
3163 if (prop_index)
3165 /* If a font-related attribute other than QCfont and QCfontset
3166 is specified, and if the original QCfont attribute has a font
3167 (font-spec or font-object), set the corresponding property in
3168 the font to nil so that the font selector doesn't think that
3169 the attribute is mandatory. Also, clear the average
3170 width. */
3171 font_clear_prop (XVECTOR (lface)->contents, prop_index);
3174 /* Changing a named face means that all realized faces depending on
3175 that face are invalid. Since we cannot tell which realized faces
3176 depend on the face, make sure they are all removed. This is done
3177 by incrementing face_change_count. The next call to
3178 init_iterator will then free realized faces. */
3179 if (!EQ (frame, Qt)
3180 && NILP (Fget (face, Qface_no_inherit))
3181 && NILP (Fequal (old_value, value)))
3183 ++face_change_count;
3184 ++windows_or_buffers_changed;
3187 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value)
3188 && NILP (Fequal (old_value, value)))
3190 Lisp_Object param;
3192 param = Qnil;
3194 if (EQ (face, Qdefault))
3196 #ifdef HAVE_WINDOW_SYSTEM
3197 /* Changed font-related attributes of the `default' face are
3198 reflected in changed `font' frame parameters. */
3199 if (FRAMEP (frame)
3200 && (prop_index || EQ (attr, QCfont))
3201 && lface_fully_specified_p (XVECTOR (lface)->contents))
3202 set_font_frame_param (frame, lface);
3203 else
3204 #endif /* HAVE_WINDOW_SYSTEM */
3206 if (EQ (attr, QCforeground))
3207 param = Qforeground_color;
3208 else if (EQ (attr, QCbackground))
3209 param = Qbackground_color;
3211 #ifdef HAVE_WINDOW_SYSTEM
3212 #ifndef HAVE_NTGUI
3213 else if (EQ (face, Qscroll_bar))
3215 /* Changing the colors of `scroll-bar' sets frame parameters
3216 `scroll-bar-foreground' and `scroll-bar-background'. */
3217 if (EQ (attr, QCforeground))
3218 param = Qscroll_bar_foreground;
3219 else if (EQ (attr, QCbackground))
3220 param = Qscroll_bar_background;
3222 #endif /* not HAVE_NTGUI */
3223 else if (EQ (face, Qborder))
3225 /* Changing background color of `border' sets frame parameter
3226 `border-color'. */
3227 if (EQ (attr, QCbackground))
3228 param = Qborder_color;
3230 else if (EQ (face, Qcursor))
3232 /* Changing background color of `cursor' sets frame parameter
3233 `cursor-color'. */
3234 if (EQ (attr, QCbackground))
3235 param = Qcursor_color;
3237 else if (EQ (face, Qmouse))
3239 /* Changing background color of `mouse' sets frame parameter
3240 `mouse-color'. */
3241 if (EQ (attr, QCbackground))
3242 param = Qmouse_color;
3244 #endif /* HAVE_WINDOW_SYSTEM */
3245 else if (EQ (face, Qmenu))
3247 /* Indicate that we have to update the menu bar when realizing
3248 faces on FRAME. FRAME t change the default for new frames.
3249 We do this by setting the flag in new face caches. */
3250 if (FRAMEP (frame))
3252 struct frame *f = XFRAME (frame);
3253 if (FRAME_FACE_CACHE (f) == NULL)
3254 FRAME_FACE_CACHE (f) = make_face_cache (f);
3255 FRAME_FACE_CACHE (f)->menu_face_changed_p = 1;
3257 else
3258 menu_face_changed_default = 1;
3261 if (!NILP (param))
3263 if (EQ (frame, Qt))
3264 /* Update `default-frame-alist', which is used for new frames. */
3266 store_in_alist (&Vdefault_frame_alist, param, value);
3268 else
3269 /* Update the current frame's parameters. */
3271 Lisp_Object cons;
3272 cons = XCAR (Vparam_value_alist);
3273 XSETCAR (cons, param);
3274 XSETCDR (cons, value);
3275 Fmodify_frame_parameters (frame, Vparam_value_alist);
3280 return face;
3284 /* Update the corresponding face when frame parameter PARAM on frame F
3285 has been assigned the value NEW_VALUE. */
3287 void
3288 update_face_from_frame_parameter (struct frame *f, Lisp_Object param,
3289 Lisp_Object new_value)
3291 Lisp_Object face = Qnil;
3292 Lisp_Object lface;
3294 /* If there are no faces yet, give up. This is the case when called
3295 from Fx_create_frame, and we do the necessary things later in
3296 face-set-after-frame-defaults. */
3297 if (NILP (f->face_alist))
3298 return;
3300 if (EQ (param, Qforeground_color))
3302 face = Qdefault;
3303 lface = lface_from_face_name (f, face, 1);
3304 ASET (lface, LFACE_FOREGROUND_INDEX,
3305 (STRINGP (new_value) ? new_value : Qunspecified));
3306 realize_basic_faces (f);
3308 else if (EQ (param, Qbackground_color))
3310 Lisp_Object frame;
3312 /* Changing the background color might change the background
3313 mode, so that we have to load new defface specs.
3314 Call frame-set-background-mode to do that. */
3315 XSETFRAME (frame, f);
3316 call1 (Qframe_set_background_mode, frame);
3318 face = Qdefault;
3319 lface = lface_from_face_name (f, face, 1);
3320 ASET (lface, LFACE_BACKGROUND_INDEX,
3321 (STRINGP (new_value) ? new_value : Qunspecified));
3322 realize_basic_faces (f);
3324 #ifdef HAVE_WINDOW_SYSTEM
3325 else if (EQ (param, Qborder_color))
3327 face = Qborder;
3328 lface = lface_from_face_name (f, face, 1);
3329 ASET (lface, LFACE_BACKGROUND_INDEX,
3330 (STRINGP (new_value) ? new_value : Qunspecified));
3332 else if (EQ (param, Qcursor_color))
3334 face = Qcursor;
3335 lface = lface_from_face_name (f, face, 1);
3336 ASET (lface, LFACE_BACKGROUND_INDEX,
3337 (STRINGP (new_value) ? new_value : Qunspecified));
3339 else if (EQ (param, Qmouse_color))
3341 face = Qmouse;
3342 lface = lface_from_face_name (f, face, 1);
3343 ASET (lface, LFACE_BACKGROUND_INDEX,
3344 (STRINGP (new_value) ? new_value : Qunspecified));
3346 #endif
3348 /* Changing a named face means that all realized faces depending on
3349 that face are invalid. Since we cannot tell which realized faces
3350 depend on the face, make sure they are all removed. This is done
3351 by incrementing face_change_count. The next call to
3352 init_iterator will then free realized faces. */
3353 if (!NILP (face)
3354 && NILP (Fget (face, Qface_no_inherit)))
3356 ++face_change_count;
3357 ++windows_or_buffers_changed;
3362 #ifdef HAVE_WINDOW_SYSTEM
3364 /* Set the `font' frame parameter of FRAME determined from the
3365 font-object set in `default' face attributes LFACE. */
3367 static void
3368 set_font_frame_param (Lisp_Object frame, Lisp_Object lface)
3370 struct frame *f = XFRAME (frame);
3371 Lisp_Object font;
3373 if (FRAME_WINDOW_P (f)
3374 /* Don't do anything if the font is `unspecified'. This can
3375 happen during frame creation. */
3376 && (font = LFACE_FONT (lface),
3377 ! UNSPECIFIEDP (font)))
3379 if (FONT_SPEC_P (font))
3381 font = font_load_for_lface (f, XVECTOR (lface)->contents, font);
3382 if (NILP (font))
3383 return;
3384 ASET (lface, LFACE_FONT_INDEX, font);
3386 f->default_face_done_p = 0;
3387 Fmodify_frame_parameters (frame, list1 (Fcons (Qfont, font)));
3391 DEFUN ("internal-face-x-get-resource", Finternal_face_x_get_resource,
3392 Sinternal_face_x_get_resource, 2, 3, 0,
3393 doc: /* Get the value of X resource RESOURCE, class CLASS.
3394 Returned value is for the display of frame FRAME. If FRAME is not
3395 specified or nil, use selected frame. This function exists because
3396 ordinary `x-get-resource' doesn't take a frame argument. */)
3397 (Lisp_Object resource, Lisp_Object class, Lisp_Object frame)
3399 Lisp_Object value = Qnil;
3400 struct frame *f;
3402 CHECK_STRING (resource);
3403 CHECK_STRING (class);
3404 f = decode_live_frame (frame);
3405 block_input ();
3406 value = display_x_get_resource (FRAME_X_DISPLAY_INFO (f),
3407 resource, class, Qnil, Qnil);
3408 unblock_input ();
3409 return value;
3413 /* Return resource string VALUE as a boolean value, i.e. nil, or t.
3414 If VALUE is "on" or "true", return t. If VALUE is "off" or
3415 "false", return nil. Otherwise, if SIGNAL_P is non-zero, signal an
3416 error; if SIGNAL_P is zero, return 0. */
3418 static Lisp_Object
3419 face_boolean_x_resource_value (Lisp_Object value, int signal_p)
3421 Lisp_Object result = make_number (0);
3423 eassert (STRINGP (value));
3425 if (xstrcasecmp (SSDATA (value), "on") == 0
3426 || xstrcasecmp (SSDATA (value), "true") == 0)
3427 result = Qt;
3428 else if (xstrcasecmp (SSDATA (value), "off") == 0
3429 || xstrcasecmp (SSDATA (value), "false") == 0)
3430 result = Qnil;
3431 else if (xstrcasecmp (SSDATA (value), "unspecified") == 0)
3432 result = Qunspecified;
3433 else if (signal_p)
3434 signal_error ("Invalid face attribute value from X resource", value);
3436 return result;
3440 DEFUN ("internal-set-lisp-face-attribute-from-resource",
3441 Finternal_set_lisp_face_attribute_from_resource,
3442 Sinternal_set_lisp_face_attribute_from_resource,
3443 3, 4, 0, doc: /* */)
3444 (Lisp_Object face, Lisp_Object attr, Lisp_Object value, Lisp_Object frame)
3446 CHECK_SYMBOL (face);
3447 CHECK_SYMBOL (attr);
3448 CHECK_STRING (value);
3450 if (xstrcasecmp (SSDATA (value), "unspecified") == 0)
3451 value = Qunspecified;
3452 else if (EQ (attr, QCheight))
3454 value = Fstring_to_number (value, make_number (10));
3455 if (XINT (value) <= 0)
3456 signal_error ("Invalid face height from X resource", value);
3458 else if (EQ (attr, QCbold) || EQ (attr, QCitalic))
3459 value = face_boolean_x_resource_value (value, 1);
3460 else if (EQ (attr, QCweight) || EQ (attr, QCslant) || EQ (attr, QCwidth))
3461 value = intern (SSDATA (value));
3462 else if (EQ (attr, QCreverse_video) || EQ (attr, QCinverse_video))
3463 value = face_boolean_x_resource_value (value, 1);
3464 else if (EQ (attr, QCunderline)
3465 || EQ (attr, QCoverline)
3466 || EQ (attr, QCstrike_through))
3468 Lisp_Object boolean_value;
3470 /* If the result of face_boolean_x_resource_value is t or nil,
3471 VALUE does NOT specify a color. */
3472 boolean_value = face_boolean_x_resource_value (value, 0);
3473 if (SYMBOLP (boolean_value))
3474 value = boolean_value;
3476 else if (EQ (attr, QCbox) || EQ (attr, QCinherit))
3477 value = Fcar (Fread_from_string (value, Qnil, Qnil));
3479 return Finternal_set_lisp_face_attribute (face, attr, value, frame);
3482 #endif /* HAVE_WINDOW_SYSTEM */
3485 /***********************************************************************
3486 Menu face
3487 ***********************************************************************/
3489 #if defined HAVE_X_WINDOWS && defined USE_X_TOOLKIT
3491 /* Make menus on frame F appear as specified by the `menu' face. */
3493 static void
3494 x_update_menu_appearance (struct frame *f)
3496 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3497 XrmDatabase rdb;
3499 if (dpyinfo
3500 && (rdb = XrmGetDatabase (FRAME_X_DISPLAY (f)),
3501 rdb != NULL))
3503 char line[512];
3504 char *buf = line;
3505 ptrdiff_t bufsize = sizeof line;
3506 Lisp_Object lface = lface_from_face_name (f, Qmenu, 1);
3507 struct face *face = FACE_FROM_ID (f, MENU_FACE_ID);
3508 const char *myname = SSDATA (Vx_resource_name);
3509 bool changed_p = 0;
3510 #ifdef USE_MOTIF
3511 const char *popup_path = "popup_menu";
3512 #else
3513 const char *popup_path = "menu.popup";
3514 #endif
3516 if (STRINGP (LFACE_FOREGROUND (lface)))
3518 exprintf (&buf, &bufsize, line, -1, "%s.%s*foreground: %s",
3519 myname, popup_path,
3520 SDATA (LFACE_FOREGROUND (lface)));
3521 XrmPutLineResource (&rdb, line);
3522 exprintf (&buf, &bufsize, line, -1, "%s.pane.menubar*foreground: %s",
3523 myname, SDATA (LFACE_FOREGROUND (lface)));
3524 XrmPutLineResource (&rdb, line);
3525 changed_p = 1;
3528 if (STRINGP (LFACE_BACKGROUND (lface)))
3530 exprintf (&buf, &bufsize, line, -1, "%s.%s*background: %s",
3531 myname, popup_path,
3532 SDATA (LFACE_BACKGROUND (lface)));
3533 XrmPutLineResource (&rdb, line);
3535 exprintf (&buf, &bufsize, line, -1, "%s.pane.menubar*background: %s",
3536 myname, SDATA (LFACE_BACKGROUND (lface)));
3537 XrmPutLineResource (&rdb, line);
3538 changed_p = 1;
3541 if (face->font
3542 /* On Solaris 5.8, it's been reported that the `menu' face
3543 can be unspecified here, during startup. Why this
3544 happens remains unknown. -- cyd */
3545 && FONTP (LFACE_FONT (lface))
3546 && (!UNSPECIFIEDP (LFACE_FAMILY (lface))
3547 || !UNSPECIFIEDP (LFACE_FOUNDRY (lface))
3548 || !UNSPECIFIEDP (LFACE_SWIDTH (lface))
3549 || !UNSPECIFIEDP (LFACE_WEIGHT (lface))
3550 || !UNSPECIFIEDP (LFACE_SLANT (lface))
3551 || !UNSPECIFIEDP (LFACE_HEIGHT (lface))))
3553 Lisp_Object xlfd = Ffont_xlfd_name (LFACE_FONT (lface), Qnil);
3554 #ifdef USE_MOTIF
3555 const char *suffix = "List";
3556 Bool motif = True;
3557 #else
3558 #if defined HAVE_X_I18N
3560 const char *suffix = "Set";
3561 #else
3562 const char *suffix = "";
3563 #endif
3564 Bool motif = False;
3565 #endif
3567 if (! NILP (xlfd))
3569 #if defined HAVE_X_I18N
3570 char *fontsetname = xic_create_fontsetname (SSDATA (xlfd), motif);
3571 #else
3572 char *fontsetname = SSDATA (xlfd);
3573 #endif
3574 exprintf (&buf, &bufsize, line, -1, "%s.pane.menubar*font%s: %s",
3575 myname, suffix, fontsetname);
3576 XrmPutLineResource (&rdb, line);
3578 exprintf (&buf, &bufsize, line, -1, "%s.%s*font%s: %s",
3579 myname, popup_path, suffix, fontsetname);
3580 XrmPutLineResource (&rdb, line);
3581 changed_p = 1;
3582 if (fontsetname != SSDATA (xlfd))
3583 xfree (fontsetname);
3587 if (changed_p && f->output_data.x->menubar_widget)
3588 free_frame_menubar (f);
3590 if (buf != line)
3591 xfree (buf);
3595 #endif /* HAVE_X_WINDOWS && USE_X_TOOLKIT */
3598 DEFUN ("face-attribute-relative-p", Fface_attribute_relative_p,
3599 Sface_attribute_relative_p,
3600 2, 2, 0,
3601 doc: /* Check whether a face attribute value is relative.
3602 Specifically, this function returns t if the attribute ATTRIBUTE
3603 with the value VALUE is relative.
3605 A relative value is one that doesn't entirely override whatever is
3606 inherited from another face. For most possible attributes,
3607 the only relative value that users see is `unspecified'.
3608 However, for :height, floating point values are also relative. */)
3609 (Lisp_Object attribute, Lisp_Object value)
3611 if (EQ (value, Qunspecified) || (EQ (value, QCignore_defface)))
3612 return Qt;
3613 else if (EQ (attribute, QCheight))
3614 return INTEGERP (value) ? Qnil : Qt;
3615 else
3616 return Qnil;
3619 DEFUN ("merge-face-attribute", Fmerge_face_attribute, Smerge_face_attribute,
3620 3, 3, 0,
3621 doc: /* Return face ATTRIBUTE VALUE1 merged with VALUE2.
3622 If VALUE1 or VALUE2 are absolute (see `face-attribute-relative-p'), then
3623 the result will be absolute, otherwise it will be relative. */)
3624 (Lisp_Object attribute, Lisp_Object value1, Lisp_Object value2)
3626 if (EQ (value1, Qunspecified) || EQ (value1, QCignore_defface))
3627 return value2;
3628 else if (EQ (attribute, QCheight))
3629 return merge_face_heights (value1, value2, value1);
3630 else
3631 return value1;
3635 DEFUN ("internal-get-lisp-face-attribute", Finternal_get_lisp_face_attribute,
3636 Sinternal_get_lisp_face_attribute,
3637 2, 3, 0,
3638 doc: /* Return face attribute KEYWORD of face SYMBOL.
3639 If SYMBOL does not name a valid Lisp face or KEYWORD isn't a valid
3640 face attribute name, signal an error.
3641 If the optional argument FRAME is given, report on face SYMBOL in that
3642 frame. If FRAME is t, report on the defaults for face SYMBOL (for new
3643 frames). If FRAME is omitted or nil, use the selected frame. */)
3644 (Lisp_Object symbol, Lisp_Object keyword, Lisp_Object frame)
3646 struct frame *f = EQ (frame, Qt) ? NULL : decode_live_frame (frame);
3647 Lisp_Object lface = lface_from_face_name (f, symbol, 1), value = Qnil;
3649 CHECK_SYMBOL (symbol);
3650 CHECK_SYMBOL (keyword);
3652 if (EQ (keyword, QCfamily))
3653 value = LFACE_FAMILY (lface);
3654 else if (EQ (keyword, QCfoundry))
3655 value = LFACE_FOUNDRY (lface);
3656 else if (EQ (keyword, QCheight))
3657 value = LFACE_HEIGHT (lface);
3658 else if (EQ (keyword, QCweight))
3659 value = LFACE_WEIGHT (lface);
3660 else if (EQ (keyword, QCslant))
3661 value = LFACE_SLANT (lface);
3662 else if (EQ (keyword, QCunderline))
3663 value = LFACE_UNDERLINE (lface);
3664 else if (EQ (keyword, QCoverline))
3665 value = LFACE_OVERLINE (lface);
3666 else if (EQ (keyword, QCstrike_through))
3667 value = LFACE_STRIKE_THROUGH (lface);
3668 else if (EQ (keyword, QCbox))
3669 value = LFACE_BOX (lface);
3670 else if (EQ (keyword, QCinverse_video)
3671 || EQ (keyword, QCreverse_video))
3672 value = LFACE_INVERSE (lface);
3673 else if (EQ (keyword, QCforeground))
3674 value = LFACE_FOREGROUND (lface);
3675 else if (EQ (keyword, QCbackground))
3676 value = LFACE_BACKGROUND (lface);
3677 else if (EQ (keyword, QCstipple))
3678 value = LFACE_STIPPLE (lface);
3679 else if (EQ (keyword, QCwidth))
3680 value = LFACE_SWIDTH (lface);
3681 else if (EQ (keyword, QCinherit))
3682 value = LFACE_INHERIT (lface);
3683 else if (EQ (keyword, QCfont))
3684 value = LFACE_FONT (lface);
3685 else if (EQ (keyword, QCfontset))
3686 value = LFACE_FONTSET (lface);
3687 else
3688 signal_error ("Invalid face attribute name", keyword);
3690 if (IGNORE_DEFFACE_P (value))
3691 return Qunspecified;
3693 return value;
3697 DEFUN ("internal-lisp-face-attribute-values",
3698 Finternal_lisp_face_attribute_values,
3699 Sinternal_lisp_face_attribute_values, 1, 1, 0,
3700 doc: /* Return a list of valid discrete values for face attribute ATTR.
3701 Value is nil if ATTR doesn't have a discrete set of valid values. */)
3702 (Lisp_Object attr)
3704 Lisp_Object result = Qnil;
3706 CHECK_SYMBOL (attr);
3708 if (EQ (attr, QCunderline) || EQ (attr, QCoverline)
3709 || EQ (attr, QCstrike_through)
3710 || EQ (attr, QCinverse_video) || EQ (attr, QCreverse_video))
3711 result = list2 (Qt, Qnil);
3713 return result;
3717 DEFUN ("internal-merge-in-global-face", Finternal_merge_in_global_face,
3718 Sinternal_merge_in_global_face, 2, 2, 0,
3719 doc: /* Add attributes from frame-default definition of FACE to FACE on FRAME.
3720 Default face attributes override any local face attributes. */)
3721 (Lisp_Object face, Lisp_Object frame)
3723 int i;
3724 Lisp_Object global_lface, local_lface, *gvec, *lvec;
3725 struct frame *f = XFRAME (frame);
3727 CHECK_LIVE_FRAME (frame);
3728 global_lface = lface_from_face_name (NULL, face, 1);
3729 local_lface = lface_from_face_name (f, face, 0);
3730 if (NILP (local_lface))
3731 local_lface = Finternal_make_lisp_face (face, frame);
3733 /* Make every specified global attribute override the local one.
3734 BEWARE!! This is only used from `face-set-after-frame-default' where
3735 the local frame is defined from default specs in `face-defface-spec'
3736 and those should be overridden by global settings. Hence the strange
3737 "global before local" priority. */
3738 lvec = XVECTOR (local_lface)->contents;
3739 gvec = XVECTOR (global_lface)->contents;
3740 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3741 if (IGNORE_DEFFACE_P (gvec[i]))
3742 ASET (local_lface, i, Qunspecified);
3743 else if (! UNSPECIFIEDP (gvec[i]))
3744 ASET (local_lface, i, AREF (global_lface, i));
3746 /* If the default face was changed, update the face cache and the
3747 `font' frame parameter. */
3748 if (EQ (face, Qdefault))
3750 struct face_cache *c = FRAME_FACE_CACHE (f);
3751 struct face *newface, *oldface = FACE_FROM_ID (f, DEFAULT_FACE_ID);
3752 Lisp_Object attrs[LFACE_VECTOR_SIZE];
3754 /* This can be NULL (e.g., in batch mode). */
3755 if (oldface)
3757 /* Ensure that the face vector is fully specified by merging
3758 the previously-cached vector. */
3759 memcpy (attrs, oldface->lface, sizeof attrs);
3760 merge_face_vectors (f, lvec, attrs, 0);
3761 vcopy (local_lface, 0, attrs, LFACE_VECTOR_SIZE);
3762 newface = realize_face (c, lvec, DEFAULT_FACE_ID);
3764 if ((! UNSPECIFIEDP (gvec[LFACE_FAMILY_INDEX])
3765 || ! UNSPECIFIEDP (gvec[LFACE_FOUNDRY_INDEX])
3766 || ! UNSPECIFIEDP (gvec[LFACE_HEIGHT_INDEX])
3767 || ! UNSPECIFIEDP (gvec[LFACE_WEIGHT_INDEX])
3768 || ! UNSPECIFIEDP (gvec[LFACE_SLANT_INDEX])
3769 || ! UNSPECIFIEDP (gvec[LFACE_SWIDTH_INDEX])
3770 || ! UNSPECIFIEDP (gvec[LFACE_FONT_INDEX]))
3771 && newface->font)
3773 Lisp_Object name = newface->font->props[FONT_NAME_INDEX];
3774 Fmodify_frame_parameters (frame, list1 (Fcons (Qfont, name)));
3777 if (STRINGP (gvec[LFACE_FOREGROUND_INDEX]))
3778 Fmodify_frame_parameters (frame,
3779 list1 (Fcons (Qforeground_color,
3780 gvec[LFACE_FOREGROUND_INDEX])));
3782 if (STRINGP (gvec[LFACE_BACKGROUND_INDEX]))
3783 Fmodify_frame_parameters (frame,
3784 list1 (Fcons (Qbackground_color,
3785 gvec[LFACE_BACKGROUND_INDEX])));
3789 return Qnil;
3793 /* The following function is implemented for compatibility with 20.2.
3794 The function is used in x-resolve-fonts when it is asked to
3795 return fonts with the same size as the font of a face. This is
3796 done in fontset.el. */
3798 DEFUN ("face-font", Fface_font, Sface_font, 1, 3, 0,
3799 doc: /* Return the font name of face FACE, or nil if it is unspecified.
3800 The font name is, by default, for ASCII characters.
3801 If the optional argument FRAME is given, report on face FACE in that frame.
3802 If FRAME is t, report on the defaults for face FACE (for new frames).
3803 The font default for a face is either nil, or a list
3804 of the form (bold), (italic) or (bold italic).
3805 If FRAME is omitted or nil, use the selected frame. And, in this case,
3806 if the optional third argument CHARACTER is given,
3807 return the font name used for CHARACTER. */)
3808 (Lisp_Object face, Lisp_Object frame, Lisp_Object character)
3810 if (EQ (frame, Qt))
3812 Lisp_Object result = Qnil;
3813 Lisp_Object lface = lface_from_face_name (NULL, face, 1);
3815 if (!UNSPECIFIEDP (LFACE_WEIGHT (lface))
3816 && !EQ (LFACE_WEIGHT (lface), Qnormal))
3817 result = Fcons (Qbold, result);
3819 if (!UNSPECIFIEDP (LFACE_SLANT (lface))
3820 && !EQ (LFACE_SLANT (lface), Qnormal))
3821 result = Fcons (Qitalic, result);
3823 return result;
3825 else
3827 struct frame *f = decode_live_frame (frame);
3828 int face_id = lookup_named_face (f, face, 1);
3829 struct face *fface = FACE_FROM_ID (f, face_id);
3831 if (! fface)
3832 return Qnil;
3833 #ifdef HAVE_WINDOW_SYSTEM
3834 if (FRAME_WINDOW_P (f) && !NILP (character))
3836 CHECK_CHARACTER (character);
3837 face_id = FACE_FOR_CHAR (f, fface, XINT (character), -1, Qnil);
3838 fface = FACE_FROM_ID (f, face_id);
3840 return (fface->font
3841 ? fface->font->props[FONT_NAME_INDEX]
3842 : Qnil);
3843 #else /* !HAVE_WINDOW_SYSTEM */
3844 return build_string (FRAME_MSDOS_P (f)
3845 ? "ms-dos"
3846 : FRAME_W32_P (f) ? "w32term"
3847 :"tty");
3848 #endif
3853 /* Compare face-attribute values v1 and v2 for equality. Value is non-zero if
3854 all attributes are `equal'. Tries to be fast because this function
3855 is called quite often. */
3857 static bool
3858 face_attr_equal_p (Lisp_Object v1, Lisp_Object v2)
3860 /* Type can differ, e.g. when one attribute is unspecified, i.e. nil,
3861 and the other is specified. */
3862 if (XTYPE (v1) != XTYPE (v2))
3863 return 0;
3865 if (EQ (v1, v2))
3866 return 1;
3868 switch (XTYPE (v1))
3870 case Lisp_String:
3871 if (SBYTES (v1) != SBYTES (v2))
3872 return 0;
3874 return memcmp (SDATA (v1), SDATA (v2), SBYTES (v1)) == 0;
3876 case_Lisp_Int:
3877 case Lisp_Symbol:
3878 return 0;
3880 default:
3881 return !NILP (Fequal (v1, v2));
3886 /* Compare face vectors V1 and V2 for equality. Value is non-zero if
3887 all attributes are `equal'. Tries to be fast because this function
3888 is called quite often. */
3890 static bool
3891 lface_equal_p (Lisp_Object *v1, Lisp_Object *v2)
3893 int i;
3894 bool equal_p = 1;
3896 for (i = 1; i < LFACE_VECTOR_SIZE && equal_p; ++i)
3897 equal_p = face_attr_equal_p (v1[i], v2[i]);
3899 return equal_p;
3903 DEFUN ("internal-lisp-face-equal-p", Finternal_lisp_face_equal_p,
3904 Sinternal_lisp_face_equal_p, 2, 3, 0,
3905 doc: /* True if FACE1 and FACE2 are equal.
3906 If the optional argument FRAME is given, report on FACE1 and FACE2 in that frame.
3907 If FRAME is t, report on the defaults for FACE1 and FACE2 (for new frames).
3908 If FRAME is omitted or nil, use the selected frame. */)
3909 (Lisp_Object face1, Lisp_Object face2, Lisp_Object frame)
3911 int equal_p;
3912 struct frame *f;
3913 Lisp_Object lface1, lface2;
3915 /* Don't use decode_window_system_frame here because this function
3916 is called before X frames exist. At that time, if FRAME is nil,
3917 selected_frame will be used which is the frame dumped with
3918 Emacs. That frame is not an X frame. */
3919 f = EQ (frame, Qt) ? NULL : decode_live_frame (frame);
3921 lface1 = lface_from_face_name (f, face1, 1);
3922 lface2 = lface_from_face_name (f, face2, 1);
3923 equal_p = lface_equal_p (XVECTOR (lface1)->contents,
3924 XVECTOR (lface2)->contents);
3925 return equal_p ? Qt : Qnil;
3929 DEFUN ("internal-lisp-face-empty-p", Finternal_lisp_face_empty_p,
3930 Sinternal_lisp_face_empty_p, 1, 2, 0,
3931 doc: /* True if FACE has no attribute specified.
3932 If the optional argument FRAME is given, report on face FACE in that frame.
3933 If FRAME is t, report on the defaults for face FACE (for new frames).
3934 If FRAME is omitted or nil, use the selected frame. */)
3935 (Lisp_Object face, Lisp_Object frame)
3937 struct frame *f = EQ (frame, Qt) ? NULL : decode_live_frame (frame);
3938 Lisp_Object lface = lface_from_face_name (f, face, 1);
3939 int i;
3941 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3942 if (!UNSPECIFIEDP (AREF (lface, i)))
3943 break;
3945 return i == LFACE_VECTOR_SIZE ? Qt : Qnil;
3949 DEFUN ("frame-face-alist", Fframe_face_alist, Sframe_face_alist,
3950 0, 1, 0,
3951 doc: /* Return an alist of frame-local faces defined on FRAME.
3952 For internal use only. */)
3953 (Lisp_Object frame)
3955 return decode_live_frame (frame)->face_alist;
3959 /* Return a hash code for Lisp string STRING with case ignored. Used
3960 below in computing a hash value for a Lisp face. */
3962 static unsigned
3963 hash_string_case_insensitive (Lisp_Object string)
3965 const unsigned char *s;
3966 unsigned hash = 0;
3967 eassert (STRINGP (string));
3968 for (s = SDATA (string); *s; ++s)
3969 hash = (hash << 1) ^ c_tolower (*s);
3970 return hash;
3974 /* Return a hash code for face attribute vector V. */
3976 static unsigned
3977 lface_hash (Lisp_Object *v)
3979 return (hash_string_case_insensitive (v[LFACE_FAMILY_INDEX])
3980 ^ hash_string_case_insensitive (v[LFACE_FOUNDRY_INDEX])
3981 ^ hash_string_case_insensitive (v[LFACE_FOREGROUND_INDEX])
3982 ^ hash_string_case_insensitive (v[LFACE_BACKGROUND_INDEX])
3983 ^ XHASH (v[LFACE_WEIGHT_INDEX])
3984 ^ XHASH (v[LFACE_SLANT_INDEX])
3985 ^ XHASH (v[LFACE_SWIDTH_INDEX])
3986 ^ XHASH (v[LFACE_HEIGHT_INDEX]));
3990 /* Return non-zero if LFACE1 and LFACE2 specify the same font (without
3991 considering charsets/registries). They do if they specify the same
3992 family, point size, weight, width, slant, and font. Both
3993 LFACE1 and LFACE2 must be fully-specified. */
3995 static int
3996 lface_same_font_attributes_p (Lisp_Object *lface1, Lisp_Object *lface2)
3998 eassert (lface_fully_specified_p (lface1)
3999 && lface_fully_specified_p (lface2));
4000 return (xstrcasecmp (SSDATA (lface1[LFACE_FAMILY_INDEX]),
4001 SSDATA (lface2[LFACE_FAMILY_INDEX])) == 0
4002 && xstrcasecmp (SSDATA (lface1[LFACE_FOUNDRY_INDEX]),
4003 SSDATA (lface2[LFACE_FOUNDRY_INDEX])) == 0
4004 && EQ (lface1[LFACE_HEIGHT_INDEX], lface2[LFACE_HEIGHT_INDEX])
4005 && EQ (lface1[LFACE_SWIDTH_INDEX], lface2[LFACE_SWIDTH_INDEX])
4006 && EQ (lface1[LFACE_WEIGHT_INDEX], lface2[LFACE_WEIGHT_INDEX])
4007 && EQ (lface1[LFACE_SLANT_INDEX], lface2[LFACE_SLANT_INDEX])
4008 && EQ (lface1[LFACE_FONT_INDEX], lface2[LFACE_FONT_INDEX])
4009 && (EQ (lface1[LFACE_FONTSET_INDEX], lface2[LFACE_FONTSET_INDEX])
4010 || (STRINGP (lface1[LFACE_FONTSET_INDEX])
4011 && STRINGP (lface2[LFACE_FONTSET_INDEX])
4012 && ! xstrcasecmp (SSDATA (lface1[LFACE_FONTSET_INDEX]),
4013 SSDATA (lface2[LFACE_FONTSET_INDEX]))))
4019 /***********************************************************************
4020 Realized Faces
4021 ***********************************************************************/
4023 /* Allocate and return a new realized face for Lisp face attribute
4024 vector ATTR. */
4026 static struct face *
4027 make_realized_face (Lisp_Object *attr)
4029 struct face *face = xzalloc (sizeof *face);
4030 face->ascii_face = face;
4031 memcpy (face->lface, attr, sizeof face->lface);
4032 return face;
4036 /* Free realized face FACE, including its X resources. FACE may
4037 be null. */
4039 static void
4040 free_realized_face (struct frame *f, struct face *face)
4042 if (face)
4044 #ifdef HAVE_WINDOW_SYSTEM
4045 if (FRAME_WINDOW_P (f))
4047 /* Free fontset of FACE if it is ASCII face. */
4048 if (face->fontset >= 0 && face == face->ascii_face)
4049 free_face_fontset (f, face);
4050 if (face->gc)
4052 block_input ();
4053 if (face->font)
4054 font_done_for_face (f, face);
4055 x_free_gc (f, face->gc);
4056 face->gc = 0;
4057 unblock_input ();
4060 free_face_colors (f, face);
4061 x_destroy_bitmap (f, face->stipple);
4063 #endif /* HAVE_WINDOW_SYSTEM */
4065 xfree (face);
4070 /* Prepare face FACE for subsequent display on frame F. This
4071 allocated GCs if they haven't been allocated yet or have been freed
4072 by clearing the face cache. */
4074 void
4075 prepare_face_for_display (struct frame *f, struct face *face)
4077 #ifdef HAVE_WINDOW_SYSTEM
4078 eassert (FRAME_WINDOW_P (f));
4080 if (face->gc == 0)
4082 XGCValues xgcv;
4083 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
4085 xgcv.foreground = face->foreground;
4086 xgcv.background = face->background;
4087 #ifdef HAVE_X_WINDOWS
4088 xgcv.graphics_exposures = False;
4089 #endif
4091 block_input ();
4092 #ifdef HAVE_X_WINDOWS
4093 if (face->stipple)
4095 xgcv.fill_style = FillOpaqueStippled;
4096 xgcv.stipple = x_bitmap_pixmap (f, face->stipple);
4097 mask |= GCFillStyle | GCStipple;
4099 #endif
4100 face->gc = x_create_gc (f, mask, &xgcv);
4101 if (face->font)
4102 font_prepare_for_face (f, face);
4103 unblock_input ();
4105 #endif /* HAVE_WINDOW_SYSTEM */
4109 /* Returns the `distance' between the colors X and Y. */
4111 static int
4112 color_distance (XColor *x, XColor *y)
4114 /* This formula is from a paper titled `Colour metric' by Thiadmer Riemersma.
4115 Quoting from that paper:
4117 This formula has results that are very close to L*u*v* (with the
4118 modified lightness curve) and, more importantly, it is a more even
4119 algorithm: it does not have a range of colors where it suddenly
4120 gives far from optimal results.
4122 See <http://www.compuphase.com/cmetric.htm> for more info. */
4124 long r = (x->red - y->red) >> 8;
4125 long g = (x->green - y->green) >> 8;
4126 long b = (x->blue - y->blue) >> 8;
4127 long r_mean = (x->red + y->red) >> 9;
4129 return
4130 (((512 + r_mean) * r * r) >> 8)
4131 + 4 * g * g
4132 + (((767 - r_mean) * b * b) >> 8);
4136 DEFUN ("color-distance", Fcolor_distance, Scolor_distance, 2, 3, 0,
4137 doc: /* Return an integer distance between COLOR1 and COLOR2 on FRAME.
4138 COLOR1 and COLOR2 may be either strings containing the color name,
4139 or lists of the form (RED GREEN BLUE).
4140 If FRAME is unspecified or nil, the current frame is used. */)
4141 (Lisp_Object color1, Lisp_Object color2, Lisp_Object frame)
4143 struct frame *f = decode_live_frame (frame);
4144 XColor cdef1, cdef2;
4146 if (!(CONSP (color1) && parse_rgb_list (color1, &cdef1))
4147 && !(STRINGP (color1) && defined_color (f, SSDATA (color1), &cdef1, 0)))
4148 signal_error ("Invalid color", color1);
4149 if (!(CONSP (color2) && parse_rgb_list (color2, &cdef2))
4150 && !(STRINGP (color2) && defined_color (f, SSDATA (color2), &cdef2, 0)))
4151 signal_error ("Invalid color", color2);
4153 return make_number (color_distance (&cdef1, &cdef2));
4157 /***********************************************************************
4158 Face Cache
4159 ***********************************************************************/
4161 /* Return a new face cache for frame F. */
4163 static struct face_cache *
4164 make_face_cache (struct frame *f)
4166 struct face_cache *c = xmalloc (sizeof *c);
4168 c->buckets = xzalloc (FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets);
4169 c->size = 50;
4170 c->used = 0;
4171 c->faces_by_id = xmalloc (c->size * sizeof *c->faces_by_id);
4172 c->f = f;
4173 c->menu_face_changed_p = menu_face_changed_default;
4174 return c;
4178 /* Clear out all graphics contexts for all realized faces, except for
4179 the basic faces. This should be done from time to time just to avoid
4180 keeping too many graphics contexts that are no longer needed. */
4182 static void
4183 clear_face_gcs (struct face_cache *c)
4185 if (c && FRAME_WINDOW_P (c->f))
4187 #ifdef HAVE_WINDOW_SYSTEM
4188 int i;
4189 for (i = BASIC_FACE_ID_SENTINEL; i < c->used; ++i)
4191 struct face *face = c->faces_by_id[i];
4192 if (face && face->gc)
4194 block_input ();
4195 if (face->font)
4196 font_done_for_face (c->f, face);
4197 x_free_gc (c->f, face->gc);
4198 face->gc = 0;
4199 unblock_input ();
4202 #endif /* HAVE_WINDOW_SYSTEM */
4207 /* Free all realized faces in face cache C, including basic faces.
4208 C may be null. If faces are freed, make sure the frame's current
4209 matrix is marked invalid, so that a display caused by an expose
4210 event doesn't try to use faces we destroyed. */
4212 static void
4213 free_realized_faces (struct face_cache *c)
4215 if (c && c->used)
4217 int i, size;
4218 struct frame *f = c->f;
4220 /* We must block input here because we can't process X events
4221 safely while only some faces are freed, or when the frame's
4222 current matrix still references freed faces. */
4223 block_input ();
4225 for (i = 0; i < c->used; ++i)
4227 free_realized_face (f, c->faces_by_id[i]);
4228 c->faces_by_id[i] = NULL;
4231 c->used = 0;
4232 size = FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
4233 memset (c->buckets, 0, size);
4235 /* Must do a thorough redisplay the next time. Mark current
4236 matrices as invalid because they will reference faces freed
4237 above. This function is also called when a frame is
4238 destroyed. In this case, the root window of F is nil. */
4239 if (WINDOWP (f->root_window))
4241 clear_current_matrices (f);
4242 ++windows_or_buffers_changed;
4245 unblock_input ();
4250 /* Free all realized faces on FRAME or on all frames if FRAME is nil.
4251 This is done after attributes of a named face have been changed,
4252 because we can't tell which realized faces depend on that face. */
4254 void
4255 free_all_realized_faces (Lisp_Object frame)
4257 if (NILP (frame))
4259 Lisp_Object rest;
4260 FOR_EACH_FRAME (rest, frame)
4261 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
4263 else
4264 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
4268 /* Free face cache C and faces in it, including their X resources. */
4270 static void
4271 free_face_cache (struct face_cache *c)
4273 if (c)
4275 free_realized_faces (c);
4276 xfree (c->buckets);
4277 xfree (c->faces_by_id);
4278 xfree (c);
4283 /* Cache realized face FACE in face cache C. HASH is the hash value
4284 of FACE. If FACE is for ASCII characters (i.e. FACE->ascii_face ==
4285 FACE), insert the new face to the beginning of the collision list
4286 of the face hash table of C. Otherwise, add the new face to the
4287 end of the collision list. This way, lookup_face can quickly find
4288 that a requested face is not cached. */
4290 static void
4291 cache_face (struct face_cache *c, struct face *face, unsigned int hash)
4293 int i = hash % FACE_CACHE_BUCKETS_SIZE;
4295 face->hash = hash;
4297 if (face->ascii_face != face)
4299 struct face *last = c->buckets[i];
4300 if (last)
4302 while (last->next)
4303 last = last->next;
4304 last->next = face;
4305 face->prev = last;
4306 face->next = NULL;
4308 else
4310 c->buckets[i] = face;
4311 face->prev = face->next = NULL;
4314 else
4316 face->prev = NULL;
4317 face->next = c->buckets[i];
4318 if (face->next)
4319 face->next->prev = face;
4320 c->buckets[i] = face;
4323 /* Find a free slot in C->faces_by_id and use the index of the free
4324 slot as FACE->id. */
4325 for (i = 0; i < c->used; ++i)
4326 if (c->faces_by_id[i] == NULL)
4327 break;
4328 face->id = i;
4330 #ifdef GLYPH_DEBUG
4331 /* Check that FACE got a unique id. */
4333 int j, n;
4334 struct face *face1;
4336 for (j = n = 0; j < FACE_CACHE_BUCKETS_SIZE; ++j)
4337 for (face1 = c->buckets[j]; face1; face1 = face1->next)
4338 if (face1->id == i)
4339 ++n;
4341 eassert (n == 1);
4343 #endif /* GLYPH_DEBUG */
4345 /* Maybe enlarge C->faces_by_id. */
4346 if (i == c->used)
4348 if (c->used == c->size)
4349 c->faces_by_id = xpalloc (c->faces_by_id, &c->size, 1, MAX_FACE_ID,
4350 sizeof *c->faces_by_id);
4351 c->used++;
4354 c->faces_by_id[i] = face;
4358 /* Remove face FACE from cache C. */
4360 static void
4361 uncache_face (struct face_cache *c, struct face *face)
4363 int i = face->hash % FACE_CACHE_BUCKETS_SIZE;
4365 if (face->prev)
4366 face->prev->next = face->next;
4367 else
4368 c->buckets[i] = face->next;
4370 if (face->next)
4371 face->next->prev = face->prev;
4373 c->faces_by_id[face->id] = NULL;
4374 if (face->id == c->used)
4375 --c->used;
4379 /* Look up a realized face with face attributes ATTR in the face cache
4380 of frame F. The face will be used to display ASCII characters.
4381 Value is the ID of the face found. If no suitable face is found,
4382 realize a new one. */
4384 static int
4385 lookup_face (struct frame *f, Lisp_Object *attr)
4387 struct face_cache *cache = FRAME_FACE_CACHE (f);
4388 unsigned hash;
4389 int i;
4390 struct face *face;
4392 eassert (cache != NULL);
4393 check_lface_attrs (attr);
4395 /* Look up ATTR in the face cache. */
4396 hash = lface_hash (attr);
4397 i = hash % FACE_CACHE_BUCKETS_SIZE;
4399 for (face = cache->buckets[i]; face; face = face->next)
4401 if (face->ascii_face != face)
4403 /* There's no more ASCII face. */
4404 face = NULL;
4405 break;
4407 if (face->hash == hash
4408 && lface_equal_p (face->lface, attr))
4409 break;
4412 /* If not found, realize a new face. */
4413 if (face == NULL)
4414 face = realize_face (cache, attr, -1);
4416 #ifdef GLYPH_DEBUG
4417 eassert (face == FACE_FROM_ID (f, face->id));
4418 #endif /* GLYPH_DEBUG */
4420 return face->id;
4423 #ifdef HAVE_WINDOW_SYSTEM
4424 /* Look up a realized face that has the same attributes as BASE_FACE
4425 except for the font in the face cache of frame F. If FONT-OBJECT
4426 is not nil, it is an already opened font. If FONT-OBJECT is nil,
4427 the face has no font. Value is the ID of the face found. If no
4428 suitable face is found, realize a new one. */
4431 face_for_font (struct frame *f, Lisp_Object font_object, struct face *base_face)
4433 struct face_cache *cache = FRAME_FACE_CACHE (f);
4434 unsigned hash;
4435 int i;
4436 struct face *face;
4438 eassert (cache != NULL);
4439 base_face = base_face->ascii_face;
4440 hash = lface_hash (base_face->lface);
4441 i = hash % FACE_CACHE_BUCKETS_SIZE;
4443 for (face = cache->buckets[i]; face; face = face->next)
4445 if (face->ascii_face == face)
4446 continue;
4447 if (face->ascii_face == base_face
4448 && face->font == (NILP (font_object) ? NULL
4449 : XFONT_OBJECT (font_object))
4450 && lface_equal_p (face->lface, base_face->lface))
4451 return face->id;
4454 /* If not found, realize a new face. */
4455 face = realize_non_ascii_face (f, font_object, base_face);
4456 return face->id;
4458 #endif /* HAVE_WINDOW_SYSTEM */
4460 /* Return the face id of the realized face for named face SYMBOL on
4461 frame F suitable for displaying ASCII characters. Value is -1 if
4462 the face couldn't be determined, which might happen if the default
4463 face isn't realized and cannot be realized. */
4466 lookup_named_face (struct frame *f, Lisp_Object symbol, int signal_p)
4468 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4469 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
4470 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4472 if (default_face == NULL)
4474 if (!realize_basic_faces (f))
4475 return -1;
4476 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4477 if (default_face == NULL)
4478 emacs_abort (); /* realize_basic_faces must have set it up */
4481 if (! get_lface_attributes (f, symbol, symbol_attrs, signal_p, 0))
4482 return -1;
4484 memcpy (attrs, default_face->lface, sizeof attrs);
4485 merge_face_vectors (f, symbol_attrs, attrs, 0);
4487 return lookup_face (f, attrs);
4491 /* Return the display face-id of the basic face whose canonical face-id
4492 is FACE_ID. The return value will usually simply be FACE_ID, unless that
4493 basic face has bee remapped via Vface_remapping_alist. This function is
4494 conservative: if something goes wrong, it will simply return FACE_ID
4495 rather than signal an error. */
4498 lookup_basic_face (struct frame *f, int face_id)
4500 Lisp_Object name, mapping;
4501 int remapped_face_id;
4503 if (NILP (Vface_remapping_alist))
4504 return face_id; /* Nothing to do. */
4506 switch (face_id)
4508 case DEFAULT_FACE_ID: name = Qdefault; break;
4509 case MODE_LINE_FACE_ID: name = Qmode_line; break;
4510 case MODE_LINE_INACTIVE_FACE_ID: name = Qmode_line_inactive; break;
4511 case HEADER_LINE_FACE_ID: name = Qheader_line; break;
4512 case TOOL_BAR_FACE_ID: name = Qtool_bar; break;
4513 case FRINGE_FACE_ID: name = Qfringe; break;
4514 case SCROLL_BAR_FACE_ID: name = Qscroll_bar; break;
4515 case BORDER_FACE_ID: name = Qborder; break;
4516 case CURSOR_FACE_ID: name = Qcursor; break;
4517 case MOUSE_FACE_ID: name = Qmouse; break;
4518 case MENU_FACE_ID: name = Qmenu; break;
4520 default:
4521 emacs_abort (); /* the caller is supposed to pass us a basic face id */
4524 /* Do a quick scan through Vface_remapping_alist, and return immediately
4525 if there is no remapping for face NAME. This is just an optimization
4526 for the very common no-remapping case. */
4527 mapping = assq_no_quit (name, Vface_remapping_alist);
4528 if (NILP (mapping))
4529 return face_id; /* Give up. */
4531 /* If there is a remapping entry, lookup the face using NAME, which will
4532 handle the remapping too. */
4533 remapped_face_id = lookup_named_face (f, name, 0);
4534 if (remapped_face_id < 0)
4535 return face_id; /* Give up. */
4537 return remapped_face_id;
4541 /* Return a face for charset ASCII that is like the face with id
4542 FACE_ID on frame F, but has a font that is STEPS steps smaller.
4543 STEPS < 0 means larger. Value is the id of the face. */
4546 smaller_face (struct frame *f, int face_id, int steps)
4548 #ifdef HAVE_WINDOW_SYSTEM
4549 struct face *face;
4550 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4551 int pt, last_pt, last_height;
4552 int delta;
4553 int new_face_id;
4554 struct face *new_face;
4556 /* If not called for an X frame, just return the original face. */
4557 if (FRAME_TERMCAP_P (f))
4558 return face_id;
4560 /* Try in increments of 1/2 pt. */
4561 delta = steps < 0 ? 5 : -5;
4562 steps = eabs (steps);
4564 face = FACE_FROM_ID (f, face_id);
4565 memcpy (attrs, face->lface, sizeof attrs);
4566 pt = last_pt = XFASTINT (attrs[LFACE_HEIGHT_INDEX]);
4567 new_face_id = face_id;
4568 last_height = FONT_HEIGHT (face->font);
4570 while (steps
4571 && pt + delta > 0
4572 /* Give up if we cannot find a font within 10pt. */
4573 && eabs (last_pt - pt) < 100)
4575 /* Look up a face for a slightly smaller/larger font. */
4576 pt += delta;
4577 attrs[LFACE_HEIGHT_INDEX] = make_number (pt);
4578 new_face_id = lookup_face (f, attrs);
4579 new_face = FACE_FROM_ID (f, new_face_id);
4581 /* If height changes, count that as one step. */
4582 if ((delta < 0 && FONT_HEIGHT (new_face->font) < last_height)
4583 || (delta > 0 && FONT_HEIGHT (new_face->font) > last_height))
4585 --steps;
4586 last_height = FONT_HEIGHT (new_face->font);
4587 last_pt = pt;
4591 return new_face_id;
4593 #else /* not HAVE_WINDOW_SYSTEM */
4595 return face_id;
4597 #endif /* not HAVE_WINDOW_SYSTEM */
4601 /* Return a face for charset ASCII that is like the face with id
4602 FACE_ID on frame F, but has height HEIGHT. */
4605 face_with_height (struct frame *f, int face_id, int height)
4607 #ifdef HAVE_WINDOW_SYSTEM
4608 struct face *face;
4609 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4611 if (FRAME_TERMCAP_P (f)
4612 || height <= 0)
4613 return face_id;
4615 face = FACE_FROM_ID (f, face_id);
4616 memcpy (attrs, face->lface, sizeof attrs);
4617 attrs[LFACE_HEIGHT_INDEX] = make_number (height);
4618 font_clear_prop (attrs, FONT_SIZE_INDEX);
4619 face_id = lookup_face (f, attrs);
4620 #endif /* HAVE_WINDOW_SYSTEM */
4622 return face_id;
4626 /* Return the face id of the realized face for named face SYMBOL on
4627 frame F suitable for displaying ASCII characters, and use
4628 attributes of the face FACE_ID for attributes that aren't
4629 completely specified by SYMBOL. This is like lookup_named_face,
4630 except that the default attributes come from FACE_ID, not from the
4631 default face. FACE_ID is assumed to be already realized. */
4634 lookup_derived_face (struct frame *f, Lisp_Object symbol, int face_id,
4635 int signal_p)
4637 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4638 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
4639 struct face *default_face = FACE_FROM_ID (f, face_id);
4641 if (!default_face)
4642 emacs_abort ();
4644 if (!get_lface_attributes (f, symbol, symbol_attrs, signal_p, 0))
4645 return -1;
4647 memcpy (attrs, default_face->lface, sizeof attrs);
4648 merge_face_vectors (f, symbol_attrs, attrs, 0);
4649 return lookup_face (f, attrs);
4652 DEFUN ("face-attributes-as-vector", Fface_attributes_as_vector,
4653 Sface_attributes_as_vector, 1, 1, 0,
4654 doc: /* Return a vector of face attributes corresponding to PLIST. */)
4655 (Lisp_Object plist)
4657 Lisp_Object lface;
4658 lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
4659 Qunspecified);
4660 merge_face_ref (XFRAME (selected_frame), plist, XVECTOR (lface)->contents,
4661 1, 0);
4662 return lface;
4667 /***********************************************************************
4668 Face capability testing
4669 ***********************************************************************/
4672 /* If the distance (as returned by color_distance) between two colors is
4673 less than this, then they are considered the same, for determining
4674 whether a color is supported or not. The range of values is 0-65535. */
4676 #define TTY_SAME_COLOR_THRESHOLD 10000
4678 #ifdef HAVE_WINDOW_SYSTEM
4680 /* Return non-zero if all the face attributes in ATTRS are supported
4681 on the window-system frame F.
4683 The definition of `supported' is somewhat heuristic, but basically means
4684 that a face containing all the attributes in ATTRS, when merged with the
4685 default face for display, can be represented in a way that's
4687 \(1) different in appearance than the default face, and
4688 \(2) `close in spirit' to what the attributes specify, if not exact. */
4690 static int
4691 x_supports_face_attributes_p (struct frame *f,
4692 Lisp_Object attrs[LFACE_VECTOR_SIZE],
4693 struct face *def_face)
4695 Lisp_Object *def_attrs = def_face->lface;
4697 /* Check that other specified attributes are different that the default
4698 face. */
4699 if ((!UNSPECIFIEDP (attrs[LFACE_UNDERLINE_INDEX])
4700 && face_attr_equal_p (attrs[LFACE_UNDERLINE_INDEX],
4701 def_attrs[LFACE_UNDERLINE_INDEX]))
4702 || (!UNSPECIFIEDP (attrs[LFACE_INVERSE_INDEX])
4703 && face_attr_equal_p (attrs[LFACE_INVERSE_INDEX],
4704 def_attrs[LFACE_INVERSE_INDEX]))
4705 || (!UNSPECIFIEDP (attrs[LFACE_FOREGROUND_INDEX])
4706 && face_attr_equal_p (attrs[LFACE_FOREGROUND_INDEX],
4707 def_attrs[LFACE_FOREGROUND_INDEX]))
4708 || (!UNSPECIFIEDP (attrs[LFACE_BACKGROUND_INDEX])
4709 && face_attr_equal_p (attrs[LFACE_BACKGROUND_INDEX],
4710 def_attrs[LFACE_BACKGROUND_INDEX]))
4711 || (!UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
4712 && face_attr_equal_p (attrs[LFACE_STIPPLE_INDEX],
4713 def_attrs[LFACE_STIPPLE_INDEX]))
4714 || (!UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
4715 && face_attr_equal_p (attrs[LFACE_OVERLINE_INDEX],
4716 def_attrs[LFACE_OVERLINE_INDEX]))
4717 || (!UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
4718 && face_attr_equal_p (attrs[LFACE_STRIKE_THROUGH_INDEX],
4719 def_attrs[LFACE_STRIKE_THROUGH_INDEX]))
4720 || (!UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
4721 && face_attr_equal_p (attrs[LFACE_BOX_INDEX],
4722 def_attrs[LFACE_BOX_INDEX])))
4723 return 0;
4725 /* Check font-related attributes, as those are the most commonly
4726 "unsupported" on a window-system (because of missing fonts). */
4727 if (!UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
4728 || !UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
4729 || !UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
4730 || !UNSPECIFIEDP (attrs[LFACE_WEIGHT_INDEX])
4731 || !UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX])
4732 || !UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX]))
4734 int face_id;
4735 struct face *face;
4736 Lisp_Object merged_attrs[LFACE_VECTOR_SIZE];
4737 int i;
4739 memcpy (merged_attrs, def_attrs, sizeof merged_attrs);
4741 merge_face_vectors (f, attrs, merged_attrs, 0);
4743 face_id = lookup_face (f, merged_attrs);
4744 face = FACE_FROM_ID (f, face_id);
4746 if (! face)
4747 error ("Cannot make face");
4749 /* If the font is the same, or no font is found, then not
4750 supported. */
4751 if (face->font == def_face->font
4752 || ! face->font)
4753 return 0;
4754 for (i = FONT_TYPE_INDEX; i <= FONT_SIZE_INDEX; i++)
4755 if (! EQ (face->font->props[i], def_face->font->props[i]))
4757 Lisp_Object s1, s2;
4759 if (i < FONT_FOUNDRY_INDEX || i > FONT_REGISTRY_INDEX
4760 || face->font->driver->case_sensitive)
4761 return 1;
4762 s1 = SYMBOL_NAME (face->font->props[i]);
4763 s2 = SYMBOL_NAME (def_face->font->props[i]);
4764 if (! EQ (Fcompare_strings (s1, make_number (0), Qnil,
4765 s2, make_number (0), Qnil, Qt), Qt))
4766 return 1;
4768 return 0;
4771 /* Everything checks out, this face is supported. */
4772 return 1;
4775 #endif /* HAVE_WINDOW_SYSTEM */
4777 /* Return non-zero if all the face attributes in ATTRS are supported
4778 on the tty frame F.
4780 The definition of `supported' is somewhat heuristic, but basically means
4781 that a face containing all the attributes in ATTRS, when merged
4782 with the default face for display, can be represented in a way that's
4784 \(1) different in appearance than the default face, and
4785 \(2) `close in spirit' to what the attributes specify, if not exact.
4787 Point (2) implies that a `:weight black' attribute will be satisfied
4788 by any terminal that can display bold, and a `:foreground "yellow"' as
4789 long as the terminal can display a yellowish color, but `:slant italic'
4790 will _not_ be satisfied by the tty display code's automatic
4791 substitution of a `dim' face for italic. */
4793 static int
4794 tty_supports_face_attributes_p (struct frame *f,
4795 Lisp_Object attrs[LFACE_VECTOR_SIZE],
4796 struct face *def_face)
4798 int weight, slant;
4799 Lisp_Object val, fg, bg;
4800 XColor fg_tty_color, fg_std_color;
4801 XColor bg_tty_color, bg_std_color;
4802 unsigned test_caps = 0;
4803 Lisp_Object *def_attrs = def_face->lface;
4805 /* First check some easy-to-check stuff; ttys support none of the
4806 following attributes, so we can just return false if any are requested
4807 (even if `nominal' values are specified, we should still return false,
4808 as that will be the same value that the default face uses). We
4809 consider :slant unsupportable on ttys, even though the face code
4810 actually `fakes' them using a dim attribute if possible. This is
4811 because the faked result is too different from what the face
4812 specifies. */
4813 if (!UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
4814 || !UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
4815 || !UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
4816 || !UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
4817 || !UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
4818 || !UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
4819 || !UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
4820 || !UNSPECIFIEDP (attrs[LFACE_BOX_INDEX]))
4821 return 0;
4823 /* Test for terminal `capabilities' (non-color character attributes). */
4825 /* font weight (bold/dim) */
4826 val = attrs[LFACE_WEIGHT_INDEX];
4827 if (!UNSPECIFIEDP (val)
4828 && (weight = FONT_WEIGHT_NAME_NUMERIC (val), weight >= 0))
4830 int def_weight = FONT_WEIGHT_NAME_NUMERIC (def_attrs[LFACE_WEIGHT_INDEX]);
4832 if (weight > 100)
4834 if (def_weight > 100)
4835 return 0; /* same as default */
4836 test_caps = TTY_CAP_BOLD;
4838 else if (weight < 100)
4840 if (def_weight < 100)
4841 return 0; /* same as default */
4842 test_caps = TTY_CAP_DIM;
4844 else if (def_weight == 100)
4845 return 0; /* same as default */
4848 /* font slant */
4849 val = attrs[LFACE_SLANT_INDEX];
4850 if (!UNSPECIFIEDP (val)
4851 && (slant = FONT_SLANT_NAME_NUMERIC (val), slant >= 0))
4853 int def_slant = FONT_SLANT_NAME_NUMERIC (def_attrs[LFACE_SLANT_INDEX]);
4854 if (slant == 100 || slant == def_slant)
4855 return 0; /* same as default */
4856 else
4857 test_caps |= TTY_CAP_ITALIC;
4860 /* underlining */
4861 val = attrs[LFACE_UNDERLINE_INDEX];
4862 if (!UNSPECIFIEDP (val))
4864 if (STRINGP (val))
4865 return 0; /* ttys can't use colored underlines */
4866 else if (EQ (CAR_SAFE (val), QCstyle) && EQ (CAR_SAFE (CDR_SAFE (val)), Qwave))
4867 return 0; /* ttys can't use wave underlines */
4868 else if (face_attr_equal_p (val, def_attrs[LFACE_UNDERLINE_INDEX]))
4869 return 0; /* same as default */
4870 else
4871 test_caps |= TTY_CAP_UNDERLINE;
4874 /* inverse video */
4875 val = attrs[LFACE_INVERSE_INDEX];
4876 if (!UNSPECIFIEDP (val))
4878 if (face_attr_equal_p (val, def_attrs[LFACE_INVERSE_INDEX]))
4879 return 0; /* same as default */
4880 else
4881 test_caps |= TTY_CAP_INVERSE;
4885 /* Color testing. */
4887 /* Default the color indices in FG_TTY_COLOR and BG_TTY_COLOR, since
4888 we use them when calling `tty_capable_p' below, even if the face
4889 specifies no colors. */
4890 fg_tty_color.pixel = FACE_TTY_DEFAULT_FG_COLOR;
4891 bg_tty_color.pixel = FACE_TTY_DEFAULT_BG_COLOR;
4893 /* Check if foreground color is close enough. */
4894 fg = attrs[LFACE_FOREGROUND_INDEX];
4895 if (STRINGP (fg))
4897 Lisp_Object def_fg = def_attrs[LFACE_FOREGROUND_INDEX];
4899 if (face_attr_equal_p (fg, def_fg))
4900 return 0; /* same as default */
4901 else if (! tty_lookup_color (f, fg, &fg_tty_color, &fg_std_color))
4902 return 0; /* not a valid color */
4903 else if (color_distance (&fg_tty_color, &fg_std_color)
4904 > TTY_SAME_COLOR_THRESHOLD)
4905 return 0; /* displayed color is too different */
4906 else
4907 /* Make sure the color is really different than the default. */
4909 XColor def_fg_color;
4910 if (tty_lookup_color (f, def_fg, &def_fg_color, 0)
4911 && (color_distance (&fg_tty_color, &def_fg_color)
4912 <= TTY_SAME_COLOR_THRESHOLD))
4913 return 0;
4917 /* Check if background color is close enough. */
4918 bg = attrs[LFACE_BACKGROUND_INDEX];
4919 if (STRINGP (bg))
4921 Lisp_Object def_bg = def_attrs[LFACE_BACKGROUND_INDEX];
4923 if (face_attr_equal_p (bg, def_bg))
4924 return 0; /* same as default */
4925 else if (! tty_lookup_color (f, bg, &bg_tty_color, &bg_std_color))
4926 return 0; /* not a valid color */
4927 else if (color_distance (&bg_tty_color, &bg_std_color)
4928 > TTY_SAME_COLOR_THRESHOLD)
4929 return 0; /* displayed color is too different */
4930 else
4931 /* Make sure the color is really different than the default. */
4933 XColor def_bg_color;
4934 if (tty_lookup_color (f, def_bg, &def_bg_color, 0)
4935 && (color_distance (&bg_tty_color, &def_bg_color)
4936 <= TTY_SAME_COLOR_THRESHOLD))
4937 return 0;
4941 /* If both foreground and background are requested, see if the
4942 distance between them is OK. We just check to see if the distance
4943 between the tty's foreground and background is close enough to the
4944 distance between the standard foreground and background. */
4945 if (STRINGP (fg) && STRINGP (bg))
4947 int delta_delta
4948 = (color_distance (&fg_std_color, &bg_std_color)
4949 - color_distance (&fg_tty_color, &bg_tty_color));
4950 if (delta_delta > TTY_SAME_COLOR_THRESHOLD
4951 || delta_delta < -TTY_SAME_COLOR_THRESHOLD)
4952 return 0;
4956 /* See if the capabilities we selected above are supported, with the
4957 given colors. */
4958 if (test_caps != 0 &&
4959 ! tty_capable_p (FRAME_TTY (f), test_caps, fg_tty_color.pixel,
4960 bg_tty_color.pixel))
4961 return 0;
4964 /* Hmmm, everything checks out, this terminal must support this face. */
4965 return 1;
4969 DEFUN ("display-supports-face-attributes-p",
4970 Fdisplay_supports_face_attributes_p, Sdisplay_supports_face_attributes_p,
4971 1, 2, 0,
4972 doc: /* Return non-nil if all the face attributes in ATTRIBUTES are supported.
4973 The optional argument DISPLAY can be a display name, a frame, or
4974 nil (meaning the selected frame's display).
4976 The definition of `supported' is somewhat heuristic, but basically means
4977 that a face containing all the attributes in ATTRIBUTES, when merged
4978 with the default face for display, can be represented in a way that's
4980 \(1) different in appearance than the default face, and
4981 \(2) `close in spirit' to what the attributes specify, if not exact.
4983 Point (2) implies that a `:weight black' attribute will be satisfied by
4984 any display that can display bold, and a `:foreground \"yellow\"' as long
4985 as it can display a yellowish color, but `:slant italic' will _not_ be
4986 satisfied by the tty display code's automatic substitution of a `dim'
4987 face for italic. */)
4988 (Lisp_Object attributes, Lisp_Object display)
4990 int supports = 0, i;
4991 Lisp_Object frame;
4992 struct frame *f;
4993 struct face *def_face;
4994 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4996 if (noninteractive || !initialized)
4997 /* We may not be able to access low-level face information in batch
4998 mode, or before being dumped, and this function is not going to
4999 be very useful in those cases anyway, so just give up. */
5000 return Qnil;
5002 if (NILP (display))
5003 frame = selected_frame;
5004 else if (FRAMEP (display))
5005 frame = display;
5006 else
5008 /* Find any frame on DISPLAY. */
5009 Lisp_Object tail;
5011 frame = Qnil;
5012 FOR_EACH_FRAME (tail, frame)
5013 if (!NILP (Fequal (Fcdr (Fassq (Qdisplay,
5014 XFRAME (frame)->param_alist)),
5015 display)))
5016 break;
5019 CHECK_LIVE_FRAME (frame);
5020 f = XFRAME (frame);
5022 for (i = 0; i < LFACE_VECTOR_SIZE; i++)
5023 attrs[i] = Qunspecified;
5024 merge_face_ref (f, attributes, attrs, 1, 0);
5026 def_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5027 if (def_face == NULL)
5029 if (! realize_basic_faces (f))
5030 error ("Cannot realize default face");
5031 def_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5032 if (def_face == NULL)
5033 emacs_abort (); /* realize_basic_faces must have set it up */
5036 /* Dispatch to the appropriate handler. */
5037 if (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
5038 supports = tty_supports_face_attributes_p (f, attrs, def_face);
5039 #ifdef HAVE_WINDOW_SYSTEM
5040 else
5041 supports = x_supports_face_attributes_p (f, attrs, def_face);
5042 #endif
5044 return supports ? Qt : Qnil;
5048 /***********************************************************************
5049 Font selection
5050 ***********************************************************************/
5052 DEFUN ("internal-set-font-selection-order",
5053 Finternal_set_font_selection_order,
5054 Sinternal_set_font_selection_order, 1, 1, 0,
5055 doc: /* Set font selection order for face font selection to ORDER.
5056 ORDER must be a list of length 4 containing the symbols `:width',
5057 `:height', `:weight', and `:slant'. Face attributes appearing
5058 first in ORDER are matched first, e.g. if `:height' appears before
5059 `:weight' in ORDER, font selection first tries to find a font with
5060 a suitable height, and then tries to match the font weight.
5061 Value is ORDER. */)
5062 (Lisp_Object order)
5064 Lisp_Object list;
5065 int i;
5066 int indices[DIM (font_sort_order)];
5068 CHECK_LIST (order);
5069 memset (indices, 0, sizeof indices);
5070 i = 0;
5072 for (list = order;
5073 CONSP (list) && i < DIM (indices);
5074 list = XCDR (list), ++i)
5076 Lisp_Object attr = XCAR (list);
5077 int xlfd;
5079 if (EQ (attr, QCwidth))
5080 xlfd = XLFD_SWIDTH;
5081 else if (EQ (attr, QCheight))
5082 xlfd = XLFD_POINT_SIZE;
5083 else if (EQ (attr, QCweight))
5084 xlfd = XLFD_WEIGHT;
5085 else if (EQ (attr, QCslant))
5086 xlfd = XLFD_SLANT;
5087 else
5088 break;
5090 if (indices[i] != 0)
5091 break;
5092 indices[i] = xlfd;
5095 if (!NILP (list) || i != DIM (indices))
5096 signal_error ("Invalid font sort order", order);
5097 for (i = 0; i < DIM (font_sort_order); ++i)
5098 if (indices[i] == 0)
5099 signal_error ("Invalid font sort order", order);
5101 if (memcmp (indices, font_sort_order, sizeof indices) != 0)
5103 memcpy (font_sort_order, indices, sizeof font_sort_order);
5104 free_all_realized_faces (Qnil);
5107 font_update_sort_order (font_sort_order);
5109 return Qnil;
5113 DEFUN ("internal-set-alternative-font-family-alist",
5114 Finternal_set_alternative_font_family_alist,
5115 Sinternal_set_alternative_font_family_alist, 1, 1, 0,
5116 doc: /* Define alternative font families to try in face font selection.
5117 ALIST is an alist of (FAMILY ALTERNATIVE1 ALTERNATIVE2 ...) entries.
5118 Each ALTERNATIVE is tried in order if no fonts of font family FAMILY can
5119 be found. Value is ALIST. */)
5120 (Lisp_Object alist)
5122 Lisp_Object entry, tail, tail2;
5124 CHECK_LIST (alist);
5125 alist = Fcopy_sequence (alist);
5126 for (tail = alist; CONSP (tail); tail = XCDR (tail))
5128 entry = XCAR (tail);
5129 CHECK_LIST (entry);
5130 entry = Fcopy_sequence (entry);
5131 XSETCAR (tail, entry);
5132 for (tail2 = entry; CONSP (tail2); tail2 = XCDR (tail2))
5133 XSETCAR (tail2, Fintern (XCAR (tail2), Qnil));
5136 Vface_alternative_font_family_alist = alist;
5137 free_all_realized_faces (Qnil);
5138 return alist;
5142 DEFUN ("internal-set-alternative-font-registry-alist",
5143 Finternal_set_alternative_font_registry_alist,
5144 Sinternal_set_alternative_font_registry_alist, 1, 1, 0,
5145 doc: /* Define alternative font registries to try in face font selection.
5146 ALIST is an alist of (REGISTRY ALTERNATIVE1 ALTERNATIVE2 ...) entries.
5147 Each ALTERNATIVE is tried in order if no fonts of font registry REGISTRY can
5148 be found. Value is ALIST. */)
5149 (Lisp_Object alist)
5151 Lisp_Object entry, tail, tail2;
5153 CHECK_LIST (alist);
5154 alist = Fcopy_sequence (alist);
5155 for (tail = alist; CONSP (tail); tail = XCDR (tail))
5157 entry = XCAR (tail);
5158 CHECK_LIST (entry);
5159 entry = Fcopy_sequence (entry);
5160 XSETCAR (tail, entry);
5161 for (tail2 = entry; CONSP (tail2); tail2 = XCDR (tail2))
5162 XSETCAR (tail2, Fdowncase (XCAR (tail2)));
5164 Vface_alternative_font_registry_alist = alist;
5165 free_all_realized_faces (Qnil);
5166 return alist;
5170 #ifdef HAVE_WINDOW_SYSTEM
5172 /* Return the fontset id of the base fontset name or alias name given
5173 by the fontset attribute of ATTRS. Value is -1 if the fontset
5174 attribute of ATTRS doesn't name a fontset. */
5176 static int
5177 face_fontset (Lisp_Object attrs[LFACE_VECTOR_SIZE])
5179 Lisp_Object name;
5181 name = attrs[LFACE_FONTSET_INDEX];
5182 if (!STRINGP (name))
5183 return -1;
5184 return fs_query_fontset (name, 0);
5187 #endif /* HAVE_WINDOW_SYSTEM */
5191 /***********************************************************************
5192 Face Realization
5193 ***********************************************************************/
5195 /* Realize basic faces on frame F. Value is zero if frame parameters
5196 of F don't contain enough information needed to realize the default
5197 face. */
5199 static bool
5200 realize_basic_faces (struct frame *f)
5202 bool success_p = 0;
5203 ptrdiff_t count = SPECPDL_INDEX ();
5205 /* Block input here so that we won't be surprised by an X expose
5206 event, for instance, without having the faces set up. */
5207 block_input ();
5208 specbind (Qscalable_fonts_allowed, Qt);
5210 if (realize_default_face (f))
5212 realize_named_face (f, Qmode_line, MODE_LINE_FACE_ID);
5213 realize_named_face (f, Qmode_line_inactive, MODE_LINE_INACTIVE_FACE_ID);
5214 realize_named_face (f, Qtool_bar, TOOL_BAR_FACE_ID);
5215 realize_named_face (f, Qfringe, FRINGE_FACE_ID);
5216 realize_named_face (f, Qheader_line, HEADER_LINE_FACE_ID);
5217 realize_named_face (f, Qscroll_bar, SCROLL_BAR_FACE_ID);
5218 realize_named_face (f, Qborder, BORDER_FACE_ID);
5219 realize_named_face (f, Qcursor, CURSOR_FACE_ID);
5220 realize_named_face (f, Qmouse, MOUSE_FACE_ID);
5221 realize_named_face (f, Qmenu, MENU_FACE_ID);
5222 realize_named_face (f, Qvertical_border, VERTICAL_BORDER_FACE_ID);
5224 /* Reflect changes in the `menu' face in menu bars. */
5225 if (FRAME_FACE_CACHE (f)->menu_face_changed_p)
5227 FRAME_FACE_CACHE (f)->menu_face_changed_p = 0;
5228 #ifdef USE_X_TOOLKIT
5229 if (FRAME_WINDOW_P (f))
5230 x_update_menu_appearance (f);
5231 #endif
5234 success_p = 1;
5237 unbind_to (count, Qnil);
5238 unblock_input ();
5239 return success_p;
5243 /* Realize the default face on frame F. If the face is not fully
5244 specified, make it fully-specified. Attributes of the default face
5245 that are not explicitly specified are taken from frame parameters. */
5247 static bool
5248 realize_default_face (struct frame *f)
5250 struct face_cache *c = FRAME_FACE_CACHE (f);
5251 Lisp_Object lface;
5252 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5253 struct face *face;
5255 /* If the `default' face is not yet known, create it. */
5256 lface = lface_from_face_name (f, Qdefault, 0);
5257 if (NILP (lface))
5259 Lisp_Object frame;
5260 XSETFRAME (frame, f);
5261 lface = Finternal_make_lisp_face (Qdefault, frame);
5264 #ifdef HAVE_WINDOW_SYSTEM
5265 if (FRAME_WINDOW_P (f))
5267 Lisp_Object font_object;
5269 XSETFONT (font_object, FRAME_FONT (f));
5270 set_lface_from_font (f, lface, font_object, f->default_face_done_p);
5271 ASET (lface, LFACE_FONTSET_INDEX, fontset_name (FRAME_FONTSET (f)));
5272 f->default_face_done_p = 1;
5274 #endif /* HAVE_WINDOW_SYSTEM */
5276 if (!FRAME_WINDOW_P (f))
5278 ASET (lface, LFACE_FAMILY_INDEX, build_string ("default"));
5279 ASET (lface, LFACE_FOUNDRY_INDEX, LFACE_FAMILY (lface));
5280 ASET (lface, LFACE_SWIDTH_INDEX, Qnormal);
5281 ASET (lface, LFACE_HEIGHT_INDEX, make_number (1));
5282 if (UNSPECIFIEDP (LFACE_WEIGHT (lface)))
5283 ASET (lface, LFACE_WEIGHT_INDEX, Qnormal);
5284 if (UNSPECIFIEDP (LFACE_SLANT (lface)))
5285 ASET (lface, LFACE_SLANT_INDEX, Qnormal);
5286 if (UNSPECIFIEDP (LFACE_FONTSET (lface)))
5287 ASET (lface, LFACE_FONTSET_INDEX, Qnil);
5290 if (UNSPECIFIEDP (LFACE_UNDERLINE (lface)))
5291 ASET (lface, LFACE_UNDERLINE_INDEX, Qnil);
5293 if (UNSPECIFIEDP (LFACE_OVERLINE (lface)))
5294 ASET (lface, LFACE_OVERLINE_INDEX, Qnil);
5296 if (UNSPECIFIEDP (LFACE_STRIKE_THROUGH (lface)))
5297 ASET (lface, LFACE_STRIKE_THROUGH_INDEX, Qnil);
5299 if (UNSPECIFIEDP (LFACE_BOX (lface)))
5300 ASET (lface, LFACE_BOX_INDEX, Qnil);
5302 if (UNSPECIFIEDP (LFACE_INVERSE (lface)))
5303 ASET (lface, LFACE_INVERSE_INDEX, Qnil);
5305 if (UNSPECIFIEDP (LFACE_FOREGROUND (lface)))
5307 /* This function is called so early that colors are not yet
5308 set in the frame parameter list. */
5309 Lisp_Object color = Fassq (Qforeground_color, f->param_alist);
5311 if (CONSP (color) && STRINGP (XCDR (color)))
5312 ASET (lface, LFACE_FOREGROUND_INDEX, XCDR (color));
5313 else if (FRAME_WINDOW_P (f))
5314 return 0;
5315 else if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
5316 ASET (lface, LFACE_FOREGROUND_INDEX, build_string (unspecified_fg));
5317 else
5318 emacs_abort ();
5321 if (UNSPECIFIEDP (LFACE_BACKGROUND (lface)))
5323 /* This function is called so early that colors are not yet
5324 set in the frame parameter list. */
5325 Lisp_Object color = Fassq (Qbackground_color, f->param_alist);
5326 if (CONSP (color) && STRINGP (XCDR (color)))
5327 ASET (lface, LFACE_BACKGROUND_INDEX, XCDR (color));
5328 else if (FRAME_WINDOW_P (f))
5329 return 0;
5330 else if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
5331 ASET (lface, LFACE_BACKGROUND_INDEX, build_string (unspecified_bg));
5332 else
5333 emacs_abort ();
5336 if (UNSPECIFIEDP (LFACE_STIPPLE (lface)))
5337 ASET (lface, LFACE_STIPPLE_INDEX, Qnil);
5339 /* Realize the face; it must be fully-specified now. */
5340 eassert (lface_fully_specified_p (XVECTOR (lface)->contents));
5341 check_lface (lface);
5342 memcpy (attrs, XVECTOR (lface)->contents, sizeof attrs);
5343 face = realize_face (c, attrs, DEFAULT_FACE_ID);
5345 #ifdef HAVE_WINDOW_SYSTEM
5346 #ifdef HAVE_X_WINDOWS
5347 if (FRAME_X_P (f) && face->font != FRAME_FONT (f))
5349 /* This can happen when making a frame on a display that does
5350 not support the default font. */
5351 if (!face->font)
5352 return 0;
5354 /* Otherwise, the font specified for the frame was not
5355 acceptable as a font for the default face (perhaps because
5356 auto-scaled fonts are rejected), so we must adjust the frame
5357 font. */
5358 x_set_font (f, LFACE_FONT (lface), Qnil);
5360 #endif /* HAVE_X_WINDOWS */
5361 #endif /* HAVE_WINDOW_SYSTEM */
5362 return 1;
5366 /* Realize basic faces other than the default face in face cache C.
5367 SYMBOL is the face name, ID is the face id the realized face must
5368 have. The default face must have been realized already. */
5370 static void
5371 realize_named_face (struct frame *f, Lisp_Object symbol, int id)
5373 struct face_cache *c = FRAME_FACE_CACHE (f);
5374 Lisp_Object lface = lface_from_face_name (f, symbol, 0);
5375 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5376 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
5378 /* The default face must exist and be fully specified. */
5379 get_lface_attributes_no_remap (f, Qdefault, attrs, 1);
5380 check_lface_attrs (attrs);
5381 eassert (lface_fully_specified_p (attrs));
5383 /* If SYMBOL isn't know as a face, create it. */
5384 if (NILP (lface))
5386 Lisp_Object frame;
5387 XSETFRAME (frame, f);
5388 lface = Finternal_make_lisp_face (symbol, frame);
5391 /* Merge SYMBOL's face with the default face. */
5392 get_lface_attributes_no_remap (f, symbol, symbol_attrs, 1);
5393 merge_face_vectors (f, symbol_attrs, attrs, 0);
5395 /* Realize the face. */
5396 realize_face (c, attrs, id);
5400 /* Realize the fully-specified face with attributes ATTRS in face
5401 cache CACHE for ASCII characters. If FORMER_FACE_ID is
5402 non-negative, it is an ID of face to remove before caching the new
5403 face. Value is a pointer to the newly created realized face. */
5405 static struct face *
5406 realize_face (struct face_cache *cache, Lisp_Object attrs[LFACE_VECTOR_SIZE],
5407 int former_face_id)
5409 struct face *face;
5411 /* LFACE must be fully specified. */
5412 eassert (cache != NULL);
5413 check_lface_attrs (attrs);
5415 if (former_face_id >= 0 && cache->used > former_face_id)
5417 /* Remove the former face. */
5418 struct face *former_face = cache->faces_by_id[former_face_id];
5419 uncache_face (cache, former_face);
5420 free_realized_face (cache->f, former_face);
5421 SET_FRAME_GARBAGED (cache->f);
5424 if (FRAME_WINDOW_P (cache->f))
5425 face = realize_x_face (cache, attrs);
5426 else if (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f))
5427 face = realize_tty_face (cache, attrs);
5428 else if (FRAME_INITIAL_P (cache->f))
5430 /* Create a dummy face. */
5431 face = make_realized_face (attrs);
5433 else
5434 emacs_abort ();
5436 /* Insert the new face. */
5437 cache_face (cache, face, lface_hash (attrs));
5438 return face;
5442 #ifdef HAVE_WINDOW_SYSTEM
5443 /* Realize the fully-specified face that uses FONT-OBJECT and has the
5444 same attributes as BASE_FACE except for the font on frame F.
5445 FONT-OBJECT may be nil, in which case, realized a face of
5446 no-font. */
5448 static struct face *
5449 realize_non_ascii_face (struct frame *f, Lisp_Object font_object,
5450 struct face *base_face)
5452 struct face_cache *cache = FRAME_FACE_CACHE (f);
5453 struct face *face;
5455 face = xmalloc (sizeof *face);
5456 *face = *base_face;
5457 face->gc = 0;
5458 face->extra = NULL;
5459 face->overstrike
5460 = (! NILP (font_object)
5461 && FONT_WEIGHT_NAME_NUMERIC (face->lface[LFACE_WEIGHT_INDEX]) > 100
5462 && FONT_WEIGHT_NUMERIC (font_object) <= 100);
5464 /* Don't try to free the colors copied bitwise from BASE_FACE. */
5465 face->colors_copied_bitwise_p = 1;
5466 face->font = NILP (font_object) ? NULL : XFONT_OBJECT (font_object);
5467 face->gc = 0;
5469 cache_face (cache, face, face->hash);
5471 return face;
5473 #endif /* HAVE_WINDOW_SYSTEM */
5476 /* Realize the fully-specified face with attributes ATTRS in face
5477 cache CACHE for ASCII characters. Do it for X frame CACHE->f. If
5478 the new face doesn't share font with the default face, a fontname
5479 is allocated from the heap and set in `font_name' of the new face,
5480 but it is not yet loaded here. Value is a pointer to the newly
5481 created realized face. */
5483 static struct face *
5484 realize_x_face (struct face_cache *cache, Lisp_Object attrs[LFACE_VECTOR_SIZE])
5486 struct face *face = NULL;
5487 #ifdef HAVE_WINDOW_SYSTEM
5488 struct face *default_face;
5489 struct frame *f;
5490 Lisp_Object stipple, underline, overline, strike_through, box;
5492 eassert (FRAME_WINDOW_P (cache->f));
5494 /* Allocate a new realized face. */
5495 face = make_realized_face (attrs);
5496 face->ascii_face = face;
5498 f = cache->f;
5500 /* Determine the font to use. Most of the time, the font will be
5501 the same as the font of the default face, so try that first. */
5502 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5503 if (default_face
5504 && lface_same_font_attributes_p (default_face->lface, attrs))
5506 face->font = default_face->font;
5507 face->fontset
5508 = make_fontset_for_ascii_face (f, default_face->fontset, face);
5510 else
5512 /* If the face attribute ATTRS specifies a fontset, use it as
5513 the base of a new realized fontset. Otherwise, use the same
5514 base fontset as of the default face. The base determines
5515 registry and encoding of a font. It may also determine
5516 foundry and family. The other fields of font name pattern
5517 are constructed from ATTRS. */
5518 int fontset = face_fontset (attrs);
5520 /* If we are realizing the default face, ATTRS should specify a
5521 fontset. In other words, if FONTSET is -1, we are not
5522 realizing the default face, thus the default face should have
5523 already been realized. */
5524 if (fontset == -1)
5526 if (default_face)
5527 fontset = default_face->fontset;
5528 if (fontset == -1)
5529 emacs_abort ();
5531 if (! FONT_OBJECT_P (attrs[LFACE_FONT_INDEX]))
5532 attrs[LFACE_FONT_INDEX]
5533 = font_load_for_lface (f, attrs, attrs[LFACE_FONT_INDEX]);
5534 if (FONT_OBJECT_P (attrs[LFACE_FONT_INDEX]))
5536 face->font = XFONT_OBJECT (attrs[LFACE_FONT_INDEX]);
5537 face->fontset = make_fontset_for_ascii_face (f, fontset, face);
5539 else
5541 face->font = NULL;
5542 face->fontset = -1;
5546 if (face->font
5547 && FONT_WEIGHT_NAME_NUMERIC (attrs[LFACE_WEIGHT_INDEX]) > 100
5548 && FONT_WEIGHT_NUMERIC (attrs[LFACE_FONT_INDEX]) <= 100)
5549 face->overstrike = 1;
5551 /* Load colors, and set remaining attributes. */
5553 load_face_colors (f, face, attrs);
5555 /* Set up box. */
5556 box = attrs[LFACE_BOX_INDEX];
5557 if (STRINGP (box))
5559 /* A simple box of line width 1 drawn in color given by
5560 the string. */
5561 face->box_color = load_color (f, face, attrs[LFACE_BOX_INDEX],
5562 LFACE_BOX_INDEX);
5563 face->box = FACE_SIMPLE_BOX;
5564 face->box_line_width = 1;
5566 else if (INTEGERP (box))
5568 /* Simple box of specified line width in foreground color of the
5569 face. */
5570 eassert (XINT (box) != 0);
5571 face->box = FACE_SIMPLE_BOX;
5572 face->box_line_width = XINT (box);
5573 face->box_color = face->foreground;
5574 face->box_color_defaulted_p = 1;
5576 else if (CONSP (box))
5578 /* `(:width WIDTH :color COLOR :shadow SHADOW)'. SHADOW
5579 being one of `raised' or `sunken'. */
5580 face->box = FACE_SIMPLE_BOX;
5581 face->box_color = face->foreground;
5582 face->box_color_defaulted_p = 1;
5583 face->box_line_width = 1;
5585 while (CONSP (box))
5587 Lisp_Object keyword, value;
5589 keyword = XCAR (box);
5590 box = XCDR (box);
5592 if (!CONSP (box))
5593 break;
5594 value = XCAR (box);
5595 box = XCDR (box);
5597 if (EQ (keyword, QCline_width))
5599 if (INTEGERP (value) && XINT (value) != 0)
5600 face->box_line_width = XINT (value);
5602 else if (EQ (keyword, QCcolor))
5604 if (STRINGP (value))
5606 face->box_color = load_color (f, face, value,
5607 LFACE_BOX_INDEX);
5608 face->use_box_color_for_shadows_p = 1;
5611 else if (EQ (keyword, QCstyle))
5613 if (EQ (value, Qreleased_button))
5614 face->box = FACE_RAISED_BOX;
5615 else if (EQ (value, Qpressed_button))
5616 face->box = FACE_SUNKEN_BOX;
5621 /* Text underline, overline, strike-through. */
5623 underline = attrs[LFACE_UNDERLINE_INDEX];
5624 if (EQ (underline, Qt))
5626 /* Use default color (same as foreground color). */
5627 face->underline_p = 1;
5628 face->underline_type = FACE_UNDER_LINE;
5629 face->underline_defaulted_p = 1;
5630 face->underline_color = 0;
5632 else if (STRINGP (underline))
5634 /* Use specified color. */
5635 face->underline_p = 1;
5636 face->underline_type = FACE_UNDER_LINE;
5637 face->underline_defaulted_p = 0;
5638 face->underline_color
5639 = load_color (f, face, underline,
5640 LFACE_UNDERLINE_INDEX);
5642 else if (NILP (underline))
5644 face->underline_p = 0;
5645 face->underline_defaulted_p = 0;
5646 face->underline_color = 0;
5648 else if (CONSP (underline))
5650 /* `(:color COLOR :style STYLE)'.
5651 STYLE being one of `line' or `wave'. */
5652 face->underline_p = 1;
5653 face->underline_color = 0;
5654 face->underline_defaulted_p = 1;
5655 face->underline_type = FACE_UNDER_LINE;
5657 /* FIXME? This is also not robust about checking the precise form.
5658 See comments in Finternal_set_lisp_face_attribute. */
5659 while (CONSP (underline))
5661 Lisp_Object keyword, value;
5663 keyword = XCAR (underline);
5664 underline = XCDR (underline);
5666 if (!CONSP (underline))
5667 break;
5668 value = XCAR (underline);
5669 underline = XCDR (underline);
5671 if (EQ (keyword, QCcolor))
5673 if (EQ (value, Qforeground_color))
5675 face->underline_defaulted_p = 1;
5676 face->underline_color = 0;
5678 else if (STRINGP (value))
5680 face->underline_defaulted_p = 0;
5681 face->underline_color = load_color (f, face, value,
5682 LFACE_UNDERLINE_INDEX);
5685 else if (EQ (keyword, QCstyle))
5687 if (EQ (value, Qline))
5688 face->underline_type = FACE_UNDER_LINE;
5689 else if (EQ (value, Qwave))
5690 face->underline_type = FACE_UNDER_WAVE;
5695 overline = attrs[LFACE_OVERLINE_INDEX];
5696 if (STRINGP (overline))
5698 face->overline_color
5699 = load_color (f, face, attrs[LFACE_OVERLINE_INDEX],
5700 LFACE_OVERLINE_INDEX);
5701 face->overline_p = 1;
5703 else if (EQ (overline, Qt))
5705 face->overline_color = face->foreground;
5706 face->overline_color_defaulted_p = 1;
5707 face->overline_p = 1;
5710 strike_through = attrs[LFACE_STRIKE_THROUGH_INDEX];
5711 if (STRINGP (strike_through))
5713 face->strike_through_color
5714 = load_color (f, face, attrs[LFACE_STRIKE_THROUGH_INDEX],
5715 LFACE_STRIKE_THROUGH_INDEX);
5716 face->strike_through_p = 1;
5718 else if (EQ (strike_through, Qt))
5720 face->strike_through_color = face->foreground;
5721 face->strike_through_color_defaulted_p = 1;
5722 face->strike_through_p = 1;
5725 stipple = attrs[LFACE_STIPPLE_INDEX];
5726 if (!NILP (stipple))
5727 face->stipple = load_pixmap (f, stipple, &face->pixmap_w, &face->pixmap_h);
5728 #endif /* HAVE_WINDOW_SYSTEM */
5730 return face;
5734 /* Map a specified color of face FACE on frame F to a tty color index.
5735 IDX is either LFACE_FOREGROUND_INDEX or LFACE_BACKGROUND_INDEX, and
5736 specifies which color to map. Set *DEFAULTED to 1 if mapping to the
5737 default foreground/background colors. */
5739 static void
5740 map_tty_color (struct frame *f, struct face *face,
5741 enum lface_attribute_index idx, int *defaulted)
5743 Lisp_Object frame, color, def;
5744 int foreground_p = idx == LFACE_FOREGROUND_INDEX;
5745 unsigned long default_pixel =
5746 foreground_p ? FACE_TTY_DEFAULT_FG_COLOR : FACE_TTY_DEFAULT_BG_COLOR;
5747 unsigned long pixel = default_pixel;
5748 #ifdef MSDOS
5749 unsigned long default_other_pixel =
5750 foreground_p ? FACE_TTY_DEFAULT_BG_COLOR : FACE_TTY_DEFAULT_FG_COLOR;
5751 #endif
5753 eassert (idx == LFACE_FOREGROUND_INDEX || idx == LFACE_BACKGROUND_INDEX);
5755 XSETFRAME (frame, f);
5756 color = face->lface[idx];
5758 if (STRINGP (color)
5759 && SCHARS (color)
5760 && CONSP (Vtty_defined_color_alist)
5761 && (def = assq_no_quit (color, call1 (Qtty_color_alist, frame)),
5762 CONSP (def)))
5764 /* Associations in tty-defined-color-alist are of the form
5765 (NAME INDEX R G B). We need the INDEX part. */
5766 pixel = XINT (XCAR (XCDR (def)));
5769 if (pixel == default_pixel && STRINGP (color))
5771 pixel = load_color (f, face, color, idx);
5773 #ifdef MSDOS
5774 /* If the foreground of the default face is the default color,
5775 use the foreground color defined by the frame. */
5776 if (FRAME_MSDOS_P (f))
5778 if (pixel == default_pixel
5779 || pixel == FACE_TTY_DEFAULT_COLOR)
5781 if (foreground_p)
5782 pixel = FRAME_FOREGROUND_PIXEL (f);
5783 else
5784 pixel = FRAME_BACKGROUND_PIXEL (f);
5785 face->lface[idx] = tty_color_name (f, pixel);
5786 *defaulted = 1;
5788 else if (pixel == default_other_pixel)
5790 if (foreground_p)
5791 pixel = FRAME_BACKGROUND_PIXEL (f);
5792 else
5793 pixel = FRAME_FOREGROUND_PIXEL (f);
5794 face->lface[idx] = tty_color_name (f, pixel);
5795 *defaulted = 1;
5798 #endif /* MSDOS */
5801 if (foreground_p)
5802 face->foreground = pixel;
5803 else
5804 face->background = pixel;
5808 /* Realize the fully-specified face with attributes ATTRS in face
5809 cache CACHE for ASCII characters. Do it for TTY frame CACHE->f.
5810 Value is a pointer to the newly created realized face. */
5812 static struct face *
5813 realize_tty_face (struct face_cache *cache,
5814 Lisp_Object attrs[LFACE_VECTOR_SIZE])
5816 struct face *face;
5817 int weight, slant;
5818 int face_colors_defaulted = 0;
5819 struct frame *f = cache->f;
5821 /* Frame must be a termcap frame. */
5822 eassert (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f));
5824 /* Allocate a new realized face. */
5825 face = make_realized_face (attrs);
5826 #if 0
5827 face->font_name = FRAME_MSDOS_P (cache->f) ? "ms-dos" : "tty";
5828 #endif
5830 /* Map face attributes to TTY appearances. */
5831 weight = FONT_WEIGHT_NAME_NUMERIC (attrs[LFACE_WEIGHT_INDEX]);
5832 slant = FONT_SLANT_NAME_NUMERIC (attrs[LFACE_SLANT_INDEX]);
5833 if (weight > 100)
5834 face->tty_bold_p = 1;
5835 if (slant != 100)
5836 face->tty_italic_p = 1;
5837 if (!NILP (attrs[LFACE_UNDERLINE_INDEX]))
5838 face->tty_underline_p = 1;
5839 if (!NILP (attrs[LFACE_INVERSE_INDEX]))
5840 face->tty_reverse_p = 1;
5842 /* Map color names to color indices. */
5843 map_tty_color (f, face, LFACE_FOREGROUND_INDEX, &face_colors_defaulted);
5844 map_tty_color (f, face, LFACE_BACKGROUND_INDEX, &face_colors_defaulted);
5846 /* Swap colors if face is inverse-video. If the colors are taken
5847 from the frame colors, they are already inverted, since the
5848 frame-creation function calls x-handle-reverse-video. */
5849 if (face->tty_reverse_p && !face_colors_defaulted)
5851 unsigned long tem = face->foreground;
5852 face->foreground = face->background;
5853 face->background = tem;
5856 if (tty_suppress_bold_inverse_default_colors_p
5857 && face->tty_bold_p
5858 && face->background == FACE_TTY_DEFAULT_FG_COLOR
5859 && face->foreground == FACE_TTY_DEFAULT_BG_COLOR)
5860 face->tty_bold_p = 0;
5862 return face;
5866 DEFUN ("tty-suppress-bold-inverse-default-colors",
5867 Ftty_suppress_bold_inverse_default_colors,
5868 Stty_suppress_bold_inverse_default_colors, 1, 1, 0,
5869 doc: /* Suppress/allow boldness of faces with inverse default colors.
5870 SUPPRESS non-nil means suppress it.
5871 This affects bold faces on TTYs whose foreground is the default background
5872 color of the display and whose background is the default foreground color.
5873 For such faces, the bold face attribute is ignored if this variable
5874 is non-nil. */)
5875 (Lisp_Object suppress)
5877 tty_suppress_bold_inverse_default_colors_p = !NILP (suppress);
5878 ++face_change_count;
5879 return suppress;
5884 /***********************************************************************
5885 Computing Faces
5886 ***********************************************************************/
5888 /* Return the ID of the face to use to display character CH with face
5889 property PROP on frame F in current_buffer. */
5892 compute_char_face (struct frame *f, int ch, Lisp_Object prop)
5894 int face_id;
5896 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
5897 ch = 0;
5899 if (NILP (prop))
5901 struct face *face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5902 face_id = FACE_FOR_CHAR (f, face, ch, -1, Qnil);
5904 else
5906 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5907 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5908 memcpy (attrs, default_face->lface, sizeof attrs);
5909 merge_face_ref (f, prop, attrs, 1, 0);
5910 face_id = lookup_face (f, attrs);
5913 return face_id;
5916 /* Return the face ID associated with buffer position POS for
5917 displaying ASCII characters. Return in *ENDPTR the position at
5918 which a different face is needed, as far as text properties and
5919 overlays are concerned. W is a window displaying current_buffer.
5921 REGION_BEG, REGION_END delimit the region, so it can be
5922 highlighted.
5924 LIMIT is a position not to scan beyond. That is to limit the time
5925 this function can take.
5927 If MOUSE is non-zero, use the character's mouse-face, not its face.
5929 BASE_FACE_ID, if non-negative, specifies a base face id to use
5930 instead of DEFAULT_FACE_ID.
5932 The face returned is suitable for displaying ASCII characters. */
5935 face_at_buffer_position (struct window *w, ptrdiff_t pos,
5936 ptrdiff_t region_beg, ptrdiff_t region_end,
5937 ptrdiff_t *endptr, ptrdiff_t limit,
5938 int mouse, int base_face_id)
5940 struct frame *f = XFRAME (w->frame);
5941 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5942 Lisp_Object prop, position;
5943 ptrdiff_t i, noverlays;
5944 Lisp_Object *overlay_vec;
5945 ptrdiff_t endpos;
5946 Lisp_Object propname = mouse ? Qmouse_face : Qface;
5947 Lisp_Object limit1, end;
5948 struct face *default_face;
5950 /* W must display the current buffer. We could write this function
5951 to use the frame and buffer of W, but right now it doesn't. */
5952 /* eassert (XBUFFER (w->contents) == current_buffer); */
5954 XSETFASTINT (position, pos);
5956 endpos = ZV;
5957 if (pos < region_beg && region_beg < endpos)
5958 endpos = region_beg;
5960 /* Get the `face' or `mouse_face' text property at POS, and
5961 determine the next position at which the property changes. */
5962 prop = Fget_text_property (position, propname, w->contents);
5963 XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
5964 end = Fnext_single_property_change (position, propname, w->contents, limit1);
5965 if (INTEGERP (end))
5966 endpos = XINT (end);
5968 /* Look at properties from overlays. */
5970 ptrdiff_t next_overlay;
5972 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, &next_overlay, 0);
5973 if (next_overlay < endpos)
5974 endpos = next_overlay;
5977 *endptr = endpos;
5980 int face_id;
5982 if (base_face_id >= 0)
5983 face_id = base_face_id;
5984 else if (NILP (Vface_remapping_alist))
5985 face_id = DEFAULT_FACE_ID;
5986 else
5987 face_id = lookup_basic_face (f, DEFAULT_FACE_ID);
5989 default_face = FACE_FROM_ID (f, face_id);
5992 /* Optimize common cases where we can use the default face. */
5993 if (noverlays == 0
5994 && NILP (prop)
5995 && !(pos >= region_beg && pos < region_end))
5996 return default_face->id;
5998 /* Begin with attributes from the default face. */
5999 memcpy (attrs, default_face->lface, sizeof attrs);
6001 /* Merge in attributes specified via text properties. */
6002 if (!NILP (prop))
6003 merge_face_ref (f, prop, attrs, 1, 0);
6005 /* Now merge the overlay data. */
6006 noverlays = sort_overlays (overlay_vec, noverlays, w);
6007 for (i = 0; i < noverlays; i++)
6009 Lisp_Object oend;
6010 ptrdiff_t oendpos;
6012 prop = Foverlay_get (overlay_vec[i], propname);
6013 if (!NILP (prop))
6014 merge_face_ref (f, prop, attrs, 1, 0);
6016 oend = OVERLAY_END (overlay_vec[i]);
6017 oendpos = OVERLAY_POSITION (oend);
6018 if (oendpos < endpos)
6019 endpos = oendpos;
6022 /* If in the region, merge in the region face. */
6023 if (pos >= region_beg && pos < region_end)
6025 merge_named_face (f, Qregion, attrs, 0);
6027 if (region_end < endpos)
6028 endpos = region_end;
6031 *endptr = endpos;
6033 /* Look up a realized face with the given face attributes,
6034 or realize a new one for ASCII characters. */
6035 return lookup_face (f, attrs);
6038 /* Return the face ID at buffer position POS for displaying ASCII
6039 characters associated with overlay strings for overlay OVERLAY.
6041 Like face_at_buffer_position except for OVERLAY. Currently it
6042 simply disregards the `face' properties of all overlays. */
6045 face_for_overlay_string (struct window *w, ptrdiff_t pos,
6046 ptrdiff_t region_beg, ptrdiff_t region_end,
6047 ptrdiff_t *endptr, ptrdiff_t limit,
6048 int mouse, Lisp_Object overlay)
6050 struct frame *f = XFRAME (w->frame);
6051 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6052 Lisp_Object prop, position;
6053 ptrdiff_t endpos;
6054 Lisp_Object propname = mouse ? Qmouse_face : Qface;
6055 Lisp_Object limit1, end;
6056 struct face *default_face;
6058 /* W must display the current buffer. We could write this function
6059 to use the frame and buffer of W, but right now it doesn't. */
6060 /* eassert (XBUFFER (w->contents) == current_buffer); */
6062 XSETFASTINT (position, pos);
6064 endpos = ZV;
6065 if (pos < region_beg && region_beg < endpos)
6066 endpos = region_beg;
6068 /* Get the `face' or `mouse_face' text property at POS, and
6069 determine the next position at which the property changes. */
6070 prop = Fget_text_property (position, propname, w->contents);
6071 XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
6072 end = Fnext_single_property_change (position, propname, w->contents, limit1);
6073 if (INTEGERP (end))
6074 endpos = XINT (end);
6076 *endptr = endpos;
6078 /* Optimize common case where we can use the default face. */
6079 if (NILP (prop)
6080 && !(pos >= region_beg && pos < region_end)
6081 && NILP (Vface_remapping_alist))
6082 return DEFAULT_FACE_ID;
6084 /* Begin with attributes from the default face. */
6085 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
6086 memcpy (attrs, default_face->lface, sizeof attrs);
6088 /* Merge in attributes specified via text properties. */
6089 if (!NILP (prop))
6090 merge_face_ref (f, prop, attrs, 1, 0);
6092 /* If in the region, merge in the region face. */
6093 if (pos >= region_beg && pos < region_end)
6095 merge_named_face (f, Qregion, attrs, 0);
6097 if (region_end < endpos)
6098 endpos = region_end;
6101 *endptr = endpos;
6103 /* Look up a realized face with the given face attributes,
6104 or realize a new one for ASCII characters. */
6105 return lookup_face (f, attrs);
6109 /* Compute the face at character position POS in Lisp string STRING on
6110 window W, for ASCII characters.
6112 If STRING is an overlay string, it comes from position BUFPOS in
6113 current_buffer, otherwise BUFPOS is zero to indicate that STRING is
6114 not an overlay string. W must display the current buffer.
6115 REGION_BEG and REGION_END give the start and end positions of the
6116 region; both are -1 if no region is visible.
6118 BASE_FACE_ID is the id of a face to merge with. For strings coming
6119 from overlays or the `display' property it is the face at BUFPOS.
6121 If MOUSE_P is non-zero, use the character's mouse-face, not its face.
6123 Set *ENDPTR to the next position where to check for faces in
6124 STRING; -1 if the face is constant from POS to the end of the
6125 string.
6127 Value is the id of the face to use. The face returned is suitable
6128 for displaying ASCII characters. */
6131 face_at_string_position (struct window *w, Lisp_Object string,
6132 ptrdiff_t pos, ptrdiff_t bufpos,
6133 ptrdiff_t region_beg, ptrdiff_t region_end,
6134 ptrdiff_t *endptr, enum face_id base_face_id,
6135 int mouse_p)
6137 Lisp_Object prop, position, end, limit;
6138 struct frame *f = XFRAME (WINDOW_FRAME (w));
6139 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6140 struct face *base_face;
6141 bool multibyte_p = STRING_MULTIBYTE (string);
6142 Lisp_Object prop_name = mouse_p ? Qmouse_face : Qface;
6144 /* Get the value of the face property at the current position within
6145 STRING. Value is nil if there is no face property. */
6146 XSETFASTINT (position, pos);
6147 prop = Fget_text_property (position, prop_name, string);
6149 /* Get the next position at which to check for faces. Value of end
6150 is nil if face is constant all the way to the end of the string.
6151 Otherwise it is a string position where to check faces next.
6152 Limit is the maximum position up to which to check for property
6153 changes in Fnext_single_property_change. Strings are usually
6154 short, so set the limit to the end of the string. */
6155 XSETFASTINT (limit, SCHARS (string));
6156 end = Fnext_single_property_change (position, prop_name, string, limit);
6157 if (INTEGERP (end))
6158 *endptr = XFASTINT (end);
6159 else
6160 *endptr = -1;
6162 base_face = FACE_FROM_ID (f, base_face_id);
6163 eassert (base_face);
6165 /* Optimize the default case that there is no face property and we
6166 are not in the region. */
6167 if (NILP (prop)
6168 && (base_face_id != DEFAULT_FACE_ID
6169 /* BUFPOS <= 0 means STRING is not an overlay string, so
6170 that the region doesn't have to be taken into account. */
6171 || bufpos <= 0
6172 || bufpos < region_beg
6173 || bufpos >= region_end)
6174 && (multibyte_p
6175 /* We can't realize faces for different charsets differently
6176 if we don't have fonts, so we can stop here if not working
6177 on a window-system frame. */
6178 || !FRAME_WINDOW_P (f)
6179 || FACE_SUITABLE_FOR_ASCII_CHAR_P (base_face, 0)))
6180 return base_face->id;
6182 /* Begin with attributes from the base face. */
6183 memcpy (attrs, base_face->lface, sizeof attrs);
6185 /* Merge in attributes specified via text properties. */
6186 if (!NILP (prop))
6187 merge_face_ref (f, prop, attrs, 1, 0);
6189 /* If in the region, merge in the region face. */
6190 if (bufpos
6191 && bufpos >= region_beg
6192 && bufpos < region_end)
6193 merge_named_face (f, Qregion, attrs, 0);
6195 /* Look up a realized face with the given face attributes,
6196 or realize a new one for ASCII characters. */
6197 return lookup_face (f, attrs);
6201 /* Merge a face into a realized face.
6203 F is frame where faces are (to be) realized.
6205 FACE_NAME is named face to merge.
6207 If FACE_NAME is nil, FACE_ID is face_id of realized face to merge.
6209 If FACE_NAME is t, FACE_ID is lface_id of face to merge.
6211 BASE_FACE_ID is realized face to merge into.
6213 Return new face id.
6217 merge_faces (struct frame *f, Lisp_Object face_name, int face_id,
6218 int base_face_id)
6220 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6221 struct face *base_face;
6223 base_face = FACE_FROM_ID (f, base_face_id);
6224 if (!base_face)
6225 return base_face_id;
6227 if (EQ (face_name, Qt))
6229 if (face_id < 0 || face_id >= lface_id_to_name_size)
6230 return base_face_id;
6231 face_name = lface_id_to_name[face_id];
6232 /* When called during make-frame, lookup_derived_face may fail
6233 if the faces are uninitialized. Don't signal an error. */
6234 face_id = lookup_derived_face (f, face_name, base_face_id, 0);
6235 return (face_id >= 0 ? face_id : base_face_id);
6238 /* Begin with attributes from the base face. */
6239 memcpy (attrs, base_face->lface, sizeof attrs);
6241 if (!NILP (face_name))
6243 if (!merge_named_face (f, face_name, attrs, 0))
6244 return base_face_id;
6246 else
6248 struct face *face;
6249 if (face_id < 0)
6250 return base_face_id;
6251 face = FACE_FROM_ID (f, face_id);
6252 if (!face)
6253 return base_face_id;
6254 merge_face_vectors (f, face->lface, attrs, 0);
6257 /* Look up a realized face with the given face attributes,
6258 or realize a new one for ASCII characters. */
6259 return lookup_face (f, attrs);
6264 #ifndef HAVE_X_WINDOWS
6265 DEFUN ("x-load-color-file", Fx_load_color_file,
6266 Sx_load_color_file, 1, 1, 0,
6267 doc: /* Create an alist of color entries from an external file.
6269 The file should define one named RGB color per line like so:
6270 R G B name
6271 where R,G,B are numbers between 0 and 255 and name is an arbitrary string. */)
6272 (Lisp_Object filename)
6274 FILE *fp;
6275 Lisp_Object cmap = Qnil;
6276 Lisp_Object abspath;
6278 CHECK_STRING (filename);
6279 abspath = Fexpand_file_name (filename, Qnil);
6281 block_input ();
6282 fp = emacs_fopen (SSDATA (abspath), "rt");
6283 if (fp)
6285 char buf[512];
6286 int red, green, blue;
6287 int num;
6289 while (fgets (buf, sizeof (buf), fp) != NULL) {
6290 if (sscanf (buf, "%u %u %u %n", &red, &green, &blue, &num) == 3)
6292 #ifdef HAVE_NTGUI
6293 int color = RGB (red, green, blue);
6294 #else
6295 int color = (red << 16) | (green << 8) | blue;
6296 #endif
6297 char *name = buf + num;
6298 ptrdiff_t len = strlen (name);
6299 len -= 0 < len && name[len - 1] == '\n';
6300 cmap = Fcons (Fcons (make_string (name, len), make_number (color)),
6301 cmap);
6304 fclose (fp);
6306 unblock_input ();
6307 return cmap;
6309 #endif
6312 /***********************************************************************
6313 Tests
6314 ***********************************************************************/
6316 #ifdef GLYPH_DEBUG
6318 /* Print the contents of the realized face FACE to stderr. */
6320 static void
6321 dump_realized_face (struct face *face)
6323 fprintf (stderr, "ID: %d\n", face->id);
6324 #ifdef HAVE_X_WINDOWS
6325 fprintf (stderr, "gc: %ld\n", (long) face->gc);
6326 #endif
6327 fprintf (stderr, "foreground: 0x%lx (%s)\n",
6328 face->foreground,
6329 SDATA (face->lface[LFACE_FOREGROUND_INDEX]));
6330 fprintf (stderr, "background: 0x%lx (%s)\n",
6331 face->background,
6332 SDATA (face->lface[LFACE_BACKGROUND_INDEX]));
6333 if (face->font)
6334 fprintf (stderr, "font_name: %s (%s)\n",
6335 SDATA (face->font->props[FONT_NAME_INDEX]),
6336 SDATA (face->lface[LFACE_FAMILY_INDEX]));
6337 #ifdef HAVE_X_WINDOWS
6338 fprintf (stderr, "font = %p\n", face->font);
6339 #endif
6340 fprintf (stderr, "fontset: %d\n", face->fontset);
6341 fprintf (stderr, "underline: %d (%s)\n",
6342 face->underline_p,
6343 SDATA (Fsymbol_name (face->lface[LFACE_UNDERLINE_INDEX])));
6344 fprintf (stderr, "hash: %d\n", face->hash);
6348 DEFUN ("dump-face", Fdump_face, Sdump_face, 0, 1, 0, doc: /* */)
6349 (Lisp_Object n)
6351 if (NILP (n))
6353 int i;
6355 fprintf (stderr, "font selection order: ");
6356 for (i = 0; i < DIM (font_sort_order); ++i)
6357 fprintf (stderr, "%d ", font_sort_order[i]);
6358 fprintf (stderr, "\n");
6360 fprintf (stderr, "alternative fonts: ");
6361 debug_print (Vface_alternative_font_family_alist);
6362 fprintf (stderr, "\n");
6364 for (i = 0; i < FRAME_FACE_CACHE (SELECTED_FRAME ())->used; ++i)
6365 Fdump_face (make_number (i));
6367 else
6369 struct face *face;
6370 CHECK_NUMBER (n);
6371 face = FACE_FROM_ID (SELECTED_FRAME (), XINT (n));
6372 if (face == NULL)
6373 error ("Not a valid face");
6374 dump_realized_face (face);
6377 return Qnil;
6381 DEFUN ("show-face-resources", Fshow_face_resources, Sshow_face_resources,
6382 0, 0, 0, doc: /* */)
6383 (void)
6385 fprintf (stderr, "number of colors = %d\n", ncolors_allocated);
6386 fprintf (stderr, "number of pixmaps = %d\n", npixmaps_allocated);
6387 fprintf (stderr, "number of GCs = %d\n", ngcs);
6388 return Qnil;
6391 #endif /* GLYPH_DEBUG */
6395 /***********************************************************************
6396 Initialization
6397 ***********************************************************************/
6399 void
6400 syms_of_xfaces (void)
6402 DEFSYM (Qface, "face");
6403 DEFSYM (Qface_no_inherit, "face-no-inherit");
6404 DEFSYM (Qbitmap_spec_p, "bitmap-spec-p");
6405 DEFSYM (Qframe_set_background_mode, "frame-set-background-mode");
6407 /* Lisp face attribute keywords. */
6408 DEFSYM (QCfamily, ":family");
6409 DEFSYM (QCheight, ":height");
6410 DEFSYM (QCweight, ":weight");
6411 DEFSYM (QCslant, ":slant");
6412 DEFSYM (QCunderline, ":underline");
6413 DEFSYM (QCinverse_video, ":inverse-video");
6414 DEFSYM (QCreverse_video, ":reverse-video");
6415 DEFSYM (QCforeground, ":foreground");
6416 DEFSYM (QCbackground, ":background");
6417 DEFSYM (QCstipple, ":stipple");
6418 DEFSYM (QCwidth, ":width");
6419 DEFSYM (QCfont, ":font");
6420 DEFSYM (QCfontset, ":fontset");
6421 DEFSYM (QCbold, ":bold");
6422 DEFSYM (QCitalic, ":italic");
6423 DEFSYM (QCoverline, ":overline");
6424 DEFSYM (QCstrike_through, ":strike-through");
6425 DEFSYM (QCbox, ":box");
6426 DEFSYM (QCinherit, ":inherit");
6428 /* Symbols used for Lisp face attribute values. */
6429 DEFSYM (QCcolor, ":color");
6430 DEFSYM (QCline_width, ":line-width");
6431 DEFSYM (QCstyle, ":style");
6432 DEFSYM (Qline, "line");
6433 DEFSYM (Qwave, "wave");
6434 DEFSYM (Qreleased_button, "released-button");
6435 DEFSYM (Qpressed_button, "pressed-button");
6436 DEFSYM (Qnormal, "normal");
6437 DEFSYM (Qextra_light, "extra-light");
6438 DEFSYM (Qlight, "light");
6439 DEFSYM (Qsemi_light, "semi-light");
6440 DEFSYM (Qsemi_bold, "semi-bold");
6441 DEFSYM (Qbold, "bold");
6442 DEFSYM (Qextra_bold, "extra-bold");
6443 DEFSYM (Qultra_bold, "ultra-bold");
6444 DEFSYM (Qoblique, "oblique");
6445 DEFSYM (Qitalic, "italic");
6446 DEFSYM (Qbackground_color, "background-color");
6447 DEFSYM (Qforeground_color, "foreground-color");
6448 DEFSYM (Qunspecified, "unspecified");
6449 DEFSYM (QCignore_defface, ":ignore-defface");
6451 DEFSYM (Qface_alias, "face-alias");
6452 DEFSYM (Qdefault, "default");
6453 DEFSYM (Qtool_bar, "tool-bar");
6454 DEFSYM (Qregion, "region");
6455 DEFSYM (Qfringe, "fringe");
6456 DEFSYM (Qheader_line, "header-line");
6457 DEFSYM (Qscroll_bar, "scroll-bar");
6458 DEFSYM (Qmenu, "menu");
6459 DEFSYM (Qcursor, "cursor");
6460 DEFSYM (Qborder, "border");
6461 DEFSYM (Qmouse, "mouse");
6462 DEFSYM (Qmode_line_inactive, "mode-line-inactive");
6463 DEFSYM (Qvertical_border, "vertical-border");
6464 DEFSYM (Qtty_color_desc, "tty-color-desc");
6465 DEFSYM (Qtty_color_standard_values, "tty-color-standard-values");
6466 DEFSYM (Qtty_color_by_index, "tty-color-by-index");
6467 DEFSYM (Qtty_color_alist, "tty-color-alist");
6468 DEFSYM (Qscalable_fonts_allowed, "scalable-fonts-allowed");
6470 Vparam_value_alist = list1 (Fcons (Qnil, Qnil));
6471 staticpro (&Vparam_value_alist);
6472 Vface_alternative_font_family_alist = Qnil;
6473 staticpro (&Vface_alternative_font_family_alist);
6474 Vface_alternative_font_registry_alist = Qnil;
6475 staticpro (&Vface_alternative_font_registry_alist);
6477 defsubr (&Sinternal_make_lisp_face);
6478 defsubr (&Sinternal_lisp_face_p);
6479 defsubr (&Sinternal_set_lisp_face_attribute);
6480 #ifdef HAVE_WINDOW_SYSTEM
6481 defsubr (&Sinternal_set_lisp_face_attribute_from_resource);
6482 #endif
6483 defsubr (&Scolor_gray_p);
6484 defsubr (&Scolor_supported_p);
6485 #ifndef HAVE_X_WINDOWS
6486 defsubr (&Sx_load_color_file);
6487 #endif
6488 defsubr (&Sface_attribute_relative_p);
6489 defsubr (&Smerge_face_attribute);
6490 defsubr (&Sinternal_get_lisp_face_attribute);
6491 defsubr (&Sinternal_lisp_face_attribute_values);
6492 defsubr (&Sinternal_lisp_face_equal_p);
6493 defsubr (&Sinternal_lisp_face_empty_p);
6494 defsubr (&Sinternal_copy_lisp_face);
6495 defsubr (&Sinternal_merge_in_global_face);
6496 defsubr (&Sface_font);
6497 defsubr (&Sframe_face_alist);
6498 defsubr (&Sdisplay_supports_face_attributes_p);
6499 defsubr (&Scolor_distance);
6500 defsubr (&Sinternal_set_font_selection_order);
6501 defsubr (&Sinternal_set_alternative_font_family_alist);
6502 defsubr (&Sinternal_set_alternative_font_registry_alist);
6503 defsubr (&Sface_attributes_as_vector);
6504 #ifdef GLYPH_DEBUG
6505 defsubr (&Sdump_face);
6506 defsubr (&Sshow_face_resources);
6507 #endif /* GLYPH_DEBUG */
6508 defsubr (&Sclear_face_cache);
6509 defsubr (&Stty_suppress_bold_inverse_default_colors);
6511 #if defined DEBUG_X_COLORS && defined HAVE_X_WINDOWS
6512 defsubr (&Sdump_colors);
6513 #endif
6515 DEFVAR_LISP ("face-new-frame-defaults", Vface_new_frame_defaults,
6516 doc: /* List of global face definitions (for internal use only.) */);
6517 Vface_new_frame_defaults = Qnil;
6519 DEFVAR_LISP ("face-default-stipple", Vface_default_stipple,
6520 doc: /* Default stipple pattern used on monochrome displays.
6521 This stipple pattern is used on monochrome displays
6522 instead of shades of gray for a face background color.
6523 See `set-face-stipple' for possible values for this variable. */);
6524 Vface_default_stipple = build_pure_c_string ("gray3");
6526 DEFVAR_LISP ("tty-defined-color-alist", Vtty_defined_color_alist,
6527 doc: /* An alist of defined terminal colors and their RGB values.
6528 See the docstring of `tty-color-alist' for the details. */);
6529 Vtty_defined_color_alist = Qnil;
6531 DEFVAR_LISP ("scalable-fonts-allowed", Vscalable_fonts_allowed,
6532 doc: /* Allowed scalable fonts.
6533 A value of nil means don't allow any scalable fonts.
6534 A value of t means allow any scalable font.
6535 Otherwise, value must be a list of regular expressions. A font may be
6536 scaled if its name matches a regular expression in the list.
6537 Note that if value is nil, a scalable font might still be used, if no
6538 other font of the appropriate family and registry is available. */);
6539 Vscalable_fonts_allowed = Qnil;
6541 DEFVAR_LISP ("face-ignored-fonts", Vface_ignored_fonts,
6542 doc: /* List of ignored fonts.
6543 Each element is a regular expression that matches names of fonts to
6544 ignore. */);
6545 Vface_ignored_fonts = Qnil;
6547 DEFVAR_LISP ("face-remapping-alist", Vface_remapping_alist,
6548 doc: /* Alist of face remappings.
6549 Each element is of the form:
6551 (FACE . REPLACEMENT),
6553 which causes display of the face FACE to use REPLACEMENT instead.
6554 REPLACEMENT is a face specification, i.e. one of the following:
6556 (1) a face name
6557 (2) a property list of attribute/value pairs, or
6558 (3) a list in which each element has the form of (1) or (2).
6560 List values for REPLACEMENT are merged to form the final face
6561 specification, with earlier entries taking precedence, in the same as
6562 as in the `face' text property.
6564 Face-name remapping cycles are suppressed; recursive references use
6565 the underlying face instead of the remapped face. So a remapping of
6566 the form:
6568 (FACE EXTRA-FACE... FACE)
6572 (FACE (FACE-ATTR VAL ...) FACE)
6574 causes EXTRA-FACE... or (FACE-ATTR VAL ...) to be _merged_ with the
6575 existing definition of FACE. Note that this isn't necessary for the
6576 default face, since every face inherits from the default face.
6578 If this variable is made buffer-local, the face remapping takes effect
6579 only in that buffer. For instance, the mode my-mode could define a
6580 face `my-mode-default', and then in the mode setup function, do:
6582 (set (make-local-variable 'face-remapping-alist)
6583 '((default my-mode-default)))).
6585 Because Emacs normally only redraws screen areas when the underlying
6586 buffer contents change, you may need to call `redraw-display' after
6587 changing this variable for it to take effect. */);
6588 Vface_remapping_alist = Qnil;
6590 DEFVAR_LISP ("face-font-rescale-alist", Vface_font_rescale_alist,
6591 doc: /* Alist of fonts vs the rescaling factors.
6592 Each element is a cons (FONT-PATTERN . RESCALE-RATIO), where
6593 FONT-PATTERN is a font-spec or a regular expression matching a font name, and
6594 RESCALE-RATIO is a floating point number to specify how much larger
6595 \(or smaller) font we should use. For instance, if a face requests
6596 a font of 10 point, we actually use a font of 10 * RESCALE-RATIO point. */);
6597 Vface_font_rescale_alist = Qnil;
6599 #ifdef HAVE_WINDOW_SYSTEM
6600 defsubr (&Sbitmap_spec_p);
6601 defsubr (&Sx_list_fonts);
6602 defsubr (&Sinternal_face_x_get_resource);
6603 defsubr (&Sx_family_fonts);
6604 #endif