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