Merge commit 2014-07-28T09:07:56Z!tsdh@gnu.org from trunk.
[emacs.git] / src / xfaces.c
blobac67c7b7be434f3a74e06cff24272fee1654deb4
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);
3122 if (! FONT_OBJECT_P (value))
3124 Lisp_Object *attrs = XVECTOR (lface)->contents;
3125 Lisp_Object font_object;
3127 font_object = font_load_for_lface (f, attrs, value);
3128 if (NILP (font_object))
3129 signal_error ("Font not available", value);
3130 value = font_object;
3132 set_lface_from_font (f, lface, value, 1);
3134 else
3135 ASET (lface, LFACE_FONT_INDEX, value);
3137 #endif /* HAVE_WINDOW_SYSTEM */
3139 else if (EQ (attr, QCfontset))
3141 #ifdef HAVE_WINDOW_SYSTEM
3142 if (EQ (frame, Qt) || FRAME_WINDOW_P (XFRAME (frame)))
3144 Lisp_Object tmp;
3146 old_value = LFACE_FONTSET (lface);
3147 tmp = Fquery_fontset (value, Qnil);
3148 if (NILP (tmp))
3149 signal_error ("Invalid fontset name", value);
3150 ASET (lface, LFACE_FONTSET_INDEX, value = tmp);
3152 #endif /* HAVE_WINDOW_SYSTEM */
3154 else if (EQ (attr, QCinherit))
3156 Lisp_Object tail;
3157 if (SYMBOLP (value))
3158 tail = Qnil;
3159 else
3160 for (tail = value; CONSP (tail); tail = XCDR (tail))
3161 if (!SYMBOLP (XCAR (tail)))
3162 break;
3163 if (NILP (tail))
3164 ASET (lface, LFACE_INHERIT_INDEX, value);
3165 else
3166 signal_error ("Invalid face inheritance", value);
3168 else if (EQ (attr, QCbold))
3170 old_value = LFACE_WEIGHT (lface);
3171 ASET (lface, LFACE_WEIGHT_INDEX, NILP (value) ? Qnormal : Qbold);
3172 prop_index = FONT_WEIGHT_INDEX;
3174 else if (EQ (attr, QCitalic))
3176 attr = QCslant;
3177 old_value = LFACE_SLANT (lface);
3178 ASET (lface, LFACE_SLANT_INDEX, NILP (value) ? Qnormal : Qitalic);
3179 prop_index = FONT_SLANT_INDEX;
3181 else
3182 signal_error ("Invalid face attribute name", attr);
3184 if (prop_index)
3186 /* If a font-related attribute other than QCfont and QCfontset
3187 is specified, and if the original QCfont attribute has a font
3188 (font-spec or font-object), set the corresponding property in
3189 the font to nil so that the font selector doesn't think that
3190 the attribute is mandatory. Also, clear the average
3191 width. */
3192 font_clear_prop (XVECTOR (lface)->contents, prop_index);
3195 /* Changing a named face means that all realized faces depending on
3196 that face are invalid. Since we cannot tell which realized faces
3197 depend on the face, make sure they are all removed. This is done
3198 by incrementing face_change_count. The next call to
3199 init_iterator will then free realized faces. */
3200 if (!EQ (frame, Qt)
3201 && NILP (Fget (face, Qface_no_inherit))
3202 && NILP (Fequal (old_value, value)))
3204 ++face_change_count;
3205 windows_or_buffers_changed = 56;
3208 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value)
3209 && NILP (Fequal (old_value, value)))
3211 Lisp_Object param;
3213 param = Qnil;
3215 if (EQ (face, Qdefault))
3217 #ifdef HAVE_WINDOW_SYSTEM
3218 /* Changed font-related attributes of the `default' face are
3219 reflected in changed `font' frame parameters. */
3220 if (FRAMEP (frame)
3221 && (prop_index || EQ (attr, QCfont))
3222 && lface_fully_specified_p (XVECTOR (lface)->contents))
3223 set_font_frame_param (frame, lface);
3224 else
3225 #endif /* HAVE_WINDOW_SYSTEM */
3227 if (EQ (attr, QCforeground))
3228 param = Qforeground_color;
3229 else if (EQ (attr, QCbackground))
3230 param = Qbackground_color;
3232 #ifdef HAVE_WINDOW_SYSTEM
3233 #ifndef HAVE_NTGUI
3234 else if (EQ (face, Qscroll_bar))
3236 /* Changing the colors of `scroll-bar' sets frame parameters
3237 `scroll-bar-foreground' and `scroll-bar-background'. */
3238 if (EQ (attr, QCforeground))
3239 param = Qscroll_bar_foreground;
3240 else if (EQ (attr, QCbackground))
3241 param = Qscroll_bar_background;
3243 #endif /* not HAVE_NTGUI */
3244 else if (EQ (face, Qborder))
3246 /* Changing background color of `border' sets frame parameter
3247 `border-color'. */
3248 if (EQ (attr, QCbackground))
3249 param = Qborder_color;
3251 else if (EQ (face, Qcursor))
3253 /* Changing background color of `cursor' sets frame parameter
3254 `cursor-color'. */
3255 if (EQ (attr, QCbackground))
3256 param = Qcursor_color;
3258 else if (EQ (face, Qmouse))
3260 /* Changing background color of `mouse' sets frame parameter
3261 `mouse-color'. */
3262 if (EQ (attr, QCbackground))
3263 param = Qmouse_color;
3265 #endif /* HAVE_WINDOW_SYSTEM */
3266 else if (EQ (face, Qmenu))
3268 /* Indicate that we have to update the menu bar when realizing
3269 faces on FRAME. FRAME t change the default for new frames.
3270 We do this by setting the flag in new face caches. */
3271 if (FRAMEP (frame))
3273 struct frame *f = XFRAME (frame);
3274 if (FRAME_FACE_CACHE (f) == NULL)
3275 FRAME_FACE_CACHE (f) = make_face_cache (f);
3276 FRAME_FACE_CACHE (f)->menu_face_changed_p = 1;
3278 else
3279 menu_face_changed_default = 1;
3282 if (!NILP (param))
3284 if (EQ (frame, Qt))
3285 /* Update `default-frame-alist', which is used for new frames. */
3287 store_in_alist (&Vdefault_frame_alist, param, value);
3289 else
3290 /* Update the current frame's parameters. */
3292 Lisp_Object cons;
3293 cons = XCAR (Vparam_value_alist);
3294 XSETCAR (cons, param);
3295 XSETCDR (cons, value);
3296 Fmodify_frame_parameters (frame, Vparam_value_alist);
3301 return face;
3305 /* Update the corresponding face when frame parameter PARAM on frame F
3306 has been assigned the value NEW_VALUE. */
3308 void
3309 update_face_from_frame_parameter (struct frame *f, Lisp_Object param,
3310 Lisp_Object new_value)
3312 Lisp_Object face = Qnil;
3313 Lisp_Object lface;
3315 /* If there are no faces yet, give up. This is the case when called
3316 from Fx_create_frame, and we do the necessary things later in
3317 face-set-after-frame-defaults. */
3318 if (NILP (f->face_alist))
3319 return;
3321 if (EQ (param, Qforeground_color))
3323 face = Qdefault;
3324 lface = lface_from_face_name (f, face, 1);
3325 ASET (lface, LFACE_FOREGROUND_INDEX,
3326 (STRINGP (new_value) ? new_value : Qunspecified));
3327 realize_basic_faces (f);
3329 else if (EQ (param, Qbackground_color))
3331 Lisp_Object frame;
3333 /* Changing the background color might change the background
3334 mode, so that we have to load new defface specs.
3335 Call frame-set-background-mode to do that. */
3336 XSETFRAME (frame, f);
3337 call1 (Qframe_set_background_mode, frame);
3339 face = Qdefault;
3340 lface = lface_from_face_name (f, face, 1);
3341 ASET (lface, LFACE_BACKGROUND_INDEX,
3342 (STRINGP (new_value) ? new_value : Qunspecified));
3343 realize_basic_faces (f);
3345 #ifdef HAVE_WINDOW_SYSTEM
3346 else if (EQ (param, Qborder_color))
3348 face = Qborder;
3349 lface = lface_from_face_name (f, face, 1);
3350 ASET (lface, LFACE_BACKGROUND_INDEX,
3351 (STRINGP (new_value) ? new_value : Qunspecified));
3353 else if (EQ (param, Qcursor_color))
3355 face = Qcursor;
3356 lface = lface_from_face_name (f, face, 1);
3357 ASET (lface, LFACE_BACKGROUND_INDEX,
3358 (STRINGP (new_value) ? new_value : Qunspecified));
3360 else if (EQ (param, Qmouse_color))
3362 face = Qmouse;
3363 lface = lface_from_face_name (f, face, 1);
3364 ASET (lface, LFACE_BACKGROUND_INDEX,
3365 (STRINGP (new_value) ? new_value : Qunspecified));
3367 #endif
3369 /* Changing a named face means that all realized faces depending on
3370 that face are invalid. Since we cannot tell which realized faces
3371 depend on the face, make sure they are all removed. This is done
3372 by incrementing face_change_count. The next call to
3373 init_iterator will then free realized faces. */
3374 if (!NILP (face)
3375 && NILP (Fget (face, Qface_no_inherit)))
3377 ++face_change_count;
3378 windows_or_buffers_changed = 57;
3383 #ifdef HAVE_WINDOW_SYSTEM
3385 /* Set the `font' frame parameter of FRAME determined from the
3386 font-object set in `default' face attributes LFACE. */
3388 static void
3389 set_font_frame_param (Lisp_Object frame, Lisp_Object lface)
3391 struct frame *f = XFRAME (frame);
3392 Lisp_Object font;
3394 if (FRAME_WINDOW_P (f)
3395 /* Don't do anything if the font is `unspecified'. This can
3396 happen during frame creation. */
3397 && (font = LFACE_FONT (lface),
3398 ! UNSPECIFIEDP (font)))
3400 if (FONT_SPEC_P (font))
3402 font = font_load_for_lface (f, XVECTOR (lface)->contents, font);
3403 if (NILP (font))
3404 return;
3405 ASET (lface, LFACE_FONT_INDEX, font);
3407 f->default_face_done_p = 0;
3408 Fmodify_frame_parameters (frame, list1 (Fcons (Qfont, font)));
3412 DEFUN ("internal-face-x-get-resource", Finternal_face_x_get_resource,
3413 Sinternal_face_x_get_resource, 2, 3, 0,
3414 doc: /* Get the value of X resource RESOURCE, class CLASS.
3415 Returned value is for the display of frame FRAME. If FRAME is not
3416 specified or nil, use selected frame. This function exists because
3417 ordinary `x-get-resource' doesn't take a frame argument. */)
3418 (Lisp_Object resource, Lisp_Object class, Lisp_Object frame)
3420 Lisp_Object value = Qnil;
3421 struct frame *f;
3423 CHECK_STRING (resource);
3424 CHECK_STRING (class);
3425 f = decode_live_frame (frame);
3426 block_input ();
3427 value = display_x_get_resource (FRAME_DISPLAY_INFO (f),
3428 resource, class, Qnil, Qnil);
3429 unblock_input ();
3430 return value;
3434 /* Return resource string VALUE as a boolean value, i.e. nil, or t.
3435 If VALUE is "on" or "true", return t. If VALUE is "off" or
3436 "false", return nil. Otherwise, if SIGNAL_P is non-zero, signal an
3437 error; if SIGNAL_P is zero, return 0. */
3439 static Lisp_Object
3440 face_boolean_x_resource_value (Lisp_Object value, int signal_p)
3442 Lisp_Object result = make_number (0);
3444 eassert (STRINGP (value));
3446 if (xstrcasecmp (SSDATA (value), "on") == 0
3447 || xstrcasecmp (SSDATA (value), "true") == 0)
3448 result = Qt;
3449 else if (xstrcasecmp (SSDATA (value), "off") == 0
3450 || xstrcasecmp (SSDATA (value), "false") == 0)
3451 result = Qnil;
3452 else if (xstrcasecmp (SSDATA (value), "unspecified") == 0)
3453 result = Qunspecified;
3454 else if (signal_p)
3455 signal_error ("Invalid face attribute value from X resource", value);
3457 return result;
3461 DEFUN ("internal-set-lisp-face-attribute-from-resource",
3462 Finternal_set_lisp_face_attribute_from_resource,
3463 Sinternal_set_lisp_face_attribute_from_resource,
3464 3, 4, 0, doc: /* */)
3465 (Lisp_Object face, Lisp_Object attr, Lisp_Object value, Lisp_Object frame)
3467 CHECK_SYMBOL (face);
3468 CHECK_SYMBOL (attr);
3469 CHECK_STRING (value);
3471 if (xstrcasecmp (SSDATA (value), "unspecified") == 0)
3472 value = Qunspecified;
3473 else if (EQ (attr, QCheight))
3475 value = Fstring_to_number (value, make_number (10));
3476 if (XINT (value) <= 0)
3477 signal_error ("Invalid face height from X resource", value);
3479 else if (EQ (attr, QCbold) || EQ (attr, QCitalic))
3480 value = face_boolean_x_resource_value (value, 1);
3481 else if (EQ (attr, QCweight) || EQ (attr, QCslant) || EQ (attr, QCwidth))
3482 value = intern (SSDATA (value));
3483 else if (EQ (attr, QCreverse_video) || EQ (attr, QCinverse_video))
3484 value = face_boolean_x_resource_value (value, 1);
3485 else if (EQ (attr, QCunderline)
3486 || EQ (attr, QCoverline)
3487 || EQ (attr, QCstrike_through))
3489 Lisp_Object boolean_value;
3491 /* If the result of face_boolean_x_resource_value is t or nil,
3492 VALUE does NOT specify a color. */
3493 boolean_value = face_boolean_x_resource_value (value, 0);
3494 if (SYMBOLP (boolean_value))
3495 value = boolean_value;
3497 else if (EQ (attr, QCbox) || EQ (attr, QCinherit))
3498 value = Fcar (Fread_from_string (value, Qnil, Qnil));
3500 return Finternal_set_lisp_face_attribute (face, attr, value, frame);
3503 #endif /* HAVE_WINDOW_SYSTEM */
3506 /***********************************************************************
3507 Menu face
3508 ***********************************************************************/
3510 #if defined HAVE_X_WINDOWS && defined USE_X_TOOLKIT
3512 /* Make menus on frame F appear as specified by the `menu' face. */
3514 static void
3515 x_update_menu_appearance (struct frame *f)
3517 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
3518 XrmDatabase rdb;
3520 if (dpyinfo
3521 && (rdb = XrmGetDatabase (FRAME_X_DISPLAY (f)),
3522 rdb != NULL))
3524 char line[512];
3525 char *buf = line;
3526 ptrdiff_t bufsize = sizeof line;
3527 Lisp_Object lface = lface_from_face_name (f, Qmenu, 1);
3528 struct face *face = FACE_FROM_ID (f, MENU_FACE_ID);
3529 const char *myname = SSDATA (Vx_resource_name);
3530 bool changed_p = 0;
3531 #ifdef USE_MOTIF
3532 const char *popup_path = "popup_menu";
3533 #else
3534 const char *popup_path = "menu.popup";
3535 #endif
3537 if (STRINGP (LFACE_FOREGROUND (lface)))
3539 exprintf (&buf, &bufsize, line, -1, "%s.%s*foreground: %s",
3540 myname, popup_path,
3541 SDATA (LFACE_FOREGROUND (lface)));
3542 XrmPutLineResource (&rdb, line);
3543 exprintf (&buf, &bufsize, line, -1, "%s.pane.menubar*foreground: %s",
3544 myname, SDATA (LFACE_FOREGROUND (lface)));
3545 XrmPutLineResource (&rdb, line);
3546 changed_p = 1;
3549 if (STRINGP (LFACE_BACKGROUND (lface)))
3551 exprintf (&buf, &bufsize, line, -1, "%s.%s*background: %s",
3552 myname, popup_path,
3553 SDATA (LFACE_BACKGROUND (lface)));
3554 XrmPutLineResource (&rdb, line);
3556 exprintf (&buf, &bufsize, line, -1, "%s.pane.menubar*background: %s",
3557 myname, SDATA (LFACE_BACKGROUND (lface)));
3558 XrmPutLineResource (&rdb, line);
3559 changed_p = 1;
3562 if (face->font
3563 /* On Solaris 5.8, it's been reported that the `menu' face
3564 can be unspecified here, during startup. Why this
3565 happens remains unknown. -- cyd */
3566 && FONTP (LFACE_FONT (lface))
3567 && (!UNSPECIFIEDP (LFACE_FAMILY (lface))
3568 || !UNSPECIFIEDP (LFACE_FOUNDRY (lface))
3569 || !UNSPECIFIEDP (LFACE_SWIDTH (lface))
3570 || !UNSPECIFIEDP (LFACE_WEIGHT (lface))
3571 || !UNSPECIFIEDP (LFACE_SLANT (lface))
3572 || !UNSPECIFIEDP (LFACE_HEIGHT (lface))))
3574 Lisp_Object xlfd = Ffont_xlfd_name (LFACE_FONT (lface), Qnil);
3575 #ifdef USE_MOTIF
3576 const char *suffix = "List";
3577 Bool motif = True;
3578 #else
3579 #if defined HAVE_X_I18N
3581 const char *suffix = "Set";
3582 #else
3583 const char *suffix = "";
3584 #endif
3585 Bool motif = False;
3586 #endif
3588 if (! NILP (xlfd))
3590 #if defined HAVE_X_I18N
3591 char *fontsetname = xic_create_fontsetname (SSDATA (xlfd), motif);
3592 #else
3593 char *fontsetname = SSDATA (xlfd);
3594 #endif
3595 exprintf (&buf, &bufsize, line, -1, "%s.pane.menubar*font%s: %s",
3596 myname, suffix, fontsetname);
3597 XrmPutLineResource (&rdb, line);
3599 exprintf (&buf, &bufsize, line, -1, "%s.%s*font%s: %s",
3600 myname, popup_path, suffix, fontsetname);
3601 XrmPutLineResource (&rdb, line);
3602 changed_p = 1;
3603 if (fontsetname != SSDATA (xlfd))
3604 xfree (fontsetname);
3608 if (changed_p && f->output_data.x->menubar_widget)
3609 free_frame_menubar (f);
3611 if (buf != line)
3612 xfree (buf);
3616 #endif /* HAVE_X_WINDOWS && USE_X_TOOLKIT */
3619 DEFUN ("face-attribute-relative-p", Fface_attribute_relative_p,
3620 Sface_attribute_relative_p,
3621 2, 2, 0,
3622 doc: /* Check whether a face attribute value is relative.
3623 Specifically, this function returns t if the attribute ATTRIBUTE
3624 with the value VALUE is relative.
3626 A relative value is one that doesn't entirely override whatever is
3627 inherited from another face. For most possible attributes,
3628 the only relative value that users see is `unspecified'.
3629 However, for :height, floating point values are also relative. */)
3630 (Lisp_Object attribute, Lisp_Object value)
3632 if (EQ (value, Qunspecified) || (EQ (value, QCignore_defface)))
3633 return Qt;
3634 else if (EQ (attribute, QCheight))
3635 return INTEGERP (value) ? Qnil : Qt;
3636 else
3637 return Qnil;
3640 DEFUN ("merge-face-attribute", Fmerge_face_attribute, Smerge_face_attribute,
3641 3, 3, 0,
3642 doc: /* Return face ATTRIBUTE VALUE1 merged with VALUE2.
3643 If VALUE1 or VALUE2 are absolute (see `face-attribute-relative-p'), then
3644 the result will be absolute, otherwise it will be relative. */)
3645 (Lisp_Object attribute, Lisp_Object value1, Lisp_Object value2)
3647 if (EQ (value1, Qunspecified) || EQ (value1, QCignore_defface))
3648 return value2;
3649 else if (EQ (attribute, QCheight))
3650 return merge_face_heights (value1, value2, value1);
3651 else
3652 return value1;
3656 DEFUN ("internal-get-lisp-face-attribute", Finternal_get_lisp_face_attribute,
3657 Sinternal_get_lisp_face_attribute,
3658 2, 3, 0,
3659 doc: /* Return face attribute KEYWORD of face SYMBOL.
3660 If SYMBOL does not name a valid Lisp face or KEYWORD isn't a valid
3661 face attribute name, signal an error.
3662 If the optional argument FRAME is given, report on face SYMBOL in that
3663 frame. If FRAME is t, report on the defaults for face SYMBOL (for new
3664 frames). If FRAME is omitted or nil, use the selected frame. */)
3665 (Lisp_Object symbol, Lisp_Object keyword, Lisp_Object frame)
3667 struct frame *f = EQ (frame, Qt) ? NULL : decode_live_frame (frame);
3668 Lisp_Object lface = lface_from_face_name (f, symbol, 1), value = Qnil;
3670 CHECK_SYMBOL (symbol);
3671 CHECK_SYMBOL (keyword);
3673 if (EQ (keyword, QCfamily))
3674 value = LFACE_FAMILY (lface);
3675 else if (EQ (keyword, QCfoundry))
3676 value = LFACE_FOUNDRY (lface);
3677 else if (EQ (keyword, QCheight))
3678 value = LFACE_HEIGHT (lface);
3679 else if (EQ (keyword, QCweight))
3680 value = LFACE_WEIGHT (lface);
3681 else if (EQ (keyword, QCslant))
3682 value = LFACE_SLANT (lface);
3683 else if (EQ (keyword, QCunderline))
3684 value = LFACE_UNDERLINE (lface);
3685 else if (EQ (keyword, QCoverline))
3686 value = LFACE_OVERLINE (lface);
3687 else if (EQ (keyword, QCstrike_through))
3688 value = LFACE_STRIKE_THROUGH (lface);
3689 else if (EQ (keyword, QCbox))
3690 value = LFACE_BOX (lface);
3691 else if (EQ (keyword, QCinverse_video)
3692 || EQ (keyword, QCreverse_video))
3693 value = LFACE_INVERSE (lface);
3694 else if (EQ (keyword, QCforeground))
3695 value = LFACE_FOREGROUND (lface);
3696 else if (EQ (keyword, QCdistant_foreground))
3697 value = LFACE_DISTANT_FOREGROUND (lface);
3698 else if (EQ (keyword, QCbackground))
3699 value = LFACE_BACKGROUND (lface);
3700 else if (EQ (keyword, QCstipple))
3701 value = LFACE_STIPPLE (lface);
3702 else if (EQ (keyword, QCwidth))
3703 value = LFACE_SWIDTH (lface);
3704 else if (EQ (keyword, QCinherit))
3705 value = LFACE_INHERIT (lface);
3706 else if (EQ (keyword, QCfont))
3707 value = LFACE_FONT (lface);
3708 else if (EQ (keyword, QCfontset))
3709 value = LFACE_FONTSET (lface);
3710 else
3711 signal_error ("Invalid face attribute name", keyword);
3713 if (IGNORE_DEFFACE_P (value))
3714 return Qunspecified;
3716 return value;
3720 DEFUN ("internal-lisp-face-attribute-values",
3721 Finternal_lisp_face_attribute_values,
3722 Sinternal_lisp_face_attribute_values, 1, 1, 0,
3723 doc: /* Return a list of valid discrete values for face attribute ATTR.
3724 Value is nil if ATTR doesn't have a discrete set of valid values. */)
3725 (Lisp_Object attr)
3727 Lisp_Object result = Qnil;
3729 CHECK_SYMBOL (attr);
3731 if (EQ (attr, QCunderline) || EQ (attr, QCoverline)
3732 || EQ (attr, QCstrike_through)
3733 || EQ (attr, QCinverse_video) || EQ (attr, QCreverse_video))
3734 result = list2 (Qt, Qnil);
3736 return result;
3740 DEFUN ("internal-merge-in-global-face", Finternal_merge_in_global_face,
3741 Sinternal_merge_in_global_face, 2, 2, 0,
3742 doc: /* Add attributes from frame-default definition of FACE to FACE on FRAME.
3743 Default face attributes override any local face attributes. */)
3744 (Lisp_Object face, Lisp_Object frame)
3746 int i;
3747 Lisp_Object global_lface, local_lface, *gvec, *lvec;
3748 struct frame *f = XFRAME (frame);
3750 CHECK_LIVE_FRAME (frame);
3751 global_lface = lface_from_face_name (NULL, face, 1);
3752 local_lface = lface_from_face_name (f, face, 0);
3753 if (NILP (local_lface))
3754 local_lface = Finternal_make_lisp_face (face, frame);
3756 /* Make every specified global attribute override the local one.
3757 BEWARE!! This is only used from `face-set-after-frame-default' where
3758 the local frame is defined from default specs in `face-defface-spec'
3759 and those should be overridden by global settings. Hence the strange
3760 "global before local" priority. */
3761 lvec = XVECTOR (local_lface)->contents;
3762 gvec = XVECTOR (global_lface)->contents;
3763 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3764 if (IGNORE_DEFFACE_P (gvec[i]))
3765 ASET (local_lface, i, Qunspecified);
3766 else if (! UNSPECIFIEDP (gvec[i]))
3767 ASET (local_lface, i, AREF (global_lface, i));
3769 /* If the default face was changed, update the face cache and the
3770 `font' frame parameter. */
3771 if (EQ (face, Qdefault))
3773 struct face_cache *c = FRAME_FACE_CACHE (f);
3774 struct face *newface, *oldface = FACE_FROM_ID (f, DEFAULT_FACE_ID);
3775 Lisp_Object attrs[LFACE_VECTOR_SIZE];
3777 /* This can be NULL (e.g., in batch mode). */
3778 if (oldface)
3780 /* Ensure that the face vector is fully specified by merging
3781 the previously-cached vector. */
3782 memcpy (attrs, oldface->lface, sizeof attrs);
3783 merge_face_vectors (f, lvec, attrs, 0);
3784 vcopy (local_lface, 0, attrs, LFACE_VECTOR_SIZE);
3785 newface = realize_face (c, lvec, DEFAULT_FACE_ID);
3787 if ((! UNSPECIFIEDP (gvec[LFACE_FAMILY_INDEX])
3788 || ! UNSPECIFIEDP (gvec[LFACE_FOUNDRY_INDEX])
3789 || ! UNSPECIFIEDP (gvec[LFACE_HEIGHT_INDEX])
3790 || ! UNSPECIFIEDP (gvec[LFACE_WEIGHT_INDEX])
3791 || ! UNSPECIFIEDP (gvec[LFACE_SLANT_INDEX])
3792 || ! UNSPECIFIEDP (gvec[LFACE_SWIDTH_INDEX])
3793 || ! UNSPECIFIEDP (gvec[LFACE_FONT_INDEX]))
3794 && newface->font)
3796 Lisp_Object name = newface->font->props[FONT_NAME_INDEX];
3797 Fmodify_frame_parameters (frame, list1 (Fcons (Qfont, name)));
3800 if (STRINGP (gvec[LFACE_FOREGROUND_INDEX]))
3801 Fmodify_frame_parameters (frame,
3802 list1 (Fcons (Qforeground_color,
3803 gvec[LFACE_FOREGROUND_INDEX])));
3805 if (STRINGP (gvec[LFACE_BACKGROUND_INDEX]))
3806 Fmodify_frame_parameters (frame,
3807 list1 (Fcons (Qbackground_color,
3808 gvec[LFACE_BACKGROUND_INDEX])));
3812 return Qnil;
3816 /* The following function is implemented for compatibility with 20.2.
3817 The function is used in x-resolve-fonts when it is asked to
3818 return fonts with the same size as the font of a face. This is
3819 done in fontset.el. */
3821 DEFUN ("face-font", Fface_font, Sface_font, 1, 3, 0,
3822 doc: /* Return the font name of face FACE, or nil if it is unspecified.
3823 The font name is, by default, for ASCII characters.
3824 If the optional argument FRAME is given, report on face FACE in that frame.
3825 If FRAME is t, report on the defaults for face FACE (for new frames).
3826 The font default for a face is either nil, or a list
3827 of the form (bold), (italic) or (bold italic).
3828 If FRAME is omitted or nil, use the selected frame. And, in this case,
3829 if the optional third argument CHARACTER is given,
3830 return the font name used for CHARACTER. */)
3831 (Lisp_Object face, Lisp_Object frame, Lisp_Object character)
3833 if (EQ (frame, Qt))
3835 Lisp_Object result = Qnil;
3836 Lisp_Object lface = lface_from_face_name (NULL, face, 1);
3838 if (!UNSPECIFIEDP (LFACE_WEIGHT (lface))
3839 && !EQ (LFACE_WEIGHT (lface), Qnormal))
3840 result = Fcons (Qbold, result);
3842 if (!UNSPECIFIEDP (LFACE_SLANT (lface))
3843 && !EQ (LFACE_SLANT (lface), Qnormal))
3844 result = Fcons (Qitalic, result);
3846 return result;
3848 else
3850 struct frame *f = decode_live_frame (frame);
3851 int face_id = lookup_named_face (f, face, 1);
3852 struct face *fface = FACE_FROM_ID (f, face_id);
3854 if (! fface)
3855 return Qnil;
3856 #ifdef HAVE_WINDOW_SYSTEM
3857 if (FRAME_WINDOW_P (f) && !NILP (character))
3859 CHECK_CHARACTER (character);
3860 face_id = FACE_FOR_CHAR (f, fface, XINT (character), -1, Qnil);
3861 fface = FACE_FROM_ID (f, face_id);
3863 return (fface->font
3864 ? fface->font->props[FONT_NAME_INDEX]
3865 : Qnil);
3866 #else /* !HAVE_WINDOW_SYSTEM */
3867 return build_string (FRAME_MSDOS_P (f)
3868 ? "ms-dos"
3869 : FRAME_W32_P (f) ? "w32term"
3870 :"tty");
3871 #endif
3876 /* Compare face-attribute values v1 and v2 for equality. Value is non-zero if
3877 all attributes are `equal'. Tries to be fast because this function
3878 is called quite often. */
3880 static bool
3881 face_attr_equal_p (Lisp_Object v1, Lisp_Object v2)
3883 /* Type can differ, e.g. when one attribute is unspecified, i.e. nil,
3884 and the other is specified. */
3885 if (XTYPE (v1) != XTYPE (v2))
3886 return 0;
3888 if (EQ (v1, v2))
3889 return 1;
3891 switch (XTYPE (v1))
3893 case Lisp_String:
3894 if (SBYTES (v1) != SBYTES (v2))
3895 return 0;
3897 return memcmp (SDATA (v1), SDATA (v2), SBYTES (v1)) == 0;
3899 case_Lisp_Int:
3900 case Lisp_Symbol:
3901 return 0;
3903 default:
3904 return !NILP (Fequal (v1, v2));
3909 /* Compare face vectors V1 and V2 for equality. Value is non-zero if
3910 all attributes are `equal'. Tries to be fast because this function
3911 is called quite often. */
3913 static bool
3914 lface_equal_p (Lisp_Object *v1, Lisp_Object *v2)
3916 int i;
3917 bool equal_p = 1;
3919 for (i = 1; i < LFACE_VECTOR_SIZE && equal_p; ++i)
3920 equal_p = face_attr_equal_p (v1[i], v2[i]);
3922 return equal_p;
3926 DEFUN ("internal-lisp-face-equal-p", Finternal_lisp_face_equal_p,
3927 Sinternal_lisp_face_equal_p, 2, 3, 0,
3928 doc: /* True if FACE1 and FACE2 are equal.
3929 If the optional argument FRAME is given, report on FACE1 and FACE2 in that frame.
3930 If FRAME is t, report on the defaults for FACE1 and FACE2 (for new frames).
3931 If FRAME is omitted or nil, use the selected frame. */)
3932 (Lisp_Object face1, Lisp_Object face2, Lisp_Object frame)
3934 int equal_p;
3935 struct frame *f;
3936 Lisp_Object lface1, lface2;
3938 /* Don't use decode_window_system_frame here because this function
3939 is called before X frames exist. At that time, if FRAME is nil,
3940 selected_frame will be used which is the frame dumped with
3941 Emacs. That frame is not an X frame. */
3942 f = EQ (frame, Qt) ? NULL : decode_live_frame (frame);
3944 lface1 = lface_from_face_name (f, face1, 1);
3945 lface2 = lface_from_face_name (f, face2, 1);
3946 equal_p = lface_equal_p (XVECTOR (lface1)->contents,
3947 XVECTOR (lface2)->contents);
3948 return equal_p ? Qt : Qnil;
3952 DEFUN ("internal-lisp-face-empty-p", Finternal_lisp_face_empty_p,
3953 Sinternal_lisp_face_empty_p, 1, 2, 0,
3954 doc: /* True if FACE has no attribute specified.
3955 If the optional argument FRAME is given, report on face FACE in that frame.
3956 If FRAME is t, report on the defaults for face FACE (for new frames).
3957 If FRAME is omitted or nil, use the selected frame. */)
3958 (Lisp_Object face, Lisp_Object frame)
3960 struct frame *f = EQ (frame, Qt) ? NULL : decode_live_frame (frame);
3961 Lisp_Object lface = lface_from_face_name (f, face, 1);
3962 int i;
3964 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3965 if (!UNSPECIFIEDP (AREF (lface, i)))
3966 break;
3968 return i == LFACE_VECTOR_SIZE ? Qt : Qnil;
3972 DEFUN ("frame-face-alist", Fframe_face_alist, Sframe_face_alist,
3973 0, 1, 0,
3974 doc: /* Return an alist of frame-local faces defined on FRAME.
3975 For internal use only. */)
3976 (Lisp_Object frame)
3978 return decode_live_frame (frame)->face_alist;
3982 /* Return a hash code for Lisp string STRING with case ignored. Used
3983 below in computing a hash value for a Lisp face. */
3985 static unsigned
3986 hash_string_case_insensitive (Lisp_Object string)
3988 const unsigned char *s;
3989 unsigned hash = 0;
3990 eassert (STRINGP (string));
3991 for (s = SDATA (string); *s; ++s)
3992 hash = (hash << 1) ^ c_tolower (*s);
3993 return hash;
3997 /* Return a hash code for face attribute vector V. */
3999 static unsigned
4000 lface_hash (Lisp_Object *v)
4002 return (hash_string_case_insensitive (v[LFACE_FAMILY_INDEX])
4003 ^ hash_string_case_insensitive (v[LFACE_FOUNDRY_INDEX])
4004 ^ hash_string_case_insensitive (v[LFACE_FOREGROUND_INDEX])
4005 ^ hash_string_case_insensitive (v[LFACE_BACKGROUND_INDEX])
4006 ^ XHASH (v[LFACE_WEIGHT_INDEX])
4007 ^ XHASH (v[LFACE_SLANT_INDEX])
4008 ^ XHASH (v[LFACE_SWIDTH_INDEX])
4009 ^ XHASH (v[LFACE_HEIGHT_INDEX]));
4012 #ifdef HAVE_WINDOW_SYSTEM
4014 /* Return non-zero if LFACE1 and LFACE2 specify the same font (without
4015 considering charsets/registries). They do if they specify the same
4016 family, point size, weight, width, slant, and font. Both
4017 LFACE1 and LFACE2 must be fully-specified. */
4019 static int
4020 lface_same_font_attributes_p (Lisp_Object *lface1, Lisp_Object *lface2)
4022 eassert (lface_fully_specified_p (lface1)
4023 && lface_fully_specified_p (lface2));
4024 return (xstrcasecmp (SSDATA (lface1[LFACE_FAMILY_INDEX]),
4025 SSDATA (lface2[LFACE_FAMILY_INDEX])) == 0
4026 && xstrcasecmp (SSDATA (lface1[LFACE_FOUNDRY_INDEX]),
4027 SSDATA (lface2[LFACE_FOUNDRY_INDEX])) == 0
4028 && EQ (lface1[LFACE_HEIGHT_INDEX], lface2[LFACE_HEIGHT_INDEX])
4029 && EQ (lface1[LFACE_SWIDTH_INDEX], lface2[LFACE_SWIDTH_INDEX])
4030 && EQ (lface1[LFACE_WEIGHT_INDEX], lface2[LFACE_WEIGHT_INDEX])
4031 && EQ (lface1[LFACE_SLANT_INDEX], lface2[LFACE_SLANT_INDEX])
4032 && EQ (lface1[LFACE_FONT_INDEX], lface2[LFACE_FONT_INDEX])
4033 && (EQ (lface1[LFACE_FONTSET_INDEX], lface2[LFACE_FONTSET_INDEX])
4034 || (STRINGP (lface1[LFACE_FONTSET_INDEX])
4035 && STRINGP (lface2[LFACE_FONTSET_INDEX])
4036 && ! xstrcasecmp (SSDATA (lface1[LFACE_FONTSET_INDEX]),
4037 SSDATA (lface2[LFACE_FONTSET_INDEX]))))
4041 #endif /* HAVE_WINDOW_SYSTEM */
4043 /***********************************************************************
4044 Realized Faces
4045 ***********************************************************************/
4047 /* Allocate and return a new realized face for Lisp face attribute
4048 vector ATTR. */
4050 static struct face *
4051 make_realized_face (Lisp_Object *attr)
4053 enum { off = offsetof (struct face, id) };
4054 struct face *face = xmalloc (sizeof *face);
4056 memcpy (face->lface, attr, sizeof face->lface);
4057 memset (&face->id, 0, sizeof *face - off);
4058 face->ascii_face = face;
4060 return face;
4064 /* Free realized face FACE, including its X resources. FACE may
4065 be null. */
4067 static void
4068 free_realized_face (struct frame *f, struct face *face)
4070 if (face)
4072 #ifdef HAVE_WINDOW_SYSTEM
4073 if (FRAME_WINDOW_P (f))
4075 /* Free fontset of FACE if it is ASCII face. */
4076 if (face->fontset >= 0 && face == face->ascii_face)
4077 free_face_fontset (f, face);
4078 if (face->gc)
4080 block_input ();
4081 if (face->font)
4082 font_done_for_face (f, face);
4083 x_free_gc (f, face->gc);
4084 face->gc = 0;
4085 unblock_input ();
4087 #ifdef HAVE_X_WINDOWS
4088 free_face_colors (f, face);
4089 #endif /* HAVE_X_WINDOWS */
4090 x_destroy_bitmap (f, face->stipple);
4092 #endif /* HAVE_WINDOW_SYSTEM */
4094 xfree (face);
4099 /* Prepare face FACE for subsequent display on frame F. This
4100 allocated GCs if they haven't been allocated yet or have been freed
4101 by clearing the face cache. */
4103 void
4104 prepare_face_for_display (struct frame *f, struct face *face)
4106 #ifdef HAVE_WINDOW_SYSTEM
4107 eassert (FRAME_WINDOW_P (f));
4109 if (face->gc == 0)
4111 XGCValues xgcv;
4112 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
4114 xgcv.foreground = face->foreground;
4115 xgcv.background = face->background;
4116 #ifdef HAVE_X_WINDOWS
4117 xgcv.graphics_exposures = False;
4118 #endif
4120 block_input ();
4121 #ifdef HAVE_X_WINDOWS
4122 if (face->stipple)
4124 xgcv.fill_style = FillOpaqueStippled;
4125 xgcv.stipple = x_bitmap_pixmap (f, face->stipple);
4126 mask |= GCFillStyle | GCStipple;
4128 #endif
4129 face->gc = x_create_gc (f, mask, &xgcv);
4130 if (face->font)
4131 font_prepare_for_face (f, face);
4132 unblock_input ();
4134 #endif /* HAVE_WINDOW_SYSTEM */
4138 /* Returns the `distance' between the colors X and Y. */
4140 static int
4141 color_distance (XColor *x, XColor *y)
4143 /* This formula is from a paper titled `Colour metric' by Thiadmer Riemersma.
4144 Quoting from that paper:
4146 This formula has results that are very close to L*u*v* (with the
4147 modified lightness curve) and, more importantly, it is a more even
4148 algorithm: it does not have a range of colors where it suddenly
4149 gives far from optimal results.
4151 See <http://www.compuphase.com/cmetric.htm> for more info. */
4153 long r = (x->red - y->red) >> 8;
4154 long g = (x->green - y->green) >> 8;
4155 long b = (x->blue - y->blue) >> 8;
4156 long r_mean = (x->red + y->red) >> 9;
4158 return
4159 (((512 + r_mean) * r * r) >> 8)
4160 + 4 * g * g
4161 + (((767 - r_mean) * b * b) >> 8);
4165 DEFUN ("color-distance", Fcolor_distance, Scolor_distance, 2, 3, 0,
4166 doc: /* Return an integer distance between COLOR1 and COLOR2 on FRAME.
4167 COLOR1 and COLOR2 may be either strings containing the color name,
4168 or lists of the form (RED GREEN BLUE).
4169 If FRAME is unspecified or nil, the current frame is used. */)
4170 (Lisp_Object color1, Lisp_Object color2, Lisp_Object frame)
4172 struct frame *f = decode_live_frame (frame);
4173 XColor cdef1, cdef2;
4175 if (!(CONSP (color1) && parse_rgb_list (color1, &cdef1))
4176 && !(STRINGP (color1) && defined_color (f, SSDATA (color1), &cdef1, 0)))
4177 signal_error ("Invalid color", color1);
4178 if (!(CONSP (color2) && parse_rgb_list (color2, &cdef2))
4179 && !(STRINGP (color2) && defined_color (f, SSDATA (color2), &cdef2, 0)))
4180 signal_error ("Invalid color", color2);
4182 return make_number (color_distance (&cdef1, &cdef2));
4186 /***********************************************************************
4187 Face Cache
4188 ***********************************************************************/
4190 /* Return a new face cache for frame F. */
4192 static struct face_cache *
4193 make_face_cache (struct frame *f)
4195 struct face_cache *c = xmalloc (sizeof *c);
4197 c->buckets = xzalloc (FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets);
4198 c->size = 50;
4199 c->used = 0;
4200 c->faces_by_id = xmalloc (c->size * sizeof *c->faces_by_id);
4201 c->f = f;
4202 c->menu_face_changed_p = menu_face_changed_default;
4203 return c;
4206 #ifdef HAVE_WINDOW_SYSTEM
4208 /* Clear out all graphics contexts for all realized faces, except for
4209 the basic faces. This should be done from time to time just to avoid
4210 keeping too many graphics contexts that are no longer needed. */
4212 static void
4213 clear_face_gcs (struct face_cache *c)
4215 if (c && FRAME_WINDOW_P (c->f))
4217 int i;
4218 for (i = BASIC_FACE_ID_SENTINEL; i < c->used; ++i)
4220 struct face *face = c->faces_by_id[i];
4221 if (face && face->gc)
4223 block_input ();
4224 if (face->font)
4225 font_done_for_face (c->f, face);
4226 x_free_gc (c->f, face->gc);
4227 face->gc = 0;
4228 unblock_input ();
4234 #endif /* HAVE_WINDOW_SYSTEM */
4236 /* Free all realized faces in face cache C, including basic faces.
4237 C may be null. If faces are freed, make sure the frame's current
4238 matrix is marked invalid, so that a display caused by an expose
4239 event doesn't try to use faces we destroyed. */
4241 static void
4242 free_realized_faces (struct face_cache *c)
4244 if (c && c->used)
4246 int i, size;
4247 struct frame *f = c->f;
4249 /* We must block input here because we can't process X events
4250 safely while only some faces are freed, or when the frame's
4251 current matrix still references freed faces. */
4252 block_input ();
4254 for (i = 0; i < c->used; ++i)
4256 free_realized_face (f, c->faces_by_id[i]);
4257 c->faces_by_id[i] = NULL;
4260 c->used = 0;
4261 size = FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
4262 memset (c->buckets, 0, size);
4264 /* Must do a thorough redisplay the next time. Mark current
4265 matrices as invalid because they will reference faces freed
4266 above. This function is also called when a frame is
4267 destroyed. In this case, the root window of F is nil. */
4268 if (WINDOWP (f->root_window))
4270 clear_current_matrices (f);
4271 windows_or_buffers_changed = 58;
4274 unblock_input ();
4279 /* Free all realized faces on FRAME or on all frames if FRAME is nil.
4280 This is done after attributes of a named face have been changed,
4281 because we can't tell which realized faces depend on that face. */
4283 void
4284 free_all_realized_faces (Lisp_Object frame)
4286 if (NILP (frame))
4288 Lisp_Object rest;
4289 FOR_EACH_FRAME (rest, frame)
4290 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
4292 else
4293 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
4297 /* Free face cache C and faces in it, including their X resources. */
4299 static void
4300 free_face_cache (struct face_cache *c)
4302 if (c)
4304 free_realized_faces (c);
4305 xfree (c->buckets);
4306 xfree (c->faces_by_id);
4307 xfree (c);
4312 /* Cache realized face FACE in face cache C. HASH is the hash value
4313 of FACE. If FACE is for ASCII characters (i.e. FACE->ascii_face ==
4314 FACE), insert the new face to the beginning of the collision list
4315 of the face hash table of C. Otherwise, add the new face to the
4316 end of the collision list. This way, lookup_face can quickly find
4317 that a requested face is not cached. */
4319 static void
4320 cache_face (struct face_cache *c, struct face *face, unsigned int hash)
4322 int i = hash % FACE_CACHE_BUCKETS_SIZE;
4324 face->hash = hash;
4326 if (face->ascii_face != face)
4328 struct face *last = c->buckets[i];
4329 if (last)
4331 while (last->next)
4332 last = last->next;
4333 last->next = face;
4334 face->prev = last;
4335 face->next = NULL;
4337 else
4339 c->buckets[i] = face;
4340 face->prev = face->next = NULL;
4343 else
4345 face->prev = NULL;
4346 face->next = c->buckets[i];
4347 if (face->next)
4348 face->next->prev = face;
4349 c->buckets[i] = face;
4352 /* Find a free slot in C->faces_by_id and use the index of the free
4353 slot as FACE->id. */
4354 for (i = 0; i < c->used; ++i)
4355 if (c->faces_by_id[i] == NULL)
4356 break;
4357 face->id = i;
4359 #ifdef GLYPH_DEBUG
4360 /* Check that FACE got a unique id. */
4362 int j, n;
4363 struct face *face1;
4365 for (j = n = 0; j < FACE_CACHE_BUCKETS_SIZE; ++j)
4366 for (face1 = c->buckets[j]; face1; face1 = face1->next)
4367 if (face1->id == i)
4368 ++n;
4370 eassert (n == 1);
4372 #endif /* GLYPH_DEBUG */
4374 /* Maybe enlarge C->faces_by_id. */
4375 if (i == c->used)
4377 if (c->used == c->size)
4378 c->faces_by_id = xpalloc (c->faces_by_id, &c->size, 1, MAX_FACE_ID,
4379 sizeof *c->faces_by_id);
4380 c->used++;
4383 c->faces_by_id[i] = face;
4387 /* Remove face FACE from cache C. */
4389 static void
4390 uncache_face (struct face_cache *c, struct face *face)
4392 int i = face->hash % FACE_CACHE_BUCKETS_SIZE;
4394 if (face->prev)
4395 face->prev->next = face->next;
4396 else
4397 c->buckets[i] = face->next;
4399 if (face->next)
4400 face->next->prev = face->prev;
4402 c->faces_by_id[face->id] = NULL;
4403 if (face->id == c->used)
4404 --c->used;
4408 /* Look up a realized face with face attributes ATTR in the face cache
4409 of frame F. The face will be used to display ASCII characters.
4410 Value is the ID of the face found. If no suitable face is found,
4411 realize a new one. */
4413 static int
4414 lookup_face (struct frame *f, Lisp_Object *attr)
4416 struct face_cache *cache = FRAME_FACE_CACHE (f);
4417 unsigned hash;
4418 int i;
4419 struct face *face;
4421 eassert (cache != NULL);
4422 check_lface_attrs (attr);
4424 /* Look up ATTR in the face cache. */
4425 hash = lface_hash (attr);
4426 i = hash % FACE_CACHE_BUCKETS_SIZE;
4428 for (face = cache->buckets[i]; face; face = face->next)
4430 if (face->ascii_face != face)
4432 /* There's no more ASCII face. */
4433 face = NULL;
4434 break;
4436 if (face->hash == hash
4437 && lface_equal_p (face->lface, attr))
4438 break;
4441 /* If not found, realize a new face. */
4442 if (face == NULL)
4443 face = realize_face (cache, attr, -1);
4445 #ifdef GLYPH_DEBUG
4446 eassert (face == FACE_FROM_ID (f, face->id));
4447 #endif /* GLYPH_DEBUG */
4449 return face->id;
4452 #ifdef HAVE_WINDOW_SYSTEM
4453 /* Look up a realized face that has the same attributes as BASE_FACE
4454 except for the font in the face cache of frame F. If FONT-OBJECT
4455 is not nil, it is an already opened font. If FONT-OBJECT is nil,
4456 the face has no font. Value is the ID of the face found. If no
4457 suitable face is found, realize a new one. */
4460 face_for_font (struct frame *f, Lisp_Object font_object, struct face *base_face)
4462 struct face_cache *cache = FRAME_FACE_CACHE (f);
4463 unsigned hash;
4464 int i;
4465 struct face *face;
4467 eassert (cache != NULL);
4468 base_face = base_face->ascii_face;
4469 hash = lface_hash (base_face->lface);
4470 i = hash % FACE_CACHE_BUCKETS_SIZE;
4472 for (face = cache->buckets[i]; face; face = face->next)
4474 if (face->ascii_face == face)
4475 continue;
4476 if (face->ascii_face == base_face
4477 && face->font == (NILP (font_object) ? NULL
4478 : XFONT_OBJECT (font_object))
4479 && lface_equal_p (face->lface, base_face->lface))
4480 return face->id;
4483 /* If not found, realize a new face. */
4484 face = realize_non_ascii_face (f, font_object, base_face);
4485 return face->id;
4487 #endif /* HAVE_WINDOW_SYSTEM */
4489 /* Return the face id of the realized face for named face SYMBOL on
4490 frame F suitable for displaying ASCII characters. Value is -1 if
4491 the face couldn't be determined, which might happen if the default
4492 face isn't realized and cannot be realized. */
4495 lookup_named_face (struct frame *f, Lisp_Object symbol, int signal_p)
4497 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4498 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
4499 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4501 if (default_face == NULL)
4503 if (!realize_basic_faces (f))
4504 return -1;
4505 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4506 if (default_face == NULL)
4507 emacs_abort (); /* realize_basic_faces must have set it up */
4510 if (! get_lface_attributes (f, symbol, symbol_attrs, signal_p, 0))
4511 return -1;
4513 memcpy (attrs, default_face->lface, sizeof attrs);
4514 merge_face_vectors (f, symbol_attrs, attrs, 0);
4516 return lookup_face (f, attrs);
4520 /* Return the display face-id of the basic face whose canonical face-id
4521 is FACE_ID. The return value will usually simply be FACE_ID, unless that
4522 basic face has bee remapped via Vface_remapping_alist. This function is
4523 conservative: if something goes wrong, it will simply return FACE_ID
4524 rather than signal an error. */
4527 lookup_basic_face (struct frame *f, int face_id)
4529 Lisp_Object name, mapping;
4530 int remapped_face_id;
4532 if (NILP (Vface_remapping_alist))
4533 return face_id; /* Nothing to do. */
4535 switch (face_id)
4537 case DEFAULT_FACE_ID: name = Qdefault; break;
4538 case MODE_LINE_FACE_ID: name = Qmode_line; break;
4539 case MODE_LINE_INACTIVE_FACE_ID: name = Qmode_line_inactive; break;
4540 case HEADER_LINE_FACE_ID: name = Qheader_line; break;
4541 case TOOL_BAR_FACE_ID: name = Qtool_bar; break;
4542 case FRINGE_FACE_ID: name = Qfringe; break;
4543 case SCROLL_BAR_FACE_ID: name = Qscroll_bar; break;
4544 case BORDER_FACE_ID: name = Qborder; break;
4545 case CURSOR_FACE_ID: name = Qcursor; break;
4546 case MOUSE_FACE_ID: name = Qmouse; break;
4547 case MENU_FACE_ID: name = Qmenu; break;
4549 default:
4550 emacs_abort (); /* the caller is supposed to pass us a basic face id */
4553 /* Do a quick scan through Vface_remapping_alist, and return immediately
4554 if there is no remapping for face NAME. This is just an optimization
4555 for the very common no-remapping case. */
4556 mapping = assq_no_quit (name, Vface_remapping_alist);
4557 if (NILP (mapping))
4558 return face_id; /* Give up. */
4560 /* If there is a remapping entry, lookup the face using NAME, which will
4561 handle the remapping too. */
4562 remapped_face_id = lookup_named_face (f, name, 0);
4563 if (remapped_face_id < 0)
4564 return face_id; /* Give up. */
4566 return remapped_face_id;
4570 /* Return a face for charset ASCII that is like the face with id
4571 FACE_ID on frame F, but has a font that is STEPS steps smaller.
4572 STEPS < 0 means larger. Value is the id of the face. */
4575 smaller_face (struct frame *f, int face_id, int steps)
4577 #ifdef HAVE_WINDOW_SYSTEM
4578 struct face *face;
4579 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4580 int pt, last_pt, last_height;
4581 int delta;
4582 int new_face_id;
4583 struct face *new_face;
4585 /* If not called for an X frame, just return the original face. */
4586 if (FRAME_TERMCAP_P (f))
4587 return face_id;
4589 /* Try in increments of 1/2 pt. */
4590 delta = steps < 0 ? 5 : -5;
4591 steps = eabs (steps);
4593 face = FACE_FROM_ID (f, face_id);
4594 memcpy (attrs, face->lface, sizeof attrs);
4595 pt = last_pt = XFASTINT (attrs[LFACE_HEIGHT_INDEX]);
4596 new_face_id = face_id;
4597 last_height = FONT_HEIGHT (face->font);
4599 while (steps
4600 && pt + delta > 0
4601 /* Give up if we cannot find a font within 10pt. */
4602 && eabs (last_pt - pt) < 100)
4604 /* Look up a face for a slightly smaller/larger font. */
4605 pt += delta;
4606 attrs[LFACE_HEIGHT_INDEX] = make_number (pt);
4607 new_face_id = lookup_face (f, attrs);
4608 new_face = FACE_FROM_ID (f, new_face_id);
4610 /* If height changes, count that as one step. */
4611 if ((delta < 0 && FONT_HEIGHT (new_face->font) < last_height)
4612 || (delta > 0 && FONT_HEIGHT (new_face->font) > last_height))
4614 --steps;
4615 last_height = FONT_HEIGHT (new_face->font);
4616 last_pt = pt;
4620 return new_face_id;
4622 #else /* not HAVE_WINDOW_SYSTEM */
4624 return face_id;
4626 #endif /* not HAVE_WINDOW_SYSTEM */
4630 /* Return a face for charset ASCII that is like the face with id
4631 FACE_ID on frame F, but has height HEIGHT. */
4634 face_with_height (struct frame *f, int face_id, int height)
4636 #ifdef HAVE_WINDOW_SYSTEM
4637 struct face *face;
4638 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4640 if (FRAME_TERMCAP_P (f)
4641 || height <= 0)
4642 return face_id;
4644 face = FACE_FROM_ID (f, face_id);
4645 memcpy (attrs, face->lface, sizeof attrs);
4646 attrs[LFACE_HEIGHT_INDEX] = make_number (height);
4647 font_clear_prop (attrs, FONT_SIZE_INDEX);
4648 face_id = lookup_face (f, attrs);
4649 #endif /* HAVE_WINDOW_SYSTEM */
4651 return face_id;
4655 /* Return the face id of the realized face for named face SYMBOL on
4656 frame F suitable for displaying ASCII characters, and use
4657 attributes of the face FACE_ID for attributes that aren't
4658 completely specified by SYMBOL. This is like lookup_named_face,
4659 except that the default attributes come from FACE_ID, not from the
4660 default face. FACE_ID is assumed to be already realized. */
4663 lookup_derived_face (struct frame *f, Lisp_Object symbol, int face_id,
4664 int signal_p)
4666 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4667 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
4668 struct face *default_face = FACE_FROM_ID (f, face_id);
4670 if (!default_face)
4671 emacs_abort ();
4673 if (!get_lface_attributes (f, symbol, symbol_attrs, signal_p, 0))
4674 return -1;
4676 memcpy (attrs, default_face->lface, sizeof attrs);
4677 merge_face_vectors (f, symbol_attrs, attrs, 0);
4678 return lookup_face (f, attrs);
4681 DEFUN ("face-attributes-as-vector", Fface_attributes_as_vector,
4682 Sface_attributes_as_vector, 1, 1, 0,
4683 doc: /* Return a vector of face attributes corresponding to PLIST. */)
4684 (Lisp_Object plist)
4686 Lisp_Object lface;
4687 lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
4688 Qunspecified);
4689 merge_face_ref (XFRAME (selected_frame), plist, XVECTOR (lface)->contents,
4690 1, 0);
4691 return lface;
4696 /***********************************************************************
4697 Face capability testing
4698 ***********************************************************************/
4701 /* If the distance (as returned by color_distance) between two colors is
4702 less than this, then they are considered the same, for determining
4703 whether a color is supported or not. The range of values is 0-65535. */
4705 #define TTY_SAME_COLOR_THRESHOLD 10000
4707 #ifdef HAVE_WINDOW_SYSTEM
4709 /* Return true if all the face attributes in ATTRS are supported
4710 on the window-system frame F.
4712 The definition of `supported' is somewhat heuristic, but basically means
4713 that a face containing all the attributes in ATTRS, when merged with the
4714 default face for display, can be represented in a way that's
4716 \(1) different in appearance than the default face, and
4717 \(2) `close in spirit' to what the attributes specify, if not exact. */
4719 static bool
4720 x_supports_face_attributes_p (struct frame *f,
4721 Lisp_Object attrs[LFACE_VECTOR_SIZE],
4722 struct face *def_face)
4724 Lisp_Object *def_attrs = def_face->lface;
4726 /* Check that other specified attributes are different that the default
4727 face. */
4728 if ((!UNSPECIFIEDP (attrs[LFACE_UNDERLINE_INDEX])
4729 && face_attr_equal_p (attrs[LFACE_UNDERLINE_INDEX],
4730 def_attrs[LFACE_UNDERLINE_INDEX]))
4731 || (!UNSPECIFIEDP (attrs[LFACE_INVERSE_INDEX])
4732 && face_attr_equal_p (attrs[LFACE_INVERSE_INDEX],
4733 def_attrs[LFACE_INVERSE_INDEX]))
4734 || (!UNSPECIFIEDP (attrs[LFACE_FOREGROUND_INDEX])
4735 && face_attr_equal_p (attrs[LFACE_FOREGROUND_INDEX],
4736 def_attrs[LFACE_FOREGROUND_INDEX]))
4737 || (!UNSPECIFIEDP (attrs[LFACE_DISTANT_FOREGROUND_INDEX])
4738 && face_attr_equal_p (attrs[LFACE_DISTANT_FOREGROUND_INDEX],
4739 def_attrs[LFACE_DISTANT_FOREGROUND_INDEX]))
4740 || (!UNSPECIFIEDP (attrs[LFACE_BACKGROUND_INDEX])
4741 && face_attr_equal_p (attrs[LFACE_BACKGROUND_INDEX],
4742 def_attrs[LFACE_BACKGROUND_INDEX]))
4743 || (!UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
4744 && face_attr_equal_p (attrs[LFACE_STIPPLE_INDEX],
4745 def_attrs[LFACE_STIPPLE_INDEX]))
4746 || (!UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
4747 && face_attr_equal_p (attrs[LFACE_OVERLINE_INDEX],
4748 def_attrs[LFACE_OVERLINE_INDEX]))
4749 || (!UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
4750 && face_attr_equal_p (attrs[LFACE_STRIKE_THROUGH_INDEX],
4751 def_attrs[LFACE_STRIKE_THROUGH_INDEX]))
4752 || (!UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
4753 && face_attr_equal_p (attrs[LFACE_BOX_INDEX],
4754 def_attrs[LFACE_BOX_INDEX])))
4755 return 0;
4757 /* Check font-related attributes, as those are the most commonly
4758 "unsupported" on a window-system (because of missing fonts). */
4759 if (!UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
4760 || !UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
4761 || !UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
4762 || !UNSPECIFIEDP (attrs[LFACE_WEIGHT_INDEX])
4763 || !UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX])
4764 || !UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX]))
4766 int face_id;
4767 struct face *face;
4768 Lisp_Object merged_attrs[LFACE_VECTOR_SIZE];
4769 int i;
4771 memcpy (merged_attrs, def_attrs, sizeof merged_attrs);
4773 merge_face_vectors (f, attrs, merged_attrs, 0);
4775 face_id = lookup_face (f, merged_attrs);
4776 face = FACE_FROM_ID (f, face_id);
4778 if (! face)
4779 error ("Cannot make face");
4781 /* If the font is the same, or no font is found, then not
4782 supported. */
4783 if (face->font == def_face->font
4784 || ! face->font)
4785 return 0;
4786 for (i = FONT_TYPE_INDEX; i <= FONT_SIZE_INDEX; i++)
4787 if (! EQ (face->font->props[i], def_face->font->props[i]))
4789 Lisp_Object s1, s2;
4791 if (i < FONT_FOUNDRY_INDEX || i > FONT_REGISTRY_INDEX
4792 || face->font->driver->case_sensitive)
4793 return 1;
4794 s1 = SYMBOL_NAME (face->font->props[i]);
4795 s2 = SYMBOL_NAME (def_face->font->props[i]);
4796 if (! EQ (Fcompare_strings (s1, make_number (0), Qnil,
4797 s2, make_number (0), Qnil, Qt), Qt))
4798 return 1;
4800 return 0;
4803 /* Everything checks out, this face is supported. */
4804 return 1;
4807 #endif /* HAVE_WINDOW_SYSTEM */
4809 /* Return true if all the face attributes in ATTRS are supported
4810 on the tty frame F.
4812 The definition of `supported' is somewhat heuristic, but basically means
4813 that a face containing all the attributes in ATTRS, when merged
4814 with the default face for display, can be represented in a way that's
4816 \(1) different in appearance than the default face, and
4817 \(2) `close in spirit' to what the attributes specify, if not exact.
4819 Point (2) implies that a `:weight black' attribute will be satisfied
4820 by any terminal that can display bold, and a `:foreground "yellow"' as
4821 long as the terminal can display a yellowish color, but `:slant italic'
4822 will _not_ be satisfied by the tty display code's automatic
4823 substitution of a `dim' face for italic. */
4825 static bool
4826 tty_supports_face_attributes_p (struct frame *f,
4827 Lisp_Object attrs[LFACE_VECTOR_SIZE],
4828 struct face *def_face)
4830 int weight, slant;
4831 Lisp_Object val, fg, bg;
4832 XColor fg_tty_color, fg_std_color;
4833 XColor bg_tty_color, bg_std_color;
4834 unsigned test_caps = 0;
4835 Lisp_Object *def_attrs = def_face->lface;
4837 /* First check some easy-to-check stuff; ttys support none of the
4838 following attributes, so we can just return false if any are requested
4839 (even if `nominal' values are specified, we should still return false,
4840 as that will be the same value that the default face uses). We
4841 consider :slant unsupportable on ttys, even though the face code
4842 actually `fakes' them using a dim attribute if possible. This is
4843 because the faked result is too different from what the face
4844 specifies. */
4845 if (!UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
4846 || !UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
4847 || !UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
4848 || !UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
4849 || !UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
4850 || !UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
4851 || !UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
4852 || !UNSPECIFIEDP (attrs[LFACE_BOX_INDEX]))
4853 return 0;
4855 /* Test for terminal `capabilities' (non-color character attributes). */
4857 /* font weight (bold/dim) */
4858 val = attrs[LFACE_WEIGHT_INDEX];
4859 if (!UNSPECIFIEDP (val)
4860 && (weight = FONT_WEIGHT_NAME_NUMERIC (val), weight >= 0))
4862 int def_weight = FONT_WEIGHT_NAME_NUMERIC (def_attrs[LFACE_WEIGHT_INDEX]);
4864 if (weight > 100)
4866 if (def_weight > 100)
4867 return 0; /* same as default */
4868 test_caps = TTY_CAP_BOLD;
4870 else if (weight < 100)
4872 if (def_weight < 100)
4873 return 0; /* same as default */
4874 test_caps = TTY_CAP_DIM;
4876 else if (def_weight == 100)
4877 return 0; /* same as default */
4880 /* font slant */
4881 val = attrs[LFACE_SLANT_INDEX];
4882 if (!UNSPECIFIEDP (val)
4883 && (slant = FONT_SLANT_NAME_NUMERIC (val), slant >= 0))
4885 int def_slant = FONT_SLANT_NAME_NUMERIC (def_attrs[LFACE_SLANT_INDEX]);
4886 if (slant == 100 || slant == def_slant)
4887 return 0; /* same as default */
4888 else
4889 test_caps |= TTY_CAP_ITALIC;
4892 /* underlining */
4893 val = attrs[LFACE_UNDERLINE_INDEX];
4894 if (!UNSPECIFIEDP (val))
4896 if (STRINGP (val))
4897 return 0; /* ttys can't use colored underlines */
4898 else if (EQ (CAR_SAFE (val), QCstyle) && EQ (CAR_SAFE (CDR_SAFE (val)), Qwave))
4899 return 0; /* ttys can't use wave underlines */
4900 else if (face_attr_equal_p (val, def_attrs[LFACE_UNDERLINE_INDEX]))
4901 return 0; /* same as default */
4902 else
4903 test_caps |= TTY_CAP_UNDERLINE;
4906 /* inverse video */
4907 val = attrs[LFACE_INVERSE_INDEX];
4908 if (!UNSPECIFIEDP (val))
4910 if (face_attr_equal_p (val, def_attrs[LFACE_INVERSE_INDEX]))
4911 return 0; /* same as default */
4912 else
4913 test_caps |= TTY_CAP_INVERSE;
4917 /* Color testing. */
4919 /* Check if foreground color is close enough. */
4920 fg = attrs[LFACE_FOREGROUND_INDEX];
4921 if (STRINGP (fg))
4923 Lisp_Object def_fg = def_attrs[LFACE_FOREGROUND_INDEX];
4925 if (face_attr_equal_p (fg, def_fg))
4926 return 0; /* same as default */
4927 else if (! tty_lookup_color (f, fg, &fg_tty_color, &fg_std_color))
4928 return 0; /* not a valid color */
4929 else if (color_distance (&fg_tty_color, &fg_std_color)
4930 > TTY_SAME_COLOR_THRESHOLD)
4931 return 0; /* displayed color is too different */
4932 else
4933 /* Make sure the color is really different than the default. */
4935 XColor def_fg_color;
4936 if (tty_lookup_color (f, def_fg, &def_fg_color, 0)
4937 && (color_distance (&fg_tty_color, &def_fg_color)
4938 <= TTY_SAME_COLOR_THRESHOLD))
4939 return 0;
4943 /* Check if background color is close enough. */
4944 bg = attrs[LFACE_BACKGROUND_INDEX];
4945 if (STRINGP (bg))
4947 Lisp_Object def_bg = def_attrs[LFACE_BACKGROUND_INDEX];
4949 if (face_attr_equal_p (bg, def_bg))
4950 return 0; /* same as default */
4951 else if (! tty_lookup_color (f, bg, &bg_tty_color, &bg_std_color))
4952 return 0; /* not a valid color */
4953 else if (color_distance (&bg_tty_color, &bg_std_color)
4954 > TTY_SAME_COLOR_THRESHOLD)
4955 return 0; /* displayed color is too different */
4956 else
4957 /* Make sure the color is really different than the default. */
4959 XColor def_bg_color;
4960 if (tty_lookup_color (f, def_bg, &def_bg_color, 0)
4961 && (color_distance (&bg_tty_color, &def_bg_color)
4962 <= TTY_SAME_COLOR_THRESHOLD))
4963 return 0;
4967 /* If both foreground and background are requested, see if the
4968 distance between them is OK. We just check to see if the distance
4969 between the tty's foreground and background is close enough to the
4970 distance between the standard foreground and background. */
4971 if (STRINGP (fg) && STRINGP (bg))
4973 int delta_delta
4974 = (color_distance (&fg_std_color, &bg_std_color)
4975 - color_distance (&fg_tty_color, &bg_tty_color));
4976 if (delta_delta > TTY_SAME_COLOR_THRESHOLD
4977 || delta_delta < -TTY_SAME_COLOR_THRESHOLD)
4978 return 0;
4982 /* See if the capabilities we selected above are supported, with the
4983 given colors. */
4984 return tty_capable_p (FRAME_TTY (f), test_caps);
4988 DEFUN ("display-supports-face-attributes-p",
4989 Fdisplay_supports_face_attributes_p, Sdisplay_supports_face_attributes_p,
4990 1, 2, 0,
4991 doc: /* Return non-nil if all the face attributes in ATTRIBUTES are supported.
4992 The optional argument DISPLAY can be a display name, a frame, or
4993 nil (meaning the selected frame's display).
4995 The definition of `supported' is somewhat heuristic, but basically means
4996 that a face containing all the attributes in ATTRIBUTES, when merged
4997 with the default face for display, can be represented in a way that's
4999 \(1) different in appearance than the default face, and
5000 \(2) `close in spirit' to what the attributes specify, if not exact.
5002 Point (2) implies that a `:weight black' attribute will be satisfied by
5003 any display that can display bold, and a `:foreground \"yellow\"' as long
5004 as it can display a yellowish color, but `:slant italic' will _not_ be
5005 satisfied by the tty display code's automatic substitution of a `dim'
5006 face for italic. */)
5007 (Lisp_Object attributes, Lisp_Object display)
5009 bool supports = 0;
5010 int i;
5011 Lisp_Object frame;
5012 struct frame *f;
5013 struct face *def_face;
5014 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5016 if (noninteractive || !initialized)
5017 /* We may not be able to access low-level face information in batch
5018 mode, or before being dumped, and this function is not going to
5019 be very useful in those cases anyway, so just give up. */
5020 return Qnil;
5022 if (NILP (display))
5023 frame = selected_frame;
5024 else if (FRAMEP (display))
5025 frame = display;
5026 else
5028 /* Find any frame on DISPLAY. */
5029 Lisp_Object tail;
5031 frame = Qnil;
5032 FOR_EACH_FRAME (tail, frame)
5033 if (!NILP (Fequal (Fcdr (Fassq (Qdisplay,
5034 XFRAME (frame)->param_alist)),
5035 display)))
5036 break;
5039 CHECK_LIVE_FRAME (frame);
5040 f = XFRAME (frame);
5042 for (i = 0; i < LFACE_VECTOR_SIZE; i++)
5043 attrs[i] = Qunspecified;
5044 merge_face_ref (f, attributes, attrs, 1, 0);
5046 def_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5047 if (def_face == NULL)
5049 if (! realize_basic_faces (f))
5050 error ("Cannot realize default face");
5051 def_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5052 if (def_face == NULL)
5053 emacs_abort (); /* realize_basic_faces must have set it up */
5056 /* Dispatch to the appropriate handler. */
5057 if (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
5058 supports = tty_supports_face_attributes_p (f, attrs, def_face);
5059 #ifdef HAVE_WINDOW_SYSTEM
5060 else
5061 supports = x_supports_face_attributes_p (f, attrs, def_face);
5062 #endif
5064 return supports ? Qt : Qnil;
5068 /***********************************************************************
5069 Font selection
5070 ***********************************************************************/
5072 DEFUN ("internal-set-font-selection-order",
5073 Finternal_set_font_selection_order,
5074 Sinternal_set_font_selection_order, 1, 1, 0,
5075 doc: /* Set font selection order for face font selection to ORDER.
5076 ORDER must be a list of length 4 containing the symbols `:width',
5077 `:height', `:weight', and `:slant'. Face attributes appearing
5078 first in ORDER are matched first, e.g. if `:height' appears before
5079 `:weight' in ORDER, font selection first tries to find a font with
5080 a suitable height, and then tries to match the font weight.
5081 Value is ORDER. */)
5082 (Lisp_Object order)
5084 Lisp_Object list;
5085 int i;
5086 int indices[DIM (font_sort_order)];
5088 CHECK_LIST (order);
5089 memset (indices, 0, sizeof indices);
5090 i = 0;
5092 for (list = order;
5093 CONSP (list) && i < DIM (indices);
5094 list = XCDR (list), ++i)
5096 Lisp_Object attr = XCAR (list);
5097 int xlfd;
5099 if (EQ (attr, QCwidth))
5100 xlfd = XLFD_SWIDTH;
5101 else if (EQ (attr, QCheight))
5102 xlfd = XLFD_POINT_SIZE;
5103 else if (EQ (attr, QCweight))
5104 xlfd = XLFD_WEIGHT;
5105 else if (EQ (attr, QCslant))
5106 xlfd = XLFD_SLANT;
5107 else
5108 break;
5110 if (indices[i] != 0)
5111 break;
5112 indices[i] = xlfd;
5115 if (!NILP (list) || i != DIM (indices))
5116 signal_error ("Invalid font sort order", order);
5117 for (i = 0; i < DIM (font_sort_order); ++i)
5118 if (indices[i] == 0)
5119 signal_error ("Invalid font sort order", order);
5121 if (memcmp (indices, font_sort_order, sizeof indices) != 0)
5123 memcpy (font_sort_order, indices, sizeof font_sort_order);
5124 free_all_realized_faces (Qnil);
5127 font_update_sort_order (font_sort_order);
5129 return Qnil;
5133 DEFUN ("internal-set-alternative-font-family-alist",
5134 Finternal_set_alternative_font_family_alist,
5135 Sinternal_set_alternative_font_family_alist, 1, 1, 0,
5136 doc: /* Define alternative font families to try in face font selection.
5137 ALIST is an alist of (FAMILY ALTERNATIVE1 ALTERNATIVE2 ...) entries.
5138 Each ALTERNATIVE is tried in order if no fonts of font family FAMILY can
5139 be found. Value is ALIST. */)
5140 (Lisp_Object alist)
5142 Lisp_Object entry, tail, tail2;
5144 CHECK_LIST (alist);
5145 alist = Fcopy_sequence (alist);
5146 for (tail = alist; CONSP (tail); tail = XCDR (tail))
5148 entry = XCAR (tail);
5149 CHECK_LIST (entry);
5150 entry = Fcopy_sequence (entry);
5151 XSETCAR (tail, entry);
5152 for (tail2 = entry; CONSP (tail2); tail2 = XCDR (tail2))
5153 XSETCAR (tail2, Fintern (XCAR (tail2), Qnil));
5156 Vface_alternative_font_family_alist = alist;
5157 free_all_realized_faces (Qnil);
5158 return alist;
5162 DEFUN ("internal-set-alternative-font-registry-alist",
5163 Finternal_set_alternative_font_registry_alist,
5164 Sinternal_set_alternative_font_registry_alist, 1, 1, 0,
5165 doc: /* Define alternative font registries to try in face font selection.
5166 ALIST is an alist of (REGISTRY ALTERNATIVE1 ALTERNATIVE2 ...) entries.
5167 Each ALTERNATIVE is tried in order if no fonts of font registry REGISTRY can
5168 be found. Value is ALIST. */)
5169 (Lisp_Object alist)
5171 Lisp_Object entry, tail, tail2;
5173 CHECK_LIST (alist);
5174 alist = Fcopy_sequence (alist);
5175 for (tail = alist; CONSP (tail); tail = XCDR (tail))
5177 entry = XCAR (tail);
5178 CHECK_LIST (entry);
5179 entry = Fcopy_sequence (entry);
5180 XSETCAR (tail, entry);
5181 for (tail2 = entry; CONSP (tail2); tail2 = XCDR (tail2))
5182 XSETCAR (tail2, Fdowncase (XCAR (tail2)));
5184 Vface_alternative_font_registry_alist = alist;
5185 free_all_realized_faces (Qnil);
5186 return alist;
5190 #ifdef HAVE_WINDOW_SYSTEM
5192 /* Return the fontset id of the base fontset name or alias name given
5193 by the fontset attribute of ATTRS. Value is -1 if the fontset
5194 attribute of ATTRS doesn't name a fontset. */
5196 static int
5197 face_fontset (Lisp_Object attrs[LFACE_VECTOR_SIZE])
5199 Lisp_Object name;
5201 name = attrs[LFACE_FONTSET_INDEX];
5202 if (!STRINGP (name))
5203 return -1;
5204 return fs_query_fontset (name, 0);
5207 #endif /* HAVE_WINDOW_SYSTEM */
5211 /***********************************************************************
5212 Face Realization
5213 ***********************************************************************/
5215 /* Realize basic faces on frame F. Value is zero if frame parameters
5216 of F don't contain enough information needed to realize the default
5217 face. */
5219 static bool
5220 realize_basic_faces (struct frame *f)
5222 bool success_p = 0;
5223 ptrdiff_t count = SPECPDL_INDEX ();
5225 /* Block input here so that we won't be surprised by an X expose
5226 event, for instance, without having the faces set up. */
5227 block_input ();
5228 specbind (Qscalable_fonts_allowed, Qt);
5230 if (realize_default_face (f))
5232 realize_named_face (f, Qmode_line, MODE_LINE_FACE_ID);
5233 realize_named_face (f, Qmode_line_inactive, MODE_LINE_INACTIVE_FACE_ID);
5234 realize_named_face (f, Qtool_bar, TOOL_BAR_FACE_ID);
5235 realize_named_face (f, Qfringe, FRINGE_FACE_ID);
5236 realize_named_face (f, Qheader_line, HEADER_LINE_FACE_ID);
5237 realize_named_face (f, Qscroll_bar, SCROLL_BAR_FACE_ID);
5238 realize_named_face (f, Qborder, BORDER_FACE_ID);
5239 realize_named_face (f, Qcursor, CURSOR_FACE_ID);
5240 realize_named_face (f, Qmouse, MOUSE_FACE_ID);
5241 realize_named_face (f, Qmenu, MENU_FACE_ID);
5242 realize_named_face (f, Qvertical_border, VERTICAL_BORDER_FACE_ID);
5243 realize_named_face (f, Qwindow_divider, WINDOW_DIVIDER_FACE_ID);
5244 realize_named_face (f, Qwindow_divider_first_pixel,
5245 WINDOW_DIVIDER_FIRST_PIXEL_FACE_ID);
5246 realize_named_face (f, Qwindow_divider_last_pixel,
5247 WINDOW_DIVIDER_LAST_PIXEL_FACE_ID);
5249 /* Reflect changes in the `menu' face in menu bars. */
5250 if (FRAME_FACE_CACHE (f)->menu_face_changed_p)
5252 FRAME_FACE_CACHE (f)->menu_face_changed_p = 0;
5253 #ifdef USE_X_TOOLKIT
5254 if (FRAME_WINDOW_P (f))
5255 x_update_menu_appearance (f);
5256 #endif
5259 success_p = 1;
5262 unbind_to (count, Qnil);
5263 unblock_input ();
5264 return success_p;
5268 /* Realize the default face on frame F. If the face is not fully
5269 specified, make it fully-specified. Attributes of the default face
5270 that are not explicitly specified are taken from frame parameters. */
5272 static bool
5273 realize_default_face (struct frame *f)
5275 struct face_cache *c = FRAME_FACE_CACHE (f);
5276 Lisp_Object lface;
5277 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5278 struct face *face;
5280 /* If the `default' face is not yet known, create it. */
5281 lface = lface_from_face_name (f, Qdefault, 0);
5282 if (NILP (lface))
5284 Lisp_Object frame;
5285 XSETFRAME (frame, f);
5286 lface = Finternal_make_lisp_face (Qdefault, frame);
5289 #ifdef HAVE_WINDOW_SYSTEM
5290 if (FRAME_WINDOW_P (f))
5292 Lisp_Object font_object;
5294 XSETFONT (font_object, FRAME_FONT (f));
5295 set_lface_from_font (f, lface, font_object, f->default_face_done_p);
5296 ASET (lface, LFACE_FONTSET_INDEX, fontset_name (FRAME_FONTSET (f)));
5297 f->default_face_done_p = 1;
5299 #endif /* HAVE_WINDOW_SYSTEM */
5301 if (!FRAME_WINDOW_P (f))
5303 ASET (lface, LFACE_FAMILY_INDEX, build_string ("default"));
5304 ASET (lface, LFACE_FOUNDRY_INDEX, LFACE_FAMILY (lface));
5305 ASET (lface, LFACE_SWIDTH_INDEX, Qnormal);
5306 ASET (lface, LFACE_HEIGHT_INDEX, make_number (1));
5307 if (UNSPECIFIEDP (LFACE_WEIGHT (lface)))
5308 ASET (lface, LFACE_WEIGHT_INDEX, Qnormal);
5309 if (UNSPECIFIEDP (LFACE_SLANT (lface)))
5310 ASET (lface, LFACE_SLANT_INDEX, Qnormal);
5311 if (UNSPECIFIEDP (LFACE_FONTSET (lface)))
5312 ASET (lface, LFACE_FONTSET_INDEX, Qnil);
5315 if (UNSPECIFIEDP (LFACE_UNDERLINE (lface)))
5316 ASET (lface, LFACE_UNDERLINE_INDEX, Qnil);
5318 if (UNSPECIFIEDP (LFACE_OVERLINE (lface)))
5319 ASET (lface, LFACE_OVERLINE_INDEX, Qnil);
5321 if (UNSPECIFIEDP (LFACE_STRIKE_THROUGH (lface)))
5322 ASET (lface, LFACE_STRIKE_THROUGH_INDEX, Qnil);
5324 if (UNSPECIFIEDP (LFACE_BOX (lface)))
5325 ASET (lface, LFACE_BOX_INDEX, Qnil);
5327 if (UNSPECIFIEDP (LFACE_INVERSE (lface)))
5328 ASET (lface, LFACE_INVERSE_INDEX, Qnil);
5330 if (UNSPECIFIEDP (LFACE_FOREGROUND (lface)))
5332 /* This function is called so early that colors are not yet
5333 set in the frame parameter list. */
5334 Lisp_Object color = Fassq (Qforeground_color, f->param_alist);
5336 if (CONSP (color) && STRINGP (XCDR (color)))
5337 ASET (lface, LFACE_FOREGROUND_INDEX, XCDR (color));
5338 else if (FRAME_WINDOW_P (f))
5339 return 0;
5340 else if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
5341 ASET (lface, LFACE_FOREGROUND_INDEX, build_string (unspecified_fg));
5342 else
5343 emacs_abort ();
5346 if (UNSPECIFIEDP (LFACE_BACKGROUND (lface)))
5348 /* This function is called so early that colors are not yet
5349 set in the frame parameter list. */
5350 Lisp_Object color = Fassq (Qbackground_color, f->param_alist);
5351 if (CONSP (color) && STRINGP (XCDR (color)))
5352 ASET (lface, LFACE_BACKGROUND_INDEX, XCDR (color));
5353 else if (FRAME_WINDOW_P (f))
5354 return 0;
5355 else if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
5356 ASET (lface, LFACE_BACKGROUND_INDEX, build_string (unspecified_bg));
5357 else
5358 emacs_abort ();
5361 if (UNSPECIFIEDP (LFACE_STIPPLE (lface)))
5362 ASET (lface, LFACE_STIPPLE_INDEX, Qnil);
5364 /* Realize the face; it must be fully-specified now. */
5365 eassert (lface_fully_specified_p (XVECTOR (lface)->contents));
5366 check_lface (lface);
5367 memcpy (attrs, XVECTOR (lface)->contents, sizeof attrs);
5368 face = realize_face (c, attrs, DEFAULT_FACE_ID);
5370 #ifdef HAVE_WINDOW_SYSTEM
5371 #ifdef HAVE_X_WINDOWS
5372 if (FRAME_X_P (f) && face->font != FRAME_FONT (f))
5374 /* This can happen when making a frame on a display that does
5375 not support the default font. */
5376 if (!face->font)
5377 return 0;
5379 /* Otherwise, the font specified for the frame was not
5380 acceptable as a font for the default face (perhaps because
5381 auto-scaled fonts are rejected), so we must adjust the frame
5382 font. */
5383 x_set_font (f, LFACE_FONT (lface), Qnil);
5385 #endif /* HAVE_X_WINDOWS */
5386 #endif /* HAVE_WINDOW_SYSTEM */
5387 return 1;
5391 /* Realize basic faces other than the default face in face cache C.
5392 SYMBOL is the face name, ID is the face id the realized face must
5393 have. The default face must have been realized already. */
5395 static void
5396 realize_named_face (struct frame *f, Lisp_Object symbol, int id)
5398 struct face_cache *c = FRAME_FACE_CACHE (f);
5399 Lisp_Object lface = lface_from_face_name (f, symbol, 0);
5400 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5401 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
5403 /* The default face must exist and be fully specified. */
5404 get_lface_attributes_no_remap (f, Qdefault, attrs, 1);
5405 check_lface_attrs (attrs);
5406 eassert (lface_fully_specified_p (attrs));
5408 /* If SYMBOL isn't know as a face, create it. */
5409 if (NILP (lface))
5411 Lisp_Object frame;
5412 XSETFRAME (frame, f);
5413 lface = Finternal_make_lisp_face (symbol, frame);
5416 /* Merge SYMBOL's face with the default face. */
5417 get_lface_attributes_no_remap (f, symbol, symbol_attrs, 1);
5418 merge_face_vectors (f, symbol_attrs, attrs, 0);
5420 /* Realize the face. */
5421 realize_face (c, attrs, id);
5425 /* Realize the fully-specified face with attributes ATTRS in face
5426 cache CACHE for ASCII characters. If FORMER_FACE_ID is
5427 non-negative, it is an ID of face to remove before caching the new
5428 face. Value is a pointer to the newly created realized face. */
5430 static struct face *
5431 realize_face (struct face_cache *cache, Lisp_Object attrs[LFACE_VECTOR_SIZE],
5432 int former_face_id)
5434 struct face *face;
5436 /* LFACE must be fully specified. */
5437 eassert (cache != NULL);
5438 check_lface_attrs (attrs);
5440 if (former_face_id >= 0 && cache->used > former_face_id)
5442 /* Remove the former face. */
5443 struct face *former_face = cache->faces_by_id[former_face_id];
5444 uncache_face (cache, former_face);
5445 free_realized_face (cache->f, former_face);
5446 SET_FRAME_GARBAGED (cache->f);
5449 if (FRAME_WINDOW_P (cache->f))
5450 face = realize_x_face (cache, attrs);
5451 else if (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f))
5452 face = realize_tty_face (cache, attrs);
5453 else if (FRAME_INITIAL_P (cache->f))
5455 /* Create a dummy face. */
5456 face = make_realized_face (attrs);
5458 else
5459 emacs_abort ();
5461 /* Insert the new face. */
5462 cache_face (cache, face, lface_hash (attrs));
5463 return face;
5467 #ifdef HAVE_WINDOW_SYSTEM
5468 /* Realize the fully-specified face that uses FONT-OBJECT and has the
5469 same attributes as BASE_FACE except for the font on frame F.
5470 FONT-OBJECT may be nil, in which case, realized a face of
5471 no-font. */
5473 static struct face *
5474 realize_non_ascii_face (struct frame *f, Lisp_Object font_object,
5475 struct face *base_face)
5477 struct face_cache *cache = FRAME_FACE_CACHE (f);
5478 struct face *face;
5480 face = xmalloc (sizeof *face);
5481 *face = *base_face;
5482 face->gc = 0;
5483 face->extra = NULL;
5484 face->overstrike
5485 = (! NILP (font_object)
5486 && FONT_WEIGHT_NAME_NUMERIC (face->lface[LFACE_WEIGHT_INDEX]) > 100
5487 && FONT_WEIGHT_NUMERIC (font_object) <= 100);
5489 /* Don't try to free the colors copied bitwise from BASE_FACE. */
5490 face->colors_copied_bitwise_p = 1;
5491 face->font = NILP (font_object) ? NULL : XFONT_OBJECT (font_object);
5492 face->gc = 0;
5494 cache_face (cache, face, face->hash);
5496 return face;
5498 #endif /* HAVE_WINDOW_SYSTEM */
5501 /* Realize the fully-specified face with attributes ATTRS in face
5502 cache CACHE for ASCII characters. Do it for X frame CACHE->f. If
5503 the new face doesn't share font with the default face, a fontname
5504 is allocated from the heap and set in `font_name' of the new face,
5505 but it is not yet loaded here. Value is a pointer to the newly
5506 created realized face. */
5508 static struct face *
5509 realize_x_face (struct face_cache *cache, Lisp_Object attrs[LFACE_VECTOR_SIZE])
5511 struct face *face = NULL;
5512 #ifdef HAVE_WINDOW_SYSTEM
5513 struct face *default_face;
5514 struct frame *f;
5515 Lisp_Object stipple, underline, overline, strike_through, box;
5517 eassert (FRAME_WINDOW_P (cache->f));
5519 /* Allocate a new realized face. */
5520 face = make_realized_face (attrs);
5521 face->ascii_face = face;
5523 f = cache->f;
5525 /* Determine the font to use. Most of the time, the font will be
5526 the same as the font of the default face, so try that first. */
5527 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5528 if (default_face
5529 && lface_same_font_attributes_p (default_face->lface, attrs))
5531 face->font = default_face->font;
5532 face->fontset
5533 = make_fontset_for_ascii_face (f, default_face->fontset, face);
5535 else
5537 /* If the face attribute ATTRS specifies a fontset, use it as
5538 the base of a new realized fontset. Otherwise, use the same
5539 base fontset as of the default face. The base determines
5540 registry and encoding of a font. It may also determine
5541 foundry and family. The other fields of font name pattern
5542 are constructed from ATTRS. */
5543 int fontset = face_fontset (attrs);
5545 /* If we are realizing the default face, ATTRS should specify a
5546 fontset. In other words, if FONTSET is -1, we are not
5547 realizing the default face, thus the default face should have
5548 already been realized. */
5549 if (fontset == -1)
5551 if (default_face)
5552 fontset = default_face->fontset;
5553 if (fontset == -1)
5554 emacs_abort ();
5556 if (! FONT_OBJECT_P (attrs[LFACE_FONT_INDEX]))
5557 attrs[LFACE_FONT_INDEX]
5558 = font_load_for_lface (f, attrs, attrs[LFACE_FONT_INDEX]);
5559 if (FONT_OBJECT_P (attrs[LFACE_FONT_INDEX]))
5561 face->font = XFONT_OBJECT (attrs[LFACE_FONT_INDEX]);
5562 face->fontset = make_fontset_for_ascii_face (f, fontset, face);
5564 else
5566 face->font = NULL;
5567 face->fontset = -1;
5571 if (face->font
5572 && FONT_WEIGHT_NAME_NUMERIC (attrs[LFACE_WEIGHT_INDEX]) > 100
5573 && FONT_WEIGHT_NUMERIC (attrs[LFACE_FONT_INDEX]) <= 100)
5574 face->overstrike = 1;
5576 /* Load colors, and set remaining attributes. */
5578 load_face_colors (f, face, attrs);
5580 /* Set up box. */
5581 box = attrs[LFACE_BOX_INDEX];
5582 if (STRINGP (box))
5584 /* A simple box of line width 1 drawn in color given by
5585 the string. */
5586 face->box_color = load_color (f, face, attrs[LFACE_BOX_INDEX],
5587 LFACE_BOX_INDEX);
5588 face->box = FACE_SIMPLE_BOX;
5589 face->box_line_width = 1;
5591 else if (INTEGERP (box))
5593 /* Simple box of specified line width in foreground color of the
5594 face. */
5595 eassert (XINT (box) != 0);
5596 face->box = FACE_SIMPLE_BOX;
5597 face->box_line_width = XINT (box);
5598 face->box_color = face->foreground;
5599 face->box_color_defaulted_p = 1;
5601 else if (CONSP (box))
5603 /* `(:width WIDTH :color COLOR :shadow SHADOW)'. SHADOW
5604 being one of `raised' or `sunken'. */
5605 face->box = FACE_SIMPLE_BOX;
5606 face->box_color = face->foreground;
5607 face->box_color_defaulted_p = 1;
5608 face->box_line_width = 1;
5610 while (CONSP (box))
5612 Lisp_Object keyword, value;
5614 keyword = XCAR (box);
5615 box = XCDR (box);
5617 if (!CONSP (box))
5618 break;
5619 value = XCAR (box);
5620 box = XCDR (box);
5622 if (EQ (keyword, QCline_width))
5624 if (INTEGERP (value) && XINT (value) != 0)
5625 face->box_line_width = XINT (value);
5627 else if (EQ (keyword, QCcolor))
5629 if (STRINGP (value))
5631 face->box_color = load_color (f, face, value,
5632 LFACE_BOX_INDEX);
5633 face->use_box_color_for_shadows_p = 1;
5636 else if (EQ (keyword, QCstyle))
5638 if (EQ (value, Qreleased_button))
5639 face->box = FACE_RAISED_BOX;
5640 else if (EQ (value, Qpressed_button))
5641 face->box = FACE_SUNKEN_BOX;
5646 /* Text underline, overline, strike-through. */
5648 underline = attrs[LFACE_UNDERLINE_INDEX];
5649 if (EQ (underline, Qt))
5651 /* Use default color (same as foreground color). */
5652 face->underline_p = 1;
5653 face->underline_type = FACE_UNDER_LINE;
5654 face->underline_defaulted_p = 1;
5655 face->underline_color = 0;
5657 else if (STRINGP (underline))
5659 /* Use specified color. */
5660 face->underline_p = 1;
5661 face->underline_type = FACE_UNDER_LINE;
5662 face->underline_defaulted_p = 0;
5663 face->underline_color
5664 = load_color (f, face, underline,
5665 LFACE_UNDERLINE_INDEX);
5667 else if (NILP (underline))
5669 face->underline_p = 0;
5670 face->underline_defaulted_p = 0;
5671 face->underline_color = 0;
5673 else if (CONSP (underline))
5675 /* `(:color COLOR :style STYLE)'.
5676 STYLE being one of `line' or `wave'. */
5677 face->underline_p = 1;
5678 face->underline_color = 0;
5679 face->underline_defaulted_p = 1;
5680 face->underline_type = FACE_UNDER_LINE;
5682 /* FIXME? This is also not robust about checking the precise form.
5683 See comments in Finternal_set_lisp_face_attribute. */
5684 while (CONSP (underline))
5686 Lisp_Object keyword, value;
5688 keyword = XCAR (underline);
5689 underline = XCDR (underline);
5691 if (!CONSP (underline))
5692 break;
5693 value = XCAR (underline);
5694 underline = XCDR (underline);
5696 if (EQ (keyword, QCcolor))
5698 if (EQ (value, Qforeground_color))
5700 face->underline_defaulted_p = 1;
5701 face->underline_color = 0;
5703 else if (STRINGP (value))
5705 face->underline_defaulted_p = 0;
5706 face->underline_color = load_color (f, face, value,
5707 LFACE_UNDERLINE_INDEX);
5710 else if (EQ (keyword, QCstyle))
5712 if (EQ (value, Qline))
5713 face->underline_type = FACE_UNDER_LINE;
5714 else if (EQ (value, Qwave))
5715 face->underline_type = FACE_UNDER_WAVE;
5720 overline = attrs[LFACE_OVERLINE_INDEX];
5721 if (STRINGP (overline))
5723 face->overline_color
5724 = load_color (f, face, attrs[LFACE_OVERLINE_INDEX],
5725 LFACE_OVERLINE_INDEX);
5726 face->overline_p = 1;
5728 else if (EQ (overline, Qt))
5730 face->overline_color = face->foreground;
5731 face->overline_color_defaulted_p = 1;
5732 face->overline_p = 1;
5735 strike_through = attrs[LFACE_STRIKE_THROUGH_INDEX];
5736 if (STRINGP (strike_through))
5738 face->strike_through_color
5739 = load_color (f, face, attrs[LFACE_STRIKE_THROUGH_INDEX],
5740 LFACE_STRIKE_THROUGH_INDEX);
5741 face->strike_through_p = 1;
5743 else if (EQ (strike_through, Qt))
5745 face->strike_through_color = face->foreground;
5746 face->strike_through_color_defaulted_p = 1;
5747 face->strike_through_p = 1;
5750 stipple = attrs[LFACE_STIPPLE_INDEX];
5751 if (!NILP (stipple))
5752 face->stipple = load_pixmap (f, stipple);
5753 #endif /* HAVE_WINDOW_SYSTEM */
5755 return face;
5759 /* Map a specified color of face FACE on frame F to a tty color index.
5760 IDX is either LFACE_FOREGROUND_INDEX or LFACE_BACKGROUND_INDEX, and
5761 specifies which color to map. Set *DEFAULTED to 1 if mapping to the
5762 default foreground/background colors. */
5764 static void
5765 map_tty_color (struct frame *f, struct face *face,
5766 enum lface_attribute_index idx, int *defaulted)
5768 Lisp_Object frame, color, def;
5769 int foreground_p = idx == LFACE_FOREGROUND_INDEX;
5770 unsigned long default_pixel =
5771 foreground_p ? FACE_TTY_DEFAULT_FG_COLOR : FACE_TTY_DEFAULT_BG_COLOR;
5772 unsigned long pixel = default_pixel;
5773 #ifdef MSDOS
5774 unsigned long default_other_pixel =
5775 foreground_p ? FACE_TTY_DEFAULT_BG_COLOR : FACE_TTY_DEFAULT_FG_COLOR;
5776 #endif
5778 eassert (idx == LFACE_FOREGROUND_INDEX || idx == LFACE_BACKGROUND_INDEX);
5780 XSETFRAME (frame, f);
5781 color = face->lface[idx];
5783 if (STRINGP (color)
5784 && SCHARS (color)
5785 && CONSP (Vtty_defined_color_alist)
5786 && (def = assq_no_quit (color, call1 (Qtty_color_alist, frame)),
5787 CONSP (def)))
5789 /* Associations in tty-defined-color-alist are of the form
5790 (NAME INDEX R G B). We need the INDEX part. */
5791 pixel = XINT (XCAR (XCDR (def)));
5794 if (pixel == default_pixel && STRINGP (color))
5796 pixel = load_color (f, face, color, idx);
5798 #ifdef MSDOS
5799 /* If the foreground of the default face is the default color,
5800 use the foreground color defined by the frame. */
5801 if (FRAME_MSDOS_P (f))
5803 if (pixel == default_pixel
5804 || pixel == FACE_TTY_DEFAULT_COLOR)
5806 if (foreground_p)
5807 pixel = FRAME_FOREGROUND_PIXEL (f);
5808 else
5809 pixel = FRAME_BACKGROUND_PIXEL (f);
5810 face->lface[idx] = tty_color_name (f, pixel);
5811 *defaulted = 1;
5813 else if (pixel == default_other_pixel)
5815 if (foreground_p)
5816 pixel = FRAME_BACKGROUND_PIXEL (f);
5817 else
5818 pixel = FRAME_FOREGROUND_PIXEL (f);
5819 face->lface[idx] = tty_color_name (f, pixel);
5820 *defaulted = 1;
5823 #endif /* MSDOS */
5826 if (foreground_p)
5827 face->foreground = pixel;
5828 else
5829 face->background = pixel;
5833 /* Realize the fully-specified face with attributes ATTRS in face
5834 cache CACHE for ASCII characters. Do it for TTY frame CACHE->f.
5835 Value is a pointer to the newly created realized face. */
5837 static struct face *
5838 realize_tty_face (struct face_cache *cache,
5839 Lisp_Object attrs[LFACE_VECTOR_SIZE])
5841 struct face *face;
5842 int weight, slant;
5843 int face_colors_defaulted = 0;
5844 struct frame *f = cache->f;
5846 /* Frame must be a termcap frame. */
5847 eassert (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f));
5849 /* Allocate a new realized face. */
5850 face = make_realized_face (attrs);
5851 #if 0
5852 face->font_name = FRAME_MSDOS_P (cache->f) ? "ms-dos" : "tty";
5853 #endif
5855 /* Map face attributes to TTY appearances. */
5856 weight = FONT_WEIGHT_NAME_NUMERIC (attrs[LFACE_WEIGHT_INDEX]);
5857 slant = FONT_SLANT_NAME_NUMERIC (attrs[LFACE_SLANT_INDEX]);
5858 if (weight > 100)
5859 face->tty_bold_p = 1;
5860 if (slant != 100)
5861 face->tty_italic_p = 1;
5862 if (!NILP (attrs[LFACE_UNDERLINE_INDEX]))
5863 face->tty_underline_p = 1;
5864 if (!NILP (attrs[LFACE_INVERSE_INDEX]))
5865 face->tty_reverse_p = 1;
5867 /* Map color names to color indices. */
5868 map_tty_color (f, face, LFACE_FOREGROUND_INDEX, &face_colors_defaulted);
5869 map_tty_color (f, face, LFACE_BACKGROUND_INDEX, &face_colors_defaulted);
5871 /* Swap colors if face is inverse-video. If the colors are taken
5872 from the frame colors, they are already inverted, since the
5873 frame-creation function calls x-handle-reverse-video. */
5874 if (face->tty_reverse_p && !face_colors_defaulted)
5876 unsigned long tem = face->foreground;
5877 face->foreground = face->background;
5878 face->background = tem;
5881 if (tty_suppress_bold_inverse_default_colors_p
5882 && face->tty_bold_p
5883 && face->background == FACE_TTY_DEFAULT_FG_COLOR
5884 && face->foreground == FACE_TTY_DEFAULT_BG_COLOR)
5885 face->tty_bold_p = 0;
5887 return face;
5891 DEFUN ("tty-suppress-bold-inverse-default-colors",
5892 Ftty_suppress_bold_inverse_default_colors,
5893 Stty_suppress_bold_inverse_default_colors, 1, 1, 0,
5894 doc: /* Suppress/allow boldness of faces with inverse default colors.
5895 SUPPRESS non-nil means suppress it.
5896 This affects bold faces on TTYs whose foreground is the default background
5897 color of the display and whose background is the default foreground color.
5898 For such faces, the bold face attribute is ignored if this variable
5899 is non-nil. */)
5900 (Lisp_Object suppress)
5902 tty_suppress_bold_inverse_default_colors_p = !NILP (suppress);
5903 ++face_change_count;
5904 return suppress;
5909 /***********************************************************************
5910 Computing Faces
5911 ***********************************************************************/
5913 /* Return the ID of the face to use to display character CH with face
5914 property PROP on frame F in current_buffer. */
5917 compute_char_face (struct frame *f, int ch, Lisp_Object prop)
5919 int face_id;
5921 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
5922 ch = 0;
5924 if (NILP (prop))
5926 struct face *face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5927 face_id = FACE_FOR_CHAR (f, face, ch, -1, Qnil);
5929 else
5931 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5932 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5933 memcpy (attrs, default_face->lface, sizeof attrs);
5934 merge_face_ref (f, prop, attrs, 1, 0);
5935 face_id = lookup_face (f, attrs);
5938 return face_id;
5941 /* Return the face ID associated with buffer position POS for
5942 displaying ASCII characters. Return in *ENDPTR the position at
5943 which a different face is needed, as far as text properties and
5944 overlays are concerned. W is a window displaying current_buffer.
5946 REGION_BEG, REGION_END delimit the region, so it can be
5947 highlighted.
5949 LIMIT is a position not to scan beyond. That is to limit the time
5950 this function can take.
5952 If MOUSE is non-zero, use the character's mouse-face, not its face.
5954 BASE_FACE_ID, if non-negative, specifies a base face id to use
5955 instead of DEFAULT_FACE_ID.
5957 The face returned is suitable for displaying ASCII characters. */
5960 face_at_buffer_position (struct window *w, ptrdiff_t pos,
5961 ptrdiff_t *endptr, ptrdiff_t limit,
5962 int mouse, int base_face_id)
5964 struct frame *f = XFRAME (w->frame);
5965 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5966 Lisp_Object prop, position;
5967 ptrdiff_t i, noverlays;
5968 Lisp_Object *overlay_vec;
5969 ptrdiff_t endpos;
5970 Lisp_Object propname = mouse ? Qmouse_face : Qface;
5971 Lisp_Object limit1, end;
5972 struct face *default_face;
5974 /* W must display the current buffer. We could write this function
5975 to use the frame and buffer of W, but right now it doesn't. */
5976 /* eassert (XBUFFER (w->contents) == current_buffer); */
5978 XSETFASTINT (position, pos);
5980 endpos = ZV;
5982 /* Get the `face' or `mouse_face' text property at POS, and
5983 determine the next position at which the property changes. */
5984 prop = Fget_text_property (position, propname, w->contents);
5985 XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
5986 end = Fnext_single_property_change (position, propname, w->contents, limit1);
5987 if (INTEGERP (end))
5988 endpos = XINT (end);
5990 /* Look at properties from overlays. */
5992 ptrdiff_t next_overlay;
5994 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, &next_overlay, 0);
5995 if (next_overlay < endpos)
5996 endpos = next_overlay;
5999 *endptr = endpos;
6002 int face_id;
6004 if (base_face_id >= 0)
6005 face_id = base_face_id;
6006 else if (NILP (Vface_remapping_alist))
6007 face_id = DEFAULT_FACE_ID;
6008 else
6009 face_id = lookup_basic_face (f, DEFAULT_FACE_ID);
6011 default_face = FACE_FROM_ID (f, face_id);
6014 /* Optimize common cases where we can use the default face. */
6015 if (noverlays == 0
6016 && NILP (prop))
6017 return default_face->id;
6019 /* Begin with attributes from the default face. */
6020 memcpy (attrs, default_face->lface, sizeof attrs);
6022 /* Merge in attributes specified via text properties. */
6023 if (!NILP (prop))
6024 merge_face_ref (f, prop, attrs, 1, 0);
6026 /* Now merge the overlay data. */
6027 noverlays = sort_overlays (overlay_vec, noverlays, w);
6028 for (i = 0; i < noverlays; i++)
6030 Lisp_Object oend;
6031 ptrdiff_t oendpos;
6033 prop = Foverlay_get (overlay_vec[i], propname);
6034 if (!NILP (prop))
6035 merge_face_ref (f, prop, attrs, 1, 0);
6037 oend = OVERLAY_END (overlay_vec[i]);
6038 oendpos = OVERLAY_POSITION (oend);
6039 if (oendpos < endpos)
6040 endpos = oendpos;
6043 *endptr = endpos;
6045 /* Look up a realized face with the given face attributes,
6046 or realize a new one for ASCII characters. */
6047 return lookup_face (f, attrs);
6050 /* Return the face ID at buffer position POS for displaying ASCII
6051 characters associated with overlay strings for overlay OVERLAY.
6053 Like face_at_buffer_position except for OVERLAY. Currently it
6054 simply disregards the `face' properties of all overlays. */
6057 face_for_overlay_string (struct window *w, ptrdiff_t pos,
6058 ptrdiff_t *endptr, ptrdiff_t limit,
6059 int mouse, Lisp_Object overlay)
6061 struct frame *f = XFRAME (w->frame);
6062 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6063 Lisp_Object prop, position;
6064 ptrdiff_t endpos;
6065 Lisp_Object propname = mouse ? Qmouse_face : Qface;
6066 Lisp_Object limit1, end;
6067 struct face *default_face;
6069 /* W must display the current buffer. We could write this function
6070 to use the frame and buffer of W, but right now it doesn't. */
6071 /* eassert (XBUFFER (w->contents) == current_buffer); */
6073 XSETFASTINT (position, pos);
6075 endpos = ZV;
6077 /* Get the `face' or `mouse_face' text property at POS, and
6078 determine the next position at which the property changes. */
6079 prop = Fget_text_property (position, propname, w->contents);
6080 XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
6081 end = Fnext_single_property_change (position, propname, w->contents, limit1);
6082 if (INTEGERP (end))
6083 endpos = XINT (end);
6085 *endptr = endpos;
6087 /* Optimize common case where we can use the default face. */
6088 if (NILP (prop)
6089 && NILP (Vface_remapping_alist))
6090 return DEFAULT_FACE_ID;
6092 /* Begin with attributes from the default face. */
6093 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
6094 memcpy (attrs, default_face->lface, sizeof attrs);
6096 /* Merge in attributes specified via text properties. */
6097 if (!NILP (prop))
6098 merge_face_ref (f, prop, attrs, 1, 0);
6100 *endptr = endpos;
6102 /* Look up a realized face with the given face attributes,
6103 or realize a new one for ASCII characters. */
6104 return lookup_face (f, attrs);
6108 /* Compute the face at character position POS in Lisp string STRING on
6109 window W, for ASCII characters.
6111 If STRING is an overlay string, it comes from position BUFPOS in
6112 current_buffer, otherwise BUFPOS is zero to indicate that STRING is
6113 not an overlay string. W must display the current buffer.
6114 REGION_BEG and REGION_END give the start and end positions of the
6115 region; both are -1 if no region is visible.
6117 BASE_FACE_ID is the id of a face to merge with. For strings coming
6118 from overlays or the `display' property it is the face at BUFPOS.
6120 If MOUSE_P is non-zero, use the character's mouse-face, not its face.
6122 Set *ENDPTR to the next position where to check for faces in
6123 STRING; -1 if the face is constant from POS to the end of the
6124 string.
6126 Value is the id of the face to use. The face returned is suitable
6127 for displaying ASCII characters. */
6130 face_at_string_position (struct window *w, Lisp_Object string,
6131 ptrdiff_t pos, ptrdiff_t bufpos,
6132 ptrdiff_t *endptr, enum face_id base_face_id,
6133 int mouse_p)
6135 Lisp_Object prop, position, end, limit;
6136 struct frame *f = XFRAME (WINDOW_FRAME (w));
6137 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6138 struct face *base_face;
6139 bool multibyte_p = STRING_MULTIBYTE (string);
6140 Lisp_Object prop_name = mouse_p ? Qmouse_face : Qface;
6142 /* Get the value of the face property at the current position within
6143 STRING. Value is nil if there is no face property. */
6144 XSETFASTINT (position, pos);
6145 prop = Fget_text_property (position, prop_name, string);
6147 /* Get the next position at which to check for faces. Value of end
6148 is nil if face is constant all the way to the end of the string.
6149 Otherwise it is a string position where to check faces next.
6150 Limit is the maximum position up to which to check for property
6151 changes in Fnext_single_property_change. Strings are usually
6152 short, so set the limit to the end of the string. */
6153 XSETFASTINT (limit, SCHARS (string));
6154 end = Fnext_single_property_change (position, prop_name, string, limit);
6155 if (INTEGERP (end))
6156 *endptr = XFASTINT (end);
6157 else
6158 *endptr = -1;
6160 base_face = FACE_FROM_ID (f, base_face_id);
6161 eassert (base_face);
6163 /* Optimize the default case that there is no face property. */
6164 if (NILP (prop)
6165 && (multibyte_p
6166 /* We can't realize faces for different charsets differently
6167 if we don't have fonts, so we can stop here if not working
6168 on a window-system frame. */
6169 || !FRAME_WINDOW_P (f)
6170 || FACE_SUITABLE_FOR_ASCII_CHAR_P (base_face, 0)))
6171 return base_face->id;
6173 /* Begin with attributes from the base face. */
6174 memcpy (attrs, base_face->lface, sizeof attrs);
6176 /* Merge in attributes specified via text properties. */
6177 if (!NILP (prop))
6178 merge_face_ref (f, prop, attrs, 1, 0);
6180 /* Look up a realized face with the given face attributes,
6181 or realize a new one for ASCII characters. */
6182 return lookup_face (f, attrs);
6186 /* Merge a face into a realized face.
6188 F is frame where faces are (to be) realized.
6190 FACE_NAME is named face to merge.
6192 If FACE_NAME is nil, FACE_ID is face_id of realized face to merge.
6194 If FACE_NAME is t, FACE_ID is lface_id of face to merge.
6196 BASE_FACE_ID is realized face to merge into.
6198 Return new face id.
6202 merge_faces (struct frame *f, Lisp_Object face_name, int face_id,
6203 int base_face_id)
6205 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6206 struct face *base_face;
6208 base_face = FACE_FROM_ID (f, base_face_id);
6209 if (!base_face)
6210 return base_face_id;
6212 if (EQ (face_name, Qt))
6214 if (face_id < 0 || face_id >= lface_id_to_name_size)
6215 return base_face_id;
6216 face_name = lface_id_to_name[face_id];
6217 /* When called during make-frame, lookup_derived_face may fail
6218 if the faces are uninitialized. Don't signal an error. */
6219 face_id = lookup_derived_face (f, face_name, base_face_id, 0);
6220 return (face_id >= 0 ? face_id : base_face_id);
6223 /* Begin with attributes from the base face. */
6224 memcpy (attrs, base_face->lface, sizeof attrs);
6226 if (!NILP (face_name))
6228 if (!merge_named_face (f, face_name, attrs, 0))
6229 return base_face_id;
6231 else
6233 struct face *face;
6234 if (face_id < 0)
6235 return base_face_id;
6236 face = FACE_FROM_ID (f, face_id);
6237 if (!face)
6238 return base_face_id;
6239 merge_face_vectors (f, face->lface, attrs, 0);
6242 /* Look up a realized face with the given face attributes,
6243 or realize a new one for ASCII characters. */
6244 return lookup_face (f, attrs);
6249 #ifndef HAVE_X_WINDOWS
6250 DEFUN ("x-load-color-file", Fx_load_color_file,
6251 Sx_load_color_file, 1, 1, 0,
6252 doc: /* Create an alist of color entries from an external file.
6254 The file should define one named RGB color per line like so:
6255 R G B name
6256 where R,G,B are numbers between 0 and 255 and name is an arbitrary string. */)
6257 (Lisp_Object filename)
6259 FILE *fp;
6260 Lisp_Object cmap = Qnil;
6261 Lisp_Object abspath;
6263 CHECK_STRING (filename);
6264 abspath = Fexpand_file_name (filename, Qnil);
6266 block_input ();
6267 fp = emacs_fopen (SSDATA (abspath), "rt");
6268 if (fp)
6270 char buf[512];
6271 int red, green, blue;
6272 int num;
6274 while (fgets (buf, sizeof (buf), fp) != NULL) {
6275 if (sscanf (buf, "%u %u %u %n", &red, &green, &blue, &num) == 3)
6277 #ifdef HAVE_NTGUI
6278 int color = RGB (red, green, blue);
6279 #else
6280 int color = (red << 16) | (green << 8) | blue;
6281 #endif
6282 char *name = buf + num;
6283 ptrdiff_t len = strlen (name);
6284 len -= 0 < len && name[len - 1] == '\n';
6285 cmap = Fcons (Fcons (make_string (name, len), make_number (color)),
6286 cmap);
6289 fclose (fp);
6291 unblock_input ();
6292 return cmap;
6294 #endif
6297 /***********************************************************************
6298 Tests
6299 ***********************************************************************/
6301 #ifdef GLYPH_DEBUG
6303 /* Print the contents of the realized face FACE to stderr. */
6305 static void
6306 dump_realized_face (struct face *face)
6308 fprintf (stderr, "ID: %d\n", face->id);
6309 #ifdef HAVE_X_WINDOWS
6310 fprintf (stderr, "gc: %ld\n", (long) face->gc);
6311 #endif
6312 fprintf (stderr, "foreground: 0x%lx (%s)\n",
6313 face->foreground,
6314 SDATA (face->lface[LFACE_FOREGROUND_INDEX]));
6315 fprintf (stderr, "background: 0x%lx (%s)\n",
6316 face->background,
6317 SDATA (face->lface[LFACE_BACKGROUND_INDEX]));
6318 if (face->font)
6319 fprintf (stderr, "font_name: %s (%s)\n",
6320 SDATA (face->font->props[FONT_NAME_INDEX]),
6321 SDATA (face->lface[LFACE_FAMILY_INDEX]));
6322 #ifdef HAVE_X_WINDOWS
6323 fprintf (stderr, "font = %p\n", face->font);
6324 #endif
6325 fprintf (stderr, "fontset: %d\n", face->fontset);
6326 fprintf (stderr, "underline: %d (%s)\n",
6327 face->underline_p,
6328 SDATA (Fsymbol_name (face->lface[LFACE_UNDERLINE_INDEX])));
6329 fprintf (stderr, "hash: %d\n", face->hash);
6333 DEFUN ("dump-face", Fdump_face, Sdump_face, 0, 1, 0, doc: /* */)
6334 (Lisp_Object n)
6336 if (NILP (n))
6338 int i;
6340 fprintf (stderr, "font selection order: ");
6341 for (i = 0; i < DIM (font_sort_order); ++i)
6342 fprintf (stderr, "%d ", font_sort_order[i]);
6343 fprintf (stderr, "\n");
6345 fprintf (stderr, "alternative fonts: ");
6346 debug_print (Vface_alternative_font_family_alist);
6347 fprintf (stderr, "\n");
6349 for (i = 0; i < FRAME_FACE_CACHE (SELECTED_FRAME ())->used; ++i)
6350 Fdump_face (make_number (i));
6352 else
6354 struct face *face;
6355 CHECK_NUMBER (n);
6356 face = FACE_FROM_ID (SELECTED_FRAME (), XINT (n));
6357 if (face == NULL)
6358 error ("Not a valid face");
6359 dump_realized_face (face);
6362 return Qnil;
6366 DEFUN ("show-face-resources", Fshow_face_resources, Sshow_face_resources,
6367 0, 0, 0, doc: /* */)
6368 (void)
6370 fprintf (stderr, "number of colors = %d\n", ncolors_allocated);
6371 fprintf (stderr, "number of pixmaps = %d\n", npixmaps_allocated);
6372 fprintf (stderr, "number of GCs = %d\n", ngcs);
6373 return Qnil;
6376 #endif /* GLYPH_DEBUG */
6380 /***********************************************************************
6381 Initialization
6382 ***********************************************************************/
6384 void
6385 syms_of_xfaces (void)
6387 DEFSYM (Qface, "face");
6388 DEFSYM (Qface_no_inherit, "face-no-inherit");
6389 DEFSYM (Qbitmap_spec_p, "bitmap-spec-p");
6390 DEFSYM (Qframe_set_background_mode, "frame-set-background-mode");
6392 /* Lisp face attribute keywords. */
6393 DEFSYM (QCfamily, ":family");
6394 DEFSYM (QCheight, ":height");
6395 DEFSYM (QCweight, ":weight");
6396 DEFSYM (QCslant, ":slant");
6397 DEFSYM (QCunderline, ":underline");
6398 DEFSYM (QCinverse_video, ":inverse-video");
6399 DEFSYM (QCreverse_video, ":reverse-video");
6400 DEFSYM (QCforeground, ":foreground");
6401 DEFSYM (QCbackground, ":background");
6402 DEFSYM (QCstipple, ":stipple");
6403 DEFSYM (QCwidth, ":width");
6404 DEFSYM (QCfont, ":font");
6405 DEFSYM (QCfontset, ":fontset");
6406 DEFSYM (QCdistant_foreground, ":distant-foreground");
6407 DEFSYM (QCbold, ":bold");
6408 DEFSYM (QCitalic, ":italic");
6409 DEFSYM (QCoverline, ":overline");
6410 DEFSYM (QCstrike_through, ":strike-through");
6411 DEFSYM (QCbox, ":box");
6412 DEFSYM (QCinherit, ":inherit");
6414 /* Symbols used for Lisp face attribute values. */
6415 DEFSYM (QCcolor, ":color");
6416 DEFSYM (QCline_width, ":line-width");
6417 DEFSYM (QCstyle, ":style");
6418 DEFSYM (Qline, "line");
6419 DEFSYM (Qwave, "wave");
6420 DEFSYM (Qreleased_button, "released-button");
6421 DEFSYM (Qpressed_button, "pressed-button");
6422 DEFSYM (Qnormal, "normal");
6423 DEFSYM (Qextra_light, "extra-light");
6424 DEFSYM (Qlight, "light");
6425 DEFSYM (Qsemi_light, "semi-light");
6426 DEFSYM (Qsemi_bold, "semi-bold");
6427 DEFSYM (Qbold, "bold");
6428 DEFSYM (Qextra_bold, "extra-bold");
6429 DEFSYM (Qultra_bold, "ultra-bold");
6430 DEFSYM (Qoblique, "oblique");
6431 DEFSYM (Qitalic, "italic");
6432 DEFSYM (Qbackground_color, "background-color");
6433 DEFSYM (Qforeground_color, "foreground-color");
6434 DEFSYM (Qunspecified, "unspecified");
6435 DEFSYM (QCignore_defface, ":ignore-defface");
6437 DEFSYM (Qface_alias, "face-alias");
6438 DEFSYM (Qdefault, "default");
6439 DEFSYM (Qtool_bar, "tool-bar");
6440 DEFSYM (Qregion, "region");
6441 DEFSYM (Qfringe, "fringe");
6442 DEFSYM (Qheader_line, "header-line");
6443 DEFSYM (Qscroll_bar, "scroll-bar");
6444 DEFSYM (Qmenu, "menu");
6445 DEFSYM (Qcursor, "cursor");
6446 DEFSYM (Qborder, "border");
6447 DEFSYM (Qmouse, "mouse");
6448 DEFSYM (Qmode_line_inactive, "mode-line-inactive");
6449 DEFSYM (Qvertical_border, "vertical-border");
6450 DEFSYM (Qwindow_divider, "window-divider");
6451 DEFSYM (Qwindow_divider_first_pixel, "window-divider-first-pixel");
6452 DEFSYM (Qwindow_divider_last_pixel, "window-divider-last-pixel");
6453 DEFSYM (Qtty_color_desc, "tty-color-desc");
6454 DEFSYM (Qtty_color_standard_values, "tty-color-standard-values");
6455 DEFSYM (Qtty_color_by_index, "tty-color-by-index");
6456 DEFSYM (Qtty_color_alist, "tty-color-alist");
6457 DEFSYM (Qscalable_fonts_allowed, "scalable-fonts-allowed");
6459 Vparam_value_alist = list1 (Fcons (Qnil, Qnil));
6460 staticpro (&Vparam_value_alist);
6461 Vface_alternative_font_family_alist = Qnil;
6462 staticpro (&Vface_alternative_font_family_alist);
6463 Vface_alternative_font_registry_alist = Qnil;
6464 staticpro (&Vface_alternative_font_registry_alist);
6466 defsubr (&Sinternal_make_lisp_face);
6467 defsubr (&Sinternal_lisp_face_p);
6468 defsubr (&Sinternal_set_lisp_face_attribute);
6469 #ifdef HAVE_WINDOW_SYSTEM
6470 defsubr (&Sinternal_set_lisp_face_attribute_from_resource);
6471 #endif
6472 defsubr (&Scolor_gray_p);
6473 defsubr (&Scolor_supported_p);
6474 #ifndef HAVE_X_WINDOWS
6475 defsubr (&Sx_load_color_file);
6476 #endif
6477 defsubr (&Sface_attribute_relative_p);
6478 defsubr (&Smerge_face_attribute);
6479 defsubr (&Sinternal_get_lisp_face_attribute);
6480 defsubr (&Sinternal_lisp_face_attribute_values);
6481 defsubr (&Sinternal_lisp_face_equal_p);
6482 defsubr (&Sinternal_lisp_face_empty_p);
6483 defsubr (&Sinternal_copy_lisp_face);
6484 defsubr (&Sinternal_merge_in_global_face);
6485 defsubr (&Sface_font);
6486 defsubr (&Sframe_face_alist);
6487 defsubr (&Sdisplay_supports_face_attributes_p);
6488 defsubr (&Scolor_distance);
6489 defsubr (&Sinternal_set_font_selection_order);
6490 defsubr (&Sinternal_set_alternative_font_family_alist);
6491 defsubr (&Sinternal_set_alternative_font_registry_alist);
6492 defsubr (&Sface_attributes_as_vector);
6493 #ifdef GLYPH_DEBUG
6494 defsubr (&Sdump_face);
6495 defsubr (&Sshow_face_resources);
6496 #endif /* GLYPH_DEBUG */
6497 defsubr (&Sclear_face_cache);
6498 defsubr (&Stty_suppress_bold_inverse_default_colors);
6500 #if defined DEBUG_X_COLORS && defined HAVE_X_WINDOWS
6501 defsubr (&Sdump_colors);
6502 #endif
6504 DEFVAR_LISP ("face-new-frame-defaults", Vface_new_frame_defaults,
6505 doc: /* List of global face definitions (for internal use only.) */);
6506 Vface_new_frame_defaults = Qnil;
6508 DEFVAR_LISP ("face-default-stipple", Vface_default_stipple,
6509 doc: /* Default stipple pattern used on monochrome displays.
6510 This stipple pattern is used on monochrome displays
6511 instead of shades of gray for a face background color.
6512 See `set-face-stipple' for possible values for this variable. */);
6513 Vface_default_stipple = build_pure_c_string ("gray3");
6515 DEFVAR_LISP ("tty-defined-color-alist", Vtty_defined_color_alist,
6516 doc: /* An alist of defined terminal colors and their RGB values.
6517 See the docstring of `tty-color-alist' for the details. */);
6518 Vtty_defined_color_alist = Qnil;
6520 DEFVAR_LISP ("scalable-fonts-allowed", Vscalable_fonts_allowed,
6521 doc: /* Allowed scalable fonts.
6522 A value of nil means don't allow any scalable fonts.
6523 A value of t means allow any scalable font.
6524 Otherwise, value must be a list of regular expressions. A font may be
6525 scaled if its name matches a regular expression in the list.
6526 Note that if value is nil, a scalable font might still be used, if no
6527 other font of the appropriate family and registry is available. */);
6528 Vscalable_fonts_allowed = Qnil;
6530 DEFVAR_LISP ("face-ignored-fonts", Vface_ignored_fonts,
6531 doc: /* List of ignored fonts.
6532 Each element is a regular expression that matches names of fonts to
6533 ignore. */);
6534 Vface_ignored_fonts = Qnil;
6536 DEFVAR_LISP ("face-remapping-alist", Vface_remapping_alist,
6537 doc: /* Alist of face remappings.
6538 Each element is of the form:
6540 (FACE . REPLACEMENT),
6542 which causes display of the face FACE to use REPLACEMENT instead.
6543 REPLACEMENT is a face specification, i.e. one of the following:
6545 (1) a face name
6546 (2) a property list of attribute/value pairs, or
6547 (3) a list in which each element has the form of (1) or (2).
6549 List values for REPLACEMENT are merged to form the final face
6550 specification, with earlier entries taking precedence, in the same as
6551 as in the `face' text property.
6553 Face-name remapping cycles are suppressed; recursive references use
6554 the underlying face instead of the remapped face. So a remapping of
6555 the form:
6557 (FACE EXTRA-FACE... FACE)
6561 (FACE (FACE-ATTR VAL ...) FACE)
6563 causes EXTRA-FACE... or (FACE-ATTR VAL ...) to be _merged_ with the
6564 existing definition of FACE. Note that this isn't necessary for the
6565 default face, since every face inherits from the default face.
6567 If this variable is made buffer-local, the face remapping takes effect
6568 only in that buffer. For instance, the mode my-mode could define a
6569 face `my-mode-default', and then in the mode setup function, do:
6571 (set (make-local-variable 'face-remapping-alist)
6572 '((default my-mode-default)))).
6574 Because Emacs normally only redraws screen areas when the underlying
6575 buffer contents change, you may need to call `redraw-display' after
6576 changing this variable for it to take effect. */);
6577 Vface_remapping_alist = Qnil;
6579 DEFVAR_LISP ("face-font-rescale-alist", Vface_font_rescale_alist,
6580 doc: /* Alist of fonts vs the rescaling factors.
6581 Each element is a cons (FONT-PATTERN . RESCALE-RATIO), where
6582 FONT-PATTERN is a font-spec or a regular expression matching a font name, and
6583 RESCALE-RATIO is a floating point number to specify how much larger
6584 \(or smaller) font we should use. For instance, if a face requests
6585 a font of 10 point, we actually use a font of 10 * RESCALE-RATIO point. */);
6586 Vface_font_rescale_alist = Qnil;
6588 #ifdef HAVE_WINDOW_SYSTEM
6589 defsubr (&Sbitmap_spec_p);
6590 defsubr (&Sx_list_fonts);
6591 defsubr (&Sinternal_face_x_get_resource);
6592 defsubr (&Sx_family_fonts);
6593 #endif