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