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