Implement face-remapping-alist feature
[emacs.git] / src / xfaces.c
blob074b71b13c1af200825f290bbc57fe9528a422ce
1 /* xfaces.c -- "Face" primitives.
2 Copyright (C) 1993, 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008 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. Relative proportionate width, aka character set width or set
31 width (swidth), e.g. `semi-compressed'.
33 3. Font height in 1/10pt.
35 4. Font weight, e.g. `bold'.
37 5. Font slant, e.g. `italic'.
39 6. Foreground color.
41 7. Background color.
43 8. Whether or not characters should be underlined, and in what color.
45 9. Whether or not characters should be displayed in inverse video.
47 10. A background stipple, a bitmap.
49 11. Whether or not characters should be overlined, and in what color.
51 12. Whether or not characters should be strike-through, and in what
52 color.
54 13. Whether or not a box should be drawn around characters, the box
55 type, and, for simple boxes, in what color.
57 14. Font-spec, or nil. This is a special attribute.
59 A font-spec is a collection of font attributes (specs).
61 When this attribute is specified, the face uses a font matching
62 with the specs as is except for what overwritten by the specs in
63 the fontset (see below). In addition, the other font-related
64 attributes (1st thru 5th) are updated from the spec.
66 On the other hand, if one of the other font-related attributes are
67 specified, the correspoinding specs in this attribute is set to nil.
69 15. A face name or list of face names from which to inherit attributes.
71 16. A specified average font width, which is invisible from Lisp,
72 and is used to ensure that a font specified on the command line,
73 for example, can be matched exactly.
75 17. A fontset name. This is another special attribute.
77 A fontset is a mappings from characters to font-specs, and the
78 specs overwrite the font-spec in the 14th attribute.
81 Faces are frame-local by nature because Emacs allows to define the
82 same named face (face names are symbols) differently for different
83 frames. Each frame has an alist of face definitions for all named
84 faces. The value of a named face in such an alist is a Lisp vector
85 with the symbol `face' in slot 0, and a slot for each of the face
86 attributes mentioned above.
88 There is also a global face alist `Vface_new_frame_defaults'. Face
89 definitions from this list are used to initialize faces of newly
90 created frames.
92 A face doesn't have to specify all attributes. Those not specified
93 have a value of `unspecified'. Faces specifying all attributes but
94 the 14th are called `fully-specified'.
97 Face merging.
99 The display style of a given character in the text is determined by
100 combining several faces. This process is called `face merging'.
101 Any aspect of the display style that isn't specified by overlays or
102 text properties is taken from the `default' face. Since it is made
103 sure that the default face is always fully-specified, face merging
104 always results in a fully-specified face.
107 Face realization.
109 After all face attributes for a character have been determined by
110 merging faces of that character, that face is `realized'. The
111 realization process maps face attributes to what is physically
112 available on the system where Emacs runs. The result is a
113 `realized face' in form of a struct face which is stored in the
114 face cache of the frame on which it was realized.
116 Face realization is done in the context of the character to display
117 because different fonts may be used for different characters. In
118 other words, for characters that have different font
119 specifications, different realized faces are needed to display
120 them.
122 Font specification is done by fontsets. See the comment in
123 fontset.c for the details. In the current implementation, all ASCII
124 characters share the same font in a fontset.
126 Faces are at first realized for ASCII characters, and, at that
127 time, assigned a specific realized fontset. Hereafter, we call
128 such a face as `ASCII face'. When a face for a multibyte character
129 is realized, it inherits (thus shares) a fontset of an ASCII face
130 that has the same attributes other than font-related ones.
132 Thus, all realized faces have a realized fontset.
135 Unibyte text.
137 Unibyte text (i.e. raw 8-bit characters) is displayed with the same
138 font as ASCII characters. That is because it is expected that
139 unibyte text users specify a font that is suitable both for ASCII
140 and raw 8-bit characters.
143 Font selection.
145 Font selection tries to find the best available matching font for a
146 given (character, face) combination.
148 If the face specifies a fontset name, that fontset determines a
149 pattern for fonts of the given character. If the face specifies a
150 font name or the other font-related attributes, a fontset is
151 realized from the default fontset. In that case, that
152 specification determines a pattern for ASCII characters and the
153 default fontset determines a pattern for multibyte characters.
155 Available fonts on the system on which Emacs runs are then matched
156 against the font pattern. The result of font selection is the best
157 match for the given face attributes in this font list.
159 Font selection can be influenced by the user.
161 1. The user can specify the relative importance he gives the face
162 attributes width, height, weight, and slant by setting
163 face-font-selection-order (faces.el) to a list of face attribute
164 names. The default is '(:width :height :weight :slant), and means
165 that font selection first tries to find a good match for the font
166 width specified by a face, then---within fonts with that
167 width---tries to find a best match for the specified font height,
168 etc.
170 2. Setting face-font-family-alternatives allows the user to
171 specify alternative font families to try if a family specified by a
172 face doesn't exist.
174 3. Setting face-font-registry-alternatives allows the user to
175 specify all alternative font registries to try for a face
176 specifying a registry.
178 4. Setting face-ignored-fonts allows the user to ignore specific
179 fonts.
182 Character composition.
184 Usually, the realization process is already finished when Emacs
185 actually reflects the desired glyph matrix on the screen. However,
186 on displaying a composition (sequence of characters to be composed
187 on the screen), a suitable font for the components of the
188 composition is selected and realized while drawing them on the
189 screen, i.e. the realization process is delayed but in principle
190 the same.
193 Initialization of basic faces.
195 The faces `default', `modeline' are considered `basic faces'.
196 When redisplay happens the first time for a newly created frame,
197 basic faces are realized for CHARSET_ASCII. Frame parameters are
198 used to fill in unspecified attributes of the default face. */
200 #include <config.h>
201 #include <stdio.h>
202 #include <sys/types.h>
203 #include <sys/stat.h>
204 #include <stdio.h> /* This needs to be before termchar.h */
206 #include "lisp.h"
207 #include "character.h"
208 #include "charset.h"
209 #include "keyboard.h"
210 #include "frame.h"
211 #include "termhooks.h"
213 #ifdef HAVE_X_WINDOWS
214 #include "xterm.h"
215 #ifdef USE_MOTIF
216 #include <Xm/Xm.h>
217 #include <Xm/XmStrDefs.h>
218 #endif /* USE_MOTIF */
219 #endif /* HAVE_X_WINDOWS */
221 #ifdef MSDOS
222 #include "dosfns.h"
223 #endif
225 #ifdef WINDOWSNT
226 #include "w32term.h"
227 #include "fontset.h"
228 /* Redefine X specifics to W32 equivalents to avoid cluttering the
229 code with #ifdef blocks. */
230 #undef FRAME_X_DISPLAY_INFO
231 #define FRAME_X_DISPLAY_INFO FRAME_W32_DISPLAY_INFO
232 #define x_display_info w32_display_info
233 #define FRAME_X_FONT_TABLE FRAME_W32_FONT_TABLE
234 #define check_x check_w32
235 #define x_list_fonts w32_list_fonts
236 #define GCGraphicsExposures 0
237 #endif /* WINDOWSNT */
239 #ifdef MAC_OS
240 #include "macterm.h"
241 #define x_display_info mac_display_info
242 #define check_x check_mac
243 #endif /* MAC_OS */
245 #include "buffer.h"
246 #include "dispextern.h"
247 #include "blockinput.h"
248 #include "window.h"
249 #include "intervals.h"
250 #include "termchar.h"
252 #include "font.h"
253 #ifdef HAVE_WINDOW_SYSTEM
254 #include "fontset.h"
255 #endif /* HAVE_WINDOW_SYSTEM */
257 #ifdef HAVE_X_WINDOWS
259 /* Compensate for a bug in Xos.h on some systems, on which it requires
260 time.h. On some such systems, Xos.h tries to redefine struct
261 timeval and struct timezone if USG is #defined while it is
262 #included. */
264 #ifdef XOS_NEEDS_TIME_H
265 #include <time.h>
266 #undef USG
267 #include <X11/Xos.h>
268 #define USG
269 #define __TIMEVAL__
270 #else /* not XOS_NEEDS_TIME_H */
271 #include <X11/Xos.h>
272 #endif /* not XOS_NEEDS_TIME_H */
274 #endif /* HAVE_X_WINDOWS */
276 #include <ctype.h>
278 /* Number of pt per inch (from the TeXbook). */
280 #define PT_PER_INCH 72.27
282 /* Non-zero if face attribute ATTR is unspecified. */
284 #define UNSPECIFIEDP(ATTR) EQ ((ATTR), Qunspecified)
286 /* Non-zero if face attribute ATTR is `ignore-defface'. */
288 #define IGNORE_DEFFACE_P(ATTR) EQ ((ATTR), Qignore_defface)
290 /* Value is the number of elements of VECTOR. */
292 #define DIM(VECTOR) (sizeof (VECTOR) / sizeof *(VECTOR))
294 /* Make a copy of string S on the stack using alloca. Value is a pointer
295 to the copy. */
297 #define STRDUPA(S) strcpy ((char *) alloca (strlen ((S)) + 1), (S))
299 /* Make a copy of the contents of Lisp string S on the stack using
300 alloca. Value is a pointer to the copy. */
302 #define LSTRDUPA(S) STRDUPA (SDATA ((S)))
304 /* Size of hash table of realized faces in face caches (should be a
305 prime number). */
307 #define FACE_CACHE_BUCKETS_SIZE 1001
309 /* Keyword symbols used for face attribute names. */
311 Lisp_Object QCfamily, QCheight, QCweight, QCslant, QCunderline;
312 Lisp_Object QCinverse_video, QCforeground, QCbackground, QCstipple;
313 Lisp_Object QCwidth, QCfont, QCbold, QCitalic;
314 Lisp_Object QCreverse_video;
315 Lisp_Object QCoverline, QCstrike_through, QCbox, QCinherit;
316 Lisp_Object QCfontset;
318 /* Keywords symbols used for font properties. */
319 extern Lisp_Object QCfoundry, QCadstyle, QCregistry;
320 extern Lisp_Object QCspacing, QCsize, QCavgwidth;
321 extern Lisp_Object Qp;
323 /* Symbols used for attribute values. */
325 Lisp_Object Qnormal, Qbold, Qultra_light, Qextra_light, Qlight;
326 Lisp_Object Qsemi_light, Qsemi_bold, Qextra_bold, Qultra_bold;
327 Lisp_Object Qoblique, Qitalic, Qreverse_oblique, Qreverse_italic;
328 Lisp_Object Qultra_condensed, Qextra_condensed, Qcondensed;
329 Lisp_Object Qsemi_condensed, Qsemi_expanded, Qexpanded, Qextra_expanded;
330 Lisp_Object Qultra_expanded;
331 Lisp_Object Qreleased_button, Qpressed_button;
332 Lisp_Object QCstyle, QCcolor, QCline_width;
333 Lisp_Object Qunspecified;
334 Lisp_Object Qignore_defface;
336 char unspecified_fg[] = "unspecified-fg", unspecified_bg[] = "unspecified-bg";
338 /* The name of the function to call when the background of the frame
339 has changed, frame_set_background_mode. */
341 Lisp_Object Qframe_set_background_mode;
343 /* Names of basic faces. */
345 Lisp_Object Qdefault, Qtool_bar, Qregion, Qfringe;
346 Lisp_Object Qheader_line, Qscroll_bar, Qcursor, Qborder, Qmouse, Qmenu;
347 Lisp_Object Qmode_line_inactive, Qvertical_border;
348 extern Lisp_Object Qmode_line;
350 /* The symbol `face-alias'. A symbols having that property is an
351 alias for another face. Value of the property is the name of
352 the aliased face. */
354 Lisp_Object Qface_alias;
356 extern Lisp_Object Qcircular_list;
358 /* Default stipple pattern used on monochrome displays. This stipple
359 pattern is used on monochrome displays instead of shades of gray
360 for a face background color. See `set-face-stipple' for possible
361 values for this variable. */
363 Lisp_Object Vface_default_stipple;
365 /* Alist of alternative font families. Each element is of the form
366 (FAMILY FAMILY1 FAMILY2 ...). If fonts of FAMILY can't be loaded,
367 try FAMILY1, then FAMILY2, ... */
369 Lisp_Object Vface_alternative_font_family_alist;
371 /* Alist of alternative font registries. Each element is of the form
372 (REGISTRY REGISTRY1 REGISTRY2...). If fonts of REGISTRY can't be
373 loaded, try REGISTRY1, then REGISTRY2, ... */
375 Lisp_Object Vface_alternative_font_registry_alist;
377 /* Allowed scalable fonts. A value of nil means don't allow any
378 scalable fonts. A value of t means allow the use of any scalable
379 font. Otherwise, value must be a list of regular expressions. A
380 font may be scaled if its name matches a regular expression in the
381 list. */
383 Lisp_Object Vscalable_fonts_allowed, Qscalable_fonts_allowed;
385 /* List of regular expressions that matches names of fonts to ignore. */
387 Lisp_Object Vface_ignored_fonts;
389 /* Alist of font name patterns vs the rescaling factor. */
391 Lisp_Object Vface_font_rescale_alist;
393 /* Maximum number of fonts to consider in font_list. If not an
394 integer > 0, DEFAULT_FONT_LIST_LIMIT is used instead. */
396 Lisp_Object Vfont_list_limit;
397 #define DEFAULT_FONT_LIST_LIMIT 100
399 /* The symbols `foreground-color' and `background-color' which can be
400 used as part of a `face' property. This is for compatibility with
401 Emacs 20.2. */
403 Lisp_Object Qforeground_color, Qbackground_color;
405 /* The symbols `face' and `mouse-face' used as text properties. */
407 Lisp_Object Qface;
408 extern Lisp_Object Qmouse_face;
410 /* Property for basic faces which other faces cannot inherit. */
412 Lisp_Object Qface_no_inherit;
414 /* Error symbol for wrong_type_argument in load_pixmap. */
416 Lisp_Object Qbitmap_spec_p;
418 /* Alist of global face definitions. Each element is of the form
419 (FACE . LFACE) where FACE is a symbol naming a face and LFACE
420 is a Lisp vector of face attributes. These faces are used
421 to initialize faces for new frames. */
423 Lisp_Object Vface_new_frame_defaults;
425 /* Alist of face remappings. Each element is of the form:
426 (FACE REPLACEMENT...) which causes display of the face FACE to use
427 REPLACEMENT... instead. REPLACEMENT... is interpreted the same way
428 the value of a `face' text property is: it may be (1) A face name,
429 (2) A list of face names, (3) A property-list of face attribute/value
430 pairs, or (4) A list of face names intermixed with lists containing
431 face attribute/value pairs.
433 Multiple entries in REPLACEMENT... are merged together to form the final
434 result, with faces or attributes earlier in the list taking precedence
435 over those that are later.
437 Face-name remapping cycles are suppressed; recursive references use
438 the underlying face instead of the remapped face. */
440 Lisp_Object Vface_remapping_alist;
442 /* The next ID to assign to Lisp faces. */
444 static int next_lface_id;
446 /* A vector mapping Lisp face Id's to face names. */
448 static Lisp_Object *lface_id_to_name;
449 static int lface_id_to_name_size;
451 /* TTY color-related functions (defined in tty-colors.el). */
453 Lisp_Object Qtty_color_desc, Qtty_color_by_index, Qtty_color_standard_values;
455 /* The name of the function used to compute colors on TTYs. */
457 Lisp_Object Qtty_color_alist;
459 /* An alist of defined terminal colors and their RGB values. */
461 Lisp_Object Vtty_defined_color_alist;
463 /* Counter for calls to clear_face_cache. If this counter reaches
464 CLEAR_FONT_TABLE_COUNT, and a frame has more than
465 CLEAR_FONT_TABLE_NFONTS load, unused fonts are freed. */
467 static int clear_font_table_count;
468 #define CLEAR_FONT_TABLE_COUNT 100
469 #define CLEAR_FONT_TABLE_NFONTS 10
471 /* Non-zero means face attributes have been changed since the last
472 redisplay. Used in redisplay_internal. */
474 int face_change_count;
476 /* Non-zero means don't display bold text if a face's foreground
477 and background colors are the inverse of the default colors of the
478 display. This is a kluge to suppress `bold black' foreground text
479 which is hard to read on an LCD monitor. */
481 int tty_suppress_bold_inverse_default_colors_p;
483 /* A list of the form `((x . y))' used to avoid consing in
484 Finternal_set_lisp_face_attribute. */
486 static Lisp_Object Vparam_value_alist;
488 /* The total number of colors currently allocated. */
490 #if GLYPH_DEBUG
491 static int ncolors_allocated;
492 static int npixmaps_allocated;
493 static int ngcs;
494 #endif
496 /* Non-zero means the definition of the `menu' face for new frames has
497 been changed. */
499 int menu_face_changed_default;
502 /* Function prototypes. */
504 struct font_name;
505 struct table_entry;
506 struct named_merge_point;
508 static void map_tty_color P_ ((struct frame *, struct face *,
509 enum lface_attribute_index, int *));
510 static Lisp_Object resolve_face_name P_ ((Lisp_Object, int));
511 static int may_use_scalable_font_p P_ ((const char *));
512 static void set_font_frame_param P_ ((Lisp_Object, Lisp_Object));
513 static int get_lface_attributes P_ ((struct frame *, Lisp_Object, Lisp_Object *,
514 int, struct named_merge_point *));
515 static int load_pixmap P_ ((struct frame *, Lisp_Object, unsigned *, unsigned *));
516 static unsigned char *xstrlwr P_ ((unsigned char *));
517 static struct frame *frame_or_selected_frame P_ ((Lisp_Object, int));
518 static void load_face_colors P_ ((struct frame *, struct face *, Lisp_Object *));
519 static void free_face_colors P_ ((struct frame *, struct face *));
520 static int face_color_gray_p P_ ((struct frame *, char *));
521 static struct face *realize_face P_ ((struct face_cache *, Lisp_Object *,
522 int));
523 static struct face *realize_non_ascii_face P_ ((struct frame *, Lisp_Object,
524 struct face *));
525 static struct face *realize_x_face P_ ((struct face_cache *, Lisp_Object *));
526 static struct face *realize_tty_face P_ ((struct face_cache *, Lisp_Object *));
527 static int realize_basic_faces P_ ((struct frame *));
528 static int realize_default_face P_ ((struct frame *));
529 static void realize_named_face P_ ((struct frame *, Lisp_Object, int));
530 static int lface_fully_specified_p P_ ((Lisp_Object *));
531 static int lface_equal_p P_ ((Lisp_Object *, Lisp_Object *));
532 static unsigned hash_string_case_insensitive P_ ((Lisp_Object));
533 static unsigned lface_hash P_ ((Lisp_Object *));
534 static int lface_same_font_attributes_p P_ ((Lisp_Object *, Lisp_Object *));
535 static struct face_cache *make_face_cache P_ ((struct frame *));
536 static void clear_face_gcs P_ ((struct face_cache *));
537 static void free_face_cache P_ ((struct face_cache *));
538 static int face_fontset P_ ((Lisp_Object *));
539 static void merge_face_vectors P_ ((struct frame *, Lisp_Object *, Lisp_Object*,
540 struct named_merge_point *));
541 static int merge_face_ref P_ ((struct frame *, Lisp_Object, Lisp_Object *,
542 int, struct named_merge_point *));
543 static int set_lface_from_font P_ ((struct frame *, Lisp_Object, Lisp_Object,
544 int));
545 static Lisp_Object lface_from_face_name P_ ((struct frame *, Lisp_Object, int));
546 static struct face *make_realized_face P_ ((Lisp_Object *));
547 static void cache_face P_ ((struct face_cache *, struct face *, unsigned));
548 static void uncache_face P_ ((struct face_cache *, struct face *));
550 #ifdef HAVE_WINDOW_SYSTEM
552 static GC x_create_gc P_ ((struct frame *, unsigned long, XGCValues *));
553 static void x_free_gc P_ ((struct frame *, GC));
555 #ifdef WINDOWSNT
556 extern Lisp_Object w32_list_fonts P_ ((struct frame *, Lisp_Object, int, int));
557 #endif /* WINDOWSNT */
559 #ifdef USE_X_TOOLKIT
560 static void x_update_menu_appearance P_ ((struct frame *));
562 extern void free_frame_menubar P_ ((struct frame *));
563 #endif /* USE_X_TOOLKIT */
565 #endif /* HAVE_WINDOW_SYSTEM */
568 /***********************************************************************
569 Utilities
570 ***********************************************************************/
572 #ifdef HAVE_X_WINDOWS
574 #ifdef DEBUG_X_COLORS
576 /* The following is a poor mans infrastructure for debugging X color
577 allocation problems on displays with PseudoColor-8. Some X servers
578 like 3.3.5 XF86_SVGA with Matrox cards apparently don't implement
579 color reference counts completely so that they don't signal an
580 error when a color is freed whose reference count is already 0.
581 Other X servers do. To help me debug this, the following code
582 implements a simple reference counting schema of its own, for a
583 single display/screen. --gerd. */
585 /* Reference counts for pixel colors. */
587 int color_count[256];
589 /* Register color PIXEL as allocated. */
591 void
592 register_color (pixel)
593 unsigned long pixel;
595 xassert (pixel < 256);
596 ++color_count[pixel];
600 /* Register color PIXEL as deallocated. */
602 void
603 unregister_color (pixel)
604 unsigned long pixel;
606 xassert (pixel < 256);
607 if (color_count[pixel] > 0)
608 --color_count[pixel];
609 else
610 abort ();
614 /* Register N colors from PIXELS as deallocated. */
616 void
617 unregister_colors (pixels, n)
618 unsigned long *pixels;
619 int n;
621 int i;
622 for (i = 0; i < n; ++i)
623 unregister_color (pixels[i]);
627 DEFUN ("dump-colors", Fdump_colors, Sdump_colors, 0, 0, 0,
628 doc: /* Dump currently allocated colors to stderr. */)
631 int i, n;
633 fputc ('\n', stderr);
635 for (i = n = 0; i < sizeof color_count / sizeof color_count[0]; ++i)
636 if (color_count[i])
638 fprintf (stderr, "%3d: %5d", i, color_count[i]);
639 ++n;
640 if (n % 5 == 0)
641 fputc ('\n', stderr);
642 else
643 fputc ('\t', stderr);
646 if (n % 5 != 0)
647 fputc ('\n', stderr);
648 return Qnil;
651 #endif /* DEBUG_X_COLORS */
654 /* Free colors used on frame F. PIXELS is an array of NPIXELS pixel
655 color values. Interrupt input must be blocked when this function
656 is called. */
658 void
659 x_free_colors (f, pixels, npixels)
660 struct frame *f;
661 unsigned long *pixels;
662 int npixels;
664 int class = FRAME_X_DISPLAY_INFO (f)->visual->class;
666 /* If display has an immutable color map, freeing colors is not
667 necessary and some servers don't allow it. So don't do it. */
668 if (class != StaticColor && class != StaticGray && class != TrueColor)
670 #ifdef DEBUG_X_COLORS
671 unregister_colors (pixels, npixels);
672 #endif
673 XFreeColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
674 pixels, npixels, 0);
679 /* Free colors used on frame F. PIXELS is an array of NPIXELS pixel
680 color values. Interrupt input must be blocked when this function
681 is called. */
683 void
684 x_free_dpy_colors (dpy, screen, cmap, pixels, npixels)
685 Display *dpy;
686 Screen *screen;
687 Colormap cmap;
688 unsigned long *pixels;
689 int npixels;
691 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
692 int class = dpyinfo->visual->class;
694 /* If display has an immutable color map, freeing colors is not
695 necessary and some servers don't allow it. So don't do it. */
696 if (class != StaticColor && class != StaticGray && class != TrueColor)
698 #ifdef DEBUG_X_COLORS
699 unregister_colors (pixels, npixels);
700 #endif
701 XFreeColors (dpy, cmap, pixels, npixels, 0);
706 /* Create and return a GC for use on frame F. GC values and mask
707 are given by XGCV and MASK. */
709 static INLINE GC
710 x_create_gc (f, mask, xgcv)
711 struct frame *f;
712 unsigned long mask;
713 XGCValues *xgcv;
715 GC gc;
716 BLOCK_INPUT;
717 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), mask, xgcv);
718 UNBLOCK_INPUT;
719 IF_DEBUG (++ngcs);
720 return gc;
724 /* Free GC which was used on frame F. */
726 static INLINE void
727 x_free_gc (f, gc)
728 struct frame *f;
729 GC gc;
731 eassert (interrupt_input_blocked);
732 IF_DEBUG (xassert (--ngcs >= 0));
733 XFreeGC (FRAME_X_DISPLAY (f), gc);
736 #endif /* HAVE_X_WINDOWS */
738 #ifdef WINDOWSNT
739 /* W32 emulation of GCs */
741 static INLINE GC
742 x_create_gc (f, mask, xgcv)
743 struct frame *f;
744 unsigned long mask;
745 XGCValues *xgcv;
747 GC gc;
748 BLOCK_INPUT;
749 gc = XCreateGC (NULL, FRAME_W32_WINDOW (f), mask, xgcv);
750 UNBLOCK_INPUT;
751 IF_DEBUG (++ngcs);
752 return gc;
756 /* Free GC which was used on frame F. */
758 static INLINE void
759 x_free_gc (f, gc)
760 struct frame *f;
761 GC gc;
763 IF_DEBUG (xassert (--ngcs >= 0));
764 xfree (gc);
767 #endif /* WINDOWSNT */
769 #ifdef MAC_OS
770 /* Mac OS emulation of GCs */
772 static INLINE GC
773 x_create_gc (f, mask, xgcv)
774 struct frame *f;
775 unsigned long mask;
776 XGCValues *xgcv;
778 GC gc;
779 BLOCK_INPUT;
780 gc = XCreateGC (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f), mask, xgcv);
781 UNBLOCK_INPUT;
782 IF_DEBUG (++ngcs);
783 return gc;
786 static INLINE void
787 x_free_gc (f, gc)
788 struct frame *f;
789 GC gc;
791 eassert (interrupt_input_blocked);
792 IF_DEBUG (xassert (--ngcs >= 0));
793 XFreeGC (FRAME_MAC_DISPLAY (f), gc);
796 #endif /* MAC_OS */
798 /* Like strcasecmp/stricmp. Used to compare parts of font names which
799 are in ISO8859-1. */
802 xstrcasecmp (s1, s2)
803 const unsigned char *s1, *s2;
805 while (*s1 && *s2)
807 unsigned char c1 = tolower (*s1);
808 unsigned char c2 = tolower (*s2);
809 if (c1 != c2)
810 return c1 < c2 ? -1 : 1;
811 ++s1, ++s2;
814 if (*s1 == 0)
815 return *s2 == 0 ? 0 : -1;
816 return 1;
820 /* If FRAME is nil, return a pointer to the selected frame.
821 Otherwise, check that FRAME is a live frame, and return a pointer
822 to it. NPARAM is the parameter number of FRAME, for
823 CHECK_LIVE_FRAME. This is here because it's a frequent pattern in
824 Lisp function definitions. */
826 static INLINE struct frame *
827 frame_or_selected_frame (frame, nparam)
828 Lisp_Object frame;
829 int nparam;
831 if (NILP (frame))
832 frame = selected_frame;
834 CHECK_LIVE_FRAME (frame);
835 return XFRAME (frame);
839 /***********************************************************************
840 Frames and faces
841 ***********************************************************************/
843 /* Initialize face cache and basic faces for frame F. */
845 void
846 init_frame_faces (f)
847 struct frame *f;
849 /* Make a face cache, if F doesn't have one. */
850 if (FRAME_FACE_CACHE (f) == NULL)
851 FRAME_FACE_CACHE (f) = make_face_cache (f);
853 #ifdef HAVE_WINDOW_SYSTEM
854 /* Make the image cache. */
855 if (FRAME_WINDOW_P (f))
857 if (FRAME_IMAGE_CACHE (f) == NULL)
858 /* Is that ever possible?? --Stef */
859 FRAME_IMAGE_CACHE (f) = make_image_cache ();
860 ++FRAME_IMAGE_CACHE (f)->refcount;
862 #endif /* HAVE_WINDOW_SYSTEM */
864 /* Realize basic faces. Must have enough information in frame
865 parameters to realize basic faces at this point. */
866 #ifdef HAVE_X_WINDOWS
867 if (!FRAME_X_P (f) || FRAME_X_WINDOW (f))
868 #endif
869 #ifdef WINDOWSNT
870 if (!FRAME_WINDOW_P (f) || FRAME_W32_WINDOW (f))
871 #endif
872 #ifdef MAC_OS
873 if (!FRAME_MAC_P (f) || FRAME_MAC_WINDOW (f))
874 #endif
875 if (!realize_basic_faces (f))
876 abort ();
880 /* Free face cache of frame F. Called from Fdelete_frame. */
882 void
883 free_frame_faces (f)
884 struct frame *f;
886 struct face_cache *face_cache = FRAME_FACE_CACHE (f);
888 if (face_cache)
890 free_face_cache (face_cache);
891 FRAME_FACE_CACHE (f) = NULL;
894 #ifdef HAVE_WINDOW_SYSTEM
895 if (FRAME_WINDOW_P (f))
897 struct image_cache *image_cache = FRAME_IMAGE_CACHE (f);
898 if (image_cache)
900 --image_cache->refcount;
901 if (image_cache->refcount == 0)
902 free_image_cache (f);
905 #endif /* HAVE_WINDOW_SYSTEM */
909 /* Clear face caches, and recompute basic faces for frame F. Call
910 this after changing frame parameters on which those faces depend,
911 or when realized faces have been freed due to changing attributes
912 of named faces. */
914 void
915 recompute_basic_faces (f)
916 struct frame *f;
918 if (FRAME_FACE_CACHE (f))
920 clear_face_cache (0);
921 if (!realize_basic_faces (f))
922 abort ();
927 /* Clear the face caches of all frames. CLEAR_FONTS_P non-zero means
928 try to free unused fonts, too. */
930 void
931 clear_face_cache (clear_fonts_p)
932 int clear_fonts_p;
934 #ifdef HAVE_WINDOW_SYSTEM
935 Lisp_Object tail, frame;
936 struct frame *f;
938 if (clear_fonts_p
939 || ++clear_font_table_count == CLEAR_FONT_TABLE_COUNT)
941 #if 0
942 /* Not yet implemented. */
943 clear_font_cache (frame);
944 #endif
946 /* From time to time see if we can unload some fonts. This also
947 frees all realized faces on all frames. Fonts needed by
948 faces will be loaded again when faces are realized again. */
949 clear_font_table_count = 0;
951 FOR_EACH_FRAME (tail, frame)
953 struct frame *f = XFRAME (frame);
954 if (FRAME_WINDOW_P (f)
955 && FRAME_X_DISPLAY_INFO (f)->n_fonts > CLEAR_FONT_TABLE_NFONTS)
956 free_all_realized_faces (frame);
959 else
961 /* Clear GCs of realized faces. */
962 FOR_EACH_FRAME (tail, frame)
964 f = XFRAME (frame);
965 if (FRAME_WINDOW_P (f))
966 clear_face_gcs (FRAME_FACE_CACHE (f));
968 clear_image_caches (Qnil);
970 #endif /* HAVE_WINDOW_SYSTEM */
974 DEFUN ("clear-face-cache", Fclear_face_cache, Sclear_face_cache, 0, 1, 0,
975 doc: /* Clear face caches on all frames.
976 Optional THOROUGHLY non-nil means try to free unused fonts, too. */)
977 (thoroughly)
978 Lisp_Object thoroughly;
980 clear_face_cache (!NILP (thoroughly));
981 ++face_change_count;
982 ++windows_or_buffers_changed;
983 return Qnil;
987 /***********************************************************************
988 X Pixmaps
989 ***********************************************************************/
991 #ifdef HAVE_WINDOW_SYSTEM
993 DEFUN ("bitmap-spec-p", Fbitmap_spec_p, Sbitmap_spec_p, 1, 1, 0,
994 doc: /* Value is non-nil if OBJECT is a valid bitmap specification.
995 A bitmap specification is either a string, a file name, or a list
996 \(WIDTH HEIGHT DATA) where WIDTH is the pixel width of the bitmap,
997 HEIGHT is its height, and DATA is a string containing the bits of
998 the pixmap. Bits are stored row by row, each row occupies
999 \(WIDTH + 7)/8 bytes. */)
1000 (object)
1001 Lisp_Object object;
1003 int pixmap_p = 0;
1005 if (STRINGP (object))
1006 /* If OBJECT is a string, it's a file name. */
1007 pixmap_p = 1;
1008 else if (CONSP (object))
1010 /* Otherwise OBJECT must be (WIDTH HEIGHT DATA), WIDTH and
1011 HEIGHT must be integers > 0, and DATA must be string large
1012 enough to hold a bitmap of the specified size. */
1013 Lisp_Object width, height, data;
1015 height = width = data = Qnil;
1017 if (CONSP (object))
1019 width = XCAR (object);
1020 object = XCDR (object);
1021 if (CONSP (object))
1023 height = XCAR (object);
1024 object = XCDR (object);
1025 if (CONSP (object))
1026 data = XCAR (object);
1030 if (NATNUMP (width) && NATNUMP (height) && STRINGP (data))
1032 int bytes_per_row = ((XFASTINT (width) + BITS_PER_CHAR - 1)
1033 / BITS_PER_CHAR);
1034 if (SBYTES (data) >= bytes_per_row * XINT (height))
1035 pixmap_p = 1;
1039 return pixmap_p ? Qt : Qnil;
1043 /* Load a bitmap according to NAME (which is either a file name or a
1044 pixmap spec) for use on frame F. Value is the bitmap_id (see
1045 xfns.c). If NAME is nil, return with a bitmap id of zero. If
1046 bitmap cannot be loaded, display a message saying so, and return
1047 zero. Store the bitmap width in *W_PTR and its height in *H_PTR,
1048 if these pointers are not null. */
1050 static int
1051 load_pixmap (f, name, w_ptr, h_ptr)
1052 FRAME_PTR f;
1053 Lisp_Object name;
1054 unsigned int *w_ptr, *h_ptr;
1056 int bitmap_id;
1058 if (NILP (name))
1059 return 0;
1061 CHECK_TYPE (!NILP (Fbitmap_spec_p (name)), Qbitmap_spec_p, name);
1063 BLOCK_INPUT;
1064 if (CONSP (name))
1066 /* Decode a bitmap spec into a bitmap. */
1068 int h, w;
1069 Lisp_Object bits;
1071 w = XINT (Fcar (name));
1072 h = XINT (Fcar (Fcdr (name)));
1073 bits = Fcar (Fcdr (Fcdr (name)));
1075 bitmap_id = x_create_bitmap_from_data (f, SDATA (bits),
1076 w, h);
1078 else
1080 /* It must be a string -- a file name. */
1081 bitmap_id = x_create_bitmap_from_file (f, name);
1083 UNBLOCK_INPUT;
1085 if (bitmap_id < 0)
1087 add_to_log ("Invalid or undefined bitmap `%s'", name, Qnil);
1088 bitmap_id = 0;
1090 if (w_ptr)
1091 *w_ptr = 0;
1092 if (h_ptr)
1093 *h_ptr = 0;
1095 else
1097 #if GLYPH_DEBUG
1098 ++npixmaps_allocated;
1099 #endif
1100 if (w_ptr)
1101 *w_ptr = x_bitmap_width (f, bitmap_id);
1103 if (h_ptr)
1104 *h_ptr = x_bitmap_height (f, bitmap_id);
1107 return bitmap_id;
1110 #endif /* HAVE_WINDOW_SYSTEM */
1114 /***********************************************************************
1115 X Colors
1116 ***********************************************************************/
1118 /* Parse RGB_LIST, and fill in the RGB fields of COLOR.
1119 RGB_LIST should contain (at least) 3 lisp integers.
1120 Return 0 if there's a problem with RGB_LIST, otherwise return 1. */
1122 static int
1123 parse_rgb_list (rgb_list, color)
1124 Lisp_Object rgb_list;
1125 XColor *color;
1127 #define PARSE_RGB_LIST_FIELD(field) \
1128 if (CONSP (rgb_list) && INTEGERP (XCAR (rgb_list))) \
1130 color->field = XINT (XCAR (rgb_list)); \
1131 rgb_list = XCDR (rgb_list); \
1133 else \
1134 return 0;
1136 PARSE_RGB_LIST_FIELD (red);
1137 PARSE_RGB_LIST_FIELD (green);
1138 PARSE_RGB_LIST_FIELD (blue);
1140 return 1;
1144 /* Lookup on frame F the color described by the lisp string COLOR.
1145 The resulting tty color is returned in TTY_COLOR; if STD_COLOR is
1146 non-zero, then the `standard' definition of the same color is
1147 returned in it. */
1149 static int
1150 tty_lookup_color (f, color, tty_color, std_color)
1151 struct frame *f;
1152 Lisp_Object color;
1153 XColor *tty_color, *std_color;
1155 Lisp_Object frame, color_desc;
1157 if (!STRINGP (color) || NILP (Ffboundp (Qtty_color_desc)))
1158 return 0;
1160 XSETFRAME (frame, f);
1162 color_desc = call2 (Qtty_color_desc, color, frame);
1163 if (CONSP (color_desc) && CONSP (XCDR (color_desc)))
1165 Lisp_Object rgb;
1167 if (! INTEGERP (XCAR (XCDR (color_desc))))
1168 return 0;
1170 tty_color->pixel = XINT (XCAR (XCDR (color_desc)));
1172 rgb = XCDR (XCDR (color_desc));
1173 if (! parse_rgb_list (rgb, tty_color))
1174 return 0;
1176 /* Should we fill in STD_COLOR too? */
1177 if (std_color)
1179 /* Default STD_COLOR to the same as TTY_COLOR. */
1180 *std_color = *tty_color;
1182 /* Do a quick check to see if the returned descriptor is
1183 actually _exactly_ equal to COLOR, otherwise we have to
1184 lookup STD_COLOR separately. If it's impossible to lookup
1185 a standard color, we just give up and use TTY_COLOR. */
1186 if ((!STRINGP (XCAR (color_desc))
1187 || NILP (Fstring_equal (color, XCAR (color_desc))))
1188 && !NILP (Ffboundp (Qtty_color_standard_values)))
1190 /* Look up STD_COLOR separately. */
1191 rgb = call1 (Qtty_color_standard_values, color);
1192 if (! parse_rgb_list (rgb, std_color))
1193 return 0;
1197 return 1;
1199 else if (NILP (Fsymbol_value (intern ("tty-defined-color-alist"))))
1200 /* We were called early during startup, and the colors are not
1201 yet set up in tty-defined-color-alist. Don't return a failure
1202 indication, since this produces the annoying "Unable to
1203 load color" messages in the *Messages* buffer. */
1204 return 1;
1205 else
1206 /* tty-color-desc seems to have returned a bad value. */
1207 return 0;
1210 /* A version of defined_color for non-X frames. */
1213 tty_defined_color (f, color_name, color_def, alloc)
1214 struct frame *f;
1215 char *color_name;
1216 XColor *color_def;
1217 int alloc;
1219 int status = 1;
1221 /* Defaults. */
1222 color_def->pixel = FACE_TTY_DEFAULT_COLOR;
1223 color_def->red = 0;
1224 color_def->blue = 0;
1225 color_def->green = 0;
1227 if (*color_name)
1228 status = tty_lookup_color (f, build_string (color_name), color_def, NULL);
1230 if (color_def->pixel == FACE_TTY_DEFAULT_COLOR && *color_name)
1232 if (strcmp (color_name, "unspecified-fg") == 0)
1233 color_def->pixel = FACE_TTY_DEFAULT_FG_COLOR;
1234 else if (strcmp (color_name, "unspecified-bg") == 0)
1235 color_def->pixel = FACE_TTY_DEFAULT_BG_COLOR;
1238 if (color_def->pixel != FACE_TTY_DEFAULT_COLOR)
1239 status = 1;
1241 return status;
1245 /* Decide if color named COLOR_NAME is valid for the display
1246 associated with the frame F; if so, return the rgb values in
1247 COLOR_DEF. If ALLOC is nonzero, allocate a new colormap cell.
1249 This does the right thing for any type of frame. */
1252 defined_color (f, color_name, color_def, alloc)
1253 struct frame *f;
1254 char *color_name;
1255 XColor *color_def;
1256 int alloc;
1258 if (!FRAME_WINDOW_P (f))
1259 return tty_defined_color (f, color_name, color_def, alloc);
1260 #ifdef HAVE_X_WINDOWS
1261 else if (FRAME_X_P (f))
1262 return x_defined_color (f, color_name, color_def, alloc);
1263 #endif
1264 #ifdef WINDOWSNT
1265 else if (FRAME_W32_P (f))
1266 return w32_defined_color (f, color_name, color_def, alloc);
1267 #endif
1268 #ifdef MAC_OS
1269 else if (FRAME_MAC_P (f))
1270 return mac_defined_color (f, color_name, color_def, alloc);
1271 #endif
1272 else
1273 abort ();
1277 /* Given the index IDX of a tty color on frame F, return its name, a
1278 Lisp string. */
1280 Lisp_Object
1281 tty_color_name (f, idx)
1282 struct frame *f;
1283 int idx;
1285 if (idx >= 0 && !NILP (Ffboundp (Qtty_color_by_index)))
1287 Lisp_Object frame;
1288 Lisp_Object coldesc;
1290 XSETFRAME (frame, f);
1291 coldesc = call2 (Qtty_color_by_index, make_number (idx), frame);
1293 if (!NILP (coldesc))
1294 return XCAR (coldesc);
1296 #ifdef MSDOS
1297 /* We can have an MSDOG frame under -nw for a short window of
1298 opportunity before internal_terminal_init is called. DTRT. */
1299 if (FRAME_MSDOS_P (f) && !inhibit_window_system)
1300 return msdos_stdcolor_name (idx);
1301 #endif
1303 if (idx == FACE_TTY_DEFAULT_FG_COLOR)
1304 return build_string (unspecified_fg);
1305 if (idx == FACE_TTY_DEFAULT_BG_COLOR)
1306 return build_string (unspecified_bg);
1308 return Qunspecified;
1312 /* Return non-zero if COLOR_NAME is a shade of gray (or white or
1313 black) on frame F.
1315 The criterion implemented here is not a terribly sophisticated one. */
1317 static int
1318 face_color_gray_p (f, color_name)
1319 struct frame *f;
1320 char *color_name;
1322 XColor color;
1323 int gray_p;
1325 if (defined_color (f, color_name, &color, 0))
1326 gray_p = (/* Any color sufficiently close to black counts as grey. */
1327 (color.red < 5000 && color.green < 5000 && color.blue < 5000)
1329 ((eabs (color.red - color.green)
1330 < max (color.red, color.green) / 20)
1331 && (eabs (color.green - color.blue)
1332 < max (color.green, color.blue) / 20)
1333 && (eabs (color.blue - color.red)
1334 < max (color.blue, color.red) / 20)));
1335 else
1336 gray_p = 0;
1338 return gray_p;
1342 /* Return non-zero if color COLOR_NAME can be displayed on frame F.
1343 BACKGROUND_P non-zero means the color will be used as background
1344 color. */
1346 static int
1347 face_color_supported_p (f, color_name, background_p)
1348 struct frame *f;
1349 char *color_name;
1350 int background_p;
1352 Lisp_Object frame;
1353 XColor not_used;
1355 XSETFRAME (frame, f);
1356 return
1357 #ifdef HAVE_WINDOW_SYSTEM
1358 FRAME_WINDOW_P (f)
1359 ? (!NILP (Fxw_display_color_p (frame))
1360 || xstrcasecmp (color_name, "black") == 0
1361 || xstrcasecmp (color_name, "white") == 0
1362 || (background_p
1363 && face_color_gray_p (f, color_name))
1364 || (!NILP (Fx_display_grayscale_p (frame))
1365 && face_color_gray_p (f, color_name)))
1367 #endif
1368 tty_defined_color (f, color_name, &not_used, 0);
1372 DEFUN ("color-gray-p", Fcolor_gray_p, Scolor_gray_p, 1, 2, 0,
1373 doc: /* Return non-nil if COLOR is a shade of gray (or white or black).
1374 FRAME specifies the frame and thus the display for interpreting COLOR.
1375 If FRAME is nil or omitted, use the selected frame. */)
1376 (color, frame)
1377 Lisp_Object color, frame;
1379 struct frame *f;
1381 CHECK_STRING (color);
1382 if (NILP (frame))
1383 frame = selected_frame;
1384 else
1385 CHECK_FRAME (frame);
1386 f = XFRAME (frame);
1387 return face_color_gray_p (f, SDATA (color)) ? Qt : Qnil;
1391 DEFUN ("color-supported-p", Fcolor_supported_p,
1392 Scolor_supported_p, 1, 3, 0,
1393 doc: /* Return non-nil if COLOR can be displayed on FRAME.
1394 BACKGROUND-P non-nil means COLOR is used as a background.
1395 Otherwise, this function tells whether it can be used as a foreground.
1396 If FRAME is nil or omitted, use the selected frame.
1397 COLOR must be a valid color name. */)
1398 (color, frame, background_p)
1399 Lisp_Object frame, color, background_p;
1401 struct frame *f;
1403 CHECK_STRING (color);
1404 if (NILP (frame))
1405 frame = selected_frame;
1406 else
1407 CHECK_FRAME (frame);
1408 f = XFRAME (frame);
1409 if (face_color_supported_p (f, SDATA (color), !NILP (background_p)))
1410 return Qt;
1411 return Qnil;
1415 /* Load color with name NAME for use by face FACE on frame F.
1416 TARGET_INDEX must be one of LFACE_FOREGROUND_INDEX,
1417 LFACE_BACKGROUND_INDEX, LFACE_UNDERLINE_INDEX, LFACE_OVERLINE_INDEX,
1418 LFACE_STRIKE_THROUGH_INDEX, or LFACE_BOX_INDEX. Value is the
1419 pixel color. If color cannot be loaded, display a message, and
1420 return the foreground, background or underline color of F, but
1421 record that fact in flags of the face so that we don't try to free
1422 these colors. */
1424 unsigned long
1425 load_color (f, face, name, target_index)
1426 struct frame *f;
1427 struct face *face;
1428 Lisp_Object name;
1429 enum lface_attribute_index target_index;
1431 XColor color;
1433 xassert (STRINGP (name));
1434 xassert (target_index == LFACE_FOREGROUND_INDEX
1435 || target_index == LFACE_BACKGROUND_INDEX
1436 || target_index == LFACE_UNDERLINE_INDEX
1437 || target_index == LFACE_OVERLINE_INDEX
1438 || target_index == LFACE_STRIKE_THROUGH_INDEX
1439 || target_index == LFACE_BOX_INDEX);
1441 /* if the color map is full, defined_color will return a best match
1442 to the values in an existing cell. */
1443 if (!defined_color (f, SDATA (name), &color, 1))
1445 add_to_log ("Unable to load color \"%s\"", name, Qnil);
1447 switch (target_index)
1449 case LFACE_FOREGROUND_INDEX:
1450 face->foreground_defaulted_p = 1;
1451 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1452 break;
1454 case LFACE_BACKGROUND_INDEX:
1455 face->background_defaulted_p = 1;
1456 color.pixel = FRAME_BACKGROUND_PIXEL (f);
1457 break;
1459 case LFACE_UNDERLINE_INDEX:
1460 face->underline_defaulted_p = 1;
1461 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1462 break;
1464 case LFACE_OVERLINE_INDEX:
1465 face->overline_color_defaulted_p = 1;
1466 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1467 break;
1469 case LFACE_STRIKE_THROUGH_INDEX:
1470 face->strike_through_color_defaulted_p = 1;
1471 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1472 break;
1474 case LFACE_BOX_INDEX:
1475 face->box_color_defaulted_p = 1;
1476 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1477 break;
1479 default:
1480 abort ();
1483 #if GLYPH_DEBUG
1484 else
1485 ++ncolors_allocated;
1486 #endif
1488 return color.pixel;
1492 #ifdef HAVE_WINDOW_SYSTEM
1494 /* Load colors for face FACE which is used on frame F. Colors are
1495 specified by slots LFACE_BACKGROUND_INDEX and LFACE_FOREGROUND_INDEX
1496 of ATTRS. If the background color specified is not supported on F,
1497 try to emulate gray colors with a stipple from Vface_default_stipple. */
1499 static void
1500 load_face_colors (f, face, attrs)
1501 struct frame *f;
1502 struct face *face;
1503 Lisp_Object *attrs;
1505 Lisp_Object fg, bg;
1507 bg = attrs[LFACE_BACKGROUND_INDEX];
1508 fg = attrs[LFACE_FOREGROUND_INDEX];
1510 /* Swap colors if face is inverse-video. */
1511 if (EQ (attrs[LFACE_INVERSE_INDEX], Qt))
1513 Lisp_Object tmp;
1514 tmp = fg;
1515 fg = bg;
1516 bg = tmp;
1519 /* Check for support for foreground, not for background because
1520 face_color_supported_p is smart enough to know that grays are
1521 "supported" as background because we are supposed to use stipple
1522 for them. */
1523 if (!face_color_supported_p (f, SDATA (bg), 0)
1524 && !NILP (Fbitmap_spec_p (Vface_default_stipple)))
1526 x_destroy_bitmap (f, face->stipple);
1527 face->stipple = load_pixmap (f, Vface_default_stipple,
1528 &face->pixmap_w, &face->pixmap_h);
1531 face->background = load_color (f, face, bg, LFACE_BACKGROUND_INDEX);
1532 face->foreground = load_color (f, face, fg, LFACE_FOREGROUND_INDEX);
1536 /* Free color PIXEL on frame F. */
1538 void
1539 unload_color (f, pixel)
1540 struct frame *f;
1541 unsigned long pixel;
1543 #ifdef HAVE_X_WINDOWS
1544 if (pixel != -1)
1546 BLOCK_INPUT;
1547 x_free_colors (f, &pixel, 1);
1548 UNBLOCK_INPUT;
1550 #endif
1554 /* Free colors allocated for FACE. */
1556 static void
1557 free_face_colors (f, face)
1558 struct frame *f;
1559 struct face *face;
1561 #ifdef HAVE_X_WINDOWS
1562 if (face->colors_copied_bitwise_p)
1563 return;
1565 BLOCK_INPUT;
1567 if (!face->foreground_defaulted_p)
1569 x_free_colors (f, &face->foreground, 1);
1570 IF_DEBUG (--ncolors_allocated);
1573 if (!face->background_defaulted_p)
1575 x_free_colors (f, &face->background, 1);
1576 IF_DEBUG (--ncolors_allocated);
1579 if (face->underline_p
1580 && !face->underline_defaulted_p)
1582 x_free_colors (f, &face->underline_color, 1);
1583 IF_DEBUG (--ncolors_allocated);
1586 if (face->overline_p
1587 && !face->overline_color_defaulted_p)
1589 x_free_colors (f, &face->overline_color, 1);
1590 IF_DEBUG (--ncolors_allocated);
1593 if (face->strike_through_p
1594 && !face->strike_through_color_defaulted_p)
1596 x_free_colors (f, &face->strike_through_color, 1);
1597 IF_DEBUG (--ncolors_allocated);
1600 if (face->box != FACE_NO_BOX
1601 && !face->box_color_defaulted_p)
1603 x_free_colors (f, &face->box_color, 1);
1604 IF_DEBUG (--ncolors_allocated);
1607 UNBLOCK_INPUT;
1608 #endif /* HAVE_X_WINDOWS */
1611 #endif /* HAVE_WINDOW_SYSTEM */
1615 /***********************************************************************
1616 XLFD Font Names
1617 ***********************************************************************/
1619 /* An enumerator for each field of an XLFD font name. */
1621 enum xlfd_field
1623 XLFD_FOUNDRY,
1624 XLFD_FAMILY,
1625 XLFD_WEIGHT,
1626 XLFD_SLANT,
1627 XLFD_SWIDTH,
1628 XLFD_ADSTYLE,
1629 XLFD_PIXEL_SIZE,
1630 XLFD_POINT_SIZE,
1631 XLFD_RESX,
1632 XLFD_RESY,
1633 XLFD_SPACING,
1634 XLFD_AVGWIDTH,
1635 XLFD_REGISTRY,
1636 XLFD_ENCODING,
1637 XLFD_LAST
1640 /* An enumerator for each possible slant value of a font. Taken from
1641 the XLFD specification. */
1643 enum xlfd_slant
1645 XLFD_SLANT_UNKNOWN,
1646 XLFD_SLANT_ROMAN,
1647 XLFD_SLANT_ITALIC,
1648 XLFD_SLANT_OBLIQUE,
1649 XLFD_SLANT_REVERSE_ITALIC,
1650 XLFD_SLANT_REVERSE_OBLIQUE,
1651 XLFD_SLANT_OTHER
1654 /* Relative font weight according to XLFD documentation. */
1656 enum xlfd_weight
1658 XLFD_WEIGHT_UNKNOWN,
1659 XLFD_WEIGHT_ULTRA_LIGHT, /* 10 */
1660 XLFD_WEIGHT_EXTRA_LIGHT, /* 20 */
1661 XLFD_WEIGHT_LIGHT, /* 30 */
1662 XLFD_WEIGHT_SEMI_LIGHT, /* 40: SemiLight, Book, ... */
1663 XLFD_WEIGHT_MEDIUM, /* 50: Medium, Normal, Regular, ... */
1664 XLFD_WEIGHT_SEMI_BOLD, /* 60: SemiBold, DemiBold, ... */
1665 XLFD_WEIGHT_BOLD, /* 70: Bold, ... */
1666 XLFD_WEIGHT_EXTRA_BOLD, /* 80: ExtraBold, Heavy, ... */
1667 XLFD_WEIGHT_ULTRA_BOLD /* 90: UltraBold, Black, ... */
1670 /* Relative proportionate width. */
1672 enum xlfd_swidth
1674 XLFD_SWIDTH_UNKNOWN,
1675 XLFD_SWIDTH_ULTRA_CONDENSED, /* 10 */
1676 XLFD_SWIDTH_EXTRA_CONDENSED, /* 20 */
1677 XLFD_SWIDTH_CONDENSED, /* 30: Condensed, Narrow, Compressed, ... */
1678 XLFD_SWIDTH_SEMI_CONDENSED, /* 40: semicondensed */
1679 XLFD_SWIDTH_MEDIUM, /* 50: Medium, Normal, Regular, ... */
1680 XLFD_SWIDTH_SEMI_EXPANDED, /* 60: SemiExpanded, DemiExpanded, ... */
1681 XLFD_SWIDTH_EXPANDED, /* 70: Expanded... */
1682 XLFD_SWIDTH_EXTRA_EXPANDED, /* 80: ExtraExpanded, Wide... */
1683 XLFD_SWIDTH_ULTRA_EXPANDED /* 90: UltraExpanded... */
1686 /* Order by which font selection chooses fonts. The default values
1687 mean `first, find a best match for the font width, then for the
1688 font height, then for weight, then for slant.' This variable can be
1689 set via set-face-font-sort-order. */
1691 #ifdef MAC_OS
1692 static int font_sort_order[4] = {
1693 XLFD_SWIDTH, XLFD_POINT_SIZE, XLFD_WEIGHT, XLFD_SLANT
1695 #else
1696 static int font_sort_order[4];
1697 #endif
1700 #ifdef HAVE_WINDOW_SYSTEM
1702 /* Return a rescaling ratio of a font of NAME. */
1704 static double
1705 font_rescale_ratio (name)
1706 char *name;
1708 Lisp_Object tail, elt;
1710 for (tail = Vface_font_rescale_alist; CONSP (tail); tail = XCDR (tail))
1712 elt = XCAR (tail);
1713 if (STRINGP (XCAR (elt)) && FLOATP (XCDR (elt))
1714 && fast_c_string_match_ignore_case (XCAR (elt), name) >= 0)
1715 return XFLOAT_DATA (XCDR (elt));
1717 return 1.0;
1720 static enum font_property_index font_props_for_sorting[FONT_SIZE_INDEX];
1722 static int
1723 compare_fonts_by_sort_order (v1, v2)
1724 const void *v1, *v2;
1726 Lisp_Object font1 = *(Lisp_Object *) v1;
1727 Lisp_Object font2 = *(Lisp_Object *) v2;
1728 int i;
1730 for (i = 0; i < FONT_SIZE_INDEX; i++)
1732 enum font_property_index idx = font_props_for_sorting[i];
1733 Lisp_Object val1 = AREF (font1, idx), val2 = AREF (font2, idx);
1734 int result;
1736 if (idx <= FONT_REGISTRY_INDEX)
1738 if (STRINGP (val1))
1739 result = STRINGP (val2) ? strcmp (SDATA (val1), SDATA (val2)) : -1;
1740 else
1741 result = STRINGP (val2) ? 1 : 0;
1743 else
1745 if (INTEGERP (val1))
1746 result = INTEGERP (val2) ? XINT (val1) - XINT (val2) : -1;
1747 else
1748 result = INTEGERP (val2) ? 1 : 0;
1750 if (result)
1751 return result;
1753 return 0;
1756 DEFUN ("x-family-fonts", Fx_family_fonts, Sx_family_fonts, 0, 2, 0,
1757 doc: /* Return a list of available fonts of family FAMILY on FRAME.
1758 If FAMILY is omitted or nil, list all families.
1759 Otherwise, FAMILY must be a string, possibly containing wildcards
1760 `?' and `*'.
1761 If FRAME is omitted or nil, use the selected frame.
1762 Each element of the result is a vector [FAMILY WIDTH POINT-SIZE WEIGHT
1763 SLANT FIXED-P FULL REGISTRY-AND-ENCODING].
1764 FAMILY is the font family name. POINT-SIZE is the size of the
1765 font in 1/10 pt. WIDTH, WEIGHT, and SLANT are symbols describing the
1766 width, weight and slant of the font. These symbols are the same as for
1767 face attributes. FIXED-P is non-nil if the font is fixed-pitch.
1768 FULL is the full name of the font, and REGISTRY-AND-ENCODING is a string
1769 giving the registry and encoding of the font.
1770 The result list is sorted according to the current setting of
1771 the face font sort order. */)
1772 (family, frame)
1773 Lisp_Object family, frame;
1775 Lisp_Object font_spec, vec;
1776 int i, nfonts;
1777 Lisp_Object result;
1779 if (NILP (frame))
1780 frame = selected_frame;
1781 CHECK_LIVE_FRAME (frame);
1783 font_spec = Ffont_spec (0, NULL);
1784 if (!NILP (family))
1786 CHECK_STRING (family);
1787 Ffont_put (font_spec, QCfamily, family);
1789 vec = font_list_entities (frame, font_spec);
1790 nfonts = ASIZE (vec);
1791 if (nfonts == 0)
1792 return Qnil;
1793 if (nfonts > 1)
1795 for (i = 0; i < 4; i++)
1796 switch (font_sort_order[i])
1798 case XLFD_SWIDTH:
1799 font_props_for_sorting[i] = FONT_WIDTH_INDEX; break;
1800 case XLFD_POINT_SIZE:
1801 font_props_for_sorting[i] = FONT_SIZE_INDEX; break;
1802 case XLFD_WEIGHT:
1803 font_props_for_sorting[i] = FONT_WEIGHT_INDEX; break;
1804 default:
1805 font_props_for_sorting[i] = FONT_SLANT_INDEX; break;
1807 font_props_for_sorting[i++] = FONT_FAMILY_INDEX;
1808 font_props_for_sorting[i++] = FONT_FOUNDRY_INDEX;
1809 font_props_for_sorting[i++] = FONT_ADSTYLE_INDEX;
1810 font_props_for_sorting[i++] = FONT_REGISTRY_INDEX;
1812 qsort (XVECTOR (vec)->contents, nfonts, sizeof (Lisp_Object),
1813 compare_fonts_by_sort_order);
1816 result = Qnil;
1817 for (i = nfonts - 1; i >= 0; --i)
1819 Lisp_Object font = AREF (vec, i);
1820 Lisp_Object v = Fmake_vector (make_number (8), Qnil);
1821 int point;
1822 Lisp_Object spacing;
1824 ASET (v, 0, AREF (font, FONT_FAMILY_INDEX));
1825 ASET (v, 1, FONT_WIDTH_SYMBOLIC (font));
1826 point = PIXEL_TO_POINT (XINT (AREF (font, FONT_SIZE_INDEX)) * 10,
1827 XFRAME (frame)->resy);
1828 ASET (v, 2, make_number (point));
1829 ASET (v, 3, FONT_WEIGHT_SYMBOLIC (font));
1830 ASET (v, 4, FONT_SLANT_SYMBOLIC (font));
1831 spacing = Ffont_get (font, QCspacing);
1832 ASET (v, 5, (NILP (spacing) || EQ (spacing, Qp)) ? Qnil : Qt);
1833 ASET (v, 6, Ffont_xlfd_name (font, Qnil));
1834 ASET (v, 7, AREF (font, FONT_REGISTRY_INDEX));
1836 result = Fcons (v, result);
1839 return result;
1843 DEFUN ("x-font-family-list", Fx_font_family_list, Sx_font_family_list,
1844 0, 1, 0,
1845 doc: /* Return a list of available font families on FRAME.
1846 If FRAME is omitted or nil, use the selected frame.
1847 Value is a list of conses (FAMILY . FIXED-P) where FAMILY
1848 is a font family, and FIXED-P is non-nil if fonts of that family
1849 are fixed-pitch. */)
1850 (frame)
1851 Lisp_Object frame;
1853 return Ffont_family_list (frame);
1857 DEFUN ("x-list-fonts", Fx_list_fonts, Sx_list_fonts, 1, 5, 0,
1858 doc: /* Return a list of the names of available fonts matching PATTERN.
1859 If optional arguments FACE and FRAME are specified, return only fonts
1860 the same size as FACE on FRAME.
1861 PATTERN is a string, perhaps with wildcard characters;
1862 the * character matches any substring, and
1863 the ? character matches any single character.
1864 PATTERN is case-insensitive.
1865 FACE is a face name--a symbol.
1867 The return value is a list of strings, suitable as arguments to
1868 `set-face-font'.
1870 Fonts Emacs can't use may or may not be excluded
1871 even if they match PATTERN and FACE.
1872 The optional fourth argument MAXIMUM sets a limit on how many
1873 fonts to match. The first MAXIMUM fonts are reported.
1874 The optional fifth argument WIDTH, if specified, is a number of columns
1875 occupied by a character of a font. In that case, return only fonts
1876 the WIDTH times as wide as FACE on FRAME. */)
1877 (pattern, face, frame, maximum, width)
1878 Lisp_Object pattern, face, frame, maximum, width;
1880 struct frame *f;
1881 int size, avgwidth;
1883 check_x ();
1884 CHECK_STRING (pattern);
1886 if (! NILP (maximum))
1887 CHECK_NATNUM (maximum);
1889 if (!NILP (width))
1890 CHECK_NUMBER (width);
1892 /* We can't simply call check_x_frame because this function may be
1893 called before any frame is created. */
1894 if (NILP (frame))
1895 frame = selected_frame;
1896 f = frame_or_selected_frame (frame, 2);
1897 if (! FRAME_WINDOW_P (f))
1899 /* Perhaps we have not yet created any frame. */
1900 f = NULL;
1901 frame = Qnil;
1902 face = Qnil;
1905 /* Determine the width standard for comparison with the fonts we find. */
1907 if (NILP (face))
1908 size = 0;
1909 else
1911 /* This is of limited utility since it works with character
1912 widths. Keep it for compatibility. --gerd. */
1913 int face_id = lookup_named_face (f, face, 0);
1914 struct face *face = (face_id < 0
1915 ? NULL
1916 : FACE_FROM_ID (f, face_id));
1918 if (face && face->font)
1920 size = face->font->pixel_size;
1921 avgwidth = face->font->average_width;
1923 else
1925 size = FRAME_FONT (f)->pixel_size;
1926 avgwidth = FRAME_FONT (f)->average_width;
1928 if (!NILP (width))
1929 avgwidth *= XINT (width);
1933 Lisp_Object font_spec;
1934 Lisp_Object args[2], tail;
1936 font_spec = font_spec_from_name (pattern);
1937 if (size)
1939 Ffont_put (font_spec, QCsize, make_number (size));
1940 Ffont_put (font_spec, QCavgwidth, make_number (avgwidth));
1942 args[0] = Flist_fonts (font_spec, frame, maximum, Qnil);
1943 for (tail = args[0]; CONSP (tail); tail = XCDR (tail))
1944 XSETCAR (tail, Ffont_xlfd_name (XCAR (tail), Qnil));
1945 if (NILP (frame))
1946 /* We don't have to check fontsets. */
1947 return args[0];
1948 args[1] = list_fontsets (f, pattern, size);
1949 return Fnconc (2, args);
1953 #endif /* HAVE_WINDOW_SYSTEM */
1957 /***********************************************************************
1958 Lisp Faces
1959 ***********************************************************************/
1961 /* Access face attributes of face LFACE, a Lisp vector. */
1963 #define LFACE_FAMILY(LFACE) AREF ((LFACE), LFACE_FAMILY_INDEX)
1964 #define LFACE_HEIGHT(LFACE) AREF ((LFACE), LFACE_HEIGHT_INDEX)
1965 #define LFACE_WEIGHT(LFACE) AREF ((LFACE), LFACE_WEIGHT_INDEX)
1966 #define LFACE_SLANT(LFACE) AREF ((LFACE), LFACE_SLANT_INDEX)
1967 #define LFACE_UNDERLINE(LFACE) AREF ((LFACE), LFACE_UNDERLINE_INDEX)
1968 #define LFACE_INVERSE(LFACE) AREF ((LFACE), LFACE_INVERSE_INDEX)
1969 #define LFACE_FOREGROUND(LFACE) AREF ((LFACE), LFACE_FOREGROUND_INDEX)
1970 #define LFACE_BACKGROUND(LFACE) AREF ((LFACE), LFACE_BACKGROUND_INDEX)
1971 #define LFACE_STIPPLE(LFACE) AREF ((LFACE), LFACE_STIPPLE_INDEX)
1972 #define LFACE_SWIDTH(LFACE) AREF ((LFACE), LFACE_SWIDTH_INDEX)
1973 #define LFACE_OVERLINE(LFACE) AREF ((LFACE), LFACE_OVERLINE_INDEX)
1974 #define LFACE_STRIKE_THROUGH(LFACE) AREF ((LFACE), LFACE_STRIKE_THROUGH_INDEX)
1975 #define LFACE_BOX(LFACE) AREF ((LFACE), LFACE_BOX_INDEX)
1976 #define LFACE_FONT(LFACE) AREF ((LFACE), LFACE_FONT_INDEX)
1977 #define LFACE_INHERIT(LFACE) AREF ((LFACE), LFACE_INHERIT_INDEX)
1978 #define LFACE_FONTSET(LFACE) AREF ((LFACE), LFACE_FONTSET_INDEX)
1980 /* Non-zero if LFACE is a Lisp face. A Lisp face is a vector of size
1981 LFACE_VECTOR_SIZE which has the symbol `face' in slot 0. */
1983 #define LFACEP(LFACE) \
1984 (VECTORP (LFACE) \
1985 && XVECTOR (LFACE)->size == LFACE_VECTOR_SIZE \
1986 && EQ (AREF (LFACE, 0), Qface))
1989 #if GLYPH_DEBUG
1991 /* Check consistency of Lisp face attribute vector ATTRS. */
1993 static void
1994 check_lface_attrs (attrs)
1995 Lisp_Object *attrs;
1997 xassert (UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
1998 || IGNORE_DEFFACE_P (attrs[LFACE_FAMILY_INDEX])
1999 || STRINGP (attrs[LFACE_FAMILY_INDEX]));
2000 xassert (UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
2001 || IGNORE_DEFFACE_P (attrs[LFACE_SWIDTH_INDEX])
2002 || SYMBOLP (attrs[LFACE_SWIDTH_INDEX]));
2003 xassert (UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
2004 || IGNORE_DEFFACE_P (attrs[LFACE_HEIGHT_INDEX])
2005 || INTEGERP (attrs[LFACE_HEIGHT_INDEX])
2006 || FLOATP (attrs[LFACE_HEIGHT_INDEX])
2007 || FUNCTIONP (attrs[LFACE_HEIGHT_INDEX]));
2008 xassert (UNSPECIFIEDP (attrs[LFACE_WEIGHT_INDEX])
2009 || IGNORE_DEFFACE_P (attrs[LFACE_WEIGHT_INDEX])
2010 || SYMBOLP (attrs[LFACE_WEIGHT_INDEX]));
2011 xassert (UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX])
2012 || IGNORE_DEFFACE_P (attrs[LFACE_SLANT_INDEX])
2013 || SYMBOLP (attrs[LFACE_SLANT_INDEX]));
2014 xassert (UNSPECIFIEDP (attrs[LFACE_UNDERLINE_INDEX])
2015 || IGNORE_DEFFACE_P (attrs[LFACE_UNDERLINE_INDEX])
2016 || SYMBOLP (attrs[LFACE_UNDERLINE_INDEX])
2017 || STRINGP (attrs[LFACE_UNDERLINE_INDEX]));
2018 xassert (UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
2019 || IGNORE_DEFFACE_P (attrs[LFACE_OVERLINE_INDEX])
2020 || SYMBOLP (attrs[LFACE_OVERLINE_INDEX])
2021 || STRINGP (attrs[LFACE_OVERLINE_INDEX]));
2022 xassert (UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
2023 || IGNORE_DEFFACE_P (attrs[LFACE_STRIKE_THROUGH_INDEX])
2024 || SYMBOLP (attrs[LFACE_STRIKE_THROUGH_INDEX])
2025 || STRINGP (attrs[LFACE_STRIKE_THROUGH_INDEX]));
2026 xassert (UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
2027 || IGNORE_DEFFACE_P (attrs[LFACE_BOX_INDEX])
2028 || SYMBOLP (attrs[LFACE_BOX_INDEX])
2029 || STRINGP (attrs[LFACE_BOX_INDEX])
2030 || INTEGERP (attrs[LFACE_BOX_INDEX])
2031 || CONSP (attrs[LFACE_BOX_INDEX]));
2032 xassert (UNSPECIFIEDP (attrs[LFACE_INVERSE_INDEX])
2033 || IGNORE_DEFFACE_P (attrs[LFACE_INVERSE_INDEX])
2034 || SYMBOLP (attrs[LFACE_INVERSE_INDEX]));
2035 xassert (UNSPECIFIEDP (attrs[LFACE_FOREGROUND_INDEX])
2036 || IGNORE_DEFFACE_P (attrs[LFACE_FOREGROUND_INDEX])
2037 || STRINGP (attrs[LFACE_FOREGROUND_INDEX]));
2038 xassert (UNSPECIFIEDP (attrs[LFACE_BACKGROUND_INDEX])
2039 || IGNORE_DEFFACE_P (attrs[LFACE_BACKGROUND_INDEX])
2040 || STRINGP (attrs[LFACE_BACKGROUND_INDEX]));
2041 xassert (UNSPECIFIEDP (attrs[LFACE_INHERIT_INDEX])
2042 || IGNORE_DEFFACE_P (attrs[LFACE_INHERIT_INDEX])
2043 || NILP (attrs[LFACE_INHERIT_INDEX])
2044 || SYMBOLP (attrs[LFACE_INHERIT_INDEX])
2045 || CONSP (attrs[LFACE_INHERIT_INDEX]));
2046 #ifdef HAVE_WINDOW_SYSTEM
2047 xassert (UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
2048 || IGNORE_DEFFACE_P (attrs[LFACE_STIPPLE_INDEX])
2049 || SYMBOLP (attrs[LFACE_STIPPLE_INDEX])
2050 || !NILP (Fbitmap_spec_p (attrs[LFACE_STIPPLE_INDEX])));
2051 xassert (UNSPECIFIEDP (attrs[LFACE_FONT_INDEX])
2052 || IGNORE_DEFFACE_P (attrs[LFACE_FONT_INDEX])
2053 || FONTP (attrs[LFACE_FONT_INDEX]));
2054 xassert (UNSPECIFIEDP (attrs[LFACE_FONTSET_INDEX])
2055 || STRINGP (attrs[LFACE_FONTSET_INDEX]));
2056 #endif
2060 /* Check consistency of attributes of Lisp face LFACE (a Lisp vector). */
2062 static void
2063 check_lface (lface)
2064 Lisp_Object lface;
2066 if (!NILP (lface))
2068 xassert (LFACEP (lface));
2069 check_lface_attrs (XVECTOR (lface)->contents);
2073 #else /* GLYPH_DEBUG == 0 */
2075 #define check_lface_attrs(attrs) (void) 0
2076 #define check_lface(lface) (void) 0
2078 #endif /* GLYPH_DEBUG == 0 */
2082 /* Face-merge cycle checking. */
2084 enum named_merge_point_kind
2086 NAMED_MERGE_POINT_NORMAL,
2087 NAMED_MERGE_POINT_REMAP
2090 /* A `named merge point' is simply a point during face-merging where we
2091 look up a face by name. We keep a stack of which named lookups we're
2092 currently processing so that we can easily detect cycles, using a
2093 linked- list of struct named_merge_point structures, typically
2094 allocated on the stack frame of the named lookup functions which are
2095 active (so no consing is required). */
2096 struct named_merge_point
2098 Lisp_Object face_name;
2099 enum named_merge_point_kind named_merge_point_kind;
2100 struct named_merge_point *prev;
2104 /* If a face merging cycle is detected for FACE_NAME, return 0,
2105 otherwise add NEW_NAMED_MERGE_POINT, which is initialized using
2106 FACE_NAME and NAMED_MERGE_POINT_KIND, as the head of the linked list
2107 pointed to by NAMED_MERGE_POINTS, and return 1. */
2109 static INLINE int
2110 push_named_merge_point (struct named_merge_point *new_named_merge_point,
2111 Lisp_Object face_name,
2112 enum named_merge_point_kind named_merge_point_kind,
2113 struct named_merge_point **named_merge_points)
2115 struct named_merge_point *prev;
2117 for (prev = *named_merge_points; prev; prev = prev->prev)
2118 if (EQ (face_name, prev->face_name))
2120 if (prev->named_merge_point_kind == named_merge_point_kind)
2121 /* A cycle, so fail. */
2122 return 0;
2123 else if (prev->named_merge_point_kind == NAMED_MERGE_POINT_REMAP)
2124 /* A remap `hides ' any previous normal merge points
2125 (because the remap means that it's actually different face),
2126 so as we know the current merge point must be normal, we
2127 can just assume it's OK. */
2128 break;
2131 new_named_merge_point->face_name = face_name;
2132 new_named_merge_point->named_merge_point_kind = named_merge_point_kind;
2133 new_named_merge_point->prev = *named_merge_points;
2135 *named_merge_points = new_named_merge_point;
2137 return 1;
2142 #if 0 /* Seems to be unused. */
2143 static Lisp_Object
2144 internal_resolve_face_name (nargs, args)
2145 int nargs;
2146 Lisp_Object *args;
2148 return Fget (args[0], args[1]);
2151 static Lisp_Object
2152 resolve_face_name_error (ignore)
2153 Lisp_Object ignore;
2155 return Qnil;
2157 #endif
2159 /* Resolve face name FACE_NAME. If FACE_NAME is a string, intern it
2160 to make it a symbol. If FACE_NAME is an alias for another face,
2161 return that face's name.
2163 Return default face in case of errors. */
2165 static Lisp_Object
2166 resolve_face_name (face_name, signal_p)
2167 Lisp_Object face_name;
2168 int signal_p;
2170 Lisp_Object orig_face;
2171 Lisp_Object tortoise, hare;
2173 if (STRINGP (face_name))
2174 face_name = intern (SDATA (face_name));
2176 if (NILP (face_name) || !SYMBOLP (face_name))
2177 return face_name;
2179 orig_face = face_name;
2180 tortoise = hare = face_name;
2182 while (1)
2184 face_name = hare;
2185 hare = Fget (hare, Qface_alias);
2186 if (NILP (hare) || !SYMBOLP (hare))
2187 break;
2189 face_name = hare;
2190 hare = Fget (hare, Qface_alias);
2191 if (NILP (hare) || !SYMBOLP (hare))
2192 break;
2194 tortoise = Fget (tortoise, Qface_alias);
2195 if (EQ (hare, tortoise))
2197 if (signal_p)
2198 xsignal1 (Qcircular_list, orig_face);
2199 return Qdefault;
2203 return face_name;
2207 /* Return the face definition of FACE_NAME on frame F. F null means
2208 return the definition for new frames. FACE_NAME may be a string or
2209 a symbol (apparently Emacs 20.2 allowed strings as face names in
2210 face text properties; Ediff uses that). If SIGNAL_P is non-zero,
2211 signal an error if FACE_NAME is not a valid face name. If SIGNAL_P
2212 is zero, value is nil if FACE_NAME is not a valid face name. */
2213 static INLINE Lisp_Object
2214 lface_from_face_name_no_resolve (f, face_name, signal_p)
2215 struct frame *f;
2216 Lisp_Object face_name;
2217 int signal_p;
2219 Lisp_Object lface;
2221 if (f)
2222 lface = assq_no_quit (face_name, f->face_alist);
2223 else
2224 lface = assq_no_quit (face_name, Vface_new_frame_defaults);
2226 if (CONSP (lface))
2227 lface = XCDR (lface);
2228 else if (signal_p)
2229 signal_error ("Invalid face", face_name);
2231 check_lface (lface);
2233 return lface;
2236 /* Return the face definition of FACE_NAME on frame F. F null means
2237 return the definition for new frames. FACE_NAME may be a string or
2238 a symbol (apparently Emacs 20.2 allowed strings as face names in
2239 face text properties; Ediff uses that). If FACE_NAME is an alias
2240 for another face, return that face's definition. If SIGNAL_P is
2241 non-zero, signal an error if FACE_NAME is not a valid face name.
2242 If SIGNAL_P is zero, value is nil if FACE_NAME is not a valid face
2243 name. */
2244 static INLINE Lisp_Object
2245 lface_from_face_name (f, face_name, signal_p)
2246 struct frame *f;
2247 Lisp_Object face_name;
2248 int signal_p;
2250 face_name = resolve_face_name (face_name, signal_p);
2251 return lface_from_face_name_no_resolve (f, face_name, signal_p);
2255 /* Get face attributes of face FACE_NAME from frame-local faces on
2256 frame F. Store the resulting attributes in ATTRS which must point
2257 to a vector of Lisp_Objects of size LFACE_VECTOR_SIZE. If SIGNAL_P
2258 is non-zero, signal an error if FACE_NAME does not name a face.
2259 Otherwise, value is zero if FACE_NAME is not a face. */
2261 static INLINE int
2262 get_lface_attributes_no_remap (f, face_name, attrs, signal_p)
2263 struct frame *f;
2264 Lisp_Object face_name;
2265 Lisp_Object *attrs;
2266 int signal_p;
2268 Lisp_Object lface;
2270 lface = lface_from_face_name_no_resolve (f, face_name, signal_p);
2272 if (! NILP (lface))
2273 bcopy (XVECTOR (lface)->contents, attrs,
2274 LFACE_VECTOR_SIZE * sizeof *attrs);
2276 return !NILP (lface);
2279 /* Get face attributes of face FACE_NAME from frame-local faces on frame
2280 F. Store the resulting attributes in ATTRS which must point to a
2281 vector of Lisp_Objects of size LFACE_VECTOR_SIZE. If FACE_NAME is an
2282 alias for another face, use that face's definition. If SIGNAL_P is
2283 non-zero, signal an error if FACE_NAME does not name a face.
2284 Otherwise, value is zero if FACE_NAME is not a face. */
2286 static INLINE int
2287 get_lface_attributes (f, face_name, attrs, signal_p, named_merge_points)
2288 struct frame *f;
2289 Lisp_Object face_name;
2290 Lisp_Object *attrs;
2291 int signal_p;
2292 struct named_merge_point *named_merge_points;
2294 Lisp_Object face_remapping;
2296 face_name = resolve_face_name (face_name, signal_p);
2298 /* See if SYMBOL has been remapped to some other face (usually this
2299 is done buffer-locally). */
2300 face_remapping = assq_no_quit (face_name, Vface_remapping_alist);
2301 if (CONSP (face_remapping))
2303 struct named_merge_point named_merge_point;
2305 if (push_named_merge_point (&named_merge_point,
2306 face_name, NAMED_MERGE_POINT_REMAP,
2307 &named_merge_points))
2309 int i;
2311 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2312 attrs[i] = Qunspecified;
2314 return merge_face_ref (f, XCDR (face_remapping), attrs,
2315 signal_p, named_merge_points);
2319 /* Default case, no remapping. */
2320 return get_lface_attributes_no_remap (f, face_name, attrs, signal_p);
2324 /* Non-zero if all attributes in face attribute vector ATTRS are
2325 specified, i.e. are non-nil. */
2327 static int
2328 lface_fully_specified_p (attrs)
2329 Lisp_Object *attrs;
2331 int i;
2333 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2334 if (i != LFACE_FONT_INDEX && i != LFACE_INHERIT_INDEX)
2335 if ((UNSPECIFIEDP (attrs[i]) || IGNORE_DEFFACE_P (attrs[i]))
2336 #ifdef MAC_OS
2337 /* MAC_TODO: No stipple support on Mac OS yet, this index is
2338 always unspecified. */
2339 && i != LFACE_STIPPLE_INDEX
2340 #endif
2342 break;
2344 return i == LFACE_VECTOR_SIZE;
2347 #ifdef HAVE_WINDOW_SYSTEM
2349 /* Set font-related attributes of Lisp face LFACE from FONT-OBJECT.
2350 If FORCE_P is zero, set only unspecified attributes of LFACE. The
2351 exception is `font' attribute. It is set to FONT_OBJECT regardless
2352 of FORCE_P. */
2354 static int
2355 set_lface_from_font (f, lface, font_object, force_p)
2356 struct frame *f;
2357 Lisp_Object lface, font_object;
2358 int force_p;
2360 Lisp_Object val;
2361 struct font *font = XFONT_OBJECT (font_object);
2363 /* Set attributes only if unspecified, otherwise face defaults for
2364 new frames would never take effect. If the font doesn't have a
2365 specific property, set a normal value for that. */
2367 if (force_p || UNSPECIFIEDP (LFACE_FAMILY (lface)))
2369 Lisp_Object foundry = AREF (font_object, FONT_FOUNDRY_INDEX);
2370 Lisp_Object family = AREF (font_object, FONT_FAMILY_INDEX);
2372 if (! NILP (foundry))
2374 if (! NILP (family))
2375 val = concat3 (SYMBOL_NAME (foundry), build_string ("-"),
2376 SYMBOL_NAME (family));
2377 else
2378 val = concat2 (SYMBOL_NAME (foundry), build_string ("-*"));
2380 else
2382 if (! NILP (family))
2383 val = SYMBOL_NAME (family);
2384 else
2385 val = build_string ("*");
2387 LFACE_FAMILY (lface) = val;
2390 if (force_p || UNSPECIFIEDP (LFACE_HEIGHT (lface)))
2392 int pt = PIXEL_TO_POINT (font->pixel_size * 10, f->resy);
2394 xassert (pt > 0);
2395 LFACE_HEIGHT (lface) = make_number (pt);
2398 if (force_p || UNSPECIFIEDP (LFACE_WEIGHT (lface)))
2400 val = FONT_WEIGHT_FOR_FACE (font_object);
2401 LFACE_WEIGHT (lface) = ! NILP (val) ? val :Qnormal;
2403 if (force_p || UNSPECIFIEDP (LFACE_SLANT (lface)))
2405 val = FONT_SLANT_FOR_FACE (font_object);
2406 LFACE_SLANT (lface) = ! NILP (val) ? val : Qnormal;
2408 if (force_p || UNSPECIFIEDP (LFACE_SWIDTH (lface)))
2410 val = FONT_WIDTH_FOR_FACE (font_object);
2411 LFACE_SWIDTH (lface) = ! NILP (val) ? val : Qnormal;
2414 LFACE_FONT (lface) = font_object;
2415 return 1;
2418 #endif /* HAVE_WINDOW_SYSTEM */
2421 /* Merges the face height FROM with the face height TO, and returns the
2422 merged height. If FROM is an invalid height, then INVALID is
2423 returned instead. FROM and TO may be either absolute face heights or
2424 `relative' heights; the returned value is always an absolute height
2425 unless both FROM and TO are relative. GCPRO is a lisp value that
2426 will be protected from garbage-collection if this function makes a
2427 call into lisp. */
2429 Lisp_Object
2430 merge_face_heights (from, to, invalid)
2431 Lisp_Object from, to, invalid;
2433 Lisp_Object result = invalid;
2435 if (INTEGERP (from))
2436 /* FROM is absolute, just use it as is. */
2437 result = from;
2438 else if (FLOATP (from))
2439 /* FROM is a scale, use it to adjust TO. */
2441 if (INTEGERP (to))
2442 /* relative X absolute => absolute */
2443 result = make_number ((EMACS_INT)(XFLOAT_DATA (from) * XINT (to)));
2444 else if (FLOATP (to))
2445 /* relative X relative => relative */
2446 result = make_float (XFLOAT_DATA (from) * XFLOAT_DATA (to));
2447 else if (UNSPECIFIEDP (to))
2448 result = from;
2450 else if (FUNCTIONP (from))
2451 /* FROM is a function, which use to adjust TO. */
2453 /* Call function with current height as argument.
2454 From is the new height. */
2455 Lisp_Object args[2];
2457 args[0] = from;
2458 args[1] = to;
2459 result = safe_call (2, args);
2461 /* Ensure that if TO was absolute, so is the result. */
2462 if (INTEGERP (to) && !INTEGERP (result))
2463 result = invalid;
2466 return result;
2470 /* Merge two Lisp face attribute vectors on frame F, FROM and TO, and
2471 store the resulting attributes in TO, which must be already be
2472 completely specified and contain only absolute attributes. Every
2473 specified attribute of FROM overrides the corresponding attribute of
2474 TO; relative attributes in FROM are merged with the absolute value in
2475 TO and replace it. NAMED_MERGE_POINTS is used internally to detect
2476 loops in face inheritance/remapping; it should be 0 when called from
2477 other places. */
2479 static INLINE void
2480 merge_face_vectors (f, from, to, named_merge_points)
2481 struct frame *f;
2482 Lisp_Object *from, *to;
2483 struct named_merge_point *named_merge_points;
2485 int i;
2487 /* If FROM inherits from some other faces, merge their attributes into
2488 TO before merging FROM's direct attributes. Note that an :inherit
2489 attribute of `unspecified' is the same as one of nil; we never
2490 merge :inherit attributes, so nil is more correct, but lots of
2491 other code uses `unspecified' as a generic value for face attributes. */
2492 if (!UNSPECIFIEDP (from[LFACE_INHERIT_INDEX])
2493 && !NILP (from[LFACE_INHERIT_INDEX]))
2494 merge_face_ref (f, from[LFACE_INHERIT_INDEX], to, 0, named_merge_points);
2496 i = LFACE_FONT_INDEX;
2497 if (!UNSPECIFIEDP (from[i]))
2499 if (!UNSPECIFIEDP (to[i]))
2500 to[i] = Fmerge_font_spec (from[i], to[i]);
2501 else
2502 to[i] = Fcopy_font_spec (from[i]);
2503 ASET (to[i], FONT_SIZE_INDEX, Qnil);
2506 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2507 if (!UNSPECIFIEDP (from[i]))
2509 if (i == LFACE_HEIGHT_INDEX && !INTEGERP (from[i]))
2511 to[i] = merge_face_heights (from[i], to[i], to[i]);
2512 font_clear_prop (to, FONT_SIZE_INDEX);
2514 else if (i != LFACE_FONT_INDEX)
2516 to[i] = from[i];
2517 if (i >= LFACE_FAMILY_INDEX && i <=LFACE_SLANT_INDEX)
2518 font_clear_prop (to,
2519 (i == LFACE_FAMILY_INDEX ? FONT_FAMILY_INDEX
2520 : i == LFACE_SWIDTH_INDEX ? FONT_WIDTH_INDEX
2521 : i == LFACE_HEIGHT_INDEX ? FONT_SIZE_INDEX
2522 : i == LFACE_WEIGHT_INDEX ? FONT_WEIGHT_INDEX
2523 : FONT_SLANT_INDEX));
2527 /* If `font' attribute is specified, reflect the font properties in
2528 it to the other attributes. */
2529 if (0 && !UNSPECIFIEDP (to[LFACE_FONT_INDEX]))
2530 font_update_lface (f, to);
2532 /* TO is always an absolute face, which should inherit from nothing.
2533 We blindly copy the :inherit attribute above and fix it up here. */
2534 to[LFACE_INHERIT_INDEX] = Qnil;
2537 /* Merge the named face FACE_NAME on frame F, into the vector of face
2538 attributes TO. NAMED_MERGE_POINTS is used to detect loops in face
2539 inheritance. Returns true if FACE_NAME is a valid face name and
2540 merging succeeded. */
2542 static int
2543 merge_named_face (f, face_name, to, named_merge_points)
2544 struct frame *f;
2545 Lisp_Object face_name;
2546 Lisp_Object *to;
2547 struct named_merge_point *named_merge_points;
2549 struct named_merge_point named_merge_point;
2551 if (push_named_merge_point (&named_merge_point,
2552 face_name, NAMED_MERGE_POINT_NORMAL,
2553 &named_merge_points))
2555 struct gcpro gcpro1;
2556 Lisp_Object from[LFACE_VECTOR_SIZE];
2557 int ok = get_lface_attributes (f, face_name, from, 0, named_merge_points);
2559 if (ok)
2561 GCPRO1 (named_merge_point.face_name);
2562 merge_face_vectors (f, from, to, named_merge_points);
2563 UNGCPRO;
2566 return ok;
2568 else
2569 return 0;
2573 /* Merge face attributes from the lisp `face reference' FACE_REF on
2574 frame F into the face attribute vector TO. If ERR_MSGS is non-zero,
2575 problems with FACE_REF cause an error message to be shown. Return
2576 non-zero if no errors occurred (regardless of the value of ERR_MSGS).
2577 NAMED_MERGE_POINTS is used to detect loops in face inheritance or
2578 list structure; it may be 0 for most callers.
2580 FACE_REF may be a single face specification or a list of such
2581 specifications. Each face specification can be:
2583 1. A symbol or string naming a Lisp face.
2585 2. A property list of the form (KEYWORD VALUE ...) where each
2586 KEYWORD is a face attribute name, and value is an appropriate value
2587 for that attribute.
2589 3. Conses or the form (FOREGROUND-COLOR . COLOR) or
2590 (BACKGROUND-COLOR . COLOR) where COLOR is a color name. This is
2591 for compatibility with 20.2.
2593 Face specifications earlier in lists take precedence over later
2594 specifications. */
2596 static int
2597 merge_face_ref (f, face_ref, to, err_msgs, named_merge_points)
2598 struct frame *f;
2599 Lisp_Object face_ref;
2600 Lisp_Object *to;
2601 int err_msgs;
2602 struct named_merge_point *named_merge_points;
2604 int ok = 1; /* Succeed without an error? */
2606 if (CONSP (face_ref))
2608 Lisp_Object first = XCAR (face_ref);
2610 if (EQ (first, Qforeground_color)
2611 || EQ (first, Qbackground_color))
2613 /* One of (FOREGROUND-COLOR . COLOR) or (BACKGROUND-COLOR
2614 . COLOR). COLOR must be a string. */
2615 Lisp_Object color_name = XCDR (face_ref);
2616 Lisp_Object color = first;
2618 if (STRINGP (color_name))
2620 if (EQ (color, Qforeground_color))
2621 to[LFACE_FOREGROUND_INDEX] = color_name;
2622 else
2623 to[LFACE_BACKGROUND_INDEX] = color_name;
2625 else
2627 if (err_msgs)
2628 add_to_log ("Invalid face color", color_name, Qnil);
2629 ok = 0;
2632 else if (SYMBOLP (first)
2633 && *SDATA (SYMBOL_NAME (first)) == ':')
2635 /* Assume this is the property list form. */
2636 while (CONSP (face_ref) && CONSP (XCDR (face_ref)))
2638 Lisp_Object keyword = XCAR (face_ref);
2639 Lisp_Object value = XCAR (XCDR (face_ref));
2640 int err = 0;
2642 /* Specifying `unspecified' is a no-op. */
2643 if (EQ (value, Qunspecified))
2645 else if (EQ (keyword, QCfamily))
2647 if (STRINGP (value))
2649 to[LFACE_FAMILY_INDEX] = value;
2650 font_clear_prop (to, FONT_FAMILY_INDEX);
2652 else
2653 err = 1;
2655 else if (EQ (keyword, QCheight))
2657 Lisp_Object new_height =
2658 merge_face_heights (value, to[LFACE_HEIGHT_INDEX], Qnil);
2660 if (! NILP (new_height))
2662 to[LFACE_HEIGHT_INDEX] = new_height;
2663 font_clear_prop (to, FONT_SIZE_INDEX);
2665 else
2666 err = 1;
2668 else if (EQ (keyword, QCweight))
2670 if (SYMBOLP (value) && FONT_WEIGHT_NAME_NUMERIC (value) >= 0)
2672 to[LFACE_WEIGHT_INDEX] = value;
2673 font_clear_prop (to, FONT_WEIGHT_INDEX);
2675 else
2676 err = 1;
2678 else if (EQ (keyword, QCslant))
2680 if (SYMBOLP (value) && FONT_SLANT_NAME_NUMERIC (value) >= 0)
2682 to[LFACE_SLANT_INDEX] = value;
2683 font_clear_prop (to, FONT_SLANT_INDEX);
2685 else
2686 err = 1;
2688 else if (EQ (keyword, QCunderline))
2690 if (EQ (value, Qt)
2691 || NILP (value)
2692 || STRINGP (value))
2693 to[LFACE_UNDERLINE_INDEX] = value;
2694 else
2695 err = 1;
2697 else if (EQ (keyword, QCoverline))
2699 if (EQ (value, Qt)
2700 || NILP (value)
2701 || STRINGP (value))
2702 to[LFACE_OVERLINE_INDEX] = value;
2703 else
2704 err = 1;
2706 else if (EQ (keyword, QCstrike_through))
2708 if (EQ (value, Qt)
2709 || NILP (value)
2710 || STRINGP (value))
2711 to[LFACE_STRIKE_THROUGH_INDEX] = value;
2712 else
2713 err = 1;
2715 else if (EQ (keyword, QCbox))
2717 if (EQ (value, Qt))
2718 value = make_number (1);
2719 if (INTEGERP (value)
2720 || STRINGP (value)
2721 || CONSP (value)
2722 || NILP (value))
2723 to[LFACE_BOX_INDEX] = value;
2724 else
2725 err = 1;
2727 else if (EQ (keyword, QCinverse_video)
2728 || EQ (keyword, QCreverse_video))
2730 if (EQ (value, Qt) || NILP (value))
2731 to[LFACE_INVERSE_INDEX] = value;
2732 else
2733 err = 1;
2735 else if (EQ (keyword, QCforeground))
2737 if (STRINGP (value))
2738 to[LFACE_FOREGROUND_INDEX] = value;
2739 else
2740 err = 1;
2742 else if (EQ (keyword, QCbackground))
2744 if (STRINGP (value))
2745 to[LFACE_BACKGROUND_INDEX] = value;
2746 else
2747 err = 1;
2749 else if (EQ (keyword, QCstipple))
2751 #ifdef HAVE_X_WINDOWS
2752 Lisp_Object pixmap_p = Fbitmap_spec_p (value);
2753 if (!NILP (pixmap_p))
2754 to[LFACE_STIPPLE_INDEX] = value;
2755 else
2756 err = 1;
2757 #endif
2759 else if (EQ (keyword, QCwidth))
2761 if (SYMBOLP (value) && FONT_WIDTH_NAME_NUMERIC (value) >= 0)
2763 to[LFACE_SWIDTH_INDEX] = value;
2764 font_clear_prop (to, FONT_WIDTH_INDEX);
2766 else
2767 err = 1;
2769 else if (EQ (keyword, QCinherit))
2771 /* This is not really very useful; it's just like a
2772 normal face reference. */
2773 if (! merge_face_ref (f, value, to,
2774 err_msgs, named_merge_points))
2775 err = 1;
2777 else
2778 err = 1;
2780 if (err)
2782 add_to_log ("Invalid face attribute %S %S", keyword, value);
2783 ok = 0;
2786 face_ref = XCDR (XCDR (face_ref));
2789 else
2791 /* This is a list of face refs. Those at the beginning of the
2792 list take precedence over what follows, so we have to merge
2793 from the end backwards. */
2794 Lisp_Object next = XCDR (face_ref);
2796 if (! NILP (next))
2797 ok = merge_face_ref (f, next, to, err_msgs, named_merge_points);
2799 if (! merge_face_ref (f, first, to, err_msgs, named_merge_points))
2800 ok = 0;
2803 else
2805 /* FACE_REF ought to be a face name. */
2806 ok = merge_named_face (f, face_ref, to, named_merge_points);
2807 if (!ok && err_msgs)
2808 add_to_log ("Invalid face reference: %s", face_ref, Qnil);
2811 return ok;
2815 DEFUN ("internal-make-lisp-face", Finternal_make_lisp_face,
2816 Sinternal_make_lisp_face, 1, 2, 0,
2817 doc: /* Make FACE, a symbol, a Lisp face with all attributes nil.
2818 If FACE was not known as a face before, create a new one.
2819 If optional argument FRAME is specified, make a frame-local face
2820 for that frame. Otherwise operate on the global face definition.
2821 Value is a vector of face attributes. */)
2822 (face, frame)
2823 Lisp_Object face, frame;
2825 Lisp_Object global_lface, lface;
2826 struct frame *f;
2827 int i;
2829 CHECK_SYMBOL (face);
2830 global_lface = lface_from_face_name (NULL, face, 0);
2832 if (!NILP (frame))
2834 CHECK_LIVE_FRAME (frame);
2835 f = XFRAME (frame);
2836 lface = lface_from_face_name (f, face, 0);
2838 else
2839 f = NULL, lface = Qnil;
2841 /* Add a global definition if there is none. */
2842 if (NILP (global_lface))
2844 global_lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
2845 Qunspecified);
2846 ASET (global_lface, 0, Qface);
2847 Vface_new_frame_defaults = Fcons (Fcons (face, global_lface),
2848 Vface_new_frame_defaults);
2850 /* Assign the new Lisp face a unique ID. The mapping from Lisp
2851 face id to Lisp face is given by the vector lface_id_to_name.
2852 The mapping from Lisp face to Lisp face id is given by the
2853 property `face' of the Lisp face name. */
2854 if (next_lface_id == lface_id_to_name_size)
2856 int new_size = max (50, 2 * lface_id_to_name_size);
2857 int sz = new_size * sizeof *lface_id_to_name;
2858 lface_id_to_name = (Lisp_Object *) xrealloc (lface_id_to_name, sz);
2859 lface_id_to_name_size = new_size;
2862 lface_id_to_name[next_lface_id] = face;
2863 Fput (face, Qface, make_number (next_lface_id));
2864 ++next_lface_id;
2866 else if (f == NULL)
2867 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2868 ASET (global_lface, i, Qunspecified);
2870 /* Add a frame-local definition. */
2871 if (f)
2873 if (NILP (lface))
2875 lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
2876 Qunspecified);
2877 ASET (lface, 0, Qface);
2878 f->face_alist = Fcons (Fcons (face, lface), f->face_alist);
2880 else
2881 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2882 ASET (lface, i, Qunspecified);
2884 else
2885 lface = global_lface;
2887 /* Changing a named face means that all realized faces depending on
2888 that face are invalid. Since we cannot tell which realized faces
2889 depend on the face, make sure they are all removed. This is done
2890 by incrementing face_change_count. The next call to
2891 init_iterator will then free realized faces. */
2892 if (NILP (Fget (face, Qface_no_inherit)))
2894 ++face_change_count;
2895 ++windows_or_buffers_changed;
2898 xassert (LFACEP (lface));
2899 check_lface (lface);
2900 return lface;
2904 DEFUN ("internal-lisp-face-p", Finternal_lisp_face_p,
2905 Sinternal_lisp_face_p, 1, 2, 0,
2906 doc: /* Return non-nil if FACE names a face.
2907 If optional second argument FRAME is non-nil, check for the
2908 existence of a frame-local face with name FACE on that frame.
2909 Otherwise check for the existence of a global face. */)
2910 (face, frame)
2911 Lisp_Object face, frame;
2913 Lisp_Object lface;
2915 face = resolve_face_name (face, 1);
2917 if (!NILP (frame))
2919 CHECK_LIVE_FRAME (frame);
2920 lface = lface_from_face_name (XFRAME (frame), face, 0);
2922 else
2923 lface = lface_from_face_name (NULL, face, 0);
2925 return lface;
2929 DEFUN ("internal-copy-lisp-face", Finternal_copy_lisp_face,
2930 Sinternal_copy_lisp_face, 4, 4, 0,
2931 doc: /* Copy face FROM to TO.
2932 If FRAME is t, copy the global face definition of FROM.
2933 Otherwise, copy the frame-local definition of FROM on FRAME.
2934 If NEW-FRAME is a frame, copy that data into the frame-local
2935 definition of TO on NEW-FRAME. If NEW-FRAME is nil,
2936 FRAME controls where the data is copied to.
2938 The value is TO. */)
2939 (from, to, frame, new_frame)
2940 Lisp_Object from, to, frame, new_frame;
2942 Lisp_Object lface, copy;
2944 CHECK_SYMBOL (from);
2945 CHECK_SYMBOL (to);
2947 if (EQ (frame, Qt))
2949 /* Copy global definition of FROM. We don't make copies of
2950 strings etc. because 20.2 didn't do it either. */
2951 lface = lface_from_face_name (NULL, from, 1);
2952 copy = Finternal_make_lisp_face (to, Qnil);
2954 else
2956 /* Copy frame-local definition of FROM. */
2957 if (NILP (new_frame))
2958 new_frame = frame;
2959 CHECK_LIVE_FRAME (frame);
2960 CHECK_LIVE_FRAME (new_frame);
2961 lface = lface_from_face_name (XFRAME (frame), from, 1);
2962 copy = Finternal_make_lisp_face (to, new_frame);
2965 bcopy (XVECTOR (lface)->contents, XVECTOR (copy)->contents,
2966 LFACE_VECTOR_SIZE * sizeof (Lisp_Object));
2968 /* Changing a named face means that all realized faces depending on
2969 that face are invalid. Since we cannot tell which realized faces
2970 depend on the face, make sure they are all removed. This is done
2971 by incrementing face_change_count. The next call to
2972 init_iterator will then free realized faces. */
2973 if (NILP (Fget (to, Qface_no_inherit)))
2975 ++face_change_count;
2976 ++windows_or_buffers_changed;
2979 return to;
2983 DEFUN ("internal-set-lisp-face-attribute", Finternal_set_lisp_face_attribute,
2984 Sinternal_set_lisp_face_attribute, 3, 4, 0,
2985 doc: /* Set attribute ATTR of FACE to VALUE.
2986 FRAME being a frame means change the face on that frame.
2987 FRAME nil means change the face of the selected frame.
2988 FRAME t means change the default for new frames.
2989 FRAME 0 means change the face on all frames, and change the default
2990 for new frames. */)
2991 (face, attr, value, frame)
2992 Lisp_Object face, attr, value, frame;
2994 Lisp_Object lface;
2995 Lisp_Object old_value = Qnil;
2996 /* Set one of enum font_property_index (> 0) if ATTR is one of
2997 font-related attributes other than QCfont and QCfontset. */
2998 enum font_property_index prop_index = 0;
3000 CHECK_SYMBOL (face);
3001 CHECK_SYMBOL (attr);
3003 face = resolve_face_name (face, 1);
3005 /* If FRAME is 0, change face on all frames, and change the
3006 default for new frames. */
3007 if (INTEGERP (frame) && XINT (frame) == 0)
3009 Lisp_Object tail;
3010 Finternal_set_lisp_face_attribute (face, attr, value, Qt);
3011 FOR_EACH_FRAME (tail, frame)
3012 Finternal_set_lisp_face_attribute (face, attr, value, frame);
3013 return face;
3016 /* Set lface to the Lisp attribute vector of FACE. */
3017 if (EQ (frame, Qt))
3019 lface = lface_from_face_name (NULL, face, 1);
3021 /* When updating face-new-frame-defaults, we put :ignore-defface
3022 where the caller wants `unspecified'. This forces the frame
3023 defaults to ignore the defface value. Otherwise, the defface
3024 will take effect, which is generally not what is intended.
3025 The value of that attribute will be inherited from some other
3026 face during face merging. See internal_merge_in_global_face. */
3027 if (UNSPECIFIEDP (value))
3028 value = Qignore_defface;
3030 else
3032 if (NILP (frame))
3033 frame = selected_frame;
3035 CHECK_LIVE_FRAME (frame);
3036 lface = lface_from_face_name (XFRAME (frame), face, 0);
3038 /* If a frame-local face doesn't exist yet, create one. */
3039 if (NILP (lface))
3040 lface = Finternal_make_lisp_face (face, frame);
3043 if (EQ (attr, QCfamily))
3045 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3047 CHECK_STRING (value);
3048 if (SCHARS (value) == 0)
3049 signal_error ("Invalid face family", value);
3051 old_value = LFACE_FAMILY (lface);
3052 LFACE_FAMILY (lface) = value;
3053 prop_index = FONT_FAMILY_INDEX;
3055 else if (EQ (attr, QCheight))
3057 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3059 Lisp_Object test;
3061 test = (EQ (face, Qdefault)
3062 ? value
3063 /* The default face must have an absolute size,
3064 otherwise, we do a test merge with a random
3065 height to see if VALUE's ok. */
3066 : merge_face_heights (value, make_number (10), Qnil));
3068 if (!INTEGERP (test) || XINT (test) <= 0)
3069 signal_error ("Invalid face height", value);
3072 old_value = LFACE_HEIGHT (lface);
3073 LFACE_HEIGHT (lface) = value;
3074 prop_index = FONT_SIZE_INDEX;
3076 else if (EQ (attr, QCweight))
3078 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3080 CHECK_SYMBOL (value);
3081 if (FONT_WEIGHT_NAME_NUMERIC (value) < 0)
3082 signal_error ("Invalid face weight", value);
3084 old_value = LFACE_WEIGHT (lface);
3085 LFACE_WEIGHT (lface) = value;
3086 prop_index = FONT_WEIGHT_INDEX;
3088 else if (EQ (attr, QCslant))
3090 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3092 CHECK_SYMBOL (value);
3093 if (FONT_SLANT_NAME_NUMERIC (value) < 0)
3094 signal_error ("Invalid face slant", value);
3096 old_value = LFACE_SLANT (lface);
3097 LFACE_SLANT (lface) = value;
3098 prop_index = FONT_SLANT_INDEX;
3100 else if (EQ (attr, QCunderline))
3102 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3103 if ((SYMBOLP (value)
3104 && !EQ (value, Qt)
3105 && !EQ (value, Qnil))
3106 /* Underline color. */
3107 || (STRINGP (value)
3108 && SCHARS (value) == 0))
3109 signal_error ("Invalid face underline", value);
3111 old_value = LFACE_UNDERLINE (lface);
3112 LFACE_UNDERLINE (lface) = value;
3114 else if (EQ (attr, QCoverline))
3116 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3117 if ((SYMBOLP (value)
3118 && !EQ (value, Qt)
3119 && !EQ (value, Qnil))
3120 /* Overline color. */
3121 || (STRINGP (value)
3122 && SCHARS (value) == 0))
3123 signal_error ("Invalid face overline", value);
3125 old_value = LFACE_OVERLINE (lface);
3126 LFACE_OVERLINE (lface) = value;
3128 else if (EQ (attr, QCstrike_through))
3130 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3131 if ((SYMBOLP (value)
3132 && !EQ (value, Qt)
3133 && !EQ (value, Qnil))
3134 /* Strike-through color. */
3135 || (STRINGP (value)
3136 && SCHARS (value) == 0))
3137 signal_error ("Invalid face strike-through", value);
3139 old_value = LFACE_STRIKE_THROUGH (lface);
3140 LFACE_STRIKE_THROUGH (lface) = value;
3142 else if (EQ (attr, QCbox))
3144 int valid_p;
3146 /* Allow t meaning a simple box of width 1 in foreground color
3147 of the face. */
3148 if (EQ (value, Qt))
3149 value = make_number (1);
3151 if (UNSPECIFIEDP (value) || IGNORE_DEFFACE_P (value))
3152 valid_p = 1;
3153 else if (NILP (value))
3154 valid_p = 1;
3155 else if (INTEGERP (value))
3156 valid_p = XINT (value) != 0;
3157 else if (STRINGP (value))
3158 valid_p = SCHARS (value) > 0;
3159 else if (CONSP (value))
3161 Lisp_Object tem;
3163 tem = value;
3164 while (CONSP (tem))
3166 Lisp_Object k, v;
3168 k = XCAR (tem);
3169 tem = XCDR (tem);
3170 if (!CONSP (tem))
3171 break;
3172 v = XCAR (tem);
3173 tem = XCDR (tem);
3175 if (EQ (k, QCline_width))
3177 if (!INTEGERP (v) || XINT (v) == 0)
3178 break;
3180 else if (EQ (k, QCcolor))
3182 if (!NILP (v) && (!STRINGP (v) || SCHARS (v) == 0))
3183 break;
3185 else if (EQ (k, QCstyle))
3187 if (!EQ (v, Qpressed_button) && !EQ (v, Qreleased_button))
3188 break;
3190 else
3191 break;
3194 valid_p = NILP (tem);
3196 else
3197 valid_p = 0;
3199 if (!valid_p)
3200 signal_error ("Invalid face box", value);
3202 old_value = LFACE_BOX (lface);
3203 LFACE_BOX (lface) = value;
3205 else if (EQ (attr, QCinverse_video)
3206 || EQ (attr, QCreverse_video))
3208 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3210 CHECK_SYMBOL (value);
3211 if (!EQ (value, Qt) && !NILP (value))
3212 signal_error ("Invalid inverse-video face attribute value", value);
3214 old_value = LFACE_INVERSE (lface);
3215 LFACE_INVERSE (lface) = value;
3217 else if (EQ (attr, QCforeground))
3219 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3221 /* Don't check for valid color names here because it depends
3222 on the frame (display) whether the color will be valid
3223 when the face is realized. */
3224 CHECK_STRING (value);
3225 if (SCHARS (value) == 0)
3226 signal_error ("Empty foreground color value", value);
3228 old_value = LFACE_FOREGROUND (lface);
3229 LFACE_FOREGROUND (lface) = value;
3231 else if (EQ (attr, QCbackground))
3233 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3235 /* Don't check for valid color names here because it depends
3236 on the frame (display) whether the color will be valid
3237 when the face is realized. */
3238 CHECK_STRING (value);
3239 if (SCHARS (value) == 0)
3240 signal_error ("Empty background color value", value);
3242 old_value = LFACE_BACKGROUND (lface);
3243 LFACE_BACKGROUND (lface) = value;
3245 else if (EQ (attr, QCstipple))
3247 #ifdef HAVE_X_WINDOWS
3248 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value)
3249 && !NILP (value)
3250 && NILP (Fbitmap_spec_p (value)))
3251 signal_error ("Invalid stipple attribute", value);
3252 old_value = LFACE_STIPPLE (lface);
3253 LFACE_STIPPLE (lface) = value;
3254 #endif /* HAVE_X_WINDOWS */
3256 else if (EQ (attr, QCwidth))
3258 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3260 CHECK_SYMBOL (value);
3261 if (FONT_WIDTH_NAME_NUMERIC (value) < 0)
3262 signal_error ("Invalid face width", value);
3264 old_value = LFACE_SWIDTH (lface);
3265 LFACE_SWIDTH (lface) = value;
3266 prop_index = FONT_WIDTH_INDEX;
3268 else if (EQ (attr, QCfont))
3270 #ifdef HAVE_WINDOW_SYSTEM
3271 if (EQ (frame, Qt) || FRAME_WINDOW_P (XFRAME (frame)))
3273 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3275 FRAME_PTR f;
3277 old_value = LFACE_FONT (lface);
3278 if (! FONTP (value))
3280 if (STRINGP (value))
3282 int fontset = fs_query_fontset (value, 0);
3284 if (fontset >= 0)
3285 value = fontset_ascii (fontset);
3286 value = font_spec_from_name (value);
3288 else
3289 signal_error ("Invalid font or font-spec", value);
3291 if (EQ (frame, Qt))
3292 f = XFRAME (selected_frame);
3293 else
3294 f = XFRAME (frame);
3295 if (! FONT_OBJECT_P (value))
3297 Lisp_Object *attrs = XVECTOR (lface)->contents;
3298 Lisp_Object font_object;
3300 font_object = font_load_for_lface (f, attrs, value);
3301 if (NILP (font_object))
3302 signal_error ("Font not available", value);
3303 value = font_object;
3305 set_lface_from_font (f, lface, value, 1);
3307 else
3308 LFACE_FONT (lface) = value;
3310 #endif /* HAVE_WINDOW_SYSTEM */
3312 else if (EQ (attr, QCfontset))
3314 #ifdef HAVE_WINDOW_SYSTEM
3315 if (EQ (frame, Qt) || FRAME_WINDOW_P (XFRAME (frame)))
3317 Lisp_Object tmp;
3319 old_value = LFACE_FONTSET (lface);
3320 tmp = Fquery_fontset (value, Qnil);
3321 if (NILP (tmp))
3322 signal_error ("Invalid fontset name", value);
3323 LFACE_FONTSET (lface) = value = tmp;
3325 #endif /* HAVE_WINDOW_SYSTEM */
3327 else if (EQ (attr, QCinherit))
3329 Lisp_Object tail;
3330 if (SYMBOLP (value))
3331 tail = Qnil;
3332 else
3333 for (tail = value; CONSP (tail); tail = XCDR (tail))
3334 if (!SYMBOLP (XCAR (tail)))
3335 break;
3336 if (NILP (tail))
3337 LFACE_INHERIT (lface) = value;
3338 else
3339 signal_error ("Invalid face inheritance", value);
3341 else if (EQ (attr, QCbold))
3343 old_value = LFACE_WEIGHT (lface);
3344 LFACE_WEIGHT (lface) = NILP (value) ? Qnormal : Qbold;
3345 prop_index = FONT_WEIGHT_INDEX;
3347 else if (EQ (attr, QCitalic))
3349 attr = QCslant;
3350 old_value = LFACE_SLANT (lface);
3351 LFACE_SLANT (lface) = NILP (value) ? Qnormal : Qitalic;
3352 prop_index = FONT_SLANT_INDEX;
3354 else
3355 signal_error ("Invalid face attribute name", attr);
3357 if (prop_index)
3358 /* If a font-related attribute other than QCfont and QCfontset is
3359 specified, and if the original QCfont attribute has a font
3360 (font-spec or font-object), set the corresponding property in
3361 the font to nil so that the font selector doesn't think that
3362 the attribute is mandatory. */
3363 font_clear_prop (XVECTOR (lface)->contents, prop_index);
3365 /* Changing a named face means that all realized faces depending on
3366 that face are invalid. Since we cannot tell which realized faces
3367 depend on the face, make sure they are all removed. This is done
3368 by incrementing face_change_count. The next call to
3369 init_iterator will then free realized faces. */
3370 if (!EQ (frame, Qt)
3371 && NILP (Fget (face, Qface_no_inherit))
3372 && NILP (Fequal (old_value, value)))
3374 ++face_change_count;
3375 ++windows_or_buffers_changed;
3378 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value)
3379 && NILP (Fequal (old_value, value)))
3381 Lisp_Object param;
3383 param = Qnil;
3385 if (EQ (face, Qdefault))
3387 #ifdef HAVE_WINDOW_SYSTEM
3388 /* Changed font-related attributes of the `default' face are
3389 reflected in changed `font' frame parameters. */
3390 if (FRAMEP (frame)
3391 && (prop_index || EQ (attr, QCfont))
3392 && lface_fully_specified_p (XVECTOR (lface)->contents))
3393 set_font_frame_param (frame, lface);
3394 else
3395 #endif /* HAVE_WINDOW_SYSTEM */
3397 if (EQ (attr, QCforeground))
3398 param = Qforeground_color;
3399 else if (EQ (attr, QCbackground))
3400 param = Qbackground_color;
3402 #ifdef HAVE_WINDOW_SYSTEM
3403 #ifndef WINDOWSNT
3404 else if (EQ (face, Qscroll_bar))
3406 /* Changing the colors of `scroll-bar' sets frame parameters
3407 `scroll-bar-foreground' and `scroll-bar-background'. */
3408 if (EQ (attr, QCforeground))
3409 param = Qscroll_bar_foreground;
3410 else if (EQ (attr, QCbackground))
3411 param = Qscroll_bar_background;
3413 #endif /* not WINDOWSNT */
3414 else if (EQ (face, Qborder))
3416 /* Changing background color of `border' sets frame parameter
3417 `border-color'. */
3418 if (EQ (attr, QCbackground))
3419 param = Qborder_color;
3421 else if (EQ (face, Qcursor))
3423 /* Changing background color of `cursor' sets frame parameter
3424 `cursor-color'. */
3425 if (EQ (attr, QCbackground))
3426 param = Qcursor_color;
3428 else if (EQ (face, Qmouse))
3430 /* Changing background color of `mouse' sets frame parameter
3431 `mouse-color'. */
3432 if (EQ (attr, QCbackground))
3433 param = Qmouse_color;
3435 #endif /* HAVE_WINDOW_SYSTEM */
3436 else if (EQ (face, Qmenu))
3438 /* Indicate that we have to update the menu bar when
3439 realizing faces on FRAME. FRAME t change the
3440 default for new frames. We do this by setting
3441 setting the flag in new face caches */
3442 if (FRAMEP (frame))
3444 struct frame *f = XFRAME (frame);
3445 if (FRAME_FACE_CACHE (f) == NULL)
3446 FRAME_FACE_CACHE (f) = make_face_cache (f);
3447 FRAME_FACE_CACHE (f)->menu_face_changed_p = 1;
3449 else
3450 menu_face_changed_default = 1;
3453 if (!NILP (param))
3455 if (EQ (frame, Qt))
3456 /* Update `default-frame-alist', which is used for new frames. */
3458 store_in_alist (&Vdefault_frame_alist, param, value);
3460 else
3461 /* Update the current frame's parameters. */
3463 Lisp_Object cons;
3464 cons = XCAR (Vparam_value_alist);
3465 XSETCAR (cons, param);
3466 XSETCDR (cons, value);
3467 Fmodify_frame_parameters (frame, Vparam_value_alist);
3472 return face;
3476 #ifdef HAVE_WINDOW_SYSTEM
3478 /* Set the `font' frame parameter of FRAME determined from the
3479 font-object set in `default' face attributes LFACE. */
3481 static void
3482 set_font_frame_param (frame, lface)
3483 Lisp_Object frame, lface;
3485 struct frame *f = XFRAME (frame);
3487 if (FRAME_WINDOW_P (f))
3489 Lisp_Object font = LFACE_FONT (lface);
3491 if (FONT_SPEC_P (font))
3493 font = font_load_for_lface (f, XVECTOR (lface)->contents, font);
3494 if (NILP (font))
3495 return;
3496 LFACE_FONT (lface) = font;
3498 f->default_face_done_p = 0;
3499 Fmodify_frame_parameters (frame, Fcons (Fcons (Qfont, font), Qnil));
3504 /* Update the corresponding face when frame parameter PARAM on frame F
3505 has been assigned the value NEW_VALUE. */
3507 void
3508 update_face_from_frame_parameter (f, param, new_value)
3509 struct frame *f;
3510 Lisp_Object param, new_value;
3512 Lisp_Object face = Qnil;
3513 Lisp_Object lface;
3515 /* If there are no faces yet, give up. This is the case when called
3516 from Fx_create_frame, and we do the necessary things later in
3517 face-set-after-frame-defaults. */
3518 if (NILP (f->face_alist))
3519 return;
3521 if (EQ (param, Qforeground_color))
3523 face = Qdefault;
3524 lface = lface_from_face_name (f, face, 1);
3525 LFACE_FOREGROUND (lface) = (STRINGP (new_value)
3526 ? new_value : Qunspecified);
3527 realize_basic_faces (f);
3529 else if (EQ (param, Qbackground_color))
3531 Lisp_Object frame;
3533 /* Changing the background color might change the background
3534 mode, so that we have to load new defface specs.
3535 Call frame-update-face-colors to do that. */
3536 XSETFRAME (frame, f);
3537 call1 (Qframe_set_background_mode, frame);
3539 face = Qdefault;
3540 lface = lface_from_face_name (f, face, 1);
3541 LFACE_BACKGROUND (lface) = (STRINGP (new_value)
3542 ? new_value : Qunspecified);
3543 realize_basic_faces (f);
3545 else if (EQ (param, Qborder_color))
3547 face = Qborder;
3548 lface = lface_from_face_name (f, face, 1);
3549 LFACE_BACKGROUND (lface) = (STRINGP (new_value)
3550 ? new_value : Qunspecified);
3552 else if (EQ (param, Qcursor_color))
3554 face = Qcursor;
3555 lface = lface_from_face_name (f, face, 1);
3556 LFACE_BACKGROUND (lface) = (STRINGP (new_value)
3557 ? new_value : Qunspecified);
3559 else if (EQ (param, Qmouse_color))
3561 face = Qmouse;
3562 lface = lface_from_face_name (f, face, 1);
3563 LFACE_BACKGROUND (lface) = (STRINGP (new_value)
3564 ? new_value : Qunspecified);
3567 /* Changing a named face means that all realized faces depending on
3568 that face are invalid. Since we cannot tell which realized faces
3569 depend on the face, make sure they are all removed. This is done
3570 by incrementing face_change_count. The next call to
3571 init_iterator will then free realized faces. */
3572 if (!NILP (face)
3573 && NILP (Fget (face, Qface_no_inherit)))
3575 ++face_change_count;
3576 ++windows_or_buffers_changed;
3581 /* Get the value of X resource RESOURCE, class CLASS for the display
3582 of frame FRAME. This is here because ordinary `x-get-resource'
3583 doesn't take a frame argument. */
3585 DEFUN ("internal-face-x-get-resource", Finternal_face_x_get_resource,
3586 Sinternal_face_x_get_resource, 3, 3, 0, doc: /* */)
3587 (resource, class, frame)
3588 Lisp_Object resource, class, frame;
3590 Lisp_Object value = Qnil;
3591 CHECK_STRING (resource);
3592 CHECK_STRING (class);
3593 CHECK_LIVE_FRAME (frame);
3594 BLOCK_INPUT;
3595 value = display_x_get_resource (FRAME_X_DISPLAY_INFO (XFRAME (frame)),
3596 resource, class, Qnil, Qnil);
3597 UNBLOCK_INPUT;
3598 return value;
3602 /* Return resource string VALUE as a boolean value, i.e. nil, or t.
3603 If VALUE is "on" or "true", return t. If VALUE is "off" or
3604 "false", return nil. Otherwise, if SIGNAL_P is non-zero, signal an
3605 error; if SIGNAL_P is zero, return 0. */
3607 static Lisp_Object
3608 face_boolean_x_resource_value (value, signal_p)
3609 Lisp_Object value;
3610 int signal_p;
3612 Lisp_Object result = make_number (0);
3614 xassert (STRINGP (value));
3616 if (xstrcasecmp (SDATA (value), "on") == 0
3617 || xstrcasecmp (SDATA (value), "true") == 0)
3618 result = Qt;
3619 else if (xstrcasecmp (SDATA (value), "off") == 0
3620 || xstrcasecmp (SDATA (value), "false") == 0)
3621 result = Qnil;
3622 else if (xstrcasecmp (SDATA (value), "unspecified") == 0)
3623 result = Qunspecified;
3624 else if (signal_p)
3625 signal_error ("Invalid face attribute value from X resource", value);
3627 return result;
3631 DEFUN ("internal-set-lisp-face-attribute-from-resource",
3632 Finternal_set_lisp_face_attribute_from_resource,
3633 Sinternal_set_lisp_face_attribute_from_resource,
3634 3, 4, 0, doc: /* */)
3635 (face, attr, value, frame)
3636 Lisp_Object face, attr, value, frame;
3638 CHECK_SYMBOL (face);
3639 CHECK_SYMBOL (attr);
3640 CHECK_STRING (value);
3642 if (xstrcasecmp (SDATA (value), "unspecified") == 0)
3643 value = Qunspecified;
3644 else if (EQ (attr, QCheight))
3646 value = Fstring_to_number (value, make_number (10));
3647 if (XINT (value) <= 0)
3648 signal_error ("Invalid face height from X resource", value);
3650 else if (EQ (attr, QCbold) || EQ (attr, QCitalic))
3651 value = face_boolean_x_resource_value (value, 1);
3652 else if (EQ (attr, QCweight) || EQ (attr, QCslant) || EQ (attr, QCwidth))
3653 value = intern (SDATA (value));
3654 else if (EQ (attr, QCreverse_video) || EQ (attr, QCinverse_video))
3655 value = face_boolean_x_resource_value (value, 1);
3656 else if (EQ (attr, QCunderline)
3657 || EQ (attr, QCoverline)
3658 || EQ (attr, QCstrike_through))
3660 Lisp_Object boolean_value;
3662 /* If the result of face_boolean_x_resource_value is t or nil,
3663 VALUE does NOT specify a color. */
3664 boolean_value = face_boolean_x_resource_value (value, 0);
3665 if (SYMBOLP (boolean_value))
3666 value = boolean_value;
3668 else if (EQ (attr, QCbox) || EQ (attr, QCinherit))
3669 value = Fcar (Fread_from_string (value, Qnil, Qnil));
3671 return Finternal_set_lisp_face_attribute (face, attr, value, frame);
3674 #endif /* HAVE_WINDOW_SYSTEM */
3677 /***********************************************************************
3678 Menu face
3679 ***********************************************************************/
3681 #if defined HAVE_X_WINDOWS && defined USE_X_TOOLKIT
3683 /* Make menus on frame F appear as specified by the `menu' face. */
3685 static void
3686 x_update_menu_appearance (f)
3687 struct frame *f;
3689 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3690 XrmDatabase rdb;
3692 if (dpyinfo
3693 && (rdb = XrmGetDatabase (FRAME_X_DISPLAY (f)),
3694 rdb != NULL))
3696 char line[512];
3697 Lisp_Object lface = lface_from_face_name (f, Qmenu, 1);
3698 struct face *face = FACE_FROM_ID (f, MENU_FACE_ID);
3699 const char *myname = SDATA (Vx_resource_name);
3700 int changed_p = 0;
3701 #ifdef USE_MOTIF
3702 const char *popup_path = "popup_menu";
3703 #else
3704 const char *popup_path = "menu.popup";
3705 #endif
3707 if (STRINGP (LFACE_FOREGROUND (lface)))
3709 sprintf (line, "%s.%s*foreground: %s",
3710 myname, popup_path,
3711 SDATA (LFACE_FOREGROUND (lface)));
3712 XrmPutLineResource (&rdb, line);
3713 sprintf (line, "%s.pane.menubar*foreground: %s",
3714 myname, SDATA (LFACE_FOREGROUND (lface)));
3715 XrmPutLineResource (&rdb, line);
3716 changed_p = 1;
3719 if (STRINGP (LFACE_BACKGROUND (lface)))
3721 sprintf (line, "%s.%s*background: %s",
3722 myname, popup_path,
3723 SDATA (LFACE_BACKGROUND (lface)));
3724 XrmPutLineResource (&rdb, line);
3725 sprintf (line, "%s.pane.menubar*background: %s",
3726 myname, SDATA (LFACE_BACKGROUND (lface)));
3727 XrmPutLineResource (&rdb, line);
3728 changed_p = 1;
3731 if (face->font
3732 && (!UNSPECIFIEDP (LFACE_FAMILY (lface))
3733 || !UNSPECIFIEDP (LFACE_SWIDTH (lface))
3734 || !UNSPECIFIEDP (LFACE_WEIGHT (lface))
3735 || !UNSPECIFIEDP (LFACE_SLANT (lface))
3736 || !UNSPECIFIEDP (LFACE_HEIGHT (lface))))
3738 Lisp_Object xlfd = Ffont_xlfd_name (LFACE_FONT (lface), Qnil);
3739 #ifdef USE_MOTIF
3740 const char *suffix = "List";
3741 Bool motif = True;
3742 #else
3743 #if defined HAVE_X_I18N
3745 const char *suffix = "Set";
3746 #else
3747 const char *suffix = "";
3748 #endif
3749 Bool motif = False;
3750 #endif
3752 if (! NILP (xlfd))
3754 #if defined HAVE_X_I18N
3755 extern char *xic_create_fontsetname
3756 P_ ((char *base_fontname, Bool motif));
3757 char *fontsetname = xic_create_fontsetname (SDATA (xlfd), motif);
3758 #else
3759 char *fontsetname = (char *) SDATA (xlfd);
3760 #endif
3761 sprintf (line, "%s.pane.menubar*font%s: %s",
3762 myname, suffix, fontsetname);
3763 XrmPutLineResource (&rdb, line);
3764 sprintf (line, "%s.%s*font%s: %s",
3765 myname, popup_path, suffix, fontsetname);
3766 XrmPutLineResource (&rdb, line);
3767 changed_p = 1;
3768 if (fontsetname != (char *) SDATA (xlfd))
3769 xfree (fontsetname);
3773 if (changed_p && f->output_data.x->menubar_widget)
3774 free_frame_menubar (f);
3778 #endif /* HAVE_X_WINDOWS && USE_X_TOOLKIT */
3781 DEFUN ("face-attribute-relative-p", Fface_attribute_relative_p,
3782 Sface_attribute_relative_p,
3783 2, 2, 0,
3784 doc: /* Check whether a face attribute value is relative.
3785 Specifically, this function returns t if the attribute ATTRIBUTE
3786 with the value VALUE is relative.
3788 A relative value is one that doesn't entirely override whatever is
3789 inherited from another face. For most possible attributes,
3790 the only relative value that users see is `unspecified'.
3791 However, for :height, floating point values are also relative. */)
3792 (attribute, value)
3793 Lisp_Object attribute, value;
3795 if (EQ (value, Qunspecified) || (EQ (value, Qignore_defface)))
3796 return Qt;
3797 else if (EQ (attribute, QCheight))
3798 return INTEGERP (value) ? Qnil : Qt;
3799 else
3800 return Qnil;
3803 DEFUN ("merge-face-attribute", Fmerge_face_attribute, Smerge_face_attribute,
3804 3, 3, 0,
3805 doc: /* Return face ATTRIBUTE VALUE1 merged with VALUE2.
3806 If VALUE1 or VALUE2 are absolute (see `face-attribute-relative-p'), then
3807 the result will be absolute, otherwise it will be relative. */)
3808 (attribute, value1, value2)
3809 Lisp_Object attribute, value1, value2;
3811 if (EQ (value1, Qunspecified) || EQ (value1, Qignore_defface))
3812 return value2;
3813 else if (EQ (attribute, QCheight))
3814 return merge_face_heights (value1, value2, value1);
3815 else
3816 return value1;
3820 DEFUN ("internal-get-lisp-face-attribute", Finternal_get_lisp_face_attribute,
3821 Sinternal_get_lisp_face_attribute,
3822 2, 3, 0,
3823 doc: /* Return face attribute KEYWORD of face SYMBOL.
3824 If SYMBOL does not name a valid Lisp face or KEYWORD isn't a valid
3825 face attribute name, signal an error.
3826 If the optional argument FRAME is given, report on face SYMBOL in that
3827 frame. If FRAME is t, report on the defaults for face SYMBOL (for new
3828 frames). If FRAME is omitted or nil, use the selected frame. */)
3829 (symbol, keyword, frame)
3830 Lisp_Object symbol, keyword, frame;
3832 Lisp_Object lface, value = Qnil;
3834 CHECK_SYMBOL (symbol);
3835 CHECK_SYMBOL (keyword);
3837 if (EQ (frame, Qt))
3838 lface = lface_from_face_name (NULL, symbol, 1);
3839 else
3841 if (NILP (frame))
3842 frame = selected_frame;
3843 CHECK_LIVE_FRAME (frame);
3844 lface = lface_from_face_name (XFRAME (frame), symbol, 1);
3847 if (EQ (keyword, QCfamily))
3848 value = LFACE_FAMILY (lface);
3849 else if (EQ (keyword, QCheight))
3850 value = LFACE_HEIGHT (lface);
3851 else if (EQ (keyword, QCweight))
3852 value = LFACE_WEIGHT (lface);
3853 else if (EQ (keyword, QCslant))
3854 value = LFACE_SLANT (lface);
3855 else if (EQ (keyword, QCunderline))
3856 value = LFACE_UNDERLINE (lface);
3857 else if (EQ (keyword, QCoverline))
3858 value = LFACE_OVERLINE (lface);
3859 else if (EQ (keyword, QCstrike_through))
3860 value = LFACE_STRIKE_THROUGH (lface);
3861 else if (EQ (keyword, QCbox))
3862 value = LFACE_BOX (lface);
3863 else if (EQ (keyword, QCinverse_video)
3864 || EQ (keyword, QCreverse_video))
3865 value = LFACE_INVERSE (lface);
3866 else if (EQ (keyword, QCforeground))
3867 value = LFACE_FOREGROUND (lface);
3868 else if (EQ (keyword, QCbackground))
3869 value = LFACE_BACKGROUND (lface);
3870 else if (EQ (keyword, QCstipple))
3871 value = LFACE_STIPPLE (lface);
3872 else if (EQ (keyword, QCwidth))
3873 value = LFACE_SWIDTH (lface);
3874 else if (EQ (keyword, QCinherit))
3875 value = LFACE_INHERIT (lface);
3876 else if (EQ (keyword, QCfont))
3877 value = LFACE_FONT (lface);
3878 else if (EQ (keyword, QCfontset))
3879 value = LFACE_FONTSET (lface);
3880 else
3881 signal_error ("Invalid face attribute name", keyword);
3883 if (IGNORE_DEFFACE_P (value))
3884 return Qunspecified;
3886 return value;
3890 DEFUN ("internal-lisp-face-attribute-values",
3891 Finternal_lisp_face_attribute_values,
3892 Sinternal_lisp_face_attribute_values, 1, 1, 0,
3893 doc: /* Return a list of valid discrete values for face attribute ATTR.
3894 Value is nil if ATTR doesn't have a discrete set of valid values. */)
3895 (attr)
3896 Lisp_Object attr;
3898 Lisp_Object result = Qnil;
3900 CHECK_SYMBOL (attr);
3902 if (EQ (attr, QCunderline))
3903 result = Fcons (Qt, Fcons (Qnil, Qnil));
3904 else if (EQ (attr, QCoverline))
3905 result = Fcons (Qt, Fcons (Qnil, Qnil));
3906 else if (EQ (attr, QCstrike_through))
3907 result = Fcons (Qt, Fcons (Qnil, Qnil));
3908 else if (EQ (attr, QCinverse_video) || EQ (attr, QCreverse_video))
3909 result = Fcons (Qt, Fcons (Qnil, Qnil));
3911 return result;
3915 DEFUN ("internal-merge-in-global-face", Finternal_merge_in_global_face,
3916 Sinternal_merge_in_global_face, 2, 2, 0,
3917 doc: /* Add attributes from frame-default definition of FACE to FACE on FRAME.
3918 Default face attributes override any local face attributes. */)
3919 (face, frame)
3920 Lisp_Object face, frame;
3922 int i;
3923 Lisp_Object global_lface, local_lface, *gvec, *lvec;
3925 CHECK_LIVE_FRAME (frame);
3926 global_lface = lface_from_face_name (NULL, face, 1);
3927 local_lface = lface_from_face_name (XFRAME (frame), face, 0);
3928 if (NILP (local_lface))
3929 local_lface = Finternal_make_lisp_face (face, frame);
3931 /* Make every specified global attribute override the local one.
3932 BEWARE!! This is only used from `face-set-after-frame-default' where
3933 the local frame is defined from default specs in `face-defface-spec'
3934 and those should be overridden by global settings. Hence the strange
3935 "global before local" priority. */
3936 lvec = XVECTOR (local_lface)->contents;
3937 gvec = XVECTOR (global_lface)->contents;
3938 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3939 if (! UNSPECIFIEDP (gvec[i]))
3941 if (IGNORE_DEFFACE_P (gvec[i]))
3942 lvec[i] = Qunspecified;
3943 else
3944 lvec[i] = gvec[i];
3947 return Qnil;
3951 /* The following function is implemented for compatibility with 20.2.
3952 The function is used in x-resolve-fonts when it is asked to
3953 return fonts with the same size as the font of a face. This is
3954 done in fontset.el. */
3956 DEFUN ("face-font", Fface_font, Sface_font, 1, 3, 0,
3957 doc: /* Return the font name of face FACE, or nil if it is unspecified.
3958 The font name is, by default, for ASCII characters.
3959 If the optional argument FRAME is given, report on face FACE in that frame.
3960 If FRAME is t, report on the defaults for face FACE (for new frames).
3961 The font default for a face is either nil, or a list
3962 of the form (bold), (italic) or (bold italic).
3963 If FRAME is omitted or nil, use the selected frame. And, in this case,
3964 if the optional third argument CHARACTER is given,
3965 return the font name used for CHARACTER. */)
3966 (face, frame, character)
3967 Lisp_Object face, frame, character;
3969 if (EQ (frame, Qt))
3971 Lisp_Object result = Qnil;
3972 Lisp_Object lface = lface_from_face_name (NULL, face, 1);
3974 if (!UNSPECIFIEDP (LFACE_WEIGHT (lface))
3975 && !EQ (LFACE_WEIGHT (lface), Qnormal))
3976 result = Fcons (Qbold, result);
3978 if (!UNSPECIFIEDP (LFACE_SLANT (lface))
3979 && !EQ (LFACE_SLANT (lface), Qnormal))
3980 result = Fcons (Qitalic, result);
3982 return result;
3984 else
3986 struct frame *f = frame_or_selected_frame (frame, 1);
3987 int face_id = lookup_named_face (f, face, 1);
3988 struct face *face = FACE_FROM_ID (f, face_id);
3990 if (! face)
3991 return Qnil;
3992 #ifdef HAVE_WINDOW_SYSTEM
3993 if (FRAME_WINDOW_P (f) && !NILP (character))
3995 CHECK_CHARACTER (character);
3996 face_id = FACE_FOR_CHAR (f, face, XINT (character), -1, Qnil);
3997 face = FACE_FROM_ID (f, face_id);
3999 return (face->font
4000 ? face->font->props[FONT_NAME_INDEX]
4001 : Qnil);
4002 #else /* !HAVE_WINDOW_SYSTEM */
4003 return build_string (FRAME_MSDOS_P (f)
4004 ? "ms-dos"
4005 : FRAME_W32_P (f) ? "w32term"
4006 :"tty");
4007 #endif
4012 /* Compare face-attribute values v1 and v2 for equality. Value is non-zero if
4013 all attributes are `equal'. Tries to be fast because this function
4014 is called quite often. */
4016 static INLINE int
4017 face_attr_equal_p (v1, v2)
4018 Lisp_Object v1, v2;
4020 /* Type can differ, e.g. when one attribute is unspecified, i.e. nil,
4021 and the other is specified. */
4022 if (XTYPE (v1) != XTYPE (v2))
4023 return 0;
4025 if (EQ (v1, v2))
4026 return 1;
4028 switch (XTYPE (v1))
4030 case Lisp_String:
4031 if (SBYTES (v1) != SBYTES (v2))
4032 return 0;
4034 return bcmp (SDATA (v1), SDATA (v2), SBYTES (v1)) == 0;
4036 case Lisp_Int:
4037 case Lisp_Symbol:
4038 return 0;
4040 default:
4041 return !NILP (Fequal (v1, v2));
4046 /* Compare face vectors V1 and V2 for equality. Value is non-zero if
4047 all attributes are `equal'. Tries to be fast because this function
4048 is called quite often. */
4050 static INLINE int
4051 lface_equal_p (v1, v2)
4052 Lisp_Object *v1, *v2;
4054 int i, equal_p = 1;
4056 for (i = 1; i < LFACE_VECTOR_SIZE && equal_p; ++i)
4057 equal_p = face_attr_equal_p (v1[i], v2[i]);
4059 return equal_p;
4063 DEFUN ("internal-lisp-face-equal-p", Finternal_lisp_face_equal_p,
4064 Sinternal_lisp_face_equal_p, 2, 3, 0,
4065 doc: /* True if FACE1 and FACE2 are equal.
4066 If the optional argument FRAME is given, report on FACE1 and FACE2 in that frame.
4067 If FRAME is t, report on the defaults for FACE1 and FACE2 (for new frames).
4068 If FRAME is omitted or nil, use the selected frame. */)
4069 (face1, face2, frame)
4070 Lisp_Object face1, face2, frame;
4072 int equal_p;
4073 struct frame *f;
4074 Lisp_Object lface1, lface2;
4076 if (EQ (frame, Qt))
4077 f = NULL;
4078 else
4079 /* Don't use check_x_frame here because this function is called
4080 before X frames exist. At that time, if FRAME is nil,
4081 selected_frame will be used which is the frame dumped with
4082 Emacs. That frame is not an X frame. */
4083 f = frame_or_selected_frame (frame, 2);
4085 lface1 = lface_from_face_name (f, face1, 1);
4086 lface2 = lface_from_face_name (f, face2, 1);
4087 equal_p = lface_equal_p (XVECTOR (lface1)->contents,
4088 XVECTOR (lface2)->contents);
4089 return equal_p ? Qt : Qnil;
4093 DEFUN ("internal-lisp-face-empty-p", Finternal_lisp_face_empty_p,
4094 Sinternal_lisp_face_empty_p, 1, 2, 0,
4095 doc: /* True if FACE has no attribute specified.
4096 If the optional argument FRAME is given, report on face FACE in that frame.
4097 If FRAME is t, report on the defaults for face FACE (for new frames).
4098 If FRAME is omitted or nil, use the selected frame. */)
4099 (face, frame)
4100 Lisp_Object face, frame;
4102 struct frame *f;
4103 Lisp_Object lface;
4104 int i;
4106 if (NILP (frame))
4107 frame = selected_frame;
4108 CHECK_LIVE_FRAME (frame);
4109 f = XFRAME (frame);
4111 if (EQ (frame, Qt))
4112 lface = lface_from_face_name (NULL, face, 1);
4113 else
4114 lface = lface_from_face_name (f, face, 1);
4116 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
4117 if (!UNSPECIFIEDP (AREF (lface, i)))
4118 break;
4120 return i == LFACE_VECTOR_SIZE ? Qt : Qnil;
4124 DEFUN ("frame-face-alist", Fframe_face_alist, Sframe_face_alist,
4125 0, 1, 0,
4126 doc: /* Return an alist of frame-local faces defined on FRAME.
4127 For internal use only. */)
4128 (frame)
4129 Lisp_Object frame;
4131 struct frame *f = frame_or_selected_frame (frame, 0);
4132 return f->face_alist;
4136 /* Return a hash code for Lisp string STRING with case ignored. Used
4137 below in computing a hash value for a Lisp face. */
4139 static INLINE unsigned
4140 hash_string_case_insensitive (string)
4141 Lisp_Object string;
4143 const unsigned char *s;
4144 unsigned hash = 0;
4145 xassert (STRINGP (string));
4146 for (s = SDATA (string); *s; ++s)
4147 hash = (hash << 1) ^ tolower (*s);
4148 return hash;
4152 /* Return a hash code for face attribute vector V. */
4154 static INLINE unsigned
4155 lface_hash (v)
4156 Lisp_Object *v;
4158 return (hash_string_case_insensitive (v[LFACE_FAMILY_INDEX])
4159 ^ hash_string_case_insensitive (v[LFACE_FOREGROUND_INDEX])
4160 ^ hash_string_case_insensitive (v[LFACE_BACKGROUND_INDEX])
4161 ^ XHASH (v[LFACE_WEIGHT_INDEX])
4162 ^ XHASH (v[LFACE_SLANT_INDEX])
4163 ^ XHASH (v[LFACE_SWIDTH_INDEX])
4164 ^ XHASH (v[LFACE_HEIGHT_INDEX]));
4168 /* Return non-zero if LFACE1 and LFACE2 specify the same font (without
4169 considering charsets/registries). They do if they specify the same
4170 family, point size, weight, width, slant, and font. Both
4171 LFACE1 and LFACE2 must be fully-specified. */
4173 static INLINE int
4174 lface_same_font_attributes_p (lface1, lface2)
4175 Lisp_Object *lface1, *lface2;
4177 xassert (lface_fully_specified_p (lface1)
4178 && lface_fully_specified_p (lface2));
4179 return (xstrcasecmp (SDATA (lface1[LFACE_FAMILY_INDEX]),
4180 SDATA (lface2[LFACE_FAMILY_INDEX])) == 0
4181 && EQ (lface1[LFACE_HEIGHT_INDEX], lface2[LFACE_HEIGHT_INDEX])
4182 && EQ (lface1[LFACE_SWIDTH_INDEX], lface2[LFACE_SWIDTH_INDEX])
4183 && EQ (lface1[LFACE_WEIGHT_INDEX], lface2[LFACE_WEIGHT_INDEX])
4184 && EQ (lface1[LFACE_SLANT_INDEX], lface2[LFACE_SLANT_INDEX])
4185 && EQ (lface1[LFACE_FONT_INDEX], lface2[LFACE_FONT_INDEX])
4186 && (EQ (lface1[LFACE_FONTSET_INDEX], lface2[LFACE_FONTSET_INDEX])
4187 || (STRINGP (lface1[LFACE_FONTSET_INDEX])
4188 && STRINGP (lface2[LFACE_FONTSET_INDEX])
4189 && ! xstrcasecmp (SDATA (lface1[LFACE_FONTSET_INDEX]),
4190 SDATA (lface2[LFACE_FONTSET_INDEX]))))
4196 /***********************************************************************
4197 Realized Faces
4198 ***********************************************************************/
4200 /* Allocate and return a new realized face for Lisp face attribute
4201 vector ATTR. */
4203 static struct face *
4204 make_realized_face (attr)
4205 Lisp_Object *attr;
4207 struct face *face = (struct face *) xmalloc (sizeof *face);
4208 bzero (face, sizeof *face);
4209 face->ascii_face = face;
4210 bcopy (attr, face->lface, sizeof face->lface);
4211 return face;
4215 /* Free realized face FACE, including its X resources. FACE may
4216 be null. */
4218 void
4219 free_realized_face (f, face)
4220 struct frame *f;
4221 struct face *face;
4223 if (face)
4225 #ifdef HAVE_WINDOW_SYSTEM
4226 if (FRAME_WINDOW_P (f))
4228 /* Free fontset of FACE if it is ASCII face. */
4229 if (face->fontset >= 0 && face == face->ascii_face)
4230 free_face_fontset (f, face);
4231 if (face->gc)
4233 BLOCK_INPUT;
4234 if (face->font)
4235 font_done_for_face (f, face);
4236 x_free_gc (f, face->gc);
4237 face->gc = 0;
4238 UNBLOCK_INPUT;
4241 free_face_colors (f, face);
4242 x_destroy_bitmap (f, face->stipple);
4244 #endif /* HAVE_WINDOW_SYSTEM */
4246 xfree (face);
4251 /* Prepare face FACE for subsequent display on frame F. This
4252 allocated GCs if they haven't been allocated yet or have been freed
4253 by clearing the face cache. */
4255 void
4256 prepare_face_for_display (f, face)
4257 struct frame *f;
4258 struct face *face;
4260 #ifdef HAVE_WINDOW_SYSTEM
4261 xassert (FRAME_WINDOW_P (f));
4263 if (face->gc == 0)
4265 XGCValues xgcv;
4266 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
4268 xgcv.foreground = face->foreground;
4269 xgcv.background = face->background;
4270 #ifdef HAVE_X_WINDOWS
4271 xgcv.graphics_exposures = False;
4272 #endif
4274 BLOCK_INPUT;
4275 #ifdef HAVE_X_WINDOWS
4276 if (face->stipple)
4278 xgcv.fill_style = FillOpaqueStippled;
4279 xgcv.stipple = x_bitmap_pixmap (f, face->stipple);
4280 mask |= GCFillStyle | GCStipple;
4282 #endif
4283 face->gc = x_create_gc (f, mask, &xgcv);
4284 if (face->font)
4285 font_prepare_for_face (f, face);
4286 UNBLOCK_INPUT;
4288 #endif /* HAVE_WINDOW_SYSTEM */
4292 /* Returns the `distance' between the colors X and Y. */
4294 static int
4295 color_distance (x, y)
4296 XColor *x, *y;
4298 /* This formula is from a paper title `Colour metric' by Thiadmer Riemersma.
4299 Quoting from that paper:
4301 This formula has results that are very close to L*u*v* (with the
4302 modified lightness curve) and, more importantly, it is a more even
4303 algorithm: it does not have a range of colours where it suddenly
4304 gives far from optimal results.
4306 See <http://www.compuphase.com/cmetric.htm> for more info. */
4308 long r = (x->red - y->red) >> 8;
4309 long g = (x->green - y->green) >> 8;
4310 long b = (x->blue - y->blue) >> 8;
4311 long r_mean = (x->red + y->red) >> 9;
4313 return
4314 (((512 + r_mean) * r * r) >> 8)
4315 + 4 * g * g
4316 + (((767 - r_mean) * b * b) >> 8);
4320 DEFUN ("color-distance", Fcolor_distance, Scolor_distance, 2, 3, 0,
4321 doc: /* Return an integer distance between COLOR1 and COLOR2 on FRAME.
4322 COLOR1 and COLOR2 may be either strings containing the color name,
4323 or lists of the form (RED GREEN BLUE).
4324 If FRAME is unspecified or nil, the current frame is used. */)
4325 (color1, color2, frame)
4326 Lisp_Object color1, color2, frame;
4328 struct frame *f;
4329 XColor cdef1, cdef2;
4331 if (NILP (frame))
4332 frame = selected_frame;
4333 CHECK_LIVE_FRAME (frame);
4334 f = XFRAME (frame);
4336 if (!(CONSP (color1) && parse_rgb_list (color1, &cdef1))
4337 && !(STRINGP (color1) && defined_color (f, SDATA (color1), &cdef1, 0)))
4338 signal_error ("Invalid color", color1);
4339 if (!(CONSP (color2) && parse_rgb_list (color2, &cdef2))
4340 && !(STRINGP (color2) && defined_color (f, SDATA (color2), &cdef2, 0)))
4341 signal_error ("Invalid color", color2);
4343 return make_number (color_distance (&cdef1, &cdef2));
4347 /***********************************************************************
4348 Face Cache
4349 ***********************************************************************/
4351 /* Return a new face cache for frame F. */
4353 static struct face_cache *
4354 make_face_cache (f)
4355 struct frame *f;
4357 struct face_cache *c;
4358 int size;
4360 c = (struct face_cache *) xmalloc (sizeof *c);
4361 bzero (c, sizeof *c);
4362 size = FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
4363 c->buckets = (struct face **) xmalloc (size);
4364 bzero (c->buckets, size);
4365 c->size = 50;
4366 c->faces_by_id = (struct face **) xmalloc (c->size * sizeof *c->faces_by_id);
4367 c->f = f;
4368 c->menu_face_changed_p = menu_face_changed_default;
4369 return c;
4373 /* Clear out all graphics contexts for all realized faces, except for
4374 the basic faces. This should be done from time to time just to avoid
4375 keeping too many graphics contexts that are no longer needed. */
4377 static void
4378 clear_face_gcs (c)
4379 struct face_cache *c;
4381 if (c && FRAME_WINDOW_P (c->f))
4383 #ifdef HAVE_WINDOW_SYSTEM
4384 int i;
4385 for (i = BASIC_FACE_ID_SENTINEL; i < c->used; ++i)
4387 struct face *face = c->faces_by_id[i];
4388 if (face && face->gc)
4390 BLOCK_INPUT;
4391 if (face->font)
4392 font_done_for_face (c->f, face);
4393 x_free_gc (c->f, face->gc);
4394 face->gc = 0;
4395 UNBLOCK_INPUT;
4398 #endif /* HAVE_WINDOW_SYSTEM */
4403 /* Free all realized faces in face cache C, including basic faces.
4404 C may be null. If faces are freed, make sure the frame's current
4405 matrix is marked invalid, so that a display caused by an expose
4406 event doesn't try to use faces we destroyed. */
4408 static void
4409 free_realized_faces (c)
4410 struct face_cache *c;
4412 if (c && c->used)
4414 int i, size;
4415 struct frame *f = c->f;
4417 /* We must block input here because we can't process X events
4418 safely while only some faces are freed, or when the frame's
4419 current matrix still references freed faces. */
4420 BLOCK_INPUT;
4422 for (i = 0; i < c->used; ++i)
4424 free_realized_face (f, c->faces_by_id[i]);
4425 c->faces_by_id[i] = NULL;
4428 c->used = 0;
4429 size = FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
4430 bzero (c->buckets, size);
4432 /* Must do a thorough redisplay the next time. Mark current
4433 matrices as invalid because they will reference faces freed
4434 above. This function is also called when a frame is
4435 destroyed. In this case, the root window of F is nil. */
4436 if (WINDOWP (f->root_window))
4438 clear_current_matrices (f);
4439 ++windows_or_buffers_changed;
4442 UNBLOCK_INPUT;
4447 /* Free all realized faces that are using FONTSET on frame F. */
4449 void
4450 free_realized_faces_for_fontset (f, fontset)
4451 struct frame *f;
4452 int fontset;
4454 struct face_cache *cache = FRAME_FACE_CACHE (f);
4455 struct face *face;
4456 int i;
4458 /* We must block input here because we can't process X events safely
4459 while only some faces are freed, or when the frame's current
4460 matrix still references freed faces. */
4461 BLOCK_INPUT;
4463 for (i = 0; i < cache->used; i++)
4465 face = cache->faces_by_id[i];
4466 if (face
4467 && face->fontset == fontset)
4469 uncache_face (cache, face);
4470 free_realized_face (f, face);
4474 /* Must do a thorough redisplay the next time. Mark current
4475 matrices as invalid because they will reference faces freed
4476 above. This function is also called when a frame is destroyed.
4477 In this case, the root window of F is nil. */
4478 if (WINDOWP (f->root_window))
4480 clear_current_matrices (f);
4481 ++windows_or_buffers_changed;
4484 UNBLOCK_INPUT;
4488 /* Free all realized faces on FRAME or on all frames if FRAME is nil.
4489 This is done after attributes of a named face have been changed,
4490 because we can't tell which realized faces depend on that face. */
4492 void
4493 free_all_realized_faces (frame)
4494 Lisp_Object frame;
4496 if (NILP (frame))
4498 Lisp_Object rest;
4499 FOR_EACH_FRAME (rest, frame)
4500 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
4502 else
4503 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
4507 /* Free face cache C and faces in it, including their X resources. */
4509 static void
4510 free_face_cache (c)
4511 struct face_cache *c;
4513 if (c)
4515 free_realized_faces (c);
4516 xfree (c->buckets);
4517 xfree (c->faces_by_id);
4518 xfree (c);
4523 /* Cache realized face FACE in face cache C. HASH is the hash value
4524 of FACE. If FACE is for ASCII characters (i.e. FACE->ascii_face ==
4525 FACE), insert the new face to the beginning of the collision list
4526 of the face hash table of C. Otherwise, add the new face to the
4527 end of the collision list. This way, lookup_face can quickly find
4528 that a requested face is not cached. */
4530 static void
4531 cache_face (c, face, hash)
4532 struct face_cache *c;
4533 struct face *face;
4534 unsigned hash;
4536 int i = hash % FACE_CACHE_BUCKETS_SIZE;
4538 face->hash = hash;
4540 if (face->ascii_face != face)
4542 struct face *last = c->buckets[i];
4543 if (last)
4545 while (last->next)
4546 last = last->next;
4547 last->next = face;
4548 face->prev = last;
4549 face->next = NULL;
4551 else
4553 c->buckets[i] = face;
4554 face->prev = face->next = NULL;
4557 else
4559 face->prev = NULL;
4560 face->next = c->buckets[i];
4561 if (face->next)
4562 face->next->prev = face;
4563 c->buckets[i] = face;
4566 /* Find a free slot in C->faces_by_id and use the index of the free
4567 slot as FACE->id. */
4568 for (i = 0; i < c->used; ++i)
4569 if (c->faces_by_id[i] == NULL)
4570 break;
4571 face->id = i;
4573 /* Maybe enlarge C->faces_by_id. */
4574 if (i == c->used)
4576 if (c->used == c->size)
4578 int new_size, sz;
4579 new_size = min (2 * c->size, MAX_FACE_ID);
4580 if (new_size == c->size)
4581 abort (); /* Alternatives? ++kfs */
4582 sz = new_size * sizeof *c->faces_by_id;
4583 c->faces_by_id = (struct face **) xrealloc (c->faces_by_id, sz);
4584 c->size = new_size;
4586 c->used++;
4589 #if GLYPH_DEBUG
4590 /* Check that FACE got a unique id. */
4592 int j, n;
4593 struct face *face;
4595 for (j = n = 0; j < FACE_CACHE_BUCKETS_SIZE; ++j)
4596 for (face = c->buckets[j]; face; face = face->next)
4597 if (face->id == i)
4598 ++n;
4600 xassert (n == 1);
4602 #endif /* GLYPH_DEBUG */
4604 c->faces_by_id[i] = face;
4608 /* Remove face FACE from cache C. */
4610 static void
4611 uncache_face (c, face)
4612 struct face_cache *c;
4613 struct face *face;
4615 int i = face->hash % FACE_CACHE_BUCKETS_SIZE;
4617 if (face->prev)
4618 face->prev->next = face->next;
4619 else
4620 c->buckets[i] = face->next;
4622 if (face->next)
4623 face->next->prev = face->prev;
4625 c->faces_by_id[face->id] = NULL;
4626 if (face->id == c->used)
4627 --c->used;
4631 /* Look up a realized face with face attributes ATTR in the face cache
4632 of frame F. The face will be used to display ASCII characters.
4633 Value is the ID of the face found. If no suitable face is found,
4634 realize a new one. */
4636 INLINE int
4637 lookup_face (f, attr)
4638 struct frame *f;
4639 Lisp_Object *attr;
4641 struct face_cache *cache = FRAME_FACE_CACHE (f);
4642 unsigned hash;
4643 int i;
4644 struct face *face;
4646 xassert (cache != NULL);
4647 check_lface_attrs (attr);
4649 /* Look up ATTR in the face cache. */
4650 hash = lface_hash (attr);
4651 i = hash % FACE_CACHE_BUCKETS_SIZE;
4653 for (face = cache->buckets[i]; face; face = face->next)
4655 if (face->ascii_face != face)
4657 /* There's no more ASCII face. */
4658 face = NULL;
4659 break;
4661 if (face->hash == hash
4662 && lface_equal_p (face->lface, attr))
4663 break;
4666 /* If not found, realize a new face. */
4667 if (face == NULL)
4668 face = realize_face (cache, attr, -1);
4670 #if GLYPH_DEBUG
4671 xassert (face == FACE_FROM_ID (f, face->id));
4672 #endif /* GLYPH_DEBUG */
4674 return face->id;
4677 #ifdef HAVE_WINDOW_SYSTEM
4678 /* Look up a realized face that has the same attributes as BASE_FACE
4679 except for the font in the face cache of frame F. If FONT-OBJECT
4680 is not nil, it is an already opened font. If FONT-OBJECT is nil,
4681 the face has no font. Value is the ID of the face found. If no
4682 suitable face is found, realize a new one. */
4685 face_for_font (f, font_object, base_face)
4686 struct frame *f;
4687 Lisp_Object font_object;
4688 struct face *base_face;
4690 struct face_cache *cache = FRAME_FACE_CACHE (f);
4691 unsigned hash;
4692 int i;
4693 struct face *face;
4695 xassert (cache != NULL);
4696 base_face = base_face->ascii_face;
4697 hash = lface_hash (base_face->lface);
4698 i = hash % FACE_CACHE_BUCKETS_SIZE;
4700 for (face = cache->buckets[i]; face; face = face->next)
4702 if (face->ascii_face == face)
4703 continue;
4704 if (face->ascii_face == base_face
4705 && face->font == (NILP (font_object) ? NULL
4706 : XFONT_OBJECT (font_object))
4707 && lface_equal_p (face->lface, base_face->lface))
4708 return face->id;
4711 /* If not found, realize a new face. */
4712 face = realize_non_ascii_face (f, font_object, base_face);
4713 return face->id;
4715 #endif /* HAVE_WINDOW_SYSTEM */
4717 /* Return the face id of the realized face for named face SYMBOL on
4718 frame F suitable for displaying ASCII characters. Value is -1 if
4719 the face couldn't be determined, which might happen if the default
4720 face isn't realized and cannot be realized. */
4723 lookup_named_face (f, symbol, signal_p)
4724 struct frame *f;
4725 Lisp_Object symbol;
4726 int signal_p;
4728 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4729 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
4730 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4732 if (default_face == NULL)
4734 if (!realize_basic_faces (f))
4735 return -1;
4736 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4737 if (default_face == NULL)
4738 abort (); /* realize_basic_faces must have set it up */
4741 if (! get_lface_attributes (f, symbol, symbol_attrs, signal_p, 0))
4742 return -1;
4744 bcopy (default_face->lface, attrs, sizeof attrs);
4745 merge_face_vectors (f, symbol_attrs, attrs, 0);
4747 return lookup_face (f, attrs);
4751 /* Return the display face-id of the basic face who's canonical face-id
4752 is FACE_ID. The return value will usually simply be FACE_ID, unless that
4753 basic face has bee remapped via Vface_remapping_alist. This function is
4754 conservative: if something goes wrong, it will simply return FACE_ID
4755 rather than signal an error. */
4758 lookup_basic_face (f, face_id)
4759 struct frame *f;
4760 int face_id;
4762 Lisp_Object name, mapping;
4763 int remapped_face_id;
4765 if (NILP (Vface_remapping_alist))
4766 return face_id; /* Nothing to do. */
4768 switch (face_id)
4770 case DEFAULT_FACE_ID: name = Qdefault; break;
4771 case MODE_LINE_FACE_ID: name = Qmode_line; break;
4772 case MODE_LINE_INACTIVE_FACE_ID: name = Qmode_line_inactive; break;
4773 case HEADER_LINE_FACE_ID: name = Qheader_line; break;
4774 case TOOL_BAR_FACE_ID: name = Qtool_bar; break;
4775 case FRINGE_FACE_ID: name = Qfringe; break;
4776 case SCROLL_BAR_FACE_ID: name = Qscroll_bar; break;
4777 case BORDER_FACE_ID: name = Qborder; break;
4778 case CURSOR_FACE_ID: name = Qcursor; break;
4779 case MOUSE_FACE_ID: name = Qmouse; break;
4780 case MENU_FACE_ID: name = Qmenu; break;
4782 default:
4783 abort (); /* the caller is supposed to pass us a basic face id */
4786 /* Do a quick scan through Vface_remapping_alist, and return immediately
4787 if there is no remapping for face NAME. This is just an optimization
4788 for the very common no-remapping case. */
4789 mapping = assq_no_quit (name, Vface_remapping_alist);
4790 if (NILP (mapping))
4791 return face_id; /* Give up. */
4793 /* If there is a remapping entry, lookup the face using NAME, which will
4794 handle the remapping too. */
4795 remapped_face_id = lookup_named_face (f, name, 0);
4796 if (remapped_face_id < 0)
4797 return face_id; /* Give up. */
4799 return remapped_face_id;
4803 /* Return the ID of the realized ASCII face of Lisp face with ID
4804 LFACE_ID on frame F. Value is -1 if LFACE_ID isn't valid. */
4807 ascii_face_of_lisp_face (f, lface_id)
4808 struct frame *f;
4809 int lface_id;
4811 int face_id;
4813 if (lface_id >= 0 && lface_id < lface_id_to_name_size)
4815 Lisp_Object face_name = lface_id_to_name[lface_id];
4816 face_id = lookup_named_face (f, face_name, 1);
4818 else
4819 face_id = -1;
4821 return face_id;
4825 /* Return a face for charset ASCII that is like the face with id
4826 FACE_ID on frame F, but has a font that is STEPS steps smaller.
4827 STEPS < 0 means larger. Value is the id of the face. */
4830 smaller_face (f, face_id, steps)
4831 struct frame *f;
4832 int face_id, steps;
4834 #ifdef HAVE_WINDOW_SYSTEM
4835 struct face *face;
4836 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4837 int pt, last_pt, last_height;
4838 int delta;
4839 int new_face_id;
4840 struct face *new_face;
4842 /* If not called for an X frame, just return the original face. */
4843 if (FRAME_TERMCAP_P (f))
4844 return face_id;
4846 /* Try in increments of 1/2 pt. */
4847 delta = steps < 0 ? 5 : -5;
4848 steps = eabs (steps);
4850 face = FACE_FROM_ID (f, face_id);
4851 bcopy (face->lface, attrs, sizeof attrs);
4852 pt = last_pt = XFASTINT (attrs[LFACE_HEIGHT_INDEX]);
4853 new_face_id = face_id;
4854 last_height = FONT_HEIGHT (face->font);
4856 while (steps
4857 && pt + delta > 0
4858 /* Give up if we cannot find a font within 10pt. */
4859 && eabs (last_pt - pt) < 100)
4861 /* Look up a face for a slightly smaller/larger font. */
4862 pt += delta;
4863 attrs[LFACE_HEIGHT_INDEX] = make_number (pt);
4864 new_face_id = lookup_face (f, attrs);
4865 new_face = FACE_FROM_ID (f, new_face_id);
4867 /* If height changes, count that as one step. */
4868 if ((delta < 0 && FONT_HEIGHT (new_face->font) < last_height)
4869 || (delta > 0 && FONT_HEIGHT (new_face->font) > last_height))
4871 --steps;
4872 last_height = FONT_HEIGHT (new_face->font);
4873 last_pt = pt;
4877 return new_face_id;
4879 #else /* not HAVE_WINDOW_SYSTEM */
4881 return face_id;
4883 #endif /* not HAVE_WINDOW_SYSTEM */
4887 /* Return a face for charset ASCII that is like the face with id
4888 FACE_ID on frame F, but has height HEIGHT. */
4891 face_with_height (f, face_id, height)
4892 struct frame *f;
4893 int face_id;
4894 int height;
4896 #ifdef HAVE_WINDOW_SYSTEM
4897 struct face *face;
4898 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4900 if (FRAME_TERMCAP_P (f)
4901 || height <= 0)
4902 return face_id;
4904 face = FACE_FROM_ID (f, face_id);
4905 bcopy (face->lface, attrs, sizeof attrs);
4906 attrs[LFACE_HEIGHT_INDEX] = make_number (height);
4907 face_id = lookup_face (f, attrs);
4908 #endif /* HAVE_WINDOW_SYSTEM */
4910 return face_id;
4914 /* Return the face id of the realized face for named face SYMBOL on
4915 frame F suitable for displaying ASCII characters, and use
4916 attributes of the face FACE_ID for attributes that aren't
4917 completely specified by SYMBOL. This is like lookup_named_face,
4918 except that the default attributes come from FACE_ID, not from the
4919 default face. FACE_ID is assumed to be already realized. */
4922 lookup_derived_face (f, symbol, face_id, signal_p)
4923 struct frame *f;
4924 Lisp_Object symbol;
4925 int face_id;
4926 int signal_p;
4928 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4929 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
4930 struct face *default_face = FACE_FROM_ID (f, face_id);
4932 if (!default_face)
4933 abort ();
4935 get_lface_attributes (f, symbol, symbol_attrs, signal_p, 0);
4936 bcopy (default_face->lface, attrs, sizeof attrs);
4937 merge_face_vectors (f, symbol_attrs, attrs, 0);
4938 return lookup_face (f, attrs);
4941 DEFUN ("face-attributes-as-vector", Fface_attributes_as_vector,
4942 Sface_attributes_as_vector, 1, 1, 0,
4943 doc: /* Return a vector of face attributes corresponding to PLIST. */)
4944 (plist)
4945 Lisp_Object plist;
4947 Lisp_Object lface;
4948 lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
4949 Qunspecified);
4950 merge_face_ref (XFRAME (selected_frame), plist, XVECTOR (lface)->contents,
4951 1, 0);
4952 return lface;
4957 /***********************************************************************
4958 Face capability testing
4959 ***********************************************************************/
4962 /* If the distance (as returned by color_distance) between two colors is
4963 less than this, then they are considered the same, for determining
4964 whether a color is supported or not. The range of values is 0-65535. */
4966 #define TTY_SAME_COLOR_THRESHOLD 10000
4968 #ifdef HAVE_WINDOW_SYSTEM
4970 /* Return non-zero if all the face attributes in ATTRS are supported
4971 on the window-system frame F.
4973 The definition of `supported' is somewhat heuristic, but basically means
4974 that a face containing all the attributes in ATTRS, when merged with the
4975 default face for display, can be represented in a way that's
4977 \(1) different in appearance than the default face, and
4978 \(2) `close in spirit' to what the attributes specify, if not exact. */
4980 static int
4981 x_supports_face_attributes_p (f, attrs, def_face)
4982 struct frame *f;
4983 Lisp_Object *attrs;
4984 struct face *def_face;
4986 Lisp_Object *def_attrs = def_face->lface;
4988 /* Check that other specified attributes are different that the default
4989 face. */
4990 if ((!UNSPECIFIEDP (attrs[LFACE_UNDERLINE_INDEX])
4991 && face_attr_equal_p (attrs[LFACE_UNDERLINE_INDEX],
4992 def_attrs[LFACE_UNDERLINE_INDEX]))
4993 || (!UNSPECIFIEDP (attrs[LFACE_INVERSE_INDEX])
4994 && face_attr_equal_p (attrs[LFACE_INVERSE_INDEX],
4995 def_attrs[LFACE_INVERSE_INDEX]))
4996 || (!UNSPECIFIEDP (attrs[LFACE_FOREGROUND_INDEX])
4997 && face_attr_equal_p (attrs[LFACE_FOREGROUND_INDEX],
4998 def_attrs[LFACE_FOREGROUND_INDEX]))
4999 || (!UNSPECIFIEDP (attrs[LFACE_BACKGROUND_INDEX])
5000 && face_attr_equal_p (attrs[LFACE_BACKGROUND_INDEX],
5001 def_attrs[LFACE_BACKGROUND_INDEX]))
5002 || (!UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
5003 && face_attr_equal_p (attrs[LFACE_STIPPLE_INDEX],
5004 def_attrs[LFACE_STIPPLE_INDEX]))
5005 || (!UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
5006 && face_attr_equal_p (attrs[LFACE_OVERLINE_INDEX],
5007 def_attrs[LFACE_OVERLINE_INDEX]))
5008 || (!UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
5009 && face_attr_equal_p (attrs[LFACE_STRIKE_THROUGH_INDEX],
5010 def_attrs[LFACE_STRIKE_THROUGH_INDEX]))
5011 || (!UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
5012 && face_attr_equal_p (attrs[LFACE_BOX_INDEX],
5013 def_attrs[LFACE_BOX_INDEX])))
5014 return 0;
5016 /* Check font-related attributes, as those are the most commonly
5017 "unsupported" on a window-system (because of missing fonts). */
5018 if (!UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
5019 || !UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
5020 || !UNSPECIFIEDP (attrs[LFACE_WEIGHT_INDEX])
5021 || !UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX])
5022 || !UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX]))
5024 int face_id;
5025 struct face *face;
5026 Lisp_Object merged_attrs[LFACE_VECTOR_SIZE];
5027 int i;
5029 bcopy (def_attrs, merged_attrs, sizeof merged_attrs);
5031 merge_face_vectors (f, attrs, merged_attrs, 0);
5033 face_id = lookup_face (f, merged_attrs);
5034 face = FACE_FROM_ID (f, face_id);
5036 if (! face)
5037 error ("Cannot make face");
5039 /* If the font is the same, then not supported. */
5040 if (face->font == def_face->font)
5041 return 0;
5042 for (i = FONT_TYPE_INDEX; i <= FONT_SIZE_INDEX; i++)
5043 if (! EQ (face->font->props[i], def_face->font->props[i]))
5045 Lisp_Object s1, s2;
5047 if (i < FONT_FOUNDRY_INDEX || i > FONT_REGISTRY_INDEX
5048 || face->font->driver->case_sensitive)
5049 return 1;
5050 s1 = SYMBOL_NAME (face->font->props[i]);
5051 s2 = SYMBOL_NAME (def_face->font->props[i]);
5052 if (! EQ (Fcompare_strings (s1, make_number (0), Qnil,
5053 s2, make_number (0), Qnil, Qt), Qt))
5054 return 1;
5056 return 0;
5059 /* Everything checks out, this face is supported. */
5060 return 1;
5063 #endif /* HAVE_WINDOW_SYSTEM */
5065 /* Return non-zero if all the face attributes in ATTRS are supported
5066 on the tty frame F.
5068 The definition of `supported' is somewhat heuristic, but basically means
5069 that a face containing all the attributes in ATTRS, when merged
5070 with the default face for display, can be represented in a way that's
5072 \(1) different in appearance than the default face, and
5073 \(2) `close in spirit' to what the attributes specify, if not exact.
5075 Point (2) implies that a `:weight black' attribute will be satisfied
5076 by any terminal that can display bold, and a `:foreground "yellow"' as
5077 long as the terminal can display a yellowish color, but `:slant italic'
5078 will _not_ be satisfied by the tty display code's automatic
5079 substitution of a `dim' face for italic. */
5081 static int
5082 tty_supports_face_attributes_p (f, attrs, def_face)
5083 struct frame *f;
5084 Lisp_Object *attrs;
5085 struct face *def_face;
5087 int weight;
5088 Lisp_Object val, fg, bg;
5089 XColor fg_tty_color, fg_std_color;
5090 XColor bg_tty_color, bg_std_color;
5091 unsigned test_caps = 0;
5092 Lisp_Object *def_attrs = def_face->lface;
5095 /* First check some easy-to-check stuff; ttys support none of the
5096 following attributes, so we can just return false if any are requested
5097 (even if `nominal' values are specified, we should still return false,
5098 as that will be the same value that the default face uses). We
5099 consider :slant unsupportable on ttys, even though the face code
5100 actually `fakes' them using a dim attribute if possible. This is
5101 because the faked result is too different from what the face
5102 specifies. */
5103 if (!UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
5104 || !UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
5105 || !UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
5106 || !UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
5107 || !UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
5108 || !UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
5109 || !UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
5110 || !UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX]))
5111 return 0;
5114 /* Test for terminal `capabilities' (non-color character attributes). */
5116 /* font weight (bold/dim) */
5117 weight = FONT_WEIGHT_NAME_NUMERIC (attrs[LFACE_WEIGHT_INDEX]);
5118 if (weight >= 0)
5120 int def_weight = FONT_WEIGHT_NAME_NUMERIC (def_attrs[LFACE_WEIGHT_INDEX]);
5122 if (weight > 100)
5124 if (def_weight > 100)
5125 return 0; /* same as default */
5126 test_caps = TTY_CAP_BOLD;
5128 else if (weight < 100)
5130 if (def_weight < 100)
5131 return 0; /* same as default */
5132 test_caps = TTY_CAP_DIM;
5134 else if (def_weight == 100)
5135 return 0; /* same as default */
5138 /* underlining */
5139 val = attrs[LFACE_UNDERLINE_INDEX];
5140 if (!UNSPECIFIEDP (val))
5142 if (STRINGP (val))
5143 return 0; /* ttys can't use colored underlines */
5144 else if (face_attr_equal_p (val, def_attrs[LFACE_UNDERLINE_INDEX]))
5145 return 0; /* same as default */
5146 else
5147 test_caps |= TTY_CAP_UNDERLINE;
5150 /* inverse video */
5151 val = attrs[LFACE_INVERSE_INDEX];
5152 if (!UNSPECIFIEDP (val))
5154 if (face_attr_equal_p (val, def_attrs[LFACE_INVERSE_INDEX]))
5155 return 0; /* same as default */
5156 else
5157 test_caps |= TTY_CAP_INVERSE;
5161 /* Color testing. */
5163 /* Default the color indices in FG_TTY_COLOR and BG_TTY_COLOR, since
5164 we use them when calling `tty_capable_p' below, even if the face
5165 specifies no colors. */
5166 fg_tty_color.pixel = FACE_TTY_DEFAULT_FG_COLOR;
5167 bg_tty_color.pixel = FACE_TTY_DEFAULT_BG_COLOR;
5169 /* Check if foreground color is close enough. */
5170 fg = attrs[LFACE_FOREGROUND_INDEX];
5171 if (STRINGP (fg))
5173 Lisp_Object def_fg = def_attrs[LFACE_FOREGROUND_INDEX];
5175 if (face_attr_equal_p (fg, def_fg))
5176 return 0; /* same as default */
5177 else if (! tty_lookup_color (f, fg, &fg_tty_color, &fg_std_color))
5178 return 0; /* not a valid color */
5179 else if (color_distance (&fg_tty_color, &fg_std_color)
5180 > TTY_SAME_COLOR_THRESHOLD)
5181 return 0; /* displayed color is too different */
5182 else
5183 /* Make sure the color is really different than the default. */
5185 XColor def_fg_color;
5186 if (tty_lookup_color (f, def_fg, &def_fg_color, 0)
5187 && (color_distance (&fg_tty_color, &def_fg_color)
5188 <= TTY_SAME_COLOR_THRESHOLD))
5189 return 0;
5193 /* Check if background color is close enough. */
5194 bg = attrs[LFACE_BACKGROUND_INDEX];
5195 if (STRINGP (bg))
5197 Lisp_Object def_bg = def_attrs[LFACE_BACKGROUND_INDEX];
5199 if (face_attr_equal_p (bg, def_bg))
5200 return 0; /* same as default */
5201 else if (! tty_lookup_color (f, bg, &bg_tty_color, &bg_std_color))
5202 return 0; /* not a valid color */
5203 else if (color_distance (&bg_tty_color, &bg_std_color)
5204 > TTY_SAME_COLOR_THRESHOLD)
5205 return 0; /* displayed color is too different */
5206 else
5207 /* Make sure the color is really different than the default. */
5209 XColor def_bg_color;
5210 if (tty_lookup_color (f, def_bg, &def_bg_color, 0)
5211 && (color_distance (&bg_tty_color, &def_bg_color)
5212 <= TTY_SAME_COLOR_THRESHOLD))
5213 return 0;
5217 /* If both foreground and background are requested, see if the
5218 distance between them is OK. We just check to see if the distance
5219 between the tty's foreground and background is close enough to the
5220 distance between the standard foreground and background. */
5221 if (STRINGP (fg) && STRINGP (bg))
5223 int delta_delta
5224 = (color_distance (&fg_std_color, &bg_std_color)
5225 - color_distance (&fg_tty_color, &bg_tty_color));
5226 if (delta_delta > TTY_SAME_COLOR_THRESHOLD
5227 || delta_delta < -TTY_SAME_COLOR_THRESHOLD)
5228 return 0;
5232 /* See if the capabilities we selected above are supported, with the
5233 given colors. */
5234 if (test_caps != 0 &&
5235 ! tty_capable_p (FRAME_TTY (f), test_caps, fg_tty_color.pixel, bg_tty_color.pixel))
5236 return 0;
5239 /* Hmmm, everything checks out, this terminal must support this face. */
5240 return 1;
5244 DEFUN ("display-supports-face-attributes-p",
5245 Fdisplay_supports_face_attributes_p, Sdisplay_supports_face_attributes_p,
5246 1, 2, 0,
5247 doc: /* Return non-nil if all the face attributes in ATTRIBUTES are supported.
5248 The optional argument DISPLAY can be a display name, a frame, or
5249 nil (meaning the selected frame's display).
5251 The definition of `supported' is somewhat heuristic, but basically means
5252 that a face containing all the attributes in ATTRIBUTES, when merged
5253 with the default face for display, can be represented in a way that's
5255 \(1) different in appearance than the default face, and
5256 \(2) `close in spirit' to what the attributes specify, if not exact.
5258 Point (2) implies that a `:weight black' attribute will be satisfied by
5259 any display that can display bold, and a `:foreground \"yellow\"' as long
5260 as it can display a yellowish color, but `:slant italic' will _not_ be
5261 satisfied by the tty display code's automatic substitution of a `dim'
5262 face for italic. */)
5263 (attributes, display)
5264 Lisp_Object attributes, display;
5266 int supports = 0, i;
5267 Lisp_Object frame;
5268 struct frame *f;
5269 struct face *def_face;
5270 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5272 if (noninteractive || !initialized)
5273 /* We may not be able to access low-level face information in batch
5274 mode, or before being dumped, and this function is not going to
5275 be very useful in those cases anyway, so just give up. */
5276 return Qnil;
5278 if (NILP (display))
5279 frame = selected_frame;
5280 else if (FRAMEP (display))
5281 frame = display;
5282 else
5284 /* Find any frame on DISPLAY. */
5285 Lisp_Object fl_tail;
5287 frame = Qnil;
5288 for (fl_tail = Vframe_list; CONSP (fl_tail); fl_tail = XCDR (fl_tail))
5290 frame = XCAR (fl_tail);
5291 if (!NILP (Fequal (Fcdr (Fassq (Qdisplay,
5292 XFRAME (frame)->param_alist)),
5293 display)))
5294 break;
5298 CHECK_LIVE_FRAME (frame);
5299 f = XFRAME (frame);
5301 for (i = 0; i < LFACE_VECTOR_SIZE; i++)
5302 attrs[i] = Qunspecified;
5303 merge_face_ref (f, attributes, attrs, 1, 0);
5305 def_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5306 if (def_face == NULL)
5308 if (! realize_basic_faces (f))
5309 error ("Cannot realize default face");
5310 def_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5311 if (def_face == NULL)
5312 abort (); /* realize_basic_faces must have set it up */
5315 /* Dispatch to the appropriate handler. */
5316 if (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
5317 supports = tty_supports_face_attributes_p (f, attrs, def_face);
5318 #ifdef HAVE_WINDOW_SYSTEM
5319 else
5320 supports = x_supports_face_attributes_p (f, attrs, def_face);
5321 #endif
5323 return supports ? Qt : Qnil;
5327 /***********************************************************************
5328 Font selection
5329 ***********************************************************************/
5331 DEFUN ("internal-set-font-selection-order",
5332 Finternal_set_font_selection_order,
5333 Sinternal_set_font_selection_order, 1, 1, 0,
5334 doc: /* Set font selection order for face font selection to ORDER.
5335 ORDER must be a list of length 4 containing the symbols `:width',
5336 `:height', `:weight', and `:slant'. Face attributes appearing
5337 first in ORDER are matched first, e.g. if `:height' appears before
5338 `:weight' in ORDER, font selection first tries to find a font with
5339 a suitable height, and then tries to match the font weight.
5340 Value is ORDER. */)
5341 (order)
5342 Lisp_Object order;
5344 Lisp_Object list;
5345 int i;
5346 int indices[DIM (font_sort_order)];
5348 CHECK_LIST (order);
5349 bzero (indices, sizeof indices);
5350 i = 0;
5352 for (list = order;
5353 CONSP (list) && i < DIM (indices);
5354 list = XCDR (list), ++i)
5356 Lisp_Object attr = XCAR (list);
5357 int xlfd;
5359 if (EQ (attr, QCwidth))
5360 xlfd = XLFD_SWIDTH;
5361 else if (EQ (attr, QCheight))
5362 xlfd = XLFD_POINT_SIZE;
5363 else if (EQ (attr, QCweight))
5364 xlfd = XLFD_WEIGHT;
5365 else if (EQ (attr, QCslant))
5366 xlfd = XLFD_SLANT;
5367 else
5368 break;
5370 if (indices[i] != 0)
5371 break;
5372 indices[i] = xlfd;
5375 if (!NILP (list) || i != DIM (indices))
5376 signal_error ("Invalid font sort order", order);
5377 for (i = 0; i < DIM (font_sort_order); ++i)
5378 if (indices[i] == 0)
5379 signal_error ("Invalid font sort order", order);
5381 if (bcmp (indices, font_sort_order, sizeof indices) != 0)
5383 bcopy (indices, font_sort_order, sizeof font_sort_order);
5384 free_all_realized_faces (Qnil);
5387 font_update_sort_order (font_sort_order);
5389 return Qnil;
5393 DEFUN ("internal-set-alternative-font-family-alist",
5394 Finternal_set_alternative_font_family_alist,
5395 Sinternal_set_alternative_font_family_alist, 1, 1, 0,
5396 doc: /* Define alternative font families to try in face font selection.
5397 ALIST is an alist of (FAMILY ALTERNATIVE1 ALTERNATIVE2 ...) entries.
5398 Each ALTERNATIVE is tried in order if no fonts of font family FAMILY can
5399 be found. Value is ALIST. */)
5400 (alist)
5401 Lisp_Object alist;
5403 CHECK_LIST (alist);
5404 Vface_alternative_font_family_alist = alist;
5405 free_all_realized_faces (Qnil);
5406 return alist;
5410 DEFUN ("internal-set-alternative-font-registry-alist",
5411 Finternal_set_alternative_font_registry_alist,
5412 Sinternal_set_alternative_font_registry_alist, 1, 1, 0,
5413 doc: /* Define alternative font registries to try in face font selection.
5414 ALIST is an alist of (REGISTRY ALTERNATIVE1 ALTERNATIVE2 ...) entries.
5415 Each ALTERNATIVE is tried in order if no fonts of font registry REGISTRY can
5416 be found. Value is ALIST. */)
5417 (alist)
5418 Lisp_Object alist;
5420 CHECK_LIST (alist);
5421 Vface_alternative_font_registry_alist = alist;
5422 free_all_realized_faces (Qnil);
5423 return alist;
5427 #ifdef HAVE_WINDOW_SYSTEM
5429 /* Ignore the difference of font point size less than this value. */
5431 #define FONT_POINT_SIZE_QUANTUM 5
5433 /* Return the fontset id of the base fontset name or alias name given
5434 by the fontset attribute of ATTRS. Value is -1 if the fontset
5435 attribute of ATTRS doesn't name a fontset. */
5437 static int
5438 face_fontset (attrs)
5439 Lisp_Object *attrs;
5441 Lisp_Object name;
5443 name = attrs[LFACE_FONTSET_INDEX];
5444 if (!STRINGP (name))
5445 return -1;
5446 return fs_query_fontset (name, 0);
5449 #endif /* HAVE_WINDOW_SYSTEM */
5453 /***********************************************************************
5454 Face Realization
5455 ***********************************************************************/
5457 /* Realize basic faces on frame F. Value is zero if frame parameters
5458 of F don't contain enough information needed to realize the default
5459 face. */
5461 static int
5462 realize_basic_faces (f)
5463 struct frame *f;
5465 int success_p = 0;
5466 int count = SPECPDL_INDEX ();
5468 /* Block input here so that we won't be surprised by an X expose
5469 event, for instance, without having the faces set up. */
5470 BLOCK_INPUT;
5471 specbind (Qscalable_fonts_allowed, Qt);
5473 if (realize_default_face (f))
5475 realize_named_face (f, Qmode_line, MODE_LINE_FACE_ID);
5476 realize_named_face (f, Qmode_line_inactive, MODE_LINE_INACTIVE_FACE_ID);
5477 realize_named_face (f, Qtool_bar, TOOL_BAR_FACE_ID);
5478 realize_named_face (f, Qfringe, FRINGE_FACE_ID);
5479 realize_named_face (f, Qheader_line, HEADER_LINE_FACE_ID);
5480 realize_named_face (f, Qscroll_bar, SCROLL_BAR_FACE_ID);
5481 realize_named_face (f, Qborder, BORDER_FACE_ID);
5482 realize_named_face (f, Qcursor, CURSOR_FACE_ID);
5483 realize_named_face (f, Qmouse, MOUSE_FACE_ID);
5484 realize_named_face (f, Qmenu, MENU_FACE_ID);
5485 realize_named_face (f, Qvertical_border, VERTICAL_BORDER_FACE_ID);
5487 /* Reflect changes in the `menu' face in menu bars. */
5488 if (FRAME_FACE_CACHE (f)->menu_face_changed_p)
5490 FRAME_FACE_CACHE (f)->menu_face_changed_p = 0;
5491 #ifdef USE_X_TOOLKIT
5492 if (FRAME_WINDOW_P (f))
5493 x_update_menu_appearance (f);
5494 #endif
5497 success_p = 1;
5500 unbind_to (count, Qnil);
5501 UNBLOCK_INPUT;
5502 return success_p;
5506 /* Realize the default face on frame F. If the face is not fully
5507 specified, make it fully-specified. Attributes of the default face
5508 that are not explicitly specified are taken from frame parameters. */
5510 static int
5511 realize_default_face (f)
5512 struct frame *f;
5514 struct face_cache *c = FRAME_FACE_CACHE (f);
5515 Lisp_Object lface;
5516 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5517 struct face *face;
5519 /* If the `default' face is not yet known, create it. */
5520 lface = lface_from_face_name (f, Qdefault, 0);
5521 if (NILP (lface))
5523 Lisp_Object frame;
5524 XSETFRAME (frame, f);
5525 lface = Finternal_make_lisp_face (Qdefault, frame);
5528 #ifdef HAVE_WINDOW_SYSTEM
5529 if (FRAME_WINDOW_P (f))
5531 Lisp_Object font_object;
5533 XSETFONT (font_object, FRAME_FONT (f));
5534 set_lface_from_font (f, lface, font_object, f->default_face_done_p);
5535 LFACE_FONTSET (lface) = fontset_name (FRAME_FONTSET (f));
5536 f->default_face_done_p = 1;
5538 #endif /* HAVE_WINDOW_SYSTEM */
5540 if (!FRAME_WINDOW_P (f))
5542 LFACE_FAMILY (lface) = build_string ("default");
5543 LFACE_SWIDTH (lface) = Qnormal;
5544 LFACE_HEIGHT (lface) = make_number (1);
5545 if (UNSPECIFIEDP (LFACE_WEIGHT (lface)))
5546 LFACE_WEIGHT (lface) = Qnormal;
5547 if (UNSPECIFIEDP (LFACE_SLANT (lface)))
5548 LFACE_SLANT (lface) = Qnormal;
5551 if (UNSPECIFIEDP (LFACE_UNDERLINE (lface)))
5552 LFACE_UNDERLINE (lface) = Qnil;
5554 if (UNSPECIFIEDP (LFACE_OVERLINE (lface)))
5555 LFACE_OVERLINE (lface) = Qnil;
5557 if (UNSPECIFIEDP (LFACE_STRIKE_THROUGH (lface)))
5558 LFACE_STRIKE_THROUGH (lface) = Qnil;
5560 if (UNSPECIFIEDP (LFACE_BOX (lface)))
5561 LFACE_BOX (lface) = Qnil;
5563 if (UNSPECIFIEDP (LFACE_INVERSE (lface)))
5564 LFACE_INVERSE (lface) = Qnil;
5566 if (UNSPECIFIEDP (LFACE_FOREGROUND (lface)))
5568 /* This function is called so early that colors are not yet
5569 set in the frame parameter list. */
5570 Lisp_Object color = Fassq (Qforeground_color, f->param_alist);
5572 if (CONSP (color) && STRINGP (XCDR (color)))
5573 LFACE_FOREGROUND (lface) = XCDR (color);
5574 else if (FRAME_WINDOW_P (f))
5575 return 0;
5576 else if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
5577 LFACE_FOREGROUND (lface) = build_string (unspecified_fg);
5578 else
5579 abort ();
5582 if (UNSPECIFIEDP (LFACE_BACKGROUND (lface)))
5584 /* This function is called so early that colors are not yet
5585 set in the frame parameter list. */
5586 Lisp_Object color = Fassq (Qbackground_color, f->param_alist);
5587 if (CONSP (color) && STRINGP (XCDR (color)))
5588 LFACE_BACKGROUND (lface) = XCDR (color);
5589 else if (FRAME_WINDOW_P (f))
5590 return 0;
5591 else if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
5592 LFACE_BACKGROUND (lface) = build_string (unspecified_bg);
5593 else
5594 abort ();
5597 if (UNSPECIFIEDP (LFACE_STIPPLE (lface)))
5598 LFACE_STIPPLE (lface) = Qnil;
5600 /* Realize the face; it must be fully-specified now. */
5601 xassert (lface_fully_specified_p (XVECTOR (lface)->contents));
5602 check_lface (lface);
5603 bcopy (XVECTOR (lface)->contents, attrs, sizeof attrs);
5604 face = realize_face (c, attrs, DEFAULT_FACE_ID);
5606 #ifdef HAVE_WINDOW_SYSTEM
5607 #ifdef HAVE_X_WINDOWS
5608 if (FRAME_X_P (f) && face->font != FRAME_FONT (f))
5610 /* This can happen when making a frame on a display that does
5611 not support the default font. */
5612 if (!face->font)
5613 return 0;
5615 /* Otherwise, the font specified for the frame was not
5616 acceptable as a font for the default face (perhaps because
5617 auto-scaled fonts are rejected), so we must adjust the frame
5618 font. */
5619 x_set_font (f, LFACE_FONT (lface), Qnil);
5621 #endif /* HAVE_X_WINDOWS */
5622 #endif /* HAVE_WINDOW_SYSTEM */
5623 return 1;
5627 /* Realize basic faces other than the default face in face cache C.
5628 SYMBOL is the face name, ID is the face id the realized face must
5629 have. The default face must have been realized already. */
5631 static void
5632 realize_named_face (f, symbol, id)
5633 struct frame *f;
5634 Lisp_Object symbol;
5635 int id;
5637 struct face_cache *c = FRAME_FACE_CACHE (f);
5638 Lisp_Object lface = lface_from_face_name (f, symbol, 0);
5639 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5640 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
5641 struct face *new_face;
5643 /* The default face must exist and be fully specified. */
5644 get_lface_attributes_no_remap (f, Qdefault, attrs, 1);
5645 check_lface_attrs (attrs);
5646 xassert (lface_fully_specified_p (attrs));
5648 /* If SYMBOL isn't know as a face, create it. */
5649 if (NILP (lface))
5651 Lisp_Object frame;
5652 XSETFRAME (frame, f);
5653 lface = Finternal_make_lisp_face (symbol, frame);
5656 /* Merge SYMBOL's face with the default face. */
5657 get_lface_attributes_no_remap (f, symbol, symbol_attrs, 1);
5658 merge_face_vectors (f, symbol_attrs, attrs, 0);
5660 /* Realize the face. */
5661 new_face = realize_face (c, attrs, id);
5665 /* Realize the fully-specified face with attributes ATTRS in face
5666 cache CACHE for ASCII characters. If FORMER_FACE_ID is
5667 non-negative, it is an ID of face to remove before caching the new
5668 face. Value is a pointer to the newly created realized face. */
5670 static struct face *
5671 realize_face (cache, attrs, former_face_id)
5672 struct face_cache *cache;
5673 Lisp_Object *attrs;
5674 int former_face_id;
5676 struct face *face;
5678 /* LFACE must be fully specified. */
5679 xassert (cache != NULL);
5680 check_lface_attrs (attrs);
5682 if (former_face_id >= 0 && cache->used > former_face_id)
5684 /* Remove the former face. */
5685 struct face *former_face = cache->faces_by_id[former_face_id];
5686 uncache_face (cache, former_face);
5687 free_realized_face (cache->f, former_face);
5690 if (FRAME_WINDOW_P (cache->f))
5691 face = realize_x_face (cache, attrs);
5692 else if (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f))
5693 face = realize_tty_face (cache, attrs);
5694 else if (FRAME_INITIAL_P (cache->f))
5696 /* Create a dummy face. */
5697 face = make_realized_face (attrs);
5699 else
5700 abort ();
5702 /* Insert the new face. */
5703 cache_face (cache, face, lface_hash (attrs));
5704 return face;
5708 #ifdef HAVE_WINDOW_SYSTEM
5709 /* Realize the fully-specified face that uses FONT-OBJECT and has the
5710 same attributes as BASE_FACE except for the font on frame F.
5711 FONT-OBJECT may be nil, in which case, realized a face of
5712 no-font. */
5714 static struct face *
5715 realize_non_ascii_face (f, font_object, base_face)
5716 struct frame *f;
5717 Lisp_Object font_object;
5718 struct face *base_face;
5720 struct face_cache *cache = FRAME_FACE_CACHE (f);
5721 struct face *face;
5723 face = (struct face *) xmalloc (sizeof *face);
5724 *face = *base_face;
5725 face->gc = 0;
5726 face->extra = NULL;
5727 face->overstrike
5728 = (! NILP (font_object)
5729 && FONT_WEIGHT_NAME_NUMERIC (face->lface[LFACE_WEIGHT_INDEX]) > 100
5730 && FONT_WEIGHT_NUMERIC (font_object) <= 100);
5732 /* Don't try to free the colors copied bitwise from BASE_FACE. */
5733 face->colors_copied_bitwise_p = 1;
5734 face->font = NILP (font_object) ? NULL : XFONT_OBJECT (font_object);
5735 face->gc = 0;
5737 cache_face (cache, face, face->hash);
5739 return face;
5741 #endif /* HAVE_WINDOW_SYSTEM */
5744 /* Realize the fully-specified face with attributes ATTRS in face
5745 cache CACHE for ASCII characters. Do it for X frame CACHE->f. If
5746 the new face doesn't share font with the default face, a fontname
5747 is allocated from the heap and set in `font_name' of the new face,
5748 but it is not yet loaded here. Value is a pointer to the newly
5749 created realized face. */
5751 static struct face *
5752 realize_x_face (cache, attrs)
5753 struct face_cache *cache;
5754 Lisp_Object *attrs;
5756 struct face *face = NULL;
5757 #ifdef HAVE_WINDOW_SYSTEM
5758 struct face *default_face;
5759 struct frame *f;
5760 Lisp_Object stipple, overline, strike_through, box;
5762 xassert (FRAME_WINDOW_P (cache->f));
5764 /* Allocate a new realized face. */
5765 face = make_realized_face (attrs);
5766 face->ascii_face = face;
5768 f = cache->f;
5770 /* Determine the font to use. Most of the time, the font will be
5771 the same as the font of the default face, so try that first. */
5772 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5773 if (default_face
5774 && lface_same_font_attributes_p (default_face->lface, attrs))
5776 face->font = default_face->font;
5777 face->fontset = make_fontset_for_ascii_face (f, -1, face);
5779 else
5781 /* If the face attribute ATTRS specifies a fontset, use it as
5782 the base of a new realized fontset. Otherwise, use the same
5783 base fontset as of the default face. The base determines
5784 registry and encoding of a font. It may also determine
5785 foundry and family. The other fields of font name pattern
5786 are constructed from ATTRS. */
5787 int fontset = face_fontset (attrs);
5789 /* If we are realizing the default face, ATTRS should specify a
5790 fontset. In other words, if FONTSET is -1, we are not
5791 realizing the default face, thus the default face should have
5792 already been realized. */
5793 if (fontset == -1)
5794 fontset = default_face->fontset;
5795 if (fontset == -1)
5796 abort ();
5797 if (! FONT_OBJECT_P (attrs[LFACE_FONT_INDEX]))
5798 attrs[LFACE_FONT_INDEX]
5799 = font_load_for_lface (f, attrs, attrs[LFACE_FONT_INDEX]);
5800 if (FONT_OBJECT_P (attrs[LFACE_FONT_INDEX]))
5802 face->font = XFONT_OBJECT (attrs[LFACE_FONT_INDEX]);
5803 face->fontset = make_fontset_for_ascii_face (f, fontset, face);
5805 else
5807 face->font = NULL;
5808 face->fontset = -1;
5812 if (face->font
5813 && FONT_WEIGHT_NAME_NUMERIC (attrs[LFACE_WEIGHT_INDEX]) > 100
5814 && FONT_WEIGHT_NUMERIC (attrs[LFACE_FONT_INDEX]) <= 100)
5815 face->overstrike = 1;
5817 /* Load colors, and set remaining attributes. */
5819 load_face_colors (f, face, attrs);
5821 /* Set up box. */
5822 box = attrs[LFACE_BOX_INDEX];
5823 if (STRINGP (box))
5825 /* A simple box of line width 1 drawn in color given by
5826 the string. */
5827 face->box_color = load_color (f, face, attrs[LFACE_BOX_INDEX],
5828 LFACE_BOX_INDEX);
5829 face->box = FACE_SIMPLE_BOX;
5830 face->box_line_width = 1;
5832 else if (INTEGERP (box))
5834 /* Simple box of specified line width in foreground color of the
5835 face. */
5836 xassert (XINT (box) != 0);
5837 face->box = FACE_SIMPLE_BOX;
5838 face->box_line_width = XINT (box);
5839 face->box_color = face->foreground;
5840 face->box_color_defaulted_p = 1;
5842 else if (CONSP (box))
5844 /* `(:width WIDTH :color COLOR :shadow SHADOW)'. SHADOW
5845 being one of `raised' or `sunken'. */
5846 face->box = FACE_SIMPLE_BOX;
5847 face->box_color = face->foreground;
5848 face->box_color_defaulted_p = 1;
5849 face->box_line_width = 1;
5851 while (CONSP (box))
5853 Lisp_Object keyword, value;
5855 keyword = XCAR (box);
5856 box = XCDR (box);
5858 if (!CONSP (box))
5859 break;
5860 value = XCAR (box);
5861 box = XCDR (box);
5863 if (EQ (keyword, QCline_width))
5865 if (INTEGERP (value) && XINT (value) != 0)
5866 face->box_line_width = XINT (value);
5868 else if (EQ (keyword, QCcolor))
5870 if (STRINGP (value))
5872 face->box_color = load_color (f, face, value,
5873 LFACE_BOX_INDEX);
5874 face->use_box_color_for_shadows_p = 1;
5877 else if (EQ (keyword, QCstyle))
5879 if (EQ (value, Qreleased_button))
5880 face->box = FACE_RAISED_BOX;
5881 else if (EQ (value, Qpressed_button))
5882 face->box = FACE_SUNKEN_BOX;
5887 /* Text underline, overline, strike-through. */
5889 if (EQ (attrs[LFACE_UNDERLINE_INDEX], Qt))
5891 /* Use default color (same as foreground color). */
5892 face->underline_p = 1;
5893 face->underline_defaulted_p = 1;
5894 face->underline_color = 0;
5896 else if (STRINGP (attrs[LFACE_UNDERLINE_INDEX]))
5898 /* Use specified color. */
5899 face->underline_p = 1;
5900 face->underline_defaulted_p = 0;
5901 face->underline_color
5902 = load_color (f, face, attrs[LFACE_UNDERLINE_INDEX],
5903 LFACE_UNDERLINE_INDEX);
5905 else if (NILP (attrs[LFACE_UNDERLINE_INDEX]))
5907 face->underline_p = 0;
5908 face->underline_defaulted_p = 0;
5909 face->underline_color = 0;
5912 overline = attrs[LFACE_OVERLINE_INDEX];
5913 if (STRINGP (overline))
5915 face->overline_color
5916 = load_color (f, face, attrs[LFACE_OVERLINE_INDEX],
5917 LFACE_OVERLINE_INDEX);
5918 face->overline_p = 1;
5920 else if (EQ (overline, Qt))
5922 face->overline_color = face->foreground;
5923 face->overline_color_defaulted_p = 1;
5924 face->overline_p = 1;
5927 strike_through = attrs[LFACE_STRIKE_THROUGH_INDEX];
5928 if (STRINGP (strike_through))
5930 face->strike_through_color
5931 = load_color (f, face, attrs[LFACE_STRIKE_THROUGH_INDEX],
5932 LFACE_STRIKE_THROUGH_INDEX);
5933 face->strike_through_p = 1;
5935 else if (EQ (strike_through, Qt))
5937 face->strike_through_color = face->foreground;
5938 face->strike_through_color_defaulted_p = 1;
5939 face->strike_through_p = 1;
5942 stipple = attrs[LFACE_STIPPLE_INDEX];
5943 if (!NILP (stipple))
5944 face->stipple = load_pixmap (f, stipple, &face->pixmap_w, &face->pixmap_h);
5945 #endif /* HAVE_WINDOW_SYSTEM */
5947 return face;
5951 /* Map a specified color of face FACE on frame F to a tty color index.
5952 IDX is either LFACE_FOREGROUND_INDEX or LFACE_BACKGROUND_INDEX, and
5953 specifies which color to map. Set *DEFAULTED to 1 if mapping to the
5954 default foreground/background colors. */
5956 static void
5957 map_tty_color (f, face, idx, defaulted)
5958 struct frame *f;
5959 struct face *face;
5960 enum lface_attribute_index idx;
5961 int *defaulted;
5963 Lisp_Object frame, color, def;
5964 int foreground_p = idx == LFACE_FOREGROUND_INDEX;
5965 unsigned long default_pixel, default_other_pixel, pixel;
5967 xassert (idx == LFACE_FOREGROUND_INDEX || idx == LFACE_BACKGROUND_INDEX);
5969 if (foreground_p)
5971 pixel = default_pixel = FACE_TTY_DEFAULT_FG_COLOR;
5972 default_other_pixel = FACE_TTY_DEFAULT_BG_COLOR;
5974 else
5976 pixel = default_pixel = FACE_TTY_DEFAULT_BG_COLOR;
5977 default_other_pixel = FACE_TTY_DEFAULT_FG_COLOR;
5980 XSETFRAME (frame, f);
5981 color = face->lface[idx];
5983 if (STRINGP (color)
5984 && SCHARS (color)
5985 && CONSP (Vtty_defined_color_alist)
5986 && (def = assq_no_quit (color, call1 (Qtty_color_alist, frame)),
5987 CONSP (def)))
5989 /* Associations in tty-defined-color-alist are of the form
5990 (NAME INDEX R G B). We need the INDEX part. */
5991 pixel = XINT (XCAR (XCDR (def)));
5994 if (pixel == default_pixel && STRINGP (color))
5996 pixel = load_color (f, face, color, idx);
5998 #ifdef MSDOS
5999 /* If the foreground of the default face is the default color,
6000 use the foreground color defined by the frame. */
6001 if (FRAME_MSDOS_P (f))
6003 if (pixel == default_pixel
6004 || pixel == FACE_TTY_DEFAULT_COLOR)
6006 if (foreground_p)
6007 pixel = FRAME_FOREGROUND_PIXEL (f);
6008 else
6009 pixel = FRAME_BACKGROUND_PIXEL (f);
6010 face->lface[idx] = tty_color_name (f, pixel);
6011 *defaulted = 1;
6013 else if (pixel == default_other_pixel)
6015 if (foreground_p)
6016 pixel = FRAME_BACKGROUND_PIXEL (f);
6017 else
6018 pixel = FRAME_FOREGROUND_PIXEL (f);
6019 face->lface[idx] = tty_color_name (f, pixel);
6020 *defaulted = 1;
6023 #endif /* MSDOS */
6026 if (foreground_p)
6027 face->foreground = pixel;
6028 else
6029 face->background = pixel;
6033 /* Realize the fully-specified face with attributes ATTRS in face
6034 cache CACHE for ASCII characters. Do it for TTY frame CACHE->f.
6035 Value is a pointer to the newly created realized face. */
6037 static struct face *
6038 realize_tty_face (cache, attrs)
6039 struct face_cache *cache;
6040 Lisp_Object *attrs;
6042 struct face *face;
6043 int weight, slant;
6044 int face_colors_defaulted = 0;
6045 struct frame *f = cache->f;
6047 /* Frame must be a termcap frame. */
6048 xassert (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f));
6050 /* Allocate a new realized face. */
6051 face = make_realized_face (attrs);
6052 #if 0
6053 face->font_name = FRAME_MSDOS_P (cache->f) ? "ms-dos" : "tty";
6054 #endif
6056 /* Map face attributes to TTY appearances. We map slant to
6057 dimmed text because we want italic text to appear differently
6058 and because dimmed text is probably used infrequently. */
6059 weight = FONT_WEIGHT_NAME_NUMERIC (attrs[LFACE_WEIGHT_INDEX]);
6060 slant = FONT_SLANT_NAME_NUMERIC (attrs[LFACE_SLANT_INDEX]);
6061 if (weight > 100)
6062 face->tty_bold_p = 1;
6063 if (weight < 100 || slant != 100)
6064 face->tty_dim_p = 1;
6065 if (!NILP (attrs[LFACE_UNDERLINE_INDEX]))
6066 face->tty_underline_p = 1;
6067 if (!NILP (attrs[LFACE_INVERSE_INDEX]))
6068 face->tty_reverse_p = 1;
6070 /* Map color names to color indices. */
6071 map_tty_color (f, face, LFACE_FOREGROUND_INDEX, &face_colors_defaulted);
6072 map_tty_color (f, face, LFACE_BACKGROUND_INDEX, &face_colors_defaulted);
6074 /* Swap colors if face is inverse-video. If the colors are taken
6075 from the frame colors, they are already inverted, since the
6076 frame-creation function calls x-handle-reverse-video. */
6077 if (face->tty_reverse_p && !face_colors_defaulted)
6079 unsigned long tem = face->foreground;
6080 face->foreground = face->background;
6081 face->background = tem;
6084 if (tty_suppress_bold_inverse_default_colors_p
6085 && face->tty_bold_p
6086 && face->background == FACE_TTY_DEFAULT_FG_COLOR
6087 && face->foreground == FACE_TTY_DEFAULT_BG_COLOR)
6088 face->tty_bold_p = 0;
6090 return face;
6094 DEFUN ("tty-suppress-bold-inverse-default-colors",
6095 Ftty_suppress_bold_inverse_default_colors,
6096 Stty_suppress_bold_inverse_default_colors, 1, 1, 0,
6097 doc: /* Suppress/allow boldness of faces with inverse default colors.
6098 SUPPRESS non-nil means suppress it.
6099 This affects bold faces on TTYs whose foreground is the default background
6100 color of the display and whose background is the default foreground color.
6101 For such faces, the bold face attribute is ignored if this variable
6102 is non-nil. */)
6103 (suppress)
6104 Lisp_Object suppress;
6106 tty_suppress_bold_inverse_default_colors_p = !NILP (suppress);
6107 ++face_change_count;
6108 return suppress;
6113 /***********************************************************************
6114 Computing Faces
6115 ***********************************************************************/
6117 /* Return the ID of the face to use to display character CH with face
6118 property PROP on frame F in current_buffer. */
6121 compute_char_face (f, ch, prop)
6122 struct frame *f;
6123 int ch;
6124 Lisp_Object prop;
6126 int face_id;
6128 if (NILP (current_buffer->enable_multibyte_characters))
6129 ch = 0;
6131 if (NILP (prop))
6133 struct face *face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
6134 face_id = FACE_FOR_CHAR (f, face, ch, -1, Qnil);
6136 else
6138 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6139 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
6140 bcopy (default_face->lface, attrs, sizeof attrs);
6141 merge_face_ref (f, prop, attrs, 1, 0);
6142 face_id = lookup_face (f, attrs);
6145 return face_id;
6148 /* Return the face ID associated with buffer position POS for
6149 displaying ASCII characters. Return in *ENDPTR the position at
6150 which a different face is needed, as far as text properties and
6151 overlays are concerned. W is a window displaying current_buffer.
6153 REGION_BEG, REGION_END delimit the region, so it can be
6154 highlighted.
6156 LIMIT is a position not to scan beyond. That is to limit the time
6157 this function can take.
6159 If MOUSE is non-zero, use the character's mouse-face, not its face.
6161 The face returned is suitable for displaying ASCII characters. */
6164 face_at_buffer_position (w, pos, region_beg, region_end,
6165 endptr, limit, mouse)
6166 struct window *w;
6167 EMACS_INT pos;
6168 EMACS_INT region_beg, region_end;
6169 EMACS_INT *endptr;
6170 EMACS_INT limit;
6171 int mouse;
6173 struct frame *f = XFRAME (w->frame);
6174 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6175 Lisp_Object prop, position;
6176 int i, noverlays;
6177 Lisp_Object *overlay_vec;
6178 Lisp_Object frame;
6179 EMACS_INT endpos;
6180 Lisp_Object propname = mouse ? Qmouse_face : Qface;
6181 Lisp_Object limit1, end;
6182 struct face *default_face;
6184 /* W must display the current buffer. We could write this function
6185 to use the frame and buffer of W, but right now it doesn't. */
6186 /* xassert (XBUFFER (w->buffer) == current_buffer); */
6188 XSETFRAME (frame, f);
6189 XSETFASTINT (position, pos);
6191 endpos = ZV;
6192 if (pos < region_beg && region_beg < endpos)
6193 endpos = region_beg;
6195 /* Get the `face' or `mouse_face' text property at POS, and
6196 determine the next position at which the property changes. */
6197 prop = Fget_text_property (position, propname, w->buffer);
6198 XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
6199 end = Fnext_single_property_change (position, propname, w->buffer, limit1);
6200 if (INTEGERP (end))
6201 endpos = XINT (end);
6203 /* Look at properties from overlays. */
6205 EMACS_INT next_overlay;
6207 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, &next_overlay, 0);
6208 if (next_overlay < endpos)
6209 endpos = next_overlay;
6212 *endptr = endpos;
6215 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
6216 if (NILP (Vface_remapping_alist))
6217 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
6218 else
6219 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
6221 /* Optimize common cases where we can use the default face. */
6222 if (noverlays == 0
6223 && NILP (prop)
6224 && !(pos >= region_beg && pos < region_end))
6225 return default_face->id;
6227 /* Begin with attributes from the default face. */
6228 bcopy (default_face->lface, attrs, sizeof attrs);
6230 /* Merge in attributes specified via text properties. */
6231 if (!NILP (prop))
6232 merge_face_ref (f, prop, attrs, 1, 0);
6234 /* Now merge the overlay data. */
6235 noverlays = sort_overlays (overlay_vec, noverlays, w);
6236 for (i = 0; i < noverlays; i++)
6238 Lisp_Object oend;
6239 int oendpos;
6241 prop = Foverlay_get (overlay_vec[i], propname);
6242 if (!NILP (prop))
6243 merge_face_ref (f, prop, attrs, 1, 0);
6245 oend = OVERLAY_END (overlay_vec[i]);
6246 oendpos = OVERLAY_POSITION (oend);
6247 if (oendpos < endpos)
6248 endpos = oendpos;
6251 /* If in the region, merge in the region face. */
6252 if (pos >= region_beg && pos < region_end)
6254 merge_named_face (f, Qregion, attrs, 0);
6256 if (region_end < endpos)
6257 endpos = region_end;
6260 *endptr = endpos;
6262 /* Look up a realized face with the given face attributes,
6263 or realize a new one for ASCII characters. */
6264 return lookup_face (f, attrs);
6267 /* Return the face ID at buffer position POS for displaying ASCII
6268 characters associated with overlay strings for overlay OVERLAY.
6270 Like face_at_buffer_position except for OVERLAY. Currently it
6271 simply disregards the `face' properties of all overlays. */
6274 face_for_overlay_string (w, pos, region_beg, region_end,
6275 endptr, limit, mouse, overlay)
6276 struct window *w;
6277 EMACS_INT pos;
6278 EMACS_INT region_beg, region_end;
6279 EMACS_INT *endptr;
6280 EMACS_INT limit;
6281 int mouse;
6282 Lisp_Object overlay;
6284 struct frame *f = XFRAME (w->frame);
6285 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6286 Lisp_Object prop, position;
6287 Lisp_Object frame;
6288 int endpos;
6289 Lisp_Object propname = mouse ? Qmouse_face : Qface;
6290 Lisp_Object limit1, end;
6291 struct face *default_face;
6293 /* W must display the current buffer. We could write this function
6294 to use the frame and buffer of W, but right now it doesn't. */
6295 /* xassert (XBUFFER (w->buffer) == current_buffer); */
6297 XSETFRAME (frame, f);
6298 XSETFASTINT (position, pos);
6300 endpos = ZV;
6301 if (pos < region_beg && region_beg < endpos)
6302 endpos = region_beg;
6304 /* Get the `face' or `mouse_face' text property at POS, and
6305 determine the next position at which the property changes. */
6306 prop = Fget_text_property (position, propname, w->buffer);
6307 XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
6308 end = Fnext_single_property_change (position, propname, w->buffer, limit1);
6309 if (INTEGERP (end))
6310 endpos = XINT (end);
6312 *endptr = endpos;
6314 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
6316 /* Optimize common cases where we can use the default face. */
6317 if (NILP (prop)
6318 && !(pos >= region_beg && pos < region_end))
6319 return DEFAULT_FACE_ID;
6321 /* Begin with attributes from the default face. */
6322 bcopy (default_face->lface, attrs, sizeof attrs);
6324 /* Merge in attributes specified via text properties. */
6325 if (!NILP (prop))
6326 merge_face_ref (f, prop, attrs, 1, 0);
6328 /* If in the region, merge in the region face. */
6329 if (pos >= region_beg && pos < region_end)
6331 merge_named_face (f, Qregion, attrs, 0);
6333 if (region_end < endpos)
6334 endpos = region_end;
6337 *endptr = endpos;
6339 /* Look up a realized face with the given face attributes,
6340 or realize a new one for ASCII characters. */
6341 return lookup_face (f, attrs);
6345 /* Compute the face at character position POS in Lisp string STRING on
6346 window W, for ASCII characters.
6348 If STRING is an overlay string, it comes from position BUFPOS in
6349 current_buffer, otherwise BUFPOS is zero to indicate that STRING is
6350 not an overlay string. W must display the current buffer.
6351 REGION_BEG and REGION_END give the start and end positions of the
6352 region; both are -1 if no region is visible.
6354 BASE_FACE_ID is the id of a face to merge with. For strings coming
6355 from overlays or the `display' property it is the face at BUFPOS.
6357 If MOUSE_P is non-zero, use the character's mouse-face, not its face.
6359 Set *ENDPTR to the next position where to check for faces in
6360 STRING; -1 if the face is constant from POS to the end of the
6361 string.
6363 Value is the id of the face to use. The face returned is suitable
6364 for displaying ASCII characters. */
6367 face_at_string_position (w, string, pos, bufpos, region_beg,
6368 region_end, endptr, base_face_id, mouse_p)
6369 struct window *w;
6370 Lisp_Object string;
6371 EMACS_INT pos, bufpos;
6372 EMACS_INT region_beg, region_end;
6373 EMACS_INT *endptr;
6374 enum face_id base_face_id;
6375 int mouse_p;
6377 Lisp_Object prop, position, end, limit;
6378 struct frame *f = XFRAME (WINDOW_FRAME (w));
6379 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6380 struct face *base_face;
6381 int multibyte_p = STRING_MULTIBYTE (string);
6382 Lisp_Object prop_name = mouse_p ? Qmouse_face : Qface;
6384 /* Get the value of the face property at the current position within
6385 STRING. Value is nil if there is no face property. */
6386 XSETFASTINT (position, pos);
6387 prop = Fget_text_property (position, prop_name, string);
6389 /* Get the next position at which to check for faces. Value of end
6390 is nil if face is constant all the way to the end of the string.
6391 Otherwise it is a string position where to check faces next.
6392 Limit is the maximum position up to which to check for property
6393 changes in Fnext_single_property_change. Strings are usually
6394 short, so set the limit to the end of the string. */
6395 XSETFASTINT (limit, SCHARS (string));
6396 end = Fnext_single_property_change (position, prop_name, string, limit);
6397 if (INTEGERP (end))
6398 *endptr = XFASTINT (end);
6399 else
6400 *endptr = -1;
6402 base_face = FACE_FROM_ID (f, base_face_id);
6403 xassert (base_face);
6405 /* Optimize the default case that there is no face property and we
6406 are not in the region. */
6407 if (NILP (prop)
6408 && (base_face_id != DEFAULT_FACE_ID
6409 /* BUFPOS <= 0 means STRING is not an overlay string, so
6410 that the region doesn't have to be taken into account. */
6411 || bufpos <= 0
6412 || bufpos < region_beg
6413 || bufpos >= region_end)
6414 && (multibyte_p
6415 /* We can't realize faces for different charsets differently
6416 if we don't have fonts, so we can stop here if not working
6417 on a window-system frame. */
6418 || !FRAME_WINDOW_P (f)
6419 || FACE_SUITABLE_FOR_CHAR_P (base_face, 0)))
6420 return base_face->id;
6422 /* Begin with attributes from the base face. */
6423 bcopy (base_face->lface, attrs, sizeof attrs);
6425 /* Merge in attributes specified via text properties. */
6426 if (!NILP (prop))
6427 merge_face_ref (f, prop, attrs, 1, 0);
6429 /* If in the region, merge in the region face. */
6430 if (bufpos
6431 && bufpos >= region_beg
6432 && bufpos < region_end)
6433 merge_named_face (f, Qregion, attrs, 0);
6435 /* Look up a realized face with the given face attributes,
6436 or realize a new one for ASCII characters. */
6437 return lookup_face (f, attrs);
6441 /* Merge a face into a realized face.
6443 F is frame where faces are (to be) realized.
6445 FACE_NAME is named face to merge.
6447 If FACE_NAME is nil, FACE_ID is face_id of realized face to merge.
6449 If FACE_NAME is t, FACE_ID is lface_id of face to merge.
6451 BASE_FACE_ID is realized face to merge into.
6453 Return new face id.
6457 merge_faces (f, face_name, face_id, base_face_id)
6458 struct frame *f;
6459 Lisp_Object face_name;
6460 int face_id, base_face_id;
6462 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6463 struct face *base_face;
6465 base_face = FACE_FROM_ID (f, base_face_id);
6466 if (!base_face)
6467 return base_face_id;
6469 if (EQ (face_name, Qt))
6471 if (face_id < 0 || face_id >= lface_id_to_name_size)
6472 return base_face_id;
6473 face_name = lface_id_to_name[face_id];
6474 face_id = lookup_derived_face (f, face_name, base_face_id, 1);
6475 if (face_id >= 0)
6476 return face_id;
6477 return base_face_id;
6480 /* Begin with attributes from the base face. */
6481 bcopy (base_face->lface, attrs, sizeof attrs);
6483 if (!NILP (face_name))
6485 if (!merge_named_face (f, face_name, attrs, 0))
6486 return base_face_id;
6488 else
6490 struct face *face;
6491 if (face_id < 0)
6492 return base_face_id;
6493 face = FACE_FROM_ID (f, face_id);
6494 if (!face)
6495 return base_face_id;
6496 merge_face_vectors (f, face->lface, attrs, 0);
6499 /* Look up a realized face with the given face attributes,
6500 or realize a new one for ASCII characters. */
6501 return lookup_face (f, attrs);
6505 /***********************************************************************
6506 Tests
6507 ***********************************************************************/
6509 #if GLYPH_DEBUG
6511 /* Print the contents of the realized face FACE to stderr. */
6513 static void
6514 dump_realized_face (face)
6515 struct face *face;
6517 fprintf (stderr, "ID: %d\n", face->id);
6518 #ifdef HAVE_X_WINDOWS
6519 fprintf (stderr, "gc: %ld\n", (long) face->gc);
6520 #endif
6521 fprintf (stderr, "foreground: 0x%lx (%s)\n",
6522 face->foreground,
6523 SDATA (face->lface[LFACE_FOREGROUND_INDEX]));
6524 fprintf (stderr, "background: 0x%lx (%s)\n",
6525 face->background,
6526 SDATA (face->lface[LFACE_BACKGROUND_INDEX]));
6527 if (face->font)
6528 fprintf (stderr, "font_name: %s (%s)\n",
6529 SDATA (face->font->props[FONT_NAME_INDEX]),
6530 SDATA (face->lface[LFACE_FAMILY_INDEX]));
6531 #ifdef HAVE_X_WINDOWS
6532 fprintf (stderr, "font = %p\n", face->font);
6533 #endif
6534 fprintf (stderr, "fontset: %d\n", face->fontset);
6535 fprintf (stderr, "underline: %d (%s)\n",
6536 face->underline_p,
6537 SDATA (Fsymbol_name (face->lface[LFACE_UNDERLINE_INDEX])));
6538 fprintf (stderr, "hash: %d\n", face->hash);
6542 DEFUN ("dump-face", Fdump_face, Sdump_face, 0, 1, 0, doc: /* */)
6544 Lisp_Object n;
6546 if (NILP (n))
6548 int i;
6550 fprintf (stderr, "font selection order: ");
6551 for (i = 0; i < DIM (font_sort_order); ++i)
6552 fprintf (stderr, "%d ", font_sort_order[i]);
6553 fprintf (stderr, "\n");
6555 fprintf (stderr, "alternative fonts: ");
6556 debug_print (Vface_alternative_font_family_alist);
6557 fprintf (stderr, "\n");
6559 for (i = 0; i < FRAME_FACE_CACHE (SELECTED_FRAME ())->used; ++i)
6560 Fdump_face (make_number (i));
6562 else
6564 struct face *face;
6565 CHECK_NUMBER (n);
6566 face = FACE_FROM_ID (SELECTED_FRAME (), XINT (n));
6567 if (face == NULL)
6568 error ("Not a valid face");
6569 dump_realized_face (face);
6572 return Qnil;
6576 DEFUN ("show-face-resources", Fshow_face_resources, Sshow_face_resources,
6577 0, 0, 0, doc: /* */)
6580 fprintf (stderr, "number of colors = %d\n", ncolors_allocated);
6581 fprintf (stderr, "number of pixmaps = %d\n", npixmaps_allocated);
6582 fprintf (stderr, "number of GCs = %d\n", ngcs);
6583 return Qnil;
6586 #endif /* GLYPH_DEBUG != 0 */
6590 /***********************************************************************
6591 Initialization
6592 ***********************************************************************/
6594 void
6595 syms_of_xfaces ()
6597 Qface = intern ("face");
6598 staticpro (&Qface);
6599 Qface_no_inherit = intern ("face-no-inherit");
6600 staticpro (&Qface_no_inherit);
6601 Qbitmap_spec_p = intern ("bitmap-spec-p");
6602 staticpro (&Qbitmap_spec_p);
6603 Qframe_set_background_mode = intern ("frame-set-background-mode");
6604 staticpro (&Qframe_set_background_mode);
6606 /* Lisp face attribute keywords. */
6607 QCfamily = intern (":family");
6608 staticpro (&QCfamily);
6609 QCheight = intern (":height");
6610 staticpro (&QCheight);
6611 QCweight = intern (":weight");
6612 staticpro (&QCweight);
6613 QCslant = intern (":slant");
6614 staticpro (&QCslant);
6615 QCunderline = intern (":underline");
6616 staticpro (&QCunderline);
6617 QCinverse_video = intern (":inverse-video");
6618 staticpro (&QCinverse_video);
6619 QCreverse_video = intern (":reverse-video");
6620 staticpro (&QCreverse_video);
6621 QCforeground = intern (":foreground");
6622 staticpro (&QCforeground);
6623 QCbackground = intern (":background");
6624 staticpro (&QCbackground);
6625 QCstipple = intern (":stipple");
6626 staticpro (&QCstipple);
6627 QCwidth = intern (":width");
6628 staticpro (&QCwidth);
6629 QCfont = intern (":font");
6630 staticpro (&QCfont);
6631 QCfontset = intern (":fontset");
6632 staticpro (&QCfontset);
6633 QCbold = intern (":bold");
6634 staticpro (&QCbold);
6635 QCitalic = intern (":italic");
6636 staticpro (&QCitalic);
6637 QCoverline = intern (":overline");
6638 staticpro (&QCoverline);
6639 QCstrike_through = intern (":strike-through");
6640 staticpro (&QCstrike_through);
6641 QCbox = intern (":box");
6642 staticpro (&QCbox);
6643 QCinherit = intern (":inherit");
6644 staticpro (&QCinherit);
6646 /* Symbols used for Lisp face attribute values. */
6647 QCcolor = intern (":color");
6648 staticpro (&QCcolor);
6649 QCline_width = intern (":line-width");
6650 staticpro (&QCline_width);
6651 QCstyle = intern (":style");
6652 staticpro (&QCstyle);
6653 Qreleased_button = intern ("released-button");
6654 staticpro (&Qreleased_button);
6655 Qpressed_button = intern ("pressed-button");
6656 staticpro (&Qpressed_button);
6657 Qnormal = intern ("normal");
6658 staticpro (&Qnormal);
6659 Qultra_light = intern ("ultra-light");
6660 staticpro (&Qultra_light);
6661 Qextra_light = intern ("extra-light");
6662 staticpro (&Qextra_light);
6663 Qlight = intern ("light");
6664 staticpro (&Qlight);
6665 Qsemi_light = intern ("semi-light");
6666 staticpro (&Qsemi_light);
6667 Qsemi_bold = intern ("semi-bold");
6668 staticpro (&Qsemi_bold);
6669 Qbold = intern ("bold");
6670 staticpro (&Qbold);
6671 Qextra_bold = intern ("extra-bold");
6672 staticpro (&Qextra_bold);
6673 Qultra_bold = intern ("ultra-bold");
6674 staticpro (&Qultra_bold);
6675 Qoblique = intern ("oblique");
6676 staticpro (&Qoblique);
6677 Qitalic = intern ("italic");
6678 staticpro (&Qitalic);
6679 Qreverse_oblique = intern ("reverse-oblique");
6680 staticpro (&Qreverse_oblique);
6681 Qreverse_italic = intern ("reverse-italic");
6682 staticpro (&Qreverse_italic);
6683 Qultra_condensed = intern ("ultra-condensed");
6684 staticpro (&Qultra_condensed);
6685 Qextra_condensed = intern ("extra-condensed");
6686 staticpro (&Qextra_condensed);
6687 Qcondensed = intern ("condensed");
6688 staticpro (&Qcondensed);
6689 Qsemi_condensed = intern ("semi-condensed");
6690 staticpro (&Qsemi_condensed);
6691 Qsemi_expanded = intern ("semi-expanded");
6692 staticpro (&Qsemi_expanded);
6693 Qexpanded = intern ("expanded");
6694 staticpro (&Qexpanded);
6695 Qextra_expanded = intern ("extra-expanded");
6696 staticpro (&Qextra_expanded);
6697 Qultra_expanded = intern ("ultra-expanded");
6698 staticpro (&Qultra_expanded);
6699 Qbackground_color = intern ("background-color");
6700 staticpro (&Qbackground_color);
6701 Qforeground_color = intern ("foreground-color");
6702 staticpro (&Qforeground_color);
6703 Qunspecified = intern ("unspecified");
6704 staticpro (&Qunspecified);
6705 Qignore_defface = intern (":ignore-defface");
6706 staticpro (&Qignore_defface);
6708 Qface_alias = intern ("face-alias");
6709 staticpro (&Qface_alias);
6710 Qdefault = intern ("default");
6711 staticpro (&Qdefault);
6712 Qtool_bar = intern ("tool-bar");
6713 staticpro (&Qtool_bar);
6714 Qregion = intern ("region");
6715 staticpro (&Qregion);
6716 Qfringe = intern ("fringe");
6717 staticpro (&Qfringe);
6718 Qheader_line = intern ("header-line");
6719 staticpro (&Qheader_line);
6720 Qscroll_bar = intern ("scroll-bar");
6721 staticpro (&Qscroll_bar);
6722 Qmenu = intern ("menu");
6723 staticpro (&Qmenu);
6724 Qcursor = intern ("cursor");
6725 staticpro (&Qcursor);
6726 Qborder = intern ("border");
6727 staticpro (&Qborder);
6728 Qmouse = intern ("mouse");
6729 staticpro (&Qmouse);
6730 Qmode_line_inactive = intern ("mode-line-inactive");
6731 staticpro (&Qmode_line_inactive);
6732 Qvertical_border = intern ("vertical-border");
6733 staticpro (&Qvertical_border);
6734 Qtty_color_desc = intern ("tty-color-desc");
6735 staticpro (&Qtty_color_desc);
6736 Qtty_color_standard_values = intern ("tty-color-standard-values");
6737 staticpro (&Qtty_color_standard_values);
6738 Qtty_color_by_index = intern ("tty-color-by-index");
6739 staticpro (&Qtty_color_by_index);
6740 Qtty_color_alist = intern ("tty-color-alist");
6741 staticpro (&Qtty_color_alist);
6742 Qscalable_fonts_allowed = intern ("scalable-fonts-allowed");
6743 staticpro (&Qscalable_fonts_allowed);
6745 Vparam_value_alist = Fcons (Fcons (Qnil, Qnil), Qnil);
6746 staticpro (&Vparam_value_alist);
6747 Vface_alternative_font_family_alist = Qnil;
6748 staticpro (&Vface_alternative_font_family_alist);
6749 Vface_alternative_font_registry_alist = Qnil;
6750 staticpro (&Vface_alternative_font_registry_alist);
6752 defsubr (&Sinternal_make_lisp_face);
6753 defsubr (&Sinternal_lisp_face_p);
6754 defsubr (&Sinternal_set_lisp_face_attribute);
6755 #ifdef HAVE_WINDOW_SYSTEM
6756 defsubr (&Sinternal_set_lisp_face_attribute_from_resource);
6757 #endif
6758 defsubr (&Scolor_gray_p);
6759 defsubr (&Scolor_supported_p);
6760 defsubr (&Sface_attribute_relative_p);
6761 defsubr (&Smerge_face_attribute);
6762 defsubr (&Sinternal_get_lisp_face_attribute);
6763 defsubr (&Sinternal_lisp_face_attribute_values);
6764 defsubr (&Sinternal_lisp_face_equal_p);
6765 defsubr (&Sinternal_lisp_face_empty_p);
6766 defsubr (&Sinternal_copy_lisp_face);
6767 defsubr (&Sinternal_merge_in_global_face);
6768 defsubr (&Sface_font);
6769 defsubr (&Sframe_face_alist);
6770 defsubr (&Sdisplay_supports_face_attributes_p);
6771 defsubr (&Scolor_distance);
6772 defsubr (&Sinternal_set_font_selection_order);
6773 defsubr (&Sinternal_set_alternative_font_family_alist);
6774 defsubr (&Sinternal_set_alternative_font_registry_alist);
6775 defsubr (&Sface_attributes_as_vector);
6776 #if GLYPH_DEBUG
6777 defsubr (&Sdump_face);
6778 defsubr (&Sshow_face_resources);
6779 #endif /* GLYPH_DEBUG */
6780 defsubr (&Sclear_face_cache);
6781 defsubr (&Stty_suppress_bold_inverse_default_colors);
6783 #if defined DEBUG_X_COLORS && defined HAVE_X_WINDOWS
6784 defsubr (&Sdump_colors);
6785 #endif
6787 DEFVAR_LISP ("font-list-limit", &Vfont_list_limit,
6788 doc: /* *Limit for font matching.
6789 If an integer > 0, font matching functions won't load more than
6790 that number of fonts when searching for a matching font. */);
6791 Vfont_list_limit = make_number (DEFAULT_FONT_LIST_LIMIT);
6793 DEFVAR_LISP ("face-new-frame-defaults", &Vface_new_frame_defaults,
6794 doc: /* List of global face definitions (for internal use only.) */);
6795 Vface_new_frame_defaults = Qnil;
6797 DEFVAR_LISP ("face-default-stipple", &Vface_default_stipple,
6798 doc: /* *Default stipple pattern used on monochrome displays.
6799 This stipple pattern is used on monochrome displays
6800 instead of shades of gray for a face background color.
6801 See `set-face-stipple' for possible values for this variable. */);
6802 Vface_default_stipple = build_string ("gray3");
6804 DEFVAR_LISP ("tty-defined-color-alist", &Vtty_defined_color_alist,
6805 doc: /* An alist of defined terminal colors and their RGB values. */);
6806 Vtty_defined_color_alist = Qnil;
6808 DEFVAR_LISP ("scalable-fonts-allowed", &Vscalable_fonts_allowed,
6809 doc: /* Allowed scalable fonts.
6810 A value of nil means don't allow any scalable fonts.
6811 A value of t means allow any scalable font.
6812 Otherwise, value must be a list of regular expressions. A font may be
6813 scaled if its name matches a regular expression in the list.
6814 Note that if value is nil, a scalable font might still be used, if no
6815 other font of the appropriate family and registry is available. */);
6816 Vscalable_fonts_allowed = Qnil;
6818 DEFVAR_LISP ("face-ignored-fonts", &Vface_ignored_fonts,
6819 doc: /* List of ignored fonts.
6820 Each element is a regular expression that matches names of fonts to
6821 ignore. */);
6822 Vface_ignored_fonts = Qnil;
6824 DEFVAR_LISP ("face-remapping-alist", &Vface_remapping_alist,
6825 doc: /* Alist of face remappings.
6826 Each element is of the form:
6828 (FACE REPLACEMENT...),
6830 which causes display of the face FACE to use REPLACEMENT... instead.
6831 REPLACEMENT... is interpreted the same way the value of a `face' text
6832 property is: it may be (1) A face name, (2) A list of face names, (3) A
6833 property-list of face attribute/value pairs, or (4) A list of face names
6834 intermixed with lists containing face attribute/value pairs.
6836 Multiple entries in REPLACEMENT... are merged together to form the final
6837 result, with faces or attributes earlier in the list taking precedence
6838 over those that are later.
6840 Face-name remapping cycles are suppressed; recursive references use the
6841 underlying face instead of the remapped face. So a remapping of the form:
6843 (FACE EXTRA-FACE... FACE)
6847 (FACE (FACE-ATTR VAL ...) FACE)
6849 will cause EXTRA-FACE... or (FACE-ATTR VAL ...) to be _merged_ with the
6850 existing definition of FACE. Note that for the default face, this isn't
6851 necessary, as every face inherits from the default face.
6853 Making this variable buffer-local is a good way to allow buffer-specific
6854 face definitions. For instance, the mode my-mode could define a face
6855 `my-mode-default', and then in the mode setup function, do:
6857 (set (make-local-variable 'face-remapping-alist)
6858 '((default my-mode-default)))). */);
6859 Vface_remapping_alist = Qnil;
6861 DEFVAR_LISP ("face-font-rescale-alist", &Vface_font_rescale_alist,
6862 doc: /* Alist of fonts vs the rescaling factors.
6863 Each element is a cons (FONT-NAME-PATTERN . RESCALE-RATIO), where
6864 FONT-NAME-PATTERN is a regular expression matching a font name, and
6865 RESCALE-RATIO is a floating point number to specify how much larger
6866 \(or smaller) font we should use. For instance, if a face requests
6867 a font of 10 point, we actually use a font of 10 * RESCALE-RATIO point. */);
6868 Vface_font_rescale_alist = Qnil;
6870 #ifdef HAVE_WINDOW_SYSTEM
6871 defsubr (&Sbitmap_spec_p);
6872 defsubr (&Sx_list_fonts);
6873 defsubr (&Sinternal_face_x_get_resource);
6874 defsubr (&Sx_family_fonts);
6875 defsubr (&Sx_font_family_list);
6876 #endif /* HAVE_WINDOW_SYSTEM */
6879 /* arch-tag: 8a0f7598-5517-408d-9ab3-1da6fcd4c749
6880 (do not change this comment) */