* Makefile.in (${srcdir}/info/dir): Make sure info directory exists (backport)
[emacs.git] / src / xfaces.c
blob1dc068cf18a823d7374510e26196ec3362d68a9f
1 /* xfaces.c -- "Face" primitives.
3 Copyright (C) 1993-1994, 1998-2014 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 /* New face implementation by Gerd Moellmann <gerd@gnu.org>. */
22 /* Faces.
24 When using Emacs with X, the display style of characters can be
25 changed by defining `faces'. Each face can specify the following
26 display attributes:
28 1. Font family name.
30 2. Font foundry name.
32 3. Relative proportionate width, aka character set width or set
33 width (swidth), e.g. `semi-compressed'.
35 4. Font height in 1/10pt.
37 5. Font weight, e.g. `bold'.
39 6. Font slant, e.g. `italic'.
41 7. Foreground color.
43 8. Background color.
45 9. Whether or not characters should be underlined, and in what color.
47 10. Whether or not characters should be displayed in inverse video.
49 11. A background stipple, a bitmap.
51 12. Whether or not characters should be overlined, and in what color.
53 13. Whether or not characters should be strike-through, and in what
54 color.
56 14. Whether or not a box should be drawn around characters, the box
57 type, and, for simple boxes, in what color.
59 15. Font-spec, or nil. This is a special attribute.
61 A font-spec is a collection of font attributes (specs).
63 When this attribute is specified, the face uses a font matching
64 with the specs as is except for what overwritten by the specs in
65 the fontset (see below). In addition, the other font-related
66 attributes (1st thru 5th) are updated from the spec.
68 On the other hand, if one of the other font-related attributes are
69 specified, the corresponding specs in this attribute is set to nil.
71 15. A face name or list of face names from which to inherit attributes.
73 16. A specified average font width, which is invisible from Lisp,
74 and is used to ensure that a font specified on the command line,
75 for example, can be matched exactly.
77 17. A fontset name. This is another special attribute.
79 A fontset is a mappings from characters to font-specs, and the
80 specs overwrite the font-spec in the 14th attribute.
83 Faces are frame-local by nature because Emacs allows to define the
84 same named face (face names are symbols) differently for different
85 frames. Each frame has an alist of face definitions for all named
86 faces. The value of a named face in such an alist is a Lisp vector
87 with the symbol `face' in slot 0, and a slot for each of the face
88 attributes mentioned above.
90 There is also a global face alist `Vface_new_frame_defaults'. Face
91 definitions from this list are used to initialize faces of newly
92 created frames.
94 A face doesn't have to specify all attributes. Those not specified
95 have a value of `unspecified'. Faces specifying all attributes but
96 the 14th are called `fully-specified'.
99 Face merging.
101 The display style of a given character in the text is determined by
102 combining several faces. This process is called `face merging'.
103 Any aspect of the display style that isn't specified by overlays or
104 text properties is taken from the `default' face. Since it is made
105 sure that the default face is always fully-specified, face merging
106 always results in a fully-specified face.
109 Face realization.
111 After all face attributes for a character have been determined by
112 merging faces of that character, that face is `realized'. The
113 realization process maps face attributes to what is physically
114 available on the system where Emacs runs. The result is a
115 `realized face' in the form of a struct face which is stored in the
116 face cache of the frame on which it was realized.
118 Face realization is done in the context of the character to display
119 because different fonts may be used for different characters. In
120 other words, for characters that have different font
121 specifications, different realized faces are needed to display
122 them.
124 Font specification is done by fontsets. See the comment in
125 fontset.c for the details. In the current implementation, all ASCII
126 characters share the same font in a fontset.
128 Faces are at first realized for ASCII characters, and, at that
129 time, assigned a specific realized fontset. Hereafter, we call
130 such a face as `ASCII face'. When a face for a multibyte character
131 is realized, it inherits (thus shares) a fontset of an ASCII face
132 that has the same attributes other than font-related ones.
134 Thus, all realized faces have a realized fontset.
137 Unibyte text.
139 Unibyte text (i.e. raw 8-bit characters) is displayed with the same
140 font as ASCII characters. That is because it is expected that
141 unibyte text users specify a font that is suitable both for ASCII
142 and raw 8-bit characters.
145 Font selection.
147 Font selection tries to find the best available matching font for a
148 given (character, face) combination.
150 If the face specifies a fontset name, that fontset determines a
151 pattern for fonts of the given character. If the face specifies a
152 font name or the other font-related attributes, a fontset is
153 realized from the default fontset. In that case, that
154 specification determines a pattern for ASCII characters and the
155 default fontset determines a pattern for multibyte characters.
157 Available fonts on the system on which Emacs runs are then matched
158 against the font pattern. The result of font selection is the best
159 match for the given face attributes in this font list.
161 Font selection can be influenced by the user.
163 1. The user can specify the relative importance he gives the face
164 attributes width, height, weight, and slant by setting
165 face-font-selection-order (faces.el) to a list of face attribute
166 names. The default is '(:width :height :weight :slant), and means
167 that font selection first tries to find a good match for the font
168 width specified by a face, then---within fonts with that
169 width---tries to find a best match for the specified font height,
170 etc.
172 2. Setting face-font-family-alternatives allows the user to
173 specify alternative font families to try if a family specified by a
174 face doesn't exist.
176 3. Setting face-font-registry-alternatives allows the user to
177 specify all alternative font registries to try for a face
178 specifying a registry.
180 4. Setting face-ignored-fonts allows the user to ignore specific
181 fonts.
184 Character composition.
186 Usually, the realization process is already finished when Emacs
187 actually reflects the desired glyph matrix on the screen. However,
188 on displaying a composition (sequence of characters to be composed
189 on the screen), a suitable font for the components of the
190 composition is selected and realized while drawing them on the
191 screen, i.e. the realization process is delayed but in principle
192 the same.
195 Initialization of basic faces.
197 The faces `default', `modeline' are considered `basic faces'.
198 When redisplay happens the first time for a newly created frame,
199 basic faces are realized for CHARSET_ASCII. Frame parameters are
200 used to fill in unspecified attributes of the default face. */
202 #include <config.h>
203 #include "sysstdio.h"
204 #include <sys/types.h>
205 #include <sys/stat.h>
207 #include "lisp.h"
208 #include "character.h"
209 #include "charset.h"
210 #include "keyboard.h"
211 #include "frame.h"
212 #include "termhooks.h"
214 #ifdef USE_MOTIF
215 #include <Xm/Xm.h>
216 #include <Xm/XmStrDefs.h>
217 #endif /* USE_MOTIF */
219 #ifdef MSDOS
220 #include "dosfns.h"
221 #endif
223 #ifdef HAVE_WINDOW_SYSTEM
224 #include TERM_HEADER
225 #include "fontset.h"
226 #ifdef HAVE_NTGUI
227 #define x_display_info w32_display_info
228 #define GCGraphicsExposures 0
229 #endif /* HAVE_NTGUI */
231 #ifdef HAVE_NS
232 #define GCGraphicsExposures 0
233 #endif /* HAVE_NS */
234 #endif /* HAVE_WINDOW_SYSTEM */
236 #include "buffer.h"
237 #include "dispextern.h"
238 #include "blockinput.h"
239 #include "window.h"
240 #include "intervals.h"
241 #include "termchar.h"
243 #include "font.h"
245 #ifdef HAVE_X_WINDOWS
247 /* Compensate for a bug in Xos.h on some systems, on which it requires
248 time.h. On some such systems, Xos.h tries to redefine struct
249 timeval and struct timezone if USG is #defined while it is
250 #included. */
252 #ifdef XOS_NEEDS_TIME_H
253 #include <time.h>
254 #undef USG
255 #include <X11/Xos.h>
256 #define USG
257 #define __TIMEVAL__
258 #if defined USG || defined __TIMEVAL__ /* Don't warn about unused macros. */
259 #endif
260 #else /* not XOS_NEEDS_TIME_H */
261 #include <X11/Xos.h>
262 #endif /* not XOS_NEEDS_TIME_H */
264 #endif /* HAVE_X_WINDOWS */
266 #include <c-ctype.h>
268 /* Non-zero if face attribute ATTR is unspecified. */
270 #define UNSPECIFIEDP(ATTR) EQ ((ATTR), Qunspecified)
272 /* Non-zero if face attribute ATTR is `ignore-defface'. */
274 #define IGNORE_DEFFACE_P(ATTR) EQ ((ATTR), QCignore_defface)
276 /* Value is the number of elements of VECTOR. */
278 #define DIM(VECTOR) (sizeof (VECTOR) / sizeof *(VECTOR))
280 /* Size of hash table of realized faces in face caches (should be a
281 prime number). */
283 #define FACE_CACHE_BUCKETS_SIZE 1001
285 /* Keyword symbols used for face attribute names. */
287 Lisp_Object QCfamily, QCheight, QCweight, QCslant;
288 static Lisp_Object QCunderline;
289 static Lisp_Object QCinverse_video, QCstipple;
290 Lisp_Object QCforeground, QCbackground;
291 Lisp_Object QCwidth;
292 static Lisp_Object QCfont, QCbold, QCitalic;
293 static Lisp_Object QCreverse_video;
294 static Lisp_Object QCoverline, QCstrike_through, QCbox, QCinherit;
295 static Lisp_Object QCfontset, QCdistant_foreground;
297 /* Symbols used for attribute values. */
299 Lisp_Object Qnormal;
300 Lisp_Object Qbold;
301 static Lisp_Object Qline, Qwave;
302 Lisp_Object Qextra_light, Qlight;
303 Lisp_Object Qsemi_light, Qsemi_bold, Qextra_bold, Qultra_bold;
304 Lisp_Object Qoblique;
305 Lisp_Object Qitalic;
306 static Lisp_Object Qreleased_button, Qpressed_button;
307 static Lisp_Object QCstyle, QCcolor, QCline_width;
308 Lisp_Object Qunspecified; /* used in dosfns.c */
309 static Lisp_Object QCignore_defface;
311 char unspecified_fg[] = "unspecified-fg", unspecified_bg[] = "unspecified-bg";
313 /* The name of the function to call when the background of the frame
314 has changed, frame_set_background_mode. */
316 static Lisp_Object Qframe_set_background_mode;
318 /* Names of basic faces. */
320 Lisp_Object Qdefault, Qtool_bar, Qfringe;
321 static Lisp_Object Qregion;
322 Lisp_Object Qheader_line, Qscroll_bar, Qcursor;
323 static Lisp_Object Qborder, Qmouse, Qmenu;
324 Lisp_Object Qmode_line_inactive;
325 static Lisp_Object Qvertical_border;
326 static Lisp_Object Qwindow_divider;
327 static Lisp_Object Qwindow_divider_first_pixel;
328 static Lisp_Object Qwindow_divider_last_pixel;
330 /* The symbol `face-alias'. A symbols having that property is an
331 alias for another face. Value of the property is the name of
332 the aliased face. */
334 static Lisp_Object Qface_alias;
336 /* Alist of alternative font families. Each element is of the form
337 (FAMILY FAMILY1 FAMILY2 ...). If fonts of FAMILY can't be loaded,
338 try FAMILY1, then FAMILY2, ... */
340 Lisp_Object Vface_alternative_font_family_alist;
342 /* Alist of alternative font registries. Each element is of the form
343 (REGISTRY REGISTRY1 REGISTRY2...). If fonts of REGISTRY can't be
344 loaded, try REGISTRY1, then REGISTRY2, ... */
346 Lisp_Object Vface_alternative_font_registry_alist;
348 /* Allowed scalable fonts. A value of nil means don't allow any
349 scalable fonts. A value of t means allow the use of any scalable
350 font. Otherwise, value must be a list of regular expressions. A
351 font may be scaled if its name matches a regular expression in the
352 list. */
354 static Lisp_Object Qscalable_fonts_allowed;
356 /* The symbols `foreground-color' and `background-color' which can be
357 used as part of a `face' property. This is for compatibility with
358 Emacs 20.2. */
360 Lisp_Object Qforeground_color, Qbackground_color;
362 /* The symbols `face' and `mouse-face' used as text properties. */
364 Lisp_Object Qface;
366 /* Property for basic faces which other faces cannot inherit. */
368 static Lisp_Object Qface_no_inherit;
370 /* Error symbol for wrong_type_argument in load_pixmap. */
372 static Lisp_Object Qbitmap_spec_p;
374 /* The next ID to assign to Lisp faces. */
376 static int next_lface_id;
378 /* A vector mapping Lisp face Id's to face names. */
380 static Lisp_Object *lface_id_to_name;
381 static ptrdiff_t lface_id_to_name_size;
383 /* TTY color-related functions (defined in tty-colors.el). */
385 static Lisp_Object Qtty_color_desc, Qtty_color_by_index, Qtty_color_standard_values;
387 /* The name of the function used to compute colors on TTYs. */
389 static Lisp_Object Qtty_color_alist;
391 #ifdef HAVE_WINDOW_SYSTEM
393 /* Counter for calls to clear_face_cache. If this counter reaches
394 CLEAR_FONT_TABLE_COUNT, and a frame has more than
395 CLEAR_FONT_TABLE_NFONTS load, unused fonts are freed. */
397 static int clear_font_table_count;
398 #define CLEAR_FONT_TABLE_COUNT 100
399 #define CLEAR_FONT_TABLE_NFONTS 10
401 #endif /* HAVE_WINDOW_SYSTEM */
403 /* Non-zero means face attributes have been changed since the last
404 redisplay. Used in redisplay_internal. */
406 int face_change_count;
408 /* Non-zero means don't display bold text if a face's foreground
409 and background colors are the inverse of the default colors of the
410 display. This is a kluge to suppress `bold black' foreground text
411 which is hard to read on an LCD monitor. */
413 static int tty_suppress_bold_inverse_default_colors_p;
415 /* A list of the form `((x . y))' used to avoid consing in
416 Finternal_set_lisp_face_attribute. */
418 static Lisp_Object Vparam_value_alist;
420 /* The total number of colors currently allocated. */
422 #ifdef GLYPH_DEBUG
423 static int ncolors_allocated;
424 static int npixmaps_allocated;
425 static int ngcs;
426 #endif
428 /* Non-zero means the definition of the `menu' face for new frames has
429 been changed. */
431 static int menu_face_changed_default;
433 struct named_merge_point;
435 static struct face *realize_face (struct face_cache *, Lisp_Object *,
436 int);
437 static struct face *realize_x_face (struct face_cache *, Lisp_Object *);
438 static struct face *realize_tty_face (struct face_cache *, Lisp_Object *);
439 static bool realize_basic_faces (struct frame *);
440 static bool realize_default_face (struct frame *);
441 static void realize_named_face (struct frame *, Lisp_Object, int);
442 static struct face_cache *make_face_cache (struct frame *);
443 static void free_face_cache (struct face_cache *);
444 static int merge_face_ref (struct frame *, Lisp_Object, Lisp_Object *,
445 int, struct named_merge_point *);
446 static int color_distance (XColor *x, XColor *y);
448 #ifdef HAVE_WINDOW_SYSTEM
449 static void set_font_frame_param (Lisp_Object, Lisp_Object);
450 static void clear_face_gcs (struct face_cache *);
451 static struct face *realize_non_ascii_face (struct frame *, Lisp_Object,
452 struct face *);
453 #endif /* HAVE_WINDOW_SYSTEM */
455 /***********************************************************************
456 Utilities
457 ***********************************************************************/
459 #ifdef HAVE_X_WINDOWS
461 #ifdef DEBUG_X_COLORS
463 /* The following is a poor mans infrastructure for debugging X color
464 allocation problems on displays with PseudoColor-8. Some X servers
465 like 3.3.5 XF86_SVGA with Matrox cards apparently don't implement
466 color reference counts completely so that they don't signal an
467 error when a color is freed whose reference count is already 0.
468 Other X servers do. To help me debug this, the following code
469 implements a simple reference counting schema of its own, for a
470 single display/screen. --gerd. */
472 /* Reference counts for pixel colors. */
474 int color_count[256];
476 /* Register color PIXEL as allocated. */
478 void
479 register_color (unsigned long pixel)
481 eassert (pixel < 256);
482 ++color_count[pixel];
486 /* Register color PIXEL as deallocated. */
488 void
489 unregister_color (unsigned long pixel)
491 eassert (pixel < 256);
492 if (color_count[pixel] > 0)
493 --color_count[pixel];
494 else
495 emacs_abort ();
499 /* Register N colors from PIXELS as deallocated. */
501 void
502 unregister_colors (unsigned long *pixels, int n)
504 int i;
505 for (i = 0; i < n; ++i)
506 unregister_color (pixels[i]);
510 DEFUN ("dump-colors", Fdump_colors, Sdump_colors, 0, 0, 0,
511 doc: /* Dump currently allocated colors to stderr. */)
512 (void)
514 int i, n;
516 fputc ('\n', stderr);
518 for (i = n = 0; i < sizeof color_count / sizeof color_count[0]; ++i)
519 if (color_count[i])
521 fprintf (stderr, "%3d: %5d", i, color_count[i]);
522 ++n;
523 if (n % 5 == 0)
524 fputc ('\n', stderr);
525 else
526 fputc ('\t', stderr);
529 if (n % 5 != 0)
530 fputc ('\n', stderr);
531 return Qnil;
534 #endif /* DEBUG_X_COLORS */
537 /* Free colors used on frame F. PIXELS is an array of NPIXELS pixel
538 color values. Interrupt input must be blocked when this function
539 is called. */
541 void
542 x_free_colors (struct frame *f, unsigned long *pixels, int npixels)
544 int class = FRAME_DISPLAY_INFO (f)->visual->class;
546 /* If display has an immutable color map, freeing colors is not
547 necessary and some servers don't allow it. So don't do it. */
548 if (class != StaticColor && class != StaticGray && class != TrueColor)
550 #ifdef DEBUG_X_COLORS
551 unregister_colors (pixels, npixels);
552 #endif
553 XFreeColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
554 pixels, npixels, 0);
559 #ifdef USE_X_TOOLKIT
561 /* Free colors used on frame F. PIXELS is an array of NPIXELS pixel
562 color values. Interrupt input must be blocked when this function
563 is called. */
565 void
566 x_free_dpy_colors (Display *dpy, Screen *screen, Colormap cmap,
567 unsigned long *pixels, int npixels)
569 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
570 int class = dpyinfo->visual->class;
572 /* If display has an immutable color map, freeing colors is not
573 necessary and some servers don't allow it. So don't do it. */
574 if (class != StaticColor && class != StaticGray && class != TrueColor)
576 #ifdef DEBUG_X_COLORS
577 unregister_colors (pixels, npixels);
578 #endif
579 XFreeColors (dpy, cmap, pixels, npixels, 0);
582 #endif /* USE_X_TOOLKIT */
584 /* Create and return a GC for use on frame F. GC values and mask
585 are given by XGCV and MASK. */
587 static GC
588 x_create_gc (struct frame *f, unsigned long mask, XGCValues *xgcv)
590 GC gc;
591 block_input ();
592 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), mask, xgcv);
593 unblock_input ();
594 IF_DEBUG (++ngcs);
595 return gc;
599 /* Free GC which was used on frame F. */
601 static void
602 x_free_gc (struct frame *f, GC gc)
604 eassert (input_blocked_p ());
605 IF_DEBUG ((--ngcs, eassert (ngcs >= 0)));
606 XFreeGC (FRAME_X_DISPLAY (f), gc);
609 #endif /* HAVE_X_WINDOWS */
611 #ifdef HAVE_NTGUI
612 /* W32 emulation of GCs */
614 static GC
615 x_create_gc (struct frame *f, unsigned long mask, XGCValues *xgcv)
617 GC gc;
618 block_input ();
619 gc = XCreateGC (NULL, FRAME_W32_WINDOW (f), mask, xgcv);
620 unblock_input ();
621 IF_DEBUG (++ngcs);
622 return gc;
626 /* Free GC which was used on frame F. */
628 static void
629 x_free_gc (struct frame *f, GC gc)
631 IF_DEBUG ((--ngcs, eassert (ngcs >= 0)));
632 xfree (gc);
635 #endif /* HAVE_NTGUI */
637 #ifdef HAVE_NS
638 /* NS emulation of GCs */
640 static GC
641 x_create_gc (struct frame *f,
642 unsigned long mask,
643 XGCValues *xgcv)
645 GC gc = xmalloc (sizeof *gc);
646 *gc = *xgcv;
647 return gc;
650 static void
651 x_free_gc (struct frame *f, GC gc)
653 xfree (gc);
655 #endif /* HAVE_NS */
657 /***********************************************************************
658 Frames and faces
659 ***********************************************************************/
661 /* Initialize face cache and basic faces for frame F. */
663 void
664 init_frame_faces (struct frame *f)
666 /* Make a face cache, if F doesn't have one. */
667 if (FRAME_FACE_CACHE (f) == NULL)
668 FRAME_FACE_CACHE (f) = make_face_cache (f);
670 #ifdef HAVE_WINDOW_SYSTEM
671 /* Make the image cache. */
672 if (FRAME_WINDOW_P (f))
674 /* We initialize the image cache when creating the first frame
675 on a terminal, and not during terminal creation. This way,
676 `x-open-connection' on a tty won't create an image cache. */
677 if (FRAME_IMAGE_CACHE (f) == NULL)
678 FRAME_IMAGE_CACHE (f) = make_image_cache ();
679 ++FRAME_IMAGE_CACHE (f)->refcount;
681 #endif /* HAVE_WINDOW_SYSTEM */
683 /* Realize faces early (Bug#17889). */
684 if (!realize_basic_faces (f))
685 emacs_abort ();
689 /* Free face cache of frame F. Called from frame-dependent
690 resource freeing function, e.g. (x|tty)_free_frame_resources. */
692 void
693 free_frame_faces (struct frame *f)
695 struct face_cache *face_cache = FRAME_FACE_CACHE (f);
697 if (face_cache)
699 free_face_cache (face_cache);
700 FRAME_FACE_CACHE (f) = NULL;
703 #ifdef HAVE_WINDOW_SYSTEM
704 if (FRAME_WINDOW_P (f))
706 struct image_cache *image_cache = FRAME_IMAGE_CACHE (f);
707 if (image_cache)
709 --image_cache->refcount;
710 if (image_cache->refcount == 0)
711 free_image_cache (f);
714 #endif /* HAVE_WINDOW_SYSTEM */
718 /* Clear face caches, and recompute basic faces for frame F. Call
719 this after changing frame parameters on which those faces depend,
720 or when realized faces have been freed due to changing attributes
721 of named faces. */
723 void
724 recompute_basic_faces (struct frame *f)
726 if (FRAME_FACE_CACHE (f))
728 clear_face_cache (0);
729 if (!realize_basic_faces (f))
730 emacs_abort ();
735 /* Clear the face caches of all frames. CLEAR_FONTS_P non-zero means
736 try to free unused fonts, too. */
738 void
739 clear_face_cache (int clear_fonts_p)
741 #ifdef HAVE_WINDOW_SYSTEM
742 Lisp_Object tail, frame;
744 if (clear_fonts_p
745 || ++clear_font_table_count == CLEAR_FONT_TABLE_COUNT)
747 /* From time to time see if we can unload some fonts. This also
748 frees all realized faces on all frames. Fonts needed by
749 faces will be loaded again when faces are realized again. */
750 clear_font_table_count = 0;
752 FOR_EACH_FRAME (tail, frame)
754 struct frame *f = XFRAME (frame);
755 if (FRAME_WINDOW_P (f)
756 && FRAME_DISPLAY_INFO (f)->n_fonts > CLEAR_FONT_TABLE_NFONTS)
758 clear_font_cache (f);
759 free_all_realized_faces (frame);
763 else
765 /* Clear GCs of realized faces. */
766 FOR_EACH_FRAME (tail, frame)
768 struct frame *f = XFRAME (frame);
769 if (FRAME_WINDOW_P (f))
770 clear_face_gcs (FRAME_FACE_CACHE (f));
772 clear_image_caches (Qnil);
774 #endif /* HAVE_WINDOW_SYSTEM */
778 DEFUN ("clear-face-cache", Fclear_face_cache, Sclear_face_cache, 0, 1, 0,
779 doc: /* Clear face caches on all frames.
780 Optional THOROUGHLY non-nil means try to free unused fonts, too. */)
781 (Lisp_Object thoroughly)
783 clear_face_cache (!NILP (thoroughly));
784 ++face_change_count;
785 windows_or_buffers_changed = 53;
786 return Qnil;
790 /***********************************************************************
791 X Pixmaps
792 ***********************************************************************/
794 #ifdef HAVE_WINDOW_SYSTEM
796 DEFUN ("bitmap-spec-p", Fbitmap_spec_p, Sbitmap_spec_p, 1, 1, 0,
797 doc: /* Value is non-nil if OBJECT is a valid bitmap specification.
798 A bitmap specification is either a string, a file name, or a list
799 \(WIDTH HEIGHT DATA) where WIDTH is the pixel width of the bitmap,
800 HEIGHT is its height, and DATA is a string containing the bits of
801 the pixmap. Bits are stored row by row, each row occupies
802 \(WIDTH + 7)/8 bytes. */)
803 (Lisp_Object object)
805 bool pixmap_p = 0;
807 if (STRINGP (object))
808 /* If OBJECT is a string, it's a file name. */
809 pixmap_p = 1;
810 else if (CONSP (object))
812 /* Otherwise OBJECT must be (WIDTH HEIGHT DATA), WIDTH and
813 HEIGHT must be ints > 0, and DATA must be string large
814 enough to hold a bitmap of the specified size. */
815 Lisp_Object width, height, data;
817 height = width = data = Qnil;
819 if (CONSP (object))
821 width = XCAR (object);
822 object = XCDR (object);
823 if (CONSP (object))
825 height = XCAR (object);
826 object = XCDR (object);
827 if (CONSP (object))
828 data = XCAR (object);
832 if (STRINGP (data)
833 && RANGED_INTEGERP (1, width, INT_MAX)
834 && RANGED_INTEGERP (1, height, INT_MAX))
836 int bytes_per_row = ((XINT (width) + BITS_PER_CHAR - 1)
837 / BITS_PER_CHAR);
838 if (XINT (height) <= SBYTES (data) / bytes_per_row)
839 pixmap_p = 1;
843 return pixmap_p ? Qt : Qnil;
847 /* Load a bitmap according to NAME (which is either a file name or a
848 pixmap spec) for use on frame F. Value is the bitmap_id (see
849 xfns.c). If NAME is nil, return with a bitmap id of zero. If
850 bitmap cannot be loaded, display a message saying so, and return
851 zero. */
853 static ptrdiff_t
854 load_pixmap (struct frame *f, Lisp_Object name)
856 ptrdiff_t bitmap_id;
858 if (NILP (name))
859 return 0;
861 CHECK_TYPE (!NILP (Fbitmap_spec_p (name)), Qbitmap_spec_p, name);
863 block_input ();
864 if (CONSP (name))
866 /* Decode a bitmap spec into a bitmap. */
868 int h, w;
869 Lisp_Object bits;
871 w = XINT (Fcar (name));
872 h = XINT (Fcar (Fcdr (name)));
873 bits = Fcar (Fcdr (Fcdr (name)));
875 bitmap_id = x_create_bitmap_from_data (f, SSDATA (bits),
876 w, h);
878 else
880 /* It must be a string -- a file name. */
881 bitmap_id = x_create_bitmap_from_file (f, name);
883 unblock_input ();
885 if (bitmap_id < 0)
887 add_to_log ("Invalid or undefined bitmap `%s'", name, Qnil);
888 bitmap_id = 0;
890 else
892 #ifdef GLYPH_DEBUG
893 ++npixmaps_allocated;
894 #endif
897 return bitmap_id;
900 #endif /* HAVE_WINDOW_SYSTEM */
904 /***********************************************************************
905 X Colors
906 ***********************************************************************/
908 /* Parse RGB_LIST, and fill in the RGB fields of COLOR.
909 RGB_LIST should contain (at least) 3 lisp integers.
910 Return 0 if there's a problem with RGB_LIST, otherwise return 1. */
912 static int
913 parse_rgb_list (Lisp_Object rgb_list, XColor *color)
915 #define PARSE_RGB_LIST_FIELD(field) \
916 if (CONSP (rgb_list) && INTEGERP (XCAR (rgb_list))) \
918 color->field = XINT (XCAR (rgb_list)); \
919 rgb_list = XCDR (rgb_list); \
921 else \
922 return 0;
924 PARSE_RGB_LIST_FIELD (red);
925 PARSE_RGB_LIST_FIELD (green);
926 PARSE_RGB_LIST_FIELD (blue);
928 return 1;
932 /* Lookup on frame F the color described by the lisp string COLOR.
933 The resulting tty color is returned in TTY_COLOR; if STD_COLOR is
934 non-zero, then the `standard' definition of the same color is
935 returned in it. */
937 static bool
938 tty_lookup_color (struct frame *f, Lisp_Object color, XColor *tty_color,
939 XColor *std_color)
941 Lisp_Object frame, color_desc;
943 if (!STRINGP (color) || NILP (Ffboundp (Qtty_color_desc)))
944 return 0;
946 XSETFRAME (frame, f);
948 color_desc = call2 (Qtty_color_desc, color, frame);
949 if (CONSP (color_desc) && CONSP (XCDR (color_desc)))
951 Lisp_Object rgb;
953 if (! INTEGERP (XCAR (XCDR (color_desc))))
954 return 0;
956 tty_color->pixel = XINT (XCAR (XCDR (color_desc)));
958 rgb = XCDR (XCDR (color_desc));
959 if (! parse_rgb_list (rgb, tty_color))
960 return 0;
962 /* Should we fill in STD_COLOR too? */
963 if (std_color)
965 /* Default STD_COLOR to the same as TTY_COLOR. */
966 *std_color = *tty_color;
968 /* Do a quick check to see if the returned descriptor is
969 actually _exactly_ equal to COLOR, otherwise we have to
970 lookup STD_COLOR separately. If it's impossible to lookup
971 a standard color, we just give up and use TTY_COLOR. */
972 if ((!STRINGP (XCAR (color_desc))
973 || NILP (Fstring_equal (color, XCAR (color_desc))))
974 && !NILP (Ffboundp (Qtty_color_standard_values)))
976 /* Look up STD_COLOR separately. */
977 rgb = call1 (Qtty_color_standard_values, color);
978 if (! parse_rgb_list (rgb, std_color))
979 return 0;
983 return 1;
985 else if (NILP (Fsymbol_value (intern ("tty-defined-color-alist"))))
986 /* We were called early during startup, and the colors are not
987 yet set up in tty-defined-color-alist. Don't return a failure
988 indication, since this produces the annoying "Unable to
989 load color" messages in the *Messages* buffer. */
990 return 1;
991 else
992 /* tty-color-desc seems to have returned a bad value. */
993 return 0;
996 /* A version of defined_color for non-X frames. */
998 static bool
999 tty_defined_color (struct frame *f, const char *color_name,
1000 XColor *color_def, bool alloc)
1002 bool status = 1;
1004 /* Defaults. */
1005 color_def->pixel = FACE_TTY_DEFAULT_COLOR;
1006 color_def->red = 0;
1007 color_def->blue = 0;
1008 color_def->green = 0;
1010 if (*color_name)
1011 status = tty_lookup_color (f, build_string (color_name), color_def, NULL);
1013 if (color_def->pixel == FACE_TTY_DEFAULT_COLOR && *color_name)
1015 if (strcmp (color_name, "unspecified-fg") == 0)
1016 color_def->pixel = FACE_TTY_DEFAULT_FG_COLOR;
1017 else if (strcmp (color_name, "unspecified-bg") == 0)
1018 color_def->pixel = FACE_TTY_DEFAULT_BG_COLOR;
1021 if (color_def->pixel != FACE_TTY_DEFAULT_COLOR)
1022 status = 1;
1024 return status;
1028 /* Decide if color named COLOR_NAME is valid for the display
1029 associated with the frame F; if so, return the rgb values in
1030 COLOR_DEF. If ALLOC, allocate a new colormap cell.
1032 This does the right thing for any type of frame. */
1034 static bool
1035 defined_color (struct frame *f, const char *color_name, XColor *color_def,
1036 bool alloc)
1038 if (!FRAME_WINDOW_P (f))
1039 return tty_defined_color (f, color_name, color_def, alloc);
1040 #ifdef HAVE_X_WINDOWS
1041 else if (FRAME_X_P (f))
1042 return x_defined_color (f, color_name, color_def, alloc);
1043 #endif
1044 #ifdef HAVE_NTGUI
1045 else if (FRAME_W32_P (f))
1046 return w32_defined_color (f, color_name, color_def, alloc);
1047 #endif
1048 #ifdef HAVE_NS
1049 else if (FRAME_NS_P (f))
1050 return ns_defined_color (f, color_name, color_def, alloc, 1);
1051 #endif
1052 else
1053 emacs_abort ();
1057 /* Given the index IDX of a tty color on frame F, return its name, a
1058 Lisp string. */
1060 Lisp_Object
1061 tty_color_name (struct frame *f, int idx)
1063 if (idx >= 0 && !NILP (Ffboundp (Qtty_color_by_index)))
1065 Lisp_Object frame;
1066 Lisp_Object coldesc;
1068 XSETFRAME (frame, f);
1069 coldesc = call2 (Qtty_color_by_index, make_number (idx), frame);
1071 if (!NILP (coldesc))
1072 return XCAR (coldesc);
1074 #ifdef MSDOS
1075 /* We can have an MSDOG frame under -nw for a short window of
1076 opportunity before internal_terminal_init is called. DTRT. */
1077 if (FRAME_MSDOS_P (f) && !inhibit_window_system)
1078 return msdos_stdcolor_name (idx);
1079 #endif
1081 if (idx == FACE_TTY_DEFAULT_FG_COLOR)
1082 return build_string (unspecified_fg);
1083 if (idx == FACE_TTY_DEFAULT_BG_COLOR)
1084 return build_string (unspecified_bg);
1086 return Qunspecified;
1090 /* Return non-zero if COLOR_NAME is a shade of gray (or white or
1091 black) on frame F.
1093 The criterion implemented here is not a terribly sophisticated one. */
1095 static int
1096 face_color_gray_p (struct frame *f, const char *color_name)
1098 XColor color;
1099 int gray_p;
1101 if (defined_color (f, color_name, &color, 0))
1102 gray_p = (/* Any color sufficiently close to black counts as gray. */
1103 (color.red < 5000 && color.green < 5000 && color.blue < 5000)
1105 ((eabs (color.red - color.green)
1106 < max (color.red, color.green) / 20)
1107 && (eabs (color.green - color.blue)
1108 < max (color.green, color.blue) / 20)
1109 && (eabs (color.blue - color.red)
1110 < max (color.blue, color.red) / 20)));
1111 else
1112 gray_p = 0;
1114 return gray_p;
1118 /* Return non-zero if color COLOR_NAME can be displayed on frame F.
1119 BACKGROUND_P non-zero means the color will be used as background
1120 color. */
1122 static int
1123 face_color_supported_p (struct frame *f, const char *color_name,
1124 int background_p)
1126 Lisp_Object frame;
1127 XColor not_used;
1129 XSETFRAME (frame, f);
1130 return
1131 #ifdef HAVE_WINDOW_SYSTEM
1132 FRAME_WINDOW_P (f)
1133 ? (!NILP (Fxw_display_color_p (frame))
1134 || xstrcasecmp (color_name, "black") == 0
1135 || xstrcasecmp (color_name, "white") == 0
1136 || (background_p
1137 && face_color_gray_p (f, color_name))
1138 || (!NILP (Fx_display_grayscale_p (frame))
1139 && face_color_gray_p (f, color_name)))
1141 #endif
1142 tty_defined_color (f, color_name, &not_used, 0);
1146 DEFUN ("color-gray-p", Fcolor_gray_p, Scolor_gray_p, 1, 2, 0,
1147 doc: /* Return non-nil if COLOR is a shade of gray (or white or black).
1148 FRAME specifies the frame and thus the display for interpreting COLOR.
1149 If FRAME is nil or omitted, use the selected frame. */)
1150 (Lisp_Object color, Lisp_Object frame)
1152 CHECK_STRING (color);
1153 return (face_color_gray_p (decode_any_frame (frame), SSDATA (color))
1154 ? Qt : Qnil);
1158 DEFUN ("color-supported-p", Fcolor_supported_p,
1159 Scolor_supported_p, 1, 3, 0,
1160 doc: /* Return non-nil if COLOR can be displayed on FRAME.
1161 BACKGROUND-P non-nil means COLOR is used as a background.
1162 Otherwise, this function tells whether it can be used as a foreground.
1163 If FRAME is nil or omitted, use the selected frame.
1164 COLOR must be a valid color name. */)
1165 (Lisp_Object color, Lisp_Object frame, Lisp_Object background_p)
1167 CHECK_STRING (color);
1168 return (face_color_supported_p (decode_any_frame (frame),
1169 SSDATA (color), !NILP (background_p))
1170 ? Qt : Qnil);
1174 static unsigned long
1175 load_color2 (struct frame *f, struct face *face, Lisp_Object name,
1176 enum lface_attribute_index target_index, XColor *color)
1178 eassert (STRINGP (name));
1179 eassert (target_index == LFACE_FOREGROUND_INDEX
1180 || target_index == LFACE_BACKGROUND_INDEX
1181 || target_index == LFACE_UNDERLINE_INDEX
1182 || target_index == LFACE_OVERLINE_INDEX
1183 || target_index == LFACE_STRIKE_THROUGH_INDEX
1184 || target_index == LFACE_BOX_INDEX);
1186 /* if the color map is full, defined_color will return a best match
1187 to the values in an existing cell. */
1188 if (!defined_color (f, SSDATA (name), color, 1))
1190 add_to_log ("Unable to load color \"%s\"", name, Qnil);
1192 switch (target_index)
1194 case LFACE_FOREGROUND_INDEX:
1195 face->foreground_defaulted_p = 1;
1196 color->pixel = FRAME_FOREGROUND_PIXEL (f);
1197 break;
1199 case LFACE_BACKGROUND_INDEX:
1200 face->background_defaulted_p = 1;
1201 color->pixel = FRAME_BACKGROUND_PIXEL (f);
1202 break;
1204 case LFACE_UNDERLINE_INDEX:
1205 face->underline_defaulted_p = 1;
1206 color->pixel = FRAME_FOREGROUND_PIXEL (f);
1207 break;
1209 case LFACE_OVERLINE_INDEX:
1210 face->overline_color_defaulted_p = 1;
1211 color->pixel = FRAME_FOREGROUND_PIXEL (f);
1212 break;
1214 case LFACE_STRIKE_THROUGH_INDEX:
1215 face->strike_through_color_defaulted_p = 1;
1216 color->pixel = FRAME_FOREGROUND_PIXEL (f);
1217 break;
1219 case LFACE_BOX_INDEX:
1220 face->box_color_defaulted_p = 1;
1221 color->pixel = FRAME_FOREGROUND_PIXEL (f);
1222 break;
1224 default:
1225 emacs_abort ();
1228 #ifdef GLYPH_DEBUG
1229 else
1230 ++ncolors_allocated;
1231 #endif
1233 return color->pixel;
1236 /* Load color with name NAME for use by face FACE on frame F.
1237 TARGET_INDEX must be one of LFACE_FOREGROUND_INDEX,
1238 LFACE_BACKGROUND_INDEX, LFACE_UNDERLINE_INDEX, LFACE_OVERLINE_INDEX,
1239 LFACE_STRIKE_THROUGH_INDEX, or LFACE_BOX_INDEX. Value is the
1240 pixel color. If color cannot be loaded, display a message, and
1241 return the foreground, background or underline color of F, but
1242 record that fact in flags of the face so that we don't try to free
1243 these colors. */
1245 #ifndef MSDOS
1246 static
1247 #endif
1248 unsigned long
1249 load_color (struct frame *f, struct face *face, Lisp_Object name,
1250 enum lface_attribute_index target_index)
1252 XColor color;
1253 return load_color2 (f, face, name, target_index, &color);
1257 #ifdef HAVE_WINDOW_SYSTEM
1259 #define NEAR_SAME_COLOR_THRESHOLD 30000
1261 /* Load colors for face FACE which is used on frame F. Colors are
1262 specified by slots LFACE_BACKGROUND_INDEX and LFACE_FOREGROUND_INDEX
1263 of ATTRS. If the background color specified is not supported on F,
1264 try to emulate gray colors with a stipple from Vface_default_stipple. */
1266 static void
1267 load_face_colors (struct frame *f, struct face *face,
1268 Lisp_Object attrs[LFACE_VECTOR_SIZE])
1270 Lisp_Object fg, bg, dfg;
1271 XColor xfg, xbg;
1273 bg = attrs[LFACE_BACKGROUND_INDEX];
1274 fg = attrs[LFACE_FOREGROUND_INDEX];
1276 /* Swap colors if face is inverse-video. */
1277 if (EQ (attrs[LFACE_INVERSE_INDEX], Qt))
1279 Lisp_Object tmp;
1280 tmp = fg;
1281 fg = bg;
1282 bg = tmp;
1285 /* Check for support for foreground, not for background because
1286 face_color_supported_p is smart enough to know that grays are
1287 "supported" as background because we are supposed to use stipple
1288 for them. */
1289 if (!face_color_supported_p (f, SSDATA (bg), 0)
1290 && !NILP (Fbitmap_spec_p (Vface_default_stipple)))
1292 x_destroy_bitmap (f, face->stipple);
1293 face->stipple = load_pixmap (f, Vface_default_stipple);
1296 face->background = load_color2 (f, face, bg, LFACE_BACKGROUND_INDEX, &xbg);
1297 face->foreground = load_color2 (f, face, fg, LFACE_FOREGROUND_INDEX, &xfg);
1299 dfg = attrs[LFACE_DISTANT_FOREGROUND_INDEX];
1300 if (!NILP (dfg) && !UNSPECIFIEDP (dfg)
1301 && color_distance (&xbg, &xfg) < NEAR_SAME_COLOR_THRESHOLD)
1303 if (EQ (attrs[LFACE_INVERSE_INDEX], Qt))
1304 face->background = load_color (f, face, dfg, LFACE_BACKGROUND_INDEX);
1305 else
1306 face->foreground = load_color (f, face, dfg, LFACE_FOREGROUND_INDEX);
1310 #ifdef HAVE_X_WINDOWS
1312 /* Free color PIXEL on frame F. */
1314 void
1315 unload_color (struct frame *f, unsigned long pixel)
1317 if (pixel != -1)
1319 block_input ();
1320 x_free_colors (f, &pixel, 1);
1321 unblock_input ();
1325 /* Free colors allocated for FACE. */
1327 static void
1328 free_face_colors (struct frame *f, struct face *face)
1330 /* PENDING(NS): need to do something here? */
1332 if (face->colors_copied_bitwise_p)
1333 return;
1335 block_input ();
1337 if (!face->foreground_defaulted_p)
1339 x_free_colors (f, &face->foreground, 1);
1340 IF_DEBUG (--ncolors_allocated);
1343 if (!face->background_defaulted_p)
1345 x_free_colors (f, &face->background, 1);
1346 IF_DEBUG (--ncolors_allocated);
1349 if (face->underline_p
1350 && !face->underline_defaulted_p)
1352 x_free_colors (f, &face->underline_color, 1);
1353 IF_DEBUG (--ncolors_allocated);
1356 if (face->overline_p
1357 && !face->overline_color_defaulted_p)
1359 x_free_colors (f, &face->overline_color, 1);
1360 IF_DEBUG (--ncolors_allocated);
1363 if (face->strike_through_p
1364 && !face->strike_through_color_defaulted_p)
1366 x_free_colors (f, &face->strike_through_color, 1);
1367 IF_DEBUG (--ncolors_allocated);
1370 if (face->box != FACE_NO_BOX
1371 && !face->box_color_defaulted_p)
1373 x_free_colors (f, &face->box_color, 1);
1374 IF_DEBUG (--ncolors_allocated);
1377 unblock_input ();
1380 #endif /* HAVE_X_WINDOWS */
1382 #endif /* HAVE_WINDOW_SYSTEM */
1386 /***********************************************************************
1387 XLFD Font Names
1388 ***********************************************************************/
1390 /* An enumerator for each field of an XLFD font name. */
1392 enum xlfd_field
1394 XLFD_FOUNDRY,
1395 XLFD_FAMILY,
1396 XLFD_WEIGHT,
1397 XLFD_SLANT,
1398 XLFD_SWIDTH,
1399 XLFD_ADSTYLE,
1400 XLFD_PIXEL_SIZE,
1401 XLFD_POINT_SIZE,
1402 XLFD_RESX,
1403 XLFD_RESY,
1404 XLFD_SPACING,
1405 XLFD_AVGWIDTH,
1406 XLFD_REGISTRY,
1407 XLFD_ENCODING,
1408 XLFD_LAST
1411 /* An enumerator for each possible slant value of a font. Taken from
1412 the XLFD specification. */
1414 enum xlfd_slant
1416 XLFD_SLANT_UNKNOWN,
1417 XLFD_SLANT_ROMAN,
1418 XLFD_SLANT_ITALIC,
1419 XLFD_SLANT_OBLIQUE,
1420 XLFD_SLANT_REVERSE_ITALIC,
1421 XLFD_SLANT_REVERSE_OBLIQUE,
1422 XLFD_SLANT_OTHER
1425 /* Relative font weight according to XLFD documentation. */
1427 enum xlfd_weight
1429 XLFD_WEIGHT_UNKNOWN,
1430 XLFD_WEIGHT_ULTRA_LIGHT, /* 10 */
1431 XLFD_WEIGHT_EXTRA_LIGHT, /* 20 */
1432 XLFD_WEIGHT_LIGHT, /* 30 */
1433 XLFD_WEIGHT_SEMI_LIGHT, /* 40: SemiLight, Book, ... */
1434 XLFD_WEIGHT_MEDIUM, /* 50: Medium, Normal, Regular, ... */
1435 XLFD_WEIGHT_SEMI_BOLD, /* 60: SemiBold, DemiBold, ... */
1436 XLFD_WEIGHT_BOLD, /* 70: Bold, ... */
1437 XLFD_WEIGHT_EXTRA_BOLD, /* 80: ExtraBold, Heavy, ... */
1438 XLFD_WEIGHT_ULTRA_BOLD /* 90: UltraBold, Black, ... */
1441 /* Relative proportionate width. */
1443 enum xlfd_swidth
1445 XLFD_SWIDTH_UNKNOWN,
1446 XLFD_SWIDTH_ULTRA_CONDENSED, /* 10 */
1447 XLFD_SWIDTH_EXTRA_CONDENSED, /* 20 */
1448 XLFD_SWIDTH_CONDENSED, /* 30: Condensed, Narrow, Compressed, ... */
1449 XLFD_SWIDTH_SEMI_CONDENSED, /* 40: semicondensed */
1450 XLFD_SWIDTH_MEDIUM, /* 50: Medium, Normal, Regular, ... */
1451 XLFD_SWIDTH_SEMI_EXPANDED, /* 60: SemiExpanded, DemiExpanded, ... */
1452 XLFD_SWIDTH_EXPANDED, /* 70: Expanded... */
1453 XLFD_SWIDTH_EXTRA_EXPANDED, /* 80: ExtraExpanded, Wide... */
1454 XLFD_SWIDTH_ULTRA_EXPANDED /* 90: UltraExpanded... */
1457 /* Order by which font selection chooses fonts. The default values
1458 mean `first, find a best match for the font width, then for the
1459 font height, then for weight, then for slant.' This variable can be
1460 set via set-face-font-sort-order. */
1462 static int font_sort_order[4];
1464 #ifdef HAVE_WINDOW_SYSTEM
1466 static enum font_property_index font_props_for_sorting[FONT_SIZE_INDEX];
1468 static int
1469 compare_fonts_by_sort_order (const void *v1, const void *v2)
1471 Lisp_Object const *p1 = v1;
1472 Lisp_Object const *p2 = v2;
1473 Lisp_Object font1 = *p1;
1474 Lisp_Object font2 = *p2;
1475 int i;
1477 for (i = 0; i < FONT_SIZE_INDEX; i++)
1479 enum font_property_index idx = font_props_for_sorting[i];
1480 Lisp_Object val1 = AREF (font1, idx), val2 = AREF (font2, idx);
1481 int result;
1483 if (idx <= FONT_REGISTRY_INDEX)
1485 if (STRINGP (val1))
1486 result = STRINGP (val2) ? strcmp (SSDATA (val1), SSDATA (val2)) : -1;
1487 else
1488 result = STRINGP (val2) ? 1 : 0;
1490 else
1492 if (INTEGERP (val1))
1493 result = (INTEGERP (val2) && XINT (val1) >= XINT (val2)
1494 ? XINT (val1) > XINT (val2)
1495 : -1);
1496 else
1497 result = INTEGERP (val2) ? 1 : 0;
1499 if (result)
1500 return result;
1502 return 0;
1505 DEFUN ("x-family-fonts", Fx_family_fonts, Sx_family_fonts, 0, 2, 0,
1506 doc: /* Return a list of available fonts of family FAMILY on FRAME.
1507 If FAMILY is omitted or nil, list all families.
1508 Otherwise, FAMILY must be a string, possibly containing wildcards
1509 `?' and `*'.
1510 If FRAME is omitted or nil, use the selected frame.
1511 Each element of the result is a vector [FAMILY WIDTH POINT-SIZE WEIGHT
1512 SLANT FIXED-P FULL REGISTRY-AND-ENCODING].
1513 FAMILY is the font family name. POINT-SIZE is the size of the
1514 font in 1/10 pt. WIDTH, WEIGHT, and SLANT are symbols describing the
1515 width, weight and slant of the font. These symbols are the same as for
1516 face attributes. FIXED-P is non-nil if the font is fixed-pitch.
1517 FULL is the full name of the font, and REGISTRY-AND-ENCODING is a string
1518 giving the registry and encoding of the font.
1519 The result list is sorted according to the current setting of
1520 the face font sort order. */)
1521 (Lisp_Object family, Lisp_Object frame)
1523 Lisp_Object font_spec, list, *drivers, vec;
1524 struct frame *f = decode_live_frame (frame);
1525 ptrdiff_t i, nfonts;
1526 EMACS_INT ndrivers;
1527 Lisp_Object result;
1528 USE_SAFE_ALLOCA;
1530 font_spec = Ffont_spec (0, NULL);
1531 if (!NILP (family))
1533 CHECK_STRING (family);
1534 font_parse_family_registry (family, Qnil, font_spec);
1537 list = font_list_entities (f, font_spec);
1538 if (NILP (list))
1539 return Qnil;
1541 /* Sort the font entities. */
1542 for (i = 0; i < 4; i++)
1543 switch (font_sort_order[i])
1545 case XLFD_SWIDTH:
1546 font_props_for_sorting[i] = FONT_WIDTH_INDEX; break;
1547 case XLFD_POINT_SIZE:
1548 font_props_for_sorting[i] = FONT_SIZE_INDEX; break;
1549 case XLFD_WEIGHT:
1550 font_props_for_sorting[i] = FONT_WEIGHT_INDEX; break;
1551 default:
1552 font_props_for_sorting[i] = FONT_SLANT_INDEX; break;
1554 font_props_for_sorting[i++] = FONT_FAMILY_INDEX;
1555 font_props_for_sorting[i++] = FONT_FOUNDRY_INDEX;
1556 font_props_for_sorting[i++] = FONT_ADSTYLE_INDEX;
1557 font_props_for_sorting[i++] = FONT_REGISTRY_INDEX;
1559 ndrivers = XINT (Flength (list));
1560 SAFE_ALLOCA_LISP (drivers, ndrivers);
1561 for (i = 0; i < ndrivers; i++, list = XCDR (list))
1562 drivers[i] = XCAR (list);
1563 vec = Fvconcat (ndrivers, drivers);
1564 nfonts = ASIZE (vec);
1566 qsort (XVECTOR (vec)->contents, nfonts, word_size,
1567 compare_fonts_by_sort_order);
1569 result = Qnil;
1570 for (i = nfonts - 1; i >= 0; --i)
1572 Lisp_Object font = AREF (vec, i);
1573 Lisp_Object v = make_uninit_vector (8);
1574 int point;
1575 Lisp_Object spacing;
1577 ASET (v, 0, AREF (font, FONT_FAMILY_INDEX));
1578 ASET (v, 1, FONT_WIDTH_SYMBOLIC (font));
1579 point = PIXEL_TO_POINT (XINT (AREF (font, FONT_SIZE_INDEX)) * 10,
1580 FRAME_RES_Y (f));
1581 ASET (v, 2, make_number (point));
1582 ASET (v, 3, FONT_WEIGHT_SYMBOLIC (font));
1583 ASET (v, 4, FONT_SLANT_SYMBOLIC (font));
1584 spacing = Ffont_get (font, QCspacing);
1585 ASET (v, 5, (NILP (spacing) || EQ (spacing, Qp)) ? Qnil : Qt);
1586 ASET (v, 6, Ffont_xlfd_name (font, Qnil));
1587 ASET (v, 7, AREF (font, FONT_REGISTRY_INDEX));
1589 result = Fcons (v, result);
1592 SAFE_FREE ();
1593 return result;
1596 DEFUN ("x-list-fonts", Fx_list_fonts, Sx_list_fonts, 1, 5, 0,
1597 doc: /* Return a list of the names of available fonts matching PATTERN.
1598 If optional arguments FACE and FRAME are specified, return only fonts
1599 the same size as FACE on FRAME.
1601 PATTERN should be a string containing a font name in the XLFD,
1602 Fontconfig, or GTK format. A font name given in the XLFD format may
1603 contain wildcard characters:
1604 the * character matches any substring, and
1605 the ? character matches any single character.
1606 PATTERN is case-insensitive.
1608 The return value is a list of strings, suitable as arguments to
1609 `set-face-font'.
1611 Fonts Emacs can't use may or may not be excluded
1612 even if they match PATTERN and FACE.
1613 The optional fourth argument MAXIMUM sets a limit on how many
1614 fonts to match. The first MAXIMUM fonts are reported.
1615 The optional fifth argument WIDTH, if specified, is a number of columns
1616 occupied by a character of a font. In that case, return only fonts
1617 the WIDTH times as wide as FACE on FRAME. */)
1618 (Lisp_Object pattern, Lisp_Object face, Lisp_Object frame,
1619 Lisp_Object maximum, Lisp_Object width)
1621 struct frame *f;
1622 int size, avgwidth IF_LINT (= 0);
1624 check_window_system (NULL);
1625 CHECK_STRING (pattern);
1627 if (! NILP (maximum))
1628 CHECK_NATNUM (maximum);
1630 if (!NILP (width))
1631 CHECK_NUMBER (width);
1633 /* We can't simply call decode_window_system_frame because
1634 this function may be called before any frame is created. */
1635 f = decode_live_frame (frame);
1636 if (! FRAME_WINDOW_P (f))
1638 /* Perhaps we have not yet created any frame. */
1639 f = NULL;
1640 frame = Qnil;
1641 face = Qnil;
1643 else
1644 XSETFRAME (frame, f);
1646 /* Determine the width standard for comparison with the fonts we find. */
1648 if (NILP (face))
1649 size = 0;
1650 else
1652 /* This is of limited utility since it works with character
1653 widths. Keep it for compatibility. --gerd. */
1654 int face_id = lookup_named_face (f, face, 0);
1655 struct face *width_face = (face_id < 0
1656 ? NULL
1657 : FACE_FROM_ID (f, face_id));
1659 if (width_face && width_face->font)
1661 size = width_face->font->pixel_size;
1662 avgwidth = width_face->font->average_width;
1664 else
1666 size = FRAME_FONT (f)->pixel_size;
1667 avgwidth = FRAME_FONT (f)->average_width;
1669 if (!NILP (width))
1670 avgwidth *= XINT (width);
1674 Lisp_Object font_spec;
1675 Lisp_Object args[2], tail;
1677 font_spec = font_spec_from_name (pattern);
1678 if (!FONTP (font_spec))
1679 signal_error ("Invalid font name", pattern);
1681 if (size)
1683 Ffont_put (font_spec, QCsize, make_number (size));
1684 Ffont_put (font_spec, QCavgwidth, make_number (avgwidth));
1686 args[0] = Flist_fonts (font_spec, frame, maximum, font_spec);
1687 for (tail = args[0]; CONSP (tail); tail = XCDR (tail))
1689 Lisp_Object font_entity;
1691 font_entity = XCAR (tail);
1692 if ((NILP (AREF (font_entity, FONT_SIZE_INDEX))
1693 || XINT (AREF (font_entity, FONT_SIZE_INDEX)) == 0)
1694 && ! NILP (AREF (font_spec, FONT_SIZE_INDEX)))
1696 /* This is a scalable font. For backward compatibility,
1697 we set the specified size. */
1698 font_entity = copy_font_spec (font_entity);
1699 ASET (font_entity, FONT_SIZE_INDEX,
1700 AREF (font_spec, FONT_SIZE_INDEX));
1702 XSETCAR (tail, Ffont_xlfd_name (font_entity, Qnil));
1704 if (NILP (frame))
1705 /* We don't have to check fontsets. */
1706 return args[0];
1707 args[1] = list_fontsets (f, pattern, size);
1708 return Fnconc (2, args);
1712 #endif /* HAVE_WINDOW_SYSTEM */
1715 /***********************************************************************
1716 Lisp Faces
1717 ***********************************************************************/
1719 /* Access face attributes of face LFACE, a Lisp vector. */
1721 #define LFACE_FAMILY(LFACE) AREF ((LFACE), LFACE_FAMILY_INDEX)
1722 #define LFACE_FOUNDRY(LFACE) AREF ((LFACE), LFACE_FOUNDRY_INDEX)
1723 #define LFACE_HEIGHT(LFACE) AREF ((LFACE), LFACE_HEIGHT_INDEX)
1724 #define LFACE_WEIGHT(LFACE) AREF ((LFACE), LFACE_WEIGHT_INDEX)
1725 #define LFACE_SLANT(LFACE) AREF ((LFACE), LFACE_SLANT_INDEX)
1726 #define LFACE_UNDERLINE(LFACE) AREF ((LFACE), LFACE_UNDERLINE_INDEX)
1727 #define LFACE_INVERSE(LFACE) AREF ((LFACE), LFACE_INVERSE_INDEX)
1728 #define LFACE_FOREGROUND(LFACE) AREF ((LFACE), LFACE_FOREGROUND_INDEX)
1729 #define LFACE_BACKGROUND(LFACE) AREF ((LFACE), LFACE_BACKGROUND_INDEX)
1730 #define LFACE_STIPPLE(LFACE) AREF ((LFACE), LFACE_STIPPLE_INDEX)
1731 #define LFACE_SWIDTH(LFACE) AREF ((LFACE), LFACE_SWIDTH_INDEX)
1732 #define LFACE_OVERLINE(LFACE) AREF ((LFACE), LFACE_OVERLINE_INDEX)
1733 #define LFACE_STRIKE_THROUGH(LFACE) AREF ((LFACE), LFACE_STRIKE_THROUGH_INDEX)
1734 #define LFACE_BOX(LFACE) AREF ((LFACE), LFACE_BOX_INDEX)
1735 #define LFACE_FONT(LFACE) AREF ((LFACE), LFACE_FONT_INDEX)
1736 #define LFACE_INHERIT(LFACE) AREF ((LFACE), LFACE_INHERIT_INDEX)
1737 #define LFACE_FONTSET(LFACE) AREF ((LFACE), LFACE_FONTSET_INDEX)
1738 #define LFACE_DISTANT_FOREGROUND(LFACE) \
1739 AREF ((LFACE), LFACE_DISTANT_FOREGROUND_INDEX)
1741 /* Non-zero if LFACE is a Lisp face. A Lisp face is a vector of size
1742 LFACE_VECTOR_SIZE which has the symbol `face' in slot 0. */
1744 #define LFACEP(LFACE) \
1745 (VECTORP (LFACE) \
1746 && ASIZE (LFACE) == LFACE_VECTOR_SIZE \
1747 && EQ (AREF (LFACE, 0), Qface))
1750 #ifdef GLYPH_DEBUG
1752 /* Check consistency of Lisp face attribute vector ATTRS. */
1754 static void
1755 check_lface_attrs (Lisp_Object attrs[LFACE_VECTOR_SIZE])
1757 eassert (UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
1758 || IGNORE_DEFFACE_P (attrs[LFACE_FAMILY_INDEX])
1759 || STRINGP (attrs[LFACE_FAMILY_INDEX]));
1760 eassert (UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
1761 || IGNORE_DEFFACE_P (attrs[LFACE_FOUNDRY_INDEX])
1762 || STRINGP (attrs[LFACE_FOUNDRY_INDEX]));
1763 eassert (UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
1764 || IGNORE_DEFFACE_P (attrs[LFACE_SWIDTH_INDEX])
1765 || SYMBOLP (attrs[LFACE_SWIDTH_INDEX]));
1766 eassert (UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
1767 || IGNORE_DEFFACE_P (attrs[LFACE_HEIGHT_INDEX])
1768 || INTEGERP (attrs[LFACE_HEIGHT_INDEX])
1769 || FLOATP (attrs[LFACE_HEIGHT_INDEX])
1770 || FUNCTIONP (attrs[LFACE_HEIGHT_INDEX]));
1771 eassert (UNSPECIFIEDP (attrs[LFACE_WEIGHT_INDEX])
1772 || IGNORE_DEFFACE_P (attrs[LFACE_WEIGHT_INDEX])
1773 || SYMBOLP (attrs[LFACE_WEIGHT_INDEX]));
1774 eassert (UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX])
1775 || IGNORE_DEFFACE_P (attrs[LFACE_SLANT_INDEX])
1776 || SYMBOLP (attrs[LFACE_SLANT_INDEX]));
1777 eassert (UNSPECIFIEDP (attrs[LFACE_UNDERLINE_INDEX])
1778 || IGNORE_DEFFACE_P (attrs[LFACE_UNDERLINE_INDEX])
1779 || SYMBOLP (attrs[LFACE_UNDERLINE_INDEX])
1780 || STRINGP (attrs[LFACE_UNDERLINE_INDEX])
1781 || CONSP (attrs[LFACE_UNDERLINE_INDEX]));
1782 eassert (UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
1783 || IGNORE_DEFFACE_P (attrs[LFACE_OVERLINE_INDEX])
1784 || SYMBOLP (attrs[LFACE_OVERLINE_INDEX])
1785 || STRINGP (attrs[LFACE_OVERLINE_INDEX]));
1786 eassert (UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
1787 || IGNORE_DEFFACE_P (attrs[LFACE_STRIKE_THROUGH_INDEX])
1788 || SYMBOLP (attrs[LFACE_STRIKE_THROUGH_INDEX])
1789 || STRINGP (attrs[LFACE_STRIKE_THROUGH_INDEX]));
1790 eassert (UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
1791 || IGNORE_DEFFACE_P (attrs[LFACE_BOX_INDEX])
1792 || SYMBOLP (attrs[LFACE_BOX_INDEX])
1793 || STRINGP (attrs[LFACE_BOX_INDEX])
1794 || INTEGERP (attrs[LFACE_BOX_INDEX])
1795 || CONSP (attrs[LFACE_BOX_INDEX]));
1796 eassert (UNSPECIFIEDP (attrs[LFACE_INVERSE_INDEX])
1797 || IGNORE_DEFFACE_P (attrs[LFACE_INVERSE_INDEX])
1798 || SYMBOLP (attrs[LFACE_INVERSE_INDEX]));
1799 eassert (UNSPECIFIEDP (attrs[LFACE_FOREGROUND_INDEX])
1800 || IGNORE_DEFFACE_P (attrs[LFACE_FOREGROUND_INDEX])
1801 || STRINGP (attrs[LFACE_FOREGROUND_INDEX]));
1802 eassert (UNSPECIFIEDP (attrs[LFACE_DISTANT_FOREGROUND_INDEX])
1803 || IGNORE_DEFFACE_P (attrs[LFACE_DISTANT_FOREGROUND_INDEX])
1804 || STRINGP (attrs[LFACE_DISTANT_FOREGROUND_INDEX]));
1805 eassert (UNSPECIFIEDP (attrs[LFACE_BACKGROUND_INDEX])
1806 || IGNORE_DEFFACE_P (attrs[LFACE_BACKGROUND_INDEX])
1807 || STRINGP (attrs[LFACE_BACKGROUND_INDEX]));
1808 eassert (UNSPECIFIEDP (attrs[LFACE_INHERIT_INDEX])
1809 || IGNORE_DEFFACE_P (attrs[LFACE_INHERIT_INDEX])
1810 || NILP (attrs[LFACE_INHERIT_INDEX])
1811 || SYMBOLP (attrs[LFACE_INHERIT_INDEX])
1812 || CONSP (attrs[LFACE_INHERIT_INDEX]));
1813 #ifdef HAVE_WINDOW_SYSTEM
1814 eassert (UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
1815 || IGNORE_DEFFACE_P (attrs[LFACE_STIPPLE_INDEX])
1816 || SYMBOLP (attrs[LFACE_STIPPLE_INDEX])
1817 || !NILP (Fbitmap_spec_p (attrs[LFACE_STIPPLE_INDEX])));
1818 eassert (UNSPECIFIEDP (attrs[LFACE_FONT_INDEX])
1819 || IGNORE_DEFFACE_P (attrs[LFACE_FONT_INDEX])
1820 || FONTP (attrs[LFACE_FONT_INDEX]));
1821 eassert (UNSPECIFIEDP (attrs[LFACE_FONTSET_INDEX])
1822 || STRINGP (attrs[LFACE_FONTSET_INDEX])
1823 || NILP (attrs[LFACE_FONTSET_INDEX]));
1824 #endif
1828 /* Check consistency of attributes of Lisp face LFACE (a Lisp vector). */
1830 static void
1831 check_lface (Lisp_Object lface)
1833 if (!NILP (lface))
1835 eassert (LFACEP (lface));
1836 check_lface_attrs (XVECTOR (lface)->contents);
1840 #else /* not GLYPH_DEBUG */
1842 #define check_lface_attrs(attrs) (void) 0
1843 #define check_lface(lface) (void) 0
1845 #endif /* GLYPH_DEBUG */
1849 /* Face-merge cycle checking. */
1851 enum named_merge_point_kind
1853 NAMED_MERGE_POINT_NORMAL,
1854 NAMED_MERGE_POINT_REMAP
1857 /* A `named merge point' is simply a point during face-merging where we
1858 look up a face by name. We keep a stack of which named lookups we're
1859 currently processing so that we can easily detect cycles, using a
1860 linked- list of struct named_merge_point structures, typically
1861 allocated on the stack frame of the named lookup functions which are
1862 active (so no consing is required). */
1863 struct named_merge_point
1865 Lisp_Object face_name;
1866 enum named_merge_point_kind named_merge_point_kind;
1867 struct named_merge_point *prev;
1871 /* If a face merging cycle is detected for FACE_NAME, return 0,
1872 otherwise add NEW_NAMED_MERGE_POINT, which is initialized using
1873 FACE_NAME and NAMED_MERGE_POINT_KIND, as the head of the linked list
1874 pointed to by NAMED_MERGE_POINTS, and return 1. */
1876 static int
1877 push_named_merge_point (struct named_merge_point *new_named_merge_point,
1878 Lisp_Object face_name,
1879 enum named_merge_point_kind named_merge_point_kind,
1880 struct named_merge_point **named_merge_points)
1882 struct named_merge_point *prev;
1884 for (prev = *named_merge_points; prev; prev = prev->prev)
1885 if (EQ (face_name, prev->face_name))
1887 if (prev->named_merge_point_kind == named_merge_point_kind)
1888 /* A cycle, so fail. */
1889 return 0;
1890 else if (prev->named_merge_point_kind == NAMED_MERGE_POINT_REMAP)
1891 /* A remap `hides ' any previous normal merge points
1892 (because the remap means that it's actually different face),
1893 so as we know the current merge point must be normal, we
1894 can just assume it's OK. */
1895 break;
1898 new_named_merge_point->face_name = face_name;
1899 new_named_merge_point->named_merge_point_kind = named_merge_point_kind;
1900 new_named_merge_point->prev = *named_merge_points;
1902 *named_merge_points = new_named_merge_point;
1904 return 1;
1908 /* Resolve face name FACE_NAME. If FACE_NAME is a string, intern it
1909 to make it a symbol. If FACE_NAME is an alias for another face,
1910 return that face's name.
1912 Return default face in case of errors. */
1914 static Lisp_Object
1915 resolve_face_name (Lisp_Object face_name, int signal_p)
1917 Lisp_Object orig_face;
1918 Lisp_Object tortoise, hare;
1920 if (STRINGP (face_name))
1921 face_name = intern (SSDATA (face_name));
1923 if (NILP (face_name) || !SYMBOLP (face_name))
1924 return face_name;
1926 orig_face = face_name;
1927 tortoise = hare = face_name;
1929 while (1)
1931 face_name = hare;
1932 hare = Fget (hare, Qface_alias);
1933 if (NILP (hare) || !SYMBOLP (hare))
1934 break;
1936 face_name = hare;
1937 hare = Fget (hare, Qface_alias);
1938 if (NILP (hare) || !SYMBOLP (hare))
1939 break;
1941 tortoise = Fget (tortoise, Qface_alias);
1942 if (EQ (hare, tortoise))
1944 if (signal_p)
1945 xsignal1 (Qcircular_list, orig_face);
1946 return Qdefault;
1950 return face_name;
1954 /* Return the face definition of FACE_NAME on frame F. F null means
1955 return the definition for new frames. FACE_NAME may be a string or
1956 a symbol (apparently Emacs 20.2 allowed strings as face names in
1957 face text properties; Ediff uses that). If SIGNAL_P is non-zero,
1958 signal an error if FACE_NAME is not a valid face name. If SIGNAL_P
1959 is zero, value is nil if FACE_NAME is not a valid face name. */
1960 static Lisp_Object
1961 lface_from_face_name_no_resolve (struct frame *f, Lisp_Object face_name,
1962 int signal_p)
1964 Lisp_Object lface;
1966 if (f)
1967 lface = assq_no_quit (face_name, f->face_alist);
1968 else
1969 lface = assq_no_quit (face_name, Vface_new_frame_defaults);
1971 if (CONSP (lface))
1972 lface = XCDR (lface);
1973 else if (signal_p)
1974 signal_error ("Invalid face", face_name);
1976 check_lface (lface);
1978 return lface;
1981 /* Return the face definition of FACE_NAME on frame F. F null means
1982 return the definition for new frames. FACE_NAME may be a string or
1983 a symbol (apparently Emacs 20.2 allowed strings as face names in
1984 face text properties; Ediff uses that). If FACE_NAME is an alias
1985 for another face, return that face's definition. If SIGNAL_P is
1986 non-zero, signal an error if FACE_NAME is not a valid face name.
1987 If SIGNAL_P is zero, value is nil if FACE_NAME is not a valid face
1988 name. */
1989 static Lisp_Object
1990 lface_from_face_name (struct frame *f, Lisp_Object face_name, int signal_p)
1992 face_name = resolve_face_name (face_name, signal_p);
1993 return lface_from_face_name_no_resolve (f, face_name, signal_p);
1997 /* Get face attributes of face FACE_NAME from frame-local faces on
1998 frame F. Store the resulting attributes in ATTRS which must point
1999 to a vector of Lisp_Objects of size LFACE_VECTOR_SIZE. If SIGNAL_P
2000 is non-zero, signal an error if FACE_NAME does not name a face.
2001 Otherwise, value is zero if FACE_NAME is not a face. */
2003 static int
2004 get_lface_attributes_no_remap (struct frame *f, Lisp_Object face_name,
2005 Lisp_Object attrs[LFACE_VECTOR_SIZE],
2006 int signal_p)
2008 Lisp_Object lface;
2010 lface = lface_from_face_name_no_resolve (f, face_name, signal_p);
2012 if (! NILP (lface))
2013 memcpy (attrs, XVECTOR (lface)->contents,
2014 LFACE_VECTOR_SIZE * sizeof *attrs);
2016 return !NILP (lface);
2019 /* Get face attributes of face FACE_NAME from frame-local faces on frame
2020 F. Store the resulting attributes in ATTRS which must point to a
2021 vector of Lisp_Objects of size LFACE_VECTOR_SIZE. If FACE_NAME is an
2022 alias for another face, use that face's definition. If SIGNAL_P is
2023 non-zero, signal an error if FACE_NAME does not name a face.
2024 Otherwise, value is zero if FACE_NAME is not a face. */
2026 static int
2027 get_lface_attributes (struct frame *f, Lisp_Object face_name,
2028 Lisp_Object attrs[LFACE_VECTOR_SIZE], int signal_p,
2029 struct named_merge_point *named_merge_points)
2031 Lisp_Object face_remapping;
2033 face_name = resolve_face_name (face_name, signal_p);
2035 /* See if SYMBOL has been remapped to some other face (usually this
2036 is done buffer-locally). */
2037 face_remapping = assq_no_quit (face_name, Vface_remapping_alist);
2038 if (CONSP (face_remapping))
2040 struct named_merge_point named_merge_point;
2042 if (push_named_merge_point (&named_merge_point,
2043 face_name, NAMED_MERGE_POINT_REMAP,
2044 &named_merge_points))
2046 int i;
2048 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2049 attrs[i] = Qunspecified;
2051 return merge_face_ref (f, XCDR (face_remapping), attrs,
2052 signal_p, named_merge_points);
2056 /* Default case, no remapping. */
2057 return get_lface_attributes_no_remap (f, face_name, attrs, signal_p);
2061 /* Non-zero if all attributes in face attribute vector ATTRS are
2062 specified, i.e. are non-nil. */
2064 static int
2065 lface_fully_specified_p (Lisp_Object attrs[LFACE_VECTOR_SIZE])
2067 int i;
2069 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2070 if (i != LFACE_FONT_INDEX && i != LFACE_INHERIT_INDEX
2071 && i != LFACE_DISTANT_FOREGROUND_INDEX)
2072 if ((UNSPECIFIEDP (attrs[i]) || IGNORE_DEFFACE_P (attrs[i])))
2073 break;
2075 return i == LFACE_VECTOR_SIZE;
2078 #ifdef HAVE_WINDOW_SYSTEM
2080 /* Set font-related attributes of Lisp face LFACE from FONT-OBJECT.
2081 If FORCE_P is zero, set only unspecified attributes of LFACE. The
2082 exception is `font' attribute. It is set to FONT_OBJECT regardless
2083 of FORCE_P. */
2085 static int
2086 set_lface_from_font (struct frame *f, Lisp_Object lface,
2087 Lisp_Object font_object, int force_p)
2089 Lisp_Object val;
2090 struct font *font = XFONT_OBJECT (font_object);
2092 /* Set attributes only if unspecified, otherwise face defaults for
2093 new frames would never take effect. If the font doesn't have a
2094 specific property, set a normal value for that. */
2096 if (force_p || UNSPECIFIEDP (LFACE_FAMILY (lface)))
2098 Lisp_Object family = AREF (font_object, FONT_FAMILY_INDEX);
2100 ASET (lface, LFACE_FAMILY_INDEX, SYMBOL_NAME (family));
2103 if (force_p || UNSPECIFIEDP (LFACE_FOUNDRY (lface)))
2105 Lisp_Object foundry = AREF (font_object, FONT_FOUNDRY_INDEX);
2107 ASET (lface, LFACE_FOUNDRY_INDEX, SYMBOL_NAME (foundry));
2110 if (force_p || UNSPECIFIEDP (LFACE_HEIGHT (lface)))
2112 int pt = PIXEL_TO_POINT (font->pixel_size * 10, FRAME_RES_Y (f));
2114 eassert (pt > 0);
2115 ASET (lface, LFACE_HEIGHT_INDEX, make_number (pt));
2118 if (force_p || UNSPECIFIEDP (LFACE_WEIGHT (lface)))
2120 val = FONT_WEIGHT_FOR_FACE (font_object);
2121 ASET (lface, LFACE_WEIGHT_INDEX, ! NILP (val) ? val :Qnormal);
2123 if (force_p || UNSPECIFIEDP (LFACE_SLANT (lface)))
2125 val = FONT_SLANT_FOR_FACE (font_object);
2126 ASET (lface, LFACE_SLANT_INDEX, ! NILP (val) ? val : Qnormal);
2128 if (force_p || UNSPECIFIEDP (LFACE_SWIDTH (lface)))
2130 val = FONT_WIDTH_FOR_FACE (font_object);
2131 ASET (lface, LFACE_SWIDTH_INDEX, ! NILP (val) ? val : Qnormal);
2134 ASET (lface, LFACE_FONT_INDEX, font_object);
2135 return 1;
2138 #endif /* HAVE_WINDOW_SYSTEM */
2141 /* Merges the face height FROM with the face height TO, and returns the
2142 merged height. If FROM is an invalid height, then INVALID is
2143 returned instead. FROM and TO may be either absolute face heights or
2144 `relative' heights; the returned value is always an absolute height
2145 unless both FROM and TO are relative. */
2147 static Lisp_Object
2148 merge_face_heights (Lisp_Object from, Lisp_Object to, Lisp_Object invalid)
2150 Lisp_Object result = invalid;
2152 if (INTEGERP (from))
2153 /* FROM is absolute, just use it as is. */
2154 result = from;
2155 else if (FLOATP (from))
2156 /* FROM is a scale, use it to adjust TO. */
2158 if (INTEGERP (to))
2159 /* relative X absolute => absolute */
2160 result = make_number (XFLOAT_DATA (from) * XINT (to));
2161 else if (FLOATP (to))
2162 /* relative X relative => relative */
2163 result = make_float (XFLOAT_DATA (from) * XFLOAT_DATA (to));
2164 else if (UNSPECIFIEDP (to))
2165 result = from;
2167 else if (FUNCTIONP (from))
2168 /* FROM is a function, which use to adjust TO. */
2170 /* Call function with current height as argument.
2171 From is the new height. */
2172 result = safe_call1 (from, to);
2174 /* Ensure that if TO was absolute, so is the result. */
2175 if (INTEGERP (to) && !INTEGERP (result))
2176 result = invalid;
2179 return result;
2183 /* Merge two Lisp face attribute vectors on frame F, FROM and TO, and
2184 store the resulting attributes in TO, which must be already be
2185 completely specified and contain only absolute attributes. Every
2186 specified attribute of FROM overrides the corresponding attribute of
2187 TO; relative attributes in FROM are merged with the absolute value in
2188 TO and replace it. NAMED_MERGE_POINTS is used internally to detect
2189 loops in face inheritance/remapping; it should be 0 when called from
2190 other places. */
2192 static void
2193 merge_face_vectors (struct frame *f, Lisp_Object *from, Lisp_Object *to,
2194 struct named_merge_point *named_merge_points)
2196 int i;
2197 Lisp_Object font = Qnil;
2199 /* If FROM inherits from some other faces, merge their attributes into
2200 TO before merging FROM's direct attributes. Note that an :inherit
2201 attribute of `unspecified' is the same as one of nil; we never
2202 merge :inherit attributes, so nil is more correct, but lots of
2203 other code uses `unspecified' as a generic value for face attributes. */
2204 if (!UNSPECIFIEDP (from[LFACE_INHERIT_INDEX])
2205 && !NILP (from[LFACE_INHERIT_INDEX]))
2206 merge_face_ref (f, from[LFACE_INHERIT_INDEX], to, 0, named_merge_points);
2208 if (FONT_SPEC_P (from[LFACE_FONT_INDEX]))
2210 if (!UNSPECIFIEDP (to[LFACE_FONT_INDEX]))
2211 font = merge_font_spec (from[LFACE_FONT_INDEX], to[LFACE_FONT_INDEX]);
2212 else
2213 font = copy_font_spec (from[LFACE_FONT_INDEX]);
2214 to[LFACE_FONT_INDEX] = font;
2217 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2218 if (!UNSPECIFIEDP (from[i]))
2220 if (i == LFACE_HEIGHT_INDEX && !INTEGERP (from[i]))
2222 to[i] = merge_face_heights (from[i], to[i], to[i]);
2223 font_clear_prop (to, FONT_SIZE_INDEX);
2225 else if (i != LFACE_FONT_INDEX && ! EQ (to[i], from[i]))
2227 to[i] = from[i];
2228 if (i >= LFACE_FAMILY_INDEX && i <=LFACE_SLANT_INDEX)
2229 font_clear_prop (to,
2230 (i == LFACE_FAMILY_INDEX ? FONT_FAMILY_INDEX
2231 : i == LFACE_FOUNDRY_INDEX ? FONT_FOUNDRY_INDEX
2232 : i == LFACE_SWIDTH_INDEX ? FONT_WIDTH_INDEX
2233 : i == LFACE_HEIGHT_INDEX ? FONT_SIZE_INDEX
2234 : i == LFACE_WEIGHT_INDEX ? FONT_WEIGHT_INDEX
2235 : FONT_SLANT_INDEX));
2239 /* If FROM specifies a font spec, make its contents take precedence
2240 over :family and other attributes. This is needed for face
2241 remapping using :font to work. */
2243 if (!NILP (font))
2245 if (! NILP (AREF (font, FONT_FOUNDRY_INDEX)))
2246 to[LFACE_FOUNDRY_INDEX] = SYMBOL_NAME (AREF (font, FONT_FOUNDRY_INDEX));
2247 if (! NILP (AREF (font, FONT_FAMILY_INDEX)))
2248 to[LFACE_FAMILY_INDEX] = SYMBOL_NAME (AREF (font, FONT_FAMILY_INDEX));
2249 if (! NILP (AREF (font, FONT_WEIGHT_INDEX)))
2250 to[LFACE_WEIGHT_INDEX] = FONT_WEIGHT_FOR_FACE (font);
2251 if (! NILP (AREF (font, FONT_SLANT_INDEX)))
2252 to[LFACE_SLANT_INDEX] = FONT_SLANT_FOR_FACE (font);
2253 if (! NILP (AREF (font, FONT_WIDTH_INDEX)))
2254 to[LFACE_SWIDTH_INDEX] = FONT_WIDTH_FOR_FACE (font);
2255 ASET (font, FONT_SIZE_INDEX, Qnil);
2258 /* TO is always an absolute face, which should inherit from nothing.
2259 We blindly copy the :inherit attribute above and fix it up here. */
2260 to[LFACE_INHERIT_INDEX] = Qnil;
2263 /* Merge the named face FACE_NAME on frame F, into the vector of face
2264 attributes TO. NAMED_MERGE_POINTS is used to detect loops in face
2265 inheritance. Returns true if FACE_NAME is a valid face name and
2266 merging succeeded. */
2268 static int
2269 merge_named_face (struct frame *f, Lisp_Object face_name, Lisp_Object *to,
2270 struct named_merge_point *named_merge_points)
2272 struct named_merge_point named_merge_point;
2274 if (push_named_merge_point (&named_merge_point,
2275 face_name, NAMED_MERGE_POINT_NORMAL,
2276 &named_merge_points))
2278 struct gcpro gcpro1;
2279 Lisp_Object from[LFACE_VECTOR_SIZE];
2280 int ok = get_lface_attributes (f, face_name, from, 0, named_merge_points);
2282 if (ok)
2284 GCPRO1 (named_merge_point.face_name);
2285 merge_face_vectors (f, from, to, named_merge_points);
2286 UNGCPRO;
2289 return ok;
2291 else
2292 return 0;
2296 /* Merge face attributes from the lisp `face reference' FACE_REF on
2297 frame F into the face attribute vector TO. If ERR_MSGS is non-zero,
2298 problems with FACE_REF cause an error message to be shown. Return
2299 non-zero if no errors occurred (regardless of the value of ERR_MSGS).
2300 NAMED_MERGE_POINTS is used to detect loops in face inheritance or
2301 list structure; it may be 0 for most callers.
2303 FACE_REF may be a single face specification or a list of such
2304 specifications. Each face specification can be:
2306 1. A symbol or string naming a Lisp face.
2308 2. A property list of the form (KEYWORD VALUE ...) where each
2309 KEYWORD is a face attribute name, and value is an appropriate value
2310 for that attribute.
2312 3. Conses or the form (FOREGROUND-COLOR . COLOR) or
2313 (BACKGROUND-COLOR . COLOR) where COLOR is a color name. This is
2314 for compatibility with 20.2.
2316 Face specifications earlier in lists take precedence over later
2317 specifications. */
2319 static int
2320 merge_face_ref (struct frame *f, Lisp_Object face_ref, Lisp_Object *to,
2321 int err_msgs, struct named_merge_point *named_merge_points)
2323 int ok = 1; /* Succeed without an error? */
2325 if (CONSP (face_ref))
2327 Lisp_Object first = XCAR (face_ref);
2329 if (EQ (first, Qforeground_color)
2330 || EQ (first, Qbackground_color))
2332 /* One of (FOREGROUND-COLOR . COLOR) or (BACKGROUND-COLOR
2333 . COLOR). COLOR must be a string. */
2334 Lisp_Object color_name = XCDR (face_ref);
2335 Lisp_Object color = first;
2337 if (STRINGP (color_name))
2339 if (EQ (color, Qforeground_color))
2340 to[LFACE_FOREGROUND_INDEX] = color_name;
2341 else
2342 to[LFACE_BACKGROUND_INDEX] = color_name;
2344 else
2346 if (err_msgs)
2347 add_to_log ("Invalid face color", color_name, Qnil);
2348 ok = 0;
2351 else if (SYMBOLP (first)
2352 && *SDATA (SYMBOL_NAME (first)) == ':')
2354 /* Assume this is the property list form. */
2355 while (CONSP (face_ref) && CONSP (XCDR (face_ref)))
2357 Lisp_Object keyword = XCAR (face_ref);
2358 Lisp_Object value = XCAR (XCDR (face_ref));
2359 int err = 0;
2361 /* Specifying `unspecified' is a no-op. */
2362 if (EQ (value, Qunspecified))
2364 else if (EQ (keyword, QCfamily))
2366 if (STRINGP (value))
2368 to[LFACE_FAMILY_INDEX] = value;
2369 font_clear_prop (to, FONT_FAMILY_INDEX);
2371 else
2372 err = 1;
2374 else if (EQ (keyword, QCfoundry))
2376 if (STRINGP (value))
2378 to[LFACE_FOUNDRY_INDEX] = value;
2379 font_clear_prop (to, FONT_FOUNDRY_INDEX);
2381 else
2382 err = 1;
2384 else if (EQ (keyword, QCheight))
2386 Lisp_Object new_height =
2387 merge_face_heights (value, to[LFACE_HEIGHT_INDEX], Qnil);
2389 if (! NILP (new_height))
2391 to[LFACE_HEIGHT_INDEX] = new_height;
2392 font_clear_prop (to, FONT_SIZE_INDEX);
2394 else
2395 err = 1;
2397 else if (EQ (keyword, QCweight))
2399 if (SYMBOLP (value) && FONT_WEIGHT_NAME_NUMERIC (value) >= 0)
2401 to[LFACE_WEIGHT_INDEX] = value;
2402 font_clear_prop (to, FONT_WEIGHT_INDEX);
2404 else
2405 err = 1;
2407 else if (EQ (keyword, QCslant))
2409 if (SYMBOLP (value) && FONT_SLANT_NAME_NUMERIC (value) >= 0)
2411 to[LFACE_SLANT_INDEX] = value;
2412 font_clear_prop (to, FONT_SLANT_INDEX);
2414 else
2415 err = 1;
2417 else if (EQ (keyword, QCunderline))
2419 if (EQ (value, Qt)
2420 || NILP (value)
2421 || STRINGP (value)
2422 || CONSP (value))
2423 to[LFACE_UNDERLINE_INDEX] = value;
2424 else
2425 err = 1;
2427 else if (EQ (keyword, QCoverline))
2429 if (EQ (value, Qt)
2430 || NILP (value)
2431 || STRINGP (value))
2432 to[LFACE_OVERLINE_INDEX] = value;
2433 else
2434 err = 1;
2436 else if (EQ (keyword, QCstrike_through))
2438 if (EQ (value, Qt)
2439 || NILP (value)
2440 || STRINGP (value))
2441 to[LFACE_STRIKE_THROUGH_INDEX] = value;
2442 else
2443 err = 1;
2445 else if (EQ (keyword, QCbox))
2447 if (EQ (value, Qt))
2448 value = make_number (1);
2449 if (INTEGERP (value)
2450 || STRINGP (value)
2451 || CONSP (value)
2452 || NILP (value))
2453 to[LFACE_BOX_INDEX] = value;
2454 else
2455 err = 1;
2457 else if (EQ (keyword, QCinverse_video)
2458 || EQ (keyword, QCreverse_video))
2460 if (EQ (value, Qt) || NILP (value))
2461 to[LFACE_INVERSE_INDEX] = value;
2462 else
2463 err = 1;
2465 else if (EQ (keyword, QCforeground))
2467 if (STRINGP (value))
2468 to[LFACE_FOREGROUND_INDEX] = value;
2469 else
2470 err = 1;
2472 else if (EQ (keyword, QCdistant_foreground))
2474 if (STRINGP (value))
2475 to[LFACE_DISTANT_FOREGROUND_INDEX] = value;
2476 else
2477 err = 1;
2479 else if (EQ (keyword, QCbackground))
2481 if (STRINGP (value))
2482 to[LFACE_BACKGROUND_INDEX] = value;
2483 else
2484 err = 1;
2486 else if (EQ (keyword, QCstipple))
2488 #if defined (HAVE_WINDOW_SYSTEM)
2489 Lisp_Object pixmap_p = Fbitmap_spec_p (value);
2490 if (!NILP (pixmap_p))
2491 to[LFACE_STIPPLE_INDEX] = value;
2492 else
2493 err = 1;
2494 #endif /* HAVE_WINDOW_SYSTEM */
2496 else if (EQ (keyword, QCwidth))
2498 if (SYMBOLP (value) && FONT_WIDTH_NAME_NUMERIC (value) >= 0)
2500 to[LFACE_SWIDTH_INDEX] = value;
2501 font_clear_prop (to, FONT_WIDTH_INDEX);
2503 else
2504 err = 1;
2506 else if (EQ (keyword, QCfont))
2508 if (FONTP (value))
2509 to[LFACE_FONT_INDEX] = value;
2510 else
2511 err = 1;
2513 else if (EQ (keyword, QCinherit))
2515 /* This is not really very useful; it's just like a
2516 normal face reference. */
2517 if (! merge_face_ref (f, value, to,
2518 err_msgs, named_merge_points))
2519 err = 1;
2521 else
2522 err = 1;
2524 if (err)
2526 add_to_log ("Invalid face attribute %S %S", keyword, value);
2527 ok = 0;
2530 face_ref = XCDR (XCDR (face_ref));
2533 else
2535 /* This is a list of face refs. Those at the beginning of the
2536 list take precedence over what follows, so we have to merge
2537 from the end backwards. */
2538 Lisp_Object next = XCDR (face_ref);
2540 if (! NILP (next))
2541 ok = merge_face_ref (f, next, to, err_msgs, named_merge_points);
2543 if (! merge_face_ref (f, first, to, err_msgs, named_merge_points))
2544 ok = 0;
2547 else
2549 /* FACE_REF ought to be a face name. */
2550 ok = merge_named_face (f, face_ref, to, named_merge_points);
2551 if (!ok && err_msgs)
2552 add_to_log ("Invalid face reference: %s", face_ref, Qnil);
2555 return ok;
2559 DEFUN ("internal-make-lisp-face", Finternal_make_lisp_face,
2560 Sinternal_make_lisp_face, 1, 2, 0,
2561 doc: /* Make FACE, a symbol, a Lisp face with all attributes nil.
2562 If FACE was not known as a face before, create a new one.
2563 If optional argument FRAME is specified, make a frame-local face
2564 for that frame. Otherwise operate on the global face definition.
2565 Value is a vector of face attributes. */)
2566 (Lisp_Object face, Lisp_Object frame)
2568 Lisp_Object global_lface, lface;
2569 struct frame *f;
2570 int i;
2572 CHECK_SYMBOL (face);
2573 global_lface = lface_from_face_name (NULL, face, 0);
2575 if (!NILP (frame))
2577 CHECK_LIVE_FRAME (frame);
2578 f = XFRAME (frame);
2579 lface = lface_from_face_name (f, face, 0);
2581 else
2582 f = NULL, lface = Qnil;
2584 /* Add a global definition if there is none. */
2585 if (NILP (global_lface))
2587 global_lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
2588 Qunspecified);
2589 ASET (global_lface, 0, Qface);
2590 Vface_new_frame_defaults = Fcons (Fcons (face, global_lface),
2591 Vface_new_frame_defaults);
2593 /* Assign the new Lisp face a unique ID. The mapping from Lisp
2594 face id to Lisp face is given by the vector lface_id_to_name.
2595 The mapping from Lisp face to Lisp face id is given by the
2596 property `face' of the Lisp face name. */
2597 if (next_lface_id == lface_id_to_name_size)
2598 lface_id_to_name =
2599 xpalloc (lface_id_to_name, &lface_id_to_name_size, 1, MAX_FACE_ID,
2600 sizeof *lface_id_to_name);
2602 lface_id_to_name[next_lface_id] = face;
2603 Fput (face, Qface, make_number (next_lface_id));
2604 ++next_lface_id;
2606 else if (f == NULL)
2607 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2608 ASET (global_lface, i, Qunspecified);
2610 /* Add a frame-local definition. */
2611 if (f)
2613 if (NILP (lface))
2615 lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
2616 Qunspecified);
2617 ASET (lface, 0, Qface);
2618 fset_face_alist (f, Fcons (Fcons (face, lface), f->face_alist));
2620 else
2621 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2622 ASET (lface, i, Qunspecified);
2624 else
2625 lface = global_lface;
2627 /* Changing a named face means that all realized faces depending on
2628 that face are invalid. Since we cannot tell which realized faces
2629 depend on the face, make sure they are all removed. This is done
2630 by incrementing face_change_count. The next call to
2631 init_iterator will then free realized faces. */
2632 if (NILP (Fget (face, Qface_no_inherit)))
2634 ++face_change_count;
2635 windows_or_buffers_changed = 54;
2638 eassert (LFACEP (lface));
2639 check_lface (lface);
2640 return lface;
2644 DEFUN ("internal-lisp-face-p", Finternal_lisp_face_p,
2645 Sinternal_lisp_face_p, 1, 2, 0,
2646 doc: /* Return non-nil if FACE names a face.
2647 FACE should be a symbol or string.
2648 If optional second argument FRAME is non-nil, check for the
2649 existence of a frame-local face with name FACE on that frame.
2650 Otherwise check for the existence of a global face. */)
2651 (Lisp_Object face, Lisp_Object frame)
2653 Lisp_Object lface;
2655 face = resolve_face_name (face, 1);
2657 if (!NILP (frame))
2659 CHECK_LIVE_FRAME (frame);
2660 lface = lface_from_face_name (XFRAME (frame), face, 0);
2662 else
2663 lface = lface_from_face_name (NULL, face, 0);
2665 return lface;
2669 DEFUN ("internal-copy-lisp-face", Finternal_copy_lisp_face,
2670 Sinternal_copy_lisp_face, 4, 4, 0,
2671 doc: /* Copy face FROM to TO.
2672 If FRAME is t, copy the global face definition of FROM.
2673 Otherwise, copy the frame-local definition of FROM on FRAME.
2674 If NEW-FRAME is a frame, copy that data into the frame-local
2675 definition of TO on NEW-FRAME. If NEW-FRAME is nil,
2676 FRAME controls where the data is copied to.
2678 The value is TO. */)
2679 (Lisp_Object from, Lisp_Object to, Lisp_Object frame, Lisp_Object new_frame)
2681 Lisp_Object lface, copy;
2683 CHECK_SYMBOL (from);
2684 CHECK_SYMBOL (to);
2686 if (EQ (frame, Qt))
2688 /* Copy global definition of FROM. We don't make copies of
2689 strings etc. because 20.2 didn't do it either. */
2690 lface = lface_from_face_name (NULL, from, 1);
2691 copy = Finternal_make_lisp_face (to, Qnil);
2693 else
2695 /* Copy frame-local definition of FROM. */
2696 if (NILP (new_frame))
2697 new_frame = frame;
2698 CHECK_LIVE_FRAME (frame);
2699 CHECK_LIVE_FRAME (new_frame);
2700 lface = lface_from_face_name (XFRAME (frame), from, 1);
2701 copy = Finternal_make_lisp_face (to, new_frame);
2704 vcopy (copy, 0, XVECTOR (lface)->contents, LFACE_VECTOR_SIZE);
2706 /* Changing a named face means that all realized faces depending on
2707 that face are invalid. Since we cannot tell which realized faces
2708 depend on the face, make sure they are all removed. This is done
2709 by incrementing face_change_count. The next call to
2710 init_iterator will then free realized faces. */
2711 if (NILP (Fget (to, Qface_no_inherit)))
2713 ++face_change_count;
2714 windows_or_buffers_changed = 55;
2717 return to;
2721 DEFUN ("internal-set-lisp-face-attribute", Finternal_set_lisp_face_attribute,
2722 Sinternal_set_lisp_face_attribute, 3, 4, 0,
2723 doc: /* Set attribute ATTR of FACE to VALUE.
2724 FRAME being a frame means change the face on that frame.
2725 FRAME nil means change the face of the selected frame.
2726 FRAME t means change the default for new frames.
2727 FRAME 0 means change the face on all frames, and change the default
2728 for new frames. */)
2729 (Lisp_Object face, Lisp_Object attr, Lisp_Object value, Lisp_Object frame)
2731 Lisp_Object lface;
2732 Lisp_Object old_value = Qnil;
2733 /* Set one of enum font_property_index (> 0) if ATTR is one of
2734 font-related attributes other than QCfont and QCfontset. */
2735 enum font_property_index prop_index = 0;
2737 CHECK_SYMBOL (face);
2738 CHECK_SYMBOL (attr);
2740 face = resolve_face_name (face, 1);
2742 /* If FRAME is 0, change face on all frames, and change the
2743 default for new frames. */
2744 if (INTEGERP (frame) && XINT (frame) == 0)
2746 Lisp_Object tail;
2747 Finternal_set_lisp_face_attribute (face, attr, value, Qt);
2748 FOR_EACH_FRAME (tail, frame)
2749 Finternal_set_lisp_face_attribute (face, attr, value, frame);
2750 return face;
2753 /* Set lface to the Lisp attribute vector of FACE. */
2754 if (EQ (frame, Qt))
2756 lface = lface_from_face_name (NULL, face, 1);
2758 /* When updating face-new-frame-defaults, we put :ignore-defface
2759 where the caller wants `unspecified'. This forces the frame
2760 defaults to ignore the defface value. Otherwise, the defface
2761 will take effect, which is generally not what is intended.
2762 The value of that attribute will be inherited from some other
2763 face during face merging. See internal_merge_in_global_face. */
2764 if (UNSPECIFIEDP (value))
2765 value = QCignore_defface;
2767 else
2769 if (NILP (frame))
2770 frame = selected_frame;
2772 CHECK_LIVE_FRAME (frame);
2773 lface = lface_from_face_name (XFRAME (frame), face, 0);
2775 /* If a frame-local face doesn't exist yet, create one. */
2776 if (NILP (lface))
2777 lface = Finternal_make_lisp_face (face, frame);
2780 if (EQ (attr, QCfamily))
2782 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2784 CHECK_STRING (value);
2785 if (SCHARS (value) == 0)
2786 signal_error ("Invalid face family", value);
2788 old_value = LFACE_FAMILY (lface);
2789 ASET (lface, LFACE_FAMILY_INDEX, value);
2790 prop_index = FONT_FAMILY_INDEX;
2792 else if (EQ (attr, QCfoundry))
2794 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2796 CHECK_STRING (value);
2797 if (SCHARS (value) == 0)
2798 signal_error ("Invalid face foundry", value);
2800 old_value = LFACE_FOUNDRY (lface);
2801 ASET (lface, LFACE_FOUNDRY_INDEX, value);
2802 prop_index = FONT_FOUNDRY_INDEX;
2804 else if (EQ (attr, QCheight))
2806 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2808 if (EQ (face, Qdefault))
2810 /* The default face must have an absolute size. */
2811 if (!INTEGERP (value) || XINT (value) <= 0)
2812 signal_error ("Default face height not absolute and positive",
2813 value);
2815 else
2817 /* For non-default faces, do a test merge with a random
2818 height to see if VALUE's ok. */
2819 Lisp_Object test = merge_face_heights (value,
2820 make_number (10),
2821 Qnil);
2822 if (!INTEGERP (test) || XINT (test) <= 0)
2823 signal_error ("Face height does not produce a positive integer",
2824 value);
2828 old_value = LFACE_HEIGHT (lface);
2829 ASET (lface, LFACE_HEIGHT_INDEX, value);
2830 prop_index = FONT_SIZE_INDEX;
2832 else if (EQ (attr, QCweight))
2834 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2836 CHECK_SYMBOL (value);
2837 if (FONT_WEIGHT_NAME_NUMERIC (value) < 0)
2838 signal_error ("Invalid face weight", value);
2840 old_value = LFACE_WEIGHT (lface);
2841 ASET (lface, LFACE_WEIGHT_INDEX, value);
2842 prop_index = FONT_WEIGHT_INDEX;
2844 else if (EQ (attr, QCslant))
2846 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2848 CHECK_SYMBOL (value);
2849 if (FONT_SLANT_NAME_NUMERIC (value) < 0)
2850 signal_error ("Invalid face slant", value);
2852 old_value = LFACE_SLANT (lface);
2853 ASET (lface, LFACE_SLANT_INDEX, value);
2854 prop_index = FONT_SLANT_INDEX;
2856 else if (EQ (attr, QCunderline))
2858 bool valid_p = 0;
2860 if (UNSPECIFIEDP (value) || IGNORE_DEFFACE_P (value))
2861 valid_p = 1;
2862 else if (NILP (value) || EQ (value, Qt))
2863 valid_p = 1;
2864 else if (STRINGP (value) && SCHARS (value) > 0)
2865 valid_p = 1;
2866 else if (CONSP (value))
2868 Lisp_Object key, val, list;
2870 list = value;
2871 /* FIXME? This errs on the side of acceptance. Eg it accepts:
2872 (defface foo '((t :underline 'foo) "doc")
2873 Maybe this is intentional, maybe it isn't.
2874 Non-nil symbols other than t are not documented as being valid.
2875 Eg compare with inverse-video, which explicitly rejects them.
2877 valid_p = 1;
2879 while (!NILP (CAR_SAFE(list)))
2881 key = CAR_SAFE (list);
2882 list = CDR_SAFE (list);
2883 val = CAR_SAFE (list);
2884 list = CDR_SAFE (list);
2886 if (NILP (key) || NILP (val))
2888 valid_p = 0;
2889 break;
2892 else if (EQ (key, QCcolor)
2893 && !(EQ (val, Qforeground_color)
2894 || (STRINGP (val) && SCHARS (val) > 0)))
2896 valid_p = 0;
2897 break;
2900 else if (EQ (key, QCstyle)
2901 && !(EQ (val, Qline) || EQ (val, Qwave)))
2903 valid_p = 0;
2904 break;
2909 if (!valid_p)
2910 signal_error ("Invalid face underline", value);
2912 old_value = LFACE_UNDERLINE (lface);
2913 ASET (lface, LFACE_UNDERLINE_INDEX, value);
2915 else if (EQ (attr, QCoverline))
2917 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2918 if ((SYMBOLP (value)
2919 && !EQ (value, Qt)
2920 && !EQ (value, Qnil))
2921 /* Overline color. */
2922 || (STRINGP (value)
2923 && SCHARS (value) == 0))
2924 signal_error ("Invalid face overline", value);
2926 old_value = LFACE_OVERLINE (lface);
2927 ASET (lface, LFACE_OVERLINE_INDEX, value);
2929 else if (EQ (attr, QCstrike_through))
2931 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2932 if ((SYMBOLP (value)
2933 && !EQ (value, Qt)
2934 && !EQ (value, Qnil))
2935 /* Strike-through color. */
2936 || (STRINGP (value)
2937 && SCHARS (value) == 0))
2938 signal_error ("Invalid face strike-through", value);
2940 old_value = LFACE_STRIKE_THROUGH (lface);
2941 ASET (lface, LFACE_STRIKE_THROUGH_INDEX, value);
2943 else if (EQ (attr, QCbox))
2945 bool valid_p;
2947 /* Allow t meaning a simple box of width 1 in foreground color
2948 of the face. */
2949 if (EQ (value, Qt))
2950 value = make_number (1);
2952 if (UNSPECIFIEDP (value) || IGNORE_DEFFACE_P (value))
2953 valid_p = 1;
2954 else if (NILP (value))
2955 valid_p = 1;
2956 else if (INTEGERP (value))
2957 valid_p = XINT (value) != 0;
2958 else if (STRINGP (value))
2959 valid_p = SCHARS (value) > 0;
2960 else if (CONSP (value))
2962 Lisp_Object tem;
2964 tem = value;
2965 while (CONSP (tem))
2967 Lisp_Object k, v;
2969 k = XCAR (tem);
2970 tem = XCDR (tem);
2971 if (!CONSP (tem))
2972 break;
2973 v = XCAR (tem);
2974 tem = XCDR (tem);
2976 if (EQ (k, QCline_width))
2978 if (!INTEGERP (v) || XINT (v) == 0)
2979 break;
2981 else if (EQ (k, QCcolor))
2983 if (!NILP (v) && (!STRINGP (v) || SCHARS (v) == 0))
2984 break;
2986 else if (EQ (k, QCstyle))
2988 if (!EQ (v, Qpressed_button) && !EQ (v, Qreleased_button))
2989 break;
2991 else
2992 break;
2995 valid_p = NILP (tem);
2997 else
2998 valid_p = 0;
3000 if (!valid_p)
3001 signal_error ("Invalid face box", value);
3003 old_value = LFACE_BOX (lface);
3004 ASET (lface, LFACE_BOX_INDEX, value);
3006 else if (EQ (attr, QCinverse_video)
3007 || EQ (attr, QCreverse_video))
3009 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3011 CHECK_SYMBOL (value);
3012 if (!EQ (value, Qt) && !NILP (value))
3013 signal_error ("Invalid inverse-video face attribute value", value);
3015 old_value = LFACE_INVERSE (lface);
3016 ASET (lface, LFACE_INVERSE_INDEX, value);
3018 else if (EQ (attr, QCforeground))
3020 /* Compatibility with 20.x. */
3021 if (NILP (value))
3022 value = Qunspecified;
3023 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3025 /* Don't check for valid color names here because it depends
3026 on the frame (display) whether the color will be valid
3027 when the face is realized. */
3028 CHECK_STRING (value);
3029 if (SCHARS (value) == 0)
3030 signal_error ("Empty foreground color value", value);
3032 old_value = LFACE_FOREGROUND (lface);
3033 ASET (lface, LFACE_FOREGROUND_INDEX, value);
3035 else if (EQ (attr, QCdistant_foreground))
3037 /* Compatibility with 20.x. */
3038 if (NILP (value))
3039 value = Qunspecified;
3040 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3042 /* Don't check for valid color names here because it depends
3043 on the frame (display) whether the color will be valid
3044 when the face is realized. */
3045 CHECK_STRING (value);
3046 if (SCHARS (value) == 0)
3047 signal_error ("Empty distant-foreground color value", value);
3049 old_value = LFACE_DISTANT_FOREGROUND (lface);
3050 ASET (lface, LFACE_DISTANT_FOREGROUND_INDEX, value);
3052 else if (EQ (attr, QCbackground))
3054 /* Compatibility with 20.x. */
3055 if (NILP (value))
3056 value = Qunspecified;
3057 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3059 /* Don't check for valid color names here because it depends
3060 on the frame (display) whether the color will be valid
3061 when the face is realized. */
3062 CHECK_STRING (value);
3063 if (SCHARS (value) == 0)
3064 signal_error ("Empty background color value", value);
3066 old_value = LFACE_BACKGROUND (lface);
3067 ASET (lface, LFACE_BACKGROUND_INDEX, value);
3069 else if (EQ (attr, QCstipple))
3071 #if defined (HAVE_WINDOW_SYSTEM)
3072 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value)
3073 && !NILP (value)
3074 && NILP (Fbitmap_spec_p (value)))
3075 signal_error ("Invalid stipple attribute", value);
3076 old_value = LFACE_STIPPLE (lface);
3077 ASET (lface, LFACE_STIPPLE_INDEX, value);
3078 #endif /* HAVE_WINDOW_SYSTEM */
3080 else if (EQ (attr, QCwidth))
3082 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3084 CHECK_SYMBOL (value);
3085 if (FONT_WIDTH_NAME_NUMERIC (value) < 0)
3086 signal_error ("Invalid face width", value);
3088 old_value = LFACE_SWIDTH (lface);
3089 ASET (lface, LFACE_SWIDTH_INDEX, value);
3090 prop_index = FONT_WIDTH_INDEX;
3092 else if (EQ (attr, QCfont))
3094 #ifdef HAVE_WINDOW_SYSTEM
3095 if (EQ (frame, Qt) || FRAME_WINDOW_P (XFRAME (frame)))
3097 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3099 struct frame *f;
3101 old_value = LFACE_FONT (lface);
3102 if (! FONTP (value))
3104 if (STRINGP (value))
3106 Lisp_Object name = value;
3107 int fontset = fs_query_fontset (name, 0);
3109 if (fontset >= 0)
3110 name = fontset_ascii (fontset);
3111 value = font_spec_from_name (name);
3112 if (!FONTP (value))
3113 signal_error ("Invalid font name", name);
3115 else
3116 signal_error ("Invalid font or font-spec", value);
3118 if (EQ (frame, Qt))
3119 f = XFRAME (selected_frame);
3120 else
3121 f = XFRAME (frame);
3123 /* FIXME:
3124 If frame is t, and selected frame is a tty frame, the font
3125 can't be realized. An improvement would be to loop over frames
3126 for a non-tty frame and use that. See discussion in
3127 bug#18573.
3129 if (f->terminal->type != output_termcap)
3131 if (! FONT_OBJECT_P (value))
3133 Lisp_Object *attrs = XVECTOR (lface)->contents;
3134 Lisp_Object font_object;
3136 font_object = font_load_for_lface (f, attrs, value);
3137 if (NILP (font_object))
3138 signal_error ("Font not available", value);
3139 value = font_object;
3141 set_lface_from_font (f, lface, value, 1);
3144 else
3145 ASET (lface, LFACE_FONT_INDEX, value);
3147 #endif /* HAVE_WINDOW_SYSTEM */
3149 else if (EQ (attr, QCfontset))
3151 #ifdef HAVE_WINDOW_SYSTEM
3152 if (EQ (frame, Qt) || FRAME_WINDOW_P (XFRAME (frame)))
3154 Lisp_Object tmp;
3156 old_value = LFACE_FONTSET (lface);
3157 tmp = Fquery_fontset (value, Qnil);
3158 if (NILP (tmp))
3159 signal_error ("Invalid fontset name", value);
3160 ASET (lface, LFACE_FONTSET_INDEX, value = tmp);
3162 #endif /* HAVE_WINDOW_SYSTEM */
3164 else if (EQ (attr, QCinherit))
3166 Lisp_Object tail;
3167 if (SYMBOLP (value))
3168 tail = Qnil;
3169 else
3170 for (tail = value; CONSP (tail); tail = XCDR (tail))
3171 if (!SYMBOLP (XCAR (tail)))
3172 break;
3173 if (NILP (tail))
3174 ASET (lface, LFACE_INHERIT_INDEX, value);
3175 else
3176 signal_error ("Invalid face inheritance", value);
3178 else if (EQ (attr, QCbold))
3180 old_value = LFACE_WEIGHT (lface);
3181 ASET (lface, LFACE_WEIGHT_INDEX, NILP (value) ? Qnormal : Qbold);
3182 prop_index = FONT_WEIGHT_INDEX;
3184 else if (EQ (attr, QCitalic))
3186 attr = QCslant;
3187 old_value = LFACE_SLANT (lface);
3188 ASET (lface, LFACE_SLANT_INDEX, NILP (value) ? Qnormal : Qitalic);
3189 prop_index = FONT_SLANT_INDEX;
3191 else
3192 signal_error ("Invalid face attribute name", attr);
3194 if (prop_index)
3196 /* If a font-related attribute other than QCfont and QCfontset
3197 is specified, and if the original QCfont attribute has a font
3198 (font-spec or font-object), set the corresponding property in
3199 the font to nil so that the font selector doesn't think that
3200 the attribute is mandatory. Also, clear the average
3201 width. */
3202 font_clear_prop (XVECTOR (lface)->contents, prop_index);
3205 /* Changing a named face means that all realized faces depending on
3206 that face are invalid. Since we cannot tell which realized faces
3207 depend on the face, make sure they are all removed. This is done
3208 by incrementing face_change_count. The next call to
3209 init_iterator will then free realized faces. */
3210 if (!EQ (frame, Qt)
3211 && NILP (Fget (face, Qface_no_inherit))
3212 && NILP (Fequal (old_value, value)))
3214 ++face_change_count;
3215 windows_or_buffers_changed = 56;
3218 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value)
3219 && NILP (Fequal (old_value, value)))
3221 Lisp_Object param;
3223 param = Qnil;
3225 if (EQ (face, Qdefault))
3227 #ifdef HAVE_WINDOW_SYSTEM
3228 /* Changed font-related attributes of the `default' face are
3229 reflected in changed `font' frame parameters. */
3230 if (FRAMEP (frame)
3231 && (prop_index || EQ (attr, QCfont))
3232 && lface_fully_specified_p (XVECTOR (lface)->contents))
3233 set_font_frame_param (frame, lface);
3234 else
3235 #endif /* HAVE_WINDOW_SYSTEM */
3237 if (EQ (attr, QCforeground))
3238 param = Qforeground_color;
3239 else if (EQ (attr, QCbackground))
3240 param = Qbackground_color;
3242 #ifdef HAVE_WINDOW_SYSTEM
3243 #ifndef HAVE_NTGUI
3244 else if (EQ (face, Qscroll_bar))
3246 /* Changing the colors of `scroll-bar' sets frame parameters
3247 `scroll-bar-foreground' and `scroll-bar-background'. */
3248 if (EQ (attr, QCforeground))
3249 param = Qscroll_bar_foreground;
3250 else if (EQ (attr, QCbackground))
3251 param = Qscroll_bar_background;
3253 #endif /* not HAVE_NTGUI */
3254 else if (EQ (face, Qborder))
3256 /* Changing background color of `border' sets frame parameter
3257 `border-color'. */
3258 if (EQ (attr, QCbackground))
3259 param = Qborder_color;
3261 else if (EQ (face, Qcursor))
3263 /* Changing background color of `cursor' sets frame parameter
3264 `cursor-color'. */
3265 if (EQ (attr, QCbackground))
3266 param = Qcursor_color;
3268 else if (EQ (face, Qmouse))
3270 /* Changing background color of `mouse' sets frame parameter
3271 `mouse-color'. */
3272 if (EQ (attr, QCbackground))
3273 param = Qmouse_color;
3275 #endif /* HAVE_WINDOW_SYSTEM */
3276 else if (EQ (face, Qmenu))
3278 /* Indicate that we have to update the menu bar when realizing
3279 faces on FRAME. FRAME t change the default for new frames.
3280 We do this by setting the flag in new face caches. */
3281 if (FRAMEP (frame))
3283 struct frame *f = XFRAME (frame);
3284 if (FRAME_FACE_CACHE (f) == NULL)
3285 FRAME_FACE_CACHE (f) = make_face_cache (f);
3286 FRAME_FACE_CACHE (f)->menu_face_changed_p = 1;
3288 else
3289 menu_face_changed_default = 1;
3292 if (!NILP (param))
3294 if (EQ (frame, Qt))
3295 /* Update `default-frame-alist', which is used for new frames. */
3297 store_in_alist (&Vdefault_frame_alist, param, value);
3299 else
3300 /* Update the current frame's parameters. */
3302 Lisp_Object cons;
3303 cons = XCAR (Vparam_value_alist);
3304 XSETCAR (cons, param);
3305 XSETCDR (cons, value);
3306 Fmodify_frame_parameters (frame, Vparam_value_alist);
3311 return face;
3315 /* Update the corresponding face when frame parameter PARAM on frame F
3316 has been assigned the value NEW_VALUE. */
3318 void
3319 update_face_from_frame_parameter (struct frame *f, Lisp_Object param,
3320 Lisp_Object new_value)
3322 Lisp_Object face = Qnil;
3323 Lisp_Object lface;
3325 /* If there are no faces yet, give up. This is the case when called
3326 from Fx_create_frame, and we do the necessary things later in
3327 face-set-after-frame-defaults. */
3328 if (NILP (f->face_alist))
3329 return;
3331 if (EQ (param, Qforeground_color))
3333 face = Qdefault;
3334 lface = lface_from_face_name (f, face, 1);
3335 ASET (lface, LFACE_FOREGROUND_INDEX,
3336 (STRINGP (new_value) ? new_value : Qunspecified));
3337 realize_basic_faces (f);
3339 else if (EQ (param, Qbackground_color))
3341 Lisp_Object frame;
3343 /* Changing the background color might change the background
3344 mode, so that we have to load new defface specs.
3345 Call frame-set-background-mode to do that. */
3346 XSETFRAME (frame, f);
3347 call1 (Qframe_set_background_mode, frame);
3349 face = Qdefault;
3350 lface = lface_from_face_name (f, face, 1);
3351 ASET (lface, LFACE_BACKGROUND_INDEX,
3352 (STRINGP (new_value) ? new_value : Qunspecified));
3353 realize_basic_faces (f);
3355 #ifdef HAVE_WINDOW_SYSTEM
3356 else if (EQ (param, Qborder_color))
3358 face = Qborder;
3359 lface = lface_from_face_name (f, face, 1);
3360 ASET (lface, LFACE_BACKGROUND_INDEX,
3361 (STRINGP (new_value) ? new_value : Qunspecified));
3363 else if (EQ (param, Qcursor_color))
3365 face = Qcursor;
3366 lface = lface_from_face_name (f, face, 1);
3367 ASET (lface, LFACE_BACKGROUND_INDEX,
3368 (STRINGP (new_value) ? new_value : Qunspecified));
3370 else if (EQ (param, Qmouse_color))
3372 face = Qmouse;
3373 lface = lface_from_face_name (f, face, 1);
3374 ASET (lface, LFACE_BACKGROUND_INDEX,
3375 (STRINGP (new_value) ? new_value : Qunspecified));
3377 #endif
3379 /* Changing a named face means that all realized faces depending on
3380 that face are invalid. Since we cannot tell which realized faces
3381 depend on the face, make sure they are all removed. This is done
3382 by incrementing face_change_count. The next call to
3383 init_iterator will then free realized faces. */
3384 if (!NILP (face)
3385 && NILP (Fget (face, Qface_no_inherit)))
3387 ++face_change_count;
3388 windows_or_buffers_changed = 57;
3393 #ifdef HAVE_WINDOW_SYSTEM
3395 /* Set the `font' frame parameter of FRAME determined from the
3396 font-object set in `default' face attributes LFACE. */
3398 static void
3399 set_font_frame_param (Lisp_Object frame, Lisp_Object lface)
3401 struct frame *f = XFRAME (frame);
3402 Lisp_Object font;
3404 if (FRAME_WINDOW_P (f)
3405 /* Don't do anything if the font is `unspecified'. This can
3406 happen during frame creation. */
3407 && (font = LFACE_FONT (lface),
3408 ! UNSPECIFIEDP (font)))
3410 if (FONT_SPEC_P (font))
3412 font = font_load_for_lface (f, XVECTOR (lface)->contents, font);
3413 if (NILP (font))
3414 return;
3415 ASET (lface, LFACE_FONT_INDEX, font);
3417 f->default_face_done_p = 0;
3418 Fmodify_frame_parameters (frame, list1 (Fcons (Qfont, font)));
3422 DEFUN ("internal-face-x-get-resource", Finternal_face_x_get_resource,
3423 Sinternal_face_x_get_resource, 2, 3, 0,
3424 doc: /* Get the value of X resource RESOURCE, class CLASS.
3425 Returned value is for the display of frame FRAME. If FRAME is not
3426 specified or nil, use selected frame. This function exists because
3427 ordinary `x-get-resource' doesn't take a frame argument. */)
3428 (Lisp_Object resource, Lisp_Object class, Lisp_Object frame)
3430 Lisp_Object value = Qnil;
3431 struct frame *f;
3433 CHECK_STRING (resource);
3434 CHECK_STRING (class);
3435 f = decode_live_frame (frame);
3436 block_input ();
3437 value = display_x_get_resource (FRAME_DISPLAY_INFO (f),
3438 resource, class, Qnil, Qnil);
3439 unblock_input ();
3440 return value;
3444 /* Return resource string VALUE as a boolean value, i.e. nil, or t.
3445 If VALUE is "on" or "true", return t. If VALUE is "off" or
3446 "false", return nil. Otherwise, if SIGNAL_P is non-zero, signal an
3447 error; if SIGNAL_P is zero, return 0. */
3449 static Lisp_Object
3450 face_boolean_x_resource_value (Lisp_Object value, int signal_p)
3452 Lisp_Object result = make_number (0);
3454 eassert (STRINGP (value));
3456 if (xstrcasecmp (SSDATA (value), "on") == 0
3457 || xstrcasecmp (SSDATA (value), "true") == 0)
3458 result = Qt;
3459 else if (xstrcasecmp (SSDATA (value), "off") == 0
3460 || xstrcasecmp (SSDATA (value), "false") == 0)
3461 result = Qnil;
3462 else if (xstrcasecmp (SSDATA (value), "unspecified") == 0)
3463 result = Qunspecified;
3464 else if (signal_p)
3465 signal_error ("Invalid face attribute value from X resource", value);
3467 return result;
3471 DEFUN ("internal-set-lisp-face-attribute-from-resource",
3472 Finternal_set_lisp_face_attribute_from_resource,
3473 Sinternal_set_lisp_face_attribute_from_resource,
3474 3, 4, 0, doc: /* */)
3475 (Lisp_Object face, Lisp_Object attr, Lisp_Object value, Lisp_Object frame)
3477 CHECK_SYMBOL (face);
3478 CHECK_SYMBOL (attr);
3479 CHECK_STRING (value);
3481 if (xstrcasecmp (SSDATA (value), "unspecified") == 0)
3482 value = Qunspecified;
3483 else if (EQ (attr, QCheight))
3485 value = Fstring_to_number (value, make_number (10));
3486 if (XINT (value) <= 0)
3487 signal_error ("Invalid face height from X resource", value);
3489 else if (EQ (attr, QCbold) || EQ (attr, QCitalic))
3490 value = face_boolean_x_resource_value (value, 1);
3491 else if (EQ (attr, QCweight) || EQ (attr, QCslant) || EQ (attr, QCwidth))
3492 value = intern (SSDATA (value));
3493 else if (EQ (attr, QCreverse_video) || EQ (attr, QCinverse_video))
3494 value = face_boolean_x_resource_value (value, 1);
3495 else if (EQ (attr, QCunderline)
3496 || EQ (attr, QCoverline)
3497 || EQ (attr, QCstrike_through))
3499 Lisp_Object boolean_value;
3501 /* If the result of face_boolean_x_resource_value is t or nil,
3502 VALUE does NOT specify a color. */
3503 boolean_value = face_boolean_x_resource_value (value, 0);
3504 if (SYMBOLP (boolean_value))
3505 value = boolean_value;
3507 else if (EQ (attr, QCbox) || EQ (attr, QCinherit))
3508 value = Fcar (Fread_from_string (value, Qnil, Qnil));
3510 return Finternal_set_lisp_face_attribute (face, attr, value, frame);
3513 #endif /* HAVE_WINDOW_SYSTEM */
3516 /***********************************************************************
3517 Menu face
3518 ***********************************************************************/
3520 #if defined HAVE_X_WINDOWS && defined USE_X_TOOLKIT
3522 /* Make menus on frame F appear as specified by the `menu' face. */
3524 static void
3525 x_update_menu_appearance (struct frame *f)
3527 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
3528 XrmDatabase rdb;
3530 if (dpyinfo
3531 && (rdb = XrmGetDatabase (FRAME_X_DISPLAY (f)),
3532 rdb != NULL))
3534 char line[512];
3535 char *buf = line;
3536 ptrdiff_t bufsize = sizeof line;
3537 Lisp_Object lface = lface_from_face_name (f, Qmenu, 1);
3538 struct face *face = FACE_FROM_ID (f, MENU_FACE_ID);
3539 const char *myname = SSDATA (Vx_resource_name);
3540 bool changed_p = 0;
3541 #ifdef USE_MOTIF
3542 const char *popup_path = "popup_menu";
3543 #else
3544 const char *popup_path = "menu.popup";
3545 #endif
3547 if (STRINGP (LFACE_FOREGROUND (lface)))
3549 exprintf (&buf, &bufsize, line, -1, "%s.%s*foreground: %s",
3550 myname, popup_path,
3551 SDATA (LFACE_FOREGROUND (lface)));
3552 XrmPutLineResource (&rdb, line);
3553 exprintf (&buf, &bufsize, line, -1, "%s.pane.menubar*foreground: %s",
3554 myname, SDATA (LFACE_FOREGROUND (lface)));
3555 XrmPutLineResource (&rdb, line);
3556 changed_p = 1;
3559 if (STRINGP (LFACE_BACKGROUND (lface)))
3561 exprintf (&buf, &bufsize, line, -1, "%s.%s*background: %s",
3562 myname, popup_path,
3563 SDATA (LFACE_BACKGROUND (lface)));
3564 XrmPutLineResource (&rdb, line);
3566 exprintf (&buf, &bufsize, line, -1, "%s.pane.menubar*background: %s",
3567 myname, SDATA (LFACE_BACKGROUND (lface)));
3568 XrmPutLineResource (&rdb, line);
3569 changed_p = 1;
3572 if (face->font
3573 /* On Solaris 5.8, it's been reported that the `menu' face
3574 can be unspecified here, during startup. Why this
3575 happens remains unknown. -- cyd */
3576 && FONTP (LFACE_FONT (lface))
3577 && (!UNSPECIFIEDP (LFACE_FAMILY (lface))
3578 || !UNSPECIFIEDP (LFACE_FOUNDRY (lface))
3579 || !UNSPECIFIEDP (LFACE_SWIDTH (lface))
3580 || !UNSPECIFIEDP (LFACE_WEIGHT (lface))
3581 || !UNSPECIFIEDP (LFACE_SLANT (lface))
3582 || !UNSPECIFIEDP (LFACE_HEIGHT (lface))))
3584 Lisp_Object xlfd = Ffont_xlfd_name (LFACE_FONT (lface), Qnil);
3585 #ifdef USE_MOTIF
3586 const char *suffix = "List";
3587 Bool motif = True;
3588 #else
3589 #if defined HAVE_X_I18N
3591 const char *suffix = "Set";
3592 #else
3593 const char *suffix = "";
3594 #endif
3595 Bool motif = False;
3596 #endif
3598 if (! NILP (xlfd))
3600 #if defined HAVE_X_I18N
3601 char *fontsetname = xic_create_fontsetname (SSDATA (xlfd), motif);
3602 #else
3603 char *fontsetname = SSDATA (xlfd);
3604 #endif
3605 exprintf (&buf, &bufsize, line, -1, "%s.pane.menubar*font%s: %s",
3606 myname, suffix, fontsetname);
3607 XrmPutLineResource (&rdb, line);
3609 exprintf (&buf, &bufsize, line, -1, "%s.%s*font%s: %s",
3610 myname, popup_path, suffix, fontsetname);
3611 XrmPutLineResource (&rdb, line);
3612 changed_p = 1;
3613 if (fontsetname != SSDATA (xlfd))
3614 xfree (fontsetname);
3618 if (changed_p && f->output_data.x->menubar_widget)
3619 free_frame_menubar (f);
3621 if (buf != line)
3622 xfree (buf);
3626 #endif /* HAVE_X_WINDOWS && USE_X_TOOLKIT */
3629 DEFUN ("face-attribute-relative-p", Fface_attribute_relative_p,
3630 Sface_attribute_relative_p,
3631 2, 2, 0,
3632 doc: /* Check whether a face attribute value is relative.
3633 Specifically, this function returns t if the attribute ATTRIBUTE
3634 with the value VALUE is relative.
3636 A relative value is one that doesn't entirely override whatever is
3637 inherited from another face. For most possible attributes,
3638 the only relative value that users see is `unspecified'.
3639 However, for :height, floating point values are also relative. */)
3640 (Lisp_Object attribute, Lisp_Object value)
3642 if (EQ (value, Qunspecified) || (EQ (value, QCignore_defface)))
3643 return Qt;
3644 else if (EQ (attribute, QCheight))
3645 return INTEGERP (value) ? Qnil : Qt;
3646 else
3647 return Qnil;
3650 DEFUN ("merge-face-attribute", Fmerge_face_attribute, Smerge_face_attribute,
3651 3, 3, 0,
3652 doc: /* Return face ATTRIBUTE VALUE1 merged with VALUE2.
3653 If VALUE1 or VALUE2 are absolute (see `face-attribute-relative-p'), then
3654 the result will be absolute, otherwise it will be relative. */)
3655 (Lisp_Object attribute, Lisp_Object value1, Lisp_Object value2)
3657 if (EQ (value1, Qunspecified) || EQ (value1, QCignore_defface))
3658 return value2;
3659 else if (EQ (attribute, QCheight))
3660 return merge_face_heights (value1, value2, value1);
3661 else
3662 return value1;
3666 DEFUN ("internal-get-lisp-face-attribute", Finternal_get_lisp_face_attribute,
3667 Sinternal_get_lisp_face_attribute,
3668 2, 3, 0,
3669 doc: /* Return face attribute KEYWORD of face SYMBOL.
3670 If SYMBOL does not name a valid Lisp face or KEYWORD isn't a valid
3671 face attribute name, signal an error.
3672 If the optional argument FRAME is given, report on face SYMBOL in that
3673 frame. If FRAME is t, report on the defaults for face SYMBOL (for new
3674 frames). If FRAME is omitted or nil, use the selected frame. */)
3675 (Lisp_Object symbol, Lisp_Object keyword, Lisp_Object frame)
3677 struct frame *f = EQ (frame, Qt) ? NULL : decode_live_frame (frame);
3678 Lisp_Object lface = lface_from_face_name (f, symbol, 1), value = Qnil;
3680 CHECK_SYMBOL (symbol);
3681 CHECK_SYMBOL (keyword);
3683 if (EQ (keyword, QCfamily))
3684 value = LFACE_FAMILY (lface);
3685 else if (EQ (keyword, QCfoundry))
3686 value = LFACE_FOUNDRY (lface);
3687 else if (EQ (keyword, QCheight))
3688 value = LFACE_HEIGHT (lface);
3689 else if (EQ (keyword, QCweight))
3690 value = LFACE_WEIGHT (lface);
3691 else if (EQ (keyword, QCslant))
3692 value = LFACE_SLANT (lface);
3693 else if (EQ (keyword, QCunderline))
3694 value = LFACE_UNDERLINE (lface);
3695 else if (EQ (keyword, QCoverline))
3696 value = LFACE_OVERLINE (lface);
3697 else if (EQ (keyword, QCstrike_through))
3698 value = LFACE_STRIKE_THROUGH (lface);
3699 else if (EQ (keyword, QCbox))
3700 value = LFACE_BOX (lface);
3701 else if (EQ (keyword, QCinverse_video)
3702 || EQ (keyword, QCreverse_video))
3703 value = LFACE_INVERSE (lface);
3704 else if (EQ (keyword, QCforeground))
3705 value = LFACE_FOREGROUND (lface);
3706 else if (EQ (keyword, QCdistant_foreground))
3707 value = LFACE_DISTANT_FOREGROUND (lface);
3708 else if (EQ (keyword, QCbackground))
3709 value = LFACE_BACKGROUND (lface);
3710 else if (EQ (keyword, QCstipple))
3711 value = LFACE_STIPPLE (lface);
3712 else if (EQ (keyword, QCwidth))
3713 value = LFACE_SWIDTH (lface);
3714 else if (EQ (keyword, QCinherit))
3715 value = LFACE_INHERIT (lface);
3716 else if (EQ (keyword, QCfont))
3717 value = LFACE_FONT (lface);
3718 else if (EQ (keyword, QCfontset))
3719 value = LFACE_FONTSET (lface);
3720 else
3721 signal_error ("Invalid face attribute name", keyword);
3723 if (IGNORE_DEFFACE_P (value))
3724 return Qunspecified;
3726 return value;
3730 DEFUN ("internal-lisp-face-attribute-values",
3731 Finternal_lisp_face_attribute_values,
3732 Sinternal_lisp_face_attribute_values, 1, 1, 0,
3733 doc: /* Return a list of valid discrete values for face attribute ATTR.
3734 Value is nil if ATTR doesn't have a discrete set of valid values. */)
3735 (Lisp_Object attr)
3737 Lisp_Object result = Qnil;
3739 CHECK_SYMBOL (attr);
3741 if (EQ (attr, QCunderline) || EQ (attr, QCoverline)
3742 || EQ (attr, QCstrike_through)
3743 || EQ (attr, QCinverse_video) || EQ (attr, QCreverse_video))
3744 result = list2 (Qt, Qnil);
3746 return result;
3750 DEFUN ("internal-merge-in-global-face", Finternal_merge_in_global_face,
3751 Sinternal_merge_in_global_face, 2, 2, 0,
3752 doc: /* Add attributes from frame-default definition of FACE to FACE on FRAME.
3753 Default face attributes override any local face attributes. */)
3754 (Lisp_Object face, Lisp_Object frame)
3756 int i;
3757 Lisp_Object global_lface, local_lface, *gvec, *lvec;
3758 struct frame *f = XFRAME (frame);
3760 CHECK_LIVE_FRAME (frame);
3761 global_lface = lface_from_face_name (NULL, face, 1);
3762 local_lface = lface_from_face_name (f, face, 0);
3763 if (NILP (local_lface))
3764 local_lface = Finternal_make_lisp_face (face, frame);
3766 /* Make every specified global attribute override the local one.
3767 BEWARE!! This is only used from `face-set-after-frame-default' where
3768 the local frame is defined from default specs in `face-defface-spec'
3769 and those should be overridden by global settings. Hence the strange
3770 "global before local" priority. */
3771 lvec = XVECTOR (local_lface)->contents;
3772 gvec = XVECTOR (global_lface)->contents;
3773 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3774 if (IGNORE_DEFFACE_P (gvec[i]))
3775 ASET (local_lface, i, Qunspecified);
3776 else if (! UNSPECIFIEDP (gvec[i]))
3777 ASET (local_lface, i, AREF (global_lface, i));
3779 /* If the default face was changed, update the face cache and the
3780 `font' frame parameter. */
3781 if (EQ (face, Qdefault))
3783 struct face_cache *c = FRAME_FACE_CACHE (f);
3784 struct face *newface, *oldface = FACE_FROM_ID (f, DEFAULT_FACE_ID);
3785 Lisp_Object attrs[LFACE_VECTOR_SIZE];
3787 /* This can be NULL (e.g., in batch mode). */
3788 if (oldface)
3790 /* Ensure that the face vector is fully specified by merging
3791 the previously-cached vector. */
3792 memcpy (attrs, oldface->lface, sizeof attrs);
3793 merge_face_vectors (f, lvec, attrs, 0);
3794 vcopy (local_lface, 0, attrs, LFACE_VECTOR_SIZE);
3795 newface = realize_face (c, lvec, DEFAULT_FACE_ID);
3797 if ((! UNSPECIFIEDP (gvec[LFACE_FAMILY_INDEX])
3798 || ! UNSPECIFIEDP (gvec[LFACE_FOUNDRY_INDEX])
3799 || ! UNSPECIFIEDP (gvec[LFACE_HEIGHT_INDEX])
3800 || ! UNSPECIFIEDP (gvec[LFACE_WEIGHT_INDEX])
3801 || ! UNSPECIFIEDP (gvec[LFACE_SLANT_INDEX])
3802 || ! UNSPECIFIEDP (gvec[LFACE_SWIDTH_INDEX])
3803 || ! UNSPECIFIEDP (gvec[LFACE_FONT_INDEX]))
3804 && newface->font)
3806 Lisp_Object name = newface->font->props[FONT_NAME_INDEX];
3807 Fmodify_frame_parameters (frame, list1 (Fcons (Qfont, name)));
3810 if (STRINGP (gvec[LFACE_FOREGROUND_INDEX]))
3811 Fmodify_frame_parameters (frame,
3812 list1 (Fcons (Qforeground_color,
3813 gvec[LFACE_FOREGROUND_INDEX])));
3815 if (STRINGP (gvec[LFACE_BACKGROUND_INDEX]))
3816 Fmodify_frame_parameters (frame,
3817 list1 (Fcons (Qbackground_color,
3818 gvec[LFACE_BACKGROUND_INDEX])));
3822 return Qnil;
3826 /* The following function is implemented for compatibility with 20.2.
3827 The function is used in x-resolve-fonts when it is asked to
3828 return fonts with the same size as the font of a face. This is
3829 done in fontset.el. */
3831 DEFUN ("face-font", Fface_font, Sface_font, 1, 3, 0,
3832 doc: /* Return the font name of face FACE, or nil if it is unspecified.
3833 The font name is, by default, for ASCII characters.
3834 If the optional argument FRAME is given, report on face FACE in that frame.
3835 If FRAME is t, report on the defaults for face FACE (for new frames).
3836 The font default for a face is either nil, or a list
3837 of the form (bold), (italic) or (bold italic).
3838 If FRAME is omitted or nil, use the selected frame. And, in this case,
3839 if the optional third argument CHARACTER is given,
3840 return the font name used for CHARACTER. */)
3841 (Lisp_Object face, Lisp_Object frame, Lisp_Object character)
3843 if (EQ (frame, Qt))
3845 Lisp_Object result = Qnil;
3846 Lisp_Object lface = lface_from_face_name (NULL, face, 1);
3848 if (!UNSPECIFIEDP (LFACE_WEIGHT (lface))
3849 && !EQ (LFACE_WEIGHT (lface), Qnormal))
3850 result = Fcons (Qbold, result);
3852 if (!UNSPECIFIEDP (LFACE_SLANT (lface))
3853 && !EQ (LFACE_SLANT (lface), Qnormal))
3854 result = Fcons (Qitalic, result);
3856 return result;
3858 else
3860 struct frame *f = decode_live_frame (frame);
3861 int face_id = lookup_named_face (f, face, 1);
3862 struct face *fface = FACE_FROM_ID (f, face_id);
3864 if (! fface)
3865 return Qnil;
3866 #ifdef HAVE_WINDOW_SYSTEM
3867 if (FRAME_WINDOW_P (f) && !NILP (character))
3869 CHECK_CHARACTER (character);
3870 face_id = FACE_FOR_CHAR (f, fface, XINT (character), -1, Qnil);
3871 fface = FACE_FROM_ID (f, face_id);
3873 return (fface->font
3874 ? fface->font->props[FONT_NAME_INDEX]
3875 : Qnil);
3876 #else /* !HAVE_WINDOW_SYSTEM */
3877 return build_string (FRAME_MSDOS_P (f)
3878 ? "ms-dos"
3879 : FRAME_W32_P (f) ? "w32term"
3880 :"tty");
3881 #endif
3886 /* Compare face-attribute values 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 face_attr_equal_p (Lisp_Object v1, Lisp_Object v2)
3893 /* Type can differ, e.g. when one attribute is unspecified, i.e. nil,
3894 and the other is specified. */
3895 if (XTYPE (v1) != XTYPE (v2))
3896 return 0;
3898 if (EQ (v1, v2))
3899 return 1;
3901 switch (XTYPE (v1))
3903 case Lisp_String:
3904 if (SBYTES (v1) != SBYTES (v2))
3905 return 0;
3907 return memcmp (SDATA (v1), SDATA (v2), SBYTES (v1)) == 0;
3909 case_Lisp_Int:
3910 case Lisp_Symbol:
3911 return 0;
3913 default:
3914 return !NILP (Fequal (v1, v2));
3919 /* Compare face vectors V1 and V2 for equality. Value is non-zero if
3920 all attributes are `equal'. Tries to be fast because this function
3921 is called quite often. */
3923 static bool
3924 lface_equal_p (Lisp_Object *v1, Lisp_Object *v2)
3926 int i;
3927 bool equal_p = 1;
3929 for (i = 1; i < LFACE_VECTOR_SIZE && equal_p; ++i)
3930 equal_p = face_attr_equal_p (v1[i], v2[i]);
3932 return equal_p;
3936 DEFUN ("internal-lisp-face-equal-p", Finternal_lisp_face_equal_p,
3937 Sinternal_lisp_face_equal_p, 2, 3, 0,
3938 doc: /* True if FACE1 and FACE2 are equal.
3939 If the optional argument FRAME is given, report on FACE1 and FACE2 in that frame.
3940 If FRAME is t, report on the defaults for FACE1 and FACE2 (for new frames).
3941 If FRAME is omitted or nil, use the selected frame. */)
3942 (Lisp_Object face1, Lisp_Object face2, Lisp_Object frame)
3944 int equal_p;
3945 struct frame *f;
3946 Lisp_Object lface1, lface2;
3948 /* Don't use decode_window_system_frame here because this function
3949 is called before X frames exist. At that time, if FRAME is nil,
3950 selected_frame will be used which is the frame dumped with
3951 Emacs. That frame is not an X frame. */
3952 f = EQ (frame, Qt) ? NULL : decode_live_frame (frame);
3954 lface1 = lface_from_face_name (f, face1, 1);
3955 lface2 = lface_from_face_name (f, face2, 1);
3956 equal_p = lface_equal_p (XVECTOR (lface1)->contents,
3957 XVECTOR (lface2)->contents);
3958 return equal_p ? Qt : Qnil;
3962 DEFUN ("internal-lisp-face-empty-p", Finternal_lisp_face_empty_p,
3963 Sinternal_lisp_face_empty_p, 1, 2, 0,
3964 doc: /* True if FACE has no attribute specified.
3965 If the optional argument FRAME is given, report on face FACE in that frame.
3966 If FRAME is t, report on the defaults for face FACE (for new frames).
3967 If FRAME is omitted or nil, use the selected frame. */)
3968 (Lisp_Object face, Lisp_Object frame)
3970 struct frame *f = EQ (frame, Qt) ? NULL : decode_live_frame (frame);
3971 Lisp_Object lface = lface_from_face_name (f, face, 1);
3972 int i;
3974 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3975 if (!UNSPECIFIEDP (AREF (lface, i)))
3976 break;
3978 return i == LFACE_VECTOR_SIZE ? Qt : Qnil;
3982 DEFUN ("frame-face-alist", Fframe_face_alist, Sframe_face_alist,
3983 0, 1, 0,
3984 doc: /* Return an alist of frame-local faces defined on FRAME.
3985 For internal use only. */)
3986 (Lisp_Object frame)
3988 return decode_live_frame (frame)->face_alist;
3992 /* Return a hash code for Lisp string STRING with case ignored. Used
3993 below in computing a hash value for a Lisp face. */
3995 static unsigned
3996 hash_string_case_insensitive (Lisp_Object string)
3998 const unsigned char *s;
3999 unsigned hash = 0;
4000 eassert (STRINGP (string));
4001 for (s = SDATA (string); *s; ++s)
4002 hash = (hash << 1) ^ c_tolower (*s);
4003 return hash;
4007 /* Return a hash code for face attribute vector V. */
4009 static unsigned
4010 lface_hash (Lisp_Object *v)
4012 return (hash_string_case_insensitive (v[LFACE_FAMILY_INDEX])
4013 ^ hash_string_case_insensitive (v[LFACE_FOUNDRY_INDEX])
4014 ^ hash_string_case_insensitive (v[LFACE_FOREGROUND_INDEX])
4015 ^ hash_string_case_insensitive (v[LFACE_BACKGROUND_INDEX])
4016 ^ XHASH (v[LFACE_WEIGHT_INDEX])
4017 ^ XHASH (v[LFACE_SLANT_INDEX])
4018 ^ XHASH (v[LFACE_SWIDTH_INDEX])
4019 ^ XHASH (v[LFACE_HEIGHT_INDEX]));
4022 #ifdef HAVE_WINDOW_SYSTEM
4024 /* Return non-zero if LFACE1 and LFACE2 specify the same font (without
4025 considering charsets/registries). They do if they specify the same
4026 family, point size, weight, width, slant, and font. Both
4027 LFACE1 and LFACE2 must be fully-specified. */
4029 static int
4030 lface_same_font_attributes_p (Lisp_Object *lface1, Lisp_Object *lface2)
4032 eassert (lface_fully_specified_p (lface1)
4033 && lface_fully_specified_p (lface2));
4034 return (xstrcasecmp (SSDATA (lface1[LFACE_FAMILY_INDEX]),
4035 SSDATA (lface2[LFACE_FAMILY_INDEX])) == 0
4036 && xstrcasecmp (SSDATA (lface1[LFACE_FOUNDRY_INDEX]),
4037 SSDATA (lface2[LFACE_FOUNDRY_INDEX])) == 0
4038 && EQ (lface1[LFACE_HEIGHT_INDEX], lface2[LFACE_HEIGHT_INDEX])
4039 && EQ (lface1[LFACE_SWIDTH_INDEX], lface2[LFACE_SWIDTH_INDEX])
4040 && EQ (lface1[LFACE_WEIGHT_INDEX], lface2[LFACE_WEIGHT_INDEX])
4041 && EQ (lface1[LFACE_SLANT_INDEX], lface2[LFACE_SLANT_INDEX])
4042 && EQ (lface1[LFACE_FONT_INDEX], lface2[LFACE_FONT_INDEX])
4043 && (EQ (lface1[LFACE_FONTSET_INDEX], lface2[LFACE_FONTSET_INDEX])
4044 || (STRINGP (lface1[LFACE_FONTSET_INDEX])
4045 && STRINGP (lface2[LFACE_FONTSET_INDEX])
4046 && ! xstrcasecmp (SSDATA (lface1[LFACE_FONTSET_INDEX]),
4047 SSDATA (lface2[LFACE_FONTSET_INDEX]))))
4051 #endif /* HAVE_WINDOW_SYSTEM */
4053 /***********************************************************************
4054 Realized Faces
4055 ***********************************************************************/
4057 /* Allocate and return a new realized face for Lisp face attribute
4058 vector ATTR. */
4060 static struct face *
4061 make_realized_face (Lisp_Object *attr)
4063 enum { off = offsetof (struct face, id) };
4064 struct face *face = xmalloc (sizeof *face);
4066 memcpy (face->lface, attr, sizeof face->lface);
4067 memset (&face->id, 0, sizeof *face - off);
4068 face->ascii_face = face;
4070 return face;
4074 /* Free realized face FACE, including its X resources. FACE may
4075 be null. */
4077 static void
4078 free_realized_face (struct frame *f, struct face *face)
4080 if (face)
4082 #ifdef HAVE_WINDOW_SYSTEM
4083 if (FRAME_WINDOW_P (f))
4085 /* Free fontset of FACE if it is ASCII face. */
4086 if (face->fontset >= 0 && face == face->ascii_face)
4087 free_face_fontset (f, face);
4088 if (face->gc)
4090 block_input ();
4091 if (face->font)
4092 font_done_for_face (f, face);
4093 x_free_gc (f, face->gc);
4094 face->gc = 0;
4095 unblock_input ();
4097 #ifdef HAVE_X_WINDOWS
4098 free_face_colors (f, face);
4099 #endif /* HAVE_X_WINDOWS */
4100 x_destroy_bitmap (f, face->stipple);
4102 #endif /* HAVE_WINDOW_SYSTEM */
4104 xfree (face);
4109 /* Prepare face FACE for subsequent display on frame F. This
4110 allocated GCs if they haven't been allocated yet or have been freed
4111 by clearing the face cache. */
4113 void
4114 prepare_face_for_display (struct frame *f, struct face *face)
4116 #ifdef HAVE_WINDOW_SYSTEM
4117 eassert (FRAME_WINDOW_P (f));
4119 if (face->gc == 0)
4121 XGCValues xgcv;
4122 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
4124 xgcv.foreground = face->foreground;
4125 xgcv.background = face->background;
4126 #ifdef HAVE_X_WINDOWS
4127 xgcv.graphics_exposures = False;
4128 #endif
4130 block_input ();
4131 #ifdef HAVE_X_WINDOWS
4132 if (face->stipple)
4134 xgcv.fill_style = FillOpaqueStippled;
4135 xgcv.stipple = x_bitmap_pixmap (f, face->stipple);
4136 mask |= GCFillStyle | GCStipple;
4138 #endif
4139 face->gc = x_create_gc (f, mask, &xgcv);
4140 if (face->font)
4141 font_prepare_for_face (f, face);
4142 unblock_input ();
4144 #endif /* HAVE_WINDOW_SYSTEM */
4148 /* Returns the `distance' between the colors X and Y. */
4150 static int
4151 color_distance (XColor *x, XColor *y)
4153 /* This formula is from a paper titled `Colour metric' by Thiadmer Riemersma.
4154 Quoting from that paper:
4156 This formula has results that are very close to L*u*v* (with the
4157 modified lightness curve) and, more importantly, it is a more even
4158 algorithm: it does not have a range of colors where it suddenly
4159 gives far from optimal results.
4161 See <http://www.compuphase.com/cmetric.htm> for more info. */
4163 long r = (x->red - y->red) >> 8;
4164 long g = (x->green - y->green) >> 8;
4165 long b = (x->blue - y->blue) >> 8;
4166 long r_mean = (x->red + y->red) >> 9;
4168 return
4169 (((512 + r_mean) * r * r) >> 8)
4170 + 4 * g * g
4171 + (((767 - r_mean) * b * b) >> 8);
4175 DEFUN ("color-distance", Fcolor_distance, Scolor_distance, 2, 3, 0,
4176 doc: /* Return an integer distance between COLOR1 and COLOR2 on FRAME.
4177 COLOR1 and COLOR2 may be either strings containing the color name,
4178 or lists of the form (RED GREEN BLUE).
4179 If FRAME is unspecified or nil, the current frame is used. */)
4180 (Lisp_Object color1, Lisp_Object color2, Lisp_Object frame)
4182 struct frame *f = decode_live_frame (frame);
4183 XColor cdef1, cdef2;
4185 if (!(CONSP (color1) && parse_rgb_list (color1, &cdef1))
4186 && !(STRINGP (color1) && defined_color (f, SSDATA (color1), &cdef1, 0)))
4187 signal_error ("Invalid color", color1);
4188 if (!(CONSP (color2) && parse_rgb_list (color2, &cdef2))
4189 && !(STRINGP (color2) && defined_color (f, SSDATA (color2), &cdef2, 0)))
4190 signal_error ("Invalid color", color2);
4192 return make_number (color_distance (&cdef1, &cdef2));
4196 /***********************************************************************
4197 Face Cache
4198 ***********************************************************************/
4200 /* Return a new face cache for frame F. */
4202 static struct face_cache *
4203 make_face_cache (struct frame *f)
4205 struct face_cache *c = xmalloc (sizeof *c);
4207 c->buckets = xzalloc (FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets);
4208 c->size = 50;
4209 c->used = 0;
4210 c->faces_by_id = xmalloc (c->size * sizeof *c->faces_by_id);
4211 c->f = f;
4212 c->menu_face_changed_p = menu_face_changed_default;
4213 return c;
4216 #ifdef HAVE_WINDOW_SYSTEM
4218 /* Clear out all graphics contexts for all realized faces, except for
4219 the basic faces. This should be done from time to time just to avoid
4220 keeping too many graphics contexts that are no longer needed. */
4222 static void
4223 clear_face_gcs (struct face_cache *c)
4225 if (c && FRAME_WINDOW_P (c->f))
4227 int i;
4228 for (i = BASIC_FACE_ID_SENTINEL; i < c->used; ++i)
4230 struct face *face = c->faces_by_id[i];
4231 if (face && face->gc)
4233 block_input ();
4234 if (face->font)
4235 font_done_for_face (c->f, face);
4236 x_free_gc (c->f, face->gc);
4237 face->gc = 0;
4238 unblock_input ();
4244 #endif /* HAVE_WINDOW_SYSTEM */
4246 /* Free all realized faces in face cache C, including basic faces.
4247 C may be null. If faces are freed, make sure the frame's current
4248 matrix is marked invalid, so that a display caused by an expose
4249 event doesn't try to use faces we destroyed. */
4251 static void
4252 free_realized_faces (struct face_cache *c)
4254 if (c && c->used)
4256 int i, size;
4257 struct frame *f = c->f;
4259 /* We must block input here because we can't process X events
4260 safely while only some faces are freed, or when the frame's
4261 current matrix still references freed faces. */
4262 block_input ();
4264 for (i = 0; i < c->used; ++i)
4266 free_realized_face (f, c->faces_by_id[i]);
4267 c->faces_by_id[i] = NULL;
4270 c->used = 0;
4271 size = FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
4272 memset (c->buckets, 0, size);
4274 /* Must do a thorough redisplay the next time. Mark current
4275 matrices as invalid because they will reference faces freed
4276 above. This function is also called when a frame is
4277 destroyed. In this case, the root window of F is nil. */
4278 if (WINDOWP (f->root_window))
4280 clear_current_matrices (f);
4281 windows_or_buffers_changed = 58;
4284 unblock_input ();
4289 /* Free all realized faces on FRAME or on all frames if FRAME is nil.
4290 This is done after attributes of a named face have been changed,
4291 because we can't tell which realized faces depend on that face. */
4293 void
4294 free_all_realized_faces (Lisp_Object frame)
4296 if (NILP (frame))
4298 Lisp_Object rest;
4299 FOR_EACH_FRAME (rest, frame)
4300 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
4302 else
4303 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
4307 /* Free face cache C and faces in it, including their X resources. */
4309 static void
4310 free_face_cache (struct face_cache *c)
4312 if (c)
4314 free_realized_faces (c);
4315 xfree (c->buckets);
4316 xfree (c->faces_by_id);
4317 xfree (c);
4322 /* Cache realized face FACE in face cache C. HASH is the hash value
4323 of FACE. If FACE is for ASCII characters (i.e. FACE->ascii_face ==
4324 FACE), insert the new face to the beginning of the collision list
4325 of the face hash table of C. Otherwise, add the new face to the
4326 end of the collision list. This way, lookup_face can quickly find
4327 that a requested face is not cached. */
4329 static void
4330 cache_face (struct face_cache *c, struct face *face, unsigned int hash)
4332 int i = hash % FACE_CACHE_BUCKETS_SIZE;
4334 face->hash = hash;
4336 if (face->ascii_face != face)
4338 struct face *last = c->buckets[i];
4339 if (last)
4341 while (last->next)
4342 last = last->next;
4343 last->next = face;
4344 face->prev = last;
4345 face->next = NULL;
4347 else
4349 c->buckets[i] = face;
4350 face->prev = face->next = NULL;
4353 else
4355 face->prev = NULL;
4356 face->next = c->buckets[i];
4357 if (face->next)
4358 face->next->prev = face;
4359 c->buckets[i] = face;
4362 /* Find a free slot in C->faces_by_id and use the index of the free
4363 slot as FACE->id. */
4364 for (i = 0; i < c->used; ++i)
4365 if (c->faces_by_id[i] == NULL)
4366 break;
4367 face->id = i;
4369 #ifdef GLYPH_DEBUG
4370 /* Check that FACE got a unique id. */
4372 int j, n;
4373 struct face *face1;
4375 for (j = n = 0; j < FACE_CACHE_BUCKETS_SIZE; ++j)
4376 for (face1 = c->buckets[j]; face1; face1 = face1->next)
4377 if (face1->id == i)
4378 ++n;
4380 eassert (n == 1);
4382 #endif /* GLYPH_DEBUG */
4384 /* Maybe enlarge C->faces_by_id. */
4385 if (i == c->used)
4387 if (c->used == c->size)
4388 c->faces_by_id = xpalloc (c->faces_by_id, &c->size, 1, MAX_FACE_ID,
4389 sizeof *c->faces_by_id);
4390 c->used++;
4393 c->faces_by_id[i] = face;
4397 /* Remove face FACE from cache C. */
4399 static void
4400 uncache_face (struct face_cache *c, struct face *face)
4402 int i = face->hash % FACE_CACHE_BUCKETS_SIZE;
4404 if (face->prev)
4405 face->prev->next = face->next;
4406 else
4407 c->buckets[i] = face->next;
4409 if (face->next)
4410 face->next->prev = face->prev;
4412 c->faces_by_id[face->id] = NULL;
4413 if (face->id == c->used)
4414 --c->used;
4418 /* Look up a realized face with face attributes ATTR in the face cache
4419 of frame F. The face will be used to display ASCII characters.
4420 Value is the ID of the face found. If no suitable face is found,
4421 realize a new one. */
4423 static int
4424 lookup_face (struct frame *f, Lisp_Object *attr)
4426 struct face_cache *cache = FRAME_FACE_CACHE (f);
4427 unsigned hash;
4428 int i;
4429 struct face *face;
4431 eassert (cache != NULL);
4432 check_lface_attrs (attr);
4434 /* Look up ATTR in the face cache. */
4435 hash = lface_hash (attr);
4436 i = hash % FACE_CACHE_BUCKETS_SIZE;
4438 for (face = cache->buckets[i]; face; face = face->next)
4440 if (face->ascii_face != face)
4442 /* There's no more ASCII face. */
4443 face = NULL;
4444 break;
4446 if (face->hash == hash
4447 && lface_equal_p (face->lface, attr))
4448 break;
4451 /* If not found, realize a new face. */
4452 if (face == NULL)
4453 face = realize_face (cache, attr, -1);
4455 #ifdef GLYPH_DEBUG
4456 eassert (face == FACE_FROM_ID (f, face->id));
4457 #endif /* GLYPH_DEBUG */
4459 return face->id;
4462 #ifdef HAVE_WINDOW_SYSTEM
4463 /* Look up a realized face that has the same attributes as BASE_FACE
4464 except for the font in the face cache of frame F. If FONT-OBJECT
4465 is not nil, it is an already opened font. If FONT-OBJECT is nil,
4466 the face has no font. Value is the ID of the face found. If no
4467 suitable face is found, realize a new one. */
4470 face_for_font (struct frame *f, Lisp_Object font_object, struct face *base_face)
4472 struct face_cache *cache = FRAME_FACE_CACHE (f);
4473 unsigned hash;
4474 int i;
4475 struct face *face;
4477 eassert (cache != NULL);
4478 base_face = base_face->ascii_face;
4479 hash = lface_hash (base_face->lface);
4480 i = hash % FACE_CACHE_BUCKETS_SIZE;
4482 for (face = cache->buckets[i]; face; face = face->next)
4484 if (face->ascii_face == face)
4485 continue;
4486 if (face->ascii_face == base_face
4487 && face->font == (NILP (font_object) ? NULL
4488 : XFONT_OBJECT (font_object))
4489 && lface_equal_p (face->lface, base_face->lface))
4490 return face->id;
4493 /* If not found, realize a new face. */
4494 face = realize_non_ascii_face (f, font_object, base_face);
4495 return face->id;
4497 #endif /* HAVE_WINDOW_SYSTEM */
4499 /* Return the face id of the realized face for named face SYMBOL on
4500 frame F suitable for displaying ASCII characters. Value is -1 if
4501 the face couldn't be determined, which might happen if the default
4502 face isn't realized and cannot be realized. */
4505 lookup_named_face (struct frame *f, Lisp_Object symbol, int signal_p)
4507 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4508 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
4509 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4511 if (default_face == NULL)
4513 if (!realize_basic_faces (f))
4514 return -1;
4515 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4516 if (default_face == NULL)
4517 emacs_abort (); /* realize_basic_faces must have set it up */
4520 if (! get_lface_attributes (f, symbol, symbol_attrs, signal_p, 0))
4521 return -1;
4523 memcpy (attrs, default_face->lface, sizeof attrs);
4524 merge_face_vectors (f, symbol_attrs, attrs, 0);
4526 return lookup_face (f, attrs);
4530 /* Return the display face-id of the basic face whose canonical face-id
4531 is FACE_ID. The return value will usually simply be FACE_ID, unless that
4532 basic face has bee remapped via Vface_remapping_alist. This function is
4533 conservative: if something goes wrong, it will simply return FACE_ID
4534 rather than signal an error. */
4537 lookup_basic_face (struct frame *f, int face_id)
4539 Lisp_Object name, mapping;
4540 int remapped_face_id;
4542 if (NILP (Vface_remapping_alist))
4543 return face_id; /* Nothing to do. */
4545 switch (face_id)
4547 case DEFAULT_FACE_ID: name = Qdefault; break;
4548 case MODE_LINE_FACE_ID: name = Qmode_line; break;
4549 case MODE_LINE_INACTIVE_FACE_ID: name = Qmode_line_inactive; break;
4550 case HEADER_LINE_FACE_ID: name = Qheader_line; break;
4551 case TOOL_BAR_FACE_ID: name = Qtool_bar; break;
4552 case FRINGE_FACE_ID: name = Qfringe; break;
4553 case SCROLL_BAR_FACE_ID: name = Qscroll_bar; break;
4554 case BORDER_FACE_ID: name = Qborder; break;
4555 case CURSOR_FACE_ID: name = Qcursor; break;
4556 case MOUSE_FACE_ID: name = Qmouse; break;
4557 case MENU_FACE_ID: name = Qmenu; break;
4559 default:
4560 emacs_abort (); /* the caller is supposed to pass us a basic face id */
4563 /* Do a quick scan through Vface_remapping_alist, and return immediately
4564 if there is no remapping for face NAME. This is just an optimization
4565 for the very common no-remapping case. */
4566 mapping = assq_no_quit (name, Vface_remapping_alist);
4567 if (NILP (mapping))
4568 return face_id; /* Give up. */
4570 /* If there is a remapping entry, lookup the face using NAME, which will
4571 handle the remapping too. */
4572 remapped_face_id = lookup_named_face (f, name, 0);
4573 if (remapped_face_id < 0)
4574 return face_id; /* Give up. */
4576 return remapped_face_id;
4580 /* Return a face for charset ASCII that is like the face with id
4581 FACE_ID on frame F, but has a font that is STEPS steps smaller.
4582 STEPS < 0 means larger. Value is the id of the face. */
4585 smaller_face (struct frame *f, int face_id, int steps)
4587 #ifdef HAVE_WINDOW_SYSTEM
4588 struct face *face;
4589 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4590 int pt, last_pt, last_height;
4591 int delta;
4592 int new_face_id;
4593 struct face *new_face;
4595 /* If not called for an X frame, just return the original face. */
4596 if (FRAME_TERMCAP_P (f))
4597 return face_id;
4599 /* Try in increments of 1/2 pt. */
4600 delta = steps < 0 ? 5 : -5;
4601 steps = eabs (steps);
4603 face = FACE_FROM_ID (f, face_id);
4604 memcpy (attrs, face->lface, sizeof attrs);
4605 pt = last_pt = XFASTINT (attrs[LFACE_HEIGHT_INDEX]);
4606 new_face_id = face_id;
4607 last_height = FONT_HEIGHT (face->font);
4609 while (steps
4610 && pt + delta > 0
4611 /* Give up if we cannot find a font within 10pt. */
4612 && eabs (last_pt - pt) < 100)
4614 /* Look up a face for a slightly smaller/larger font. */
4615 pt += delta;
4616 attrs[LFACE_HEIGHT_INDEX] = make_number (pt);
4617 new_face_id = lookup_face (f, attrs);
4618 new_face = FACE_FROM_ID (f, new_face_id);
4620 /* If height changes, count that as one step. */
4621 if ((delta < 0 && FONT_HEIGHT (new_face->font) < last_height)
4622 || (delta > 0 && FONT_HEIGHT (new_face->font) > last_height))
4624 --steps;
4625 last_height = FONT_HEIGHT (new_face->font);
4626 last_pt = pt;
4630 return new_face_id;
4632 #else /* not HAVE_WINDOW_SYSTEM */
4634 return face_id;
4636 #endif /* not HAVE_WINDOW_SYSTEM */
4640 /* Return a face for charset ASCII that is like the face with id
4641 FACE_ID on frame F, but has height HEIGHT. */
4644 face_with_height (struct frame *f, int face_id, int height)
4646 #ifdef HAVE_WINDOW_SYSTEM
4647 struct face *face;
4648 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4650 if (FRAME_TERMCAP_P (f)
4651 || height <= 0)
4652 return face_id;
4654 face = FACE_FROM_ID (f, face_id);
4655 memcpy (attrs, face->lface, sizeof attrs);
4656 attrs[LFACE_HEIGHT_INDEX] = make_number (height);
4657 font_clear_prop (attrs, FONT_SIZE_INDEX);
4658 face_id = lookup_face (f, attrs);
4659 #endif /* HAVE_WINDOW_SYSTEM */
4661 return face_id;
4665 /* Return the face id of the realized face for named face SYMBOL on
4666 frame F suitable for displaying ASCII characters, and use
4667 attributes of the face FACE_ID for attributes that aren't
4668 completely specified by SYMBOL. This is like lookup_named_face,
4669 except that the default attributes come from FACE_ID, not from the
4670 default face. FACE_ID is assumed to be already realized. */
4673 lookup_derived_face (struct frame *f, Lisp_Object symbol, int face_id,
4674 int signal_p)
4676 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4677 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
4678 struct face *default_face = FACE_FROM_ID (f, face_id);
4680 if (!default_face)
4681 emacs_abort ();
4683 if (!get_lface_attributes (f, symbol, symbol_attrs, signal_p, 0))
4684 return -1;
4686 memcpy (attrs, default_face->lface, sizeof attrs);
4687 merge_face_vectors (f, symbol_attrs, attrs, 0);
4688 return lookup_face (f, attrs);
4691 DEFUN ("face-attributes-as-vector", Fface_attributes_as_vector,
4692 Sface_attributes_as_vector, 1, 1, 0,
4693 doc: /* Return a vector of face attributes corresponding to PLIST. */)
4694 (Lisp_Object plist)
4696 Lisp_Object lface;
4697 lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
4698 Qunspecified);
4699 merge_face_ref (XFRAME (selected_frame), plist, XVECTOR (lface)->contents,
4700 1, 0);
4701 return lface;
4706 /***********************************************************************
4707 Face capability testing
4708 ***********************************************************************/
4711 /* If the distance (as returned by color_distance) between two colors is
4712 less than this, then they are considered the same, for determining
4713 whether a color is supported or not. The range of values is 0-65535. */
4715 #define TTY_SAME_COLOR_THRESHOLD 10000
4717 #ifdef HAVE_WINDOW_SYSTEM
4719 /* Return true if all the face attributes in ATTRS are supported
4720 on the window-system frame F.
4722 The definition of `supported' is somewhat heuristic, but basically means
4723 that a face containing all the attributes in ATTRS, when merged with the
4724 default face for display, can be represented in a way that's
4726 \(1) different in appearance than the default face, and
4727 \(2) `close in spirit' to what the attributes specify, if not exact. */
4729 static bool
4730 x_supports_face_attributes_p (struct frame *f,
4731 Lisp_Object attrs[LFACE_VECTOR_SIZE],
4732 struct face *def_face)
4734 Lisp_Object *def_attrs = def_face->lface;
4736 /* Check that other specified attributes are different that the default
4737 face. */
4738 if ((!UNSPECIFIEDP (attrs[LFACE_UNDERLINE_INDEX])
4739 && face_attr_equal_p (attrs[LFACE_UNDERLINE_INDEX],
4740 def_attrs[LFACE_UNDERLINE_INDEX]))
4741 || (!UNSPECIFIEDP (attrs[LFACE_INVERSE_INDEX])
4742 && face_attr_equal_p (attrs[LFACE_INVERSE_INDEX],
4743 def_attrs[LFACE_INVERSE_INDEX]))
4744 || (!UNSPECIFIEDP (attrs[LFACE_FOREGROUND_INDEX])
4745 && face_attr_equal_p (attrs[LFACE_FOREGROUND_INDEX],
4746 def_attrs[LFACE_FOREGROUND_INDEX]))
4747 || (!UNSPECIFIEDP (attrs[LFACE_DISTANT_FOREGROUND_INDEX])
4748 && face_attr_equal_p (attrs[LFACE_DISTANT_FOREGROUND_INDEX],
4749 def_attrs[LFACE_DISTANT_FOREGROUND_INDEX]))
4750 || (!UNSPECIFIEDP (attrs[LFACE_BACKGROUND_INDEX])
4751 && face_attr_equal_p (attrs[LFACE_BACKGROUND_INDEX],
4752 def_attrs[LFACE_BACKGROUND_INDEX]))
4753 || (!UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
4754 && face_attr_equal_p (attrs[LFACE_STIPPLE_INDEX],
4755 def_attrs[LFACE_STIPPLE_INDEX]))
4756 || (!UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
4757 && face_attr_equal_p (attrs[LFACE_OVERLINE_INDEX],
4758 def_attrs[LFACE_OVERLINE_INDEX]))
4759 || (!UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
4760 && face_attr_equal_p (attrs[LFACE_STRIKE_THROUGH_INDEX],
4761 def_attrs[LFACE_STRIKE_THROUGH_INDEX]))
4762 || (!UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
4763 && face_attr_equal_p (attrs[LFACE_BOX_INDEX],
4764 def_attrs[LFACE_BOX_INDEX])))
4765 return 0;
4767 /* Check font-related attributes, as those are the most commonly
4768 "unsupported" on a window-system (because of missing fonts). */
4769 if (!UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
4770 || !UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
4771 || !UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
4772 || !UNSPECIFIEDP (attrs[LFACE_WEIGHT_INDEX])
4773 || !UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX])
4774 || !UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX]))
4776 int face_id;
4777 struct face *face;
4778 Lisp_Object merged_attrs[LFACE_VECTOR_SIZE];
4779 int i;
4781 memcpy (merged_attrs, def_attrs, sizeof merged_attrs);
4783 merge_face_vectors (f, attrs, merged_attrs, 0);
4785 face_id = lookup_face (f, merged_attrs);
4786 face = FACE_FROM_ID (f, face_id);
4788 if (! face)
4789 error ("Cannot make face");
4791 /* If the font is the same, or no font is found, then not
4792 supported. */
4793 if (face->font == def_face->font
4794 || ! face->font)
4795 return 0;
4796 for (i = FONT_TYPE_INDEX; i <= FONT_SIZE_INDEX; i++)
4797 if (! EQ (face->font->props[i], def_face->font->props[i]))
4799 Lisp_Object s1, s2;
4801 if (i < FONT_FOUNDRY_INDEX || i > FONT_REGISTRY_INDEX
4802 || face->font->driver->case_sensitive)
4803 return 1;
4804 s1 = SYMBOL_NAME (face->font->props[i]);
4805 s2 = SYMBOL_NAME (def_face->font->props[i]);
4806 if (! EQ (Fcompare_strings (s1, make_number (0), Qnil,
4807 s2, make_number (0), Qnil, Qt), Qt))
4808 return 1;
4810 return 0;
4813 /* Everything checks out, this face is supported. */
4814 return 1;
4817 #endif /* HAVE_WINDOW_SYSTEM */
4819 /* Return true if all the face attributes in ATTRS are supported
4820 on the tty frame F.
4822 The definition of `supported' is somewhat heuristic, but basically means
4823 that a face containing all the attributes in ATTRS, when merged
4824 with the default face for display, can be represented in a way that's
4826 \(1) different in appearance than the default face, and
4827 \(2) `close in spirit' to what the attributes specify, if not exact.
4829 Point (2) implies that a `:weight black' attribute will be satisfied
4830 by any terminal that can display bold, and a `:foreground "yellow"' as
4831 long as the terminal can display a yellowish color, but `:slant italic'
4832 will _not_ be satisfied by the tty display code's automatic
4833 substitution of a `dim' face for italic. */
4835 static bool
4836 tty_supports_face_attributes_p (struct frame *f,
4837 Lisp_Object attrs[LFACE_VECTOR_SIZE],
4838 struct face *def_face)
4840 int weight, slant;
4841 Lisp_Object val, fg, bg;
4842 XColor fg_tty_color, fg_std_color;
4843 XColor bg_tty_color, bg_std_color;
4844 unsigned test_caps = 0;
4845 Lisp_Object *def_attrs = def_face->lface;
4847 /* First check some easy-to-check stuff; ttys support none of the
4848 following attributes, so we can just return false if any are requested
4849 (even if `nominal' values are specified, we should still return false,
4850 as that will be the same value that the default face uses). We
4851 consider :slant unsupportable on ttys, even though the face code
4852 actually `fakes' them using a dim attribute if possible. This is
4853 because the faked result is too different from what the face
4854 specifies. */
4855 if (!UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
4856 || !UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
4857 || !UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
4858 || !UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
4859 || !UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
4860 || !UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
4861 || !UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
4862 || !UNSPECIFIEDP (attrs[LFACE_BOX_INDEX]))
4863 return 0;
4865 /* Test for terminal `capabilities' (non-color character attributes). */
4867 /* font weight (bold/dim) */
4868 val = attrs[LFACE_WEIGHT_INDEX];
4869 if (!UNSPECIFIEDP (val)
4870 && (weight = FONT_WEIGHT_NAME_NUMERIC (val), weight >= 0))
4872 int def_weight = FONT_WEIGHT_NAME_NUMERIC (def_attrs[LFACE_WEIGHT_INDEX]);
4874 if (weight > 100)
4876 if (def_weight > 100)
4877 return 0; /* same as default */
4878 test_caps = TTY_CAP_BOLD;
4880 else if (weight < 100)
4882 if (def_weight < 100)
4883 return 0; /* same as default */
4884 test_caps = TTY_CAP_DIM;
4886 else if (def_weight == 100)
4887 return 0; /* same as default */
4890 /* font slant */
4891 val = attrs[LFACE_SLANT_INDEX];
4892 if (!UNSPECIFIEDP (val)
4893 && (slant = FONT_SLANT_NAME_NUMERIC (val), slant >= 0))
4895 int def_slant = FONT_SLANT_NAME_NUMERIC (def_attrs[LFACE_SLANT_INDEX]);
4896 if (slant == 100 || slant == def_slant)
4897 return 0; /* same as default */
4898 else
4899 test_caps |= TTY_CAP_ITALIC;
4902 /* underlining */
4903 val = attrs[LFACE_UNDERLINE_INDEX];
4904 if (!UNSPECIFIEDP (val))
4906 if (STRINGP (val))
4907 return 0; /* ttys can't use colored underlines */
4908 else if (EQ (CAR_SAFE (val), QCstyle) && EQ (CAR_SAFE (CDR_SAFE (val)), Qwave))
4909 return 0; /* ttys can't use wave underlines */
4910 else if (face_attr_equal_p (val, def_attrs[LFACE_UNDERLINE_INDEX]))
4911 return 0; /* same as default */
4912 else
4913 test_caps |= TTY_CAP_UNDERLINE;
4916 /* inverse video */
4917 val = attrs[LFACE_INVERSE_INDEX];
4918 if (!UNSPECIFIEDP (val))
4920 if (face_attr_equal_p (val, def_attrs[LFACE_INVERSE_INDEX]))
4921 return 0; /* same as default */
4922 else
4923 test_caps |= TTY_CAP_INVERSE;
4927 /* Color testing. */
4929 /* Check if foreground color is close enough. */
4930 fg = attrs[LFACE_FOREGROUND_INDEX];
4931 if (STRINGP (fg))
4933 Lisp_Object def_fg = def_attrs[LFACE_FOREGROUND_INDEX];
4935 if (face_attr_equal_p (fg, def_fg))
4936 return 0; /* same as default */
4937 else if (! tty_lookup_color (f, fg, &fg_tty_color, &fg_std_color))
4938 return 0; /* not a valid color */
4939 else if (color_distance (&fg_tty_color, &fg_std_color)
4940 > TTY_SAME_COLOR_THRESHOLD)
4941 return 0; /* displayed color is too different */
4942 else
4943 /* Make sure the color is really different than the default. */
4945 XColor def_fg_color;
4946 if (tty_lookup_color (f, def_fg, &def_fg_color, 0)
4947 && (color_distance (&fg_tty_color, &def_fg_color)
4948 <= TTY_SAME_COLOR_THRESHOLD))
4949 return 0;
4953 /* Check if background color is close enough. */
4954 bg = attrs[LFACE_BACKGROUND_INDEX];
4955 if (STRINGP (bg))
4957 Lisp_Object def_bg = def_attrs[LFACE_BACKGROUND_INDEX];
4959 if (face_attr_equal_p (bg, def_bg))
4960 return 0; /* same as default */
4961 else if (! tty_lookup_color (f, bg, &bg_tty_color, &bg_std_color))
4962 return 0; /* not a valid color */
4963 else if (color_distance (&bg_tty_color, &bg_std_color)
4964 > TTY_SAME_COLOR_THRESHOLD)
4965 return 0; /* displayed color is too different */
4966 else
4967 /* Make sure the color is really different than the default. */
4969 XColor def_bg_color;
4970 if (tty_lookup_color (f, def_bg, &def_bg_color, 0)
4971 && (color_distance (&bg_tty_color, &def_bg_color)
4972 <= TTY_SAME_COLOR_THRESHOLD))
4973 return 0;
4977 /* If both foreground and background are requested, see if the
4978 distance between them is OK. We just check to see if the distance
4979 between the tty's foreground and background is close enough to the
4980 distance between the standard foreground and background. */
4981 if (STRINGP (fg) && STRINGP (bg))
4983 int delta_delta
4984 = (color_distance (&fg_std_color, &bg_std_color)
4985 - color_distance (&fg_tty_color, &bg_tty_color));
4986 if (delta_delta > TTY_SAME_COLOR_THRESHOLD
4987 || delta_delta < -TTY_SAME_COLOR_THRESHOLD)
4988 return 0;
4992 /* See if the capabilities we selected above are supported, with the
4993 given colors. */
4994 return tty_capable_p (FRAME_TTY (f), test_caps);
4998 DEFUN ("display-supports-face-attributes-p",
4999 Fdisplay_supports_face_attributes_p, Sdisplay_supports_face_attributes_p,
5000 1, 2, 0,
5001 doc: /* Return non-nil if all the face attributes in ATTRIBUTES are supported.
5002 The optional argument DISPLAY can be a display name, a frame, or
5003 nil (meaning the selected frame's display).
5005 The definition of `supported' is somewhat heuristic, but basically means
5006 that a face containing all the attributes in ATTRIBUTES, when merged
5007 with the default face for display, can be represented in a way that's
5009 \(1) different in appearance than the default face, and
5010 \(2) `close in spirit' to what the attributes specify, if not exact.
5012 Point (2) implies that a `:weight black' attribute will be satisfied by
5013 any display that can display bold, and a `:foreground \"yellow\"' as long
5014 as it can display a yellowish color, but `:slant italic' will _not_ be
5015 satisfied by the tty display code's automatic substitution of a `dim'
5016 face for italic. */)
5017 (Lisp_Object attributes, Lisp_Object display)
5019 bool supports = 0;
5020 int i;
5021 Lisp_Object frame;
5022 struct frame *f;
5023 struct face *def_face;
5024 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5026 if (noninteractive || !initialized)
5027 /* We may not be able to access low-level face information in batch
5028 mode, or before being dumped, and this function is not going to
5029 be very useful in those cases anyway, so just give up. */
5030 return Qnil;
5032 if (NILP (display))
5033 frame = selected_frame;
5034 else if (FRAMEP (display))
5035 frame = display;
5036 else
5038 /* Find any frame on DISPLAY. */
5039 Lisp_Object tail;
5041 frame = Qnil;
5042 FOR_EACH_FRAME (tail, frame)
5043 if (!NILP (Fequal (Fcdr (Fassq (Qdisplay,
5044 XFRAME (frame)->param_alist)),
5045 display)))
5046 break;
5049 CHECK_LIVE_FRAME (frame);
5050 f = XFRAME (frame);
5052 for (i = 0; i < LFACE_VECTOR_SIZE; i++)
5053 attrs[i] = Qunspecified;
5054 merge_face_ref (f, attributes, attrs, 1, 0);
5056 def_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5057 if (def_face == NULL)
5059 if (! realize_basic_faces (f))
5060 error ("Cannot realize default face");
5061 def_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5062 if (def_face == NULL)
5063 emacs_abort (); /* realize_basic_faces must have set it up */
5066 /* Dispatch to the appropriate handler. */
5067 if (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
5068 supports = tty_supports_face_attributes_p (f, attrs, def_face);
5069 #ifdef HAVE_WINDOW_SYSTEM
5070 else
5071 supports = x_supports_face_attributes_p (f, attrs, def_face);
5072 #endif
5074 return supports ? Qt : Qnil;
5078 /***********************************************************************
5079 Font selection
5080 ***********************************************************************/
5082 DEFUN ("internal-set-font-selection-order",
5083 Finternal_set_font_selection_order,
5084 Sinternal_set_font_selection_order, 1, 1, 0,
5085 doc: /* Set font selection order for face font selection to ORDER.
5086 ORDER must be a list of length 4 containing the symbols `:width',
5087 `:height', `:weight', and `:slant'. Face attributes appearing
5088 first in ORDER are matched first, e.g. if `:height' appears before
5089 `:weight' in ORDER, font selection first tries to find a font with
5090 a suitable height, and then tries to match the font weight.
5091 Value is ORDER. */)
5092 (Lisp_Object order)
5094 Lisp_Object list;
5095 int i;
5096 int indices[DIM (font_sort_order)];
5098 CHECK_LIST (order);
5099 memset (indices, 0, sizeof indices);
5100 i = 0;
5102 for (list = order;
5103 CONSP (list) && i < DIM (indices);
5104 list = XCDR (list), ++i)
5106 Lisp_Object attr = XCAR (list);
5107 int xlfd;
5109 if (EQ (attr, QCwidth))
5110 xlfd = XLFD_SWIDTH;
5111 else if (EQ (attr, QCheight))
5112 xlfd = XLFD_POINT_SIZE;
5113 else if (EQ (attr, QCweight))
5114 xlfd = XLFD_WEIGHT;
5115 else if (EQ (attr, QCslant))
5116 xlfd = XLFD_SLANT;
5117 else
5118 break;
5120 if (indices[i] != 0)
5121 break;
5122 indices[i] = xlfd;
5125 if (!NILP (list) || i != DIM (indices))
5126 signal_error ("Invalid font sort order", order);
5127 for (i = 0; i < DIM (font_sort_order); ++i)
5128 if (indices[i] == 0)
5129 signal_error ("Invalid font sort order", order);
5131 if (memcmp (indices, font_sort_order, sizeof indices) != 0)
5133 memcpy (font_sort_order, indices, sizeof font_sort_order);
5134 free_all_realized_faces (Qnil);
5137 font_update_sort_order (font_sort_order);
5139 return Qnil;
5143 DEFUN ("internal-set-alternative-font-family-alist",
5144 Finternal_set_alternative_font_family_alist,
5145 Sinternal_set_alternative_font_family_alist, 1, 1, 0,
5146 doc: /* Define alternative font families to try in face font selection.
5147 ALIST is an alist of (FAMILY ALTERNATIVE1 ALTERNATIVE2 ...) entries.
5148 Each ALTERNATIVE is tried in order if no fonts of font family FAMILY can
5149 be found. Value is ALIST. */)
5150 (Lisp_Object alist)
5152 Lisp_Object entry, tail, tail2;
5154 CHECK_LIST (alist);
5155 alist = Fcopy_sequence (alist);
5156 for (tail = alist; CONSP (tail); tail = XCDR (tail))
5158 entry = XCAR (tail);
5159 CHECK_LIST (entry);
5160 entry = Fcopy_sequence (entry);
5161 XSETCAR (tail, entry);
5162 for (tail2 = entry; CONSP (tail2); tail2 = XCDR (tail2))
5163 XSETCAR (tail2, Fintern (XCAR (tail2), Qnil));
5166 Vface_alternative_font_family_alist = alist;
5167 free_all_realized_faces (Qnil);
5168 return alist;
5172 DEFUN ("internal-set-alternative-font-registry-alist",
5173 Finternal_set_alternative_font_registry_alist,
5174 Sinternal_set_alternative_font_registry_alist, 1, 1, 0,
5175 doc: /* Define alternative font registries to try in face font selection.
5176 ALIST is an alist of (REGISTRY ALTERNATIVE1 ALTERNATIVE2 ...) entries.
5177 Each ALTERNATIVE is tried in order if no fonts of font registry REGISTRY can
5178 be found. Value is ALIST. */)
5179 (Lisp_Object alist)
5181 Lisp_Object entry, tail, tail2;
5183 CHECK_LIST (alist);
5184 alist = Fcopy_sequence (alist);
5185 for (tail = alist; CONSP (tail); tail = XCDR (tail))
5187 entry = XCAR (tail);
5188 CHECK_LIST (entry);
5189 entry = Fcopy_sequence (entry);
5190 XSETCAR (tail, entry);
5191 for (tail2 = entry; CONSP (tail2); tail2 = XCDR (tail2))
5192 XSETCAR (tail2, Fdowncase (XCAR (tail2)));
5194 Vface_alternative_font_registry_alist = alist;
5195 free_all_realized_faces (Qnil);
5196 return alist;
5200 #ifdef HAVE_WINDOW_SYSTEM
5202 /* Return the fontset id of the base fontset name or alias name given
5203 by the fontset attribute of ATTRS. Value is -1 if the fontset
5204 attribute of ATTRS doesn't name a fontset. */
5206 static int
5207 face_fontset (Lisp_Object attrs[LFACE_VECTOR_SIZE])
5209 Lisp_Object name;
5211 name = attrs[LFACE_FONTSET_INDEX];
5212 if (!STRINGP (name))
5213 return -1;
5214 return fs_query_fontset (name, 0);
5217 #endif /* HAVE_WINDOW_SYSTEM */
5221 /***********************************************************************
5222 Face Realization
5223 ***********************************************************************/
5225 /* Realize basic faces on frame F. Value is zero if frame parameters
5226 of F don't contain enough information needed to realize the default
5227 face. */
5229 static bool
5230 realize_basic_faces (struct frame *f)
5232 bool success_p = 0;
5233 ptrdiff_t count = SPECPDL_INDEX ();
5235 /* Block input here so that we won't be surprised by an X expose
5236 event, for instance, without having the faces set up. */
5237 block_input ();
5238 specbind (Qscalable_fonts_allowed, Qt);
5240 if (realize_default_face (f))
5242 realize_named_face (f, Qmode_line, MODE_LINE_FACE_ID);
5243 realize_named_face (f, Qmode_line_inactive, MODE_LINE_INACTIVE_FACE_ID);
5244 realize_named_face (f, Qtool_bar, TOOL_BAR_FACE_ID);
5245 realize_named_face (f, Qfringe, FRINGE_FACE_ID);
5246 realize_named_face (f, Qheader_line, HEADER_LINE_FACE_ID);
5247 realize_named_face (f, Qscroll_bar, SCROLL_BAR_FACE_ID);
5248 realize_named_face (f, Qborder, BORDER_FACE_ID);
5249 realize_named_face (f, Qcursor, CURSOR_FACE_ID);
5250 realize_named_face (f, Qmouse, MOUSE_FACE_ID);
5251 realize_named_face (f, Qmenu, MENU_FACE_ID);
5252 realize_named_face (f, Qvertical_border, VERTICAL_BORDER_FACE_ID);
5253 realize_named_face (f, Qwindow_divider, WINDOW_DIVIDER_FACE_ID);
5254 realize_named_face (f, Qwindow_divider_first_pixel,
5255 WINDOW_DIVIDER_FIRST_PIXEL_FACE_ID);
5256 realize_named_face (f, Qwindow_divider_last_pixel,
5257 WINDOW_DIVIDER_LAST_PIXEL_FACE_ID);
5259 /* Reflect changes in the `menu' face in menu bars. */
5260 if (FRAME_FACE_CACHE (f)->menu_face_changed_p)
5262 FRAME_FACE_CACHE (f)->menu_face_changed_p = 0;
5263 #ifdef USE_X_TOOLKIT
5264 if (FRAME_WINDOW_P (f))
5265 x_update_menu_appearance (f);
5266 #endif
5269 success_p = 1;
5272 unbind_to (count, Qnil);
5273 unblock_input ();
5274 return success_p;
5278 /* Realize the default face on frame F. If the face is not fully
5279 specified, make it fully-specified. Attributes of the default face
5280 that are not explicitly specified are taken from frame parameters. */
5282 static bool
5283 realize_default_face (struct frame *f)
5285 struct face_cache *c = FRAME_FACE_CACHE (f);
5286 Lisp_Object lface;
5287 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5288 struct face *face;
5290 /* If the `default' face is not yet known, create it. */
5291 lface = lface_from_face_name (f, Qdefault, 0);
5292 if (NILP (lface))
5294 Lisp_Object frame;
5295 XSETFRAME (frame, f);
5296 lface = Finternal_make_lisp_face (Qdefault, frame);
5299 #ifdef HAVE_WINDOW_SYSTEM
5300 if (FRAME_WINDOW_P (f))
5302 Lisp_Object font_object;
5304 XSETFONT (font_object, FRAME_FONT (f));
5305 set_lface_from_font (f, lface, font_object, f->default_face_done_p);
5306 ASET (lface, LFACE_FONTSET_INDEX, fontset_name (FRAME_FONTSET (f)));
5307 f->default_face_done_p = 1;
5309 #endif /* HAVE_WINDOW_SYSTEM */
5311 if (!FRAME_WINDOW_P (f))
5313 ASET (lface, LFACE_FAMILY_INDEX, build_string ("default"));
5314 ASET (lface, LFACE_FOUNDRY_INDEX, LFACE_FAMILY (lface));
5315 ASET (lface, LFACE_SWIDTH_INDEX, Qnormal);
5316 ASET (lface, LFACE_HEIGHT_INDEX, make_number (1));
5317 if (UNSPECIFIEDP (LFACE_WEIGHT (lface)))
5318 ASET (lface, LFACE_WEIGHT_INDEX, Qnormal);
5319 if (UNSPECIFIEDP (LFACE_SLANT (lface)))
5320 ASET (lface, LFACE_SLANT_INDEX, Qnormal);
5321 if (UNSPECIFIEDP (LFACE_FONTSET (lface)))
5322 ASET (lface, LFACE_FONTSET_INDEX, Qnil);
5325 if (UNSPECIFIEDP (LFACE_UNDERLINE (lface)))
5326 ASET (lface, LFACE_UNDERLINE_INDEX, Qnil);
5328 if (UNSPECIFIEDP (LFACE_OVERLINE (lface)))
5329 ASET (lface, LFACE_OVERLINE_INDEX, Qnil);
5331 if (UNSPECIFIEDP (LFACE_STRIKE_THROUGH (lface)))
5332 ASET (lface, LFACE_STRIKE_THROUGH_INDEX, Qnil);
5334 if (UNSPECIFIEDP (LFACE_BOX (lface)))
5335 ASET (lface, LFACE_BOX_INDEX, Qnil);
5337 if (UNSPECIFIEDP (LFACE_INVERSE (lface)))
5338 ASET (lface, LFACE_INVERSE_INDEX, Qnil);
5340 if (UNSPECIFIEDP (LFACE_FOREGROUND (lface)))
5342 /* This function is called so early that colors are not yet
5343 set in the frame parameter list. */
5344 Lisp_Object color = Fassq (Qforeground_color, f->param_alist);
5346 if (CONSP (color) && STRINGP (XCDR (color)))
5347 ASET (lface, LFACE_FOREGROUND_INDEX, XCDR (color));
5348 else if (FRAME_WINDOW_P (f))
5349 return 0;
5350 else if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
5351 ASET (lface, LFACE_FOREGROUND_INDEX, build_string (unspecified_fg));
5352 else
5353 emacs_abort ();
5356 if (UNSPECIFIEDP (LFACE_BACKGROUND (lface)))
5358 /* This function is called so early that colors are not yet
5359 set in the frame parameter list. */
5360 Lisp_Object color = Fassq (Qbackground_color, f->param_alist);
5361 if (CONSP (color) && STRINGP (XCDR (color)))
5362 ASET (lface, LFACE_BACKGROUND_INDEX, XCDR (color));
5363 else if (FRAME_WINDOW_P (f))
5364 return 0;
5365 else if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
5366 ASET (lface, LFACE_BACKGROUND_INDEX, build_string (unspecified_bg));
5367 else
5368 emacs_abort ();
5371 if (UNSPECIFIEDP (LFACE_STIPPLE (lface)))
5372 ASET (lface, LFACE_STIPPLE_INDEX, Qnil);
5374 /* Realize the face; it must be fully-specified now. */
5375 eassert (lface_fully_specified_p (XVECTOR (lface)->contents));
5376 check_lface (lface);
5377 memcpy (attrs, XVECTOR (lface)->contents, sizeof attrs);
5378 face = realize_face (c, attrs, DEFAULT_FACE_ID);
5380 #ifdef HAVE_WINDOW_SYSTEM
5381 #ifdef HAVE_X_WINDOWS
5382 if (FRAME_X_P (f) && face->font != FRAME_FONT (f))
5384 /* This can happen when making a frame on a display that does
5385 not support the default font. */
5386 if (!face->font)
5387 return 0;
5389 /* Otherwise, the font specified for the frame was not
5390 acceptable as a font for the default face (perhaps because
5391 auto-scaled fonts are rejected), so we must adjust the frame
5392 font. */
5393 x_set_font (f, LFACE_FONT (lface), Qnil);
5395 #endif /* HAVE_X_WINDOWS */
5396 #endif /* HAVE_WINDOW_SYSTEM */
5397 return 1;
5401 /* Realize basic faces other than the default face in face cache C.
5402 SYMBOL is the face name, ID is the face id the realized face must
5403 have. The default face must have been realized already. */
5405 static void
5406 realize_named_face (struct frame *f, Lisp_Object symbol, int id)
5408 struct face_cache *c = FRAME_FACE_CACHE (f);
5409 Lisp_Object lface = lface_from_face_name (f, symbol, 0);
5410 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5411 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
5413 /* The default face must exist and be fully specified. */
5414 get_lface_attributes_no_remap (f, Qdefault, attrs, 1);
5415 check_lface_attrs (attrs);
5416 eassert (lface_fully_specified_p (attrs));
5418 /* If SYMBOL isn't know as a face, create it. */
5419 if (NILP (lface))
5421 Lisp_Object frame;
5422 XSETFRAME (frame, f);
5423 lface = Finternal_make_lisp_face (symbol, frame);
5426 /* Merge SYMBOL's face with the default face. */
5427 get_lface_attributes_no_remap (f, symbol, symbol_attrs, 1);
5428 merge_face_vectors (f, symbol_attrs, attrs, 0);
5430 /* Realize the face. */
5431 realize_face (c, attrs, id);
5435 /* Realize the fully-specified face with attributes ATTRS in face
5436 cache CACHE for ASCII characters. If FORMER_FACE_ID is
5437 non-negative, it is an ID of face to remove before caching the new
5438 face. Value is a pointer to the newly created realized face. */
5440 static struct face *
5441 realize_face (struct face_cache *cache, Lisp_Object attrs[LFACE_VECTOR_SIZE],
5442 int former_face_id)
5444 struct face *face;
5446 /* LFACE must be fully specified. */
5447 eassert (cache != NULL);
5448 check_lface_attrs (attrs);
5450 if (former_face_id >= 0 && cache->used > former_face_id)
5452 /* Remove the former face. */
5453 struct face *former_face = cache->faces_by_id[former_face_id];
5454 uncache_face (cache, former_face);
5455 free_realized_face (cache->f, former_face);
5456 SET_FRAME_GARBAGED (cache->f);
5459 if (FRAME_WINDOW_P (cache->f))
5460 face = realize_x_face (cache, attrs);
5461 else if (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f))
5462 face = realize_tty_face (cache, attrs);
5463 else if (FRAME_INITIAL_P (cache->f))
5465 /* Create a dummy face. */
5466 face = make_realized_face (attrs);
5468 else
5469 emacs_abort ();
5471 /* Insert the new face. */
5472 cache_face (cache, face, lface_hash (attrs));
5473 return face;
5477 #ifdef HAVE_WINDOW_SYSTEM
5478 /* Realize the fully-specified face that uses FONT-OBJECT and has the
5479 same attributes as BASE_FACE except for the font on frame F.
5480 FONT-OBJECT may be nil, in which case, realized a face of
5481 no-font. */
5483 static struct face *
5484 realize_non_ascii_face (struct frame *f, Lisp_Object font_object,
5485 struct face *base_face)
5487 struct face_cache *cache = FRAME_FACE_CACHE (f);
5488 struct face *face;
5490 face = xmalloc (sizeof *face);
5491 *face = *base_face;
5492 face->gc = 0;
5493 face->extra = NULL;
5494 face->overstrike
5495 = (! NILP (font_object)
5496 && FONT_WEIGHT_NAME_NUMERIC (face->lface[LFACE_WEIGHT_INDEX]) > 100
5497 && FONT_WEIGHT_NUMERIC (font_object) <= 100);
5499 /* Don't try to free the colors copied bitwise from BASE_FACE. */
5500 face->colors_copied_bitwise_p = 1;
5501 face->font = NILP (font_object) ? NULL : XFONT_OBJECT (font_object);
5502 face->gc = 0;
5504 cache_face (cache, face, face->hash);
5506 return face;
5508 #endif /* HAVE_WINDOW_SYSTEM */
5511 /* Realize the fully-specified face with attributes ATTRS in face
5512 cache CACHE for ASCII characters. Do it for X frame CACHE->f. If
5513 the new face doesn't share font with the default face, a fontname
5514 is allocated from the heap and set in `font_name' of the new face,
5515 but it is not yet loaded here. Value is a pointer to the newly
5516 created realized face. */
5518 static struct face *
5519 realize_x_face (struct face_cache *cache, Lisp_Object attrs[LFACE_VECTOR_SIZE])
5521 struct face *face = NULL;
5522 #ifdef HAVE_WINDOW_SYSTEM
5523 struct face *default_face;
5524 struct frame *f;
5525 Lisp_Object stipple, underline, overline, strike_through, box;
5527 eassert (FRAME_WINDOW_P (cache->f));
5529 /* Allocate a new realized face. */
5530 face = make_realized_face (attrs);
5531 face->ascii_face = face;
5533 f = cache->f;
5535 /* Determine the font to use. Most of the time, the font will be
5536 the same as the font of the default face, so try that first. */
5537 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5538 if (default_face
5539 && lface_same_font_attributes_p (default_face->lface, attrs))
5541 face->font = default_face->font;
5542 face->fontset
5543 = make_fontset_for_ascii_face (f, default_face->fontset, face);
5545 else
5547 /* If the face attribute ATTRS specifies a fontset, use it as
5548 the base of a new realized fontset. Otherwise, use the same
5549 base fontset as of the default face. The base determines
5550 registry and encoding of a font. It may also determine
5551 foundry and family. The other fields of font name pattern
5552 are constructed from ATTRS. */
5553 int fontset = face_fontset (attrs);
5555 /* If we are realizing the default face, ATTRS should specify a
5556 fontset. In other words, if FONTSET is -1, we are not
5557 realizing the default face, thus the default face should have
5558 already been realized. */
5559 if (fontset == -1)
5561 if (default_face)
5562 fontset = default_face->fontset;
5563 if (fontset == -1)
5564 emacs_abort ();
5566 if (! FONT_OBJECT_P (attrs[LFACE_FONT_INDEX]))
5567 attrs[LFACE_FONT_INDEX]
5568 = font_load_for_lface (f, attrs, attrs[LFACE_FONT_INDEX]);
5569 if (FONT_OBJECT_P (attrs[LFACE_FONT_INDEX]))
5571 face->font = XFONT_OBJECT (attrs[LFACE_FONT_INDEX]);
5572 face->fontset = make_fontset_for_ascii_face (f, fontset, face);
5574 else
5576 face->font = NULL;
5577 face->fontset = -1;
5581 if (face->font
5582 && FONT_WEIGHT_NAME_NUMERIC (attrs[LFACE_WEIGHT_INDEX]) > 100
5583 && FONT_WEIGHT_NUMERIC (attrs[LFACE_FONT_INDEX]) <= 100)
5584 face->overstrike = 1;
5586 /* Load colors, and set remaining attributes. */
5588 load_face_colors (f, face, attrs);
5590 /* Set up box. */
5591 box = attrs[LFACE_BOX_INDEX];
5592 if (STRINGP (box))
5594 /* A simple box of line width 1 drawn in color given by
5595 the string. */
5596 face->box_color = load_color (f, face, attrs[LFACE_BOX_INDEX],
5597 LFACE_BOX_INDEX);
5598 face->box = FACE_SIMPLE_BOX;
5599 face->box_line_width = 1;
5601 else if (INTEGERP (box))
5603 /* Simple box of specified line width in foreground color of the
5604 face. */
5605 eassert (XINT (box) != 0);
5606 face->box = FACE_SIMPLE_BOX;
5607 face->box_line_width = XINT (box);
5608 face->box_color = face->foreground;
5609 face->box_color_defaulted_p = 1;
5611 else if (CONSP (box))
5613 /* `(:width WIDTH :color COLOR :shadow SHADOW)'. SHADOW
5614 being one of `raised' or `sunken'. */
5615 face->box = FACE_SIMPLE_BOX;
5616 face->box_color = face->foreground;
5617 face->box_color_defaulted_p = 1;
5618 face->box_line_width = 1;
5620 while (CONSP (box))
5622 Lisp_Object keyword, value;
5624 keyword = XCAR (box);
5625 box = XCDR (box);
5627 if (!CONSP (box))
5628 break;
5629 value = XCAR (box);
5630 box = XCDR (box);
5632 if (EQ (keyword, QCline_width))
5634 if (INTEGERP (value) && XINT (value) != 0)
5635 face->box_line_width = XINT (value);
5637 else if (EQ (keyword, QCcolor))
5639 if (STRINGP (value))
5641 face->box_color = load_color (f, face, value,
5642 LFACE_BOX_INDEX);
5643 face->use_box_color_for_shadows_p = 1;
5646 else if (EQ (keyword, QCstyle))
5648 if (EQ (value, Qreleased_button))
5649 face->box = FACE_RAISED_BOX;
5650 else if (EQ (value, Qpressed_button))
5651 face->box = FACE_SUNKEN_BOX;
5656 /* Text underline, overline, strike-through. */
5658 underline = attrs[LFACE_UNDERLINE_INDEX];
5659 if (EQ (underline, Qt))
5661 /* Use default color (same as foreground color). */
5662 face->underline_p = 1;
5663 face->underline_type = FACE_UNDER_LINE;
5664 face->underline_defaulted_p = 1;
5665 face->underline_color = 0;
5667 else if (STRINGP (underline))
5669 /* Use specified color. */
5670 face->underline_p = 1;
5671 face->underline_type = FACE_UNDER_LINE;
5672 face->underline_defaulted_p = 0;
5673 face->underline_color
5674 = load_color (f, face, underline,
5675 LFACE_UNDERLINE_INDEX);
5677 else if (NILP (underline))
5679 face->underline_p = 0;
5680 face->underline_defaulted_p = 0;
5681 face->underline_color = 0;
5683 else if (CONSP (underline))
5685 /* `(:color COLOR :style STYLE)'.
5686 STYLE being one of `line' or `wave'. */
5687 face->underline_p = 1;
5688 face->underline_color = 0;
5689 face->underline_defaulted_p = 1;
5690 face->underline_type = FACE_UNDER_LINE;
5692 /* FIXME? This is also not robust about checking the precise form.
5693 See comments in Finternal_set_lisp_face_attribute. */
5694 while (CONSP (underline))
5696 Lisp_Object keyword, value;
5698 keyword = XCAR (underline);
5699 underline = XCDR (underline);
5701 if (!CONSP (underline))
5702 break;
5703 value = XCAR (underline);
5704 underline = XCDR (underline);
5706 if (EQ (keyword, QCcolor))
5708 if (EQ (value, Qforeground_color))
5710 face->underline_defaulted_p = 1;
5711 face->underline_color = 0;
5713 else if (STRINGP (value))
5715 face->underline_defaulted_p = 0;
5716 face->underline_color = load_color (f, face, value,
5717 LFACE_UNDERLINE_INDEX);
5720 else if (EQ (keyword, QCstyle))
5722 if (EQ (value, Qline))
5723 face->underline_type = FACE_UNDER_LINE;
5724 else if (EQ (value, Qwave))
5725 face->underline_type = FACE_UNDER_WAVE;
5730 overline = attrs[LFACE_OVERLINE_INDEX];
5731 if (STRINGP (overline))
5733 face->overline_color
5734 = load_color (f, face, attrs[LFACE_OVERLINE_INDEX],
5735 LFACE_OVERLINE_INDEX);
5736 face->overline_p = 1;
5738 else if (EQ (overline, Qt))
5740 face->overline_color = face->foreground;
5741 face->overline_color_defaulted_p = 1;
5742 face->overline_p = 1;
5745 strike_through = attrs[LFACE_STRIKE_THROUGH_INDEX];
5746 if (STRINGP (strike_through))
5748 face->strike_through_color
5749 = load_color (f, face, attrs[LFACE_STRIKE_THROUGH_INDEX],
5750 LFACE_STRIKE_THROUGH_INDEX);
5751 face->strike_through_p = 1;
5753 else if (EQ (strike_through, Qt))
5755 face->strike_through_color = face->foreground;
5756 face->strike_through_color_defaulted_p = 1;
5757 face->strike_through_p = 1;
5760 stipple = attrs[LFACE_STIPPLE_INDEX];
5761 if (!NILP (stipple))
5762 face->stipple = load_pixmap (f, stipple);
5763 #endif /* HAVE_WINDOW_SYSTEM */
5765 return face;
5769 /* Map a specified color of face FACE on frame F to a tty color index.
5770 IDX is either LFACE_FOREGROUND_INDEX or LFACE_BACKGROUND_INDEX, and
5771 specifies which color to map. Set *DEFAULTED to 1 if mapping to the
5772 default foreground/background colors. */
5774 static void
5775 map_tty_color (struct frame *f, struct face *face,
5776 enum lface_attribute_index idx, int *defaulted)
5778 Lisp_Object frame, color, def;
5779 int foreground_p = idx == LFACE_FOREGROUND_INDEX;
5780 unsigned long default_pixel =
5781 foreground_p ? FACE_TTY_DEFAULT_FG_COLOR : FACE_TTY_DEFAULT_BG_COLOR;
5782 unsigned long pixel = default_pixel;
5783 #ifdef MSDOS
5784 unsigned long default_other_pixel =
5785 foreground_p ? FACE_TTY_DEFAULT_BG_COLOR : FACE_TTY_DEFAULT_FG_COLOR;
5786 #endif
5788 eassert (idx == LFACE_FOREGROUND_INDEX || idx == LFACE_BACKGROUND_INDEX);
5790 XSETFRAME (frame, f);
5791 color = face->lface[idx];
5793 if (STRINGP (color)
5794 && SCHARS (color)
5795 && CONSP (Vtty_defined_color_alist)
5796 && (def = assq_no_quit (color, call1 (Qtty_color_alist, frame)),
5797 CONSP (def)))
5799 /* Associations in tty-defined-color-alist are of the form
5800 (NAME INDEX R G B). We need the INDEX part. */
5801 pixel = XINT (XCAR (XCDR (def)));
5804 if (pixel == default_pixel && STRINGP (color))
5806 pixel = load_color (f, face, color, idx);
5808 #ifdef MSDOS
5809 /* If the foreground of the default face is the default color,
5810 use the foreground color defined by the frame. */
5811 if (FRAME_MSDOS_P (f))
5813 if (pixel == default_pixel
5814 || pixel == FACE_TTY_DEFAULT_COLOR)
5816 if (foreground_p)
5817 pixel = FRAME_FOREGROUND_PIXEL (f);
5818 else
5819 pixel = FRAME_BACKGROUND_PIXEL (f);
5820 face->lface[idx] = tty_color_name (f, pixel);
5821 *defaulted = 1;
5823 else if (pixel == default_other_pixel)
5825 if (foreground_p)
5826 pixel = FRAME_BACKGROUND_PIXEL (f);
5827 else
5828 pixel = FRAME_FOREGROUND_PIXEL (f);
5829 face->lface[idx] = tty_color_name (f, pixel);
5830 *defaulted = 1;
5833 #endif /* MSDOS */
5836 if (foreground_p)
5837 face->foreground = pixel;
5838 else
5839 face->background = pixel;
5843 /* Realize the fully-specified face with attributes ATTRS in face
5844 cache CACHE for ASCII characters. Do it for TTY frame CACHE->f.
5845 Value is a pointer to the newly created realized face. */
5847 static struct face *
5848 realize_tty_face (struct face_cache *cache,
5849 Lisp_Object attrs[LFACE_VECTOR_SIZE])
5851 struct face *face;
5852 int weight, slant;
5853 int face_colors_defaulted = 0;
5854 struct frame *f = cache->f;
5856 /* Frame must be a termcap frame. */
5857 eassert (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f));
5859 /* Allocate a new realized face. */
5860 face = make_realized_face (attrs);
5861 #if 0
5862 face->font_name = FRAME_MSDOS_P (cache->f) ? "ms-dos" : "tty";
5863 #endif
5865 /* Map face attributes to TTY appearances. */
5866 weight = FONT_WEIGHT_NAME_NUMERIC (attrs[LFACE_WEIGHT_INDEX]);
5867 slant = FONT_SLANT_NAME_NUMERIC (attrs[LFACE_SLANT_INDEX]);
5868 if (weight > 100)
5869 face->tty_bold_p = 1;
5870 if (slant != 100)
5871 face->tty_italic_p = 1;
5872 if (!NILP (attrs[LFACE_UNDERLINE_INDEX]))
5873 face->tty_underline_p = 1;
5874 if (!NILP (attrs[LFACE_INVERSE_INDEX]))
5875 face->tty_reverse_p = 1;
5877 /* Map color names to color indices. */
5878 map_tty_color (f, face, LFACE_FOREGROUND_INDEX, &face_colors_defaulted);
5879 map_tty_color (f, face, LFACE_BACKGROUND_INDEX, &face_colors_defaulted);
5881 /* Swap colors if face is inverse-video. If the colors are taken
5882 from the frame colors, they are already inverted, since the
5883 frame-creation function calls x-handle-reverse-video. */
5884 if (face->tty_reverse_p && !face_colors_defaulted)
5886 unsigned long tem = face->foreground;
5887 face->foreground = face->background;
5888 face->background = tem;
5891 if (tty_suppress_bold_inverse_default_colors_p
5892 && face->tty_bold_p
5893 && face->background == FACE_TTY_DEFAULT_FG_COLOR
5894 && face->foreground == FACE_TTY_DEFAULT_BG_COLOR)
5895 face->tty_bold_p = 0;
5897 return face;
5901 DEFUN ("tty-suppress-bold-inverse-default-colors",
5902 Ftty_suppress_bold_inverse_default_colors,
5903 Stty_suppress_bold_inverse_default_colors, 1, 1, 0,
5904 doc: /* Suppress/allow boldness of faces with inverse default colors.
5905 SUPPRESS non-nil means suppress it.
5906 This affects bold faces on TTYs whose foreground is the default background
5907 color of the display and whose background is the default foreground color.
5908 For such faces, the bold face attribute is ignored if this variable
5909 is non-nil. */)
5910 (Lisp_Object suppress)
5912 tty_suppress_bold_inverse_default_colors_p = !NILP (suppress);
5913 ++face_change_count;
5914 return suppress;
5919 /***********************************************************************
5920 Computing Faces
5921 ***********************************************************************/
5923 /* Return the ID of the face to use to display character CH with face
5924 property PROP on frame F in current_buffer. */
5927 compute_char_face (struct frame *f, int ch, Lisp_Object prop)
5929 int face_id;
5931 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
5932 ch = 0;
5934 if (NILP (prop))
5936 struct face *face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5937 face_id = FACE_FOR_CHAR (f, face, ch, -1, Qnil);
5939 else
5941 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5942 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5943 memcpy (attrs, default_face->lface, sizeof attrs);
5944 merge_face_ref (f, prop, attrs, 1, 0);
5945 face_id = lookup_face (f, attrs);
5948 return face_id;
5951 /* Return the face ID associated with buffer position POS for
5952 displaying ASCII characters. Return in *ENDPTR the position at
5953 which a different face is needed, as far as text properties and
5954 overlays are concerned. W is a window displaying current_buffer.
5956 REGION_BEG, REGION_END delimit the region, so it can be
5957 highlighted.
5959 LIMIT is a position not to scan beyond. That is to limit the time
5960 this function can take.
5962 If MOUSE is non-zero, use the character's mouse-face, not its face.
5964 BASE_FACE_ID, if non-negative, specifies a base face id to use
5965 instead of DEFAULT_FACE_ID.
5967 The face returned is suitable for displaying ASCII characters. */
5970 face_at_buffer_position (struct window *w, ptrdiff_t pos,
5971 ptrdiff_t *endptr, ptrdiff_t limit,
5972 int mouse, int base_face_id)
5974 struct frame *f = XFRAME (w->frame);
5975 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5976 Lisp_Object prop, position;
5977 ptrdiff_t i, noverlays;
5978 Lisp_Object *overlay_vec;
5979 ptrdiff_t endpos;
5980 Lisp_Object propname = mouse ? Qmouse_face : Qface;
5981 Lisp_Object limit1, end;
5982 struct face *default_face;
5984 /* W must display the current buffer. We could write this function
5985 to use the frame and buffer of W, but right now it doesn't. */
5986 /* eassert (XBUFFER (w->contents) == current_buffer); */
5988 XSETFASTINT (position, pos);
5990 endpos = ZV;
5992 /* Get the `face' or `mouse_face' text property at POS, and
5993 determine the next position at which the property changes. */
5994 prop = Fget_text_property (position, propname, w->contents);
5995 XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
5996 end = Fnext_single_property_change (position, propname, w->contents, limit1);
5997 if (INTEGERP (end))
5998 endpos = XINT (end);
6000 /* Look at properties from overlays. */
6002 ptrdiff_t next_overlay;
6004 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, &next_overlay, 0);
6005 if (next_overlay < endpos)
6006 endpos = next_overlay;
6009 *endptr = endpos;
6012 int face_id;
6014 if (base_face_id >= 0)
6015 face_id = base_face_id;
6016 else if (NILP (Vface_remapping_alist))
6017 face_id = DEFAULT_FACE_ID;
6018 else
6019 face_id = lookup_basic_face (f, DEFAULT_FACE_ID);
6021 default_face = FACE_FROM_ID (f, face_id);
6024 /* Optimize common cases where we can use the default face. */
6025 if (noverlays == 0
6026 && NILP (prop))
6027 return default_face->id;
6029 /* Begin with attributes from the default face. */
6030 memcpy (attrs, default_face->lface, sizeof attrs);
6032 /* Merge in attributes specified via text properties. */
6033 if (!NILP (prop))
6034 merge_face_ref (f, prop, attrs, 1, 0);
6036 /* Now merge the overlay data. */
6037 noverlays = sort_overlays (overlay_vec, noverlays, w);
6038 for (i = 0; i < noverlays; i++)
6040 Lisp_Object oend;
6041 ptrdiff_t oendpos;
6043 prop = Foverlay_get (overlay_vec[i], propname);
6044 if (!NILP (prop))
6045 merge_face_ref (f, prop, attrs, 1, 0);
6047 oend = OVERLAY_END (overlay_vec[i]);
6048 oendpos = OVERLAY_POSITION (oend);
6049 if (oendpos < endpos)
6050 endpos = oendpos;
6053 *endptr = endpos;
6055 /* Look up a realized face with the given face attributes,
6056 or realize a new one for ASCII characters. */
6057 return lookup_face (f, attrs);
6060 /* Return the face ID at buffer position POS for displaying ASCII
6061 characters associated with overlay strings for overlay OVERLAY.
6063 Like face_at_buffer_position except for OVERLAY. Currently it
6064 simply disregards the `face' properties of all overlays. */
6067 face_for_overlay_string (struct window *w, ptrdiff_t pos,
6068 ptrdiff_t *endptr, ptrdiff_t limit,
6069 int mouse, Lisp_Object overlay)
6071 struct frame *f = XFRAME (w->frame);
6072 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6073 Lisp_Object prop, position;
6074 ptrdiff_t endpos;
6075 Lisp_Object propname = mouse ? Qmouse_face : Qface;
6076 Lisp_Object limit1, end;
6077 struct face *default_face;
6079 /* W must display the current buffer. We could write this function
6080 to use the frame and buffer of W, but right now it doesn't. */
6081 /* eassert (XBUFFER (w->contents) == current_buffer); */
6083 XSETFASTINT (position, pos);
6085 endpos = ZV;
6087 /* Get the `face' or `mouse_face' text property at POS, and
6088 determine the next position at which the property changes. */
6089 prop = Fget_text_property (position, propname, w->contents);
6090 XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
6091 end = Fnext_single_property_change (position, propname, w->contents, limit1);
6092 if (INTEGERP (end))
6093 endpos = XINT (end);
6095 *endptr = endpos;
6097 /* Optimize common case where we can use the default face. */
6098 if (NILP (prop)
6099 && NILP (Vface_remapping_alist))
6100 return DEFAULT_FACE_ID;
6102 /* Begin with attributes from the default face. */
6103 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
6104 memcpy (attrs, default_face->lface, sizeof attrs);
6106 /* Merge in attributes specified via text properties. */
6107 if (!NILP (prop))
6108 merge_face_ref (f, prop, attrs, 1, 0);
6110 *endptr = endpos;
6112 /* Look up a realized face with the given face attributes,
6113 or realize a new one for ASCII characters. */
6114 return lookup_face (f, attrs);
6118 /* Compute the face at character position POS in Lisp string STRING on
6119 window W, for ASCII characters.
6121 If STRING is an overlay string, it comes from position BUFPOS in
6122 current_buffer, otherwise BUFPOS is zero to indicate that STRING is
6123 not an overlay string. W must display the current buffer.
6124 REGION_BEG and REGION_END give the start and end positions of the
6125 region; both are -1 if no region is visible.
6127 BASE_FACE_ID is the id of a face to merge with. For strings coming
6128 from overlays or the `display' property it is the face at BUFPOS.
6130 If MOUSE_P is non-zero, use the character's mouse-face, not its face.
6132 Set *ENDPTR to the next position where to check for faces in
6133 STRING; -1 if the face is constant from POS to the end of the
6134 string.
6136 Value is the id of the face to use. The face returned is suitable
6137 for displaying ASCII characters. */
6140 face_at_string_position (struct window *w, Lisp_Object string,
6141 ptrdiff_t pos, ptrdiff_t bufpos,
6142 ptrdiff_t *endptr, enum face_id base_face_id,
6143 int mouse_p)
6145 Lisp_Object prop, position, end, limit;
6146 struct frame *f = XFRAME (WINDOW_FRAME (w));
6147 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6148 struct face *base_face;
6149 bool multibyte_p = STRING_MULTIBYTE (string);
6150 Lisp_Object prop_name = mouse_p ? Qmouse_face : Qface;
6152 /* Get the value of the face property at the current position within
6153 STRING. Value is nil if there is no face property. */
6154 XSETFASTINT (position, pos);
6155 prop = Fget_text_property (position, prop_name, string);
6157 /* Get the next position at which to check for faces. Value of end
6158 is nil if face is constant all the way to the end of the string.
6159 Otherwise it is a string position where to check faces next.
6160 Limit is the maximum position up to which to check for property
6161 changes in Fnext_single_property_change. Strings are usually
6162 short, so set the limit to the end of the string. */
6163 XSETFASTINT (limit, SCHARS (string));
6164 end = Fnext_single_property_change (position, prop_name, string, limit);
6165 if (INTEGERP (end))
6166 *endptr = XFASTINT (end);
6167 else
6168 *endptr = -1;
6170 base_face = FACE_FROM_ID (f, base_face_id);
6171 eassert (base_face);
6173 /* Optimize the default case that there is no face property. */
6174 if (NILP (prop)
6175 && (multibyte_p
6176 /* We can't realize faces for different charsets differently
6177 if we don't have fonts, so we can stop here if not working
6178 on a window-system frame. */
6179 || !FRAME_WINDOW_P (f)
6180 || FACE_SUITABLE_FOR_ASCII_CHAR_P (base_face, 0)))
6181 return base_face->id;
6183 /* Begin with attributes from the base face. */
6184 memcpy (attrs, base_face->lface, sizeof attrs);
6186 /* Merge in attributes specified via text properties. */
6187 if (!NILP (prop))
6188 merge_face_ref (f, prop, attrs, 1, 0);
6190 /* Look up a realized face with the given face attributes,
6191 or realize a new one for ASCII characters. */
6192 return lookup_face (f, attrs);
6196 /* Merge a face into a realized face.
6198 F is frame where faces are (to be) realized.
6200 FACE_NAME is named face to merge.
6202 If FACE_NAME is nil, FACE_ID is face_id of realized face to merge.
6204 If FACE_NAME is t, FACE_ID is lface_id of face to merge.
6206 BASE_FACE_ID is realized face to merge into.
6208 Return new face id.
6212 merge_faces (struct frame *f, Lisp_Object face_name, int face_id,
6213 int base_face_id)
6215 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6216 struct face *base_face;
6218 base_face = FACE_FROM_ID (f, base_face_id);
6219 if (!base_face)
6220 return base_face_id;
6222 if (EQ (face_name, Qt))
6224 if (face_id < 0 || face_id >= lface_id_to_name_size)
6225 return base_face_id;
6226 face_name = lface_id_to_name[face_id];
6227 /* When called during make-frame, lookup_derived_face may fail
6228 if the faces are uninitialized. Don't signal an error. */
6229 face_id = lookup_derived_face (f, face_name, base_face_id, 0);
6230 return (face_id >= 0 ? face_id : base_face_id);
6233 /* Begin with attributes from the base face. */
6234 memcpy (attrs, base_face->lface, sizeof attrs);
6236 if (!NILP (face_name))
6238 if (!merge_named_face (f, face_name, attrs, 0))
6239 return base_face_id;
6241 else
6243 struct face *face;
6244 if (face_id < 0)
6245 return base_face_id;
6246 face = FACE_FROM_ID (f, face_id);
6247 if (!face)
6248 return base_face_id;
6249 merge_face_vectors (f, face->lface, attrs, 0);
6252 /* Look up a realized face with the given face attributes,
6253 or realize a new one for ASCII characters. */
6254 return lookup_face (f, attrs);
6259 #ifndef HAVE_X_WINDOWS
6260 DEFUN ("x-load-color-file", Fx_load_color_file,
6261 Sx_load_color_file, 1, 1, 0,
6262 doc: /* Create an alist of color entries from an external file.
6264 The file should define one named RGB color per line like so:
6265 R G B name
6266 where R,G,B are numbers between 0 and 255 and name is an arbitrary string. */)
6267 (Lisp_Object filename)
6269 FILE *fp;
6270 Lisp_Object cmap = Qnil;
6271 Lisp_Object abspath;
6273 CHECK_STRING (filename);
6274 abspath = Fexpand_file_name (filename, Qnil);
6276 block_input ();
6277 fp = emacs_fopen (SSDATA (abspath), "rt");
6278 if (fp)
6280 char buf[512];
6281 int red, green, blue;
6282 int num;
6284 while (fgets (buf, sizeof (buf), fp) != NULL) {
6285 if (sscanf (buf, "%u %u %u %n", &red, &green, &blue, &num) == 3)
6287 #ifdef HAVE_NTGUI
6288 int color = RGB (red, green, blue);
6289 #else
6290 int color = (red << 16) | (green << 8) | blue;
6291 #endif
6292 char *name = buf + num;
6293 ptrdiff_t len = strlen (name);
6294 len -= 0 < len && name[len - 1] == '\n';
6295 cmap = Fcons (Fcons (make_string (name, len), make_number (color)),
6296 cmap);
6299 fclose (fp);
6301 unblock_input ();
6302 return cmap;
6304 #endif
6307 /***********************************************************************
6308 Tests
6309 ***********************************************************************/
6311 #ifdef GLYPH_DEBUG
6313 /* Print the contents of the realized face FACE to stderr. */
6315 static void
6316 dump_realized_face (struct face *face)
6318 fprintf (stderr, "ID: %d\n", face->id);
6319 #ifdef HAVE_X_WINDOWS
6320 fprintf (stderr, "gc: %ld\n", (long) face->gc);
6321 #endif
6322 fprintf (stderr, "foreground: 0x%lx (%s)\n",
6323 face->foreground,
6324 SDATA (face->lface[LFACE_FOREGROUND_INDEX]));
6325 fprintf (stderr, "background: 0x%lx (%s)\n",
6326 face->background,
6327 SDATA (face->lface[LFACE_BACKGROUND_INDEX]));
6328 if (face->font)
6329 fprintf (stderr, "font_name: %s (%s)\n",
6330 SDATA (face->font->props[FONT_NAME_INDEX]),
6331 SDATA (face->lface[LFACE_FAMILY_INDEX]));
6332 #ifdef HAVE_X_WINDOWS
6333 fprintf (stderr, "font = %p\n", face->font);
6334 #endif
6335 fprintf (stderr, "fontset: %d\n", face->fontset);
6336 fprintf (stderr, "underline: %d (%s)\n",
6337 face->underline_p,
6338 SDATA (Fsymbol_name (face->lface[LFACE_UNDERLINE_INDEX])));
6339 fprintf (stderr, "hash: %d\n", face->hash);
6343 DEFUN ("dump-face", Fdump_face, Sdump_face, 0, 1, 0, doc: /* */)
6344 (Lisp_Object n)
6346 if (NILP (n))
6348 int i;
6350 fprintf (stderr, "font selection order: ");
6351 for (i = 0; i < DIM (font_sort_order); ++i)
6352 fprintf (stderr, "%d ", font_sort_order[i]);
6353 fprintf (stderr, "\n");
6355 fprintf (stderr, "alternative fonts: ");
6356 debug_print (Vface_alternative_font_family_alist);
6357 fprintf (stderr, "\n");
6359 for (i = 0; i < FRAME_FACE_CACHE (SELECTED_FRAME ())->used; ++i)
6360 Fdump_face (make_number (i));
6362 else
6364 struct face *face;
6365 CHECK_NUMBER (n);
6366 face = FACE_FROM_ID (SELECTED_FRAME (), XINT (n));
6367 if (face == NULL)
6368 error ("Not a valid face");
6369 dump_realized_face (face);
6372 return Qnil;
6376 DEFUN ("show-face-resources", Fshow_face_resources, Sshow_face_resources,
6377 0, 0, 0, doc: /* */)
6378 (void)
6380 fprintf (stderr, "number of colors = %d\n", ncolors_allocated);
6381 fprintf (stderr, "number of pixmaps = %d\n", npixmaps_allocated);
6382 fprintf (stderr, "number of GCs = %d\n", ngcs);
6383 return Qnil;
6386 #endif /* GLYPH_DEBUG */
6390 /***********************************************************************
6391 Initialization
6392 ***********************************************************************/
6394 void
6395 syms_of_xfaces (void)
6397 DEFSYM (Qface, "face");
6398 DEFSYM (Qface_no_inherit, "face-no-inherit");
6399 DEFSYM (Qbitmap_spec_p, "bitmap-spec-p");
6400 DEFSYM (Qframe_set_background_mode, "frame-set-background-mode");
6402 /* Lisp face attribute keywords. */
6403 DEFSYM (QCfamily, ":family");
6404 DEFSYM (QCheight, ":height");
6405 DEFSYM (QCweight, ":weight");
6406 DEFSYM (QCslant, ":slant");
6407 DEFSYM (QCunderline, ":underline");
6408 DEFSYM (QCinverse_video, ":inverse-video");
6409 DEFSYM (QCreverse_video, ":reverse-video");
6410 DEFSYM (QCforeground, ":foreground");
6411 DEFSYM (QCbackground, ":background");
6412 DEFSYM (QCstipple, ":stipple");
6413 DEFSYM (QCwidth, ":width");
6414 DEFSYM (QCfont, ":font");
6415 DEFSYM (QCfontset, ":fontset");
6416 DEFSYM (QCdistant_foreground, ":distant-foreground");
6417 DEFSYM (QCbold, ":bold");
6418 DEFSYM (QCitalic, ":italic");
6419 DEFSYM (QCoverline, ":overline");
6420 DEFSYM (QCstrike_through, ":strike-through");
6421 DEFSYM (QCbox, ":box");
6422 DEFSYM (QCinherit, ":inherit");
6424 /* Symbols used for Lisp face attribute values. */
6425 DEFSYM (QCcolor, ":color");
6426 DEFSYM (QCline_width, ":line-width");
6427 DEFSYM (QCstyle, ":style");
6428 DEFSYM (Qline, "line");
6429 DEFSYM (Qwave, "wave");
6430 DEFSYM (Qreleased_button, "released-button");
6431 DEFSYM (Qpressed_button, "pressed-button");
6432 DEFSYM (Qnormal, "normal");
6433 DEFSYM (Qextra_light, "extra-light");
6434 DEFSYM (Qlight, "light");
6435 DEFSYM (Qsemi_light, "semi-light");
6436 DEFSYM (Qsemi_bold, "semi-bold");
6437 DEFSYM (Qbold, "bold");
6438 DEFSYM (Qextra_bold, "extra-bold");
6439 DEFSYM (Qultra_bold, "ultra-bold");
6440 DEFSYM (Qoblique, "oblique");
6441 DEFSYM (Qitalic, "italic");
6442 DEFSYM (Qbackground_color, "background-color");
6443 DEFSYM (Qforeground_color, "foreground-color");
6444 DEFSYM (Qunspecified, "unspecified");
6445 DEFSYM (QCignore_defface, ":ignore-defface");
6447 DEFSYM (Qface_alias, "face-alias");
6448 DEFSYM (Qdefault, "default");
6449 DEFSYM (Qtool_bar, "tool-bar");
6450 DEFSYM (Qregion, "region");
6451 DEFSYM (Qfringe, "fringe");
6452 DEFSYM (Qheader_line, "header-line");
6453 DEFSYM (Qscroll_bar, "scroll-bar");
6454 DEFSYM (Qmenu, "menu");
6455 DEFSYM (Qcursor, "cursor");
6456 DEFSYM (Qborder, "border");
6457 DEFSYM (Qmouse, "mouse");
6458 DEFSYM (Qmode_line_inactive, "mode-line-inactive");
6459 DEFSYM (Qvertical_border, "vertical-border");
6460 DEFSYM (Qwindow_divider, "window-divider");
6461 DEFSYM (Qwindow_divider_first_pixel, "window-divider-first-pixel");
6462 DEFSYM (Qwindow_divider_last_pixel, "window-divider-last-pixel");
6463 DEFSYM (Qtty_color_desc, "tty-color-desc");
6464 DEFSYM (Qtty_color_standard_values, "tty-color-standard-values");
6465 DEFSYM (Qtty_color_by_index, "tty-color-by-index");
6466 DEFSYM (Qtty_color_alist, "tty-color-alist");
6467 DEFSYM (Qscalable_fonts_allowed, "scalable-fonts-allowed");
6469 Vparam_value_alist = list1 (Fcons (Qnil, Qnil));
6470 staticpro (&Vparam_value_alist);
6471 Vface_alternative_font_family_alist = Qnil;
6472 staticpro (&Vface_alternative_font_family_alist);
6473 Vface_alternative_font_registry_alist = Qnil;
6474 staticpro (&Vface_alternative_font_registry_alist);
6476 defsubr (&Sinternal_make_lisp_face);
6477 defsubr (&Sinternal_lisp_face_p);
6478 defsubr (&Sinternal_set_lisp_face_attribute);
6479 #ifdef HAVE_WINDOW_SYSTEM
6480 defsubr (&Sinternal_set_lisp_face_attribute_from_resource);
6481 #endif
6482 defsubr (&Scolor_gray_p);
6483 defsubr (&Scolor_supported_p);
6484 #ifndef HAVE_X_WINDOWS
6485 defsubr (&Sx_load_color_file);
6486 #endif
6487 defsubr (&Sface_attribute_relative_p);
6488 defsubr (&Smerge_face_attribute);
6489 defsubr (&Sinternal_get_lisp_face_attribute);
6490 defsubr (&Sinternal_lisp_face_attribute_values);
6491 defsubr (&Sinternal_lisp_face_equal_p);
6492 defsubr (&Sinternal_lisp_face_empty_p);
6493 defsubr (&Sinternal_copy_lisp_face);
6494 defsubr (&Sinternal_merge_in_global_face);
6495 defsubr (&Sface_font);
6496 defsubr (&Sframe_face_alist);
6497 defsubr (&Sdisplay_supports_face_attributes_p);
6498 defsubr (&Scolor_distance);
6499 defsubr (&Sinternal_set_font_selection_order);
6500 defsubr (&Sinternal_set_alternative_font_family_alist);
6501 defsubr (&Sinternal_set_alternative_font_registry_alist);
6502 defsubr (&Sface_attributes_as_vector);
6503 #ifdef GLYPH_DEBUG
6504 defsubr (&Sdump_face);
6505 defsubr (&Sshow_face_resources);
6506 #endif /* GLYPH_DEBUG */
6507 defsubr (&Sclear_face_cache);
6508 defsubr (&Stty_suppress_bold_inverse_default_colors);
6510 #if defined DEBUG_X_COLORS && defined HAVE_X_WINDOWS
6511 defsubr (&Sdump_colors);
6512 #endif
6514 DEFVAR_LISP ("face-new-frame-defaults", Vface_new_frame_defaults,
6515 doc: /* List of global face definitions (for internal use only.) */);
6516 Vface_new_frame_defaults = Qnil;
6518 DEFVAR_LISP ("face-default-stipple", Vface_default_stipple,
6519 doc: /* Default stipple pattern used on monochrome displays.
6520 This stipple pattern is used on monochrome displays
6521 instead of shades of gray for a face background color.
6522 See `set-face-stipple' for possible values for this variable. */);
6523 Vface_default_stipple = build_pure_c_string ("gray3");
6525 DEFVAR_LISP ("tty-defined-color-alist", Vtty_defined_color_alist,
6526 doc: /* An alist of defined terminal colors and their RGB values.
6527 See the docstring of `tty-color-alist' for the details. */);
6528 Vtty_defined_color_alist = Qnil;
6530 DEFVAR_LISP ("scalable-fonts-allowed", Vscalable_fonts_allowed,
6531 doc: /* Allowed scalable fonts.
6532 A value of nil means don't allow any scalable fonts.
6533 A value of t means allow any scalable font.
6534 Otherwise, value must be a list of regular expressions. A font may be
6535 scaled if its name matches a regular expression in the list.
6536 Note that if value is nil, a scalable font might still be used, if no
6537 other font of the appropriate family and registry is available. */);
6538 Vscalable_fonts_allowed = Qnil;
6540 DEFVAR_LISP ("face-ignored-fonts", Vface_ignored_fonts,
6541 doc: /* List of ignored fonts.
6542 Each element is a regular expression that matches names of fonts to
6543 ignore. */);
6544 Vface_ignored_fonts = Qnil;
6546 DEFVAR_LISP ("face-remapping-alist", Vface_remapping_alist,
6547 doc: /* Alist of face remappings.
6548 Each element is of the form:
6550 (FACE . REPLACEMENT),
6552 which causes display of the face FACE to use REPLACEMENT instead.
6553 REPLACEMENT is a face specification, i.e. one of the following:
6555 (1) a face name
6556 (2) a property list of attribute/value pairs, or
6557 (3) a list in which each element has the form of (1) or (2).
6559 List values for REPLACEMENT are merged to form the final face
6560 specification, with earlier entries taking precedence, in the same as
6561 as in the `face' text property.
6563 Face-name remapping cycles are suppressed; recursive references use
6564 the underlying face instead of the remapped face. So a remapping of
6565 the form:
6567 (FACE EXTRA-FACE... FACE)
6571 (FACE (FACE-ATTR VAL ...) FACE)
6573 causes EXTRA-FACE... or (FACE-ATTR VAL ...) to be _merged_ with the
6574 existing definition of FACE. Note that this isn't necessary for the
6575 default face, since every face inherits from the default face.
6577 If this variable is made buffer-local, the face remapping takes effect
6578 only in that buffer. For instance, the mode my-mode could define a
6579 face `my-mode-default', and then in the mode setup function, do:
6581 (set (make-local-variable 'face-remapping-alist)
6582 '((default my-mode-default)))).
6584 Because Emacs normally only redraws screen areas when the underlying
6585 buffer contents change, you may need to call `redraw-display' after
6586 changing this variable for it to take effect. */);
6587 Vface_remapping_alist = Qnil;
6589 DEFVAR_LISP ("face-font-rescale-alist", Vface_font_rescale_alist,
6590 doc: /* Alist of fonts vs the rescaling factors.
6591 Each element is a cons (FONT-PATTERN . RESCALE-RATIO), where
6592 FONT-PATTERN is a font-spec or a regular expression matching a font name, and
6593 RESCALE-RATIO is a floating point number to specify how much larger
6594 \(or smaller) font we should use. For instance, if a face requests
6595 a font of 10 point, we actually use a font of 10 * RESCALE-RATIO point. */);
6596 Vface_font_rescale_alist = Qnil;
6598 #ifdef HAVE_WINDOW_SYSTEM
6599 defsubr (&Sbitmap_spec_p);
6600 defsubr (&Sx_list_fonts);
6601 defsubr (&Sinternal_face_x_get_resource);
6602 defsubr (&Sx_family_fonts);
6603 #endif