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