* emacs-lisp/byte-opt.el (toplevel): Add compare-window-configurations,
[bpt/emacs.git] / src / xfaces.c
... / ...
CommitLineData
1/* xfaces.c -- "Face" primitives.
2
3Copyright (C) 1993-1994, 1998-2012 Free Software Foundation, Inc.
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation, either version 3 of the License, or
10(at your option) any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
20/* New face implementation by Gerd Moellmann <gerd@gnu.org>. */
21
22/* Faces.
23
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:
27
28 1. Font family name.
29
30 2. Font foundry name.
31
32 3. Relative proportionate width, aka character set width or set
33 width (swidth), e.g. `semi-compressed'.
34
35 4. Font height in 1/10pt.
36
37 5. Font weight, e.g. `bold'.
38
39 6. Font slant, e.g. `italic'.
40
41 7. Foreground color.
42
43 8. Background color.
44
45 9. Whether or not characters should be underlined, and in what color.
46
47 10. Whether or not characters should be displayed in inverse video.
48
49 11. A background stipple, a bitmap.
50
51 12. Whether or not characters should be overlined, and in what color.
52
53 13. Whether or not characters should be strike-through, and in what
54 color.
55
56 14. Whether or not a box should be drawn around characters, the box
57 type, and, for simple boxes, in what color.
58
59 15. Font-spec, or nil. This is a special attribute.
60
61 A font-spec is a collection of font attributes (specs).
62
63 When this attribute is specified, the face uses a font matching
64 with the specs as is except for what overwritten by the specs in
65 the fontset (see below). In addition, the other font-related
66 attributes (1st thru 5th) are updated from the spec.
67
68 On the other hand, if one of the other font-related attributes are
69 specified, the corresponding specs in this attribute is set to nil.
70
71 15. A face name or list of face names from which to inherit attributes.
72
73 16. A specified average font width, which is invisible from Lisp,
74 and is used to ensure that a font specified on the command line,
75 for example, can be matched exactly.
76
77 17. A fontset name. This is another special attribute.
78
79 A fontset is a mappings from characters to font-specs, and the
80 specs overwrite the font-spec in the 14th attribute.
81
82
83 Faces are frame-local by nature because Emacs allows to define the
84 same named face (face names are symbols) differently for different
85 frames. Each frame has an alist of face definitions for all named
86 faces. The value of a named face in such an alist is a Lisp vector
87 with the symbol `face' in slot 0, and a slot for each of the face
88 attributes mentioned above.
89
90 There is also a global face alist `Vface_new_frame_defaults'. Face
91 definitions from this list are used to initialize faces of newly
92 created frames.
93
94 A face doesn't have to specify all attributes. Those not specified
95 have a value of `unspecified'. Faces specifying all attributes but
96 the 14th are called `fully-specified'.
97
98
99 Face merging.
100
101 The display style of a given character in the text is determined by
102 combining several faces. This process is called `face merging'.
103 Any aspect of the display style that isn't specified by overlays or
104 text properties is taken from the `default' face. Since it is made
105 sure that the default face is always fully-specified, face merging
106 always results in a fully-specified face.
107
108
109 Face realization.
110
111 After all face attributes for a character have been determined by
112 merging faces of that character, that face is `realized'. The
113 realization process maps face attributes to what is physically
114 available on the system where Emacs runs. The result is a
115 `realized face' in form of a struct face which is stored in the
116 face cache of the frame on which it was realized.
117
118 Face realization is done in the context of the character to display
119 because different fonts may be used for different characters. In
120 other words, for characters that have different font
121 specifications, different realized faces are needed to display
122 them.
123
124 Font specification is done by fontsets. See the comment in
125 fontset.c for the details. In the current implementation, all ASCII
126 characters share the same font in a fontset.
127
128 Faces are at first realized for ASCII characters, and, at that
129 time, assigned a specific realized fontset. Hereafter, we call
130 such a face as `ASCII face'. When a face for a multibyte character
131 is realized, it inherits (thus shares) a fontset of an ASCII face
132 that has the same attributes other than font-related ones.
133
134 Thus, all realized faces have a realized fontset.
135
136
137 Unibyte text.
138
139 Unibyte text (i.e. raw 8-bit characters) is displayed with the same
140 font as ASCII characters. That is because it is expected that
141 unibyte text users specify a font that is suitable both for ASCII
142 and raw 8-bit characters.
143
144
145 Font selection.
146
147 Font selection tries to find the best available matching font for a
148 given (character, face) combination.
149
150 If the face specifies a fontset name, that fontset determines a
151 pattern for fonts of the given character. If the face specifies a
152 font name or the other font-related attributes, a fontset is
153 realized from the default fontset. In that case, that
154 specification determines a pattern for ASCII characters and the
155 default fontset determines a pattern for multibyte characters.
156
157 Available fonts on the system on which Emacs runs are then matched
158 against the font pattern. The result of font selection is the best
159 match for the given face attributes in this font list.
160
161 Font selection can be influenced by the user.
162
163 1. The user can specify the relative importance he gives the face
164 attributes width, height, weight, and slant by setting
165 face-font-selection-order (faces.el) to a list of face attribute
166 names. The default is '(:width :height :weight :slant), and means
167 that font selection first tries to find a good match for the font
168 width specified by a face, then---within fonts with that
169 width---tries to find a best match for the specified font height,
170 etc.
171
172 2. Setting face-font-family-alternatives allows the user to
173 specify alternative font families to try if a family specified by a
174 face doesn't exist.
175
176 3. Setting face-font-registry-alternatives allows the user to
177 specify all alternative font registries to try for a face
178 specifying a registry.
179
180 4. Setting face-ignored-fonts allows the user to ignore specific
181 fonts.
182
183
184 Character composition.
185
186 Usually, the realization process is already finished when Emacs
187 actually reflects the desired glyph matrix on the screen. However,
188 on displaying a composition (sequence of characters to be composed
189 on the screen), a suitable font for the components of the
190 composition is selected and realized while drawing them on the
191 screen, i.e. the realization process is delayed but in principle
192 the same.
193
194
195 Initialization of basic faces.
196
197 The faces `default', `modeline' are considered `basic faces'.
198 When redisplay happens the first time for a newly created frame,
199 basic faces are realized for CHARSET_ASCII. Frame parameters are
200 used to fill in unspecified attributes of the default face. */
201
202#include <config.h>
203#include <stdio.h>
204#include <sys/types.h>
205#include <sys/stat.h>
206#include <stdio.h> /* This needs to be before termchar.h */
207
208#include "lisp.h"
209#include "character.h"
210#include "charset.h"
211#include "keyboard.h"
212#include "frame.h"
213#include "termhooks.h"
214
215#ifdef HAVE_X_WINDOWS
216#include "xterm.h"
217#ifdef USE_MOTIF
218#include <Xm/Xm.h>
219#include <Xm/XmStrDefs.h>
220#endif /* USE_MOTIF */
221#endif /* HAVE_X_WINDOWS */
222
223#ifdef MSDOS
224#include "dosfns.h"
225#endif
226
227#ifdef HAVE_WINDOW_SYSTEM
228#include TERM_HEADER
229#include "fontset.h"
230#ifdef HAVE_NTGUI
231#undef FRAME_X_DISPLAY_INFO
232#define FRAME_X_DISPLAY_INFO FRAME_W32_DISPLAY_INFO
233#define x_display_info w32_display_info
234#define check_x check_w32
235#define GCGraphicsExposures 0
236#endif /* HAVE_NTGUI */
237
238#ifdef HAVE_NS
239#undef FRAME_X_DISPLAY_INFO
240#define FRAME_X_DISPLAY_INFO FRAME_NS_DISPLAY_INFO
241#define x_display_info ns_display_info
242#define check_x check_ns
243#define GCGraphicsExposures 0
244#endif /* HAVE_NS */
245#endif /* HAVE_WINDOW_SYSTEM */
246
247#include "buffer.h"
248#include "dispextern.h"
249#include "blockinput.h"
250#include "window.h"
251#include "intervals.h"
252#include "termchar.h"
253
254#include "font.h"
255
256#ifdef HAVE_X_WINDOWS
257
258/* Compensate for a bug in Xos.h on some systems, on which it requires
259 time.h. On some such systems, Xos.h tries to redefine struct
260 timeval and struct timezone if USG is #defined while it is
261 #included. */
262
263#ifdef XOS_NEEDS_TIME_H
264#include <time.h>
265#undef USG
266#include <X11/Xos.h>
267#define USG
268#define __TIMEVAL__
269#if defined USG || defined __TIMEVAL__ /* Don't warn about unused macros. */
270#endif
271#else /* not XOS_NEEDS_TIME_H */
272#include <X11/Xos.h>
273#endif /* not XOS_NEEDS_TIME_H */
274
275#endif /* HAVE_X_WINDOWS */
276
277#include <c-ctype.h>
278
279/* Number of pt per inch (from the TeXbook). */
280
281#define PT_PER_INCH 72.27
282
283/* Non-zero if face attribute ATTR is unspecified. */
284
285#define UNSPECIFIEDP(ATTR) EQ ((ATTR), Qunspecified)
286
287/* Non-zero if face attribute ATTR is `ignore-defface'. */
288
289#define IGNORE_DEFFACE_P(ATTR) EQ ((ATTR), QCignore_defface)
290
291/* Value is the number of elements of VECTOR. */
292
293#define DIM(VECTOR) (sizeof (VECTOR) / sizeof *(VECTOR))
294
295/* Size of hash table of realized faces in face caches (should be a
296 prime number). */
297
298#define FACE_CACHE_BUCKETS_SIZE 1001
299
300/* Keyword symbols used for face attribute names. */
301
302Lisp_Object QCfamily, QCheight, QCweight, QCslant;
303static Lisp_Object QCunderline;
304static Lisp_Object QCinverse_video, QCstipple;
305Lisp_Object QCforeground, QCbackground;
306Lisp_Object QCwidth;
307static Lisp_Object QCfont, QCbold, QCitalic;
308static Lisp_Object QCreverse_video;
309static Lisp_Object QCoverline, QCstrike_through, QCbox, QCinherit;
310static Lisp_Object QCfontset;
311
312/* Symbols used for attribute values. */
313
314Lisp_Object Qnormal;
315Lisp_Object Qbold;
316static Lisp_Object Qline, Qwave;
317static Lisp_Object Qultra_light, Qreverse_oblique, Qreverse_italic;
318Lisp_Object Qextra_light, Qlight;
319Lisp_Object Qsemi_light, Qsemi_bold, Qextra_bold, Qultra_bold;
320Lisp_Object Qoblique;
321Lisp_Object Qitalic;
322static Lisp_Object Qultra_condensed, Qextra_condensed;
323Lisp_Object Qcondensed;
324static Lisp_Object Qsemi_condensed, Qsemi_expanded, Qextra_expanded;
325Lisp_Object Qexpanded;
326static Lisp_Object Qultra_expanded;
327static Lisp_Object Qreleased_button, Qpressed_button;
328static Lisp_Object QCstyle, QCcolor, QCline_width;
329Lisp_Object Qunspecified; /* used in dosfns.c */
330static Lisp_Object QCignore_defface;
331
332char unspecified_fg[] = "unspecified-fg", unspecified_bg[] = "unspecified-bg";
333
334/* The name of the function to call when the background of the frame
335 has changed, frame_set_background_mode. */
336
337static Lisp_Object Qframe_set_background_mode;
338
339/* Names of basic faces. */
340
341Lisp_Object Qdefault, Qtool_bar, Qfringe;
342static Lisp_Object Qregion;
343Lisp_Object Qheader_line, Qscroll_bar, Qcursor;
344static Lisp_Object Qborder, Qmouse, Qmenu;
345Lisp_Object Qmode_line_inactive;
346static Lisp_Object Qvertical_border;
347
348/* The symbol `face-alias'. A symbols having that property is an
349 alias for another face. Value of the property is the name of
350 the aliased face. */
351
352static Lisp_Object Qface_alias;
353
354/* Alist of alternative font families. Each element is of the form
355 (FAMILY FAMILY1 FAMILY2 ...). If fonts of FAMILY can't be loaded,
356 try FAMILY1, then FAMILY2, ... */
357
358Lisp_Object Vface_alternative_font_family_alist;
359
360/* Alist of alternative font registries. Each element is of the form
361 (REGISTRY REGISTRY1 REGISTRY2...). If fonts of REGISTRY can't be
362 loaded, try REGISTRY1, then REGISTRY2, ... */
363
364Lisp_Object Vface_alternative_font_registry_alist;
365
366/* Allowed scalable fonts. A value of nil means don't allow any
367 scalable fonts. A value of t means allow the use of any scalable
368 font. Otherwise, value must be a list of regular expressions. A
369 font may be scaled if its name matches a regular expression in the
370 list. */
371
372static Lisp_Object Qscalable_fonts_allowed;
373
374/* The symbols `foreground-color' and `background-color' which can be
375 used as part of a `face' property. This is for compatibility with
376 Emacs 20.2. */
377
378Lisp_Object Qforeground_color, Qbackground_color;
379
380/* The symbols `face' and `mouse-face' used as text properties. */
381
382Lisp_Object Qface;
383
384/* Property for basic faces which other faces cannot inherit. */
385
386static Lisp_Object Qface_no_inherit;
387
388/* Error symbol for wrong_type_argument in load_pixmap. */
389
390static Lisp_Object Qbitmap_spec_p;
391
392/* The next ID to assign to Lisp faces. */
393
394static int next_lface_id;
395
396/* A vector mapping Lisp face Id's to face names. */
397
398static Lisp_Object *lface_id_to_name;
399static ptrdiff_t lface_id_to_name_size;
400
401/* TTY color-related functions (defined in tty-colors.el). */
402
403static Lisp_Object Qtty_color_desc, Qtty_color_by_index, Qtty_color_standard_values;
404
405/* The name of the function used to compute colors on TTYs. */
406
407static Lisp_Object Qtty_color_alist;
408
409/* Counter for calls to clear_face_cache. If this counter reaches
410 CLEAR_FONT_TABLE_COUNT, and a frame has more than
411 CLEAR_FONT_TABLE_NFONTS load, unused fonts are freed. */
412
413static int clear_font_table_count;
414#define CLEAR_FONT_TABLE_COUNT 100
415#define CLEAR_FONT_TABLE_NFONTS 10
416
417/* Non-zero means face attributes have been changed since the last
418 redisplay. Used in redisplay_internal. */
419
420int face_change_count;
421
422/* Non-zero means don't display bold text if a face's foreground
423 and background colors are the inverse of the default colors of the
424 display. This is a kluge to suppress `bold black' foreground text
425 which is hard to read on an LCD monitor. */
426
427static int tty_suppress_bold_inverse_default_colors_p;
428
429/* A list of the form `((x . y))' used to avoid consing in
430 Finternal_set_lisp_face_attribute. */
431
432static Lisp_Object Vparam_value_alist;
433
434/* The total number of colors currently allocated. */
435
436#ifdef GLYPH_DEBUG
437static int ncolors_allocated;
438static int npixmaps_allocated;
439static int ngcs;
440#endif
441
442/* Non-zero means the definition of the `menu' face for new frames has
443 been changed. */
444
445static int menu_face_changed_default;
446
447\f
448/* Function prototypes. */
449
450struct table_entry;
451struct named_merge_point;
452
453static void set_font_frame_param (Lisp_Object, Lisp_Object);
454static struct face *realize_face (struct face_cache *, Lisp_Object *,
455 int);
456static struct face *realize_non_ascii_face (struct frame *, Lisp_Object,
457 struct face *);
458static struct face *realize_x_face (struct face_cache *, Lisp_Object *);
459static struct face *realize_tty_face (struct face_cache *, Lisp_Object *);
460static int realize_basic_faces (struct frame *);
461static int realize_default_face (struct frame *);
462static void realize_named_face (struct frame *, Lisp_Object, int);
463static struct face_cache *make_face_cache (struct frame *);
464static void clear_face_gcs (struct face_cache *);
465static void free_face_cache (struct face_cache *);
466static int merge_face_ref (struct frame *, Lisp_Object, Lisp_Object *,
467 int, struct named_merge_point *);
468
469\f
470/***********************************************************************
471 Utilities
472 ***********************************************************************/
473
474#ifdef HAVE_X_WINDOWS
475
476#ifdef DEBUG_X_COLORS
477
478/* The following is a poor mans infrastructure for debugging X color
479 allocation problems on displays with PseudoColor-8. Some X servers
480 like 3.3.5 XF86_SVGA with Matrox cards apparently don't implement
481 color reference counts completely so that they don't signal an
482 error when a color is freed whose reference count is already 0.
483 Other X servers do. To help me debug this, the following code
484 implements a simple reference counting schema of its own, for a
485 single display/screen. --gerd. */
486
487/* Reference counts for pixel colors. */
488
489int color_count[256];
490
491/* Register color PIXEL as allocated. */
492
493void
494register_color (unsigned long pixel)
495{
496 eassert (pixel < 256);
497 ++color_count[pixel];
498}
499
500
501/* Register color PIXEL as deallocated. */
502
503void
504unregister_color (unsigned long pixel)
505{
506 eassert (pixel < 256);
507 if (color_count[pixel] > 0)
508 --color_count[pixel];
509 else
510 emacs_abort ();
511}
512
513
514/* Register N colors from PIXELS as deallocated. */
515
516void
517unregister_colors (unsigned long *pixels, int n)
518{
519 int i;
520 for (i = 0; i < n; ++i)
521 unregister_color (pixels[i]);
522}
523
524
525DEFUN ("dump-colors", Fdump_colors, Sdump_colors, 0, 0, 0,
526 doc: /* Dump currently allocated colors to stderr. */)
527 (void)
528{
529 int i, n;
530
531 fputc ('\n', stderr);
532
533 for (i = n = 0; i < sizeof color_count / sizeof color_count[0]; ++i)
534 if (color_count[i])
535 {
536 fprintf (stderr, "%3d: %5d", i, color_count[i]);
537 ++n;
538 if (n % 5 == 0)
539 fputc ('\n', stderr);
540 else
541 fputc ('\t', stderr);
542 }
543
544 if (n % 5 != 0)
545 fputc ('\n', stderr);
546 return Qnil;
547}
548
549#endif /* DEBUG_X_COLORS */
550
551
552/* Free colors used on frame F. PIXELS is an array of NPIXELS pixel
553 color values. Interrupt input must be blocked when this function
554 is called. */
555
556void
557x_free_colors (struct frame *f, long unsigned int *pixels, int npixels)
558{
559 int class = FRAME_X_DISPLAY_INFO (f)->visual->class;
560
561 /* If display has an immutable color map, freeing colors is not
562 necessary and some servers don't allow it. So don't do it. */
563 if (class != StaticColor && class != StaticGray && class != TrueColor)
564 {
565#ifdef DEBUG_X_COLORS
566 unregister_colors (pixels, npixels);
567#endif
568 XFreeColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
569 pixels, npixels, 0);
570 }
571}
572
573
574#ifdef USE_X_TOOLKIT
575
576/* Free colors used on frame F. PIXELS is an array of NPIXELS pixel
577 color values. Interrupt input must be blocked when this function
578 is called. */
579
580void
581x_free_dpy_colors (Display *dpy, Screen *screen, Colormap cmap,
582 long unsigned int *pixels, int npixels)
583{
584 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
585 int class = dpyinfo->visual->class;
586
587 /* If display has an immutable color map, freeing colors is not
588 necessary and some servers don't allow it. So don't do it. */
589 if (class != StaticColor && class != StaticGray && class != TrueColor)
590 {
591#ifdef DEBUG_X_COLORS
592 unregister_colors (pixels, npixels);
593#endif
594 XFreeColors (dpy, cmap, pixels, npixels, 0);
595 }
596}
597#endif /* USE_X_TOOLKIT */
598
599/* Create and return a GC for use on frame F. GC values and mask
600 are given by XGCV and MASK. */
601
602static GC
603x_create_gc (struct frame *f, long unsigned int mask, XGCValues *xgcv)
604{
605 GC gc;
606 block_input ();
607 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), mask, xgcv);
608 unblock_input ();
609 IF_DEBUG (++ngcs);
610 return gc;
611}
612
613
614/* Free GC which was used on frame F. */
615
616static void
617x_free_gc (struct frame *f, GC gc)
618{
619 eassert (input_blocked_p ());
620 IF_DEBUG (eassert (--ngcs >= 0));
621 XFreeGC (FRAME_X_DISPLAY (f), gc);
622}
623
624#endif /* HAVE_X_WINDOWS */
625
626#ifdef HAVE_NTGUI
627/* W32 emulation of GCs */
628
629static GC
630x_create_gc (struct frame *f, unsigned long mask, XGCValues *xgcv)
631{
632 GC gc;
633 block_input ();
634 gc = XCreateGC (NULL, FRAME_W32_WINDOW (f), mask, xgcv);
635 unblock_input ();
636 IF_DEBUG (++ngcs);
637 return gc;
638}
639
640
641/* Free GC which was used on frame F. */
642
643static void
644x_free_gc (struct frame *f, GC gc)
645{
646 IF_DEBUG (eassert (--ngcs >= 0));
647 xfree (gc);
648}
649
650#endif /* HAVE_NTGUI */
651
652#ifdef HAVE_NS
653/* NS emulation of GCs */
654
655static GC
656x_create_gc (struct frame *f,
657 unsigned long mask,
658 XGCValues *xgcv)
659{
660 GC gc = xmalloc (sizeof *gc);
661 *gc = *xgcv;
662 return gc;
663}
664
665static void
666x_free_gc (struct frame *f, GC gc)
667{
668 xfree (gc);
669}
670#endif /* HAVE_NS */
671
672/* If FRAME is nil, return a pointer to the selected frame.
673 Otherwise, check that FRAME is a live frame, and return a pointer
674 to it. NPARAM is the parameter number of FRAME, for
675 CHECK_LIVE_FRAME. This is here because it's a frequent pattern in
676 Lisp function definitions. */
677
678static struct frame *
679frame_or_selected_frame (Lisp_Object frame, int nparam)
680{
681 if (NILP (frame))
682 frame = selected_frame;
683
684 CHECK_LIVE_FRAME (frame);
685 return XFRAME (frame);
686}
687
688\f
689/***********************************************************************
690 Frames and faces
691 ***********************************************************************/
692
693/* Initialize face cache and basic faces for frame F. */
694
695void
696init_frame_faces (struct frame *f)
697{
698 /* Make a face cache, if F doesn't have one. */
699 if (FRAME_FACE_CACHE (f) == NULL)
700 FRAME_FACE_CACHE (f) = make_face_cache (f);
701
702#ifdef HAVE_WINDOW_SYSTEM
703 /* Make the image cache. */
704 if (FRAME_WINDOW_P (f))
705 {
706 /* We initialize the image cache when creating the first frame
707 on a terminal, and not during terminal creation. This way,
708 `x-open-connection' on a tty won't create an image cache. */
709 if (FRAME_IMAGE_CACHE (f) == NULL)
710 FRAME_IMAGE_CACHE (f) = make_image_cache ();
711 ++FRAME_IMAGE_CACHE (f)->refcount;
712 }
713#endif /* HAVE_WINDOW_SYSTEM */
714
715 /* Realize basic faces. Must have enough information in frame
716 parameters to realize basic faces at this point. */
717#ifdef HAVE_X_WINDOWS
718 if (!FRAME_X_P (f) || FRAME_X_WINDOW (f))
719#endif
720#ifdef HAVE_NTGUI
721 if (!FRAME_WINDOW_P (f) || FRAME_W32_WINDOW (f))
722#endif
723#ifdef HAVE_NS
724 if (!FRAME_NS_P (f) || FRAME_NS_WINDOW (f))
725#endif
726 if (!realize_basic_faces (f))
727 emacs_abort ();
728}
729
730
731/* Free face cache of frame F. Called from delete_frame. */
732
733void
734free_frame_faces (struct frame *f)
735{
736 struct face_cache *face_cache = FRAME_FACE_CACHE (f);
737
738 if (face_cache)
739 {
740 free_face_cache (face_cache);
741 FRAME_FACE_CACHE (f) = NULL;
742 }
743
744#ifdef HAVE_WINDOW_SYSTEM
745 if (FRAME_WINDOW_P (f))
746 {
747 struct image_cache *image_cache = FRAME_IMAGE_CACHE (f);
748 if (image_cache)
749 {
750 --image_cache->refcount;
751 if (image_cache->refcount == 0)
752 free_image_cache (f);
753 }
754 }
755#endif /* HAVE_WINDOW_SYSTEM */
756}
757
758
759/* Clear face caches, and recompute basic faces for frame F. Call
760 this after changing frame parameters on which those faces depend,
761 or when realized faces have been freed due to changing attributes
762 of named faces. */
763
764void
765recompute_basic_faces (struct frame *f)
766{
767 if (FRAME_FACE_CACHE (f))
768 {
769 clear_face_cache (0);
770 if (!realize_basic_faces (f))
771 emacs_abort ();
772 }
773}
774
775
776/* Clear the face caches of all frames. CLEAR_FONTS_P non-zero means
777 try to free unused fonts, too. */
778
779void
780clear_face_cache (int clear_fonts_p)
781{
782#ifdef HAVE_WINDOW_SYSTEM
783 Lisp_Object tail, frame;
784
785 if (clear_fonts_p
786 || ++clear_font_table_count == CLEAR_FONT_TABLE_COUNT)
787 {
788#if 0
789 /* Not yet implemented. */
790 clear_font_cache (frame);
791#endif
792
793 /* From time to time see if we can unload some fonts. This also
794 frees all realized faces on all frames. Fonts needed by
795 faces will be loaded again when faces are realized again. */
796 clear_font_table_count = 0;
797
798 FOR_EACH_FRAME (tail, frame)
799 {
800 struct frame *f = XFRAME (frame);
801 if (FRAME_WINDOW_P (f)
802 && FRAME_X_DISPLAY_INFO (f)->n_fonts > CLEAR_FONT_TABLE_NFONTS)
803 free_all_realized_faces (frame);
804 }
805 }
806 else
807 {
808 /* Clear GCs of realized faces. */
809 FOR_EACH_FRAME (tail, frame)
810 {
811 struct frame *f = XFRAME (frame);
812 if (FRAME_WINDOW_P (f))
813 clear_face_gcs (FRAME_FACE_CACHE (f));
814 }
815 clear_image_caches (Qnil);
816 }
817#endif /* HAVE_WINDOW_SYSTEM */
818}
819
820
821DEFUN ("clear-face-cache", Fclear_face_cache, Sclear_face_cache, 0, 1, 0,
822 doc: /* Clear face caches on all frames.
823Optional THOROUGHLY non-nil means try to free unused fonts, too. */)
824 (Lisp_Object thoroughly)
825{
826 clear_face_cache (!NILP (thoroughly));
827 ++face_change_count;
828 ++windows_or_buffers_changed;
829 return Qnil;
830}
831
832\f
833/***********************************************************************
834 X Pixmaps
835 ***********************************************************************/
836
837#ifdef HAVE_WINDOW_SYSTEM
838
839DEFUN ("bitmap-spec-p", Fbitmap_spec_p, Sbitmap_spec_p, 1, 1, 0,
840 doc: /* Value is non-nil if OBJECT is a valid bitmap specification.
841A bitmap specification is either a string, a file name, or a list
842\(WIDTH HEIGHT DATA) where WIDTH is the pixel width of the bitmap,
843HEIGHT is its height, and DATA is a string containing the bits of
844the pixmap. Bits are stored row by row, each row occupies
845\(WIDTH + 7)/8 bytes. */)
846 (Lisp_Object object)
847{
848 int pixmap_p = 0;
849
850 if (STRINGP (object))
851 /* If OBJECT is a string, it's a file name. */
852 pixmap_p = 1;
853 else if (CONSP (object))
854 {
855 /* Otherwise OBJECT must be (WIDTH HEIGHT DATA), WIDTH and
856 HEIGHT must be ints > 0, and DATA must be string large
857 enough to hold a bitmap of the specified size. */
858 Lisp_Object width, height, data;
859
860 height = width = data = Qnil;
861
862 if (CONSP (object))
863 {
864 width = XCAR (object);
865 object = XCDR (object);
866 if (CONSP (object))
867 {
868 height = XCAR (object);
869 object = XCDR (object);
870 if (CONSP (object))
871 data = XCAR (object);
872 }
873 }
874
875 if (STRINGP (data)
876 && RANGED_INTEGERP (1, width, INT_MAX)
877 && RANGED_INTEGERP (1, height, INT_MAX))
878 {
879 int bytes_per_row = ((XINT (width) + BITS_PER_CHAR - 1)
880 / BITS_PER_CHAR);
881 if (XINT (height) <= SBYTES (data) / bytes_per_row)
882 pixmap_p = 1;
883 }
884 }
885
886 return pixmap_p ? Qt : Qnil;
887}
888
889
890/* Load a bitmap according to NAME (which is either a file name or a
891 pixmap spec) for use on frame F. Value is the bitmap_id (see
892 xfns.c). If NAME is nil, return with a bitmap id of zero. If
893 bitmap cannot be loaded, display a message saying so, and return
894 zero. Store the bitmap width in *W_PTR and its height in *H_PTR,
895 if these pointers are not null. */
896
897static ptrdiff_t
898load_pixmap (FRAME_PTR f, Lisp_Object name, unsigned int *w_ptr,
899 unsigned int *h_ptr)
900{
901 ptrdiff_t bitmap_id;
902
903 if (NILP (name))
904 return 0;
905
906 CHECK_TYPE (!NILP (Fbitmap_spec_p (name)), Qbitmap_spec_p, name);
907
908 block_input ();
909 if (CONSP (name))
910 {
911 /* Decode a bitmap spec into a bitmap. */
912
913 int h, w;
914 Lisp_Object bits;
915
916 w = XINT (Fcar (name));
917 h = XINT (Fcar (Fcdr (name)));
918 bits = Fcar (Fcdr (Fcdr (name)));
919
920 bitmap_id = x_create_bitmap_from_data (f, SSDATA (bits),
921 w, h);
922 }
923 else
924 {
925 /* It must be a string -- a file name. */
926 bitmap_id = x_create_bitmap_from_file (f, name);
927 }
928 unblock_input ();
929
930 if (bitmap_id < 0)
931 {
932 add_to_log ("Invalid or undefined bitmap `%s'", name, Qnil);
933 bitmap_id = 0;
934
935 if (w_ptr)
936 *w_ptr = 0;
937 if (h_ptr)
938 *h_ptr = 0;
939 }
940 else
941 {
942#ifdef GLYPH_DEBUG
943 ++npixmaps_allocated;
944#endif
945 if (w_ptr)
946 *w_ptr = x_bitmap_width (f, bitmap_id);
947
948 if (h_ptr)
949 *h_ptr = x_bitmap_height (f, bitmap_id);
950 }
951
952 return bitmap_id;
953}
954
955#endif /* HAVE_WINDOW_SYSTEM */
956
957
958\f
959/***********************************************************************
960 X Colors
961 ***********************************************************************/
962
963/* Parse RGB_LIST, and fill in the RGB fields of COLOR.
964 RGB_LIST should contain (at least) 3 lisp integers.
965 Return 0 if there's a problem with RGB_LIST, otherwise return 1. */
966
967static int
968parse_rgb_list (Lisp_Object rgb_list, XColor *color)
969{
970#define PARSE_RGB_LIST_FIELD(field) \
971 if (CONSP (rgb_list) && INTEGERP (XCAR (rgb_list))) \
972 { \
973 color->field = XINT (XCAR (rgb_list)); \
974 rgb_list = XCDR (rgb_list); \
975 } \
976 else \
977 return 0;
978
979 PARSE_RGB_LIST_FIELD (red);
980 PARSE_RGB_LIST_FIELD (green);
981 PARSE_RGB_LIST_FIELD (blue);
982
983 return 1;
984}
985
986
987/* Lookup on frame F the color described by the lisp string COLOR.
988 The resulting tty color is returned in TTY_COLOR; if STD_COLOR is
989 non-zero, then the `standard' definition of the same color is
990 returned in it. */
991
992static bool
993tty_lookup_color (struct frame *f, Lisp_Object color, XColor *tty_color,
994 XColor *std_color)
995{
996 Lisp_Object frame, color_desc;
997
998 if (!STRINGP (color) || NILP (Ffboundp (Qtty_color_desc)))
999 return 0;
1000
1001 XSETFRAME (frame, f);
1002
1003 color_desc = call2 (Qtty_color_desc, color, frame);
1004 if (CONSP (color_desc) && CONSP (XCDR (color_desc)))
1005 {
1006 Lisp_Object rgb;
1007
1008 if (! INTEGERP (XCAR (XCDR (color_desc))))
1009 return 0;
1010
1011 tty_color->pixel = XINT (XCAR (XCDR (color_desc)));
1012
1013 rgb = XCDR (XCDR (color_desc));
1014 if (! parse_rgb_list (rgb, tty_color))
1015 return 0;
1016
1017 /* Should we fill in STD_COLOR too? */
1018 if (std_color)
1019 {
1020 /* Default STD_COLOR to the same as TTY_COLOR. */
1021 *std_color = *tty_color;
1022
1023 /* Do a quick check to see if the returned descriptor is
1024 actually _exactly_ equal to COLOR, otherwise we have to
1025 lookup STD_COLOR separately. If it's impossible to lookup
1026 a standard color, we just give up and use TTY_COLOR. */
1027 if ((!STRINGP (XCAR (color_desc))
1028 || NILP (Fstring_equal (color, XCAR (color_desc))))
1029 && !NILP (Ffboundp (Qtty_color_standard_values)))
1030 {
1031 /* Look up STD_COLOR separately. */
1032 rgb = call1 (Qtty_color_standard_values, color);
1033 if (! parse_rgb_list (rgb, std_color))
1034 return 0;
1035 }
1036 }
1037
1038 return 1;
1039 }
1040 else if (NILP (Fsymbol_value (intern ("tty-defined-color-alist"))))
1041 /* We were called early during startup, and the colors are not
1042 yet set up in tty-defined-color-alist. Don't return a failure
1043 indication, since this produces the annoying "Unable to
1044 load color" messages in the *Messages* buffer. */
1045 return 1;
1046 else
1047 /* tty-color-desc seems to have returned a bad value. */
1048 return 0;
1049}
1050
1051/* A version of defined_color for non-X frames. */
1052
1053static bool
1054tty_defined_color (struct frame *f, const char *color_name,
1055 XColor *color_def, bool alloc)
1056{
1057 bool status = 1;
1058
1059 /* Defaults. */
1060 color_def->pixel = FACE_TTY_DEFAULT_COLOR;
1061 color_def->red = 0;
1062 color_def->blue = 0;
1063 color_def->green = 0;
1064
1065 if (*color_name)
1066 status = tty_lookup_color (f, build_string (color_name), color_def, NULL);
1067
1068 if (color_def->pixel == FACE_TTY_DEFAULT_COLOR && *color_name)
1069 {
1070 if (strcmp (color_name, "unspecified-fg") == 0)
1071 color_def->pixel = FACE_TTY_DEFAULT_FG_COLOR;
1072 else if (strcmp (color_name, "unspecified-bg") == 0)
1073 color_def->pixel = FACE_TTY_DEFAULT_BG_COLOR;
1074 }
1075
1076 if (color_def->pixel != FACE_TTY_DEFAULT_COLOR)
1077 status = 1;
1078
1079 return status;
1080}
1081
1082
1083/* Decide if color named COLOR_NAME is valid for the display
1084 associated with the frame F; if so, return the rgb values in
1085 COLOR_DEF. If ALLOC, allocate a new colormap cell.
1086
1087 This does the right thing for any type of frame. */
1088
1089static bool
1090defined_color (struct frame *f, const char *color_name, XColor *color_def,
1091 bool alloc)
1092{
1093 if (!FRAME_WINDOW_P (f))
1094 return tty_defined_color (f, color_name, color_def, alloc);
1095#ifdef HAVE_X_WINDOWS
1096 else if (FRAME_X_P (f))
1097 return x_defined_color (f, color_name, color_def, alloc);
1098#endif
1099#ifdef HAVE_NTGUI
1100 else if (FRAME_W32_P (f))
1101 return w32_defined_color (f, color_name, color_def, alloc);
1102#endif
1103#ifdef HAVE_NS
1104 else if (FRAME_NS_P (f))
1105 return ns_defined_color (f, color_name, color_def, alloc, 1);
1106#endif
1107 else
1108 emacs_abort ();
1109}
1110
1111
1112/* Given the index IDX of a tty color on frame F, return its name, a
1113 Lisp string. */
1114
1115Lisp_Object
1116tty_color_name (struct frame *f, int idx)
1117{
1118 if (idx >= 0 && !NILP (Ffboundp (Qtty_color_by_index)))
1119 {
1120 Lisp_Object frame;
1121 Lisp_Object coldesc;
1122
1123 XSETFRAME (frame, f);
1124 coldesc = call2 (Qtty_color_by_index, make_number (idx), frame);
1125
1126 if (!NILP (coldesc))
1127 return XCAR (coldesc);
1128 }
1129#ifdef MSDOS
1130 /* We can have an MSDOG frame under -nw for a short window of
1131 opportunity before internal_terminal_init is called. DTRT. */
1132 if (FRAME_MSDOS_P (f) && !inhibit_window_system)
1133 return msdos_stdcolor_name (idx);
1134#endif
1135
1136 if (idx == FACE_TTY_DEFAULT_FG_COLOR)
1137 return build_string (unspecified_fg);
1138 if (idx == FACE_TTY_DEFAULT_BG_COLOR)
1139 return build_string (unspecified_bg);
1140
1141 return Qunspecified;
1142}
1143
1144
1145/* Return non-zero if COLOR_NAME is a shade of gray (or white or
1146 black) on frame F.
1147
1148 The criterion implemented here is not a terribly sophisticated one. */
1149
1150static int
1151face_color_gray_p (struct frame *f, const char *color_name)
1152{
1153 XColor color;
1154 int gray_p;
1155
1156 if (defined_color (f, color_name, &color, 0))
1157 gray_p = (/* Any color sufficiently close to black counts as gray. */
1158 (color.red < 5000 && color.green < 5000 && color.blue < 5000)
1159 ||
1160 ((eabs (color.red - color.green)
1161 < max (color.red, color.green) / 20)
1162 && (eabs (color.green - color.blue)
1163 < max (color.green, color.blue) / 20)
1164 && (eabs (color.blue - color.red)
1165 < max (color.blue, color.red) / 20)));
1166 else
1167 gray_p = 0;
1168
1169 return gray_p;
1170}
1171
1172
1173/* Return non-zero if color COLOR_NAME can be displayed on frame F.
1174 BACKGROUND_P non-zero means the color will be used as background
1175 color. */
1176
1177static int
1178face_color_supported_p (struct frame *f, const char *color_name,
1179 int background_p)
1180{
1181 Lisp_Object frame;
1182 XColor not_used;
1183
1184 XSETFRAME (frame, f);
1185 return
1186#ifdef HAVE_WINDOW_SYSTEM
1187 FRAME_WINDOW_P (f)
1188 ? (!NILP (Fxw_display_color_p (frame))
1189 || xstrcasecmp (color_name, "black") == 0
1190 || xstrcasecmp (color_name, "white") == 0
1191 || (background_p
1192 && face_color_gray_p (f, color_name))
1193 || (!NILP (Fx_display_grayscale_p (frame))
1194 && face_color_gray_p (f, color_name)))
1195 :
1196#endif
1197 tty_defined_color (f, color_name, &not_used, 0);
1198}
1199
1200
1201DEFUN ("color-gray-p", Fcolor_gray_p, Scolor_gray_p, 1, 2, 0,
1202 doc: /* Return non-nil if COLOR is a shade of gray (or white or black).
1203FRAME specifies the frame and thus the display for interpreting COLOR.
1204If FRAME is nil or omitted, use the selected frame. */)
1205 (Lisp_Object color, Lisp_Object frame)
1206{
1207 struct frame *f;
1208
1209 CHECK_STRING (color);
1210 if (NILP (frame))
1211 frame = selected_frame;
1212 else
1213 CHECK_FRAME (frame);
1214 f = XFRAME (frame);
1215 return face_color_gray_p (f, SSDATA (color)) ? Qt : Qnil;
1216}
1217
1218
1219DEFUN ("color-supported-p", Fcolor_supported_p,
1220 Scolor_supported_p, 1, 3, 0,
1221 doc: /* Return non-nil if COLOR can be displayed on FRAME.
1222BACKGROUND-P non-nil means COLOR is used as a background.
1223Otherwise, this function tells whether it can be used as a foreground.
1224If FRAME is nil or omitted, use the selected frame.
1225COLOR must be a valid color name. */)
1226 (Lisp_Object color, Lisp_Object frame, Lisp_Object background_p)
1227{
1228 struct frame *f;
1229
1230 CHECK_STRING (color);
1231 if (NILP (frame))
1232 frame = selected_frame;
1233 else
1234 CHECK_FRAME (frame);
1235 f = XFRAME (frame);
1236 if (face_color_supported_p (f, SSDATA (color), !NILP (background_p)))
1237 return Qt;
1238 return Qnil;
1239}
1240
1241
1242/* Load color with name NAME for use by face FACE on frame F.
1243 TARGET_INDEX must be one of LFACE_FOREGROUND_INDEX,
1244 LFACE_BACKGROUND_INDEX, LFACE_UNDERLINE_INDEX, LFACE_OVERLINE_INDEX,
1245 LFACE_STRIKE_THROUGH_INDEX, or LFACE_BOX_INDEX. Value is the
1246 pixel color. If color cannot be loaded, display a message, and
1247 return the foreground, background or underline color of F, but
1248 record that fact in flags of the face so that we don't try to free
1249 these colors. */
1250
1251unsigned long
1252load_color (struct frame *f, struct face *face, Lisp_Object name,
1253 enum lface_attribute_index target_index)
1254{
1255 XColor color;
1256
1257 eassert (STRINGP (name));
1258 eassert (target_index == LFACE_FOREGROUND_INDEX
1259 || target_index == LFACE_BACKGROUND_INDEX
1260 || target_index == LFACE_UNDERLINE_INDEX
1261 || target_index == LFACE_OVERLINE_INDEX
1262 || target_index == LFACE_STRIKE_THROUGH_INDEX
1263 || target_index == LFACE_BOX_INDEX);
1264
1265 /* if the color map is full, defined_color will return a best match
1266 to the values in an existing cell. */
1267 if (!defined_color (f, SSDATA (name), &color, 1))
1268 {
1269 add_to_log ("Unable to load color \"%s\"", name, Qnil);
1270
1271 switch (target_index)
1272 {
1273 case LFACE_FOREGROUND_INDEX:
1274 face->foreground_defaulted_p = 1;
1275 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1276 break;
1277
1278 case LFACE_BACKGROUND_INDEX:
1279 face->background_defaulted_p = 1;
1280 color.pixel = FRAME_BACKGROUND_PIXEL (f);
1281 break;
1282
1283 case LFACE_UNDERLINE_INDEX:
1284 face->underline_defaulted_p = 1;
1285 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1286 break;
1287
1288 case LFACE_OVERLINE_INDEX:
1289 face->overline_color_defaulted_p = 1;
1290 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1291 break;
1292
1293 case LFACE_STRIKE_THROUGH_INDEX:
1294 face->strike_through_color_defaulted_p = 1;
1295 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1296 break;
1297
1298 case LFACE_BOX_INDEX:
1299 face->box_color_defaulted_p = 1;
1300 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1301 break;
1302
1303 default:
1304 emacs_abort ();
1305 }
1306 }
1307#ifdef GLYPH_DEBUG
1308 else
1309 ++ncolors_allocated;
1310#endif
1311
1312 return color.pixel;
1313}
1314
1315
1316#ifdef HAVE_WINDOW_SYSTEM
1317
1318/* Load colors for face FACE which is used on frame F. Colors are
1319 specified by slots LFACE_BACKGROUND_INDEX and LFACE_FOREGROUND_INDEX
1320 of ATTRS. If the background color specified is not supported on F,
1321 try to emulate gray colors with a stipple from Vface_default_stipple. */
1322
1323static void
1324load_face_colors (struct frame *f, struct face *face,
1325 Lisp_Object attrs[LFACE_VECTOR_SIZE])
1326{
1327 Lisp_Object fg, bg;
1328
1329 bg = attrs[LFACE_BACKGROUND_INDEX];
1330 fg = attrs[LFACE_FOREGROUND_INDEX];
1331
1332 /* Swap colors if face is inverse-video. */
1333 if (EQ (attrs[LFACE_INVERSE_INDEX], Qt))
1334 {
1335 Lisp_Object tmp;
1336 tmp = fg;
1337 fg = bg;
1338 bg = tmp;
1339 }
1340
1341 /* Check for support for foreground, not for background because
1342 face_color_supported_p is smart enough to know that grays are
1343 "supported" as background because we are supposed to use stipple
1344 for them. */
1345 if (!face_color_supported_p (f, SSDATA (bg), 0)
1346 && !NILP (Fbitmap_spec_p (Vface_default_stipple)))
1347 {
1348 x_destroy_bitmap (f, face->stipple);
1349 face->stipple = load_pixmap (f, Vface_default_stipple,
1350 &face->pixmap_w, &face->pixmap_h);
1351 }
1352
1353 face->background = load_color (f, face, bg, LFACE_BACKGROUND_INDEX);
1354 face->foreground = load_color (f, face, fg, LFACE_FOREGROUND_INDEX);
1355}
1356
1357
1358/* Free color PIXEL on frame F. */
1359
1360void
1361unload_color (struct frame *f, long unsigned int pixel)
1362{
1363#ifdef HAVE_X_WINDOWS
1364 if (pixel != -1)
1365 {
1366 block_input ();
1367 x_free_colors (f, &pixel, 1);
1368 unblock_input ();
1369 }
1370#endif
1371}
1372
1373
1374/* Free colors allocated for FACE. */
1375
1376static void
1377free_face_colors (struct frame *f, struct face *face)
1378{
1379/* PENDING(NS): need to do something here? */
1380#ifdef HAVE_X_WINDOWS
1381 if (face->colors_copied_bitwise_p)
1382 return;
1383
1384 block_input ();
1385
1386 if (!face->foreground_defaulted_p)
1387 {
1388 x_free_colors (f, &face->foreground, 1);
1389 IF_DEBUG (--ncolors_allocated);
1390 }
1391
1392 if (!face->background_defaulted_p)
1393 {
1394 x_free_colors (f, &face->background, 1);
1395 IF_DEBUG (--ncolors_allocated);
1396 }
1397
1398 if (face->underline_p
1399 && !face->underline_defaulted_p)
1400 {
1401 x_free_colors (f, &face->underline_color, 1);
1402 IF_DEBUG (--ncolors_allocated);
1403 }
1404
1405 if (face->overline_p
1406 && !face->overline_color_defaulted_p)
1407 {
1408 x_free_colors (f, &face->overline_color, 1);
1409 IF_DEBUG (--ncolors_allocated);
1410 }
1411
1412 if (face->strike_through_p
1413 && !face->strike_through_color_defaulted_p)
1414 {
1415 x_free_colors (f, &face->strike_through_color, 1);
1416 IF_DEBUG (--ncolors_allocated);
1417 }
1418
1419 if (face->box != FACE_NO_BOX
1420 && !face->box_color_defaulted_p)
1421 {
1422 x_free_colors (f, &face->box_color, 1);
1423 IF_DEBUG (--ncolors_allocated);
1424 }
1425
1426 unblock_input ();
1427#endif /* HAVE_X_WINDOWS */
1428}
1429
1430#endif /* HAVE_WINDOW_SYSTEM */
1431
1432
1433\f
1434/***********************************************************************
1435 XLFD Font Names
1436 ***********************************************************************/
1437
1438/* An enumerator for each field of an XLFD font name. */
1439
1440enum xlfd_field
1441{
1442 XLFD_FOUNDRY,
1443 XLFD_FAMILY,
1444 XLFD_WEIGHT,
1445 XLFD_SLANT,
1446 XLFD_SWIDTH,
1447 XLFD_ADSTYLE,
1448 XLFD_PIXEL_SIZE,
1449 XLFD_POINT_SIZE,
1450 XLFD_RESX,
1451 XLFD_RESY,
1452 XLFD_SPACING,
1453 XLFD_AVGWIDTH,
1454 XLFD_REGISTRY,
1455 XLFD_ENCODING,
1456 XLFD_LAST
1457};
1458
1459/* An enumerator for each possible slant value of a font. Taken from
1460 the XLFD specification. */
1461
1462enum xlfd_slant
1463{
1464 XLFD_SLANT_UNKNOWN,
1465 XLFD_SLANT_ROMAN,
1466 XLFD_SLANT_ITALIC,
1467 XLFD_SLANT_OBLIQUE,
1468 XLFD_SLANT_REVERSE_ITALIC,
1469 XLFD_SLANT_REVERSE_OBLIQUE,
1470 XLFD_SLANT_OTHER
1471};
1472
1473/* Relative font weight according to XLFD documentation. */
1474
1475enum xlfd_weight
1476{
1477 XLFD_WEIGHT_UNKNOWN,
1478 XLFD_WEIGHT_ULTRA_LIGHT, /* 10 */
1479 XLFD_WEIGHT_EXTRA_LIGHT, /* 20 */
1480 XLFD_WEIGHT_LIGHT, /* 30 */
1481 XLFD_WEIGHT_SEMI_LIGHT, /* 40: SemiLight, Book, ... */
1482 XLFD_WEIGHT_MEDIUM, /* 50: Medium, Normal, Regular, ... */
1483 XLFD_WEIGHT_SEMI_BOLD, /* 60: SemiBold, DemiBold, ... */
1484 XLFD_WEIGHT_BOLD, /* 70: Bold, ... */
1485 XLFD_WEIGHT_EXTRA_BOLD, /* 80: ExtraBold, Heavy, ... */
1486 XLFD_WEIGHT_ULTRA_BOLD /* 90: UltraBold, Black, ... */
1487};
1488
1489/* Relative proportionate width. */
1490
1491enum xlfd_swidth
1492{
1493 XLFD_SWIDTH_UNKNOWN,
1494 XLFD_SWIDTH_ULTRA_CONDENSED, /* 10 */
1495 XLFD_SWIDTH_EXTRA_CONDENSED, /* 20 */
1496 XLFD_SWIDTH_CONDENSED, /* 30: Condensed, Narrow, Compressed, ... */
1497 XLFD_SWIDTH_SEMI_CONDENSED, /* 40: semicondensed */
1498 XLFD_SWIDTH_MEDIUM, /* 50: Medium, Normal, Regular, ... */
1499 XLFD_SWIDTH_SEMI_EXPANDED, /* 60: SemiExpanded, DemiExpanded, ... */
1500 XLFD_SWIDTH_EXPANDED, /* 70: Expanded... */
1501 XLFD_SWIDTH_EXTRA_EXPANDED, /* 80: ExtraExpanded, Wide... */
1502 XLFD_SWIDTH_ULTRA_EXPANDED /* 90: UltraExpanded... */
1503};
1504
1505/* Order by which font selection chooses fonts. The default values
1506 mean `first, find a best match for the font width, then for the
1507 font height, then for weight, then for slant.' This variable can be
1508 set via set-face-font-sort-order. */
1509
1510static int font_sort_order[4];
1511
1512#ifdef HAVE_WINDOW_SYSTEM
1513
1514static enum font_property_index font_props_for_sorting[FONT_SIZE_INDEX];
1515
1516static int
1517compare_fonts_by_sort_order (const void *v1, const void *v2)
1518{
1519 Lisp_Object const *p1 = v1;
1520 Lisp_Object const *p2 = v2;
1521 Lisp_Object font1 = *p1;
1522 Lisp_Object font2 = *p2;
1523 int i;
1524
1525 for (i = 0; i < FONT_SIZE_INDEX; i++)
1526 {
1527 enum font_property_index idx = font_props_for_sorting[i];
1528 Lisp_Object val1 = AREF (font1, idx), val2 = AREF (font2, idx);
1529 int result;
1530
1531 if (idx <= FONT_REGISTRY_INDEX)
1532 {
1533 if (STRINGP (val1))
1534 result = STRINGP (val2) ? strcmp (SSDATA (val1), SSDATA (val2)) : -1;
1535 else
1536 result = STRINGP (val2) ? 1 : 0;
1537 }
1538 else
1539 {
1540 if (INTEGERP (val1))
1541 result = (INTEGERP (val2) && XINT (val1) >= XINT (val2)
1542 ? XINT (val1) > XINT (val2)
1543 : -1);
1544 else
1545 result = INTEGERP (val2) ? 1 : 0;
1546 }
1547 if (result)
1548 return result;
1549 }
1550 return 0;
1551}
1552
1553DEFUN ("x-family-fonts", Fx_family_fonts, Sx_family_fonts, 0, 2, 0,
1554 doc: /* Return a list of available fonts of family FAMILY on FRAME.
1555If FAMILY is omitted or nil, list all families.
1556Otherwise, FAMILY must be a string, possibly containing wildcards
1557`?' and `*'.
1558If FRAME is omitted or nil, use the selected frame.
1559Each element of the result is a vector [FAMILY WIDTH POINT-SIZE WEIGHT
1560SLANT FIXED-P FULL REGISTRY-AND-ENCODING].
1561FAMILY is the font family name. POINT-SIZE is the size of the
1562font in 1/10 pt. WIDTH, WEIGHT, and SLANT are symbols describing the
1563width, weight and slant of the font. These symbols are the same as for
1564face attributes. FIXED-P is non-nil if the font is fixed-pitch.
1565FULL is the full name of the font, and REGISTRY-AND-ENCODING is a string
1566giving the registry and encoding of the font.
1567The result list is sorted according to the current setting of
1568the face font sort order. */)
1569 (Lisp_Object family, Lisp_Object frame)
1570{
1571 Lisp_Object font_spec, list, *drivers, vec;
1572 ptrdiff_t i, nfonts;
1573 EMACS_INT ndrivers;
1574 Lisp_Object result;
1575 USE_SAFE_ALLOCA;
1576
1577 if (NILP (frame))
1578 frame = selected_frame;
1579 CHECK_LIVE_FRAME (frame);
1580
1581 font_spec = Ffont_spec (0, NULL);
1582 if (!NILP (family))
1583 {
1584 CHECK_STRING (family);
1585 font_parse_family_registry (family, Qnil, font_spec);
1586 }
1587
1588 list = font_list_entities (frame, font_spec);
1589 if (NILP (list))
1590 return Qnil;
1591
1592 /* Sort the font entities. */
1593 for (i = 0; i < 4; i++)
1594 switch (font_sort_order[i])
1595 {
1596 case XLFD_SWIDTH:
1597 font_props_for_sorting[i] = FONT_WIDTH_INDEX; break;
1598 case XLFD_POINT_SIZE:
1599 font_props_for_sorting[i] = FONT_SIZE_INDEX; break;
1600 case XLFD_WEIGHT:
1601 font_props_for_sorting[i] = FONT_WEIGHT_INDEX; break;
1602 default:
1603 font_props_for_sorting[i] = FONT_SLANT_INDEX; break;
1604 }
1605 font_props_for_sorting[i++] = FONT_FAMILY_INDEX;
1606 font_props_for_sorting[i++] = FONT_FOUNDRY_INDEX;
1607 font_props_for_sorting[i++] = FONT_ADSTYLE_INDEX;
1608 font_props_for_sorting[i++] = FONT_REGISTRY_INDEX;
1609
1610 ndrivers = XINT (Flength (list));
1611 SAFE_ALLOCA_LISP (drivers, ndrivers);
1612 for (i = 0; i < ndrivers; i++, list = XCDR (list))
1613 drivers[i] = XCAR (list);
1614 vec = Fvconcat (ndrivers, drivers);
1615 nfonts = ASIZE (vec);
1616
1617 qsort (XVECTOR (vec)->contents, nfonts, word_size,
1618 compare_fonts_by_sort_order);
1619
1620 result = Qnil;
1621 for (i = nfonts - 1; i >= 0; --i)
1622 {
1623 Lisp_Object font = AREF (vec, i);
1624 Lisp_Object v = Fmake_vector (make_number (8), Qnil);
1625 int point;
1626 Lisp_Object spacing;
1627
1628 ASET (v, 0, AREF (font, FONT_FAMILY_INDEX));
1629 ASET (v, 1, FONT_WIDTH_SYMBOLIC (font));
1630 point = PIXEL_TO_POINT (XINT (AREF (font, FONT_SIZE_INDEX)) * 10,
1631 XFRAME (frame)->resy);
1632 ASET (v, 2, make_number (point));
1633 ASET (v, 3, FONT_WEIGHT_SYMBOLIC (font));
1634 ASET (v, 4, FONT_SLANT_SYMBOLIC (font));
1635 spacing = Ffont_get (font, QCspacing);
1636 ASET (v, 5, (NILP (spacing) || EQ (spacing, Qp)) ? Qnil : Qt);
1637 ASET (v, 6, Ffont_xlfd_name (font, Qnil));
1638 ASET (v, 7, AREF (font, FONT_REGISTRY_INDEX));
1639
1640 result = Fcons (v, result);
1641 }
1642
1643 SAFE_FREE ();
1644 return result;
1645}
1646
1647DEFUN ("x-list-fonts", Fx_list_fonts, Sx_list_fonts, 1, 5, 0,
1648 doc: /* Return a list of the names of available fonts matching PATTERN.
1649If optional arguments FACE and FRAME are specified, return only fonts
1650the same size as FACE on FRAME.
1651
1652PATTERN should be a string containing a font name in the XLFD,
1653Fontconfig, or GTK format. A font name given in the XLFD format may
1654contain wildcard characters:
1655 the * character matches any substring, and
1656 the ? character matches any single character.
1657 PATTERN is case-insensitive.
1658
1659The return value is a list of strings, suitable as arguments to
1660`set-face-font'.
1661
1662Fonts Emacs can't use may or may not be excluded
1663even if they match PATTERN and FACE.
1664The optional fourth argument MAXIMUM sets a limit on how many
1665fonts to match. The first MAXIMUM fonts are reported.
1666The optional fifth argument WIDTH, if specified, is a number of columns
1667occupied by a character of a font. In that case, return only fonts
1668the WIDTH times as wide as FACE on FRAME. */)
1669 (Lisp_Object pattern, Lisp_Object face, Lisp_Object frame,
1670 Lisp_Object maximum, Lisp_Object width)
1671{
1672 struct frame *f;
1673 int size, avgwidth IF_LINT (= 0);
1674
1675 check_x ();
1676 CHECK_STRING (pattern);
1677
1678 if (! NILP (maximum))
1679 CHECK_NATNUM (maximum);
1680
1681 if (!NILP (width))
1682 CHECK_NUMBER (width);
1683
1684 /* We can't simply call check_x_frame because this function may be
1685 called before any frame is created. */
1686 if (NILP (frame))
1687 frame = selected_frame;
1688 f = frame_or_selected_frame (frame, 2);
1689 if (! FRAME_WINDOW_P (f))
1690 {
1691 /* Perhaps we have not yet created any frame. */
1692 f = NULL;
1693 frame = Qnil;
1694 face = Qnil;
1695 }
1696
1697 /* Determine the width standard for comparison with the fonts we find. */
1698
1699 if (NILP (face))
1700 size = 0;
1701 else
1702 {
1703 /* This is of limited utility since it works with character
1704 widths. Keep it for compatibility. --gerd. */
1705 int face_id = lookup_named_face (f, face, 0);
1706 struct face *width_face = (face_id < 0
1707 ? NULL
1708 : FACE_FROM_ID (f, face_id));
1709
1710 if (width_face && width_face->font)
1711 {
1712 size = width_face->font->pixel_size;
1713 avgwidth = width_face->font->average_width;
1714 }
1715 else
1716 {
1717 size = FRAME_FONT (f)->pixel_size;
1718 avgwidth = FRAME_FONT (f)->average_width;
1719 }
1720 if (!NILP (width))
1721 avgwidth *= XINT (width);
1722 }
1723
1724 {
1725 Lisp_Object font_spec;
1726 Lisp_Object args[2], tail;
1727
1728 font_spec = font_spec_from_name (pattern);
1729 if (!FONTP (font_spec))
1730 signal_error ("Invalid font name", pattern);
1731
1732 if (size)
1733 {
1734 Ffont_put (font_spec, QCsize, make_number (size));
1735 Ffont_put (font_spec, QCavgwidth, make_number (avgwidth));
1736 }
1737 args[0] = Flist_fonts (font_spec, frame, maximum, font_spec);
1738 for (tail = args[0]; CONSP (tail); tail = XCDR (tail))
1739 {
1740 Lisp_Object font_entity;
1741
1742 font_entity = XCAR (tail);
1743 if ((NILP (AREF (font_entity, FONT_SIZE_INDEX))
1744 || XINT (AREF (font_entity, FONT_SIZE_INDEX)) == 0)
1745 && ! NILP (AREF (font_spec, FONT_SIZE_INDEX)))
1746 {
1747 /* This is a scalable font. For backward compatibility,
1748 we set the specified size. */
1749 font_entity = copy_font_spec (font_entity);
1750 ASET (font_entity, FONT_SIZE_INDEX,
1751 AREF (font_spec, FONT_SIZE_INDEX));
1752 }
1753 XSETCAR (tail, Ffont_xlfd_name (font_entity, Qnil));
1754 }
1755 if (NILP (frame))
1756 /* We don't have to check fontsets. */
1757 return args[0];
1758 args[1] = list_fontsets (f, pattern, size);
1759 return Fnconc (2, args);
1760 }
1761}
1762
1763#endif /* HAVE_WINDOW_SYSTEM */
1764
1765\f
1766/***********************************************************************
1767 Lisp Faces
1768 ***********************************************************************/
1769
1770/* Access face attributes of face LFACE, a Lisp vector. */
1771
1772#define LFACE_FAMILY(LFACE) AREF ((LFACE), LFACE_FAMILY_INDEX)
1773#define LFACE_FOUNDRY(LFACE) AREF ((LFACE), LFACE_FOUNDRY_INDEX)
1774#define LFACE_HEIGHT(LFACE) AREF ((LFACE), LFACE_HEIGHT_INDEX)
1775#define LFACE_WEIGHT(LFACE) AREF ((LFACE), LFACE_WEIGHT_INDEX)
1776#define LFACE_SLANT(LFACE) AREF ((LFACE), LFACE_SLANT_INDEX)
1777#define LFACE_UNDERLINE(LFACE) AREF ((LFACE), LFACE_UNDERLINE_INDEX)
1778#define LFACE_INVERSE(LFACE) AREF ((LFACE), LFACE_INVERSE_INDEX)
1779#define LFACE_FOREGROUND(LFACE) AREF ((LFACE), LFACE_FOREGROUND_INDEX)
1780#define LFACE_BACKGROUND(LFACE) AREF ((LFACE), LFACE_BACKGROUND_INDEX)
1781#define LFACE_STIPPLE(LFACE) AREF ((LFACE), LFACE_STIPPLE_INDEX)
1782#define LFACE_SWIDTH(LFACE) AREF ((LFACE), LFACE_SWIDTH_INDEX)
1783#define LFACE_OVERLINE(LFACE) AREF ((LFACE), LFACE_OVERLINE_INDEX)
1784#define LFACE_STRIKE_THROUGH(LFACE) AREF ((LFACE), LFACE_STRIKE_THROUGH_INDEX)
1785#define LFACE_BOX(LFACE) AREF ((LFACE), LFACE_BOX_INDEX)
1786#define LFACE_FONT(LFACE) AREF ((LFACE), LFACE_FONT_INDEX)
1787#define LFACE_INHERIT(LFACE) AREF ((LFACE), LFACE_INHERIT_INDEX)
1788#define LFACE_FONTSET(LFACE) AREF ((LFACE), LFACE_FONTSET_INDEX)
1789
1790/* Non-zero if LFACE is a Lisp face. A Lisp face is a vector of size
1791 LFACE_VECTOR_SIZE which has the symbol `face' in slot 0. */
1792
1793#define LFACEP(LFACE) \
1794 (VECTORP (LFACE) \
1795 && ASIZE (LFACE) == LFACE_VECTOR_SIZE \
1796 && EQ (AREF (LFACE, 0), Qface))
1797
1798
1799#ifdef GLYPH_DEBUG
1800
1801/* Check consistency of Lisp face attribute vector ATTRS. */
1802
1803static void
1804check_lface_attrs (Lisp_Object attrs[LFACE_VECTOR_SIZE])
1805{
1806 eassert (UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
1807 || IGNORE_DEFFACE_P (attrs[LFACE_FAMILY_INDEX])
1808 || STRINGP (attrs[LFACE_FAMILY_INDEX]));
1809 eassert (UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
1810 || IGNORE_DEFFACE_P (attrs[LFACE_FOUNDRY_INDEX])
1811 || STRINGP (attrs[LFACE_FOUNDRY_INDEX]));
1812 eassert (UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
1813 || IGNORE_DEFFACE_P (attrs[LFACE_SWIDTH_INDEX])
1814 || SYMBOLP (attrs[LFACE_SWIDTH_INDEX]));
1815 eassert (UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
1816 || IGNORE_DEFFACE_P (attrs[LFACE_HEIGHT_INDEX])
1817 || INTEGERP (attrs[LFACE_HEIGHT_INDEX])
1818 || FLOATP (attrs[LFACE_HEIGHT_INDEX])
1819 || FUNCTIONP (attrs[LFACE_HEIGHT_INDEX]));
1820 eassert (UNSPECIFIEDP (attrs[LFACE_WEIGHT_INDEX])
1821 || IGNORE_DEFFACE_P (attrs[LFACE_WEIGHT_INDEX])
1822 || SYMBOLP (attrs[LFACE_WEIGHT_INDEX]));
1823 eassert (UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX])
1824 || IGNORE_DEFFACE_P (attrs[LFACE_SLANT_INDEX])
1825 || SYMBOLP (attrs[LFACE_SLANT_INDEX]));
1826 eassert (UNSPECIFIEDP (attrs[LFACE_UNDERLINE_INDEX])
1827 || IGNORE_DEFFACE_P (attrs[LFACE_UNDERLINE_INDEX])
1828 || SYMBOLP (attrs[LFACE_UNDERLINE_INDEX])
1829 || STRINGP (attrs[LFACE_UNDERLINE_INDEX])
1830 || CONSP (attrs[LFACE_UNDERLINE_INDEX]));
1831 eassert (UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
1832 || IGNORE_DEFFACE_P (attrs[LFACE_OVERLINE_INDEX])
1833 || SYMBOLP (attrs[LFACE_OVERLINE_INDEX])
1834 || STRINGP (attrs[LFACE_OVERLINE_INDEX]));
1835 eassert (UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
1836 || IGNORE_DEFFACE_P (attrs[LFACE_STRIKE_THROUGH_INDEX])
1837 || SYMBOLP (attrs[LFACE_STRIKE_THROUGH_INDEX])
1838 || STRINGP (attrs[LFACE_STRIKE_THROUGH_INDEX]));
1839 eassert (UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
1840 || IGNORE_DEFFACE_P (attrs[LFACE_BOX_INDEX])
1841 || SYMBOLP (attrs[LFACE_BOX_INDEX])
1842 || STRINGP (attrs[LFACE_BOX_INDEX])
1843 || INTEGERP (attrs[LFACE_BOX_INDEX])
1844 || CONSP (attrs[LFACE_BOX_INDEX]));
1845 eassert (UNSPECIFIEDP (attrs[LFACE_INVERSE_INDEX])
1846 || IGNORE_DEFFACE_P (attrs[LFACE_INVERSE_INDEX])
1847 || SYMBOLP (attrs[LFACE_INVERSE_INDEX]));
1848 eassert (UNSPECIFIEDP (attrs[LFACE_FOREGROUND_INDEX])
1849 || IGNORE_DEFFACE_P (attrs[LFACE_FOREGROUND_INDEX])
1850 || STRINGP (attrs[LFACE_FOREGROUND_INDEX]));
1851 eassert (UNSPECIFIEDP (attrs[LFACE_BACKGROUND_INDEX])
1852 || IGNORE_DEFFACE_P (attrs[LFACE_BACKGROUND_INDEX])
1853 || STRINGP (attrs[LFACE_BACKGROUND_INDEX]));
1854 eassert (UNSPECIFIEDP (attrs[LFACE_INHERIT_INDEX])
1855 || IGNORE_DEFFACE_P (attrs[LFACE_INHERIT_INDEX])
1856 || NILP (attrs[LFACE_INHERIT_INDEX])
1857 || SYMBOLP (attrs[LFACE_INHERIT_INDEX])
1858 || CONSP (attrs[LFACE_INHERIT_INDEX]));
1859#ifdef HAVE_WINDOW_SYSTEM
1860 eassert (UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
1861 || IGNORE_DEFFACE_P (attrs[LFACE_STIPPLE_INDEX])
1862 || SYMBOLP (attrs[LFACE_STIPPLE_INDEX])
1863 || !NILP (Fbitmap_spec_p (attrs[LFACE_STIPPLE_INDEX])));
1864 eassert (UNSPECIFIEDP (attrs[LFACE_FONT_INDEX])
1865 || IGNORE_DEFFACE_P (attrs[LFACE_FONT_INDEX])
1866 || FONTP (attrs[LFACE_FONT_INDEX]));
1867 eassert (UNSPECIFIEDP (attrs[LFACE_FONTSET_INDEX])
1868 || STRINGP (attrs[LFACE_FONTSET_INDEX])
1869 || NILP (attrs[LFACE_FONTSET_INDEX]));
1870#endif
1871}
1872
1873
1874/* Check consistency of attributes of Lisp face LFACE (a Lisp vector). */
1875
1876static void
1877check_lface (Lisp_Object lface)
1878{
1879 if (!NILP (lface))
1880 {
1881 eassert (LFACEP (lface));
1882 check_lface_attrs (XVECTOR (lface)->contents);
1883 }
1884}
1885
1886#else /* not GLYPH_DEBUG */
1887
1888#define check_lface_attrs(attrs) (void) 0
1889#define check_lface(lface) (void) 0
1890
1891#endif /* GLYPH_DEBUG */
1892
1893
1894\f
1895/* Face-merge cycle checking. */
1896
1897enum named_merge_point_kind
1898{
1899 NAMED_MERGE_POINT_NORMAL,
1900 NAMED_MERGE_POINT_REMAP
1901};
1902
1903/* A `named merge point' is simply a point during face-merging where we
1904 look up a face by name. We keep a stack of which named lookups we're
1905 currently processing so that we can easily detect cycles, using a
1906 linked- list of struct named_merge_point structures, typically
1907 allocated on the stack frame of the named lookup functions which are
1908 active (so no consing is required). */
1909struct named_merge_point
1910{
1911 Lisp_Object face_name;
1912 enum named_merge_point_kind named_merge_point_kind;
1913 struct named_merge_point *prev;
1914};
1915
1916
1917/* If a face merging cycle is detected for FACE_NAME, return 0,
1918 otherwise add NEW_NAMED_MERGE_POINT, which is initialized using
1919 FACE_NAME and NAMED_MERGE_POINT_KIND, as the head of the linked list
1920 pointed to by NAMED_MERGE_POINTS, and return 1. */
1921
1922static int
1923push_named_merge_point (struct named_merge_point *new_named_merge_point,
1924 Lisp_Object face_name,
1925 enum named_merge_point_kind named_merge_point_kind,
1926 struct named_merge_point **named_merge_points)
1927{
1928 struct named_merge_point *prev;
1929
1930 for (prev = *named_merge_points; prev; prev = prev->prev)
1931 if (EQ (face_name, prev->face_name))
1932 {
1933 if (prev->named_merge_point_kind == named_merge_point_kind)
1934 /* A cycle, so fail. */
1935 return 0;
1936 else if (prev->named_merge_point_kind == NAMED_MERGE_POINT_REMAP)
1937 /* A remap `hides ' any previous normal merge points
1938 (because the remap means that it's actually different face),
1939 so as we know the current merge point must be normal, we
1940 can just assume it's OK. */
1941 break;
1942 }
1943
1944 new_named_merge_point->face_name = face_name;
1945 new_named_merge_point->named_merge_point_kind = named_merge_point_kind;
1946 new_named_merge_point->prev = *named_merge_points;
1947
1948 *named_merge_points = new_named_merge_point;
1949
1950 return 1;
1951}
1952
1953\f
1954/* Resolve face name FACE_NAME. If FACE_NAME is a string, intern it
1955 to make it a symbol. If FACE_NAME is an alias for another face,
1956 return that face's name.
1957
1958 Return default face in case of errors. */
1959
1960static Lisp_Object
1961resolve_face_name (Lisp_Object face_name, int signal_p)
1962{
1963 Lisp_Object orig_face;
1964 Lisp_Object tortoise, hare;
1965
1966 if (STRINGP (face_name))
1967 face_name = intern (SSDATA (face_name));
1968
1969 if (NILP (face_name) || !SYMBOLP (face_name))
1970 return face_name;
1971
1972 orig_face = face_name;
1973 tortoise = hare = face_name;
1974
1975 while (1)
1976 {
1977 face_name = hare;
1978 hare = Fget (hare, Qface_alias);
1979 if (NILP (hare) || !SYMBOLP (hare))
1980 break;
1981
1982 face_name = hare;
1983 hare = Fget (hare, Qface_alias);
1984 if (NILP (hare) || !SYMBOLP (hare))
1985 break;
1986
1987 tortoise = Fget (tortoise, Qface_alias);
1988 if (EQ (hare, tortoise))
1989 {
1990 if (signal_p)
1991 xsignal1 (Qcircular_list, orig_face);
1992 return Qdefault;
1993 }
1994 }
1995
1996 return face_name;
1997}
1998
1999
2000/* Return the face definition of FACE_NAME on frame F. F null means
2001 return the definition for new frames. FACE_NAME may be a string or
2002 a symbol (apparently Emacs 20.2 allowed strings as face names in
2003 face text properties; Ediff uses that). If SIGNAL_P is non-zero,
2004 signal an error if FACE_NAME is not a valid face name. If SIGNAL_P
2005 is zero, value is nil if FACE_NAME is not a valid face name. */
2006static Lisp_Object
2007lface_from_face_name_no_resolve (struct frame *f, Lisp_Object face_name,
2008 int signal_p)
2009{
2010 Lisp_Object lface;
2011
2012 if (f)
2013 lface = assq_no_quit (face_name, f->face_alist);
2014 else
2015 lface = assq_no_quit (face_name, Vface_new_frame_defaults);
2016
2017 if (CONSP (lface))
2018 lface = XCDR (lface);
2019 else if (signal_p)
2020 signal_error ("Invalid face", face_name);
2021
2022 check_lface (lface);
2023
2024 return lface;
2025}
2026
2027/* Return the face definition of FACE_NAME on frame F. F null means
2028 return the definition for new frames. FACE_NAME may be a string or
2029 a symbol (apparently Emacs 20.2 allowed strings as face names in
2030 face text properties; Ediff uses that). If FACE_NAME is an alias
2031 for another face, return that face's definition. If SIGNAL_P is
2032 non-zero, signal an error if FACE_NAME is not a valid face name.
2033 If SIGNAL_P is zero, value is nil if FACE_NAME is not a valid face
2034 name. */
2035static Lisp_Object
2036lface_from_face_name (struct frame *f, Lisp_Object face_name, int signal_p)
2037{
2038 face_name = resolve_face_name (face_name, signal_p);
2039 return lface_from_face_name_no_resolve (f, face_name, signal_p);
2040}
2041
2042
2043/* Get face attributes of face FACE_NAME from frame-local faces on
2044 frame F. Store the resulting attributes in ATTRS which must point
2045 to a vector of Lisp_Objects of size LFACE_VECTOR_SIZE. If SIGNAL_P
2046 is non-zero, signal an error if FACE_NAME does not name a face.
2047 Otherwise, value is zero if FACE_NAME is not a face. */
2048
2049static int
2050get_lface_attributes_no_remap (struct frame *f, Lisp_Object face_name,
2051 Lisp_Object attrs[LFACE_VECTOR_SIZE],
2052 int signal_p)
2053{
2054 Lisp_Object lface;
2055
2056 lface = lface_from_face_name_no_resolve (f, face_name, signal_p);
2057
2058 if (! NILP (lface))
2059 memcpy (attrs, XVECTOR (lface)->contents,
2060 LFACE_VECTOR_SIZE * sizeof *attrs);
2061
2062 return !NILP (lface);
2063}
2064
2065/* Get face attributes of face FACE_NAME from frame-local faces on frame
2066 F. Store the resulting attributes in ATTRS which must point to a
2067 vector of Lisp_Objects of size LFACE_VECTOR_SIZE. If FACE_NAME is an
2068 alias for another face, use that face's definition. If SIGNAL_P is
2069 non-zero, signal an error if FACE_NAME does not name a face.
2070 Otherwise, value is zero if FACE_NAME is not a face. */
2071
2072static int
2073get_lface_attributes (struct frame *f, Lisp_Object face_name,
2074 Lisp_Object attrs[LFACE_VECTOR_SIZE], int signal_p,
2075 struct named_merge_point *named_merge_points)
2076{
2077 Lisp_Object face_remapping;
2078
2079 face_name = resolve_face_name (face_name, signal_p);
2080
2081 /* See if SYMBOL has been remapped to some other face (usually this
2082 is done buffer-locally). */
2083 face_remapping = assq_no_quit (face_name, Vface_remapping_alist);
2084 if (CONSP (face_remapping))
2085 {
2086 struct named_merge_point named_merge_point;
2087
2088 if (push_named_merge_point (&named_merge_point,
2089 face_name, NAMED_MERGE_POINT_REMAP,
2090 &named_merge_points))
2091 {
2092 int i;
2093
2094 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2095 attrs[i] = Qunspecified;
2096
2097 return merge_face_ref (f, XCDR (face_remapping), attrs,
2098 signal_p, named_merge_points);
2099 }
2100 }
2101
2102 /* Default case, no remapping. */
2103 return get_lface_attributes_no_remap (f, face_name, attrs, signal_p);
2104}
2105
2106
2107/* Non-zero if all attributes in face attribute vector ATTRS are
2108 specified, i.e. are non-nil. */
2109
2110static int
2111lface_fully_specified_p (Lisp_Object attrs[LFACE_VECTOR_SIZE])
2112{
2113 int i;
2114
2115 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2116 if (i != LFACE_FONT_INDEX && i != LFACE_INHERIT_INDEX)
2117 if ((UNSPECIFIEDP (attrs[i]) || IGNORE_DEFFACE_P (attrs[i])))
2118 break;
2119
2120 return i == LFACE_VECTOR_SIZE;
2121}
2122
2123#ifdef HAVE_WINDOW_SYSTEM
2124
2125/* Set font-related attributes of Lisp face LFACE from FONT-OBJECT.
2126 If FORCE_P is zero, set only unspecified attributes of LFACE. The
2127 exception is `font' attribute. It is set to FONT_OBJECT regardless
2128 of FORCE_P. */
2129
2130static int
2131set_lface_from_font (struct frame *f, Lisp_Object lface,
2132 Lisp_Object font_object, int force_p)
2133{
2134 Lisp_Object val;
2135 struct font *font = XFONT_OBJECT (font_object);
2136
2137 /* Set attributes only if unspecified, otherwise face defaults for
2138 new frames would never take effect. If the font doesn't have a
2139 specific property, set a normal value for that. */
2140
2141 if (force_p || UNSPECIFIEDP (LFACE_FAMILY (lface)))
2142 {
2143 Lisp_Object family = AREF (font_object, FONT_FAMILY_INDEX);
2144
2145 ASET (lface, LFACE_FAMILY_INDEX, SYMBOL_NAME (family));
2146 }
2147
2148 if (force_p || UNSPECIFIEDP (LFACE_FOUNDRY (lface)))
2149 {
2150 Lisp_Object foundry = AREF (font_object, FONT_FOUNDRY_INDEX);
2151
2152 ASET (lface, LFACE_FOUNDRY_INDEX, SYMBOL_NAME (foundry));
2153 }
2154
2155 if (force_p || UNSPECIFIEDP (LFACE_HEIGHT (lface)))
2156 {
2157 int pt = PIXEL_TO_POINT (font->pixel_size * 10, f->resy);
2158
2159 eassert (pt > 0);
2160 ASET (lface, LFACE_HEIGHT_INDEX, make_number (pt));
2161 }
2162
2163 if (force_p || UNSPECIFIEDP (LFACE_WEIGHT (lface)))
2164 {
2165 val = FONT_WEIGHT_FOR_FACE (font_object);
2166 ASET (lface, LFACE_WEIGHT_INDEX, ! NILP (val) ? val :Qnormal);
2167 }
2168 if (force_p || UNSPECIFIEDP (LFACE_SLANT (lface)))
2169 {
2170 val = FONT_SLANT_FOR_FACE (font_object);
2171 ASET (lface, LFACE_SLANT_INDEX, ! NILP (val) ? val : Qnormal);
2172 }
2173 if (force_p || UNSPECIFIEDP (LFACE_SWIDTH (lface)))
2174 {
2175 val = FONT_WIDTH_FOR_FACE (font_object);
2176 ASET (lface, LFACE_SWIDTH_INDEX, ! NILP (val) ? val : Qnormal);
2177 }
2178
2179 ASET (lface, LFACE_FONT_INDEX, font_object);
2180 return 1;
2181}
2182
2183#endif /* HAVE_WINDOW_SYSTEM */
2184
2185
2186/* Merges the face height FROM with the face height TO, and returns the
2187 merged height. If FROM is an invalid height, then INVALID is
2188 returned instead. FROM and TO may be either absolute face heights or
2189 `relative' heights; the returned value is always an absolute height
2190 unless both FROM and TO are relative. */
2191
2192static Lisp_Object
2193merge_face_heights (Lisp_Object from, Lisp_Object to, Lisp_Object invalid)
2194{
2195 Lisp_Object result = invalid;
2196
2197 if (INTEGERP (from))
2198 /* FROM is absolute, just use it as is. */
2199 result = from;
2200 else if (FLOATP (from))
2201 /* FROM is a scale, use it to adjust TO. */
2202 {
2203 if (INTEGERP (to))
2204 /* relative X absolute => absolute */
2205 result = make_number (XFLOAT_DATA (from) * XINT (to));
2206 else if (FLOATP (to))
2207 /* relative X relative => relative */
2208 result = make_float (XFLOAT_DATA (from) * XFLOAT_DATA (to));
2209 else if (UNSPECIFIEDP (to))
2210 result = from;
2211 }
2212 else if (FUNCTIONP (from))
2213 /* FROM is a function, which use to adjust TO. */
2214 {
2215 /* Call function with current height as argument.
2216 From is the new height. */
2217 result = safe_call1 (from, to);
2218
2219 /* Ensure that if TO was absolute, so is the result. */
2220 if (INTEGERP (to) && !INTEGERP (result))
2221 result = invalid;
2222 }
2223
2224 return result;
2225}
2226
2227
2228/* Merge two Lisp face attribute vectors on frame F, FROM and TO, and
2229 store the resulting attributes in TO, which must be already be
2230 completely specified and contain only absolute attributes. Every
2231 specified attribute of FROM overrides the corresponding attribute of
2232 TO; relative attributes in FROM are merged with the absolute value in
2233 TO and replace it. NAMED_MERGE_POINTS is used internally to detect
2234 loops in face inheritance/remapping; it should be 0 when called from
2235 other places. */
2236
2237static void
2238merge_face_vectors (struct frame *f, Lisp_Object *from, Lisp_Object *to,
2239 struct named_merge_point *named_merge_points)
2240{
2241 int i;
2242 Lisp_Object font = Qnil;
2243
2244 /* If FROM inherits from some other faces, merge their attributes into
2245 TO before merging FROM's direct attributes. Note that an :inherit
2246 attribute of `unspecified' is the same as one of nil; we never
2247 merge :inherit attributes, so nil is more correct, but lots of
2248 other code uses `unspecified' as a generic value for face attributes. */
2249 if (!UNSPECIFIEDP (from[LFACE_INHERIT_INDEX])
2250 && !NILP (from[LFACE_INHERIT_INDEX]))
2251 merge_face_ref (f, from[LFACE_INHERIT_INDEX], to, 0, named_merge_points);
2252
2253 if (FONT_SPEC_P (from[LFACE_FONT_INDEX]))
2254 {
2255 if (!UNSPECIFIEDP (to[LFACE_FONT_INDEX]))
2256 font = merge_font_spec (from[LFACE_FONT_INDEX], to[LFACE_FONT_INDEX]);
2257 else
2258 font = copy_font_spec (from[LFACE_FONT_INDEX]);
2259 to[LFACE_FONT_INDEX] = font;
2260 }
2261
2262 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2263 if (!UNSPECIFIEDP (from[i]))
2264 {
2265 if (i == LFACE_HEIGHT_INDEX && !INTEGERP (from[i]))
2266 {
2267 to[i] = merge_face_heights (from[i], to[i], to[i]);
2268 font_clear_prop (to, FONT_SIZE_INDEX);
2269 }
2270 else if (i != LFACE_FONT_INDEX && ! EQ (to[i], from[i]))
2271 {
2272 to[i] = from[i];
2273 if (i >= LFACE_FAMILY_INDEX && i <=LFACE_SLANT_INDEX)
2274 font_clear_prop (to,
2275 (i == LFACE_FAMILY_INDEX ? FONT_FAMILY_INDEX
2276 : i == LFACE_FOUNDRY_INDEX ? FONT_FOUNDRY_INDEX
2277 : i == LFACE_SWIDTH_INDEX ? FONT_WIDTH_INDEX
2278 : i == LFACE_HEIGHT_INDEX ? FONT_SIZE_INDEX
2279 : i == LFACE_WEIGHT_INDEX ? FONT_WEIGHT_INDEX
2280 : FONT_SLANT_INDEX));
2281 }
2282 }
2283
2284 /* If FROM specifies a font spec, make its contents take precedence
2285 over :family and other attributes. This is needed for face
2286 remapping using :font to work. */
2287
2288 if (!NILP (font))
2289 {
2290 if (! NILP (AREF (font, FONT_FOUNDRY_INDEX)))
2291 to[LFACE_FOUNDRY_INDEX] = SYMBOL_NAME (AREF (font, FONT_FOUNDRY_INDEX));
2292 if (! NILP (AREF (font, FONT_FAMILY_INDEX)))
2293 to[LFACE_FAMILY_INDEX] = SYMBOL_NAME (AREF (font, FONT_FAMILY_INDEX));
2294 if (! NILP (AREF (font, FONT_WEIGHT_INDEX)))
2295 to[LFACE_WEIGHT_INDEX] = FONT_WEIGHT_FOR_FACE (font);
2296 if (! NILP (AREF (font, FONT_SLANT_INDEX)))
2297 to[LFACE_SLANT_INDEX] = FONT_SLANT_FOR_FACE (font);
2298 if (! NILP (AREF (font, FONT_WIDTH_INDEX)))
2299 to[LFACE_SWIDTH_INDEX] = FONT_WIDTH_FOR_FACE (font);
2300 ASET (font, FONT_SIZE_INDEX, Qnil);
2301 }
2302
2303 /* TO is always an absolute face, which should inherit from nothing.
2304 We blindly copy the :inherit attribute above and fix it up here. */
2305 to[LFACE_INHERIT_INDEX] = Qnil;
2306}
2307
2308/* Merge the named face FACE_NAME on frame F, into the vector of face
2309 attributes TO. NAMED_MERGE_POINTS is used to detect loops in face
2310 inheritance. Returns true if FACE_NAME is a valid face name and
2311 merging succeeded. */
2312
2313static int
2314merge_named_face (struct frame *f, Lisp_Object face_name, Lisp_Object *to,
2315 struct named_merge_point *named_merge_points)
2316{
2317 struct named_merge_point named_merge_point;
2318
2319 if (push_named_merge_point (&named_merge_point,
2320 face_name, NAMED_MERGE_POINT_NORMAL,
2321 &named_merge_points))
2322 {
2323 struct gcpro gcpro1;
2324 Lisp_Object from[LFACE_VECTOR_SIZE];
2325 int ok = get_lface_attributes (f, face_name, from, 0, named_merge_points);
2326
2327 if (ok)
2328 {
2329 GCPRO1 (named_merge_point.face_name);
2330 merge_face_vectors (f, from, to, named_merge_points);
2331 UNGCPRO;
2332 }
2333
2334 return ok;
2335 }
2336 else
2337 return 0;
2338}
2339
2340
2341/* Merge face attributes from the lisp `face reference' FACE_REF on
2342 frame F into the face attribute vector TO. If ERR_MSGS is non-zero,
2343 problems with FACE_REF cause an error message to be shown. Return
2344 non-zero if no errors occurred (regardless of the value of ERR_MSGS).
2345 NAMED_MERGE_POINTS is used to detect loops in face inheritance or
2346 list structure; it may be 0 for most callers.
2347
2348 FACE_REF may be a single face specification or a list of such
2349 specifications. Each face specification can be:
2350
2351 1. A symbol or string naming a Lisp face.
2352
2353 2. A property list of the form (KEYWORD VALUE ...) where each
2354 KEYWORD is a face attribute name, and value is an appropriate value
2355 for that attribute.
2356
2357 3. Conses or the form (FOREGROUND-COLOR . COLOR) or
2358 (BACKGROUND-COLOR . COLOR) where COLOR is a color name. This is
2359 for compatibility with 20.2.
2360
2361 Face specifications earlier in lists take precedence over later
2362 specifications. */
2363
2364static int
2365merge_face_ref (struct frame *f, Lisp_Object face_ref, Lisp_Object *to,
2366 int err_msgs, struct named_merge_point *named_merge_points)
2367{
2368 int ok = 1; /* Succeed without an error? */
2369
2370 if (CONSP (face_ref))
2371 {
2372 Lisp_Object first = XCAR (face_ref);
2373
2374 if (EQ (first, Qforeground_color)
2375 || EQ (first, Qbackground_color))
2376 {
2377 /* One of (FOREGROUND-COLOR . COLOR) or (BACKGROUND-COLOR
2378 . COLOR). COLOR must be a string. */
2379 Lisp_Object color_name = XCDR (face_ref);
2380 Lisp_Object color = first;
2381
2382 if (STRINGP (color_name))
2383 {
2384 if (EQ (color, Qforeground_color))
2385 to[LFACE_FOREGROUND_INDEX] = color_name;
2386 else
2387 to[LFACE_BACKGROUND_INDEX] = color_name;
2388 }
2389 else
2390 {
2391 if (err_msgs)
2392 add_to_log ("Invalid face color", color_name, Qnil);
2393 ok = 0;
2394 }
2395 }
2396 else if (SYMBOLP (first)
2397 && *SDATA (SYMBOL_NAME (first)) == ':')
2398 {
2399 /* Assume this is the property list form. */
2400 while (CONSP (face_ref) && CONSP (XCDR (face_ref)))
2401 {
2402 Lisp_Object keyword = XCAR (face_ref);
2403 Lisp_Object value = XCAR (XCDR (face_ref));
2404 int err = 0;
2405
2406 /* Specifying `unspecified' is a no-op. */
2407 if (EQ (value, Qunspecified))
2408 ;
2409 else if (EQ (keyword, QCfamily))
2410 {
2411 if (STRINGP (value))
2412 {
2413 to[LFACE_FAMILY_INDEX] = value;
2414 font_clear_prop (to, FONT_FAMILY_INDEX);
2415 }
2416 else
2417 err = 1;
2418 }
2419 else if (EQ (keyword, QCfoundry))
2420 {
2421 if (STRINGP (value))
2422 {
2423 to[LFACE_FOUNDRY_INDEX] = value;
2424 font_clear_prop (to, FONT_FOUNDRY_INDEX);
2425 }
2426 else
2427 err = 1;
2428 }
2429 else if (EQ (keyword, QCheight))
2430 {
2431 Lisp_Object new_height =
2432 merge_face_heights (value, to[LFACE_HEIGHT_INDEX], Qnil);
2433
2434 if (! NILP (new_height))
2435 {
2436 to[LFACE_HEIGHT_INDEX] = new_height;
2437 font_clear_prop (to, FONT_SIZE_INDEX);
2438 }
2439 else
2440 err = 1;
2441 }
2442 else if (EQ (keyword, QCweight))
2443 {
2444 if (SYMBOLP (value) && FONT_WEIGHT_NAME_NUMERIC (value) >= 0)
2445 {
2446 to[LFACE_WEIGHT_INDEX] = value;
2447 font_clear_prop (to, FONT_WEIGHT_INDEX);
2448 }
2449 else
2450 err = 1;
2451 }
2452 else if (EQ (keyword, QCslant))
2453 {
2454 if (SYMBOLP (value) && FONT_SLANT_NAME_NUMERIC (value) >= 0)
2455 {
2456 to[LFACE_SLANT_INDEX] = value;
2457 font_clear_prop (to, FONT_SLANT_INDEX);
2458 }
2459 else
2460 err = 1;
2461 }
2462 else if (EQ (keyword, QCunderline))
2463 {
2464 if (EQ (value, Qt)
2465 || NILP (value)
2466 || STRINGP (value)
2467 || CONSP (value))
2468 to[LFACE_UNDERLINE_INDEX] = value;
2469 else
2470 err = 1;
2471 }
2472 else if (EQ (keyword, QCoverline))
2473 {
2474 if (EQ (value, Qt)
2475 || NILP (value)
2476 || STRINGP (value))
2477 to[LFACE_OVERLINE_INDEX] = value;
2478 else
2479 err = 1;
2480 }
2481 else if (EQ (keyword, QCstrike_through))
2482 {
2483 if (EQ (value, Qt)
2484 || NILP (value)
2485 || STRINGP (value))
2486 to[LFACE_STRIKE_THROUGH_INDEX] = value;
2487 else
2488 err = 1;
2489 }
2490 else if (EQ (keyword, QCbox))
2491 {
2492 if (EQ (value, Qt))
2493 value = make_number (1);
2494 if (INTEGERP (value)
2495 || STRINGP (value)
2496 || CONSP (value)
2497 || NILP (value))
2498 to[LFACE_BOX_INDEX] = value;
2499 else
2500 err = 1;
2501 }
2502 else if (EQ (keyword, QCinverse_video)
2503 || EQ (keyword, QCreverse_video))
2504 {
2505 if (EQ (value, Qt) || NILP (value))
2506 to[LFACE_INVERSE_INDEX] = value;
2507 else
2508 err = 1;
2509 }
2510 else if (EQ (keyword, QCforeground))
2511 {
2512 if (STRINGP (value))
2513 to[LFACE_FOREGROUND_INDEX] = value;
2514 else
2515 err = 1;
2516 }
2517 else if (EQ (keyword, QCbackground))
2518 {
2519 if (STRINGP (value))
2520 to[LFACE_BACKGROUND_INDEX] = value;
2521 else
2522 err = 1;
2523 }
2524 else if (EQ (keyword, QCstipple))
2525 {
2526#if defined (HAVE_WINDOW_SYSTEM)
2527 Lisp_Object pixmap_p = Fbitmap_spec_p (value);
2528 if (!NILP (pixmap_p))
2529 to[LFACE_STIPPLE_INDEX] = value;
2530 else
2531 err = 1;
2532#endif /* HAVE_WINDOW_SYSTEM */
2533 }
2534 else if (EQ (keyword, QCwidth))
2535 {
2536 if (SYMBOLP (value) && FONT_WIDTH_NAME_NUMERIC (value) >= 0)
2537 {
2538 to[LFACE_SWIDTH_INDEX] = value;
2539 font_clear_prop (to, FONT_WIDTH_INDEX);
2540 }
2541 else
2542 err = 1;
2543 }
2544 else if (EQ (keyword, QCfont))
2545 {
2546 if (FONTP (value))
2547 to[LFACE_FONT_INDEX] = value;
2548 else
2549 err = 1;
2550 }
2551 else if (EQ (keyword, QCinherit))
2552 {
2553 /* This is not really very useful; it's just like a
2554 normal face reference. */
2555 if (! merge_face_ref (f, value, to,
2556 err_msgs, named_merge_points))
2557 err = 1;
2558 }
2559 else
2560 err = 1;
2561
2562 if (err)
2563 {
2564 add_to_log ("Invalid face attribute %S %S", keyword, value);
2565 ok = 0;
2566 }
2567
2568 face_ref = XCDR (XCDR (face_ref));
2569 }
2570 }
2571 else
2572 {
2573 /* This is a list of face refs. Those at the beginning of the
2574 list take precedence over what follows, so we have to merge
2575 from the end backwards. */
2576 Lisp_Object next = XCDR (face_ref);
2577
2578 if (! NILP (next))
2579 ok = merge_face_ref (f, next, to, err_msgs, named_merge_points);
2580
2581 if (! merge_face_ref (f, first, to, err_msgs, named_merge_points))
2582 ok = 0;
2583 }
2584 }
2585 else
2586 {
2587 /* FACE_REF ought to be a face name. */
2588 ok = merge_named_face (f, face_ref, to, named_merge_points);
2589 if (!ok && err_msgs)
2590 add_to_log ("Invalid face reference: %s", face_ref, Qnil);
2591 }
2592
2593 return ok;
2594}
2595
2596
2597DEFUN ("internal-make-lisp-face", Finternal_make_lisp_face,
2598 Sinternal_make_lisp_face, 1, 2, 0,
2599 doc: /* Make FACE, a symbol, a Lisp face with all attributes nil.
2600If FACE was not known as a face before, create a new one.
2601If optional argument FRAME is specified, make a frame-local face
2602for that frame. Otherwise operate on the global face definition.
2603Value is a vector of face attributes. */)
2604 (Lisp_Object face, Lisp_Object frame)
2605{
2606 Lisp_Object global_lface, lface;
2607 struct frame *f;
2608 int i;
2609
2610 CHECK_SYMBOL (face);
2611 global_lface = lface_from_face_name (NULL, face, 0);
2612
2613 if (!NILP (frame))
2614 {
2615 CHECK_LIVE_FRAME (frame);
2616 f = XFRAME (frame);
2617 lface = lface_from_face_name (f, face, 0);
2618 }
2619 else
2620 f = NULL, lface = Qnil;
2621
2622 /* Add a global definition if there is none. */
2623 if (NILP (global_lface))
2624 {
2625 global_lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
2626 Qunspecified);
2627 ASET (global_lface, 0, Qface);
2628 Vface_new_frame_defaults = Fcons (Fcons (face, global_lface),
2629 Vface_new_frame_defaults);
2630
2631 /* Assign the new Lisp face a unique ID. The mapping from Lisp
2632 face id to Lisp face is given by the vector lface_id_to_name.
2633 The mapping from Lisp face to Lisp face id is given by the
2634 property `face' of the Lisp face name. */
2635 if (next_lface_id == lface_id_to_name_size)
2636 lface_id_to_name =
2637 xpalloc (lface_id_to_name, &lface_id_to_name_size, 1, MAX_FACE_ID,
2638 sizeof *lface_id_to_name);
2639
2640 lface_id_to_name[next_lface_id] = face;
2641 Fput (face, Qface, make_number (next_lface_id));
2642 ++next_lface_id;
2643 }
2644 else if (f == NULL)
2645 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2646 ASET (global_lface, i, Qunspecified);
2647
2648 /* Add a frame-local definition. */
2649 if (f)
2650 {
2651 if (NILP (lface))
2652 {
2653 lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
2654 Qunspecified);
2655 ASET (lface, 0, Qface);
2656 fset_face_alist (f, Fcons (Fcons (face, lface), f->face_alist));
2657 }
2658 else
2659 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2660 ASET (lface, i, Qunspecified);
2661 }
2662 else
2663 lface = global_lface;
2664
2665 /* Changing a named face means that all realized faces depending on
2666 that face are invalid. Since we cannot tell which realized faces
2667 depend on the face, make sure they are all removed. This is done
2668 by incrementing face_change_count. The next call to
2669 init_iterator will then free realized faces. */
2670 if (NILP (Fget (face, Qface_no_inherit)))
2671 {
2672 ++face_change_count;
2673 ++windows_or_buffers_changed;
2674 }
2675
2676 eassert (LFACEP (lface));
2677 check_lface (lface);
2678 return lface;
2679}
2680
2681
2682DEFUN ("internal-lisp-face-p", Finternal_lisp_face_p,
2683 Sinternal_lisp_face_p, 1, 2, 0,
2684 doc: /* Return non-nil if FACE names a face.
2685FACE should be a symbol or string.
2686If optional second argument FRAME is non-nil, check for the
2687existence of a frame-local face with name FACE on that frame.
2688Otherwise check for the existence of a global face. */)
2689 (Lisp_Object face, Lisp_Object frame)
2690{
2691 Lisp_Object lface;
2692
2693 face = resolve_face_name (face, 1);
2694
2695 if (!NILP (frame))
2696 {
2697 CHECK_LIVE_FRAME (frame);
2698 lface = lface_from_face_name (XFRAME (frame), face, 0);
2699 }
2700 else
2701 lface = lface_from_face_name (NULL, face, 0);
2702
2703 return lface;
2704}
2705
2706
2707DEFUN ("internal-copy-lisp-face", Finternal_copy_lisp_face,
2708 Sinternal_copy_lisp_face, 4, 4, 0,
2709 doc: /* Copy face FROM to TO.
2710If FRAME is t, copy the global face definition of FROM.
2711Otherwise, copy the frame-local definition of FROM on FRAME.
2712If NEW-FRAME is a frame, copy that data into the frame-local
2713definition of TO on NEW-FRAME. If NEW-FRAME is nil,
2714FRAME controls where the data is copied to.
2715
2716The value is TO. */)
2717 (Lisp_Object from, Lisp_Object to, Lisp_Object frame, Lisp_Object new_frame)
2718{
2719 Lisp_Object lface, copy;
2720
2721 CHECK_SYMBOL (from);
2722 CHECK_SYMBOL (to);
2723
2724 if (EQ (frame, Qt))
2725 {
2726 /* Copy global definition of FROM. We don't make copies of
2727 strings etc. because 20.2 didn't do it either. */
2728 lface = lface_from_face_name (NULL, from, 1);
2729 copy = Finternal_make_lisp_face (to, Qnil);
2730 }
2731 else
2732 {
2733 /* Copy frame-local definition of FROM. */
2734 if (NILP (new_frame))
2735 new_frame = frame;
2736 CHECK_LIVE_FRAME (frame);
2737 CHECK_LIVE_FRAME (new_frame);
2738 lface = lface_from_face_name (XFRAME (frame), from, 1);
2739 copy = Finternal_make_lisp_face (to, new_frame);
2740 }
2741
2742 vcopy (copy, 0, XVECTOR (lface)->contents, LFACE_VECTOR_SIZE);
2743
2744 /* Changing a named face means that all realized faces depending on
2745 that face are invalid. Since we cannot tell which realized faces
2746 depend on the face, make sure they are all removed. This is done
2747 by incrementing face_change_count. The next call to
2748 init_iterator will then free realized faces. */
2749 if (NILP (Fget (to, Qface_no_inherit)))
2750 {
2751 ++face_change_count;
2752 ++windows_or_buffers_changed;
2753 }
2754
2755 return to;
2756}
2757
2758
2759DEFUN ("internal-set-lisp-face-attribute", Finternal_set_lisp_face_attribute,
2760 Sinternal_set_lisp_face_attribute, 3, 4, 0,
2761 doc: /* Set attribute ATTR of FACE to VALUE.
2762FRAME being a frame means change the face on that frame.
2763FRAME nil means change the face of the selected frame.
2764FRAME t means change the default for new frames.
2765FRAME 0 means change the face on all frames, and change the default
2766 for new frames. */)
2767 (Lisp_Object face, Lisp_Object attr, Lisp_Object value, Lisp_Object frame)
2768{
2769 Lisp_Object lface;
2770 Lisp_Object old_value = Qnil;
2771 /* Set one of enum font_property_index (> 0) if ATTR is one of
2772 font-related attributes other than QCfont and QCfontset. */
2773 enum font_property_index prop_index = 0;
2774
2775 CHECK_SYMBOL (face);
2776 CHECK_SYMBOL (attr);
2777
2778 face = resolve_face_name (face, 1);
2779
2780 /* If FRAME is 0, change face on all frames, and change the
2781 default for new frames. */
2782 if (INTEGERP (frame) && XINT (frame) == 0)
2783 {
2784 Lisp_Object tail;
2785 Finternal_set_lisp_face_attribute (face, attr, value, Qt);
2786 FOR_EACH_FRAME (tail, frame)
2787 Finternal_set_lisp_face_attribute (face, attr, value, frame);
2788 return face;
2789 }
2790
2791 /* Set lface to the Lisp attribute vector of FACE. */
2792 if (EQ (frame, Qt))
2793 {
2794 lface = lface_from_face_name (NULL, face, 1);
2795
2796 /* When updating face-new-frame-defaults, we put :ignore-defface
2797 where the caller wants `unspecified'. This forces the frame
2798 defaults to ignore the defface value. Otherwise, the defface
2799 will take effect, which is generally not what is intended.
2800 The value of that attribute will be inherited from some other
2801 face during face merging. See internal_merge_in_global_face. */
2802 if (UNSPECIFIEDP (value))
2803 value = QCignore_defface;
2804 }
2805 else
2806 {
2807 if (NILP (frame))
2808 frame = selected_frame;
2809
2810 CHECK_LIVE_FRAME (frame);
2811 lface = lface_from_face_name (XFRAME (frame), face, 0);
2812
2813 /* If a frame-local face doesn't exist yet, create one. */
2814 if (NILP (lface))
2815 lface = Finternal_make_lisp_face (face, frame);
2816 }
2817
2818 if (EQ (attr, QCfamily))
2819 {
2820 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2821 {
2822 CHECK_STRING (value);
2823 if (SCHARS (value) == 0)
2824 signal_error ("Invalid face family", value);
2825 }
2826 old_value = LFACE_FAMILY (lface);
2827 ASET (lface, LFACE_FAMILY_INDEX, value);
2828 prop_index = FONT_FAMILY_INDEX;
2829 }
2830 else if (EQ (attr, QCfoundry))
2831 {
2832 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2833 {
2834 CHECK_STRING (value);
2835 if (SCHARS (value) == 0)
2836 signal_error ("Invalid face foundry", value);
2837 }
2838 old_value = LFACE_FOUNDRY (lface);
2839 ASET (lface, LFACE_FOUNDRY_INDEX, value);
2840 prop_index = FONT_FOUNDRY_INDEX;
2841 }
2842 else if (EQ (attr, QCheight))
2843 {
2844 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2845 {
2846 if (EQ (face, Qdefault))
2847 {
2848 /* The default face must have an absolute size. */
2849 if (!INTEGERP (value) || XINT (value) <= 0)
2850 signal_error ("Default face height not absolute and positive",
2851 value);
2852 }
2853 else
2854 {
2855 /* For non-default faces, do a test merge with a random
2856 height to see if VALUE's ok. */
2857 Lisp_Object test = merge_face_heights (value,
2858 make_number (10),
2859 Qnil);
2860 if (!INTEGERP (test) || XINT (test) <= 0)
2861 signal_error ("Face height does not produce a positive integer",
2862 value);
2863 }
2864 }
2865
2866 old_value = LFACE_HEIGHT (lface);
2867 ASET (lface, LFACE_HEIGHT_INDEX, value);
2868 prop_index = FONT_SIZE_INDEX;
2869 }
2870 else if (EQ (attr, QCweight))
2871 {
2872 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2873 {
2874 CHECK_SYMBOL (value);
2875 if (FONT_WEIGHT_NAME_NUMERIC (value) < 0)
2876 signal_error ("Invalid face weight", value);
2877 }
2878 old_value = LFACE_WEIGHT (lface);
2879 ASET (lface, LFACE_WEIGHT_INDEX, value);
2880 prop_index = FONT_WEIGHT_INDEX;
2881 }
2882 else if (EQ (attr, QCslant))
2883 {
2884 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2885 {
2886 CHECK_SYMBOL (value);
2887 if (FONT_SLANT_NAME_NUMERIC (value) < 0)
2888 signal_error ("Invalid face slant", value);
2889 }
2890 old_value = LFACE_SLANT (lface);
2891 ASET (lface, LFACE_SLANT_INDEX, value);
2892 prop_index = FONT_SLANT_INDEX;
2893 }
2894 else if (EQ (attr, QCunderline))
2895 {
2896 int valid_p = 0;
2897
2898 if (UNSPECIFIEDP (value) || IGNORE_DEFFACE_P (value))
2899 valid_p = 1;
2900 else if (NILP (value) || EQ (value, Qt))
2901 valid_p = 1;
2902 else if (STRINGP (value) && SCHARS (value) > 0)
2903 valid_p = 1;
2904 else if (CONSP (value))
2905 {
2906 Lisp_Object key, val, list;
2907
2908 list = value;
2909 valid_p = 1;
2910
2911 while (!NILP (CAR_SAFE(list)))
2912 {
2913 key = CAR_SAFE (list);
2914 list = CDR_SAFE (list);
2915 val = CAR_SAFE (list);
2916 list = CDR_SAFE (list);
2917
2918 if (NILP (key) || NILP (val))
2919 {
2920 valid_p = 0;
2921 break;
2922 }
2923
2924 else if (EQ (key, QCcolor)
2925 && !(EQ (val, Qforeground_color)
2926 || (STRINGP (val) && SCHARS (val) > 0)))
2927 {
2928 valid_p = 0;
2929 break;
2930 }
2931
2932 else if (EQ (key, QCstyle)
2933 && !(EQ (val, Qline) || EQ (val, Qwave)))
2934 {
2935 valid_p = 0;
2936 break;
2937 }
2938 }
2939 }
2940
2941 if (!valid_p)
2942 signal_error ("Invalid face underline", value);
2943
2944 old_value = LFACE_UNDERLINE (lface);
2945 ASET (lface, LFACE_UNDERLINE_INDEX, value);
2946 }
2947 else if (EQ (attr, QCoverline))
2948 {
2949 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2950 if ((SYMBOLP (value)
2951 && !EQ (value, Qt)
2952 && !EQ (value, Qnil))
2953 /* Overline color. */
2954 || (STRINGP (value)
2955 && SCHARS (value) == 0))
2956 signal_error ("Invalid face overline", value);
2957
2958 old_value = LFACE_OVERLINE (lface);
2959 ASET (lface, LFACE_OVERLINE_INDEX, value);
2960 }
2961 else if (EQ (attr, QCstrike_through))
2962 {
2963 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2964 if ((SYMBOLP (value)
2965 && !EQ (value, Qt)
2966 && !EQ (value, Qnil))
2967 /* Strike-through color. */
2968 || (STRINGP (value)
2969 && SCHARS (value) == 0))
2970 signal_error ("Invalid face strike-through", value);
2971
2972 old_value = LFACE_STRIKE_THROUGH (lface);
2973 ASET (lface, LFACE_STRIKE_THROUGH_INDEX, value);
2974 }
2975 else if (EQ (attr, QCbox))
2976 {
2977 int valid_p;
2978
2979 /* Allow t meaning a simple box of width 1 in foreground color
2980 of the face. */
2981 if (EQ (value, Qt))
2982 value = make_number (1);
2983
2984 if (UNSPECIFIEDP (value) || IGNORE_DEFFACE_P (value))
2985 valid_p = 1;
2986 else if (NILP (value))
2987 valid_p = 1;
2988 else if (INTEGERP (value))
2989 valid_p = XINT (value) != 0;
2990 else if (STRINGP (value))
2991 valid_p = SCHARS (value) > 0;
2992 else if (CONSP (value))
2993 {
2994 Lisp_Object tem;
2995
2996 tem = value;
2997 while (CONSP (tem))
2998 {
2999 Lisp_Object k, v;
3000
3001 k = XCAR (tem);
3002 tem = XCDR (tem);
3003 if (!CONSP (tem))
3004 break;
3005 v = XCAR (tem);
3006 tem = XCDR (tem);
3007
3008 if (EQ (k, QCline_width))
3009 {
3010 if (!INTEGERP (v) || XINT (v) == 0)
3011 break;
3012 }
3013 else if (EQ (k, QCcolor))
3014 {
3015 if (!NILP (v) && (!STRINGP (v) || SCHARS (v) == 0))
3016 break;
3017 }
3018 else if (EQ (k, QCstyle))
3019 {
3020 if (!EQ (v, Qpressed_button) && !EQ (v, Qreleased_button))
3021 break;
3022 }
3023 else
3024 break;
3025 }
3026
3027 valid_p = NILP (tem);
3028 }
3029 else
3030 valid_p = 0;
3031
3032 if (!valid_p)
3033 signal_error ("Invalid face box", value);
3034
3035 old_value = LFACE_BOX (lface);
3036 ASET (lface, LFACE_BOX_INDEX, value);
3037 }
3038 else if (EQ (attr, QCinverse_video)
3039 || EQ (attr, QCreverse_video))
3040 {
3041 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3042 {
3043 CHECK_SYMBOL (value);
3044 if (!EQ (value, Qt) && !NILP (value))
3045 signal_error ("Invalid inverse-video face attribute value", value);
3046 }
3047 old_value = LFACE_INVERSE (lface);
3048 ASET (lface, LFACE_INVERSE_INDEX, value);
3049 }
3050 else if (EQ (attr, QCforeground))
3051 {
3052 /* Compatibility with 20.x. */
3053 if (NILP (value))
3054 value = Qunspecified;
3055 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3056 {
3057 /* Don't check for valid color names here because it depends
3058 on the frame (display) whether the color will be valid
3059 when the face is realized. */
3060 CHECK_STRING (value);
3061 if (SCHARS (value) == 0)
3062 signal_error ("Empty foreground color value", value);
3063 }
3064 old_value = LFACE_FOREGROUND (lface);
3065 ASET (lface, LFACE_FOREGROUND_INDEX, value);
3066 }
3067 else if (EQ (attr, QCbackground))
3068 {
3069 /* Compatibility with 20.x. */
3070 if (NILP (value))
3071 value = Qunspecified;
3072 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3073 {
3074 /* Don't check for valid color names here because it depends
3075 on the frame (display) whether the color will be valid
3076 when the face is realized. */
3077 CHECK_STRING (value);
3078 if (SCHARS (value) == 0)
3079 signal_error ("Empty background color value", value);
3080 }
3081 old_value = LFACE_BACKGROUND (lface);
3082 ASET (lface, LFACE_BACKGROUND_INDEX, value);
3083 }
3084 else if (EQ (attr, QCstipple))
3085 {
3086#if defined (HAVE_WINDOW_SYSTEM)
3087 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value)
3088 && !NILP (value)
3089 && NILP (Fbitmap_spec_p (value)))
3090 signal_error ("Invalid stipple attribute", value);
3091 old_value = LFACE_STIPPLE (lface);
3092 ASET (lface, LFACE_STIPPLE_INDEX, value);
3093#endif /* HAVE_WINDOW_SYSTEM */
3094 }
3095 else if (EQ (attr, QCwidth))
3096 {
3097 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3098 {
3099 CHECK_SYMBOL (value);
3100 if (FONT_WIDTH_NAME_NUMERIC (value) < 0)
3101 signal_error ("Invalid face width", value);
3102 }
3103 old_value = LFACE_SWIDTH (lface);
3104 ASET (lface, LFACE_SWIDTH_INDEX, value);
3105 prop_index = FONT_WIDTH_INDEX;
3106 }
3107 else if (EQ (attr, QCfont))
3108 {
3109#ifdef HAVE_WINDOW_SYSTEM
3110 if (EQ (frame, Qt) || FRAME_WINDOW_P (XFRAME (frame)))
3111 {
3112 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3113 {
3114 FRAME_PTR f;
3115
3116 old_value = LFACE_FONT (lface);
3117 if (! FONTP (value))
3118 {
3119 if (STRINGP (value))
3120 {
3121 Lisp_Object name = value;
3122 int fontset = fs_query_fontset (name, 0);
3123
3124 if (fontset >= 0)
3125 name = fontset_ascii (fontset);
3126 value = font_spec_from_name (name);
3127 if (!FONTP (value))
3128 signal_error ("Invalid font name", name);
3129 }
3130 else
3131 signal_error ("Invalid font or font-spec", value);
3132 }
3133 if (EQ (frame, Qt))
3134 f = XFRAME (selected_frame);
3135 else
3136 f = XFRAME (frame);
3137 if (! FONT_OBJECT_P (value))
3138 {
3139 Lisp_Object *attrs = XVECTOR (lface)->contents;
3140 Lisp_Object font_object;
3141
3142 font_object = font_load_for_lface (f, attrs, value);
3143 if (NILP (font_object))
3144 signal_error ("Font not available", value);
3145 value = font_object;
3146 }
3147 set_lface_from_font (f, lface, value, 1);
3148 }
3149 else
3150 ASET (lface, LFACE_FONT_INDEX, value);
3151 }
3152#endif /* HAVE_WINDOW_SYSTEM */
3153 }
3154 else if (EQ (attr, QCfontset))
3155 {
3156#ifdef HAVE_WINDOW_SYSTEM
3157 if (EQ (frame, Qt) || FRAME_WINDOW_P (XFRAME (frame)))
3158 {
3159 Lisp_Object tmp;
3160
3161 old_value = LFACE_FONTSET (lface);
3162 tmp = Fquery_fontset (value, Qnil);
3163 if (NILP (tmp))
3164 signal_error ("Invalid fontset name", value);
3165 ASET (lface, LFACE_FONTSET_INDEX, value = tmp);
3166 }
3167#endif /* HAVE_WINDOW_SYSTEM */
3168 }
3169 else if (EQ (attr, QCinherit))
3170 {
3171 Lisp_Object tail;
3172 if (SYMBOLP (value))
3173 tail = Qnil;
3174 else
3175 for (tail = value; CONSP (tail); tail = XCDR (tail))
3176 if (!SYMBOLP (XCAR (tail)))
3177 break;
3178 if (NILP (tail))
3179 ASET (lface, LFACE_INHERIT_INDEX, value);
3180 else
3181 signal_error ("Invalid face inheritance", value);
3182 }
3183 else if (EQ (attr, QCbold))
3184 {
3185 old_value = LFACE_WEIGHT (lface);
3186 ASET (lface, LFACE_WEIGHT_INDEX, NILP (value) ? Qnormal : Qbold);
3187 prop_index = FONT_WEIGHT_INDEX;
3188 }
3189 else if (EQ (attr, QCitalic))
3190 {
3191 attr = QCslant;
3192 old_value = LFACE_SLANT (lface);
3193 ASET (lface, LFACE_SLANT_INDEX, NILP (value) ? Qnormal : Qitalic);
3194 prop_index = FONT_SLANT_INDEX;
3195 }
3196 else
3197 signal_error ("Invalid face attribute name", attr);
3198
3199 if (prop_index)
3200 {
3201 /* If a font-related attribute other than QCfont and QCfontset
3202 is specified, and if the original QCfont attribute has a font
3203 (font-spec or font-object), set the corresponding property in
3204 the font to nil so that the font selector doesn't think that
3205 the attribute is mandatory. Also, clear the average
3206 width. */
3207 font_clear_prop (XVECTOR (lface)->contents, prop_index);
3208 }
3209
3210 /* Changing a named face means that all realized faces depending on
3211 that face are invalid. Since we cannot tell which realized faces
3212 depend on the face, make sure they are all removed. This is done
3213 by incrementing face_change_count. The next call to
3214 init_iterator will then free realized faces. */
3215 if (!EQ (frame, Qt)
3216 && NILP (Fget (face, Qface_no_inherit))
3217 && NILP (Fequal (old_value, value)))
3218 {
3219 ++face_change_count;
3220 ++windows_or_buffers_changed;
3221 }
3222
3223 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value)
3224 && NILP (Fequal (old_value, value)))
3225 {
3226 Lisp_Object param;
3227
3228 param = Qnil;
3229
3230 if (EQ (face, Qdefault))
3231 {
3232#ifdef HAVE_WINDOW_SYSTEM
3233 /* Changed font-related attributes of the `default' face are
3234 reflected in changed `font' frame parameters. */
3235 if (FRAMEP (frame)
3236 && (prop_index || EQ (attr, QCfont))
3237 && lface_fully_specified_p (XVECTOR (lface)->contents))
3238 set_font_frame_param (frame, lface);
3239 else
3240#endif /* HAVE_WINDOW_SYSTEM */
3241
3242 if (EQ (attr, QCforeground))
3243 param = Qforeground_color;
3244 else if (EQ (attr, QCbackground))
3245 param = Qbackground_color;
3246 }
3247#ifdef HAVE_WINDOW_SYSTEM
3248#ifndef HAVE_NTGUI
3249 else if (EQ (face, Qscroll_bar))
3250 {
3251 /* Changing the colors of `scroll-bar' sets frame parameters
3252 `scroll-bar-foreground' and `scroll-bar-background'. */
3253 if (EQ (attr, QCforeground))
3254 param = Qscroll_bar_foreground;
3255 else if (EQ (attr, QCbackground))
3256 param = Qscroll_bar_background;
3257 }
3258#endif /* not HAVE_NTGUI */
3259 else if (EQ (face, Qborder))
3260 {
3261 /* Changing background color of `border' sets frame parameter
3262 `border-color'. */
3263 if (EQ (attr, QCbackground))
3264 param = Qborder_color;
3265 }
3266 else if (EQ (face, Qcursor))
3267 {
3268 /* Changing background color of `cursor' sets frame parameter
3269 `cursor-color'. */
3270 if (EQ (attr, QCbackground))
3271 param = Qcursor_color;
3272 }
3273 else if (EQ (face, Qmouse))
3274 {
3275 /* Changing background color of `mouse' sets frame parameter
3276 `mouse-color'. */
3277 if (EQ (attr, QCbackground))
3278 param = Qmouse_color;
3279 }
3280#endif /* HAVE_WINDOW_SYSTEM */
3281 else if (EQ (face, Qmenu))
3282 {
3283 /* Indicate that we have to update the menu bar when
3284 realizing faces on FRAME. FRAME t change the
3285 default for new frames. We do this by setting
3286 setting the flag in new face caches */
3287 if (FRAMEP (frame))
3288 {
3289 struct frame *f = XFRAME (frame);
3290 if (FRAME_FACE_CACHE (f) == NULL)
3291 FRAME_FACE_CACHE (f) = make_face_cache (f);
3292 FRAME_FACE_CACHE (f)->menu_face_changed_p = 1;
3293 }
3294 else
3295 menu_face_changed_default = 1;
3296 }
3297
3298 if (!NILP (param))
3299 {
3300 if (EQ (frame, Qt))
3301 /* Update `default-frame-alist', which is used for new frames. */
3302 {
3303 store_in_alist (&Vdefault_frame_alist, param, value);
3304 }
3305 else
3306 /* Update the current frame's parameters. */
3307 {
3308 Lisp_Object cons;
3309 cons = XCAR (Vparam_value_alist);
3310 XSETCAR (cons, param);
3311 XSETCDR (cons, value);
3312 Fmodify_frame_parameters (frame, Vparam_value_alist);
3313 }
3314 }
3315 }
3316
3317 return face;
3318}
3319
3320
3321/* Update the corresponding face when frame parameter PARAM on frame F
3322 has been assigned the value NEW_VALUE. */
3323
3324void
3325update_face_from_frame_parameter (struct frame *f, Lisp_Object param,
3326 Lisp_Object new_value)
3327{
3328 Lisp_Object face = Qnil;
3329 Lisp_Object lface;
3330
3331 /* If there are no faces yet, give up. This is the case when called
3332 from Fx_create_frame, and we do the necessary things later in
3333 face-set-after-frame-defaults. */
3334 if (NILP (f->face_alist))
3335 return;
3336
3337 if (EQ (param, Qforeground_color))
3338 {
3339 face = Qdefault;
3340 lface = lface_from_face_name (f, face, 1);
3341 ASET (lface, LFACE_FOREGROUND_INDEX,
3342 (STRINGP (new_value) ? new_value : Qunspecified));
3343 realize_basic_faces (f);
3344 }
3345 else if (EQ (param, Qbackground_color))
3346 {
3347 Lisp_Object frame;
3348
3349 /* Changing the background color might change the background
3350 mode, so that we have to load new defface specs.
3351 Call frame-set-background-mode to do that. */
3352 XSETFRAME (frame, f);
3353 call1 (Qframe_set_background_mode, frame);
3354
3355 face = Qdefault;
3356 lface = lface_from_face_name (f, face, 1);
3357 ASET (lface, LFACE_BACKGROUND_INDEX,
3358 (STRINGP (new_value) ? new_value : Qunspecified));
3359 realize_basic_faces (f);
3360 }
3361#ifdef HAVE_WINDOW_SYSTEM
3362 else if (EQ (param, Qborder_color))
3363 {
3364 face = Qborder;
3365 lface = lface_from_face_name (f, face, 1);
3366 ASET (lface, LFACE_BACKGROUND_INDEX,
3367 (STRINGP (new_value) ? new_value : Qunspecified));
3368 }
3369 else if (EQ (param, Qcursor_color))
3370 {
3371 face = Qcursor;
3372 lface = lface_from_face_name (f, face, 1);
3373 ASET (lface, LFACE_BACKGROUND_INDEX,
3374 (STRINGP (new_value) ? new_value : Qunspecified));
3375 }
3376 else if (EQ (param, Qmouse_color))
3377 {
3378 face = Qmouse;
3379 lface = lface_from_face_name (f, face, 1);
3380 ASET (lface, LFACE_BACKGROUND_INDEX,
3381 (STRINGP (new_value) ? new_value : Qunspecified));
3382 }
3383#endif
3384
3385 /* Changing a named face means that all realized faces depending on
3386 that face are invalid. Since we cannot tell which realized faces
3387 depend on the face, make sure they are all removed. This is done
3388 by incrementing face_change_count. The next call to
3389 init_iterator will then free realized faces. */
3390 if (!NILP (face)
3391 && NILP (Fget (face, Qface_no_inherit)))
3392 {
3393 ++face_change_count;
3394 ++windows_or_buffers_changed;
3395 }
3396}
3397
3398
3399#ifdef HAVE_WINDOW_SYSTEM
3400
3401/* Set the `font' frame parameter of FRAME determined from the
3402 font-object set in `default' face attributes LFACE. */
3403
3404static void
3405set_font_frame_param (Lisp_Object frame, Lisp_Object lface)
3406{
3407 struct frame *f = XFRAME (frame);
3408 Lisp_Object font;
3409
3410 if (FRAME_WINDOW_P (f)
3411 /* Don't do anything if the font is `unspecified'. This can
3412 happen during frame creation. */
3413 && (font = LFACE_FONT (lface),
3414 ! UNSPECIFIEDP (font)))
3415 {
3416 if (FONT_SPEC_P (font))
3417 {
3418 font = font_load_for_lface (f, XVECTOR (lface)->contents, font);
3419 if (NILP (font))
3420 return;
3421 ASET (lface, LFACE_FONT_INDEX, font);
3422 }
3423 f->default_face_done_p = 0;
3424 Fmodify_frame_parameters (frame, Fcons (Fcons (Qfont, font), Qnil));
3425 }
3426}
3427
3428
3429/* Get the value of X resource RESOURCE, class CLASS for the display
3430 of frame FRAME. This is here because ordinary `x-get-resource'
3431 doesn't take a frame argument. */
3432
3433DEFUN ("internal-face-x-get-resource", Finternal_face_x_get_resource,
3434 Sinternal_face_x_get_resource, 3, 3, 0, doc: /* */)
3435 (Lisp_Object resource, Lisp_Object class, Lisp_Object frame)
3436{
3437 Lisp_Object value = Qnil;
3438 CHECK_STRING (resource);
3439 CHECK_STRING (class);
3440 CHECK_LIVE_FRAME (frame);
3441 block_input ();
3442 value = display_x_get_resource (FRAME_X_DISPLAY_INFO (XFRAME (frame)),
3443 resource, class, Qnil, Qnil);
3444 unblock_input ();
3445 return value;
3446}
3447
3448
3449/* Return resource string VALUE as a boolean value, i.e. nil, or t.
3450 If VALUE is "on" or "true", return t. If VALUE is "off" or
3451 "false", return nil. Otherwise, if SIGNAL_P is non-zero, signal an
3452 error; if SIGNAL_P is zero, return 0. */
3453
3454static Lisp_Object
3455face_boolean_x_resource_value (Lisp_Object value, int signal_p)
3456{
3457 Lisp_Object result = make_number (0);
3458
3459 eassert (STRINGP (value));
3460
3461 if (xstrcasecmp (SSDATA (value), "on") == 0
3462 || xstrcasecmp (SSDATA (value), "true") == 0)
3463 result = Qt;
3464 else if (xstrcasecmp (SSDATA (value), "off") == 0
3465 || xstrcasecmp (SSDATA (value), "false") == 0)
3466 result = Qnil;
3467 else if (xstrcasecmp (SSDATA (value), "unspecified") == 0)
3468 result = Qunspecified;
3469 else if (signal_p)
3470 signal_error ("Invalid face attribute value from X resource", value);
3471
3472 return result;
3473}
3474
3475
3476DEFUN ("internal-set-lisp-face-attribute-from-resource",
3477 Finternal_set_lisp_face_attribute_from_resource,
3478 Sinternal_set_lisp_face_attribute_from_resource,
3479 3, 4, 0, doc: /* */)
3480 (Lisp_Object face, Lisp_Object attr, Lisp_Object value, Lisp_Object frame)
3481{
3482 CHECK_SYMBOL (face);
3483 CHECK_SYMBOL (attr);
3484 CHECK_STRING (value);
3485
3486 if (xstrcasecmp (SSDATA (value), "unspecified") == 0)
3487 value = Qunspecified;
3488 else if (EQ (attr, QCheight))
3489 {
3490 value = Fstring_to_number (value, make_number (10));
3491 if (XINT (value) <= 0)
3492 signal_error ("Invalid face height from X resource", value);
3493 }
3494 else if (EQ (attr, QCbold) || EQ (attr, QCitalic))
3495 value = face_boolean_x_resource_value (value, 1);
3496 else if (EQ (attr, QCweight) || EQ (attr, QCslant) || EQ (attr, QCwidth))
3497 value = intern (SSDATA (value));
3498 else if (EQ (attr, QCreverse_video) || EQ (attr, QCinverse_video))
3499 value = face_boolean_x_resource_value (value, 1);
3500 else if (EQ (attr, QCunderline)
3501 || EQ (attr, QCoverline)
3502 || EQ (attr, QCstrike_through))
3503 {
3504 Lisp_Object boolean_value;
3505
3506 /* If the result of face_boolean_x_resource_value is t or nil,
3507 VALUE does NOT specify a color. */
3508 boolean_value = face_boolean_x_resource_value (value, 0);
3509 if (SYMBOLP (boolean_value))
3510 value = boolean_value;
3511 }
3512 else if (EQ (attr, QCbox) || EQ (attr, QCinherit))
3513 value = Fcar (Fread_from_string (value, Qnil, Qnil));
3514
3515 return Finternal_set_lisp_face_attribute (face, attr, value, frame);
3516}
3517
3518#endif /* HAVE_WINDOW_SYSTEM */
3519
3520\f
3521/***********************************************************************
3522 Menu face
3523 ***********************************************************************/
3524
3525#if defined HAVE_X_WINDOWS && defined USE_X_TOOLKIT
3526
3527/* Make menus on frame F appear as specified by the `menu' face. */
3528
3529static void
3530x_update_menu_appearance (struct frame *f)
3531{
3532 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3533 XrmDatabase rdb;
3534
3535 if (dpyinfo
3536 && (rdb = XrmGetDatabase (FRAME_X_DISPLAY (f)),
3537 rdb != NULL))
3538 {
3539 char line[512];
3540 char *buf = line;
3541 ptrdiff_t bufsize = sizeof line;
3542 Lisp_Object lface = lface_from_face_name (f, Qmenu, 1);
3543 struct face *face = FACE_FROM_ID (f, MENU_FACE_ID);
3544 const char *myname = SSDATA (Vx_resource_name);
3545 int changed_p = 0;
3546#ifdef USE_MOTIF
3547 const char *popup_path = "popup_menu";
3548#else
3549 const char *popup_path = "menu.popup";
3550#endif
3551
3552 if (STRINGP (LFACE_FOREGROUND (lface)))
3553 {
3554 exprintf (&buf, &bufsize, line, -1, "%s.%s*foreground: %s",
3555 myname, popup_path,
3556 SDATA (LFACE_FOREGROUND (lface)));
3557 XrmPutLineResource (&rdb, line);
3558 exprintf (&buf, &bufsize, line, -1, "%s.pane.menubar*foreground: %s",
3559 myname, SDATA (LFACE_FOREGROUND (lface)));
3560 XrmPutLineResource (&rdb, line);
3561 changed_p = 1;
3562 }
3563
3564 if (STRINGP (LFACE_BACKGROUND (lface)))
3565 {
3566 exprintf (&buf, &bufsize, line, -1, "%s.%s*background: %s",
3567 myname, popup_path,
3568 SDATA (LFACE_BACKGROUND (lface)));
3569 XrmPutLineResource (&rdb, line);
3570
3571 exprintf (&buf, &bufsize, line, -1, "%s.pane.menubar*background: %s",
3572 myname, SDATA (LFACE_BACKGROUND (lface)));
3573 XrmPutLineResource (&rdb, line);
3574 changed_p = 1;
3575 }
3576
3577 if (face->font
3578 /* On Solaris 5.8, it's been reported that the `menu' face
3579 can be unspecified here, during startup. Why this
3580 happens remains unknown. -- cyd */
3581 && FONTP (LFACE_FONT (lface))
3582 && (!UNSPECIFIEDP (LFACE_FAMILY (lface))
3583 || !UNSPECIFIEDP (LFACE_FOUNDRY (lface))
3584 || !UNSPECIFIEDP (LFACE_SWIDTH (lface))
3585 || !UNSPECIFIEDP (LFACE_WEIGHT (lface))
3586 || !UNSPECIFIEDP (LFACE_SLANT (lface))
3587 || !UNSPECIFIEDP (LFACE_HEIGHT (lface))))
3588 {
3589 Lisp_Object xlfd = Ffont_xlfd_name (LFACE_FONT (lface), Qnil);
3590#ifdef USE_MOTIF
3591 const char *suffix = "List";
3592 Bool motif = True;
3593#else
3594#if defined HAVE_X_I18N
3595
3596 const char *suffix = "Set";
3597#else
3598 const char *suffix = "";
3599#endif
3600 Bool motif = False;
3601#endif
3602
3603 if (! NILP (xlfd))
3604 {
3605#if defined HAVE_X_I18N
3606 char *fontsetname = xic_create_fontsetname (SSDATA (xlfd), motif);
3607#else
3608 char *fontsetname = SSDATA (xlfd);
3609#endif
3610 exprintf (&buf, &bufsize, line, -1, "%s.pane.menubar*font%s: %s",
3611 myname, suffix, fontsetname);
3612 XrmPutLineResource (&rdb, line);
3613
3614 exprintf (&buf, &bufsize, line, -1, "%s.%s*font%s: %s",
3615 myname, popup_path, suffix, fontsetname);
3616 XrmPutLineResource (&rdb, line);
3617 changed_p = 1;
3618 if (fontsetname != SSDATA (xlfd))
3619 xfree (fontsetname);
3620 }
3621 }
3622
3623 if (changed_p && f->output_data.x->menubar_widget)
3624 free_frame_menubar (f);
3625
3626 if (buf != line)
3627 xfree (buf);
3628 }
3629}
3630
3631#endif /* HAVE_X_WINDOWS && USE_X_TOOLKIT */
3632
3633
3634DEFUN ("face-attribute-relative-p", Fface_attribute_relative_p,
3635 Sface_attribute_relative_p,
3636 2, 2, 0,
3637 doc: /* Check whether a face attribute value is relative.
3638Specifically, this function returns t if the attribute ATTRIBUTE
3639with the value VALUE is relative.
3640
3641A relative value is one that doesn't entirely override whatever is
3642inherited from another face. For most possible attributes,
3643the only relative value that users see is `unspecified'.
3644However, for :height, floating point values are also relative. */)
3645 (Lisp_Object attribute, Lisp_Object value)
3646{
3647 if (EQ (value, Qunspecified) || (EQ (value, QCignore_defface)))
3648 return Qt;
3649 else if (EQ (attribute, QCheight))
3650 return INTEGERP (value) ? Qnil : Qt;
3651 else
3652 return Qnil;
3653}
3654
3655DEFUN ("merge-face-attribute", Fmerge_face_attribute, Smerge_face_attribute,
3656 3, 3, 0,
3657 doc: /* Return face ATTRIBUTE VALUE1 merged with VALUE2.
3658If VALUE1 or VALUE2 are absolute (see `face-attribute-relative-p'), then
3659the result will be absolute, otherwise it will be relative. */)
3660 (Lisp_Object attribute, Lisp_Object value1, Lisp_Object value2)
3661{
3662 if (EQ (value1, Qunspecified) || EQ (value1, QCignore_defface))
3663 return value2;
3664 else if (EQ (attribute, QCheight))
3665 return merge_face_heights (value1, value2, value1);
3666 else
3667 return value1;
3668}
3669
3670
3671DEFUN ("internal-get-lisp-face-attribute", Finternal_get_lisp_face_attribute,
3672 Sinternal_get_lisp_face_attribute,
3673 2, 3, 0,
3674 doc: /* Return face attribute KEYWORD of face SYMBOL.
3675If SYMBOL does not name a valid Lisp face or KEYWORD isn't a valid
3676face attribute name, signal an error.
3677If the optional argument FRAME is given, report on face SYMBOL in that
3678frame. If FRAME is t, report on the defaults for face SYMBOL (for new
3679frames). If FRAME is omitted or nil, use the selected frame. */)
3680 (Lisp_Object symbol, Lisp_Object keyword, Lisp_Object frame)
3681{
3682 Lisp_Object lface, value = Qnil;
3683
3684 CHECK_SYMBOL (symbol);
3685 CHECK_SYMBOL (keyword);
3686
3687 if (EQ (frame, Qt))
3688 lface = lface_from_face_name (NULL, symbol, 1);
3689 else
3690 {
3691 if (NILP (frame))
3692 frame = selected_frame;
3693 CHECK_LIVE_FRAME (frame);
3694 lface = lface_from_face_name (XFRAME (frame), symbol, 1);
3695 }
3696
3697 if (EQ (keyword, QCfamily))
3698 value = LFACE_FAMILY (lface);
3699 else if (EQ (keyword, QCfoundry))
3700 value = LFACE_FOUNDRY (lface);
3701 else if (EQ (keyword, QCheight))
3702 value = LFACE_HEIGHT (lface);
3703 else if (EQ (keyword, QCweight))
3704 value = LFACE_WEIGHT (lface);
3705 else if (EQ (keyword, QCslant))
3706 value = LFACE_SLANT (lface);
3707 else if (EQ (keyword, QCunderline))
3708 value = LFACE_UNDERLINE (lface);
3709 else if (EQ (keyword, QCoverline))
3710 value = LFACE_OVERLINE (lface);
3711 else if (EQ (keyword, QCstrike_through))
3712 value = LFACE_STRIKE_THROUGH (lface);
3713 else if (EQ (keyword, QCbox))
3714 value = LFACE_BOX (lface);
3715 else if (EQ (keyword, QCinverse_video)
3716 || EQ (keyword, QCreverse_video))
3717 value = LFACE_INVERSE (lface);
3718 else if (EQ (keyword, QCforeground))
3719 value = LFACE_FOREGROUND (lface);
3720 else if (EQ (keyword, QCbackground))
3721 value = LFACE_BACKGROUND (lface);
3722 else if (EQ (keyword, QCstipple))
3723 value = LFACE_STIPPLE (lface);
3724 else if (EQ (keyword, QCwidth))
3725 value = LFACE_SWIDTH (lface);
3726 else if (EQ (keyword, QCinherit))
3727 value = LFACE_INHERIT (lface);
3728 else if (EQ (keyword, QCfont))
3729 value = LFACE_FONT (lface);
3730 else if (EQ (keyword, QCfontset))
3731 value = LFACE_FONTSET (lface);
3732 else
3733 signal_error ("Invalid face attribute name", keyword);
3734
3735 if (IGNORE_DEFFACE_P (value))
3736 return Qunspecified;
3737
3738 return value;
3739}
3740
3741
3742DEFUN ("internal-lisp-face-attribute-values",
3743 Finternal_lisp_face_attribute_values,
3744 Sinternal_lisp_face_attribute_values, 1, 1, 0,
3745 doc: /* Return a list of valid discrete values for face attribute ATTR.
3746Value is nil if ATTR doesn't have a discrete set of valid values. */)
3747 (Lisp_Object attr)
3748{
3749 Lisp_Object result = Qnil;
3750
3751 CHECK_SYMBOL (attr);
3752
3753 if (EQ (attr, QCunderline))
3754 result = Fcons (Qt, Fcons (Qnil, Qnil));
3755 else if (EQ (attr, QCoverline))
3756 result = Fcons (Qt, Fcons (Qnil, Qnil));
3757 else if (EQ (attr, QCstrike_through))
3758 result = Fcons (Qt, Fcons (Qnil, Qnil));
3759 else if (EQ (attr, QCinverse_video) || EQ (attr, QCreverse_video))
3760 result = Fcons (Qt, Fcons (Qnil, Qnil));
3761
3762 return result;
3763}
3764
3765
3766DEFUN ("internal-merge-in-global-face", Finternal_merge_in_global_face,
3767 Sinternal_merge_in_global_face, 2, 2, 0,
3768 doc: /* Add attributes from frame-default definition of FACE to FACE on FRAME.
3769Default face attributes override any local face attributes. */)
3770 (Lisp_Object face, Lisp_Object frame)
3771{
3772 int i;
3773 Lisp_Object global_lface, local_lface, *gvec, *lvec;
3774 struct frame *f = XFRAME (frame);
3775
3776 CHECK_LIVE_FRAME (frame);
3777 global_lface = lface_from_face_name (NULL, face, 1);
3778 local_lface = lface_from_face_name (f, face, 0);
3779 if (NILP (local_lface))
3780 local_lface = Finternal_make_lisp_face (face, frame);
3781
3782 /* Make every specified global attribute override the local one.
3783 BEWARE!! This is only used from `face-set-after-frame-default' where
3784 the local frame is defined from default specs in `face-defface-spec'
3785 and those should be overridden by global settings. Hence the strange
3786 "global before local" priority. */
3787 lvec = XVECTOR (local_lface)->contents;
3788 gvec = XVECTOR (global_lface)->contents;
3789 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3790 if (IGNORE_DEFFACE_P (gvec[i]))
3791 ASET (local_lface, i, Qunspecified);
3792 else if (! UNSPECIFIEDP (gvec[i]))
3793 ASET (local_lface, i, AREF (global_lface, i));
3794
3795 /* If the default face was changed, update the face cache and the
3796 `font' frame parameter. */
3797 if (EQ (face, Qdefault))
3798 {
3799 struct face_cache *c = FRAME_FACE_CACHE (f);
3800 struct face *newface, *oldface = FACE_FROM_ID (f, DEFAULT_FACE_ID);
3801 Lisp_Object attrs[LFACE_VECTOR_SIZE];
3802
3803 /* This can be NULL (e.g., in batch mode). */
3804 if (oldface)
3805 {
3806 /* Ensure that the face vector is fully specified by merging
3807 the previously-cached vector. */
3808 memcpy (attrs, oldface->lface, sizeof attrs);
3809 merge_face_vectors (f, lvec, attrs, 0);
3810 vcopy (local_lface, 0, attrs, LFACE_VECTOR_SIZE);
3811 newface = realize_face (c, lvec, DEFAULT_FACE_ID);
3812
3813 if ((! UNSPECIFIEDP (gvec[LFACE_FAMILY_INDEX])
3814 || ! UNSPECIFIEDP (gvec[LFACE_FOUNDRY_INDEX])
3815 || ! UNSPECIFIEDP (gvec[LFACE_HEIGHT_INDEX])
3816 || ! UNSPECIFIEDP (gvec[LFACE_WEIGHT_INDEX])
3817 || ! UNSPECIFIEDP (gvec[LFACE_SLANT_INDEX])
3818 || ! UNSPECIFIEDP (gvec[LFACE_SWIDTH_INDEX])
3819 || ! UNSPECIFIEDP (gvec[LFACE_FONT_INDEX]))
3820 && newface->font)
3821 {
3822 Lisp_Object name = newface->font->props[FONT_NAME_INDEX];
3823 Fmodify_frame_parameters (frame, Fcons (Fcons (Qfont, name),
3824 Qnil));
3825 }
3826
3827 if (STRINGP (gvec[LFACE_FOREGROUND_INDEX]))
3828 Fmodify_frame_parameters (frame,
3829 Fcons (Fcons (Qforeground_color,
3830 gvec[LFACE_FOREGROUND_INDEX]),
3831 Qnil));
3832
3833 if (STRINGP (gvec[LFACE_BACKGROUND_INDEX]))
3834 Fmodify_frame_parameters (frame,
3835 Fcons (Fcons (Qbackground_color,
3836 gvec[LFACE_BACKGROUND_INDEX]),
3837 Qnil));
3838 }
3839 }
3840
3841 return Qnil;
3842}
3843
3844
3845/* The following function is implemented for compatibility with 20.2.
3846 The function is used in x-resolve-fonts when it is asked to
3847 return fonts with the same size as the font of a face. This is
3848 done in fontset.el. */
3849
3850DEFUN ("face-font", Fface_font, Sface_font, 1, 3, 0,
3851 doc: /* Return the font name of face FACE, or nil if it is unspecified.
3852The font name is, by default, for ASCII characters.
3853If the optional argument FRAME is given, report on face FACE in that frame.
3854If FRAME is t, report on the defaults for face FACE (for new frames).
3855 The font default for a face is either nil, or a list
3856 of the form (bold), (italic) or (bold italic).
3857If FRAME is omitted or nil, use the selected frame. And, in this case,
3858if the optional third argument CHARACTER is given,
3859return the font name used for CHARACTER. */)
3860 (Lisp_Object face, Lisp_Object frame, Lisp_Object character)
3861{
3862 if (EQ (frame, Qt))
3863 {
3864 Lisp_Object result = Qnil;
3865 Lisp_Object lface = lface_from_face_name (NULL, face, 1);
3866
3867 if (!UNSPECIFIEDP (LFACE_WEIGHT (lface))
3868 && !EQ (LFACE_WEIGHT (lface), Qnormal))
3869 result = Fcons (Qbold, result);
3870
3871 if (!UNSPECIFIEDP (LFACE_SLANT (lface))
3872 && !EQ (LFACE_SLANT (lface), Qnormal))
3873 result = Fcons (Qitalic, result);
3874
3875 return result;
3876 }
3877 else
3878 {
3879 struct frame *f = frame_or_selected_frame (frame, 1);
3880 int face_id = lookup_named_face (f, face, 1);
3881 struct face *fface = FACE_FROM_ID (f, face_id);
3882
3883 if (! fface)
3884 return Qnil;
3885#ifdef HAVE_WINDOW_SYSTEM
3886 if (FRAME_WINDOW_P (f) && !NILP (character))
3887 {
3888 CHECK_CHARACTER (character);
3889 face_id = FACE_FOR_CHAR (f, fface, XINT (character), -1, Qnil);
3890 fface = FACE_FROM_ID (f, face_id);
3891 }
3892 return (fface->font
3893 ? fface->font->props[FONT_NAME_INDEX]
3894 : Qnil);
3895#else /* !HAVE_WINDOW_SYSTEM */
3896 return build_string (FRAME_MSDOS_P (f)
3897 ? "ms-dos"
3898 : FRAME_W32_P (f) ? "w32term"
3899 :"tty");
3900#endif
3901 }
3902}
3903
3904
3905/* Compare face-attribute values v1 and v2 for equality. Value is non-zero if
3906 all attributes are `equal'. Tries to be fast because this function
3907 is called quite often. */
3908
3909static int
3910face_attr_equal_p (Lisp_Object v1, Lisp_Object v2)
3911{
3912 /* Type can differ, e.g. when one attribute is unspecified, i.e. nil,
3913 and the other is specified. */
3914 if (XTYPE (v1) != XTYPE (v2))
3915 return 0;
3916
3917 if (EQ (v1, v2))
3918 return 1;
3919
3920 switch (XTYPE (v1))
3921 {
3922 case Lisp_String:
3923 if (SBYTES (v1) != SBYTES (v2))
3924 return 0;
3925
3926 return memcmp (SDATA (v1), SDATA (v2), SBYTES (v1)) == 0;
3927
3928 case_Lisp_Int:
3929 case Lisp_Symbol:
3930 return 0;
3931
3932 default:
3933 return !NILP (Fequal (v1, v2));
3934 }
3935}
3936
3937
3938/* Compare face vectors V1 and V2 for equality. Value is non-zero if
3939 all attributes are `equal'. Tries to be fast because this function
3940 is called quite often. */
3941
3942static int
3943lface_equal_p (Lisp_Object *v1, Lisp_Object *v2)
3944{
3945 int i, equal_p = 1;
3946
3947 for (i = 1; i < LFACE_VECTOR_SIZE && equal_p; ++i)
3948 equal_p = face_attr_equal_p (v1[i], v2[i]);
3949
3950 return equal_p;
3951}
3952
3953
3954DEFUN ("internal-lisp-face-equal-p", Finternal_lisp_face_equal_p,
3955 Sinternal_lisp_face_equal_p, 2, 3, 0,
3956 doc: /* True if FACE1 and FACE2 are equal.
3957If the optional argument FRAME is given, report on FACE1 and FACE2 in that frame.
3958If FRAME is t, report on the defaults for FACE1 and FACE2 (for new frames).
3959If FRAME is omitted or nil, use the selected frame. */)
3960 (Lisp_Object face1, Lisp_Object face2, Lisp_Object frame)
3961{
3962 int equal_p;
3963 struct frame *f;
3964 Lisp_Object lface1, lface2;
3965
3966 if (EQ (frame, Qt))
3967 f = NULL;
3968 else
3969 /* Don't use check_x_frame here because this function is called
3970 before X frames exist. At that time, if FRAME is nil,
3971 selected_frame will be used which is the frame dumped with
3972 Emacs. That frame is not an X frame. */
3973 f = frame_or_selected_frame (frame, 2);
3974
3975 lface1 = lface_from_face_name (f, face1, 1);
3976 lface2 = lface_from_face_name (f, face2, 1);
3977 equal_p = lface_equal_p (XVECTOR (lface1)->contents,
3978 XVECTOR (lface2)->contents);
3979 return equal_p ? Qt : Qnil;
3980}
3981
3982
3983DEFUN ("internal-lisp-face-empty-p", Finternal_lisp_face_empty_p,
3984 Sinternal_lisp_face_empty_p, 1, 2, 0,
3985 doc: /* True if FACE has no attribute specified.
3986If the optional argument FRAME is given, report on face FACE in that frame.
3987If FRAME is t, report on the defaults for face FACE (for new frames).
3988If FRAME is omitted or nil, use the selected frame. */)
3989 (Lisp_Object face, Lisp_Object frame)
3990{
3991 struct frame *f;
3992 Lisp_Object lface;
3993 int i;
3994
3995 if (NILP (frame))
3996 frame = selected_frame;
3997 CHECK_LIVE_FRAME (frame);
3998 f = XFRAME (frame);
3999
4000 if (EQ (frame, Qt))
4001 lface = lface_from_face_name (NULL, face, 1);
4002 else
4003 lface = lface_from_face_name (f, face, 1);
4004
4005 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
4006 if (!UNSPECIFIEDP (AREF (lface, i)))
4007 break;
4008
4009 return i == LFACE_VECTOR_SIZE ? Qt : Qnil;
4010}
4011
4012
4013DEFUN ("frame-face-alist", Fframe_face_alist, Sframe_face_alist,
4014 0, 1, 0,
4015 doc: /* Return an alist of frame-local faces defined on FRAME.
4016For internal use only. */)
4017 (Lisp_Object frame)
4018{
4019 struct frame *f = frame_or_selected_frame (frame, 0);
4020 return f->face_alist;
4021}
4022
4023
4024/* Return a hash code for Lisp string STRING with case ignored. Used
4025 below in computing a hash value for a Lisp face. */
4026
4027static unsigned
4028hash_string_case_insensitive (Lisp_Object string)
4029{
4030 const unsigned char *s;
4031 unsigned hash = 0;
4032 eassert (STRINGP (string));
4033 for (s = SDATA (string); *s; ++s)
4034 hash = (hash << 1) ^ c_tolower (*s);
4035 return hash;
4036}
4037
4038
4039/* Return a hash code for face attribute vector V. */
4040
4041static unsigned
4042lface_hash (Lisp_Object *v)
4043{
4044 return (hash_string_case_insensitive (v[LFACE_FAMILY_INDEX])
4045 ^ hash_string_case_insensitive (v[LFACE_FOUNDRY_INDEX])
4046 ^ hash_string_case_insensitive (v[LFACE_FOREGROUND_INDEX])
4047 ^ hash_string_case_insensitive (v[LFACE_BACKGROUND_INDEX])
4048 ^ XHASH (v[LFACE_WEIGHT_INDEX])
4049 ^ XHASH (v[LFACE_SLANT_INDEX])
4050 ^ XHASH (v[LFACE_SWIDTH_INDEX])
4051 ^ XHASH (v[LFACE_HEIGHT_INDEX]));
4052}
4053
4054
4055/* Return non-zero if LFACE1 and LFACE2 specify the same font (without
4056 considering charsets/registries). They do if they specify the same
4057 family, point size, weight, width, slant, and font. Both
4058 LFACE1 and LFACE2 must be fully-specified. */
4059
4060static int
4061lface_same_font_attributes_p (Lisp_Object *lface1, Lisp_Object *lface2)
4062{
4063 eassert (lface_fully_specified_p (lface1)
4064 && lface_fully_specified_p (lface2));
4065 return (xstrcasecmp (SSDATA (lface1[LFACE_FAMILY_INDEX]),
4066 SSDATA (lface2[LFACE_FAMILY_INDEX])) == 0
4067 && xstrcasecmp (SSDATA (lface1[LFACE_FOUNDRY_INDEX]),
4068 SSDATA (lface2[LFACE_FOUNDRY_INDEX])) == 0
4069 && EQ (lface1[LFACE_HEIGHT_INDEX], lface2[LFACE_HEIGHT_INDEX])
4070 && EQ (lface1[LFACE_SWIDTH_INDEX], lface2[LFACE_SWIDTH_INDEX])
4071 && EQ (lface1[LFACE_WEIGHT_INDEX], lface2[LFACE_WEIGHT_INDEX])
4072 && EQ (lface1[LFACE_SLANT_INDEX], lface2[LFACE_SLANT_INDEX])
4073 && EQ (lface1[LFACE_FONT_INDEX], lface2[LFACE_FONT_INDEX])
4074 && (EQ (lface1[LFACE_FONTSET_INDEX], lface2[LFACE_FONTSET_INDEX])
4075 || (STRINGP (lface1[LFACE_FONTSET_INDEX])
4076 && STRINGP (lface2[LFACE_FONTSET_INDEX])
4077 && ! xstrcasecmp (SSDATA (lface1[LFACE_FONTSET_INDEX]),
4078 SSDATA (lface2[LFACE_FONTSET_INDEX]))))
4079 );
4080}
4081
4082
4083\f
4084/***********************************************************************
4085 Realized Faces
4086 ***********************************************************************/
4087
4088/* Allocate and return a new realized face for Lisp face attribute
4089 vector ATTR. */
4090
4091static struct face *
4092make_realized_face (Lisp_Object *attr)
4093{
4094 struct face *face = xzalloc (sizeof *face);
4095 face->ascii_face = face;
4096 memcpy (face->lface, attr, sizeof face->lface);
4097 return face;
4098}
4099
4100
4101/* Free realized face FACE, including its X resources. FACE may
4102 be null. */
4103
4104static void
4105free_realized_face (struct frame *f, struct face *face)
4106{
4107 if (face)
4108 {
4109#ifdef HAVE_WINDOW_SYSTEM
4110 if (FRAME_WINDOW_P (f))
4111 {
4112 /* Free fontset of FACE if it is ASCII face. */
4113 if (face->fontset >= 0 && face == face->ascii_face)
4114 free_face_fontset (f, face);
4115 if (face->gc)
4116 {
4117 block_input ();
4118 if (face->font)
4119 font_done_for_face (f, face);
4120 x_free_gc (f, face->gc);
4121 face->gc = 0;
4122 unblock_input ();
4123 }
4124
4125 free_face_colors (f, face);
4126 x_destroy_bitmap (f, face->stipple);
4127 }
4128#endif /* HAVE_WINDOW_SYSTEM */
4129
4130 xfree (face);
4131 }
4132}
4133
4134
4135/* Prepare face FACE for subsequent display on frame F. This
4136 allocated GCs if they haven't been allocated yet or have been freed
4137 by clearing the face cache. */
4138
4139void
4140prepare_face_for_display (struct frame *f, struct face *face)
4141{
4142#ifdef HAVE_WINDOW_SYSTEM
4143 eassert (FRAME_WINDOW_P (f));
4144
4145 if (face->gc == 0)
4146 {
4147 XGCValues xgcv;
4148 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
4149
4150 xgcv.foreground = face->foreground;
4151 xgcv.background = face->background;
4152#ifdef HAVE_X_WINDOWS
4153 xgcv.graphics_exposures = False;
4154#endif
4155
4156 block_input ();
4157#ifdef HAVE_X_WINDOWS
4158 if (face->stipple)
4159 {
4160 xgcv.fill_style = FillOpaqueStippled;
4161 xgcv.stipple = x_bitmap_pixmap (f, face->stipple);
4162 mask |= GCFillStyle | GCStipple;
4163 }
4164#endif
4165 face->gc = x_create_gc (f, mask, &xgcv);
4166 if (face->font)
4167 font_prepare_for_face (f, face);
4168 unblock_input ();
4169 }
4170#endif /* HAVE_WINDOW_SYSTEM */
4171}
4172
4173\f
4174/* Returns the `distance' between the colors X and Y. */
4175
4176static int
4177color_distance (XColor *x, XColor *y)
4178{
4179 /* This formula is from a paper titled `Colour metric' by Thiadmer Riemersma.
4180 Quoting from that paper:
4181
4182 This formula has results that are very close to L*u*v* (with the
4183 modified lightness curve) and, more importantly, it is a more even
4184 algorithm: it does not have a range of colors where it suddenly
4185 gives far from optimal results.
4186
4187 See <http://www.compuphase.com/cmetric.htm> for more info. */
4188
4189 long r = (x->red - y->red) >> 8;
4190 long g = (x->green - y->green) >> 8;
4191 long b = (x->blue - y->blue) >> 8;
4192 long r_mean = (x->red + y->red) >> 9;
4193
4194 return
4195 (((512 + r_mean) * r * r) >> 8)
4196 + 4 * g * g
4197 + (((767 - r_mean) * b * b) >> 8);
4198}
4199
4200
4201DEFUN ("color-distance", Fcolor_distance, Scolor_distance, 2, 3, 0,
4202 doc: /* Return an integer distance between COLOR1 and COLOR2 on FRAME.
4203COLOR1 and COLOR2 may be either strings containing the color name,
4204or lists of the form (RED GREEN BLUE).
4205If FRAME is unspecified or nil, the current frame is used. */)
4206 (Lisp_Object color1, Lisp_Object color2, Lisp_Object frame)
4207{
4208 struct frame *f;
4209 XColor cdef1, cdef2;
4210
4211 if (NILP (frame))
4212 frame = selected_frame;
4213 CHECK_LIVE_FRAME (frame);
4214 f = XFRAME (frame);
4215
4216 if (!(CONSP (color1) && parse_rgb_list (color1, &cdef1))
4217 && !(STRINGP (color1) && defined_color (f, SSDATA (color1), &cdef1, 0)))
4218 signal_error ("Invalid color", color1);
4219 if (!(CONSP (color2) && parse_rgb_list (color2, &cdef2))
4220 && !(STRINGP (color2) && defined_color (f, SSDATA (color2), &cdef2, 0)))
4221 signal_error ("Invalid color", color2);
4222
4223 return make_number (color_distance (&cdef1, &cdef2));
4224}
4225
4226\f
4227/***********************************************************************
4228 Face Cache
4229 ***********************************************************************/
4230
4231/* Return a new face cache for frame F. */
4232
4233static struct face_cache *
4234make_face_cache (struct frame *f)
4235{
4236 struct face_cache *c;
4237 int size;
4238
4239 c = xzalloc (sizeof *c);
4240 size = FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
4241 c->buckets = xzalloc (size);
4242 c->size = 50;
4243 c->faces_by_id = xmalloc (c->size * sizeof *c->faces_by_id);
4244 c->f = f;
4245 c->menu_face_changed_p = menu_face_changed_default;
4246 return c;
4247}
4248
4249
4250/* Clear out all graphics contexts for all realized faces, except for
4251 the basic faces. This should be done from time to time just to avoid
4252 keeping too many graphics contexts that are no longer needed. */
4253
4254static void
4255clear_face_gcs (struct face_cache *c)
4256{
4257 if (c && FRAME_WINDOW_P (c->f))
4258 {
4259#ifdef HAVE_WINDOW_SYSTEM
4260 int i;
4261 for (i = BASIC_FACE_ID_SENTINEL; i < c->used; ++i)
4262 {
4263 struct face *face = c->faces_by_id[i];
4264 if (face && face->gc)
4265 {
4266 block_input ();
4267 if (face->font)
4268 font_done_for_face (c->f, face);
4269 x_free_gc (c->f, face->gc);
4270 face->gc = 0;
4271 unblock_input ();
4272 }
4273 }
4274#endif /* HAVE_WINDOW_SYSTEM */
4275 }
4276}
4277
4278
4279/* Free all realized faces in face cache C, including basic faces.
4280 C may be null. If faces are freed, make sure the frame's current
4281 matrix is marked invalid, so that a display caused by an expose
4282 event doesn't try to use faces we destroyed. */
4283
4284static void
4285free_realized_faces (struct face_cache *c)
4286{
4287 if (c && c->used)
4288 {
4289 int i, size;
4290 struct frame *f = c->f;
4291
4292 /* We must block input here because we can't process X events
4293 safely while only some faces are freed, or when the frame's
4294 current matrix still references freed faces. */
4295 block_input ();
4296
4297 for (i = 0; i < c->used; ++i)
4298 {
4299 free_realized_face (f, c->faces_by_id[i]);
4300 c->faces_by_id[i] = NULL;
4301 }
4302
4303 c->used = 0;
4304 size = FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
4305 memset (c->buckets, 0, size);
4306
4307 /* Must do a thorough redisplay the next time. Mark current
4308 matrices as invalid because they will reference faces freed
4309 above. This function is also called when a frame is
4310 destroyed. In this case, the root window of F is nil. */
4311 if (WINDOWP (f->root_window))
4312 {
4313 clear_current_matrices (f);
4314 ++windows_or_buffers_changed;
4315 }
4316
4317 unblock_input ();
4318 }
4319}
4320
4321
4322/* Free all realized faces on FRAME or on all frames if FRAME is nil.
4323 This is done after attributes of a named face have been changed,
4324 because we can't tell which realized faces depend on that face. */
4325
4326void
4327free_all_realized_faces (Lisp_Object frame)
4328{
4329 if (NILP (frame))
4330 {
4331 Lisp_Object rest;
4332 FOR_EACH_FRAME (rest, frame)
4333 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
4334 }
4335 else
4336 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
4337}
4338
4339
4340/* Free face cache C and faces in it, including their X resources. */
4341
4342static void
4343free_face_cache (struct face_cache *c)
4344{
4345 if (c)
4346 {
4347 free_realized_faces (c);
4348 xfree (c->buckets);
4349 xfree (c->faces_by_id);
4350 xfree (c);
4351 }
4352}
4353
4354
4355/* Cache realized face FACE in face cache C. HASH is the hash value
4356 of FACE. If FACE is for ASCII characters (i.e. FACE->ascii_face ==
4357 FACE), insert the new face to the beginning of the collision list
4358 of the face hash table of C. Otherwise, add the new face to the
4359 end of the collision list. This way, lookup_face can quickly find
4360 that a requested face is not cached. */
4361
4362static void
4363cache_face (struct face_cache *c, struct face *face, unsigned int hash)
4364{
4365 int i = hash % FACE_CACHE_BUCKETS_SIZE;
4366
4367 face->hash = hash;
4368
4369 if (face->ascii_face != face)
4370 {
4371 struct face *last = c->buckets[i];
4372 if (last)
4373 {
4374 while (last->next)
4375 last = last->next;
4376 last->next = face;
4377 face->prev = last;
4378 face->next = NULL;
4379 }
4380 else
4381 {
4382 c->buckets[i] = face;
4383 face->prev = face->next = NULL;
4384 }
4385 }
4386 else
4387 {
4388 face->prev = NULL;
4389 face->next = c->buckets[i];
4390 if (face->next)
4391 face->next->prev = face;
4392 c->buckets[i] = face;
4393 }
4394
4395 /* Find a free slot in C->faces_by_id and use the index of the free
4396 slot as FACE->id. */
4397 for (i = 0; i < c->used; ++i)
4398 if (c->faces_by_id[i] == NULL)
4399 break;
4400 face->id = i;
4401
4402#ifdef GLYPH_DEBUG
4403 /* Check that FACE got a unique id. */
4404 {
4405 int j, n;
4406 struct face *face1;
4407
4408 for (j = n = 0; j < FACE_CACHE_BUCKETS_SIZE; ++j)
4409 for (face1 = c->buckets[j]; face1; face1 = face1->next)
4410 if (face1->id == i)
4411 ++n;
4412
4413 eassert (n == 1);
4414 }
4415#endif /* GLYPH_DEBUG */
4416
4417 /* Maybe enlarge C->faces_by_id. */
4418 if (i == c->used)
4419 {
4420 if (c->used == c->size)
4421 c->faces_by_id = xpalloc (c->faces_by_id, &c->size, 1, MAX_FACE_ID,
4422 sizeof *c->faces_by_id);
4423 c->used++;
4424 }
4425
4426 c->faces_by_id[i] = face;
4427}
4428
4429
4430/* Remove face FACE from cache C. */
4431
4432static void
4433uncache_face (struct face_cache *c, struct face *face)
4434{
4435 int i = face->hash % FACE_CACHE_BUCKETS_SIZE;
4436
4437 if (face->prev)
4438 face->prev->next = face->next;
4439 else
4440 c->buckets[i] = face->next;
4441
4442 if (face->next)
4443 face->next->prev = face->prev;
4444
4445 c->faces_by_id[face->id] = NULL;
4446 if (face->id == c->used)
4447 --c->used;
4448}
4449
4450
4451/* Look up a realized face with face attributes ATTR in the face cache
4452 of frame F. The face will be used to display ASCII characters.
4453 Value is the ID of the face found. If no suitable face is found,
4454 realize a new one. */
4455
4456static int
4457lookup_face (struct frame *f, Lisp_Object *attr)
4458{
4459 struct face_cache *cache = FRAME_FACE_CACHE (f);
4460 unsigned hash;
4461 int i;
4462 struct face *face;
4463
4464 eassert (cache != NULL);
4465 check_lface_attrs (attr);
4466
4467 /* Look up ATTR in the face cache. */
4468 hash = lface_hash (attr);
4469 i = hash % FACE_CACHE_BUCKETS_SIZE;
4470
4471 for (face = cache->buckets[i]; face; face = face->next)
4472 {
4473 if (face->ascii_face != face)
4474 {
4475 /* There's no more ASCII face. */
4476 face = NULL;
4477 break;
4478 }
4479 if (face->hash == hash
4480 && lface_equal_p (face->lface, attr))
4481 break;
4482 }
4483
4484 /* If not found, realize a new face. */
4485 if (face == NULL)
4486 face = realize_face (cache, attr, -1);
4487
4488#ifdef GLYPH_DEBUG
4489 eassert (face == FACE_FROM_ID (f, face->id));
4490#endif /* GLYPH_DEBUG */
4491
4492 return face->id;
4493}
4494
4495#ifdef HAVE_WINDOW_SYSTEM
4496/* Look up a realized face that has the same attributes as BASE_FACE
4497 except for the font in the face cache of frame F. If FONT-OBJECT
4498 is not nil, it is an already opened font. If FONT-OBJECT is nil,
4499 the face has no font. Value is the ID of the face found. If no
4500 suitable face is found, realize a new one. */
4501
4502int
4503face_for_font (struct frame *f, Lisp_Object font_object, struct face *base_face)
4504{
4505 struct face_cache *cache = FRAME_FACE_CACHE (f);
4506 unsigned hash;
4507 int i;
4508 struct face *face;
4509
4510 eassert (cache != NULL);
4511 base_face = base_face->ascii_face;
4512 hash = lface_hash (base_face->lface);
4513 i = hash % FACE_CACHE_BUCKETS_SIZE;
4514
4515 for (face = cache->buckets[i]; face; face = face->next)
4516 {
4517 if (face->ascii_face == face)
4518 continue;
4519 if (face->ascii_face == base_face
4520 && face->font == (NILP (font_object) ? NULL
4521 : XFONT_OBJECT (font_object))
4522 && lface_equal_p (face->lface, base_face->lface))
4523 return face->id;
4524 }
4525
4526 /* If not found, realize a new face. */
4527 face = realize_non_ascii_face (f, font_object, base_face);
4528 return face->id;
4529}
4530#endif /* HAVE_WINDOW_SYSTEM */
4531
4532/* Return the face id of the realized face for named face SYMBOL on
4533 frame F suitable for displaying ASCII characters. Value is -1 if
4534 the face couldn't be determined, which might happen if the default
4535 face isn't realized and cannot be realized. */
4536
4537int
4538lookup_named_face (struct frame *f, Lisp_Object symbol, int signal_p)
4539{
4540 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4541 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
4542 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4543
4544 if (default_face == NULL)
4545 {
4546 if (!realize_basic_faces (f))
4547 return -1;
4548 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4549 if (default_face == NULL)
4550 emacs_abort (); /* realize_basic_faces must have set it up */
4551 }
4552
4553 if (! get_lface_attributes (f, symbol, symbol_attrs, signal_p, 0))
4554 return -1;
4555
4556 memcpy (attrs, default_face->lface, sizeof attrs);
4557 merge_face_vectors (f, symbol_attrs, attrs, 0);
4558
4559 return lookup_face (f, attrs);
4560}
4561
4562
4563/* Return the display face-id of the basic face whose canonical face-id
4564 is FACE_ID. The return value will usually simply be FACE_ID, unless that
4565 basic face has bee remapped via Vface_remapping_alist. This function is
4566 conservative: if something goes wrong, it will simply return FACE_ID
4567 rather than signal an error. */
4568
4569int
4570lookup_basic_face (struct frame *f, int face_id)
4571{
4572 Lisp_Object name, mapping;
4573 int remapped_face_id;
4574
4575 if (NILP (Vface_remapping_alist))
4576 return face_id; /* Nothing to do. */
4577
4578 switch (face_id)
4579 {
4580 case DEFAULT_FACE_ID: name = Qdefault; break;
4581 case MODE_LINE_FACE_ID: name = Qmode_line; break;
4582 case MODE_LINE_INACTIVE_FACE_ID: name = Qmode_line_inactive; break;
4583 case HEADER_LINE_FACE_ID: name = Qheader_line; break;
4584 case TOOL_BAR_FACE_ID: name = Qtool_bar; break;
4585 case FRINGE_FACE_ID: name = Qfringe; break;
4586 case SCROLL_BAR_FACE_ID: name = Qscroll_bar; break;
4587 case BORDER_FACE_ID: name = Qborder; break;
4588 case CURSOR_FACE_ID: name = Qcursor; break;
4589 case MOUSE_FACE_ID: name = Qmouse; break;
4590 case MENU_FACE_ID: name = Qmenu; break;
4591
4592 default:
4593 emacs_abort (); /* the caller is supposed to pass us a basic face id */
4594 }
4595
4596 /* Do a quick scan through Vface_remapping_alist, and return immediately
4597 if there is no remapping for face NAME. This is just an optimization
4598 for the very common no-remapping case. */
4599 mapping = assq_no_quit (name, Vface_remapping_alist);
4600 if (NILP (mapping))
4601 return face_id; /* Give up. */
4602
4603 /* If there is a remapping entry, lookup the face using NAME, which will
4604 handle the remapping too. */
4605 remapped_face_id = lookup_named_face (f, name, 0);
4606 if (remapped_face_id < 0)
4607 return face_id; /* Give up. */
4608
4609 return remapped_face_id;
4610}
4611
4612
4613/* Return a face for charset ASCII that is like the face with id
4614 FACE_ID on frame F, but has a font that is STEPS steps smaller.
4615 STEPS < 0 means larger. Value is the id of the face. */
4616
4617int
4618smaller_face (struct frame *f, int face_id, int steps)
4619{
4620#ifdef HAVE_WINDOW_SYSTEM
4621 struct face *face;
4622 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4623 int pt, last_pt, last_height;
4624 int delta;
4625 int new_face_id;
4626 struct face *new_face;
4627
4628 /* If not called for an X frame, just return the original face. */
4629 if (FRAME_TERMCAP_P (f))
4630 return face_id;
4631
4632 /* Try in increments of 1/2 pt. */
4633 delta = steps < 0 ? 5 : -5;
4634 steps = eabs (steps);
4635
4636 face = FACE_FROM_ID (f, face_id);
4637 memcpy (attrs, face->lface, sizeof attrs);
4638 pt = last_pt = XFASTINT (attrs[LFACE_HEIGHT_INDEX]);
4639 new_face_id = face_id;
4640 last_height = FONT_HEIGHT (face->font);
4641
4642 while (steps
4643 && pt + delta > 0
4644 /* Give up if we cannot find a font within 10pt. */
4645 && eabs (last_pt - pt) < 100)
4646 {
4647 /* Look up a face for a slightly smaller/larger font. */
4648 pt += delta;
4649 attrs[LFACE_HEIGHT_INDEX] = make_number (pt);
4650 new_face_id = lookup_face (f, attrs);
4651 new_face = FACE_FROM_ID (f, new_face_id);
4652
4653 /* If height changes, count that as one step. */
4654 if ((delta < 0 && FONT_HEIGHT (new_face->font) < last_height)
4655 || (delta > 0 && FONT_HEIGHT (new_face->font) > last_height))
4656 {
4657 --steps;
4658 last_height = FONT_HEIGHT (new_face->font);
4659 last_pt = pt;
4660 }
4661 }
4662
4663 return new_face_id;
4664
4665#else /* not HAVE_WINDOW_SYSTEM */
4666
4667 return face_id;
4668
4669#endif /* not HAVE_WINDOW_SYSTEM */
4670}
4671
4672
4673/* Return a face for charset ASCII that is like the face with id
4674 FACE_ID on frame F, but has height HEIGHT. */
4675
4676int
4677face_with_height (struct frame *f, int face_id, int height)
4678{
4679#ifdef HAVE_WINDOW_SYSTEM
4680 struct face *face;
4681 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4682
4683 if (FRAME_TERMCAP_P (f)
4684 || height <= 0)
4685 return face_id;
4686
4687 face = FACE_FROM_ID (f, face_id);
4688 memcpy (attrs, face->lface, sizeof attrs);
4689 attrs[LFACE_HEIGHT_INDEX] = make_number (height);
4690 font_clear_prop (attrs, FONT_SIZE_INDEX);
4691 face_id = lookup_face (f, attrs);
4692#endif /* HAVE_WINDOW_SYSTEM */
4693
4694 return face_id;
4695}
4696
4697
4698/* Return the face id of the realized face for named face SYMBOL on
4699 frame F suitable for displaying ASCII characters, and use
4700 attributes of the face FACE_ID for attributes that aren't
4701 completely specified by SYMBOL. This is like lookup_named_face,
4702 except that the default attributes come from FACE_ID, not from the
4703 default face. FACE_ID is assumed to be already realized. */
4704
4705int
4706lookup_derived_face (struct frame *f, Lisp_Object symbol, int face_id,
4707 int signal_p)
4708{
4709 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4710 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
4711 struct face *default_face = FACE_FROM_ID (f, face_id);
4712
4713 if (!default_face)
4714 emacs_abort ();
4715
4716 if (!get_lface_attributes (f, symbol, symbol_attrs, signal_p, 0))
4717 return -1;
4718
4719 memcpy (attrs, default_face->lface, sizeof attrs);
4720 merge_face_vectors (f, symbol_attrs, attrs, 0);
4721 return lookup_face (f, attrs);
4722}
4723
4724DEFUN ("face-attributes-as-vector", Fface_attributes_as_vector,
4725 Sface_attributes_as_vector, 1, 1, 0,
4726 doc: /* Return a vector of face attributes corresponding to PLIST. */)
4727 (Lisp_Object plist)
4728{
4729 Lisp_Object lface;
4730 lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
4731 Qunspecified);
4732 merge_face_ref (XFRAME (selected_frame), plist, XVECTOR (lface)->contents,
4733 1, 0);
4734 return lface;
4735}
4736
4737
4738\f
4739/***********************************************************************
4740 Face capability testing
4741 ***********************************************************************/
4742
4743
4744/* If the distance (as returned by color_distance) between two colors is
4745 less than this, then they are considered the same, for determining
4746 whether a color is supported or not. The range of values is 0-65535. */
4747
4748#define TTY_SAME_COLOR_THRESHOLD 10000
4749
4750#ifdef HAVE_WINDOW_SYSTEM
4751
4752/* Return non-zero if all the face attributes in ATTRS are supported
4753 on the window-system frame F.
4754
4755 The definition of `supported' is somewhat heuristic, but basically means
4756 that a face containing all the attributes in ATTRS, when merged with the
4757 default face for display, can be represented in a way that's
4758
4759 \(1) different in appearance than the default face, and
4760 \(2) `close in spirit' to what the attributes specify, if not exact. */
4761
4762static int
4763x_supports_face_attributes_p (struct frame *f,
4764 Lisp_Object attrs[LFACE_VECTOR_SIZE],
4765 struct face *def_face)
4766{
4767 Lisp_Object *def_attrs = def_face->lface;
4768
4769 /* Check that other specified attributes are different that the default
4770 face. */
4771 if ((!UNSPECIFIEDP (attrs[LFACE_UNDERLINE_INDEX])
4772 && face_attr_equal_p (attrs[LFACE_UNDERLINE_INDEX],
4773 def_attrs[LFACE_UNDERLINE_INDEX]))
4774 || (!UNSPECIFIEDP (attrs[LFACE_INVERSE_INDEX])
4775 && face_attr_equal_p (attrs[LFACE_INVERSE_INDEX],
4776 def_attrs[LFACE_INVERSE_INDEX]))
4777 || (!UNSPECIFIEDP (attrs[LFACE_FOREGROUND_INDEX])
4778 && face_attr_equal_p (attrs[LFACE_FOREGROUND_INDEX],
4779 def_attrs[LFACE_FOREGROUND_INDEX]))
4780 || (!UNSPECIFIEDP (attrs[LFACE_BACKGROUND_INDEX])
4781 && face_attr_equal_p (attrs[LFACE_BACKGROUND_INDEX],
4782 def_attrs[LFACE_BACKGROUND_INDEX]))
4783 || (!UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
4784 && face_attr_equal_p (attrs[LFACE_STIPPLE_INDEX],
4785 def_attrs[LFACE_STIPPLE_INDEX]))
4786 || (!UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
4787 && face_attr_equal_p (attrs[LFACE_OVERLINE_INDEX],
4788 def_attrs[LFACE_OVERLINE_INDEX]))
4789 || (!UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
4790 && face_attr_equal_p (attrs[LFACE_STRIKE_THROUGH_INDEX],
4791 def_attrs[LFACE_STRIKE_THROUGH_INDEX]))
4792 || (!UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
4793 && face_attr_equal_p (attrs[LFACE_BOX_INDEX],
4794 def_attrs[LFACE_BOX_INDEX])))
4795 return 0;
4796
4797 /* Check font-related attributes, as those are the most commonly
4798 "unsupported" on a window-system (because of missing fonts). */
4799 if (!UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
4800 || !UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
4801 || !UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
4802 || !UNSPECIFIEDP (attrs[LFACE_WEIGHT_INDEX])
4803 || !UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX])
4804 || !UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX]))
4805 {
4806 int face_id;
4807 struct face *face;
4808 Lisp_Object merged_attrs[LFACE_VECTOR_SIZE];
4809 int i;
4810
4811 memcpy (merged_attrs, def_attrs, sizeof merged_attrs);
4812
4813 merge_face_vectors (f, attrs, merged_attrs, 0);
4814
4815 face_id = lookup_face (f, merged_attrs);
4816 face = FACE_FROM_ID (f, face_id);
4817
4818 if (! face)
4819 error ("Cannot make face");
4820
4821 /* If the font is the same, or no font is found, then not
4822 supported. */
4823 if (face->font == def_face->font
4824 || ! face->font)
4825 return 0;
4826 for (i = FONT_TYPE_INDEX; i <= FONT_SIZE_INDEX; i++)
4827 if (! EQ (face->font->props[i], def_face->font->props[i]))
4828 {
4829 Lisp_Object s1, s2;
4830
4831 if (i < FONT_FOUNDRY_INDEX || i > FONT_REGISTRY_INDEX
4832 || face->font->driver->case_sensitive)
4833 return 1;
4834 s1 = SYMBOL_NAME (face->font->props[i]);
4835 s2 = SYMBOL_NAME (def_face->font->props[i]);
4836 if (! EQ (Fcompare_strings (s1, make_number (0), Qnil,
4837 s2, make_number (0), Qnil, Qt), Qt))
4838 return 1;
4839 }
4840 return 0;
4841 }
4842
4843 /* Everything checks out, this face is supported. */
4844 return 1;
4845}
4846
4847#endif /* HAVE_WINDOW_SYSTEM */
4848
4849/* Return non-zero if all the face attributes in ATTRS are supported
4850 on the tty frame F.
4851
4852 The definition of `supported' is somewhat heuristic, but basically means
4853 that a face containing all the attributes in ATTRS, when merged
4854 with the default face for display, can be represented in a way that's
4855
4856 \(1) different in appearance than the default face, and
4857 \(2) `close in spirit' to what the attributes specify, if not exact.
4858
4859 Point (2) implies that a `:weight black' attribute will be satisfied
4860 by any terminal that can display bold, and a `:foreground "yellow"' as
4861 long as the terminal can display a yellowish color, but `:slant italic'
4862 will _not_ be satisfied by the tty display code's automatic
4863 substitution of a `dim' face for italic. */
4864
4865static int
4866tty_supports_face_attributes_p (struct frame *f,
4867 Lisp_Object attrs[LFACE_VECTOR_SIZE],
4868 struct face *def_face)
4869{
4870 int weight, slant;
4871 Lisp_Object val, fg, bg;
4872 XColor fg_tty_color, fg_std_color;
4873 XColor bg_tty_color, bg_std_color;
4874 unsigned test_caps = 0;
4875 Lisp_Object *def_attrs = def_face->lface;
4876
4877 /* First check some easy-to-check stuff; ttys support none of the
4878 following attributes, so we can just return false if any are requested
4879 (even if `nominal' values are specified, we should still return false,
4880 as that will be the same value that the default face uses). We
4881 consider :slant unsupportable on ttys, even though the face code
4882 actually `fakes' them using a dim attribute if possible. This is
4883 because the faked result is too different from what the face
4884 specifies. */
4885 if (!UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
4886 || !UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
4887 || !UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
4888 || !UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
4889 || !UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
4890 || !UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
4891 || !UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
4892 || !UNSPECIFIEDP (attrs[LFACE_BOX_INDEX]))
4893 return 0;
4894
4895 /* Test for terminal `capabilities' (non-color character attributes). */
4896
4897 /* font weight (bold/dim) */
4898 val = attrs[LFACE_WEIGHT_INDEX];
4899 if (!UNSPECIFIEDP (val)
4900 && (weight = FONT_WEIGHT_NAME_NUMERIC (val), weight >= 0))
4901 {
4902 int def_weight = FONT_WEIGHT_NAME_NUMERIC (def_attrs[LFACE_WEIGHT_INDEX]);
4903
4904 if (weight > 100)
4905 {
4906 if (def_weight > 100)
4907 return 0; /* same as default */
4908 test_caps = TTY_CAP_BOLD;
4909 }
4910 else if (weight < 100)
4911 {
4912 if (def_weight < 100)
4913 return 0; /* same as default */
4914 test_caps = TTY_CAP_DIM;
4915 }
4916 else if (def_weight == 100)
4917 return 0; /* same as default */
4918 }
4919
4920 /* font slant */
4921 val = attrs[LFACE_SLANT_INDEX];
4922 if (!UNSPECIFIEDP (val)
4923 && (slant = FONT_SLANT_NAME_NUMERIC (val), slant >= 0))
4924 {
4925 int def_slant = FONT_SLANT_NAME_NUMERIC (def_attrs[LFACE_SLANT_INDEX]);
4926 if (slant == 100 || slant == def_slant)
4927 return 0; /* same as default */
4928 else
4929 test_caps |= TTY_CAP_ITALIC;
4930 }
4931
4932 /* underlining */
4933 val = attrs[LFACE_UNDERLINE_INDEX];
4934 if (!UNSPECIFIEDP (val))
4935 {
4936 if (STRINGP (val))
4937 return 0; /* ttys can't use colored underlines */
4938 else if (face_attr_equal_p (val, def_attrs[LFACE_UNDERLINE_INDEX]))
4939 return 0; /* same as default */
4940 else
4941 test_caps |= TTY_CAP_UNDERLINE;
4942 }
4943
4944 /* inverse video */
4945 val = attrs[LFACE_INVERSE_INDEX];
4946 if (!UNSPECIFIEDP (val))
4947 {
4948 if (face_attr_equal_p (val, def_attrs[LFACE_INVERSE_INDEX]))
4949 return 0; /* same as default */
4950 else
4951 test_caps |= TTY_CAP_INVERSE;
4952 }
4953
4954
4955 /* Color testing. */
4956
4957 /* Default the color indices in FG_TTY_COLOR and BG_TTY_COLOR, since
4958 we use them when calling `tty_capable_p' below, even if the face
4959 specifies no colors. */
4960 fg_tty_color.pixel = FACE_TTY_DEFAULT_FG_COLOR;
4961 bg_tty_color.pixel = FACE_TTY_DEFAULT_BG_COLOR;
4962
4963 /* Check if foreground color is close enough. */
4964 fg = attrs[LFACE_FOREGROUND_INDEX];
4965 if (STRINGP (fg))
4966 {
4967 Lisp_Object def_fg = def_attrs[LFACE_FOREGROUND_INDEX];
4968
4969 if (face_attr_equal_p (fg, def_fg))
4970 return 0; /* same as default */
4971 else if (! tty_lookup_color (f, fg, &fg_tty_color, &fg_std_color))
4972 return 0; /* not a valid color */
4973 else if (color_distance (&fg_tty_color, &fg_std_color)
4974 > TTY_SAME_COLOR_THRESHOLD)
4975 return 0; /* displayed color is too different */
4976 else
4977 /* Make sure the color is really different than the default. */
4978 {
4979 XColor def_fg_color;
4980 if (tty_lookup_color (f, def_fg, &def_fg_color, 0)
4981 && (color_distance (&fg_tty_color, &def_fg_color)
4982 <= TTY_SAME_COLOR_THRESHOLD))
4983 return 0;
4984 }
4985 }
4986
4987 /* Check if background color is close enough. */
4988 bg = attrs[LFACE_BACKGROUND_INDEX];
4989 if (STRINGP (bg))
4990 {
4991 Lisp_Object def_bg = def_attrs[LFACE_BACKGROUND_INDEX];
4992
4993 if (face_attr_equal_p (bg, def_bg))
4994 return 0; /* same as default */
4995 else if (! tty_lookup_color (f, bg, &bg_tty_color, &bg_std_color))
4996 return 0; /* not a valid color */
4997 else if (color_distance (&bg_tty_color, &bg_std_color)
4998 > TTY_SAME_COLOR_THRESHOLD)
4999 return 0; /* displayed color is too different */
5000 else
5001 /* Make sure the color is really different than the default. */
5002 {
5003 XColor def_bg_color;
5004 if (tty_lookup_color (f, def_bg, &def_bg_color, 0)
5005 && (color_distance (&bg_tty_color, &def_bg_color)
5006 <= TTY_SAME_COLOR_THRESHOLD))
5007 return 0;
5008 }
5009 }
5010
5011 /* If both foreground and background are requested, see if the
5012 distance between them is OK. We just check to see if the distance
5013 between the tty's foreground and background is close enough to the
5014 distance between the standard foreground and background. */
5015 if (STRINGP (fg) && STRINGP (bg))
5016 {
5017 int delta_delta
5018 = (color_distance (&fg_std_color, &bg_std_color)
5019 - color_distance (&fg_tty_color, &bg_tty_color));
5020 if (delta_delta > TTY_SAME_COLOR_THRESHOLD
5021 || delta_delta < -TTY_SAME_COLOR_THRESHOLD)
5022 return 0;
5023 }
5024
5025
5026 /* See if the capabilities we selected above are supported, with the
5027 given colors. */
5028 if (test_caps != 0 &&
5029 ! tty_capable_p (FRAME_TTY (f), test_caps, fg_tty_color.pixel,
5030 bg_tty_color.pixel))
5031 return 0;
5032
5033
5034 /* Hmmm, everything checks out, this terminal must support this face. */
5035 return 1;
5036}
5037
5038
5039DEFUN ("display-supports-face-attributes-p",
5040 Fdisplay_supports_face_attributes_p, Sdisplay_supports_face_attributes_p,
5041 1, 2, 0,
5042 doc: /* Return non-nil if all the face attributes in ATTRIBUTES are supported.
5043The optional argument DISPLAY can be a display name, a frame, or
5044nil (meaning the selected frame's display).
5045
5046The definition of `supported' is somewhat heuristic, but basically means
5047that a face containing all the attributes in ATTRIBUTES, when merged
5048with the default face for display, can be represented in a way that's
5049
5050 \(1) different in appearance than the default face, and
5051 \(2) `close in spirit' to what the attributes specify, if not exact.
5052
5053Point (2) implies that a `:weight black' attribute will be satisfied by
5054any display that can display bold, and a `:foreground \"yellow\"' as long
5055as it can display a yellowish color, but `:slant italic' will _not_ be
5056satisfied by the tty display code's automatic substitution of a `dim'
5057face for italic. */)
5058 (Lisp_Object attributes, Lisp_Object display)
5059{
5060 int supports = 0, i;
5061 Lisp_Object frame;
5062 struct frame *f;
5063 struct face *def_face;
5064 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5065
5066 if (noninteractive || !initialized)
5067 /* We may not be able to access low-level face information in batch
5068 mode, or before being dumped, and this function is not going to
5069 be very useful in those cases anyway, so just give up. */
5070 return Qnil;
5071
5072 if (NILP (display))
5073 frame = selected_frame;
5074 else if (FRAMEP (display))
5075 frame = display;
5076 else
5077 {
5078 /* Find any frame on DISPLAY. */
5079 Lisp_Object fl_tail;
5080
5081 frame = Qnil;
5082 for (fl_tail = Vframe_list; CONSP (fl_tail); fl_tail = XCDR (fl_tail))
5083 {
5084 frame = XCAR (fl_tail);
5085 if (!NILP (Fequal (Fcdr (Fassq (Qdisplay,
5086 XFRAME (frame)->param_alist)),
5087 display)))
5088 break;
5089 }
5090 }
5091
5092 CHECK_LIVE_FRAME (frame);
5093 f = XFRAME (frame);
5094
5095 for (i = 0; i < LFACE_VECTOR_SIZE; i++)
5096 attrs[i] = Qunspecified;
5097 merge_face_ref (f, attributes, attrs, 1, 0);
5098
5099 def_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5100 if (def_face == NULL)
5101 {
5102 if (! realize_basic_faces (f))
5103 error ("Cannot realize default face");
5104 def_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5105 if (def_face == NULL)
5106 emacs_abort (); /* realize_basic_faces must have set it up */
5107 }
5108
5109 /* Dispatch to the appropriate handler. */
5110 if (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
5111 supports = tty_supports_face_attributes_p (f, attrs, def_face);
5112#ifdef HAVE_WINDOW_SYSTEM
5113 else
5114 supports = x_supports_face_attributes_p (f, attrs, def_face);
5115#endif
5116
5117 return supports ? Qt : Qnil;
5118}
5119
5120\f
5121/***********************************************************************
5122 Font selection
5123 ***********************************************************************/
5124
5125DEFUN ("internal-set-font-selection-order",
5126 Finternal_set_font_selection_order,
5127 Sinternal_set_font_selection_order, 1, 1, 0,
5128 doc: /* Set font selection order for face font selection to ORDER.
5129ORDER must be a list of length 4 containing the symbols `:width',
5130`:height', `:weight', and `:slant'. Face attributes appearing
5131first in ORDER are matched first, e.g. if `:height' appears before
5132`:weight' in ORDER, font selection first tries to find a font with
5133a suitable height, and then tries to match the font weight.
5134Value is ORDER. */)
5135 (Lisp_Object order)
5136{
5137 Lisp_Object list;
5138 int i;
5139 int indices[DIM (font_sort_order)];
5140
5141 CHECK_LIST (order);
5142 memset (indices, 0, sizeof indices);
5143 i = 0;
5144
5145 for (list = order;
5146 CONSP (list) && i < DIM (indices);
5147 list = XCDR (list), ++i)
5148 {
5149 Lisp_Object attr = XCAR (list);
5150 int xlfd;
5151
5152 if (EQ (attr, QCwidth))
5153 xlfd = XLFD_SWIDTH;
5154 else if (EQ (attr, QCheight))
5155 xlfd = XLFD_POINT_SIZE;
5156 else if (EQ (attr, QCweight))
5157 xlfd = XLFD_WEIGHT;
5158 else if (EQ (attr, QCslant))
5159 xlfd = XLFD_SLANT;
5160 else
5161 break;
5162
5163 if (indices[i] != 0)
5164 break;
5165 indices[i] = xlfd;
5166 }
5167
5168 if (!NILP (list) || i != DIM (indices))
5169 signal_error ("Invalid font sort order", order);
5170 for (i = 0; i < DIM (font_sort_order); ++i)
5171 if (indices[i] == 0)
5172 signal_error ("Invalid font sort order", order);
5173
5174 if (memcmp (indices, font_sort_order, sizeof indices) != 0)
5175 {
5176 memcpy (font_sort_order, indices, sizeof font_sort_order);
5177 free_all_realized_faces (Qnil);
5178 }
5179
5180 font_update_sort_order (font_sort_order);
5181
5182 return Qnil;
5183}
5184
5185
5186DEFUN ("internal-set-alternative-font-family-alist",
5187 Finternal_set_alternative_font_family_alist,
5188 Sinternal_set_alternative_font_family_alist, 1, 1, 0,
5189 doc: /* Define alternative font families to try in face font selection.
5190ALIST is an alist of (FAMILY ALTERNATIVE1 ALTERNATIVE2 ...) entries.
5191Each ALTERNATIVE is tried in order if no fonts of font family FAMILY can
5192be found. Value is ALIST. */)
5193 (Lisp_Object alist)
5194{
5195 Lisp_Object entry, tail, tail2;
5196
5197 CHECK_LIST (alist);
5198 alist = Fcopy_sequence (alist);
5199 for (tail = alist; CONSP (tail); tail = XCDR (tail))
5200 {
5201 entry = XCAR (tail);
5202 CHECK_LIST (entry);
5203 entry = Fcopy_sequence (entry);
5204 XSETCAR (tail, entry);
5205 for (tail2 = entry; CONSP (tail2); tail2 = XCDR (tail2))
5206 XSETCAR (tail2, Fintern (XCAR (tail2), Qnil));
5207 }
5208
5209 Vface_alternative_font_family_alist = alist;
5210 free_all_realized_faces (Qnil);
5211 return alist;
5212}
5213
5214
5215DEFUN ("internal-set-alternative-font-registry-alist",
5216 Finternal_set_alternative_font_registry_alist,
5217 Sinternal_set_alternative_font_registry_alist, 1, 1, 0,
5218 doc: /* Define alternative font registries to try in face font selection.
5219ALIST is an alist of (REGISTRY ALTERNATIVE1 ALTERNATIVE2 ...) entries.
5220Each ALTERNATIVE is tried in order if no fonts of font registry REGISTRY can
5221be found. Value is ALIST. */)
5222 (Lisp_Object alist)
5223{
5224 Lisp_Object entry, tail, tail2;
5225
5226 CHECK_LIST (alist);
5227 alist = Fcopy_sequence (alist);
5228 for (tail = alist; CONSP (tail); tail = XCDR (tail))
5229 {
5230 entry = XCAR (tail);
5231 CHECK_LIST (entry);
5232 entry = Fcopy_sequence (entry);
5233 XSETCAR (tail, entry);
5234 for (tail2 = entry; CONSP (tail2); tail2 = XCDR (tail2))
5235 XSETCAR (tail2, Fdowncase (XCAR (tail2)));
5236 }
5237 Vface_alternative_font_registry_alist = alist;
5238 free_all_realized_faces (Qnil);
5239 return alist;
5240}
5241
5242
5243#ifdef HAVE_WINDOW_SYSTEM
5244
5245/* Return the fontset id of the base fontset name or alias name given
5246 by the fontset attribute of ATTRS. Value is -1 if the fontset
5247 attribute of ATTRS doesn't name a fontset. */
5248
5249static int
5250face_fontset (Lisp_Object attrs[LFACE_VECTOR_SIZE])
5251{
5252 Lisp_Object name;
5253
5254 name = attrs[LFACE_FONTSET_INDEX];
5255 if (!STRINGP (name))
5256 return -1;
5257 return fs_query_fontset (name, 0);
5258}
5259
5260#endif /* HAVE_WINDOW_SYSTEM */
5261
5262
5263\f
5264/***********************************************************************
5265 Face Realization
5266 ***********************************************************************/
5267
5268/* Realize basic faces on frame F. Value is zero if frame parameters
5269 of F don't contain enough information needed to realize the default
5270 face. */
5271
5272static int
5273realize_basic_faces (struct frame *f)
5274{
5275 int success_p = 0;
5276 ptrdiff_t count = SPECPDL_INDEX ();
5277
5278 /* Block input here so that we won't be surprised by an X expose
5279 event, for instance, without having the faces set up. */
5280 block_input ();
5281 specbind (Qscalable_fonts_allowed, Qt);
5282
5283 if (realize_default_face (f))
5284 {
5285 realize_named_face (f, Qmode_line, MODE_LINE_FACE_ID);
5286 realize_named_face (f, Qmode_line_inactive, MODE_LINE_INACTIVE_FACE_ID);
5287 realize_named_face (f, Qtool_bar, TOOL_BAR_FACE_ID);
5288 realize_named_face (f, Qfringe, FRINGE_FACE_ID);
5289 realize_named_face (f, Qheader_line, HEADER_LINE_FACE_ID);
5290 realize_named_face (f, Qscroll_bar, SCROLL_BAR_FACE_ID);
5291 realize_named_face (f, Qborder, BORDER_FACE_ID);
5292 realize_named_face (f, Qcursor, CURSOR_FACE_ID);
5293 realize_named_face (f, Qmouse, MOUSE_FACE_ID);
5294 realize_named_face (f, Qmenu, MENU_FACE_ID);
5295 realize_named_face (f, Qvertical_border, VERTICAL_BORDER_FACE_ID);
5296
5297 /* Reflect changes in the `menu' face in menu bars. */
5298 if (FRAME_FACE_CACHE (f)->menu_face_changed_p)
5299 {
5300 FRAME_FACE_CACHE (f)->menu_face_changed_p = 0;
5301#ifdef USE_X_TOOLKIT
5302 if (FRAME_WINDOW_P (f))
5303 x_update_menu_appearance (f);
5304#endif
5305 }
5306
5307 success_p = 1;
5308 }
5309
5310 unbind_to (count, Qnil);
5311 unblock_input ();
5312 return success_p;
5313}
5314
5315
5316/* Realize the default face on frame F. If the face is not fully
5317 specified, make it fully-specified. Attributes of the default face
5318 that are not explicitly specified are taken from frame parameters. */
5319
5320static int
5321realize_default_face (struct frame *f)
5322{
5323 struct face_cache *c = FRAME_FACE_CACHE (f);
5324 Lisp_Object lface;
5325 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5326 struct face *face;
5327
5328 /* If the `default' face is not yet known, create it. */
5329 lface = lface_from_face_name (f, Qdefault, 0);
5330 if (NILP (lface))
5331 {
5332 Lisp_Object frame;
5333 XSETFRAME (frame, f);
5334 lface = Finternal_make_lisp_face (Qdefault, frame);
5335 }
5336
5337#ifdef HAVE_WINDOW_SYSTEM
5338 if (FRAME_WINDOW_P (f))
5339 {
5340 Lisp_Object font_object;
5341
5342 XSETFONT (font_object, FRAME_FONT (f));
5343 set_lface_from_font (f, lface, font_object, f->default_face_done_p);
5344 ASET (lface, LFACE_FONTSET_INDEX, fontset_name (FRAME_FONTSET (f)));
5345 f->default_face_done_p = 1;
5346 }
5347#endif /* HAVE_WINDOW_SYSTEM */
5348
5349 if (!FRAME_WINDOW_P (f))
5350 {
5351 ASET (lface, LFACE_FAMILY_INDEX, build_string ("default"));
5352 ASET (lface, LFACE_FOUNDRY_INDEX, LFACE_FAMILY (lface));
5353 ASET (lface, LFACE_SWIDTH_INDEX, Qnormal);
5354 ASET (lface, LFACE_HEIGHT_INDEX, make_number (1));
5355 if (UNSPECIFIEDP (LFACE_WEIGHT (lface)))
5356 ASET (lface, LFACE_WEIGHT_INDEX, Qnormal);
5357 if (UNSPECIFIEDP (LFACE_SLANT (lface)))
5358 ASET (lface, LFACE_SLANT_INDEX, Qnormal);
5359 if (UNSPECIFIEDP (LFACE_FONTSET (lface)))
5360 ASET (lface, LFACE_FONTSET_INDEX, Qnil);
5361 }
5362
5363 if (UNSPECIFIEDP (LFACE_UNDERLINE (lface)))
5364 ASET (lface, LFACE_UNDERLINE_INDEX, Qnil);
5365
5366 if (UNSPECIFIEDP (LFACE_OVERLINE (lface)))
5367 ASET (lface, LFACE_OVERLINE_INDEX, Qnil);
5368
5369 if (UNSPECIFIEDP (LFACE_STRIKE_THROUGH (lface)))
5370 ASET (lface, LFACE_STRIKE_THROUGH_INDEX, Qnil);
5371
5372 if (UNSPECIFIEDP (LFACE_BOX (lface)))
5373 ASET (lface, LFACE_BOX_INDEX, Qnil);
5374
5375 if (UNSPECIFIEDP (LFACE_INVERSE (lface)))
5376 ASET (lface, LFACE_INVERSE_INDEX, Qnil);
5377
5378 if (UNSPECIFIEDP (LFACE_FOREGROUND (lface)))
5379 {
5380 /* This function is called so early that colors are not yet
5381 set in the frame parameter list. */
5382 Lisp_Object color = Fassq (Qforeground_color, f->param_alist);
5383
5384 if (CONSP (color) && STRINGP (XCDR (color)))
5385 ASET (lface, LFACE_FOREGROUND_INDEX, XCDR (color));
5386 else if (FRAME_WINDOW_P (f))
5387 return 0;
5388 else if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
5389 ASET (lface, LFACE_FOREGROUND_INDEX, build_string (unspecified_fg));
5390 else
5391 emacs_abort ();
5392 }
5393
5394 if (UNSPECIFIEDP (LFACE_BACKGROUND (lface)))
5395 {
5396 /* This function is called so early that colors are not yet
5397 set in the frame parameter list. */
5398 Lisp_Object color = Fassq (Qbackground_color, f->param_alist);
5399 if (CONSP (color) && STRINGP (XCDR (color)))
5400 ASET (lface, LFACE_BACKGROUND_INDEX, XCDR (color));
5401 else if (FRAME_WINDOW_P (f))
5402 return 0;
5403 else if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
5404 ASET (lface, LFACE_BACKGROUND_INDEX, build_string (unspecified_bg));
5405 else
5406 emacs_abort ();
5407 }
5408
5409 if (UNSPECIFIEDP (LFACE_STIPPLE (lface)))
5410 ASET (lface, LFACE_STIPPLE_INDEX, Qnil);
5411
5412 /* Realize the face; it must be fully-specified now. */
5413 eassert (lface_fully_specified_p (XVECTOR (lface)->contents));
5414 check_lface (lface);
5415 memcpy (attrs, XVECTOR (lface)->contents, sizeof attrs);
5416 face = realize_face (c, attrs, DEFAULT_FACE_ID);
5417
5418#ifdef HAVE_WINDOW_SYSTEM
5419#ifdef HAVE_X_WINDOWS
5420 if (FRAME_X_P (f) && face->font != FRAME_FONT (f))
5421 {
5422 /* This can happen when making a frame on a display that does
5423 not support the default font. */
5424 if (!face->font)
5425 return 0;
5426
5427 /* Otherwise, the font specified for the frame was not
5428 acceptable as a font for the default face (perhaps because
5429 auto-scaled fonts are rejected), so we must adjust the frame
5430 font. */
5431 x_set_font (f, LFACE_FONT (lface), Qnil);
5432 }
5433#endif /* HAVE_X_WINDOWS */
5434#endif /* HAVE_WINDOW_SYSTEM */
5435 return 1;
5436}
5437
5438
5439/* Realize basic faces other than the default face in face cache C.
5440 SYMBOL is the face name, ID is the face id the realized face must
5441 have. The default face must have been realized already. */
5442
5443static void
5444realize_named_face (struct frame *f, Lisp_Object symbol, int id)
5445{
5446 struct face_cache *c = FRAME_FACE_CACHE (f);
5447 Lisp_Object lface = lface_from_face_name (f, symbol, 0);
5448 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5449 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
5450
5451 /* The default face must exist and be fully specified. */
5452 get_lface_attributes_no_remap (f, Qdefault, attrs, 1);
5453 check_lface_attrs (attrs);
5454 eassert (lface_fully_specified_p (attrs));
5455
5456 /* If SYMBOL isn't know as a face, create it. */
5457 if (NILP (lface))
5458 {
5459 Lisp_Object frame;
5460 XSETFRAME (frame, f);
5461 lface = Finternal_make_lisp_face (symbol, frame);
5462 }
5463
5464 /* Merge SYMBOL's face with the default face. */
5465 get_lface_attributes_no_remap (f, symbol, symbol_attrs, 1);
5466 merge_face_vectors (f, symbol_attrs, attrs, 0);
5467
5468 /* Realize the face. */
5469 realize_face (c, attrs, id);
5470}
5471
5472
5473/* Realize the fully-specified face with attributes ATTRS in face
5474 cache CACHE for ASCII characters. If FORMER_FACE_ID is
5475 non-negative, it is an ID of face to remove before caching the new
5476 face. Value is a pointer to the newly created realized face. */
5477
5478static struct face *
5479realize_face (struct face_cache *cache, Lisp_Object attrs[LFACE_VECTOR_SIZE],
5480 int former_face_id)
5481{
5482 struct face *face;
5483
5484 /* LFACE must be fully specified. */
5485 eassert (cache != NULL);
5486 check_lface_attrs (attrs);
5487
5488 if (former_face_id >= 0 && cache->used > former_face_id)
5489 {
5490 /* Remove the former face. */
5491 struct face *former_face = cache->faces_by_id[former_face_id];
5492 uncache_face (cache, former_face);
5493 free_realized_face (cache->f, former_face);
5494 SET_FRAME_GARBAGED (cache->f);
5495 }
5496
5497 if (FRAME_WINDOW_P (cache->f))
5498 face = realize_x_face (cache, attrs);
5499 else if (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f))
5500 face = realize_tty_face (cache, attrs);
5501 else if (FRAME_INITIAL_P (cache->f))
5502 {
5503 /* Create a dummy face. */
5504 face = make_realized_face (attrs);
5505 }
5506 else
5507 emacs_abort ();
5508
5509 /* Insert the new face. */
5510 cache_face (cache, face, lface_hash (attrs));
5511 return face;
5512}
5513
5514
5515#ifdef HAVE_WINDOW_SYSTEM
5516/* Realize the fully-specified face that uses FONT-OBJECT and has the
5517 same attributes as BASE_FACE except for the font on frame F.
5518 FONT-OBJECT may be nil, in which case, realized a face of
5519 no-font. */
5520
5521static struct face *
5522realize_non_ascii_face (struct frame *f, Lisp_Object font_object,
5523 struct face *base_face)
5524{
5525 struct face_cache *cache = FRAME_FACE_CACHE (f);
5526 struct face *face;
5527
5528 face = xmalloc (sizeof *face);
5529 *face = *base_face;
5530 face->gc = 0;
5531 face->extra = NULL;
5532 face->overstrike
5533 = (! NILP (font_object)
5534 && FONT_WEIGHT_NAME_NUMERIC (face->lface[LFACE_WEIGHT_INDEX]) > 100
5535 && FONT_WEIGHT_NUMERIC (font_object) <= 100);
5536
5537 /* Don't try to free the colors copied bitwise from BASE_FACE. */
5538 face->colors_copied_bitwise_p = 1;
5539 face->font = NILP (font_object) ? NULL : XFONT_OBJECT (font_object);
5540 face->gc = 0;
5541
5542 cache_face (cache, face, face->hash);
5543
5544 return face;
5545}
5546#endif /* HAVE_WINDOW_SYSTEM */
5547
5548
5549/* Realize the fully-specified face with attributes ATTRS in face
5550 cache CACHE for ASCII characters. Do it for X frame CACHE->f. If
5551 the new face doesn't share font with the default face, a fontname
5552 is allocated from the heap and set in `font_name' of the new face,
5553 but it is not yet loaded here. Value is a pointer to the newly
5554 created realized face. */
5555
5556static struct face *
5557realize_x_face (struct face_cache *cache, Lisp_Object attrs[LFACE_VECTOR_SIZE])
5558{
5559 struct face *face = NULL;
5560#ifdef HAVE_WINDOW_SYSTEM
5561 struct face *default_face;
5562 struct frame *f;
5563 Lisp_Object stipple, underline, overline, strike_through, box;
5564
5565 eassert (FRAME_WINDOW_P (cache->f));
5566
5567 /* Allocate a new realized face. */
5568 face = make_realized_face (attrs);
5569 face->ascii_face = face;
5570
5571 f = cache->f;
5572
5573 /* Determine the font to use. Most of the time, the font will be
5574 the same as the font of the default face, so try that first. */
5575 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5576 if (default_face
5577 && lface_same_font_attributes_p (default_face->lface, attrs))
5578 {
5579 face->font = default_face->font;
5580 face->fontset
5581 = make_fontset_for_ascii_face (f, default_face->fontset, face);
5582 }
5583 else
5584 {
5585 /* If the face attribute ATTRS specifies a fontset, use it as
5586 the base of a new realized fontset. Otherwise, use the same
5587 base fontset as of the default face. The base determines
5588 registry and encoding of a font. It may also determine
5589 foundry and family. The other fields of font name pattern
5590 are constructed from ATTRS. */
5591 int fontset = face_fontset (attrs);
5592
5593 /* If we are realizing the default face, ATTRS should specify a
5594 fontset. In other words, if FONTSET is -1, we are not
5595 realizing the default face, thus the default face should have
5596 already been realized. */
5597 if (fontset == -1)
5598 {
5599 if (default_face)
5600 fontset = default_face->fontset;
5601 if (fontset == -1)
5602 emacs_abort ();
5603 }
5604 if (! FONT_OBJECT_P (attrs[LFACE_FONT_INDEX]))
5605 attrs[LFACE_FONT_INDEX]
5606 = font_load_for_lface (f, attrs, attrs[LFACE_FONT_INDEX]);
5607 if (FONT_OBJECT_P (attrs[LFACE_FONT_INDEX]))
5608 {
5609 face->font = XFONT_OBJECT (attrs[LFACE_FONT_INDEX]);
5610 face->fontset = make_fontset_for_ascii_face (f, fontset, face);
5611 }
5612 else
5613 {
5614 face->font = NULL;
5615 face->fontset = -1;
5616 }
5617 }
5618
5619 if (face->font
5620 && FONT_WEIGHT_NAME_NUMERIC (attrs[LFACE_WEIGHT_INDEX]) > 100
5621 && FONT_WEIGHT_NUMERIC (attrs[LFACE_FONT_INDEX]) <= 100)
5622 face->overstrike = 1;
5623
5624 /* Load colors, and set remaining attributes. */
5625
5626 load_face_colors (f, face, attrs);
5627
5628 /* Set up box. */
5629 box = attrs[LFACE_BOX_INDEX];
5630 if (STRINGP (box))
5631 {
5632 /* A simple box of line width 1 drawn in color given by
5633 the string. */
5634 face->box_color = load_color (f, face, attrs[LFACE_BOX_INDEX],
5635 LFACE_BOX_INDEX);
5636 face->box = FACE_SIMPLE_BOX;
5637 face->box_line_width = 1;
5638 }
5639 else if (INTEGERP (box))
5640 {
5641 /* Simple box of specified line width in foreground color of the
5642 face. */
5643 eassert (XINT (box) != 0);
5644 face->box = FACE_SIMPLE_BOX;
5645 face->box_line_width = XINT (box);
5646 face->box_color = face->foreground;
5647 face->box_color_defaulted_p = 1;
5648 }
5649 else if (CONSP (box))
5650 {
5651 /* `(:width WIDTH :color COLOR :shadow SHADOW)'. SHADOW
5652 being one of `raised' or `sunken'. */
5653 face->box = FACE_SIMPLE_BOX;
5654 face->box_color = face->foreground;
5655 face->box_color_defaulted_p = 1;
5656 face->box_line_width = 1;
5657
5658 while (CONSP (box))
5659 {
5660 Lisp_Object keyword, value;
5661
5662 keyword = XCAR (box);
5663 box = XCDR (box);
5664
5665 if (!CONSP (box))
5666 break;
5667 value = XCAR (box);
5668 box = XCDR (box);
5669
5670 if (EQ (keyword, QCline_width))
5671 {
5672 if (INTEGERP (value) && XINT (value) != 0)
5673 face->box_line_width = XINT (value);
5674 }
5675 else if (EQ (keyword, QCcolor))
5676 {
5677 if (STRINGP (value))
5678 {
5679 face->box_color = load_color (f, face, value,
5680 LFACE_BOX_INDEX);
5681 face->use_box_color_for_shadows_p = 1;
5682 }
5683 }
5684 else if (EQ (keyword, QCstyle))
5685 {
5686 if (EQ (value, Qreleased_button))
5687 face->box = FACE_RAISED_BOX;
5688 else if (EQ (value, Qpressed_button))
5689 face->box = FACE_SUNKEN_BOX;
5690 }
5691 }
5692 }
5693
5694 /* Text underline, overline, strike-through. */
5695
5696 underline = attrs[LFACE_UNDERLINE_INDEX];
5697 if (EQ (underline, Qt))
5698 {
5699 /* Use default color (same as foreground color). */
5700 face->underline_p = 1;
5701 face->underline_type = FACE_UNDER_LINE;
5702 face->underline_defaulted_p = 1;
5703 face->underline_color = 0;
5704 }
5705 else if (STRINGP (underline))
5706 {
5707 /* Use specified color. */
5708 face->underline_p = 1;
5709 face->underline_type = FACE_UNDER_LINE;
5710 face->underline_defaulted_p = 0;
5711 face->underline_color
5712 = load_color (f, face, underline,
5713 LFACE_UNDERLINE_INDEX);
5714 }
5715 else if (NILP (underline))
5716 {
5717 face->underline_p = 0;
5718 face->underline_defaulted_p = 0;
5719 face->underline_color = 0;
5720 }
5721 else if (CONSP (underline))
5722 {
5723 /* `(:color COLOR :style STYLE)'.
5724 STYLE being one of `line' or `wave'. */
5725 face->underline_p = 1;
5726 face->underline_color = 0;
5727 face->underline_defaulted_p = 1;
5728 face->underline_type = FACE_UNDER_LINE;
5729
5730 while (CONSP (underline))
5731 {
5732 Lisp_Object keyword, value;
5733
5734 keyword = XCAR (underline);
5735 underline = XCDR (underline);
5736
5737 if (!CONSP (underline))
5738 break;
5739 value = XCAR (underline);
5740 underline = XCDR (underline);
5741
5742 if (EQ (keyword, QCcolor))
5743 {
5744 if (EQ (value, Qforeground_color))
5745 {
5746 face->underline_defaulted_p = 1;
5747 face->underline_color = 0;
5748 }
5749 else if (STRINGP (value))
5750 {
5751 face->underline_defaulted_p = 0;
5752 face->underline_color = load_color (f, face, value,
5753 LFACE_UNDERLINE_INDEX);
5754 }
5755 }
5756 else if (EQ (keyword, QCstyle))
5757 {
5758 if (EQ (value, Qline))
5759 face->underline_type = FACE_UNDER_LINE;
5760 else if (EQ (value, Qwave))
5761 face->underline_type = FACE_UNDER_WAVE;
5762 }
5763 }
5764 }
5765
5766 overline = attrs[LFACE_OVERLINE_INDEX];
5767 if (STRINGP (overline))
5768 {
5769 face->overline_color
5770 = load_color (f, face, attrs[LFACE_OVERLINE_INDEX],
5771 LFACE_OVERLINE_INDEX);
5772 face->overline_p = 1;
5773 }
5774 else if (EQ (overline, Qt))
5775 {
5776 face->overline_color = face->foreground;
5777 face->overline_color_defaulted_p = 1;
5778 face->overline_p = 1;
5779 }
5780
5781 strike_through = attrs[LFACE_STRIKE_THROUGH_INDEX];
5782 if (STRINGP (strike_through))
5783 {
5784 face->strike_through_color
5785 = load_color (f, face, attrs[LFACE_STRIKE_THROUGH_INDEX],
5786 LFACE_STRIKE_THROUGH_INDEX);
5787 face->strike_through_p = 1;
5788 }
5789 else if (EQ (strike_through, Qt))
5790 {
5791 face->strike_through_color = face->foreground;
5792 face->strike_through_color_defaulted_p = 1;
5793 face->strike_through_p = 1;
5794 }
5795
5796 stipple = attrs[LFACE_STIPPLE_INDEX];
5797 if (!NILP (stipple))
5798 face->stipple = load_pixmap (f, stipple, &face->pixmap_w, &face->pixmap_h);
5799#endif /* HAVE_WINDOW_SYSTEM */
5800
5801 return face;
5802}
5803
5804
5805/* Map a specified color of face FACE on frame F to a tty color index.
5806 IDX is either LFACE_FOREGROUND_INDEX or LFACE_BACKGROUND_INDEX, and
5807 specifies which color to map. Set *DEFAULTED to 1 if mapping to the
5808 default foreground/background colors. */
5809
5810static void
5811map_tty_color (struct frame *f, struct face *face,
5812 enum lface_attribute_index idx, int *defaulted)
5813{
5814 Lisp_Object frame, color, def;
5815 int foreground_p = idx == LFACE_FOREGROUND_INDEX;
5816 unsigned long default_pixel =
5817 foreground_p ? FACE_TTY_DEFAULT_FG_COLOR : FACE_TTY_DEFAULT_BG_COLOR;
5818 unsigned long pixel = default_pixel;
5819#ifdef MSDOS
5820 unsigned long default_other_pixel =
5821 foreground_p ? FACE_TTY_DEFAULT_BG_COLOR : FACE_TTY_DEFAULT_FG_COLOR;
5822#endif
5823
5824 eassert (idx == LFACE_FOREGROUND_INDEX || idx == LFACE_BACKGROUND_INDEX);
5825
5826 XSETFRAME (frame, f);
5827 color = face->lface[idx];
5828
5829 if (STRINGP (color)
5830 && SCHARS (color)
5831 && CONSP (Vtty_defined_color_alist)
5832 && (def = assq_no_quit (color, call1 (Qtty_color_alist, frame)),
5833 CONSP (def)))
5834 {
5835 /* Associations in tty-defined-color-alist are of the form
5836 (NAME INDEX R G B). We need the INDEX part. */
5837 pixel = XINT (XCAR (XCDR (def)));
5838 }
5839
5840 if (pixel == default_pixel && STRINGP (color))
5841 {
5842 pixel = load_color (f, face, color, idx);
5843
5844#ifdef MSDOS
5845 /* If the foreground of the default face is the default color,
5846 use the foreground color defined by the frame. */
5847 if (FRAME_MSDOS_P (f))
5848 {
5849 if (pixel == default_pixel
5850 || pixel == FACE_TTY_DEFAULT_COLOR)
5851 {
5852 if (foreground_p)
5853 pixel = FRAME_FOREGROUND_PIXEL (f);
5854 else
5855 pixel = FRAME_BACKGROUND_PIXEL (f);
5856 face->lface[idx] = tty_color_name (f, pixel);
5857 *defaulted = 1;
5858 }
5859 else if (pixel == default_other_pixel)
5860 {
5861 if (foreground_p)
5862 pixel = FRAME_BACKGROUND_PIXEL (f);
5863 else
5864 pixel = FRAME_FOREGROUND_PIXEL (f);
5865 face->lface[idx] = tty_color_name (f, pixel);
5866 *defaulted = 1;
5867 }
5868 }
5869#endif /* MSDOS */
5870 }
5871
5872 if (foreground_p)
5873 face->foreground = pixel;
5874 else
5875 face->background = pixel;
5876}
5877
5878
5879/* Realize the fully-specified face with attributes ATTRS in face
5880 cache CACHE for ASCII characters. Do it for TTY frame CACHE->f.
5881 Value is a pointer to the newly created realized face. */
5882
5883static struct face *
5884realize_tty_face (struct face_cache *cache,
5885 Lisp_Object attrs[LFACE_VECTOR_SIZE])
5886{
5887 struct face *face;
5888 int weight, slant;
5889 int face_colors_defaulted = 0;
5890 struct frame *f = cache->f;
5891
5892 /* Frame must be a termcap frame. */
5893 eassert (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f));
5894
5895 /* Allocate a new realized face. */
5896 face = make_realized_face (attrs);
5897#if 0
5898 face->font_name = FRAME_MSDOS_P (cache->f) ? "ms-dos" : "tty";
5899#endif
5900
5901 /* Map face attributes to TTY appearances. */
5902 weight = FONT_WEIGHT_NAME_NUMERIC (attrs[LFACE_WEIGHT_INDEX]);
5903 slant = FONT_SLANT_NAME_NUMERIC (attrs[LFACE_SLANT_INDEX]);
5904 if (weight > 100)
5905 face->tty_bold_p = 1;
5906 if (slant != 100)
5907 face->tty_italic_p = 1;
5908 if (!NILP (attrs[LFACE_UNDERLINE_INDEX]))
5909 face->tty_underline_p = 1;
5910 if (!NILP (attrs[LFACE_INVERSE_INDEX]))
5911 face->tty_reverse_p = 1;
5912
5913 /* Map color names to color indices. */
5914 map_tty_color (f, face, LFACE_FOREGROUND_INDEX, &face_colors_defaulted);
5915 map_tty_color (f, face, LFACE_BACKGROUND_INDEX, &face_colors_defaulted);
5916
5917 /* Swap colors if face is inverse-video. If the colors are taken
5918 from the frame colors, they are already inverted, since the
5919 frame-creation function calls x-handle-reverse-video. */
5920 if (face->tty_reverse_p && !face_colors_defaulted)
5921 {
5922 unsigned long tem = face->foreground;
5923 face->foreground = face->background;
5924 face->background = tem;
5925 }
5926
5927 if (tty_suppress_bold_inverse_default_colors_p
5928 && face->tty_bold_p
5929 && face->background == FACE_TTY_DEFAULT_FG_COLOR
5930 && face->foreground == FACE_TTY_DEFAULT_BG_COLOR)
5931 face->tty_bold_p = 0;
5932
5933 return face;
5934}
5935
5936
5937DEFUN ("tty-suppress-bold-inverse-default-colors",
5938 Ftty_suppress_bold_inverse_default_colors,
5939 Stty_suppress_bold_inverse_default_colors, 1, 1, 0,
5940 doc: /* Suppress/allow boldness of faces with inverse default colors.
5941SUPPRESS non-nil means suppress it.
5942This affects bold faces on TTYs whose foreground is the default background
5943color of the display and whose background is the default foreground color.
5944For such faces, the bold face attribute is ignored if this variable
5945is non-nil. */)
5946 (Lisp_Object suppress)
5947{
5948 tty_suppress_bold_inverse_default_colors_p = !NILP (suppress);
5949 ++face_change_count;
5950 return suppress;
5951}
5952
5953
5954\f
5955/***********************************************************************
5956 Computing Faces
5957 ***********************************************************************/
5958
5959/* Return the ID of the face to use to display character CH with face
5960 property PROP on frame F in current_buffer. */
5961
5962int
5963compute_char_face (struct frame *f, int ch, Lisp_Object prop)
5964{
5965 int face_id;
5966
5967 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
5968 ch = 0;
5969
5970 if (NILP (prop))
5971 {
5972 struct face *face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5973 face_id = FACE_FOR_CHAR (f, face, ch, -1, Qnil);
5974 }
5975 else
5976 {
5977 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5978 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5979 memcpy (attrs, default_face->lface, sizeof attrs);
5980 merge_face_ref (f, prop, attrs, 1, 0);
5981 face_id = lookup_face (f, attrs);
5982 }
5983
5984 return face_id;
5985}
5986
5987/* Return the face ID associated with buffer position POS for
5988 displaying ASCII characters. Return in *ENDPTR the position at
5989 which a different face is needed, as far as text properties and
5990 overlays are concerned. W is a window displaying current_buffer.
5991
5992 REGION_BEG, REGION_END delimit the region, so it can be
5993 highlighted.
5994
5995 LIMIT is a position not to scan beyond. That is to limit the time
5996 this function can take.
5997
5998 If MOUSE is non-zero, use the character's mouse-face, not its face.
5999
6000 BASE_FACE_ID, if non-negative, specifies a base face id to use
6001 instead of DEFAULT_FACE_ID.
6002
6003 The face returned is suitable for displaying ASCII characters. */
6004
6005int
6006face_at_buffer_position (struct window *w, ptrdiff_t pos,
6007 ptrdiff_t region_beg, ptrdiff_t region_end,
6008 ptrdiff_t *endptr, ptrdiff_t limit,
6009 int mouse, int base_face_id)
6010{
6011 struct frame *f = XFRAME (w->frame);
6012 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6013 Lisp_Object prop, position;
6014 ptrdiff_t i, noverlays;
6015 Lisp_Object *overlay_vec;
6016 ptrdiff_t endpos;
6017 Lisp_Object propname = mouse ? Qmouse_face : Qface;
6018 Lisp_Object limit1, end;
6019 struct face *default_face;
6020
6021 /* W must display the current buffer. We could write this function
6022 to use the frame and buffer of W, but right now it doesn't. */
6023 /* eassert (XBUFFER (w->buffer) == current_buffer); */
6024
6025 XSETFASTINT (position, pos);
6026
6027 endpos = ZV;
6028 if (pos < region_beg && region_beg < endpos)
6029 endpos = region_beg;
6030
6031 /* Get the `face' or `mouse_face' text property at POS, and
6032 determine the next position at which the property changes. */
6033 prop = Fget_text_property (position, propname, w->buffer);
6034 XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
6035 end = Fnext_single_property_change (position, propname, w->buffer, limit1);
6036 if (INTEGERP (end))
6037 endpos = XINT (end);
6038
6039 /* Look at properties from overlays. */
6040 {
6041 ptrdiff_t next_overlay;
6042
6043 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, &next_overlay, 0);
6044 if (next_overlay < endpos)
6045 endpos = next_overlay;
6046 }
6047
6048 *endptr = endpos;
6049
6050 {
6051 int face_id;
6052
6053 if (base_face_id >= 0)
6054 face_id = base_face_id;
6055 else if (NILP (Vface_remapping_alist))
6056 face_id = DEFAULT_FACE_ID;
6057 else
6058 face_id = lookup_basic_face (f, DEFAULT_FACE_ID);
6059
6060 default_face = FACE_FROM_ID (f, face_id);
6061 }
6062
6063 /* Optimize common cases where we can use the default face. */
6064 if (noverlays == 0
6065 && NILP (prop)
6066 && !(pos >= region_beg && pos < region_end))
6067 return default_face->id;
6068
6069 /* Begin with attributes from the default face. */
6070 memcpy (attrs, default_face->lface, sizeof attrs);
6071
6072 /* Merge in attributes specified via text properties. */
6073 if (!NILP (prop))
6074 merge_face_ref (f, prop, attrs, 1, 0);
6075
6076 /* Now merge the overlay data. */
6077 noverlays = sort_overlays (overlay_vec, noverlays, w);
6078 for (i = 0; i < noverlays; i++)
6079 {
6080 Lisp_Object oend;
6081 ptrdiff_t oendpos;
6082
6083 prop = Foverlay_get (overlay_vec[i], propname);
6084 if (!NILP (prop))
6085 merge_face_ref (f, prop, attrs, 1, 0);
6086
6087 oend = OVERLAY_END (overlay_vec[i]);
6088 oendpos = OVERLAY_POSITION (oend);
6089 if (oendpos < endpos)
6090 endpos = oendpos;
6091 }
6092
6093 /* If in the region, merge in the region face. */
6094 if (pos >= region_beg && pos < region_end)
6095 {
6096 merge_named_face (f, Qregion, attrs, 0);
6097
6098 if (region_end < endpos)
6099 endpos = region_end;
6100 }
6101
6102 *endptr = endpos;
6103
6104 /* Look up a realized face with the given face attributes,
6105 or realize a new one for ASCII characters. */
6106 return lookup_face (f, attrs);
6107}
6108
6109/* Return the face ID at buffer position POS for displaying ASCII
6110 characters associated with overlay strings for overlay OVERLAY.
6111
6112 Like face_at_buffer_position except for OVERLAY. Currently it
6113 simply disregards the `face' properties of all overlays. */
6114
6115int
6116face_for_overlay_string (struct window *w, ptrdiff_t pos,
6117 ptrdiff_t region_beg, ptrdiff_t region_end,
6118 ptrdiff_t *endptr, ptrdiff_t limit,
6119 int mouse, Lisp_Object overlay)
6120{
6121 struct frame *f = XFRAME (w->frame);
6122 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6123 Lisp_Object prop, position;
6124 ptrdiff_t endpos;
6125 Lisp_Object propname = mouse ? Qmouse_face : Qface;
6126 Lisp_Object limit1, end;
6127 struct face *default_face;
6128
6129 /* W must display the current buffer. We could write this function
6130 to use the frame and buffer of W, but right now it doesn't. */
6131 /* eassert (XBUFFER (w->buffer) == current_buffer); */
6132
6133 XSETFASTINT (position, pos);
6134
6135 endpos = ZV;
6136 if (pos < region_beg && region_beg < endpos)
6137 endpos = region_beg;
6138
6139 /* Get the `face' or `mouse_face' text property at POS, and
6140 determine the next position at which the property changes. */
6141 prop = Fget_text_property (position, propname, w->buffer);
6142 XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
6143 end = Fnext_single_property_change (position, propname, w->buffer, limit1);
6144 if (INTEGERP (end))
6145 endpos = XINT (end);
6146
6147 *endptr = endpos;
6148
6149 /* Optimize common case where we can use the default face. */
6150 if (NILP (prop)
6151 && !(pos >= region_beg && pos < region_end)
6152 && NILP (Vface_remapping_alist))
6153 return DEFAULT_FACE_ID;
6154
6155 /* Begin with attributes from the default face. */
6156 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
6157 memcpy (attrs, default_face->lface, sizeof attrs);
6158
6159 /* Merge in attributes specified via text properties. */
6160 if (!NILP (prop))
6161 merge_face_ref (f, prop, attrs, 1, 0);
6162
6163 /* If in the region, merge in the region face. */
6164 if (pos >= region_beg && pos < region_end)
6165 {
6166 merge_named_face (f, Qregion, attrs, 0);
6167
6168 if (region_end < endpos)
6169 endpos = region_end;
6170 }
6171
6172 *endptr = endpos;
6173
6174 /* Look up a realized face with the given face attributes,
6175 or realize a new one for ASCII characters. */
6176 return lookup_face (f, attrs);
6177}
6178
6179
6180/* Compute the face at character position POS in Lisp string STRING on
6181 window W, for ASCII characters.
6182
6183 If STRING is an overlay string, it comes from position BUFPOS in
6184 current_buffer, otherwise BUFPOS is zero to indicate that STRING is
6185 not an overlay string. W must display the current buffer.
6186 REGION_BEG and REGION_END give the start and end positions of the
6187 region; both are -1 if no region is visible.
6188
6189 BASE_FACE_ID is the id of a face to merge with. For strings coming
6190 from overlays or the `display' property it is the face at BUFPOS.
6191
6192 If MOUSE_P is non-zero, use the character's mouse-face, not its face.
6193
6194 Set *ENDPTR to the next position where to check for faces in
6195 STRING; -1 if the face is constant from POS to the end of the
6196 string.
6197
6198 Value is the id of the face to use. The face returned is suitable
6199 for displaying ASCII characters. */
6200
6201int
6202face_at_string_position (struct window *w, Lisp_Object string,
6203 ptrdiff_t pos, ptrdiff_t bufpos,
6204 ptrdiff_t region_beg, ptrdiff_t region_end,
6205 ptrdiff_t *endptr, enum face_id base_face_id,
6206 int mouse_p)
6207{
6208 Lisp_Object prop, position, end, limit;
6209 struct frame *f = XFRAME (WINDOW_FRAME (w));
6210 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6211 struct face *base_face;
6212 int multibyte_p = STRING_MULTIBYTE (string);
6213 Lisp_Object prop_name = mouse_p ? Qmouse_face : Qface;
6214
6215 /* Get the value of the face property at the current position within
6216 STRING. Value is nil if there is no face property. */
6217 XSETFASTINT (position, pos);
6218 prop = Fget_text_property (position, prop_name, string);
6219
6220 /* Get the next position at which to check for faces. Value of end
6221 is nil if face is constant all the way to the end of the string.
6222 Otherwise it is a string position where to check faces next.
6223 Limit is the maximum position up to which to check for property
6224 changes in Fnext_single_property_change. Strings are usually
6225 short, so set the limit to the end of the string. */
6226 XSETFASTINT (limit, SCHARS (string));
6227 end = Fnext_single_property_change (position, prop_name, string, limit);
6228 if (INTEGERP (end))
6229 *endptr = XFASTINT (end);
6230 else
6231 *endptr = -1;
6232
6233 base_face = FACE_FROM_ID (f, base_face_id);
6234 eassert (base_face);
6235
6236 /* Optimize the default case that there is no face property and we
6237 are not in the region. */
6238 if (NILP (prop)
6239 && (base_face_id != DEFAULT_FACE_ID
6240 /* BUFPOS <= 0 means STRING is not an overlay string, so
6241 that the region doesn't have to be taken into account. */
6242 || bufpos <= 0
6243 || bufpos < region_beg
6244 || bufpos >= region_end)
6245 && (multibyte_p
6246 /* We can't realize faces for different charsets differently
6247 if we don't have fonts, so we can stop here if not working
6248 on a window-system frame. */
6249 || !FRAME_WINDOW_P (f)
6250 || FACE_SUITABLE_FOR_ASCII_CHAR_P (base_face, 0)))
6251 return base_face->id;
6252
6253 /* Begin with attributes from the base face. */
6254 memcpy (attrs, base_face->lface, sizeof attrs);
6255
6256 /* Merge in attributes specified via text properties. */
6257 if (!NILP (prop))
6258 merge_face_ref (f, prop, attrs, 1, 0);
6259
6260 /* If in the region, merge in the region face. */
6261 if (bufpos
6262 && bufpos >= region_beg
6263 && bufpos < region_end)
6264 merge_named_face (f, Qregion, attrs, 0);
6265
6266 /* Look up a realized face with the given face attributes,
6267 or realize a new one for ASCII characters. */
6268 return lookup_face (f, attrs);
6269}
6270
6271
6272/* Merge a face into a realized face.
6273
6274 F is frame where faces are (to be) realized.
6275
6276 FACE_NAME is named face to merge.
6277
6278 If FACE_NAME is nil, FACE_ID is face_id of realized face to merge.
6279
6280 If FACE_NAME is t, FACE_ID is lface_id of face to merge.
6281
6282 BASE_FACE_ID is realized face to merge into.
6283
6284 Return new face id.
6285*/
6286
6287int
6288merge_faces (struct frame *f, Lisp_Object face_name, int face_id,
6289 int base_face_id)
6290{
6291 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6292 struct face *base_face;
6293
6294 base_face = FACE_FROM_ID (f, base_face_id);
6295 if (!base_face)
6296 return base_face_id;
6297
6298 if (EQ (face_name, Qt))
6299 {
6300 if (face_id < 0 || face_id >= lface_id_to_name_size)
6301 return base_face_id;
6302 face_name = lface_id_to_name[face_id];
6303 /* When called during make-frame, lookup_derived_face may fail
6304 if the faces are uninitialized. Don't signal an error. */
6305 face_id = lookup_derived_face (f, face_name, base_face_id, 0);
6306 return (face_id >= 0 ? face_id : base_face_id);
6307 }
6308
6309 /* Begin with attributes from the base face. */
6310 memcpy (attrs, base_face->lface, sizeof attrs);
6311
6312 if (!NILP (face_name))
6313 {
6314 if (!merge_named_face (f, face_name, attrs, 0))
6315 return base_face_id;
6316 }
6317 else
6318 {
6319 struct face *face;
6320 if (face_id < 0)
6321 return base_face_id;
6322 face = FACE_FROM_ID (f, face_id);
6323 if (!face)
6324 return base_face_id;
6325 merge_face_vectors (f, face->lface, attrs, 0);
6326 }
6327
6328 /* Look up a realized face with the given face attributes,
6329 or realize a new one for ASCII characters. */
6330 return lookup_face (f, attrs);
6331}
6332
6333\f
6334
6335#ifndef HAVE_X_WINDOWS
6336DEFUN ("x-load-color-file", Fx_load_color_file,
6337 Sx_load_color_file, 1, 1, 0,
6338 doc: /* Create an alist of color entries from an external file.
6339
6340The file should define one named RGB color per line like so:
6341 R G B name
6342where R,G,B are numbers between 0 and 255 and name is an arbitrary string. */)
6343 (Lisp_Object filename)
6344{
6345 FILE *fp;
6346 Lisp_Object cmap = Qnil;
6347 Lisp_Object abspath;
6348
6349 CHECK_STRING (filename);
6350 abspath = Fexpand_file_name (filename, Qnil);
6351
6352 fp = fopen (SSDATA (abspath), "rt");
6353 if (fp)
6354 {
6355 char buf[512];
6356 int red, green, blue;
6357 int num;
6358
6359 block_input ();
6360
6361 while (fgets (buf, sizeof (buf), fp) != NULL) {
6362 if (sscanf (buf, "%u %u %u %n", &red, &green, &blue, &num) == 3)
6363 {
6364 char *name = buf + num;
6365 num = strlen (name) - 1;
6366 if (num >= 0 && name[num] == '\n')
6367 name[num] = 0;
6368 cmap = Fcons (Fcons (build_string (name),
6369#ifdef HAVE_NTGUI
6370 make_number (RGB (red, green, blue))),
6371#else
6372 make_number ((red << 16) | (green << 8) | blue)),
6373#endif
6374 cmap);
6375 }
6376 }
6377 fclose (fp);
6378
6379 unblock_input ();
6380 }
6381
6382 return cmap;
6383}
6384#endif
6385
6386\f
6387/***********************************************************************
6388 Tests
6389 ***********************************************************************/
6390
6391#ifdef GLYPH_DEBUG
6392
6393/* Print the contents of the realized face FACE to stderr. */
6394
6395static void
6396dump_realized_face (struct face *face)
6397{
6398 fprintf (stderr, "ID: %d\n", face->id);
6399#ifdef HAVE_X_WINDOWS
6400 fprintf (stderr, "gc: %ld\n", (long) face->gc);
6401#endif
6402 fprintf (stderr, "foreground: 0x%lx (%s)\n",
6403 face->foreground,
6404 SDATA (face->lface[LFACE_FOREGROUND_INDEX]));
6405 fprintf (stderr, "background: 0x%lx (%s)\n",
6406 face->background,
6407 SDATA (face->lface[LFACE_BACKGROUND_INDEX]));
6408 if (face->font)
6409 fprintf (stderr, "font_name: %s (%s)\n",
6410 SDATA (face->font->props[FONT_NAME_INDEX]),
6411 SDATA (face->lface[LFACE_FAMILY_INDEX]));
6412#ifdef HAVE_X_WINDOWS
6413 fprintf (stderr, "font = %p\n", face->font);
6414#endif
6415 fprintf (stderr, "fontset: %d\n", face->fontset);
6416 fprintf (stderr, "underline: %d (%s)\n",
6417 face->underline_p,
6418 SDATA (Fsymbol_name (face->lface[LFACE_UNDERLINE_INDEX])));
6419 fprintf (stderr, "hash: %d\n", face->hash);
6420}
6421
6422
6423DEFUN ("dump-face", Fdump_face, Sdump_face, 0, 1, 0, doc: /* */)
6424 (Lisp_Object n)
6425{
6426 if (NILP (n))
6427 {
6428 int i;
6429
6430 fprintf (stderr, "font selection order: ");
6431 for (i = 0; i < DIM (font_sort_order); ++i)
6432 fprintf (stderr, "%d ", font_sort_order[i]);
6433 fprintf (stderr, "\n");
6434
6435 fprintf (stderr, "alternative fonts: ");
6436 debug_print (Vface_alternative_font_family_alist);
6437 fprintf (stderr, "\n");
6438
6439 for (i = 0; i < FRAME_FACE_CACHE (SELECTED_FRAME ())->used; ++i)
6440 Fdump_face (make_number (i));
6441 }
6442 else
6443 {
6444 struct face *face;
6445 CHECK_NUMBER (n);
6446 face = FACE_FROM_ID (SELECTED_FRAME (), XINT (n));
6447 if (face == NULL)
6448 error ("Not a valid face");
6449 dump_realized_face (face);
6450 }
6451
6452 return Qnil;
6453}
6454
6455
6456DEFUN ("show-face-resources", Fshow_face_resources, Sshow_face_resources,
6457 0, 0, 0, doc: /* */)
6458 (void)
6459{
6460 fprintf (stderr, "number of colors = %d\n", ncolors_allocated);
6461 fprintf (stderr, "number of pixmaps = %d\n", npixmaps_allocated);
6462 fprintf (stderr, "number of GCs = %d\n", ngcs);
6463 return Qnil;
6464}
6465
6466#endif /* GLYPH_DEBUG */
6467
6468
6469\f
6470/***********************************************************************
6471 Initialization
6472 ***********************************************************************/
6473
6474void
6475syms_of_xfaces (void)
6476{
6477 DEFSYM (Qface, "face");
6478 DEFSYM (Qface_no_inherit, "face-no-inherit");
6479 DEFSYM (Qbitmap_spec_p, "bitmap-spec-p");
6480 DEFSYM (Qframe_set_background_mode, "frame-set-background-mode");
6481
6482 /* Lisp face attribute keywords. */
6483 DEFSYM (QCfamily, ":family");
6484 DEFSYM (QCheight, ":height");
6485 DEFSYM (QCweight, ":weight");
6486 DEFSYM (QCslant, ":slant");
6487 DEFSYM (QCunderline, ":underline");
6488 DEFSYM (QCinverse_video, ":inverse-video");
6489 DEFSYM (QCreverse_video, ":reverse-video");
6490 DEFSYM (QCforeground, ":foreground");
6491 DEFSYM (QCbackground, ":background");
6492 DEFSYM (QCstipple, ":stipple");
6493 DEFSYM (QCwidth, ":width");
6494 DEFSYM (QCfont, ":font");
6495 DEFSYM (QCfontset, ":fontset");
6496 DEFSYM (QCbold, ":bold");
6497 DEFSYM (QCitalic, ":italic");
6498 DEFSYM (QCoverline, ":overline");
6499 DEFSYM (QCstrike_through, ":strike-through");
6500 DEFSYM (QCbox, ":box");
6501 DEFSYM (QCinherit, ":inherit");
6502
6503 /* Symbols used for Lisp face attribute values. */
6504 DEFSYM (QCcolor, ":color");
6505 DEFSYM (QCline_width, ":line-width");
6506 DEFSYM (QCstyle, ":style");
6507 DEFSYM (Qline, "line");
6508 DEFSYM (Qwave, "wave");
6509 DEFSYM (Qreleased_button, "released-button");
6510 DEFSYM (Qpressed_button, "pressed-button");
6511 DEFSYM (Qnormal, "normal");
6512 DEFSYM (Qultra_light, "ultra-light");
6513 DEFSYM (Qextra_light, "extra-light");
6514 DEFSYM (Qlight, "light");
6515 DEFSYM (Qsemi_light, "semi-light");
6516 DEFSYM (Qsemi_bold, "semi-bold");
6517 DEFSYM (Qbold, "bold");
6518 DEFSYM (Qextra_bold, "extra-bold");
6519 DEFSYM (Qultra_bold, "ultra-bold");
6520 DEFSYM (Qoblique, "oblique");
6521 DEFSYM (Qitalic, "italic");
6522 DEFSYM (Qreverse_oblique, "reverse-oblique");
6523 DEFSYM (Qreverse_italic, "reverse-italic");
6524 DEFSYM (Qultra_condensed, "ultra-condensed");
6525 DEFSYM (Qextra_condensed, "extra-condensed");
6526 DEFSYM (Qcondensed, "condensed");
6527 DEFSYM (Qsemi_condensed, "semi-condensed");
6528 DEFSYM (Qsemi_expanded, "semi-expanded");
6529 DEFSYM (Qexpanded, "expanded");
6530 DEFSYM (Qextra_expanded, "extra-expanded");
6531 DEFSYM (Qultra_expanded, "ultra-expanded");
6532 DEFSYM (Qbackground_color, "background-color");
6533 DEFSYM (Qforeground_color, "foreground-color");
6534 DEFSYM (Qunspecified, "unspecified");
6535 DEFSYM (QCignore_defface, ":ignore-defface");
6536
6537 DEFSYM (Qface_alias, "face-alias");
6538 DEFSYM (Qdefault, "default");
6539 DEFSYM (Qtool_bar, "tool-bar");
6540 DEFSYM (Qregion, "region");
6541 DEFSYM (Qfringe, "fringe");
6542 DEFSYM (Qheader_line, "header-line");
6543 DEFSYM (Qscroll_bar, "scroll-bar");
6544 DEFSYM (Qmenu, "menu");
6545 DEFSYM (Qcursor, "cursor");
6546 DEFSYM (Qborder, "border");
6547 DEFSYM (Qmouse, "mouse");
6548 DEFSYM (Qmode_line_inactive, "mode-line-inactive");
6549 DEFSYM (Qvertical_border, "vertical-border");
6550 DEFSYM (Qtty_color_desc, "tty-color-desc");
6551 DEFSYM (Qtty_color_standard_values, "tty-color-standard-values");
6552 DEFSYM (Qtty_color_by_index, "tty-color-by-index");
6553 DEFSYM (Qtty_color_alist, "tty-color-alist");
6554 DEFSYM (Qscalable_fonts_allowed, "scalable-fonts-allowed");
6555
6556 Vparam_value_alist = Fcons (Fcons (Qnil, Qnil), Qnil);
6557 staticpro (&Vparam_value_alist);
6558 Vface_alternative_font_family_alist = Qnil;
6559 staticpro (&Vface_alternative_font_family_alist);
6560 Vface_alternative_font_registry_alist = Qnil;
6561 staticpro (&Vface_alternative_font_registry_alist);
6562
6563 defsubr (&Sinternal_make_lisp_face);
6564 defsubr (&Sinternal_lisp_face_p);
6565 defsubr (&Sinternal_set_lisp_face_attribute);
6566#ifdef HAVE_WINDOW_SYSTEM
6567 defsubr (&Sinternal_set_lisp_face_attribute_from_resource);
6568#endif
6569 defsubr (&Scolor_gray_p);
6570 defsubr (&Scolor_supported_p);
6571#ifndef HAVE_X_WINDOWS
6572 defsubr (&Sx_load_color_file);
6573#endif
6574 defsubr (&Sface_attribute_relative_p);
6575 defsubr (&Smerge_face_attribute);
6576 defsubr (&Sinternal_get_lisp_face_attribute);
6577 defsubr (&Sinternal_lisp_face_attribute_values);
6578 defsubr (&Sinternal_lisp_face_equal_p);
6579 defsubr (&Sinternal_lisp_face_empty_p);
6580 defsubr (&Sinternal_copy_lisp_face);
6581 defsubr (&Sinternal_merge_in_global_face);
6582 defsubr (&Sface_font);
6583 defsubr (&Sframe_face_alist);
6584 defsubr (&Sdisplay_supports_face_attributes_p);
6585 defsubr (&Scolor_distance);
6586 defsubr (&Sinternal_set_font_selection_order);
6587 defsubr (&Sinternal_set_alternative_font_family_alist);
6588 defsubr (&Sinternal_set_alternative_font_registry_alist);
6589 defsubr (&Sface_attributes_as_vector);
6590#ifdef GLYPH_DEBUG
6591 defsubr (&Sdump_face);
6592 defsubr (&Sshow_face_resources);
6593#endif /* GLYPH_DEBUG */
6594 defsubr (&Sclear_face_cache);
6595 defsubr (&Stty_suppress_bold_inverse_default_colors);
6596
6597#if defined DEBUG_X_COLORS && defined HAVE_X_WINDOWS
6598 defsubr (&Sdump_colors);
6599#endif
6600
6601 DEFVAR_LISP ("face-new-frame-defaults", Vface_new_frame_defaults,
6602 doc: /* List of global face definitions (for internal use only.) */);
6603 Vface_new_frame_defaults = Qnil;
6604
6605 DEFVAR_LISP ("face-default-stipple", Vface_default_stipple,
6606 doc: /* Default stipple pattern used on monochrome displays.
6607This stipple pattern is used on monochrome displays
6608instead of shades of gray for a face background color.
6609See `set-face-stipple' for possible values for this variable. */);
6610 Vface_default_stipple = build_pure_c_string ("gray3");
6611
6612 DEFVAR_LISP ("tty-defined-color-alist", Vtty_defined_color_alist,
6613 doc: /* An alist of defined terminal colors and their RGB values.
6614See the docstring of `tty-color-alist' for the details. */);
6615 Vtty_defined_color_alist = Qnil;
6616
6617 DEFVAR_LISP ("scalable-fonts-allowed", Vscalable_fonts_allowed,
6618 doc: /* Allowed scalable fonts.
6619A value of nil means don't allow any scalable fonts.
6620A value of t means allow any scalable font.
6621Otherwise, value must be a list of regular expressions. A font may be
6622scaled if its name matches a regular expression in the list.
6623Note that if value is nil, a scalable font might still be used, if no
6624other font of the appropriate family and registry is available. */);
6625 Vscalable_fonts_allowed = Qnil;
6626
6627 DEFVAR_LISP ("face-ignored-fonts", Vface_ignored_fonts,
6628 doc: /* List of ignored fonts.
6629Each element is a regular expression that matches names of fonts to
6630ignore. */);
6631 Vface_ignored_fonts = Qnil;
6632
6633 DEFVAR_LISP ("face-remapping-alist", Vface_remapping_alist,
6634 doc: /* Alist of face remappings.
6635Each element is of the form:
6636
6637 (FACE . REPLACEMENT),
6638
6639which causes display of the face FACE to use REPLACEMENT instead.
6640REPLACEMENT is a face specification, i.e. one of the following:
6641
6642 (1) a face name
6643 (2) a property list of attribute/value pairs, or
6644 (3) a list in which each element has the form of (1) or (2).
6645
6646List values for REPLACEMENT are merged to form the final face
6647specification, with earlier entries taking precedence, in the same as
6648as in the `face' text property.
6649
6650Face-name remapping cycles are suppressed; recursive references use
6651the underlying face instead of the remapped face. So a remapping of
6652the form:
6653
6654 (FACE EXTRA-FACE... FACE)
6655
6656or:
6657
6658 (FACE (FACE-ATTR VAL ...) FACE)
6659
6660causes EXTRA-FACE... or (FACE-ATTR VAL ...) to be _merged_ with the
6661existing definition of FACE. Note that this isn't necessary for the
6662default face, since every face inherits from the default face.
6663
6664If this variable is made buffer-local, the face remapping takes effect
6665only in that buffer. For instance, the mode my-mode could define a
6666face `my-mode-default', and then in the mode setup function, do:
6667
6668 (set (make-local-variable 'face-remapping-alist)
6669 '((default my-mode-default)))).
6670
6671Because Emacs normally only redraws screen areas when the underlying
6672buffer contents change, you may need to call `redraw-display' after
6673changing this variable for it to take effect. */);
6674 Vface_remapping_alist = Qnil;
6675
6676 DEFVAR_LISP ("face-font-rescale-alist", Vface_font_rescale_alist,
6677 doc: /* Alist of fonts vs the rescaling factors.
6678Each element is a cons (FONT-PATTERN . RESCALE-RATIO), where
6679FONT-PATTERN is a font-spec or a regular expression matching a font name, and
6680RESCALE-RATIO is a floating point number to specify how much larger
6681\(or smaller) font we should use. For instance, if a face requests
6682a font of 10 point, we actually use a font of 10 * RESCALE-RATIO point. */);
6683 Vface_font_rescale_alist = Qnil;
6684
6685#ifdef HAVE_WINDOW_SYSTEM
6686 defsubr (&Sbitmap_spec_p);
6687 defsubr (&Sx_list_fonts);
6688 defsubr (&Sinternal_face_x_get_resource);
6689 defsubr (&Sx_family_fonts);
6690#endif
6691}