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