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