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