]> code.delx.au - gnu-emacs/blob - src/xfaces.c
(compute_char_face): Use Fsafe_length.
[gnu-emacs] / 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->display.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->display.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->display.x->computed_faces for a matching face, add one if
108 none is found, and return the index into
109 FRAME->display.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->display.x->foreground_pixel;
235
236 if (face->background != FACE_DEFAULT)
237 xgcv.background = face->background;
238 else
239 xgcv.background = f->display.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->display.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 if (pixel == FACE_DEFAULT
367 || pixel == BLACK_PIX_DEFAULT (f)
368 || pixel == WHITE_PIX_DEFAULT (f))
369 return;
370 cmap = DefaultColormapOfScreen (DefaultScreenOfDisplay (dpy));
371 BLOCK_INPUT;
372 XFreeColors (dpy, cmap, &pixel, 1, (unsigned long)0);
373 UNBLOCK_INPUT;
374 }
375
376 DEFUN ("pixmap-spec-p", Fpixmap_spec_p, Spixmap_spec_p, 1, 1, 0,
377 "Return t if ARG is a valid pixmap specification.")
378 (arg)
379 Lisp_Object arg;
380 {
381 Lisp_Object height, width;
382
383 return ((STRINGP (arg)
384 || (CONSP (arg)
385 && CONSP (XCONS (arg)->cdr)
386 && CONSP (XCONS (XCONS (arg)->cdr)->cdr)
387 && NILP (XCONS (XCONS (XCONS (arg)->cdr)->cdr)->cdr)
388 && (width = XCONS (arg)->car, INTEGERP (width))
389 && (height = XCONS (XCONS (arg)->cdr)->car, INTEGERP (height))
390 && STRINGP (XCONS (XCONS (XCONS (arg)->cdr)->cdr)->car)
391 && XINT (width) > 0
392 && XINT (height) > 0
393 /* The string must have enough bits for width * height. */
394 && ((XSTRING (XCONS (XCONS (XCONS (arg)->cdr)->cdr)->car)->size
395 * (INTBITS / sizeof (int)))
396 >= XFASTINT (width) * XFASTINT (height))))
397 ? Qt : Qnil);
398 }
399
400 /* Load a bitmap according to NAME (which is either a file name
401 or a pixmap spec). Return the bitmap_id (see xfns.c)
402 or get an error if NAME is invalid.
403
404 Store the bitmap width in *W_PTR and height in *H_PTR. */
405
406 static long
407 load_pixmap (f, name, w_ptr, h_ptr)
408 FRAME_PTR f;
409 Lisp_Object name;
410 unsigned int *w_ptr, *h_ptr;
411 {
412 int bitmap_id;
413 Lisp_Object tem;
414
415 if (NILP (name))
416 return FACE_DEFAULT;
417
418 tem = Fpixmap_spec_p (name);
419 if (NILP (tem))
420 wrong_type_argument (Qpixmap_spec_p, name);
421
422 BLOCK_INPUT;
423
424 if (CONSP (name))
425 {
426 /* Decode a bitmap spec into a bitmap. */
427
428 int h, w;
429 Lisp_Object bits;
430
431 w = XINT (Fcar (name));
432 h = XINT (Fcar (Fcdr (name)));
433 bits = Fcar (Fcdr (Fcdr (name)));
434
435 bitmap_id = x_create_bitmap_from_data (f, XSTRING (bits)->data,
436 w, h);
437 }
438 else
439 {
440 /* It must be a string -- a file name. */
441 bitmap_id = x_create_bitmap_from_file (f, name);
442 }
443 UNBLOCK_INPUT;
444
445 if (bitmap_id < 0)
446 Fsignal (Qerror, Fcons (build_string ("invalid or undefined bitmap"),
447 Fcons (name, Qnil)));
448
449 *w_ptr = x_bitmap_width (f, bitmap_id);
450 *h_ptr = x_bitmap_height (f, bitmap_id);
451
452 return bitmap_id;
453 }
454
455 #else /* !HAVE_X_WINDOWS */
456
457 /* Stubs for MSDOS when not under X. */
458
459 struct face *
460 intern_face (f, face)
461 struct frame *f;
462 struct face *face;
463 {
464 return face;
465 }
466
467 void
468 clear_face_cache ()
469 {
470 /* No action. */
471 }
472
473 #ifdef MSDOS
474 unsigned long
475 load_color (f, name)
476 FRAME_PTR f;
477 Lisp_Object name;
478 {
479 Lisp_Object result;
480
481 if (NILP (name))
482 return FACE_DEFAULT;
483
484 CHECK_STRING (name, 0);
485 result = call1 (Qmsdos_color_translate, name);
486 if (INTEGERP (result))
487 return XINT (result);
488 else
489 Fsignal (Qerror, Fcons (build_string ("undefined color"),
490 Fcons (name, Qnil)));
491 }
492 #endif
493 #endif /* !HAVE_X_WINDOWS */
494
495 \f
496 /* Managing parameter face arrays for frames. */
497
498 void
499 init_frame_faces (f)
500 FRAME_PTR f;
501 {
502 ensure_face_ready (f, 0);
503 ensure_face_ready (f, 1);
504
505 FRAME_N_COMPUTED_FACES (f) = 0;
506 FRAME_SIZE_COMPUTED_FACES (f) = 0;
507
508 new_computed_face (f, FRAME_PARAM_FACES (f)[0]);
509 new_computed_face (f, FRAME_PARAM_FACES (f)[1]);
510 recompute_basic_faces (f);
511
512 #ifdef MULTI_FRAME
513 /* Find another X frame. */
514 {
515 Lisp_Object tail, frame, result;
516
517 result = Qnil;
518 FOR_EACH_FRAME (tail, frame)
519 if (FRAME_X_P (XFRAME (frame))
520 && XFRAME (frame) != f)
521 {
522 result = frame;
523 break;
524 }
525
526 /* If we didn't find any X frames other than f, then we don't need
527 any faces other than 0 and 1, so we're okay. Otherwise, make
528 sure that all faces valid on the selected frame are also valid
529 on this new frame. */
530 if (FRAMEP (result))
531 {
532 int i;
533 int n_faces = FRAME_N_PARAM_FACES (XFRAME (result));
534 struct face **faces = FRAME_PARAM_FACES (XFRAME (result));
535
536 for (i = 2; i < n_faces; i++)
537 if (faces[i])
538 ensure_face_ready (f, i);
539 }
540 }
541 #endif /* MULTI_FRAME */
542 }
543
544
545 /* Called from Fdelete_frame. */
546
547 void
548 free_frame_faces (f)
549 struct frame *f;
550 {
551 Display *dpy = FRAME_X_DISPLAY (f);
552 int i;
553
554 BLOCK_INPUT;
555
556 for (i = 0; i < FRAME_N_PARAM_FACES (f); i++)
557 {
558 struct face *face = FRAME_PARAM_FACES (f) [i];
559 if (face)
560 {
561 unload_font (f, face->font);
562 unload_color (f, face->foreground);
563 unload_color (f, face->background);
564 x_destroy_bitmap (f, face->stipple);
565 xfree (face);
566 }
567 }
568 xfree (FRAME_PARAM_FACES (f));
569 FRAME_PARAM_FACES (f) = 0;
570 FRAME_N_PARAM_FACES (f) = 0;
571
572 /* All faces in FRAME_COMPUTED_FACES use resources copied from
573 FRAME_PARAM_FACES; we can free them without fuss.
574 But we do free the GCs and the face objects themselves. */
575 for (i = 0; i < FRAME_N_COMPUTED_FACES (f); i++)
576 {
577 struct face *face = FRAME_COMPUTED_FACES (f) [i];
578 if (face)
579 {
580 if (face->gc)
581 XFreeGC (dpy, face->gc);
582 xfree (face);
583 }
584 }
585 xfree (FRAME_COMPUTED_FACES (f));
586 FRAME_COMPUTED_FACES (f) = 0;
587 FRAME_N_COMPUTED_FACES (f) = 0;
588
589 UNBLOCK_INPUT;
590 }
591 \f
592 /* Interning faces in a frame's face array. */
593
594 static int
595 new_computed_face (f, new_face)
596 struct frame *f;
597 struct face *new_face;
598 {
599 int i = FRAME_N_COMPUTED_FACES (f);
600
601 if (i >= FRAME_SIZE_COMPUTED_FACES (f))
602 {
603 int new_size = i + 32;
604
605 FRAME_COMPUTED_FACES (f)
606 = (struct face **) (FRAME_SIZE_COMPUTED_FACES (f) == 0
607 ? xmalloc (new_size * sizeof (struct face *))
608 : xrealloc (FRAME_COMPUTED_FACES (f),
609 new_size * sizeof (struct face *)));
610 FRAME_SIZE_COMPUTED_FACES (f) = new_size;
611 }
612
613 i = FRAME_N_COMPUTED_FACES (f)++;
614 FRAME_COMPUTED_FACES (f)[i] = copy_face (new_face);
615 return i;
616 }
617
618
619 /* Find a match for NEW_FACE in a FRAME's computed face array, and add
620 it if we don't find one. */
621 static int
622 intern_computed_face (f, new_face)
623 struct frame *f;
624 struct face *new_face;
625 {
626 int len = FRAME_N_COMPUTED_FACES (f);
627 int i;
628
629 /* Search for a computed face already on F equivalent to FACE. */
630 for (i = 0; i < len; i++)
631 {
632 if (! FRAME_COMPUTED_FACES (f)[i])
633 abort ();
634 if (face_eql (new_face, FRAME_COMPUTED_FACES (f)[i]))
635 return i;
636 }
637
638 /* We didn't find one; add a new one. */
639 return new_computed_face (f, new_face);
640 }
641
642 /* Make parameter face id ID valid on frame F. */
643
644 static void
645 ensure_face_ready (f, id)
646 struct frame *f;
647 int id;
648 {
649 if (FRAME_N_PARAM_FACES (f) <= id)
650 {
651 int n = id + 10;
652 int i;
653 if (!FRAME_N_PARAM_FACES (f))
654 FRAME_PARAM_FACES (f)
655 = (struct face **) xmalloc (sizeof (struct face *) * n);
656 else
657 FRAME_PARAM_FACES (f)
658 = (struct face **) xrealloc (FRAME_PARAM_FACES (f),
659 sizeof (struct face *) * n);
660
661 bzero (FRAME_PARAM_FACES (f) + FRAME_N_PARAM_FACES (f),
662 (n - FRAME_N_PARAM_FACES (f)) * sizeof (struct face *));
663 FRAME_N_PARAM_FACES (f) = n;
664 }
665
666 if (FRAME_PARAM_FACES (f) [id] == 0)
667 FRAME_PARAM_FACES (f) [id] = allocate_face ();
668 }
669 \f
670 #ifdef HAVE_X_WINDOWS
671 /* Return non-zero if FONT1 and FONT2 have the same width.
672 We do not check the height, because we can now deal with
673 different heights.
674 We assume that they're both character-cell fonts. */
675
676 int
677 same_size_fonts (font1, font2)
678 XFontStruct *font1, *font2;
679 {
680 XCharStruct *bounds1 = &font1->min_bounds;
681 XCharStruct *bounds2 = &font2->min_bounds;
682
683 return (bounds1->width == bounds2->width);
684 }
685
686 /* Update the line_height of frame F according to the biggest font in
687 any face. Return nonzero if if line_height changes. */
688
689 int
690 frame_update_line_height (f)
691 FRAME_PTR f;
692 {
693 int i;
694 int biggest = FONT_HEIGHT (f->display.x->font);
695
696 for (i = 0; i < f->display.x->n_param_faces; i++)
697 if (f->display.x->param_faces[i] != 0
698 && f->display.x->param_faces[i]->font != (XFontStruct *) FACE_DEFAULT)
699 {
700 int height = FONT_HEIGHT (f->display.x->param_faces[i]->font);
701 if (height > biggest)
702 biggest = height;
703 }
704
705 if (biggest == f->display.x->line_height)
706 return 0;
707
708 f->display.x->line_height = biggest;
709 return 1;
710 }
711 #endif /* not HAVE_X_WINDOWS */
712 \f
713 /* Modify face TO by copying from FROM all properties which have
714 nondefault settings. */
715
716 static void
717 merge_faces (from, to)
718 struct face *from, *to;
719 {
720 /* Only merge the font if it's the same width as the base font.
721 Otherwise ignore it, since we can't handle it properly. */
722 if (from->font != (XFontStruct *) FACE_DEFAULT
723 && same_size_fonts (from->font, to->font))
724 to->font = from->font;
725 if (from->foreground != FACE_DEFAULT)
726 to->foreground = from->foreground;
727 if (from->background != FACE_DEFAULT)
728 to->background = from->background;
729 if (from->stipple != FACE_DEFAULT)
730 {
731 to->stipple = from->stipple;
732 to->pixmap_h = from->pixmap_h;
733 to->pixmap_w = from->pixmap_w;
734 }
735 if (from->underline)
736 to->underline = from->underline;
737 }
738
739 /* Set up the basic set of facial parameters, based on the frame's
740 data; all faces are deltas applied to this. */
741
742 static void
743 compute_base_face (f, face)
744 FRAME_PTR f;
745 struct face *face;
746 {
747 face->gc = 0;
748 face->foreground = FRAME_FOREGROUND_PIXEL (f);
749 face->background = FRAME_BACKGROUND_PIXEL (f);
750 face->font = FRAME_FONT (f);
751 face->stipple = 0;
752 face->underline = 0;
753 }
754
755 /* Return the face ID to use to display a special glyph which selects
756 FACE_CODE as the face ID, assuming that ordinarily the face would
757 be CURRENT_FACE. F is the frame. */
758
759 int
760 compute_glyph_face (f, face_code, current_face)
761 struct frame *f;
762 int face_code, current_face;
763 {
764 struct face face;
765
766 face = *FRAME_COMPUTED_FACES (f)[current_face];
767
768 if (face_code >= 0 && face_code < FRAME_N_PARAM_FACES (f)
769 && FRAME_PARAM_FACES (f) [face_code] != 0)
770 merge_faces (FRAME_PARAM_FACES (f) [face_code], &face);
771
772 return intern_computed_face (f, &face);
773 }
774
775 /* Return the face ID to use to display a special glyph which selects
776 FACE_CODE as the face ID, assuming that ordinarily the face would
777 be CURRENT_FACE. F is the frame. */
778
779 int
780 compute_glyph_face_1 (f, face_name, current_face)
781 struct frame *f;
782 Lisp_Object face_name;
783 int current_face;
784 {
785 struct face face;
786
787 face = *FRAME_COMPUTED_FACES (f)[current_face];
788
789 if (!NILP (face_name))
790 {
791 int facecode = face_name_id_number (f, face_name);
792 if (facecode >= 0 && facecode < FRAME_N_PARAM_FACES (f)
793 && FRAME_PARAM_FACES (f) [facecode] != 0)
794 merge_faces (FRAME_PARAM_FACES (f) [facecode], &face);
795 }
796
797 return intern_computed_face (f, &face);
798 }
799 \f
800 /* Return the face ID associated with a buffer position POS.
801 Store into *ENDPTR the position at which a different face is needed.
802 This does not take account of glyphs that specify their own face codes.
803 F is the frame in use for display, and W is a window displaying
804 the current buffer.
805
806 REGION_BEG, REGION_END delimit the region, so it can be highlighted.
807
808 LIMIT is a position not to scan beyond. That is to limit
809 the time this function can take.
810
811 If MOUSE is nonzero, use the character's mouse-face, not its face. */
812
813 int
814 compute_char_face (f, w, pos, region_beg, region_end, endptr, limit, mouse)
815 struct frame *f;
816 struct window *w;
817 int pos;
818 int region_beg, region_end;
819 int *endptr;
820 int limit;
821 int mouse;
822 {
823 struct face face;
824 Lisp_Object prop, position;
825 int i, j, noverlays;
826 int facecode;
827 Lisp_Object *overlay_vec;
828 Lisp_Object frame;
829 int endpos;
830 Lisp_Object propname;
831
832 /* W must display the current buffer. We could write this function
833 to use the frame and buffer of W, but right now it doesn't. */
834 if (XBUFFER (w->buffer) != current_buffer)
835 abort ();
836
837 XSETFRAME (frame, f);
838
839 endpos = ZV;
840 if (pos < region_beg && region_beg < endpos)
841 endpos = region_beg;
842
843 XSETFASTINT (position, pos);
844
845 if (mouse)
846 propname = Qmouse_face;
847 else
848 propname = Qface;
849
850 prop = Fget_text_property (position, propname, w->buffer);
851
852 {
853 Lisp_Object limit1, end;
854
855 XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
856 end = Fnext_single_property_change (position, propname, w->buffer, limit1);
857 if (INTEGERP (end))
858 endpos = XINT (end);
859 }
860
861 {
862 int next_overlay;
863 int len;
864
865 /* First try with room for 40 overlays. */
866 len = 40;
867 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
868
869 noverlays = overlays_at (pos, 0, &overlay_vec, &len,
870 &next_overlay, (int *) 0);
871
872 /* If there are more than 40,
873 make enough space for all, and try again. */
874 if (noverlays > len)
875 {
876 len = noverlays;
877 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
878 noverlays = overlays_at (pos, 0, &overlay_vec, &len,
879 &next_overlay, (int *) 0);
880 }
881
882 if (next_overlay < endpos)
883 endpos = next_overlay;
884 }
885
886 *endptr = endpos;
887
888 /* Optimize the default case. */
889 if (noverlays == 0 && NILP (prop)
890 && !(pos >= region_beg && pos < region_end))
891 return 0;
892
893 compute_base_face (f, &face);
894
895 if (CONSP (prop))
896 {
897 /* We have a list of faces, merge them in reverse order */
898 Lisp_Object length;
899 int len;
900 Lisp_Object *faces;
901
902 length = Fsafe_length (prop);
903 len = XFASTINT (length);
904
905 /* Put them into an array */
906 faces = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
907 for (j = 0; j < len; j++)
908 {
909 faces[j] = Fcar (prop);
910 prop = Fcdr (prop);
911 }
912 /* So that we can merge them in the reverse order */
913 for (j = len - 1; j >= 0; j--)
914 {
915 facecode = face_name_id_number (f, faces[j]);
916 if (facecode >= 0 && facecode < FRAME_N_PARAM_FACES (f)
917 && FRAME_PARAM_FACES (f) [facecode] != 0)
918 merge_faces (FRAME_PARAM_FACES (f) [facecode], &face);
919 }
920 }
921 else if (!NILP (prop))
922 {
923 facecode = face_name_id_number (f, prop);
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 noverlays = sort_overlays (overlay_vec, noverlays, w);
930
931 /* Now merge the overlay data in that order. */
932 for (i = 0; i < noverlays; i++)
933 {
934 prop = Foverlay_get (overlay_vec[i], propname);
935 if (CONSP (prop))
936 {
937 /* We have a list of faces, merge them in reverse order */
938 Lisp_Object length;
939 int len;
940 Lisp_Object *faces;
941
942 length = Fsafe_length (prop);
943 len = XFASTINT (length);
944
945 /* Put them into an array */
946 faces = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
947 for (j = 0; j < len; j++)
948 {
949 faces[j] = Fcar (prop);
950 prop = Fcdr (prop);
951 }
952 /* So that we can merge them in the reverse order */
953 for (j = len - 1; j >= 0; j--)
954 {
955 facecode = face_name_id_number (f, faces[j]);
956 if (facecode >= 0 && facecode < FRAME_N_PARAM_FACES (f)
957 && FRAME_PARAM_FACES (f) [facecode] != 0)
958 merge_faces (FRAME_PARAM_FACES (f) [facecode], &face);
959 }
960 }
961 else if (!NILP (prop))
962 {
963 Lisp_Object oend;
964 int oendpos;
965
966 facecode = face_name_id_number (f, prop);
967 if (facecode >= 0 && facecode < FRAME_N_PARAM_FACES (f)
968 && FRAME_PARAM_FACES (f) [facecode] != 0)
969 merge_faces (FRAME_PARAM_FACES (f)[facecode], &face);
970
971 oend = OVERLAY_END (overlay_vec[i]);
972 oendpos = OVERLAY_POSITION (oend);
973 if (oendpos < endpos)
974 endpos = oendpos;
975 }
976 }
977
978 if (pos >= region_beg && pos < region_end)
979 {
980 if (region_end < endpos)
981 endpos = region_end;
982 if (region_face >= 0 && region_face < next_face_id)
983 merge_faces (FRAME_PARAM_FACES (f)[region_face], &face);
984 }
985
986 *endptr = endpos;
987
988 return intern_computed_face (f, &face);
989 }
990 \f
991 /* Recompute the GC's for the default and modeline faces.
992 We call this after changing frame parameters on which those GC's
993 depend. */
994
995 void
996 recompute_basic_faces (f)
997 FRAME_PTR f;
998 {
999 /* If the frame's faces haven't been initialized yet, don't worry about
1000 this stuff. */
1001 if (FRAME_N_PARAM_FACES (f) < 2)
1002 return;
1003
1004 BLOCK_INPUT;
1005
1006 if (FRAME_DEFAULT_FACE (f)->gc)
1007 XFreeGC (FRAME_X_DISPLAY (f), FRAME_DEFAULT_FACE (f)->gc);
1008 if (FRAME_MODE_LINE_FACE (f)->gc)
1009 XFreeGC (FRAME_X_DISPLAY (f), FRAME_MODE_LINE_FACE (f)->gc);
1010
1011 compute_base_face (f, FRAME_DEFAULT_FACE (f));
1012 compute_base_face (f, FRAME_MODE_LINE_FACE (f));
1013
1014 merge_faces (FRAME_DEFAULT_PARAM_FACE (f), FRAME_DEFAULT_FACE (f));
1015 merge_faces (FRAME_MODE_LINE_PARAM_FACE (f), FRAME_MODE_LINE_FACE (f));
1016
1017 intern_face (f, FRAME_DEFAULT_FACE (f));
1018 intern_face (f, FRAME_MODE_LINE_FACE (f));
1019
1020 UNBLOCK_INPUT;
1021 }
1022
1023
1024 \f
1025 /* Lisp interface. */
1026
1027 DEFUN ("frame-face-alist", Fframe_face_alist, Sframe_face_alist, 1, 1, 0,
1028 "")
1029 (frame)
1030 Lisp_Object frame;
1031 {
1032 CHECK_FRAME (frame, 0);
1033 return XFRAME (frame)->face_alist;
1034 }
1035
1036 DEFUN ("set-frame-face-alist", Fset_frame_face_alist, Sset_frame_face_alist,
1037 2, 2, 0, "")
1038 (frame, value)
1039 Lisp_Object frame, value;
1040 {
1041 CHECK_FRAME (frame, 0);
1042 XFRAME (frame)->face_alist = value;
1043 return value;
1044 }
1045
1046
1047 DEFUN ("make-face-internal", Fmake_face_internal, Smake_face_internal, 1, 1, 0,
1048 "Create face number FACE-ID on all frames.")
1049 (face_id)
1050 Lisp_Object face_id;
1051 {
1052 Lisp_Object rest, frame;
1053 int id = XINT (face_id);
1054
1055 CHECK_NUMBER (face_id, 0);
1056 if (id < 0 || id >= next_face_id)
1057 error ("Face id out of range");
1058
1059 FOR_EACH_FRAME (rest, frame)
1060 {
1061 if (FRAME_X_P (XFRAME (frame)))
1062 ensure_face_ready (XFRAME (frame), id);
1063 }
1064 return Qnil;
1065 }
1066
1067
1068 DEFUN ("set-face-attribute-internal", Fset_face_attribute_internal,
1069 Sset_face_attribute_internal, 4, 4, 0, "")
1070 (face_id, attr_name, attr_value, frame)
1071 Lisp_Object face_id, attr_name, attr_value, frame;
1072 {
1073 struct face *face;
1074 struct frame *f;
1075 int magic_p;
1076 int id;
1077 int garbaged = 0;
1078
1079 CHECK_FRAME (frame, 0);
1080 CHECK_NUMBER (face_id, 0);
1081 CHECK_SYMBOL (attr_name, 0);
1082
1083 f = XFRAME (frame);
1084 id = XINT (face_id);
1085 if (id < 0 || id >= next_face_id)
1086 error ("Face id out of range");
1087
1088 if (! FRAME_X_P (f))
1089 return Qnil;
1090
1091 ensure_face_ready (f, id);
1092 face = FRAME_PARAM_FACES (f) [XFASTINT (face_id)];
1093
1094 if (EQ (attr_name, intern ("font")))
1095 {
1096 #if defined (MSDOS) && !defined (HAVE_X_WINDOWS)
1097 face->font = 0; /* The one and only font. */
1098 #else
1099 XFontStruct *font = load_font (f, attr_value);
1100 if (face->font != f->display.x->font)
1101 unload_font (f, face->font);
1102 face->font = font;
1103 if (frame_update_line_height (f))
1104 x_set_window_size (f, 0, f->width, f->height);
1105 /* Must clear cache, since it might contain the font
1106 we just got rid of. */
1107 garbaged = 1;
1108 #endif
1109 }
1110 else if (EQ (attr_name, intern ("foreground")))
1111 {
1112 unsigned long new_color = load_color (f, attr_value);
1113 unload_color (f, face->foreground);
1114 face->foreground = new_color;
1115 garbaged = 1;
1116 }
1117 else if (EQ (attr_name, intern ("background")))
1118 {
1119 unsigned long new_color = load_color (f, attr_value);
1120 unload_color (f, face->background);
1121 #if defined (MSDOS) && !defined (HAVE_X_WINDOWS)
1122 new_color &= ~8; /* Bright would give blinking characters. */
1123 #endif
1124 face->background = new_color;
1125 garbaged = 1;
1126 }
1127 else if (EQ (attr_name, intern ("background-pixmap")))
1128 {
1129 unsigned int w, h;
1130 unsigned long new_pixmap = load_pixmap (f, attr_value, &w, &h);
1131 x_destroy_bitmap (f, face->stipple);
1132 face->stipple = new_pixmap;
1133 face->pixmap_w = w;
1134 face->pixmap_h = h;
1135 garbaged = 1;
1136 }
1137 else if (EQ (attr_name, intern ("underline")))
1138 {
1139 int new = !NILP (attr_value);
1140 face->underline = new;
1141 }
1142 else
1143 error ("unknown face attribute");
1144
1145 if (id == 0 || id == 1)
1146 recompute_basic_faces (f);
1147
1148 /* We must redraw the frame whenever any face font or color changes,
1149 because it's possible that a merged (display) face
1150 contains the font or color we just replaced.
1151 And we must inhibit any Expose events until the redraw is done,
1152 since they would try to use the invalid display faces. */
1153 if (garbaged)
1154 SET_FRAME_GARBAGED (f);
1155
1156 return Qnil;
1157 }
1158
1159 DEFUN ("internal-next-face-id", Finternal_next_face_id, Sinternal_next_face_id,
1160 0, 0, 0, "")
1161 ()
1162 {
1163 return make_number (next_face_id++);
1164 }
1165
1166 /* Return the face id for name NAME on frame FRAME.
1167 (It should be the same for all frames,
1168 but it's as easy to use the "right" frame to look it up
1169 as to use any other one.) */
1170
1171 int
1172 face_name_id_number (f, name)
1173 FRAME_PTR f;
1174 Lisp_Object name;
1175 {
1176 Lisp_Object tem;
1177
1178 tem = Fcdr (assq_no_quit (name, f->face_alist));
1179 if (NILP (tem))
1180 return 0;
1181 CHECK_VECTOR (tem, 0);
1182 tem = XVECTOR (tem)->contents[2];
1183 CHECK_NUMBER (tem, 0);
1184 return XINT (tem);
1185 }
1186 \f
1187 /* Emacs initialization. */
1188
1189 void
1190 syms_of_xfaces ()
1191 {
1192 Qface = intern ("face");
1193 staticpro (&Qface);
1194 Qmouse_face = intern ("mouse-face");
1195 staticpro (&Qmouse_face);
1196 Qpixmap_spec_p = intern ("pixmap-spec-p");
1197 staticpro (&Qpixmap_spec_p);
1198
1199 DEFVAR_INT ("region-face", &region_face,
1200 "Face number to use to highlight the region\n\
1201 The region is highlighted with this face\n\
1202 when Transient Mark mode is enabled and the mark is active.");
1203
1204 #ifdef HAVE_X_WINDOWS
1205 defsubr (&Spixmap_spec_p);
1206 #endif
1207 defsubr (&Sframe_face_alist);
1208 defsubr (&Sset_frame_face_alist);
1209 defsubr (&Smake_face_internal);
1210 defsubr (&Sset_face_attribute_internal);
1211 defsubr (&Sinternal_next_face_id);
1212 }
1213
1214 #endif /* HAVE_FACES */