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