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