(unload_font): Invalidate computed faces.
[emacs.git] / src / xfaces.c
blob254aaf275f1b1b8f09200c8a30355fbdabfe0aa0
1 /* "Face" primitives.
2 Copyright (C) 1993, 1994 Free Software Foundation.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20 /* This is derived from work by Lucid (some parts very loosely so). */
22 #include <sys/types.h>
23 #include <sys/stat.h>
25 #include <config.h>
26 #include "lisp.h"
28 #ifdef HAVE_FACES
30 #ifdef HAVE_X_WINDOWS
31 #include "xterm.h"
32 #endif
33 #ifdef MSDOS
34 #include "dosfns.h"
35 #endif
36 #include "buffer.h"
37 #include "dispextern.h"
38 #include "frame.h"
39 #include "blockinput.h"
40 #include "window.h"
41 #include "intervals.h"
43 #ifdef HAVE_X_WINDOWS
44 /* Compensate for bug in Xos.h on some systems, on which it requires
45 time.h. On some such systems, Xos.h tries to redefine struct
46 timeval and struct timezone if USG is #defined while it is
47 #included. */
48 #ifdef XOS_NEEDS_TIME_H
50 #include <time.h>
51 #undef USG
52 #include <X11/Xos.h>
53 #define USG
54 #define __TIMEVAL__
56 #else
58 #include <X11/Xos.h>
60 #endif
61 #endif /* HAVE_X_WINDOWS */
63 /* An explanation of the face data structures. */
65 /* ========================= Face Data Structures =========================
67 Let FACE-NAME be a symbol naming a face.
69 Let FACE-VECTOR be (assq FACE-NAME (frame-face-alist FRAME))
70 FACE-VECTOR is either nil, or a vector of the form
71 [face NAME ID FONT FOREGROUND BACKGROUND BACKGROUND-PIXMAP UNDERLINE-P]
72 where
73 face is the symbol `face',
74 NAME is the symbol with which this vector is associated (a backpointer),
75 ID is the face ID, an integer used internally by the C code to identify
76 the face,
77 FONT, FOREGROUND, and BACKGROUND are strings naming the fonts and colors
78 to use with the face,
79 BACKGROUND-PIXMAP is the name of an x bitmap filename, which we don't
80 use right now, and
81 UNDERLINE-P is non-nil if the face should be underlined.
82 If any of these elements are nil, that parameter is considered
83 unspecified; parameters from faces specified by lower-priority
84 overlays or text properties, or the parameters of the frame itself,
85 can show through. (lisp/faces.el maintains these lists.)
87 (assq FACE-NAME global-face-data) returns a vector describing the
88 global parameters for that face.
90 Let PARAM-FACE be FRAME->output_data.x->param_faces[Faref (FACE-VECTOR, 2)].
91 PARAM_FACE is a struct face whose members are the Xlib analogues of
92 the parameters in FACE-VECTOR. If an element of FACE-VECTOR is
93 nil, then the corresponding member of PARAM_FACE is FACE_DEFAULT.
94 These faces are called "parameter faces", because they're the ones
95 lisp manipulates to control what gets displayed. Elements 0 and 1
96 of FRAME->output_data.x->param_faces are special - they describe the
97 default and mode line faces. None of the faces in param_faces have
98 GC's. (See src/dispextern.h for the definiton of struct face.
99 lisp/faces.el maintains the isomorphism between face_alist and
100 param_faces.)
102 The functions compute_char_face and compute_glyph_face find and
103 combine the parameter faces associated with overlays and text
104 properties. The resulting faces are called "computed faces"; none
105 of their members are FACE_DEFAULT; they are completely specified.
106 They then call intern_compute_face to search
107 FRAME->output_data.x->computed_faces for a matching face, add one if
108 none is found, and return the index into
109 FRAME->output_data.x->computed_faces. FRAME's glyph matrices use these
110 indices to record the faces of the matrix characters, and the X
111 display hooks consult compute_faces to decide how to display these
112 characters. Elements 0 and 1 of computed_faces always describe the
113 default and mode-line faces.
115 Each computed face belongs to a particular frame.
117 Computed faces have graphics contexts some of the time.
118 intern_face builds a GC for a specified computed face
119 if it doesn't have one already.
120 clear_face_cache clears out the GCs of all computed faces.
121 This is done from time to time so that we don't hold on to
122 lots of GCs that are no longer needed.
124 If a computed face has 0 as its font,
125 it is unused, and can be reused by new_computed_face.
127 Constraints:
129 Symbols naming faces must have associations on all frames; for any
130 FRAME, for all FACE-NAME, if (assq FACE-NAME (frame-face-alist
131 FRAME)) is non-nil, it must be non-nil for all frames.
133 Analogously, indices into param_faces must be valid on all frames;
134 if param_faces[i] is a non-zero face pointer on one frame, then it
135 must be filled in on all frames. Code assumes that face ID's can
136 be used on any frame.
138 Some subtleties:
140 Why do we keep param_faces and computed_faces separate?
141 computed_faces contains an element for every combination of facial
142 parameters we have ever displayed. indices into param_faces have
143 to be valid on all frames. If they were the same array, then that
144 array would grow very large on all frames, because any facial
145 combination displayed on any frame would need to be a valid entry
146 on all frames. */
148 /* Definitions and declarations. */
150 /* The number of face-id's in use (same for all frames). */
151 static int next_face_id;
153 /* The number of the face to use to indicate the region. */
154 static int region_face;
156 /* This is what appears in a slot in a face to signify that the face
157 does not specify that display aspect. */
158 #define FACE_DEFAULT (~0)
160 Lisp_Object Qface, Qmouse_face;
161 Lisp_Object Qpixmap_spec_p;
163 int face_name_id_number ( /* FRAME_PTR, Lisp_Object name */ );
165 struct face *intern_face ( /* FRAME_PTR, struct face * */ );
166 static int new_computed_face ( /* FRAME_PTR, struct face * */ );
167 static int intern_computed_face ( /* FRAME_PTR, struct face * */ );
168 static void ensure_face_ready ( /* FRAME_PTR, int id */ );
169 void recompute_basic_faces ( /* FRAME_PTR f */ );
171 /* Allocating, copying, and comparing struct faces. */
173 /* Allocate a new face */
174 static struct face *
175 allocate_face ()
177 struct face *result = (struct face *) xmalloc (sizeof (struct face));
178 bzero (result, sizeof (struct face));
179 result->font = (XFontStruct *) FACE_DEFAULT;
180 result->foreground = FACE_DEFAULT;
181 result->background = FACE_DEFAULT;
182 result->stipple = FACE_DEFAULT;
183 return result;
186 /* Make a new face that's a copy of an existing one. */
187 static struct face *
188 copy_face (face)
189 struct face *face;
191 struct face *result = allocate_face ();
193 result->font = face->font;
194 result->foreground = face->foreground;
195 result->background = face->background;
196 result->stipple = face->stipple;
197 result->underline = face->underline;
198 result->pixmap_h = face->pixmap_h;
199 result->pixmap_w = face->pixmap_w;
201 return result;
204 static int
205 face_eql (face1, face2)
206 struct face *face1, *face2;
208 return ( face1->font == face2->font
209 && face1->foreground == face2->foreground
210 && face1->background == face2->background
211 && face1->stipple == face2->stipple
212 && face1->underline == face2->underline);
215 /* Managing graphics contexts of faces. */
217 #ifdef HAVE_X_WINDOWS
218 /* Given a computed face, construct its graphics context if necessary. */
220 struct face *
221 intern_face (f, face)
222 struct frame *f;
223 struct face *face;
225 GC gc;
226 XGCValues xgcv;
227 unsigned long mask;
229 if (face->gc)
230 return face;
232 BLOCK_INPUT;
234 if (face->foreground != FACE_DEFAULT)
235 xgcv.foreground = face->foreground;
236 else
237 xgcv.foreground = f->output_data.x->foreground_pixel;
239 if (face->background != FACE_DEFAULT)
240 xgcv.background = face->background;
241 else
242 xgcv.background = f->output_data.x->background_pixel;
244 if (face->font && face->font != (XFontStruct *) FACE_DEFAULT)
245 xgcv.font = face->font->fid;
246 else
247 xgcv.font = f->output_data.x->font->fid;
249 xgcv.graphics_exposures = 0;
251 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
252 if (face->stipple && face->stipple != FACE_DEFAULT)
254 xgcv.fill_style = FillStippled;
255 xgcv.stipple = x_bitmap_pixmap (f, face->stipple);
256 mask |= GCFillStyle | GCStipple;
259 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
260 mask, &xgcv);
262 face->gc = gc;
264 UNBLOCK_INPUT;
266 return face;
269 /* Clear out all graphics contexts for all computed faces
270 except for the default and mode line faces.
271 This should be done from time to time just to avoid
272 keeping too many graphics contexts that are no longer needed. */
274 void
275 clear_face_cache ()
277 Lisp_Object tail, frame;
279 BLOCK_INPUT;
280 FOR_EACH_FRAME (tail, frame)
282 FRAME_PTR f = XFRAME (frame);
283 if (FRAME_X_P (f))
285 int i;
286 Display *dpy = FRAME_X_DISPLAY (f);
288 for (i = 2; i < FRAME_N_COMPUTED_FACES (f); i++)
290 struct face *face = FRAME_COMPUTED_FACES (f) [i];
291 if (face->gc)
292 XFreeGC (dpy, face->gc);
293 face->gc = 0;
298 UNBLOCK_INPUT;
301 /* Allocating, freeing, and duplicating fonts, colors, and pixmaps.
303 These functions operate on param faces only.
304 Computed faces get their fonts, colors and pixmaps
305 by merging param faces. */
307 static XFontStruct *
308 load_font (f, name)
309 struct frame *f;
310 Lisp_Object name;
312 XFontStruct *font;
314 if (NILP (name))
315 return (XFontStruct *) FACE_DEFAULT;
317 CHECK_STRING (name, 0);
318 BLOCK_INPUT;
319 font = XLoadQueryFont (FRAME_X_DISPLAY (f), (char *) XSTRING (name)->data);
320 UNBLOCK_INPUT;
322 if (! font)
323 Fsignal (Qerror, Fcons (build_string ("undefined font"),
324 Fcons (name, Qnil)));
325 return font;
328 static void
329 unload_font (f, font)
330 struct frame *f;
331 XFontStruct *font;
333 int len = FRAME_N_COMPUTED_FACES (f);
334 int i;
336 if (!font || font == ((XFontStruct *) FACE_DEFAULT))
337 return;
339 BLOCK_INPUT;
340 /* Invalidate any computed faces which use this font,
341 and free their GC's if they have any. */
342 for (i = 0; i < len; i++)
344 struct face *face = FRAME_COMPUTED_FACES (f)[i];
345 if (face->font == font)
347 Display *dpy = FRAME_X_DISPLAY (f);
348 if (face->gc)
349 XFreeGC (dpy, face->gc);
350 face->gc = 0;
351 face->font = 0;
355 XFreeFont (FRAME_X_DISPLAY (f), font);
356 UNBLOCK_INPUT;
359 static unsigned long
360 load_color (f, name)
361 struct frame *f;
362 Lisp_Object name;
364 XColor color;
365 int result;
367 if (NILP (name))
368 return FACE_DEFAULT;
370 CHECK_STRING (name, 0);
371 /* if the colormap is full, defined_color will return a best match
372 to the values in an an existing cell. */
373 result = defined_color(f, (char *) XSTRING (name)->data, &color, 1);
374 if (! result)
375 Fsignal (Qerror, Fcons (build_string ("undefined color"),
376 Fcons (name, Qnil)));
377 return (unsigned long) color.pixel;
380 static void
381 unload_color (f, pixel)
382 struct frame *f;
383 unsigned long pixel;
385 Colormap cmap;
386 Display *dpy = FRAME_X_DISPLAY (f);
387 int class = FRAME_X_DISPLAY_INFO (f)->visual->class;
389 if (pixel == FACE_DEFAULT
390 || pixel == BLACK_PIX_DEFAULT (f)
391 || pixel == WHITE_PIX_DEFAULT (f))
392 return;
393 cmap = DefaultColormapOfScreen (DefaultScreenOfDisplay (dpy));
395 /* If display has an immutable color map, freeing colors is not
396 necessary and some servers don't allow it. So don't do it. */
397 if (! (class == StaticColor || class == StaticGray || class == TrueColor))
399 int len = FRAME_N_COMPUTED_FACES (f);
400 int i;
402 BLOCK_INPUT;
403 /* Invalidate any computed faces which use this color,
404 and free their GC's if they have any. */
405 for (i = 0; i < len; i++)
407 struct face *face = FRAME_COMPUTED_FACES (f)[i];
408 if (face->foreground == pixel
409 || face->background == pixel)
411 Display *dpy = FRAME_X_DISPLAY (f);
412 if (face->gc)
413 XFreeGC (dpy, face->gc);
414 face->gc = 0;
415 face->font = 0;
419 XFreeColors (dpy, cmap, &pixel, 1, (unsigned long)0);
420 UNBLOCK_INPUT;
424 DEFUN ("pixmap-spec-p", Fpixmap_spec_p, Spixmap_spec_p, 1, 1, 0,
425 "Return t if ARG is a valid pixmap specification.")
426 (arg)
427 Lisp_Object arg;
429 Lisp_Object height, width;
431 return ((STRINGP (arg)
432 || (CONSP (arg)
433 && CONSP (XCONS (arg)->cdr)
434 && CONSP (XCONS (XCONS (arg)->cdr)->cdr)
435 && NILP (XCONS (XCONS (XCONS (arg)->cdr)->cdr)->cdr)
436 && (width = XCONS (arg)->car, INTEGERP (width))
437 && (height = XCONS (XCONS (arg)->cdr)->car, INTEGERP (height))
438 && STRINGP (XCONS (XCONS (XCONS (arg)->cdr)->cdr)->car)
439 && XINT (width) > 0
440 && XINT (height) > 0
441 /* The string must have enough bits for width * height. */
442 && ((XSTRING (XCONS (XCONS (XCONS (arg)->cdr)->cdr)->car)->size
443 * (BITS_PER_INT / sizeof (int)))
444 >= XFASTINT (width) * XFASTINT (height))))
445 ? Qt : Qnil);
448 /* Load a bitmap according to NAME (which is either a file name
449 or a pixmap spec). Return the bitmap_id (see xfns.c)
450 or get an error if NAME is invalid.
452 Store the bitmap width in *W_PTR and height in *H_PTR. */
454 static long
455 load_pixmap (f, name, w_ptr, h_ptr)
456 FRAME_PTR f;
457 Lisp_Object name;
458 unsigned int *w_ptr, *h_ptr;
460 int bitmap_id;
461 Lisp_Object tem;
463 if (NILP (name))
464 return FACE_DEFAULT;
466 tem = Fpixmap_spec_p (name);
467 if (NILP (tem))
468 wrong_type_argument (Qpixmap_spec_p, name);
470 BLOCK_INPUT;
472 if (CONSP (name))
474 /* Decode a bitmap spec into a bitmap. */
476 int h, w;
477 Lisp_Object bits;
479 w = XINT (Fcar (name));
480 h = XINT (Fcar (Fcdr (name)));
481 bits = Fcar (Fcdr (Fcdr (name)));
483 bitmap_id = x_create_bitmap_from_data (f, XSTRING (bits)->data,
484 w, h);
486 else
488 /* It must be a string -- a file name. */
489 bitmap_id = x_create_bitmap_from_file (f, name);
491 UNBLOCK_INPUT;
493 if (bitmap_id < 0)
494 Fsignal (Qerror, Fcons (build_string ("invalid or undefined bitmap"),
495 Fcons (name, Qnil)));
497 *w_ptr = x_bitmap_width (f, bitmap_id);
498 *h_ptr = x_bitmap_height (f, bitmap_id);
500 return bitmap_id;
503 #else /* !HAVE_X_WINDOWS */
505 /* Stubs for MSDOS when not under X. */
507 struct face *
508 intern_face (f, face)
509 struct frame *f;
510 struct face *face;
512 return face;
515 void
516 clear_face_cache ()
518 /* No action. */
521 #ifdef MSDOS
522 unsigned long
523 load_color (f, name)
524 FRAME_PTR f;
525 Lisp_Object name;
527 Lisp_Object result;
529 if (NILP (name))
530 return FACE_DEFAULT;
532 CHECK_STRING (name, 0);
533 result = call1 (Qmsdos_color_translate, name);
534 if (INTEGERP (result))
535 return XINT (result);
536 else
537 Fsignal (Qerror, Fcons (build_string ("undefined color"),
538 Fcons (name, Qnil)));
540 #endif
541 #endif /* !HAVE_X_WINDOWS */
544 /* Managing parameter face arrays for frames. */
546 void
547 init_frame_faces (f)
548 FRAME_PTR f;
550 ensure_face_ready (f, 0);
551 ensure_face_ready (f, 1);
553 FRAME_N_COMPUTED_FACES (f) = 0;
554 FRAME_SIZE_COMPUTED_FACES (f) = 0;
556 new_computed_face (f, FRAME_PARAM_FACES (f)[0]);
557 new_computed_face (f, FRAME_PARAM_FACES (f)[1]);
558 recompute_basic_faces (f);
560 #ifdef MULTI_FRAME
561 /* Find another X frame. */
563 Lisp_Object tail, frame, result;
565 result = Qnil;
566 FOR_EACH_FRAME (tail, frame)
567 if (FRAME_X_P (XFRAME (frame))
568 && XFRAME (frame) != f)
570 result = frame;
571 break;
574 /* If we didn't find any X frames other than f, then we don't need
575 any faces other than 0 and 1, so we're okay. Otherwise, make
576 sure that all faces valid on the selected frame are also valid
577 on this new frame. */
578 if (FRAMEP (result))
580 int i;
581 int n_faces = FRAME_N_PARAM_FACES (XFRAME (result));
582 struct face **faces = FRAME_PARAM_FACES (XFRAME (result));
584 for (i = 2; i < n_faces; i++)
585 if (faces[i])
586 ensure_face_ready (f, i);
589 #endif /* MULTI_FRAME */
593 /* Called from Fdelete_frame. */
595 void
596 free_frame_faces (f)
597 struct frame *f;
599 Display *dpy = FRAME_X_DISPLAY (f);
600 int i;
602 BLOCK_INPUT;
604 for (i = 0; i < FRAME_N_PARAM_FACES (f); i++)
606 struct face *face = FRAME_PARAM_FACES (f) [i];
607 if (face)
609 unload_font (f, face->font);
610 unload_color (f, face->foreground);
611 unload_color (f, face->background);
612 x_destroy_bitmap (f, face->stipple);
613 xfree (face);
616 xfree (FRAME_PARAM_FACES (f));
617 FRAME_PARAM_FACES (f) = 0;
618 FRAME_N_PARAM_FACES (f) = 0;
620 /* All faces in FRAME_COMPUTED_FACES use resources copied from
621 FRAME_PARAM_FACES; we can free them without fuss.
622 But we do free the GCs and the face objects themselves. */
623 for (i = 0; i < FRAME_N_COMPUTED_FACES (f); i++)
625 struct face *face = FRAME_COMPUTED_FACES (f) [i];
626 if (face)
628 if (face->gc)
629 XFreeGC (dpy, face->gc);
630 xfree (face);
633 xfree (FRAME_COMPUTED_FACES (f));
634 FRAME_COMPUTED_FACES (f) = 0;
635 FRAME_N_COMPUTED_FACES (f) = 0;
637 UNBLOCK_INPUT;
640 /* Interning faces in a frame's face array. */
642 static int
643 new_computed_face (f, new_face)
644 struct frame *f;
645 struct face *new_face;
647 int len = FRAME_N_COMPUTED_FACES (f);
648 int i;
650 /* Search for an unused computed face in the middle of the table. */
651 for (i = 0; i < len; i++)
653 struct face *face = FRAME_COMPUTED_FACES (f)[i];
654 if (face->font == 0)
656 FRAME_COMPUTED_FACES (f)[i] = copy_face (new_face);
657 return i;
661 if (i >= FRAME_SIZE_COMPUTED_FACES (f))
663 int new_size = i + 32;
665 FRAME_COMPUTED_FACES (f)
666 = (struct face **) (FRAME_SIZE_COMPUTED_FACES (f) == 0
667 ? xmalloc (new_size * sizeof (struct face *))
668 : xrealloc (FRAME_COMPUTED_FACES (f),
669 new_size * sizeof (struct face *)));
670 FRAME_SIZE_COMPUTED_FACES (f) = new_size;
673 i = FRAME_N_COMPUTED_FACES (f)++;
674 FRAME_COMPUTED_FACES (f)[i] = copy_face (new_face);
675 return i;
679 /* Find a match for NEW_FACE in a FRAME's computed face array, and add
680 it if we don't find one. */
681 static int
682 intern_computed_face (f, new_face)
683 struct frame *f;
684 struct face *new_face;
686 int len = FRAME_N_COMPUTED_FACES (f);
687 int i;
689 /* Search for a computed face already on F equivalent to FACE. */
690 for (i = 0; i < len; i++)
692 if (! FRAME_COMPUTED_FACES (f)[i])
693 abort ();
694 if (face_eql (new_face, FRAME_COMPUTED_FACES (f)[i]))
695 return i;
698 /* We didn't find one; add a new one. */
699 return new_computed_face (f, new_face);
702 /* Make parameter face id ID valid on frame F. */
704 static void
705 ensure_face_ready (f, id)
706 struct frame *f;
707 int id;
709 if (FRAME_N_PARAM_FACES (f) <= id)
711 int n = id + 10;
712 int i;
713 if (!FRAME_N_PARAM_FACES (f))
714 FRAME_PARAM_FACES (f)
715 = (struct face **) xmalloc (sizeof (struct face *) * n);
716 else
717 FRAME_PARAM_FACES (f)
718 = (struct face **) xrealloc (FRAME_PARAM_FACES (f),
719 sizeof (struct face *) * n);
721 bzero (FRAME_PARAM_FACES (f) + FRAME_N_PARAM_FACES (f),
722 (n - FRAME_N_PARAM_FACES (f)) * sizeof (struct face *));
723 FRAME_N_PARAM_FACES (f) = n;
726 if (FRAME_PARAM_FACES (f) [id] == 0)
727 FRAME_PARAM_FACES (f) [id] = allocate_face ();
730 #ifdef HAVE_X_WINDOWS
731 /* Return non-zero if FONT1 and FONT2 have the same width.
732 We do not check the height, because we can now deal with
733 different heights.
734 We assume that they're both character-cell fonts. */
737 same_size_fonts (font1, font2)
738 XFontStruct *font1, *font2;
740 XCharStruct *bounds1 = &font1->min_bounds;
741 XCharStruct *bounds2 = &font2->min_bounds;
743 return (bounds1->width == bounds2->width);
746 /* Update the line_height of frame F according to the biggest font in
747 any face. Return nonzero if if line_height changes. */
750 frame_update_line_height (f)
751 FRAME_PTR f;
753 int i;
754 int biggest = FONT_HEIGHT (f->output_data.x->font);
756 for (i = 0; i < f->output_data.x->n_param_faces; i++)
757 if (f->output_data.x->param_faces[i] != 0
758 && f->output_data.x->param_faces[i]->font != (XFontStruct *) FACE_DEFAULT)
760 int height = FONT_HEIGHT (f->output_data.x->param_faces[i]->font);
761 if (height > biggest)
762 biggest = height;
765 if (biggest == f->output_data.x->line_height)
766 return 0;
768 f->output_data.x->line_height = biggest;
769 return 1;
771 #endif /* not HAVE_X_WINDOWS */
773 /* Modify face TO by copying from FROM all properties which have
774 nondefault settings. */
776 static void
777 merge_faces (from, to)
778 struct face *from, *to;
780 /* Only merge the font if it's the same width as the base font.
781 Otherwise ignore it, since we can't handle it properly. */
782 if (from->font != (XFontStruct *) FACE_DEFAULT
783 && same_size_fonts (from->font, to->font))
784 to->font = from->font;
785 if (from->foreground != FACE_DEFAULT)
786 to->foreground = from->foreground;
787 if (from->background != FACE_DEFAULT)
788 to->background = from->background;
789 if (from->stipple != FACE_DEFAULT)
791 to->stipple = from->stipple;
792 to->pixmap_h = from->pixmap_h;
793 to->pixmap_w = from->pixmap_w;
795 if (from->underline)
796 to->underline = from->underline;
799 /* Set up the basic set of facial parameters, based on the frame's
800 data; all faces are deltas applied to this. */
802 static void
803 compute_base_face (f, face)
804 FRAME_PTR f;
805 struct face *face;
807 face->gc = 0;
808 face->foreground = FRAME_FOREGROUND_PIXEL (f);
809 face->background = FRAME_BACKGROUND_PIXEL (f);
810 face->font = FRAME_FONT (f);
811 face->stipple = 0;
812 face->underline = 0;
815 /* Return the face ID to use to display a special glyph which selects
816 FACE_CODE as the face ID, assuming that ordinarily the face would
817 be CURRENT_FACE. F is the frame. */
820 compute_glyph_face (f, face_code, current_face)
821 struct frame *f;
822 int face_code, current_face;
824 struct face face;
826 face = *FRAME_COMPUTED_FACES (f)[current_face];
828 if (face_code >= 0 && face_code < FRAME_N_PARAM_FACES (f)
829 && FRAME_PARAM_FACES (f) [face_code] != 0)
830 merge_faces (FRAME_PARAM_FACES (f) [face_code], &face);
832 return intern_computed_face (f, &face);
835 /* Return the face ID to use to display a special glyph which selects
836 FACE_CODE as the face ID, assuming that ordinarily the face would
837 be CURRENT_FACE. F is the frame. */
840 compute_glyph_face_1 (f, face_name, current_face)
841 struct frame *f;
842 Lisp_Object face_name;
843 int current_face;
845 struct face face;
847 face = *FRAME_COMPUTED_FACES (f)[current_face];
849 if (!NILP (face_name))
851 int facecode = face_name_id_number (f, face_name);
852 if (facecode >= 0 && facecode < FRAME_N_PARAM_FACES (f)
853 && FRAME_PARAM_FACES (f) [facecode] != 0)
854 merge_faces (FRAME_PARAM_FACES (f) [facecode], &face);
857 return intern_computed_face (f, &face);
860 /* Return the face ID associated with a buffer position POS.
861 Store into *ENDPTR the position at which a different face is needed.
862 This does not take account of glyphs that specify their own face codes.
863 F is the frame in use for display, and W is a window displaying
864 the current buffer.
866 REGION_BEG, REGION_END delimit the region, so it can be highlighted.
868 LIMIT is a position not to scan beyond. That is to limit
869 the time this function can take.
871 If MOUSE is nonzero, use the character's mouse-face, not its face. */
874 compute_char_face (f, w, pos, region_beg, region_end, endptr, limit, mouse)
875 struct frame *f;
876 struct window *w;
877 int pos;
878 int region_beg, region_end;
879 int *endptr;
880 int limit;
881 int mouse;
883 struct face face;
884 Lisp_Object prop, position;
885 int i, j, noverlays;
886 int facecode;
887 Lisp_Object *overlay_vec;
888 Lisp_Object frame;
889 int endpos;
890 Lisp_Object propname;
892 /* W must display the current buffer. We could write this function
893 to use the frame and buffer of W, but right now it doesn't. */
894 if (XBUFFER (w->buffer) != current_buffer)
895 abort ();
897 XSETFRAME (frame, f);
899 endpos = ZV;
900 if (pos < region_beg && region_beg < endpos)
901 endpos = region_beg;
903 XSETFASTINT (position, pos);
905 if (mouse)
906 propname = Qmouse_face;
907 else
908 propname = Qface;
910 prop = Fget_text_property (position, propname, w->buffer);
913 Lisp_Object limit1, end;
915 XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
916 end = Fnext_single_property_change (position, propname, w->buffer, limit1);
917 if (INTEGERP (end))
918 endpos = XINT (end);
922 int next_overlay;
923 int len;
925 /* First try with room for 40 overlays. */
926 len = 40;
927 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
929 noverlays = overlays_at (pos, 0, &overlay_vec, &len,
930 &next_overlay, (int *) 0);
932 /* If there are more than 40,
933 make enough space for all, and try again. */
934 if (noverlays > len)
936 len = noverlays;
937 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
938 noverlays = overlays_at (pos, 0, &overlay_vec, &len,
939 &next_overlay, (int *) 0);
942 if (next_overlay < endpos)
943 endpos = next_overlay;
946 *endptr = endpos;
948 /* Optimize the default case. */
949 if (noverlays == 0 && NILP (prop)
950 && !(pos >= region_beg && pos < region_end))
951 return 0;
953 compute_base_face (f, &face);
955 if (CONSP (prop))
957 /* We have a list of faces, merge them in reverse order */
958 Lisp_Object length;
959 int len;
960 Lisp_Object *faces;
962 length = Fsafe_length (prop);
963 len = XFASTINT (length);
965 /* Put them into an array */
966 faces = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
967 for (j = 0; j < len; j++)
969 faces[j] = Fcar (prop);
970 prop = Fcdr (prop);
972 /* So that we can merge them in the reverse order */
973 for (j = len - 1; j >= 0; j--)
975 facecode = face_name_id_number (f, faces[j]);
976 if (facecode >= 0 && facecode < FRAME_N_PARAM_FACES (f)
977 && FRAME_PARAM_FACES (f) [facecode] != 0)
978 merge_faces (FRAME_PARAM_FACES (f) [facecode], &face);
981 else if (!NILP (prop))
983 facecode = face_name_id_number (f, prop);
984 if (facecode >= 0 && facecode < FRAME_N_PARAM_FACES (f)
985 && FRAME_PARAM_FACES (f) [facecode] != 0)
986 merge_faces (FRAME_PARAM_FACES (f) [facecode], &face);
989 noverlays = sort_overlays (overlay_vec, noverlays, w);
991 /* Now merge the overlay data in that order. */
992 for (i = 0; i < noverlays; i++)
994 prop = Foverlay_get (overlay_vec[i], propname);
995 if (CONSP (prop))
997 /* We have a list of faces, merge them in reverse order */
998 Lisp_Object length;
999 int len;
1000 Lisp_Object *faces;
1002 length = Fsafe_length (prop);
1003 len = XFASTINT (length);
1005 /* Put them into an array */
1006 faces = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
1007 for (j = 0; j < len; j++)
1009 faces[j] = Fcar (prop);
1010 prop = Fcdr (prop);
1012 /* So that we can merge them in the reverse order */
1013 for (j = len - 1; j >= 0; j--)
1015 facecode = face_name_id_number (f, faces[j]);
1016 if (facecode >= 0 && facecode < FRAME_N_PARAM_FACES (f)
1017 && FRAME_PARAM_FACES (f) [facecode] != 0)
1018 merge_faces (FRAME_PARAM_FACES (f) [facecode], &face);
1021 else if (!NILP (prop))
1023 Lisp_Object oend;
1024 int oendpos;
1026 facecode = face_name_id_number (f, prop);
1027 if (facecode >= 0 && facecode < FRAME_N_PARAM_FACES (f)
1028 && FRAME_PARAM_FACES (f) [facecode] != 0)
1029 merge_faces (FRAME_PARAM_FACES (f)[facecode], &face);
1031 oend = OVERLAY_END (overlay_vec[i]);
1032 oendpos = OVERLAY_POSITION (oend);
1033 if (oendpos < endpos)
1034 endpos = oendpos;
1038 if (pos >= region_beg && pos < region_end)
1040 if (region_end < endpos)
1041 endpos = region_end;
1042 if (region_face >= 0 && region_face < next_face_id)
1043 merge_faces (FRAME_PARAM_FACES (f)[region_face], &face);
1046 *endptr = endpos;
1048 return intern_computed_face (f, &face);
1051 /* Recompute the GC's for the default and modeline faces.
1052 We call this after changing frame parameters on which those GC's
1053 depend. */
1055 void
1056 recompute_basic_faces (f)
1057 FRAME_PTR f;
1059 /* If the frame's faces haven't been initialized yet, don't worry about
1060 this stuff. */
1061 if (FRAME_N_PARAM_FACES (f) < 2)
1062 return;
1064 BLOCK_INPUT;
1066 if (FRAME_DEFAULT_FACE (f)->gc)
1067 XFreeGC (FRAME_X_DISPLAY (f), FRAME_DEFAULT_FACE (f)->gc);
1068 if (FRAME_MODE_LINE_FACE (f)->gc)
1069 XFreeGC (FRAME_X_DISPLAY (f), FRAME_MODE_LINE_FACE (f)->gc);
1071 compute_base_face (f, FRAME_DEFAULT_FACE (f));
1072 compute_base_face (f, FRAME_MODE_LINE_FACE (f));
1074 merge_faces (FRAME_DEFAULT_PARAM_FACE (f), FRAME_DEFAULT_FACE (f));
1075 merge_faces (FRAME_MODE_LINE_PARAM_FACE (f), FRAME_MODE_LINE_FACE (f));
1077 intern_face (f, FRAME_DEFAULT_FACE (f));
1078 intern_face (f, FRAME_MODE_LINE_FACE (f));
1080 UNBLOCK_INPUT;
1085 /* Lisp interface. */
1087 DEFUN ("frame-face-alist", Fframe_face_alist, Sframe_face_alist, 1, 1, 0,
1089 (frame)
1090 Lisp_Object frame;
1092 CHECK_FRAME (frame, 0);
1093 return XFRAME (frame)->face_alist;
1096 DEFUN ("set-frame-face-alist", Fset_frame_face_alist, Sset_frame_face_alist,
1097 2, 2, 0, "")
1098 (frame, value)
1099 Lisp_Object frame, value;
1101 CHECK_FRAME (frame, 0);
1102 XFRAME (frame)->face_alist = value;
1103 return value;
1107 DEFUN ("make-face-internal", Fmake_face_internal, Smake_face_internal, 1, 1, 0,
1108 "Create face number FACE-ID on all frames.")
1109 (face_id)
1110 Lisp_Object face_id;
1112 Lisp_Object rest, frame;
1113 int id = XINT (face_id);
1115 CHECK_NUMBER (face_id, 0);
1116 if (id < 0 || id >= next_face_id)
1117 error ("Face id out of range");
1119 FOR_EACH_FRAME (rest, frame)
1121 if (FRAME_X_P (XFRAME (frame)))
1122 ensure_face_ready (XFRAME (frame), id);
1124 return Qnil;
1128 DEFUN ("set-face-attribute-internal", Fset_face_attribute_internal,
1129 Sset_face_attribute_internal, 4, 4, 0, "")
1130 (face_id, attr_name, attr_value, frame)
1131 Lisp_Object face_id, attr_name, attr_value, frame;
1133 struct face *face;
1134 struct frame *f;
1135 int magic_p;
1136 int id;
1137 int garbaged = 0;
1139 CHECK_FRAME (frame, 0);
1140 CHECK_NUMBER (face_id, 0);
1141 CHECK_SYMBOL (attr_name, 0);
1143 f = XFRAME (frame);
1144 id = XINT (face_id);
1145 if (id < 0 || id >= next_face_id)
1146 error ("Face id out of range");
1148 if (! FRAME_X_P (f))
1149 return Qnil;
1151 ensure_face_ready (f, id);
1152 face = FRAME_PARAM_FACES (f) [XFASTINT (face_id)];
1154 if (EQ (attr_name, intern ("font")))
1156 #if defined (MSDOS) && !defined (HAVE_X_WINDOWS)
1157 face->font = 0; /* The one and only font. */
1158 #else
1159 XFontStruct *font = load_font (f, attr_value);
1160 if (face->font != f->output_data.x->font)
1161 unload_font (f, face->font);
1162 face->font = font;
1163 if (frame_update_line_height (f))
1164 x_set_window_size (f, 0, f->width, f->height);
1165 /* Must clear cache, since it might contain the font
1166 we just got rid of. */
1167 garbaged = 1;
1168 #endif
1170 else if (EQ (attr_name, intern ("foreground")))
1172 unsigned long new_color = load_color (f, attr_value);
1173 unload_color (f, face->foreground);
1174 face->foreground = new_color;
1175 garbaged = 1;
1177 else if (EQ (attr_name, intern ("background")))
1179 unsigned long new_color = load_color (f, attr_value);
1180 unload_color (f, face->background);
1181 #if defined (MSDOS) && !defined (HAVE_X_WINDOWS)
1182 new_color &= ~8; /* Bright would give blinking characters. */
1183 #endif
1184 face->background = new_color;
1185 garbaged = 1;
1187 else if (EQ (attr_name, intern ("background-pixmap")))
1189 unsigned int w, h;
1190 unsigned long new_pixmap = load_pixmap (f, attr_value, &w, &h);
1191 x_destroy_bitmap (f, face->stipple);
1192 face->stipple = new_pixmap;
1193 face->pixmap_w = w;
1194 face->pixmap_h = h;
1195 garbaged = 1;
1197 else if (EQ (attr_name, intern ("underline")))
1199 int new = !NILP (attr_value);
1200 face->underline = new;
1202 else
1203 error ("unknown face attribute");
1205 if (id == 0 || id == 1)
1206 recompute_basic_faces (f);
1208 /* We must redraw the frame whenever any face font or color changes,
1209 because it's possible that a merged (display) face
1210 contains the font or color we just replaced.
1211 And we must inhibit any Expose events until the redraw is done,
1212 since they would try to use the invalid display faces. */
1213 if (garbaged)
1214 SET_FRAME_GARBAGED (f);
1216 return Qnil;
1219 DEFUN ("internal-next-face-id", Finternal_next_face_id, Sinternal_next_face_id,
1220 0, 0, 0, "")
1223 return make_number (next_face_id++);
1226 /* Return the face id for name NAME on frame FRAME.
1227 (It should be the same for all frames,
1228 but it's as easy to use the "right" frame to look it up
1229 as to use any other one.) */
1232 face_name_id_number (f, name)
1233 FRAME_PTR f;
1234 Lisp_Object name;
1236 Lisp_Object tem;
1238 tem = Fcdr (assq_no_quit (name, f->face_alist));
1239 if (NILP (tem))
1240 return 0;
1241 CHECK_VECTOR (tem, 0);
1242 tem = XVECTOR (tem)->contents[2];
1243 CHECK_NUMBER (tem, 0);
1244 return XINT (tem);
1247 /* Emacs initialization. */
1249 void
1250 syms_of_xfaces ()
1252 Qface = intern ("face");
1253 staticpro (&Qface);
1254 Qmouse_face = intern ("mouse-face");
1255 staticpro (&Qmouse_face);
1256 Qpixmap_spec_p = intern ("pixmap-spec-p");
1257 staticpro (&Qpixmap_spec_p);
1259 DEFVAR_INT ("region-face", &region_face,
1260 "Face number to use to highlight the region\n\
1261 The region is highlighted with this face\n\
1262 when Transient Mark mode is enabled and the mark is active.");
1264 #ifdef HAVE_X_WINDOWS
1265 defsubr (&Spixmap_spec_p);
1266 #endif
1267 defsubr (&Sframe_face_alist);
1268 defsubr (&Sset_frame_face_alist);
1269 defsubr (&Smake_face_internal);
1270 defsubr (&Sset_face_attribute_internal);
1271 defsubr (&Sinternal_next_face_id);
1274 #endif /* HAVE_FACES */