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