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