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