(redisplay, mark_window_display_accurate, redisplay_window, try_window,
[bpt/emacs.git] / src / xfaces.c
CommitLineData
cb637678 1/* "Face" primitives.
3a22ee35 2 Copyright (C) 1993, 1994 Free Software Foundation.
7b7739b1 3
c115973b
JB
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
7b7739b1 8the Free Software Foundation; either version 2, or (at your option)
c115973b
JB
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
357f32fc 20/* This is derived from work by Lucid (some parts very loosely so). */
7b7739b1 21
c115973b
JB
22#include <sys/types.h>
23#include <sys/stat.h>
24
18160b98 25#include <config.h>
c115973b
JB
26#include "lisp.h"
27
cb637678
JB
28#ifdef HAVE_X_WINDOWS
29
c115973b
JB
30#include "xterm.h"
31#include "buffer.h"
f211082d 32#include "dispextern.h"
7b7739b1 33#include "frame.h"
357f32fc 34#include "blockinput.h"
b6d40e46 35#include "window.h"
bde7c500 36#include "intervals.h"
c115973b 37
657070ac
JB
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. */
6233db35 42#ifdef XOS_NEEDS_TIME_H
657070ac 43
e11d186d 44#include <time.h>
657070ac
JB
45#undef USG
46#include <X11/Xos.h>
47#define USG
e11d186d 48#define __TIMEVAL__
e11d186d 49
657070ac 50#else
7a4d2269 51
c115973b
JB
52#include <X11/Xos.h>
53
657070ac
JB
54#endif
55
cb637678
JB
56\f
57/* An explanation of the face data structures. */
58
59/* ========================= Face Data Structures =========================
60
7b00de84 61 Let FACE-NAME be a symbol naming a face.
cb637678 62
7b00de84
JB
63 Let FACE-VECTOR be (assq FACE-NAME (frame-face-alist FRAME))
64 FACE-VECTOR is either nil, or a vector of the form
cb637678
JB
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.
7b00de84
JB
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.
cb637678
JB
126
127 Some subtleties:
7b7739b1 128
7b00de84
JB
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
cb637678
JB
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
7b00de84 146 We're often applying intern_face to faces in computed_faces -
cb637678
JB
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
c115973b 157/* A table of display faces. */
7b00de84 158static struct face **face_vector;
c115973b 159/* The length in use of the table. */
7b00de84 160static int nfaces;
c115973b 161/* The allocated length of the table. */
7b00de84 162static int nfaces_allocated;
c115973b
JB
163
164/* The number of face-id's in use (same for all frames). */
165int next_face_id;
166
bc0db68d
RS
167/* The number of the face to use to indicate the region. */
168int region_face;
169
9516fe94
RS
170/* This is what appears in a slot in a face to signify that the face
171 does not specify that display aspect. */
7b7739b1
JB
172#define FACE_DEFAULT (~0)
173
6f134486 174Lisp_Object Qface, Qmouse_face;
f211082d 175
415445e2
JB
176static void build_face ( /* FRAME_PTR, struct face * */ );
177int face_name_id_number ( /* FRAME_PTR, Lisp_Object name */ );
cb637678 178
415445e2 179struct face *intern_face ( /* FRAME_PTR, struct face * */ );
7b00de84
JB
180static int new_computed_face ( /* FRAME_PTR, struct face * */ );
181static int intern_computed_face ( /* FRAME_PTR, struct face * */ );
415445e2 182static void ensure_face_ready ( /* FRAME_PTR, int id */ );
254a1b66 183void recompute_basic_faces ( /* FRAME_PTR f */ );
c115973b 184\f
cb637678
JB
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}
c115973b 199
cb637678 200/* Make a new face that's a copy of an existing one. */
c115973b
JB
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;
f211082d 210 result->stipple = face->stipple;
c115973b
JB
211 result->underline = face->underline;
212
213 return result;
214}
215
216static int
217face_eql (face1, face2)
218 struct face *face1, *face2;
219{
cb637678 220 return ( face1->font == face2->font
c115973b
JB
221 && face1->foreground == face2->foreground
222 && face1->background == face2->background
cb637678
JB
223 && face1->stipple == face2->stipple
224 && face1->underline == face2->underline);
c115973b 225}
cb637678
JB
226\f
227/* Interning faces in the `face_vector' cache, and clearing that cache. */
c115973b
JB
228
229/* Return the unique display face corresponding to the user-level face FACE.
c115973b
JB
230 If there isn't one, make one, and find a slot in the face_vector to
231 put it in. */
c115973b 232static struct face *
7b7739b1 233get_cached_face (f, face)
c115973b
JB
234 struct frame *f;
235 struct face *face;
236{
237 int i, empty = -1;
f211082d 238 struct face *result;
c115973b 239
cb637678
JB
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
c115973b
JB
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))
90e6242c
RS
252 {
253 face->cached_index = i;
254 return face_vector[i];
255 }
c115973b
JB
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
90e6242c 280 face->cached_index = empty;
c115973b
JB
281 return result;
282}
283
7b00de84 284/* Given a computed face, return an equivalent display face
cb637678
JB
285 (one which has a graphics context). */
286
287struct face *
288intern_face (f, face)
289 struct frame *f;
290 struct face *face;
291{
cb637678
JB
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
f6b98e0b
JB
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
cb637678
JB
312 /* Get a specialized display face. */
313 return get_cached_face (f, face);
314}
315
c115973b
JB
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;
f211082d 326 int i;
c115973b
JB
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];
f211082d
JB
333 if (face->gc)
334 XFreeGC (dpy, face->gc);
c115973b
JB
335 xfree (face);
336 }
337 nfaces = 0;
338
339 UNBLOCK_INPUT;
340}
341\f
cb637678
JB
342/* Allocating and freeing X resources for display faces. */
343
f211082d
JB
344/* Make a graphics context for face FACE, which is on frame F,
345 if that can be done. */
c115973b
JB
346static void
347build_face (f, face)
cb637678
JB
348 struct frame *f;
349 struct face *face;
c115973b
JB
350{
351 GC gc;
352 XGCValues xgcv;
353 unsigned long mask;
354
660ed669
JB
355 BLOCK_INPUT;
356
f211082d
JB
357 if (face->foreground != FACE_DEFAULT)
358 xgcv.foreground = face->foreground;
359 else
660ed669
JB
360 xgcv.foreground = f->display.x->foreground_pixel;
361
f211082d
JB
362 if (face->background != FACE_DEFAULT)
363 xgcv.background = face->background;
364 else
660ed669
JB
365 xgcv.background = f->display.x->background_pixel;
366
f211082d
JB
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;
660ed669 371
c115973b 372 xgcv.graphics_exposures = 0;
660ed669 373
c115973b
JB
374 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
375 gc = XCreateGC (x_current_display, FRAME_X_WINDOW (f),
376 mask, &xgcv);
660ed669 377
c115973b 378#if 0
f211082d
JB
379 if (face->stipple && face->stipple != FACE_DEFAULT)
380 XSetStipple (x_current_display, gc, face->stipple);
c115973b 381#endif
660ed669 382
f211082d 383 face->gc = gc;
660ed669
JB
384
385 UNBLOCK_INPUT;
c115973b 386}
cb637678
JB
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;
660ed669
JB
418
419 BLOCK_INPUT;
cb637678 420 XFreeFont (x_current_display, font);
660ed669 421 UNBLOCK_INPUT;
cb637678
JB
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;
7a4d2269 458 unsigned long pixel;
cb637678 459{
1d9fd7fe
JB
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
cb637678
JB
467 Colormap cmap;
468 Display *dpy = x_current_display;
32dc0866
JB
469 if (pixel == FACE_DEFAULT
470 || pixel == BLACK_PIX_DEFAULT
471 || pixel == WHITE_PIX_DEFAULT)
cb637678
JB
472 return;
473 cmap = DefaultColormapOfScreen (DefaultScreenOfDisplay (x_current_display));
474 BLOCK_INPUT;
475 XFreeColors (dpy, cmap, &pixel, 1, 0);
476 UNBLOCK_INPUT;
1d9fd7fe 477#endif
cb637678
JB
478}
479\f
7b00de84 480/* Managing parameter face arrays for frames. */
cb637678 481
cb637678
JB
482void
483init_frame_faces (f)
660ed669 484 FRAME_PTR f;
cb637678
JB
485{
486 ensure_face_ready (f, 0);
cb637678 487 ensure_face_ready (f, 1);
660ed669 488
fffc2367
RS
489 FRAME_N_COMPUTED_FACES (f) = 0;
490 FRAME_SIZE_COMPUTED_FACES (f) = 0;
491
7b00de84
JB
492 new_computed_face (f, FRAME_PARAM_FACES (f)[0]);
493 new_computed_face (f, FRAME_PARAM_FACES (f)[1]);
660ed669 494 recompute_basic_faces (f);
7b37f67b 495
1120eb5e
JB
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;
7b00de84
JB
516 int n_faces = FRAME_N_PARAM_FACES (XFRAME (result));
517 struct face **faces = FRAME_PARAM_FACES (XFRAME (result));
1120eb5e
JB
518
519 for (i = 2; i < n_faces; i++)
520 if (faces[i])
521 ensure_face_ready (f, i);
522 }
523 }
cb637678
JB
524}
525
660ed669 526
cb637678
JB
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
660ed669
JB
535 BLOCK_INPUT;
536
7b00de84 537 for (i = 0; i < FRAME_N_PARAM_FACES (f); i++)
cb637678 538 {
7b00de84 539 struct face *face = FRAME_PARAM_FACES (f) [i];
660ed669
JB
540 if (face)
541 {
542 if (face->gc)
543 XFreeGC (dpy, face->gc);
7b00de84
JB
544 unload_font (f, face->font);
545 unload_color (f, face->foreground);
546 unload_color (f, face->background);
cb637678 547#if 0
7b00de84 548 unload_pixmap (f, face->stipple);
cb637678 549#endif
660ed669
JB
550 xfree (face);
551 }
cb637678 552 }
7b00de84
JB
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;
660ed669
JB
562
563 UNBLOCK_INPUT;
cb637678 564}
c115973b 565\f
cb637678
JB
566/* Interning faces in a frame's face array. */
567
660ed669 568static int
7b00de84
JB
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)
fffc2367
RS
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 *)));
7b00de84
JB
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;
b6d40e46 598 struct face *new_face;
cb637678 599{
7b00de84 600 int len = FRAME_N_COMPUTED_FACES (f);
cb637678
JB
601 int i;
602
7b00de84 603 /* Search for a computed face already on F equivalent to FACE. */
cb637678
JB
604 for (i = 0; i < len; i++)
605 {
7b00de84
JB
606 if (! FRAME_COMPUTED_FACES (f)[i])
607 abort ();
608 if (face_eql (new_face, FRAME_COMPUTED_FACES (f)[i]))
cb637678
JB
609 return i;
610 }
611
612 /* We didn't find one; add a new one. */
7b00de84 613 return new_computed_face (f, new_face);
cb637678
JB
614}
615
7b00de84 616/* Make parameter face id ID valid on frame F. */
cb637678
JB
617
618static void
619ensure_face_ready (f, id)
620 struct frame *f;
621 int id;
622{
7b00de84 623 if (FRAME_N_PARAM_FACES (f) <= id)
cb637678
JB
624 {
625 int n = id + 10;
626 int i;
7b00de84
JB
627 if (!FRAME_N_PARAM_FACES (f))
628 FRAME_PARAM_FACES (f)
cb637678
JB
629 = (struct face **) xmalloc (sizeof (struct face *) * n);
630 else
7b00de84
JB
631 FRAME_PARAM_FACES (f)
632 = (struct face **) xrealloc (FRAME_PARAM_FACES (f),
cb637678
JB
633 sizeof (struct face *) * n);
634
7b00de84
JB
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;
cb637678
JB
638 }
639
7b00de84
JB
640 if (FRAME_PARAM_FACES (f) [id] == 0)
641 FRAME_PARAM_FACES (f) [id] = allocate_face ();
cb637678
JB
642}
643\f
867dd159
RS
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.
660ed669 647 We assume that they're both character-cell fonts. */
867dd159 648
660ed669
JB
649int
650same_size_fonts (font1, font2)
651 XFontStruct *font1, *font2;
652{
653 XCharStruct *bounds1 = &font1->min_bounds;
654 XCharStruct *bounds2 = &font2->min_bounds;
655
46407861 656 return (bounds1->width == bounds2->width);
660ed669
JB
657}
658
729425b1 659/* Update the line_height of frame F according to the biggest font in
6717f691 660 any face. Return nonzero if if line_height changes. */
729425b1 661
6717f691 662int
729425b1
RS
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++)
867dd159
RS
670 if (f->display.x->param_faces[i] != 0
671 && f->display.x->param_faces[i]->font != (XFontStruct *) FACE_DEFAULT)
729425b1
RS
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)
6717f691 679 return 0;
729425b1
RS
680
681 f->display.x->line_height = biggest;
6717f691 682 return 1;
729425b1 683}
867dd159 684\f
7b7739b1
JB
685/* Modify face TO by copying from FROM all properties which have
686 nondefault settings. */
867dd159 687
7b7739b1
JB
688static void
689merge_faces (from, to)
690 struct face *from, *to;
691{
867dd159
RS
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. */
68a97335 694 if (from->font != (XFontStruct *) FACE_DEFAULT
68a97335
JB
695 && same_size_fonts (from->font, to->font))
696 to->font = from->font;
7b7739b1
JB
697 if (from->foreground != FACE_DEFAULT)
698 to->foreground = from->foreground;
699 if (from->background != FACE_DEFAULT)
700 to->background = from->background;
f211082d
JB
701 if (from->stipple != FACE_DEFAULT)
702 to->stipple = from->stipple;
7b7739b1
JB
703 if (from->underline)
704 to->underline = from->underline;
705}
706
660ed669
JB
707/* Set up the basic set of facial parameters, based on the frame's
708 data; all faces are deltas applied to this. */
867dd159 709
660ed669
JB
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;
582080c1 721 face->stipple = 0;
660ed669 722 face->underline = 0;
582080c1
JB
723
724 /* Avoid a face comparison by making this invalid. */
725 face->cached_index = -1;
660ed669
JB
726}
727
867dd159
RS
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);
660ed669 744
867dd159
RS
745 return intern_computed_face (f, &face);
746}
2e16580f
RS
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}
867dd159 772\f
cb637678 773/* Return the face ID associated with a buffer position POS.
7b7739b1
JB
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.
f6b98e0b 776 F is the frame in use for display, and W is a window displaying
bc0db68d
RS
777 the current buffer.
778
b349f4fb
RS
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
6f134486
RS
782 the time this function can take.
783
784 If MOUSE is nonzero, use the character's mouse-face, not its face. */
bc0db68d 785
cb637678 786int
6f134486 787compute_char_face (f, w, pos, region_beg, region_end, endptr, limit, mouse)
7b7739b1 788 struct frame *f;
f211082d 789 struct window *w;
7b7739b1 790 int pos;
bc0db68d 791 int region_beg, region_end;
7b7739b1 792 int *endptr;
b349f4fb 793 int limit;
6f134486 794 int mouse;
7b7739b1
JB
795{
796 struct face face;
b6d40e46 797 Lisp_Object prop, position;
7b7739b1
JB
798 int i, j, noverlays;
799 int facecode;
7b7739b1 800 Lisp_Object *overlay_vec;
f211082d 801 Lisp_Object frame;
f6b98e0b 802 int endpos;
6f134486 803 Lisp_Object propname;
f6b98e0b
JB
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 ();
f211082d 809
ac22a6c4 810 XSETFRAME (frame, f);
7b7739b1 811
f6b98e0b 812 endpos = ZV;
bc0db68d
RS
813 if (pos < region_beg && region_beg < endpos)
814 endpos = region_beg;
f6b98e0b 815
7b7739b1 816 XFASTINT (position) = pos;
6f134486
RS
817
818 if (mouse)
819 propname = Qmouse_face;
820 else
821 propname = Qface;
822
823 prop = Fget_text_property (position, propname, w->buffer);
824
b6d40e46 825 {
b349f4fb 826 Lisp_Object limit1, end;
7b7739b1 827
b349f4fb 828 XFASTINT (limit1) = (limit < endpos ? limit : endpos);
6f134486 829 end = Fnext_single_property_change (position, propname, w->buffer, limit1);
b6d40e46
JB
830 if (INTEGERP (end))
831 endpos = XINT (end);
832 }
833
834 {
f6b98e0b 835 int next_overlay;
9516fe94
RS
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
82277c2f 842 noverlays = overlays_at (pos, 0, &overlay_vec, &len, &next_overlay, NULL);
9516fe94
RS
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));
82277c2f
RS
850 noverlays = overlays_at (pos, 0, &overlay_vec, &len,
851 &next_overlay, NULL);
9516fe94 852 }
b6d40e46 853
f6b98e0b
JB
854 if (next_overlay < endpos)
855 endpos = next_overlay;
b6d40e46
JB
856 }
857
858 *endptr = endpos;
7b7739b1
JB
859
860 /* Optimize the default case. */
bc0db68d
RS
861 if (noverlays == 0 && NILP (prop)
862 && !(pos >= region_beg && pos < region_end))
cb637678 863 return 0;
7b7739b1 864
660ed669 865 compute_base_face (f, &face);
7b7739b1 866
4699e6d2
RS
867 if (CONSP (prop))
868 {
869 /* We have a list of faces, merge them in reverse order */
870 Lisp_Object length = Flength (prop);
871 int len = XINT (length);
872 Lisp_Object *faces;
873
874 /* Put them into an array */
875 faces = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
876 for (j = 0; j < len; j++)
877 {
878 faces[j] = Fcar (prop);
879 prop = Fcdr (prop);
880 }
881 /* So that we can merge them in the reverse order */
882 for (j = len - 1; j >= 0; j--)
883 {
884 facecode = face_name_id_number (f, faces[j]);
885 if (facecode >= 0 && facecode < FRAME_N_PARAM_FACES (f)
886 && FRAME_PARAM_FACES (f) [facecode] != 0)
887 merge_faces (FRAME_PARAM_FACES (f) [facecode], &face);
888 }
889 }
890 else if (!NILP (prop))
7b7739b1 891 {
1d9fd7fe 892 facecode = face_name_id_number (f, prop);
7b00de84
JB
893 if (facecode >= 0 && facecode < FRAME_N_PARAM_FACES (f)
894 && FRAME_PARAM_FACES (f) [facecode] != 0)
895 merge_faces (FRAME_PARAM_FACES (f) [facecode], &face);
7b7739b1
JB
896 }
897
4d6ebff8 898 noverlays = sort_overlays (overlay_vec, noverlays, w);
7b7739b1
JB
899
900 /* Now merge the overlay data in that order. */
7b7739b1
JB
901 for (i = 0; i < noverlays; i++)
902 {
6f134486 903 prop = Foverlay_get (overlay_vec[i], propname);
4699e6d2
RS
904 if (CONSP (prop))
905 {
906 /* We have a list of faces, merge them in reverse order */
907 Lisp_Object length = Flength (prop);
908 int len = XINT (length);
909 Lisp_Object *faces;
910 int i;
911
912 /* Put them into an array */
913 faces = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
914 for (j = 0; j < len; j++)
915 {
916 faces[j] = Fcar (prop);
917 prop = Fcdr (prop);
918 }
919 /* So that we can merge them in the reverse order */
920 for (j = len - 1; j >= 0; j--)
921 {
922 facecode = face_name_id_number (f, faces[j]);
923 if (facecode >= 0 && facecode < FRAME_N_PARAM_FACES (f)
924 && FRAME_PARAM_FACES (f) [facecode] != 0)
925 merge_faces (FRAME_PARAM_FACES (f) [facecode], &face);
926 }
927 }
928 else if (!NILP (prop))
7b7739b1
JB
929 {
930 Lisp_Object oend;
931 int oendpos;
932
1d9fd7fe 933 facecode = face_name_id_number (f, prop);
7b00de84
JB
934 if (facecode >= 0 && facecode < FRAME_N_PARAM_FACES (f)
935 && FRAME_PARAM_FACES (f) [facecode] != 0)
6f134486 936 merge_faces (FRAME_PARAM_FACES (f)[facecode], &face);
7b7739b1 937
4d6ebff8 938 oend = OVERLAY_END (overlay_vec[i]);
7b7739b1 939 oendpos = OVERLAY_POSITION (oend);
f6b98e0b 940 if (oendpos < endpos)
7b7739b1
JB
941 endpos = oendpos;
942 }
943 }
944
bc0db68d
RS
945 if (pos >= region_beg && pos < region_end)
946 {
947 if (region_end < endpos)
948 endpos = region_end;
949 if (region_face >= 0 && region_face < next_face_id)
6f134486 950 merge_faces (FRAME_PARAM_FACES (f)[region_face], &face);
bc0db68d
RS
951 }
952
7b7739b1
JB
953 *endptr = endpos;
954
7b00de84 955 return intern_computed_face (f, &face);
f211082d 956}
867dd159 957\f
660ed669
JB
958/* Recompute the GC's for the default and modeline faces.
959 We call this after changing frame parameters on which those GC's
960 depend. */
fffc2367 961
660ed669
JB
962void
963recompute_basic_faces (f)
964 FRAME_PTR f;
965{
966 /* If the frame's faces haven't been initialized yet, don't worry about
967 this stuff. */
7b00de84 968 if (FRAME_N_PARAM_FACES (f) < 2)
660ed669
JB
969 return;
970
971 BLOCK_INPUT;
972
973 if (FRAME_DEFAULT_FACE (f)->gc)
974 XFreeGC (x_current_display, FRAME_DEFAULT_FACE (f)->gc);
660ed669
JB
975 if (FRAME_MODE_LINE_FACE (f)->gc)
976 XFreeGC (x_current_display, FRAME_MODE_LINE_FACE (f)->gc);
7b00de84
JB
977
978 compute_base_face (f, FRAME_DEFAULT_FACE (f));
979 compute_base_face (f, FRAME_MODE_LINE_FACE (f));
980
981 merge_faces (FRAME_DEFAULT_PARAM_FACE (f), FRAME_DEFAULT_FACE (f));
982 merge_faces (FRAME_MODE_LINE_PARAM_FACE (f), FRAME_MODE_LINE_FACE (f));
983
984 build_face (f, FRAME_DEFAULT_FACE (f));
660ed669
JB
985 build_face (f, FRAME_MODE_LINE_FACE (f));
986
987 UNBLOCK_INPUT;
988}
989
990
c115973b 991\f
cb637678 992/* Lisp interface. */
c115973b
JB
993
994DEFUN ("frame-face-alist", Fframe_face_alist, Sframe_face_alist, 1, 1, 0,
995 "")
996 (frame)
997 Lisp_Object frame;
998{
999 CHECK_FRAME (frame, 0);
1000 return XFRAME (frame)->face_alist;
1001}
1002
1003DEFUN ("set-frame-face-alist", Fset_frame_face_alist, Sset_frame_face_alist,
1004 2, 2, 0, "")
1005 (frame, value)
1006 Lisp_Object frame, value;
1007{
1008 CHECK_FRAME (frame, 0);
1009 XFRAME (frame)->face_alist = value;
1010 return value;
1011}
1012
1013
1014DEFUN ("make-face-internal", Fmake_face_internal, Smake_face_internal, 1, 1, 0,
1015 "Create face number FACE-ID on all frames.")
1016 (face_id)
1017 Lisp_Object face_id;
1018{
1019 Lisp_Object rest;
1020 int id = XINT (face_id);
1021
f211082d
JB
1022 CHECK_NUMBER (face_id, 0);
1023 if (id < 0 || id >= next_face_id)
1024 error ("Face id out of range");
c115973b
JB
1025
1026 for (rest = Vframe_list; !NILP (rest); rest = XCONS (rest)->cdr)
1027 {
1028 struct frame *f = XFRAME (XCONS (rest)->car);
cb637678
JB
1029 if (FRAME_X_P (f))
1030 ensure_face_ready (f, id);
c115973b
JB
1031 }
1032 return Qnil;
1033}
1034
1035
1036DEFUN ("set-face-attribute-internal", Fset_face_attribute_internal,
1037 Sset_face_attribute_internal, 4, 4, 0, "")
1038 (face_id, attr_name, attr_value, frame)
1039 Lisp_Object face_id, attr_name, attr_value, frame;
1040{
1041 struct face *face;
1042 struct frame *f;
1043 int magic_p;
1044 int id;
53dfb388 1045 int garbaged = 0;
c115973b
JB
1046
1047 CHECK_FRAME (frame, 0);
f211082d 1048 CHECK_NUMBER (face_id, 0);
c115973b
JB
1049 CHECK_SYMBOL (attr_name, 0);
1050
1051 f = XFRAME (frame);
1052 id = XINT (face_id);
f211082d
JB
1053 if (id < 0 || id >= next_face_id)
1054 error ("Face id out of range");
c115973b 1055
b6d40e46 1056 if (! FRAME_X_P (f))
b839cea0 1057 return Qnil;
b6d40e46 1058
c115973b 1059 ensure_face_ready (f, id);
7b00de84 1060 face = FRAME_PARAM_FACES (f) [XFASTINT (face_id)];
c115973b
JB
1061
1062 if (EQ (attr_name, intern ("font")))
1063 {
f211082d 1064 XFontStruct *font = load_font (f, attr_value);
ebc64af3
RS
1065 if (face->font != f->display.x->font)
1066 unload_font (f, face->font);
c115973b 1067 face->font = font;
6717f691
RS
1068 if (frame_update_line_height (f))
1069 x_set_window_size (f, 0, f->width, f->height);
53dfb388
RS
1070 /* Must clear cache, since it might contain the font
1071 we just got rid of. */
1072 garbaged = 1;
c115973b
JB
1073 }
1074 else if (EQ (attr_name, intern ("foreground")))
1075 {
f211082d 1076 unsigned long new_color = load_color (f, attr_value);
c115973b
JB
1077 unload_color (f, face->foreground);
1078 face->foreground = new_color;
53dfb388 1079 garbaged = 1;
c115973b
JB
1080 }
1081 else if (EQ (attr_name, intern ("background")))
1082 {
f211082d 1083 unsigned long new_color = load_color (f, attr_value);
c115973b
JB
1084 unload_color (f, face->background);
1085 face->background = new_color;
53dfb388 1086 garbaged = 1;
c115973b
JB
1087 }
1088#if 0
1089 else if (EQ (attr_name, intern ("background-pixmap")))
1090 {
c115973b
JB
1091 unsigned int w, h, d;
1092 unsigned long new_pixmap = load_pixmap (f, attr_value, &w, &h, &d, 0);
f211082d
JB
1093 unload_pixmap (f, face->stipple);
1094 if (NILP (attr_value))
1095 new_pixmap = 0;
1096 face->stipple = new_pixmap;
c115973b
JB
1097 face->pixmap_w = w;
1098 face->pixmap_h = h;
1099/* face->pixmap_depth = d; */
c115973b
JB
1100 }
1101#endif /* 0 */
1102 else if (EQ (attr_name, intern ("underline")))
1103 {
1104 int new = !NILP (attr_value);
1105 face->underline = new;
1106 }
1107 else
1108 error ("unknown face attribute");
1109
7b00de84
JB
1110 if (id == 0 || id == 1)
1111 recompute_basic_faces (f);
c115973b 1112
53dfb388
RS
1113 /* We must redraw the frame whenever any face font or color changes,
1114 because it's possible that a merged (display) face
1115 contains the font or color we just replaced.
1116 And we must inhibit any Expose events until the redraw is done,
1117 since they would try to use the invalid display faces. */
1118 if (garbaged)
584d0634 1119 SET_FRAME_GARBAGED (f);
584d0634 1120
c115973b
JB
1121 return Qnil;
1122}
1123
1124DEFUN ("internal-next-face-id", Finternal_next_face_id, Sinternal_next_face_id,
1125 0, 0, 0, "")
1126 ()
1127{
1128 return make_number (next_face_id++);
1129}
f211082d
JB
1130
1131/* Return the face id for name NAME on frame FRAME.
1132 (It should be the same for all frames,
1133 but it's as easy to use the "right" frame to look it up
1134 as to use any other one.) */
1135
1d9fd7fe
JB
1136int
1137face_name_id_number (f, name)
1138 FRAME_PTR f;
1139 Lisp_Object name;
f211082d
JB
1140{
1141 Lisp_Object tem;
1142
82411f65 1143 tem = Fcdr (assq_no_quit (name, f->face_alist));
b6d40e46
JB
1144 if (NILP (tem))
1145 return 0;
f211082d
JB
1146 CHECK_VECTOR (tem, 0);
1147 tem = XVECTOR (tem)->contents[2];
1148 CHECK_NUMBER (tem, 0);
1149 return XINT (tem);
1150}
c115973b 1151\f
cb637678
JB
1152/* Emacs initialization. */
1153
c115973b 1154void
f211082d 1155syms_of_xfaces ()
c115973b 1156{
f211082d
JB
1157 Qface = intern ("face");
1158 staticpro (&Qface);
6f134486
RS
1159 Qmouse_face = intern ("mouse-face");
1160 staticpro (&Qmouse_face);
f211082d 1161
bc0db68d
RS
1162 DEFVAR_INT ("region-face", &region_face,
1163 "Face number to use to highlight the region\n\
1164The region is highlighted with this face\n\
1165when Transient Mark mode is enabled and the mark is active.");
1166
c115973b
JB
1167 defsubr (&Sframe_face_alist);
1168 defsubr (&Sset_frame_face_alist);
1169 defsubr (&Smake_face_internal);
1170 defsubr (&Sset_face_attribute_internal);
1171 defsubr (&Sinternal_next_face_id);
1172}
cb637678
JB
1173
1174#endif /* HAVE_X_WINDOWS */
1175