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