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