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