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