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