(byte-compile-insert-header): If emacs-version
[bpt/emacs.git] / src / xfaces.c
1 /* "Face" primitives.
2 Copyright (C) 1993, 1994 Free Software Foundation.
3
4 This file is part of GNU Emacs.
5
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.
10
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.
15
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, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21 /* This is derived from work by Lucid (some parts very loosely so). */
22
23 #include <sys/types.h>
24 #include <sys/stat.h>
25
26 #include <config.h>
27 #include "lisp.h"
28
29 #ifdef HAVE_FACES
30
31 #ifdef HAVE_X_WINDOWS
32 #include "xterm.h"
33 #endif
34 #ifdef MSDOS
35 #include "dosfns.h"
36 #endif
37 #include "buffer.h"
38 #include "dispextern.h"
39 #include "frame.h"
40 #include "blockinput.h"
41 #include "window.h"
42 #include "intervals.h"
43
44 #ifdef HAVE_X_WINDOWS
45 /* Compensate for bug in Xos.h on some systems, on which it requires
46 time.h. On some such systems, Xos.h tries to redefine struct
47 timeval and struct timezone if USG is #defined while it is
48 #included. */
49 #ifdef XOS_NEEDS_TIME_H
50
51 #include <time.h>
52 #undef USG
53 #include <X11/Xos.h>
54 #define USG
55 #define __TIMEVAL__
56
57 #else
58
59 #include <X11/Xos.h>
60
61 #endif
62 #endif /* HAVE_X_WINDOWS */
63 \f
64 /* An explanation of the face data structures. */
65
66 /* ========================= Face Data Structures =========================
67
68 Let FACE-NAME be a symbol naming a face.
69
70 Let FACE-VECTOR be (assq FACE-NAME (frame-face-alist FRAME))
71 FACE-VECTOR is either nil, or a vector of the form
72 [face NAME ID FONT FOREGROUND BACKGROUND BACKGROUND-PIXMAP UNDERLINE-P]
73 where
74 face is the symbol `face',
75 NAME is the symbol with which this vector is associated (a backpointer),
76 ID is the face ID, an integer used internally by the C code to identify
77 the face,
78 FONT, FOREGROUND, and BACKGROUND are strings naming the fonts and colors
79 to use with the face,
80 BACKGROUND-PIXMAP is the name of an x bitmap filename, which we don't
81 use right now, and
82 UNDERLINE-P is non-nil if the face should be underlined.
83 If any of these elements are nil, that parameter is considered
84 unspecified; parameters from faces specified by lower-priority
85 overlays or text properties, or the parameters of the frame itself,
86 can show through. (lisp/faces.el maintains these lists.)
87
88 (assq FACE-NAME global-face-data) returns a vector describing the
89 global parameters for that face.
90
91 Let PARAM-FACE be FRAME->output_data.x->param_faces[Faref (FACE-VECTOR, 2)].
92 PARAM_FACE is a struct face whose members are the Xlib analogues of
93 the parameters in FACE-VECTOR. If an element of FACE-VECTOR is
94 nil, then the corresponding member of PARAM_FACE is FACE_DEFAULT.
95 These faces are called "parameter faces", because they're the ones
96 lisp manipulates to control what gets displayed. Elements 0 and 1
97 of FRAME->output_data.x->param_faces are special - they describe the
98 default and mode line faces. None of the faces in param_faces have
99 GC's. (See src/dispextern.h for the definition of struct face.
100 lisp/faces.el maintains the isomorphism between face_alist and
101 param_faces.)
102
103 The functions compute_char_face and compute_glyph_face find and
104 combine the parameter faces associated with overlays and text
105 properties. The resulting faces are called "computed faces"; none
106 of their members are FACE_DEFAULT; they are completely specified.
107 They then call intern_compute_face to search
108 FRAME->output_data.x->computed_faces for a matching face, add one if
109 none is found, and return the index into
110 FRAME->output_data.x->computed_faces. FRAME's glyph matrices use these
111 indices to record the faces of the matrix characters, and the X
112 display hooks consult compute_faces to decide how to display these
113 characters. Elements 0 and 1 of computed_faces always describe the
114 default and mode-line faces.
115
116 Each computed face belongs to a particular frame.
117
118 Computed faces have graphics contexts some of the time.
119 intern_face builds a GC for a specified computed face
120 if it doesn't have one already.
121 clear_face_cache clears out the GCs of all computed faces.
122 This is done from time to time so that we don't hold on to
123 lots of GCs that are no longer needed.
124
125 If a computed face has 0 as its font,
126 it is unused, and can be reused by new_computed_face.
127
128 Constraints:
129
130 Symbols naming faces must have associations on all frames; for any
131 FRAME, for all FACE-NAME, if (assq FACE-NAME (frame-face-alist
132 FRAME)) is non-nil, it must be non-nil for all frames.
133
134 Analogously, indices into param_faces must be valid on all frames;
135 if param_faces[i] is a non-zero face pointer on one frame, then it
136 must be filled in on all frames. Code assumes that face ID's can
137 be used on any frame.
138
139 Some subtleties:
140
141 Why do we keep param_faces and computed_faces separate?
142 computed_faces contains an element for every combination of facial
143 parameters we have ever displayed. indices into param_faces have
144 to be valid on all frames. If they were the same array, then that
145 array would grow very large on all frames, because any facial
146 combination displayed on any frame would need to be a valid entry
147 on all frames. */
148 \f
149 /* Definitions and declarations. */
150
151 /* The number of face-id's in use (same for all frames). */
152 static int next_face_id;
153
154 /* The number of the face to use to indicate the region. */
155 static int region_face;
156
157 /* This is what appears in a slot in a face to signify that the face
158 does not specify that display aspect. */
159 #define FACE_DEFAULT (~0)
160
161 Lisp_Object Qface, Qmouse_face;
162 Lisp_Object Qpixmap_spec_p;
163
164 int face_name_id_number ( /* FRAME_PTR, Lisp_Object name */ );
165
166 struct face *intern_face ( /* FRAME_PTR, struct face * */ );
167 static int new_computed_face ( /* FRAME_PTR, struct face * */ );
168 static int intern_computed_face ( /* FRAME_PTR, struct face * */ );
169 static void ensure_face_ready ( /* FRAME_PTR, int id */ );
170 void recompute_basic_faces ( /* FRAME_PTR f */ );
171 \f
172 /* Allocating, copying, and comparing struct faces. */
173
174 /* Allocate a new face */
175 static struct face *
176 allocate_face ()
177 {
178 struct face *result = (struct face *) xmalloc (sizeof (struct face));
179 bzero (result, sizeof (struct face));
180 result->font = (XFontStruct *) FACE_DEFAULT;
181 result->foreground = FACE_DEFAULT;
182 result->background = FACE_DEFAULT;
183 result->stipple = FACE_DEFAULT;
184 return result;
185 }
186
187 /* Make a new face that's a copy of an existing one. */
188 static struct face *
189 copy_face (face)
190 struct face *face;
191 {
192 struct face *result = allocate_face ();
193
194 result->font = face->font;
195 result->foreground = face->foreground;
196 result->background = face->background;
197 result->stipple = face->stipple;
198 result->underline = face->underline;
199 result->pixmap_h = face->pixmap_h;
200 result->pixmap_w = face->pixmap_w;
201
202 return result;
203 }
204
205 static int
206 face_eql (face1, face2)
207 struct face *face1, *face2;
208 {
209 return ( face1->font == face2->font
210 && face1->foreground == face2->foreground
211 && face1->background == face2->background
212 && face1->stipple == face2->stipple
213 && face1->underline == face2->underline);
214 }
215 \f
216 /* Managing graphics contexts of faces. */
217
218 #ifdef HAVE_X_WINDOWS
219 /* Given a computed face, construct its graphics context if necessary. */
220
221 struct face *
222 intern_face (f, face)
223 struct frame *f;
224 struct face *face;
225 {
226 GC gc;
227 XGCValues xgcv;
228 unsigned long mask;
229
230 if (face->gc)
231 return face;
232
233 BLOCK_INPUT;
234
235 if (face->foreground != FACE_DEFAULT)
236 xgcv.foreground = face->foreground;
237 else
238 xgcv.foreground = f->output_data.x->foreground_pixel;
239
240 if (face->background != FACE_DEFAULT)
241 xgcv.background = face->background;
242 else
243 xgcv.background = f->output_data.x->background_pixel;
244
245 if (face->font && face->font != (XFontStruct *) FACE_DEFAULT)
246 xgcv.font = face->font->fid;
247 else
248 xgcv.font = f->output_data.x->font->fid;
249
250 xgcv.graphics_exposures = 0;
251
252 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
253 if (face->stipple && face->stipple != FACE_DEFAULT)
254 {
255 xgcv.fill_style = FillStippled;
256 xgcv.stipple = x_bitmap_pixmap (f, face->stipple);
257 mask |= GCFillStyle | GCStipple;
258 }
259
260 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
261 mask, &xgcv);
262
263 face->gc = gc;
264
265 UNBLOCK_INPUT;
266
267 return face;
268 }
269
270 /* Clear out all graphics contexts for all computed faces
271 except for the default and mode line faces.
272 This should be done from time to time just to avoid
273 keeping too many graphics contexts that are no longer needed. */
274
275 void
276 clear_face_cache ()
277 {
278 Lisp_Object tail, frame;
279
280 BLOCK_INPUT;
281 FOR_EACH_FRAME (tail, frame)
282 {
283 FRAME_PTR f = XFRAME (frame);
284 if (FRAME_X_P (f))
285 {
286 int i;
287 Display *dpy = FRAME_X_DISPLAY (f);
288
289 for (i = 2; i < FRAME_N_COMPUTED_FACES (f); i++)
290 {
291 struct face *face = FRAME_COMPUTED_FACES (f) [i];
292 if (face->gc)
293 XFreeGC (dpy, face->gc);
294 face->gc = 0;
295 }
296 }
297 }
298
299 UNBLOCK_INPUT;
300 }
301 \f
302 /* Allocating, freeing, and duplicating fonts, colors, and pixmaps.
303
304 These functions operate on param faces only.
305 Computed faces get their fonts, colors and pixmaps
306 by merging param faces. */
307
308 static XFontStruct *
309 load_font (f, name)
310 struct frame *f;
311 Lisp_Object name;
312 {
313 XFontStruct *font;
314
315 if (NILP (name))
316 return (XFontStruct *) FACE_DEFAULT;
317
318 CHECK_STRING (name, 0);
319 BLOCK_INPUT;
320 font = XLoadQueryFont (FRAME_X_DISPLAY (f), (char *) XSTRING (name)->data);
321 UNBLOCK_INPUT;
322
323 if (! font)
324 Fsignal (Qerror, Fcons (build_string ("undefined font"),
325 Fcons (name, Qnil)));
326 return font;
327 }
328
329 static void
330 unload_font (f, font)
331 struct frame *f;
332 XFontStruct *font;
333 {
334 int len = FRAME_N_COMPUTED_FACES (f);
335 int i;
336
337 if (!font || font == ((XFontStruct *) FACE_DEFAULT))
338 return;
339
340 BLOCK_INPUT;
341 /* Invalidate any computed faces which use this font,
342 and free their GC's if they have any. */
343 for (i = 2; i < len; i++)
344 {
345 struct face *face = FRAME_COMPUTED_FACES (f)[i];
346 if (face->font == font)
347 {
348 Display *dpy = FRAME_X_DISPLAY (f);
349 if (face->gc)
350 XFreeGC (dpy, face->gc);
351 face->gc = 0;
352 /* This marks the computed face as available to reuse. */
353 face->font = 0;
354 }
355 }
356
357 XFreeFont (FRAME_X_DISPLAY (f), font);
358 UNBLOCK_INPUT;
359 }
360
361 static unsigned long
362 load_color (f, name)
363 struct frame *f;
364 Lisp_Object name;
365 {
366 XColor color;
367 int result;
368
369 if (NILP (name))
370 return FACE_DEFAULT;
371
372 CHECK_STRING (name, 0);
373 /* if the colormap is full, defined_color will return a best match
374 to the values in an an existing cell. */
375 result = defined_color(f, (char *) XSTRING (name)->data, &color, 1);
376 if (! result)
377 Fsignal (Qerror, Fcons (build_string ("undefined color"),
378 Fcons (name, Qnil)));
379 return (unsigned long) color.pixel;
380 }
381
382 static void
383 unload_color (f, pixel)
384 struct frame *f;
385 unsigned long pixel;
386 {
387 Colormap cmap;
388 Display *dpy = FRAME_X_DISPLAY (f);
389 int class = FRAME_X_DISPLAY_INFO (f)->visual->class;
390
391 if (pixel == FACE_DEFAULT
392 || pixel == BLACK_PIX_DEFAULT (f)
393 || pixel == WHITE_PIX_DEFAULT (f))
394 return;
395 cmap = DefaultColormapOfScreen (DefaultScreenOfDisplay (dpy));
396
397 /* If display has an immutable color map, freeing colors is not
398 necessary and some servers don't allow it. So don't do it. */
399 if (! (class == StaticColor || class == StaticGray || class == TrueColor))
400 {
401 int len = FRAME_N_COMPUTED_FACES (f);
402 int i;
403
404 BLOCK_INPUT;
405 /* Invalidate any computed faces which use this color,
406 and free their GC's if they have any. */
407 for (i = 2; i < len; i++)
408 {
409 struct face *face = FRAME_COMPUTED_FACES (f)[i];
410 if (face->foreground == pixel
411 || face->background == pixel)
412 {
413 Display *dpy = FRAME_X_DISPLAY (f);
414 if (face->gc)
415 XFreeGC (dpy, face->gc);
416 face->gc = 0;
417 /* This marks the computed face as available to reuse. */
418 face->font = 0;
419 }
420 }
421
422 XFreeColors (dpy, cmap, &pixel, 1, (unsigned long)0);
423 UNBLOCK_INPUT;
424 }
425 }
426
427 DEFUN ("pixmap-spec-p", Fpixmap_spec_p, Spixmap_spec_p, 1, 1, 0,
428 "Return t if OBJECT is a valid pixmap specification.")
429 (object)
430 Lisp_Object object;
431 {
432 Lisp_Object height, width;
433
434 return ((STRINGP (object)
435 || (CONSP (object)
436 && CONSP (XCONS (object)->cdr)
437 && CONSP (XCONS (XCONS (object)->cdr)->cdr)
438 && NILP (XCONS (XCONS (XCONS (object)->cdr)->cdr)->cdr)
439 && (width = XCONS (object)->car, INTEGERP (width))
440 && (height = XCONS (XCONS (object)->cdr)->car, INTEGERP (height))
441 && STRINGP (XCONS (XCONS (XCONS (object)->cdr)->cdr)->car)
442 && XINT (width) > 0
443 && XINT (height) > 0
444 /* The string must have enough bits for width * height. */
445 && ((XSTRING (XCONS (XCONS (XCONS (object)->cdr)->cdr)->car)->size
446 * (BITS_PER_INT / sizeof (int)))
447 >= XFASTINT (width) * XFASTINT (height))))
448 ? Qt : Qnil);
449 }
450
451 /* Load a bitmap according to NAME (which is either a file name
452 or a pixmap spec). Return the bitmap_id (see xfns.c)
453 or get an error if NAME is invalid.
454
455 Store the bitmap width in *W_PTR and height in *H_PTR. */
456
457 static long
458 load_pixmap (f, name, w_ptr, h_ptr)
459 FRAME_PTR f;
460 Lisp_Object name;
461 unsigned int *w_ptr, *h_ptr;
462 {
463 int bitmap_id;
464 Lisp_Object tem;
465
466 if (NILP (name))
467 return FACE_DEFAULT;
468
469 tem = Fpixmap_spec_p (name);
470 if (NILP (tem))
471 wrong_type_argument (Qpixmap_spec_p, name);
472
473 BLOCK_INPUT;
474
475 if (CONSP (name))
476 {
477 /* Decode a bitmap spec into a bitmap. */
478
479 int h, w;
480 Lisp_Object bits;
481
482 w = XINT (Fcar (name));
483 h = XINT (Fcar (Fcdr (name)));
484 bits = Fcar (Fcdr (Fcdr (name)));
485
486 bitmap_id = x_create_bitmap_from_data (f, XSTRING (bits)->data,
487 w, h);
488 }
489 else
490 {
491 /* It must be a string -- a file name. */
492 bitmap_id = x_create_bitmap_from_file (f, name);
493 }
494 UNBLOCK_INPUT;
495
496 if (bitmap_id < 0)
497 Fsignal (Qerror, Fcons (build_string ("invalid or undefined bitmap"),
498 Fcons (name, Qnil)));
499
500 *w_ptr = x_bitmap_width (f, bitmap_id);
501 *h_ptr = x_bitmap_height (f, bitmap_id);
502
503 return bitmap_id;
504 }
505
506 #else /* !HAVE_X_WINDOWS */
507
508 /* Stubs for MSDOS when not under X. */
509
510 struct face *
511 intern_face (f, face)
512 struct frame *f;
513 struct face *face;
514 {
515 return face;
516 }
517
518 void
519 clear_face_cache ()
520 {
521 /* No action. */
522 }
523
524 #ifdef MSDOS
525 unsigned long
526 load_color (f, name)
527 FRAME_PTR f;
528 Lisp_Object name;
529 {
530 Lisp_Object result;
531
532 if (NILP (name))
533 return FACE_DEFAULT;
534
535 CHECK_STRING (name, 0);
536 result = call1 (Qmsdos_color_translate, name);
537 if (INTEGERP (result))
538 return XINT (result);
539 else
540 Fsignal (Qerror, Fcons (build_string ("undefined color"),
541 Fcons (name, Qnil)));
542 }
543 #endif
544 #endif /* !HAVE_X_WINDOWS */
545
546 \f
547 /* Managing parameter face arrays for frames. */
548
549 void
550 init_frame_faces (f)
551 FRAME_PTR f;
552 {
553 ensure_face_ready (f, 0);
554 ensure_face_ready (f, 1);
555
556 FRAME_N_COMPUTED_FACES (f) = 0;
557 FRAME_SIZE_COMPUTED_FACES (f) = 0;
558
559 new_computed_face (f, FRAME_PARAM_FACES (f)[0]);
560 new_computed_face (f, FRAME_PARAM_FACES (f)[1]);
561 recompute_basic_faces (f);
562
563 /* Find another X frame. */
564 {
565 Lisp_Object tail, frame, result;
566
567 result = Qnil;
568 FOR_EACH_FRAME (tail, frame)
569 if ((FRAME_MSDOS_P (XFRAME (frame)) || FRAME_X_P (XFRAME (frame)))
570 && XFRAME (frame) != f)
571 {
572 result = frame;
573 break;
574 }
575
576 /* If we didn't find any X frames other than f, then we don't need
577 any faces other than 0 and 1, so we're okay. Otherwise, make
578 sure that all faces valid on the selected frame are also valid
579 on this new frame. */
580 if (FRAMEP (result))
581 {
582 int i;
583 int n_faces = FRAME_N_PARAM_FACES (XFRAME (result));
584 struct face **faces = FRAME_PARAM_FACES (XFRAME (result));
585
586 for (i = 2; i < n_faces; i++)
587 if (faces[i])
588 ensure_face_ready (f, i);
589 }
590 }
591 }
592
593
594 /* Called from Fdelete_frame. */
595
596 void
597 free_frame_faces (f)
598 struct frame *f;
599 {
600 Display *dpy = FRAME_X_DISPLAY (f);
601 int i;
602
603 BLOCK_INPUT;
604
605 for (i = 0; i < FRAME_N_PARAM_FACES (f); i++)
606 {
607 struct face *face = FRAME_PARAM_FACES (f) [i];
608 if (face)
609 {
610 unload_font (f, face->font);
611 unload_color (f, face->foreground);
612 unload_color (f, face->background);
613 x_destroy_bitmap (f, face->stipple);
614 xfree (face);
615 }
616 }
617 xfree (FRAME_PARAM_FACES (f));
618 FRAME_PARAM_FACES (f) = 0;
619 FRAME_N_PARAM_FACES (f) = 0;
620
621 /* All faces in FRAME_COMPUTED_FACES use resources copied from
622 FRAME_PARAM_FACES; we can free them without fuss.
623 But we do free the GCs and the face objects themselves. */
624 for (i = 0; i < FRAME_N_COMPUTED_FACES (f); i++)
625 {
626 struct face *face = FRAME_COMPUTED_FACES (f) [i];
627 if (face)
628 {
629 if (face->gc)
630 XFreeGC (dpy, face->gc);
631 xfree (face);
632 }
633 }
634 xfree (FRAME_COMPUTED_FACES (f));
635 FRAME_COMPUTED_FACES (f) = 0;
636 FRAME_N_COMPUTED_FACES (f) = 0;
637
638 UNBLOCK_INPUT;
639 }
640 \f
641 /* Interning faces in a frame's face array. */
642
643 static int
644 new_computed_face (f, new_face)
645 struct frame *f;
646 struct face *new_face;
647 {
648 int len = FRAME_N_COMPUTED_FACES (f);
649 int i;
650
651 /* Search for an unused computed face in the middle of the table. */
652 for (i = 0; i < len; i++)
653 {
654 struct face *face = FRAME_COMPUTED_FACES (f)[i];
655 if (face->font == 0)
656 {
657 FRAME_COMPUTED_FACES (f)[i] = copy_face (new_face);
658 return i;
659 }
660 }
661
662 if (i >= FRAME_SIZE_COMPUTED_FACES (f))
663 {
664 int new_size = i + 32;
665
666 FRAME_COMPUTED_FACES (f)
667 = (struct face **) (FRAME_SIZE_COMPUTED_FACES (f) == 0
668 ? xmalloc (new_size * sizeof (struct face *))
669 : xrealloc (FRAME_COMPUTED_FACES (f),
670 new_size * sizeof (struct face *)));
671 FRAME_SIZE_COMPUTED_FACES (f) = new_size;
672 }
673
674 i = FRAME_N_COMPUTED_FACES (f)++;
675 FRAME_COMPUTED_FACES (f)[i] = copy_face (new_face);
676 return i;
677 }
678
679
680 /* Find a match for NEW_FACE in a FRAME's computed face array, and add
681 it if we don't find one. */
682 static int
683 intern_computed_face (f, new_face)
684 struct frame *f;
685 struct face *new_face;
686 {
687 int len = FRAME_N_COMPUTED_FACES (f);
688 int i;
689
690 /* Search for a computed face already on F equivalent to FACE. */
691 for (i = 0; i < len; i++)
692 {
693 if (! FRAME_COMPUTED_FACES (f)[i])
694 abort ();
695 if (face_eql (new_face, FRAME_COMPUTED_FACES (f)[i]))
696 return i;
697 }
698
699 /* We didn't find one; add a new one. */
700 return new_computed_face (f, new_face);
701 }
702
703 /* Make parameter face id ID valid on frame F. */
704
705 static void
706 ensure_face_ready (f, id)
707 struct frame *f;
708 int id;
709 {
710 if (FRAME_N_PARAM_FACES (f) <= id)
711 {
712 int n = id + 10;
713 int i;
714 if (!FRAME_N_PARAM_FACES (f))
715 FRAME_PARAM_FACES (f)
716 = (struct face **) xmalloc (sizeof (struct face *) * n);
717 else
718 FRAME_PARAM_FACES (f)
719 = (struct face **) xrealloc (FRAME_PARAM_FACES (f),
720 sizeof (struct face *) * n);
721
722 bzero (FRAME_PARAM_FACES (f) + FRAME_N_PARAM_FACES (f),
723 (n - FRAME_N_PARAM_FACES (f)) * sizeof (struct face *));
724 FRAME_N_PARAM_FACES (f) = n;
725 }
726
727 if (FRAME_PARAM_FACES (f) [id] == 0)
728 FRAME_PARAM_FACES (f) [id] = allocate_face ();
729 }
730 \f
731 #ifdef HAVE_X_WINDOWS
732 /* Return non-zero if FONT1 and FONT2 have the same width.
733 We do not check the height, because we can now deal with
734 different heights.
735 We assume that they're both character-cell fonts. */
736
737 int
738 same_size_fonts (font1, font2)
739 XFontStruct *font1, *font2;
740 {
741 XCharStruct *bounds1 = &font1->min_bounds;
742 XCharStruct *bounds2 = &font2->min_bounds;
743
744 return (bounds1->width == bounds2->width);
745 }
746
747 /* Update the line_height of frame F according to the biggest font in
748 any face. Return nonzero if if line_height changes. */
749
750 int
751 frame_update_line_height (f)
752 FRAME_PTR f;
753 {
754 int i;
755 int biggest = FONT_HEIGHT (f->output_data.x->font);
756
757 for (i = 0; i < f->output_data.x->n_param_faces; i++)
758 if (f->output_data.x->param_faces[i] != 0
759 && f->output_data.x->param_faces[i]->font != (XFontStruct *) FACE_DEFAULT)
760 {
761 int height = FONT_HEIGHT (f->output_data.x->param_faces[i]->font);
762 if (height > biggest)
763 biggest = height;
764 }
765
766 if (biggest == f->output_data.x->line_height)
767 return 0;
768
769 f->output_data.x->line_height = biggest;
770 return 1;
771 }
772 #endif /* not HAVE_X_WINDOWS */
773 \f
774 /* Modify face TO by copying from FROM all properties which have
775 nondefault settings. */
776
777 static void
778 merge_faces (from, to)
779 struct face *from, *to;
780 {
781 /* Only merge the font if it's the same width as the base font.
782 Otherwise ignore it, since we can't handle it properly. */
783 if (from->font != (XFontStruct *) FACE_DEFAULT
784 && same_size_fonts (from->font, to->font))
785 to->font = from->font;
786 if (from->foreground != FACE_DEFAULT)
787 to->foreground = from->foreground;
788 if (from->background != FACE_DEFAULT)
789 to->background = from->background;
790 if (from->stipple != FACE_DEFAULT)
791 {
792 to->stipple = from->stipple;
793 to->pixmap_h = from->pixmap_h;
794 to->pixmap_w = from->pixmap_w;
795 }
796 if (from->underline)
797 to->underline = from->underline;
798 }
799
800 /* Set up the basic set of facial parameters, based on the frame's
801 data; all faces are deltas applied to this. */
802
803 static void
804 compute_base_face (f, face)
805 FRAME_PTR f;
806 struct face *face;
807 {
808 face->gc = 0;
809 face->foreground = FRAME_FOREGROUND_PIXEL (f);
810 face->background = FRAME_BACKGROUND_PIXEL (f);
811 face->font = FRAME_FONT (f);
812 face->stipple = 0;
813 face->underline = 0;
814 }
815
816 /* Return the face ID to use to display a special glyph which selects
817 FACE_CODE as the face ID, assuming that ordinarily the face would
818 be CURRENT_FACE. F is the frame. */
819
820 int
821 compute_glyph_face (f, face_code, current_face)
822 struct frame *f;
823 int face_code, current_face;
824 {
825 struct face face;
826
827 face = *FRAME_COMPUTED_FACES (f)[current_face];
828
829 if (face_code >= 0 && face_code < FRAME_N_PARAM_FACES (f)
830 && FRAME_PARAM_FACES (f) [face_code] != 0)
831 merge_faces (FRAME_PARAM_FACES (f) [face_code], &face);
832
833 return intern_computed_face (f, &face);
834 }
835
836 /* Return the face ID to use to display a special glyph which selects
837 FACE_CODE as the face ID, assuming that ordinarily the face would
838 be CURRENT_FACE. F is the frame. */
839
840 int
841 compute_glyph_face_1 (f, face_name, current_face)
842 struct frame *f;
843 Lisp_Object face_name;
844 int current_face;
845 {
846 struct face face;
847
848 face = *FRAME_COMPUTED_FACES (f)[current_face];
849
850 if (!NILP (face_name))
851 {
852 int facecode = face_name_id_number (f, face_name);
853 if (facecode >= 0 && facecode < FRAME_N_PARAM_FACES (f)
854 && FRAME_PARAM_FACES (f) [facecode] != 0)
855 merge_faces (FRAME_PARAM_FACES (f) [facecode], &face);
856 }
857
858 return intern_computed_face (f, &face);
859 }
860 \f
861 /* Return the face ID associated with a buffer position POS.
862 Store into *ENDPTR the position at which a different face is needed.
863 This does not take account of glyphs that specify their own face codes.
864 F is the frame in use for display, and W is a window displaying
865 the current buffer.
866
867 REGION_BEG, REGION_END delimit the region, so it can be highlighted.
868
869 LIMIT is a position not to scan beyond. That is to limit
870 the time this function can take.
871
872 If MOUSE is nonzero, use the character's mouse-face, not its face. */
873
874 int
875 compute_char_face (f, w, pos, region_beg, region_end, endptr, limit, mouse)
876 struct frame *f;
877 struct window *w;
878 int pos;
879 int region_beg, region_end;
880 int *endptr;
881 int limit;
882 int mouse;
883 {
884 struct face face;
885 Lisp_Object prop, position;
886 int i, j, noverlays;
887 int facecode;
888 Lisp_Object *overlay_vec;
889 Lisp_Object frame;
890 int endpos;
891 Lisp_Object propname;
892
893 /* W must display the current buffer. We could write this function
894 to use the frame and buffer of W, but right now it doesn't. */
895 if (XBUFFER (w->buffer) != current_buffer)
896 abort ();
897
898 XSETFRAME (frame, f);
899
900 endpos = ZV;
901 if (pos < region_beg && region_beg < endpos)
902 endpos = region_beg;
903
904 XSETFASTINT (position, pos);
905
906 if (mouse)
907 propname = Qmouse_face;
908 else
909 propname = Qface;
910
911 prop = Fget_text_property (position, propname, w->buffer);
912
913 {
914 Lisp_Object limit1, end;
915
916 XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
917 end = Fnext_single_property_change (position, propname, w->buffer, limit1);
918 if (INTEGERP (end))
919 endpos = XINT (end);
920 }
921
922 {
923 int next_overlay;
924 int len;
925
926 /* First try with room for 40 overlays. */
927 len = 40;
928 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
929
930 noverlays = overlays_at (pos, 0, &overlay_vec, &len,
931 &next_overlay, (int *) 0);
932
933 /* If there are more than 40,
934 make enough space for all, and try again. */
935 if (noverlays > len)
936 {
937 len = noverlays;
938 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
939 noverlays = overlays_at (pos, 0, &overlay_vec, &len,
940 &next_overlay, (int *) 0);
941 }
942
943 if (next_overlay < endpos)
944 endpos = next_overlay;
945 }
946
947 *endptr = endpos;
948
949 /* Optimize the default case. */
950 if (noverlays == 0 && NILP (prop)
951 && !(pos >= region_beg && pos < region_end))
952 return 0;
953
954 compute_base_face (f, &face);
955
956 if (CONSP (prop))
957 {
958 /* We have a list of faces, merge them in reverse order */
959 Lisp_Object length;
960 int len;
961 Lisp_Object *faces;
962
963 length = Fsafe_length (prop);
964 len = XFASTINT (length);
965
966 /* Put them into an array */
967 faces = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
968 for (j = 0; j < len; j++)
969 {
970 faces[j] = Fcar (prop);
971 prop = Fcdr (prop);
972 }
973 /* So that we can merge them in the reverse order */
974 for (j = len - 1; j >= 0; j--)
975 {
976 facecode = face_name_id_number (f, faces[j]);
977 if (facecode >= 0 && facecode < FRAME_N_PARAM_FACES (f)
978 && FRAME_PARAM_FACES (f) [facecode] != 0)
979 merge_faces (FRAME_PARAM_FACES (f) [facecode], &face);
980 }
981 }
982 else if (!NILP (prop))
983 {
984 facecode = face_name_id_number (f, prop);
985 if (facecode >= 0 && facecode < FRAME_N_PARAM_FACES (f)
986 && FRAME_PARAM_FACES (f) [facecode] != 0)
987 merge_faces (FRAME_PARAM_FACES (f) [facecode], &face);
988 }
989
990 noverlays = sort_overlays (overlay_vec, noverlays, w);
991
992 /* Now merge the overlay data in that order. */
993 for (i = 0; i < noverlays; i++)
994 {
995 prop = Foverlay_get (overlay_vec[i], propname);
996 if (CONSP (prop))
997 {
998 /* We have a list of faces, merge them in reverse order */
999 Lisp_Object length;
1000 int len;
1001 Lisp_Object *faces;
1002
1003 length = Fsafe_length (prop);
1004 len = XFASTINT (length);
1005
1006 /* Put them into an array */
1007 faces = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
1008 for (j = 0; j < len; j++)
1009 {
1010 faces[j] = Fcar (prop);
1011 prop = Fcdr (prop);
1012 }
1013 /* So that we can merge them in the reverse order */
1014 for (j = len - 1; j >= 0; j--)
1015 {
1016 facecode = face_name_id_number (f, faces[j]);
1017 if (facecode >= 0 && facecode < FRAME_N_PARAM_FACES (f)
1018 && FRAME_PARAM_FACES (f) [facecode] != 0)
1019 merge_faces (FRAME_PARAM_FACES (f) [facecode], &face);
1020 }
1021 }
1022 else if (!NILP (prop))
1023 {
1024 Lisp_Object oend;
1025 int oendpos;
1026
1027 facecode = face_name_id_number (f, prop);
1028 if (facecode >= 0 && facecode < FRAME_N_PARAM_FACES (f)
1029 && FRAME_PARAM_FACES (f) [facecode] != 0)
1030 merge_faces (FRAME_PARAM_FACES (f)[facecode], &face);
1031
1032 oend = OVERLAY_END (overlay_vec[i]);
1033 oendpos = OVERLAY_POSITION (oend);
1034 if (oendpos < endpos)
1035 endpos = oendpos;
1036 }
1037 }
1038
1039 if (pos >= region_beg && pos < region_end)
1040 {
1041 if (region_end < endpos)
1042 endpos = region_end;
1043 if (region_face >= 0 && region_face < next_face_id)
1044 merge_faces (FRAME_PARAM_FACES (f)[region_face], &face);
1045 }
1046
1047 *endptr = endpos;
1048
1049 return intern_computed_face (f, &face);
1050 }
1051 \f
1052 /* Recompute the GC's for the default and modeline faces.
1053 We call this after changing frame parameters on which those GC's
1054 depend. */
1055
1056 void
1057 recompute_basic_faces (f)
1058 FRAME_PTR f;
1059 {
1060 /* If the frame's faces haven't been initialized yet, don't worry about
1061 this stuff. */
1062 if (FRAME_N_PARAM_FACES (f) < 2)
1063 return;
1064
1065 BLOCK_INPUT;
1066
1067 if (FRAME_DEFAULT_FACE (f)->gc)
1068 XFreeGC (FRAME_X_DISPLAY (f), FRAME_DEFAULT_FACE (f)->gc);
1069 if (FRAME_MODE_LINE_FACE (f)->gc)
1070 XFreeGC (FRAME_X_DISPLAY (f), FRAME_MODE_LINE_FACE (f)->gc);
1071
1072 compute_base_face (f, FRAME_DEFAULT_FACE (f));
1073 compute_base_face (f, FRAME_MODE_LINE_FACE (f));
1074
1075 merge_faces (FRAME_DEFAULT_PARAM_FACE (f), FRAME_DEFAULT_FACE (f));
1076 merge_faces (FRAME_MODE_LINE_PARAM_FACE (f), FRAME_MODE_LINE_FACE (f));
1077
1078 intern_face (f, FRAME_DEFAULT_FACE (f));
1079 intern_face (f, FRAME_MODE_LINE_FACE (f));
1080
1081 UNBLOCK_INPUT;
1082 }
1083
1084
1085 \f
1086 /* Lisp interface. */
1087
1088 DEFUN ("frame-face-alist", Fframe_face_alist, Sframe_face_alist, 1, 1, 0,
1089 "")
1090 (frame)
1091 Lisp_Object frame;
1092 {
1093 CHECK_FRAME (frame, 0);
1094 return XFRAME (frame)->face_alist;
1095 }
1096
1097 DEFUN ("set-frame-face-alist", Fset_frame_face_alist, Sset_frame_face_alist,
1098 2, 2, 0, "")
1099 (frame, value)
1100 Lisp_Object frame, value;
1101 {
1102 CHECK_FRAME (frame, 0);
1103 XFRAME (frame)->face_alist = value;
1104 return value;
1105 }
1106
1107
1108 DEFUN ("make-face-internal", Fmake_face_internal, Smake_face_internal, 1, 1, 0,
1109 "Create face number FACE-ID on all frames.")
1110 (face_id)
1111 Lisp_Object face_id;
1112 {
1113 Lisp_Object rest, frame;
1114 int id = XINT (face_id);
1115
1116 CHECK_NUMBER (face_id, 0);
1117 if (id < 0 || id >= next_face_id)
1118 error ("Face id out of range");
1119
1120 FOR_EACH_FRAME (rest, frame)
1121 {
1122 if (FRAME_MSDOS_P (XFRAME (frame)) || FRAME_X_P (XFRAME (frame)))
1123 ensure_face_ready (XFRAME (frame), id);
1124 }
1125 return Qnil;
1126 }
1127
1128
1129 DEFUN ("set-face-attribute-internal", Fset_face_attribute_internal,
1130 Sset_face_attribute_internal, 4, 4, 0, "")
1131 (face_id, attr_name, attr_value, frame)
1132 Lisp_Object face_id, attr_name, attr_value, frame;
1133 {
1134 struct face *face;
1135 struct frame *f;
1136 int magic_p;
1137 int id;
1138 int garbaged = 0;
1139
1140 CHECK_FRAME (frame, 0);
1141 CHECK_NUMBER (face_id, 0);
1142 CHECK_SYMBOL (attr_name, 0);
1143
1144 f = XFRAME (frame);
1145 id = XINT (face_id);
1146 if (id < 0 || id >= next_face_id)
1147 error ("Face id out of range");
1148
1149 if (! FRAME_X_P (f) && ! FRAME_MSDOS_P (f))
1150 return Qnil;
1151
1152 ensure_face_ready (f, id);
1153 face = FRAME_PARAM_FACES (f) [XFASTINT (face_id)];
1154
1155 if (EQ (attr_name, intern ("font")))
1156 {
1157 #if defined (MSDOS) && !defined (HAVE_X_WINDOWS)
1158 /* The one and only font. Must *not* be zero (which
1159 is taken to mean an unused face nowadays). */
1160 face->font = (XFontStruct *)1 ;
1161 #else
1162 XFontStruct *font = load_font (f, attr_value);
1163 if (face->font != f->output_data.x->font)
1164 unload_font (f, face->font);
1165 face->font = font;
1166 if (frame_update_line_height (f))
1167 x_set_window_size (f, 0, f->width, f->height);
1168 /* Must clear cache, since it might contain the font
1169 we just got rid of. */
1170 garbaged = 1;
1171 #endif
1172 }
1173 else if (EQ (attr_name, intern ("foreground")))
1174 {
1175 unsigned long new_color = load_color (f, attr_value);
1176 unload_color (f, face->foreground);
1177 face->foreground = new_color;
1178 garbaged = 1;
1179 }
1180 else if (EQ (attr_name, intern ("background")))
1181 {
1182 unsigned long new_color = load_color (f, attr_value);
1183 unload_color (f, face->background);
1184 face->background = new_color;
1185 garbaged = 1;
1186 }
1187 else if (EQ (attr_name, intern ("background-pixmap")))
1188 {
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;
1196 }
1197 else if (EQ (attr_name, intern ("underline")))
1198 {
1199 int new = !NILP (attr_value);
1200 face->underline = new;
1201 }
1202 else
1203 error ("unknown face attribute");
1204
1205 if (id == 0 || id == 1)
1206 recompute_basic_faces (f);
1207
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 {
1215 SET_FRAME_GARBAGED (f);
1216 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 1;
1217 }
1218
1219 return Qnil;
1220 }
1221
1222 DEFUN ("internal-next-face-id", Finternal_next_face_id, Sinternal_next_face_id,
1223 0, 0, 0, "")
1224 ()
1225 {
1226 return make_number (next_face_id++);
1227 }
1228
1229 /* Return the face id for name NAME on frame FRAME.
1230 (It should be the same for all frames,
1231 but it's as easy to use the "right" frame to look it up
1232 as to use any other one.) */
1233
1234 int
1235 face_name_id_number (f, name)
1236 FRAME_PTR f;
1237 Lisp_Object name;
1238 {
1239 Lisp_Object tem;
1240
1241 tem = Fcdr (assq_no_quit (name, f->face_alist));
1242 if (NILP (tem))
1243 return 0;
1244 CHECK_VECTOR (tem, 0);
1245 tem = XVECTOR (tem)->contents[2];
1246 CHECK_NUMBER (tem, 0);
1247 return XINT (tem);
1248 }
1249 \f
1250 /* Emacs initialization. */
1251
1252 void
1253 syms_of_xfaces ()
1254 {
1255 Qface = intern ("face");
1256 staticpro (&Qface);
1257 Qmouse_face = intern ("mouse-face");
1258 staticpro (&Qmouse_face);
1259 Qpixmap_spec_p = intern ("pixmap-spec-p");
1260 staticpro (&Qpixmap_spec_p);
1261
1262 DEFVAR_INT ("region-face", &region_face,
1263 "Face number to use to highlight the region\n\
1264 The region is highlighted with this face\n\
1265 when Transient Mark mode is enabled and the mark is active.");
1266
1267 #ifdef HAVE_X_WINDOWS
1268 defsubr (&Spixmap_spec_p);
1269 #endif
1270 defsubr (&Sframe_face_alist);
1271 defsubr (&Sset_frame_face_alist);
1272 defsubr (&Smake_face_internal);
1273 defsubr (&Sset_face_attribute_internal);
1274 defsubr (&Sinternal_next_face_id);
1275 }
1276
1277 #endif /* HAVE_FACES */