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