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