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