(menu-bar-help-menu): Binding for view-emacs-FAQ.
[emacs.git] / src / xfaces.c
blob5fe6d2092c5c6f1b7750b9f34026bc8bf7dbf3d8
1 /* "Face" primitives.
2 Copyright (C) 1993, 1994 Free Software Foundation.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20 /* This is derived from work by Lucid (some parts very loosely so). */
22 #include <sys/types.h>
23 #include <sys/stat.h>
25 #include <config.h>
26 #include "lisp.h"
28 #ifdef HAVE_FACES
30 #ifdef HAVE_X_WINDOWS
31 #include "xterm.h"
32 #endif
33 #ifdef MSDOS
34 #include "dosfns.h"
35 #endif
36 #include "buffer.h"
37 #include "dispextern.h"
38 #include "frame.h"
39 #include "blockinput.h"
40 #include "window.h"
41 #include "intervals.h"
43 #ifdef HAVE_X_WINDOWS
44 /* Compensate for bug in Xos.h on some systems, on which it requires
45 time.h. On some such systems, Xos.h tries to redefine struct
46 timeval and struct timezone if USG is #defined while it is
47 #included. */
48 #ifdef XOS_NEEDS_TIME_H
50 #include <time.h>
51 #undef USG
52 #include <X11/Xos.h>
53 #define USG
54 #define __TIMEVAL__
56 #else
58 #include <X11/Xos.h>
60 #endif
61 #endif /* HAVE_X_WINDOWS */
63 /* An explanation of the face data structures. */
65 /* ========================= Face Data Structures =========================
67 Let FACE-NAME be a symbol naming a face.
69 Let FACE-VECTOR be (assq FACE-NAME (frame-face-alist FRAME))
70 FACE-VECTOR is either nil, or a vector of the form
71 [face NAME ID FONT FOREGROUND BACKGROUND BACKGROUND-PIXMAP UNDERLINE-P]
72 where
73 face is the symbol `face',
74 NAME is the symbol with which this vector is associated (a backpointer),
75 ID is the face ID, an integer used internally by the C code to identify
76 the face,
77 FONT, FOREGROUND, and BACKGROUND are strings naming the fonts and colors
78 to use with the face,
79 BACKGROUND-PIXMAP is the name of an x bitmap filename, which we don't
80 use right now, and
81 UNDERLINE-P is non-nil if the face should be underlined.
82 If any of these elements are nil, that parameter is considered
83 unspecified; parameters from faces specified by lower-priority
84 overlays or text properties, or the parameters of the frame itself,
85 can show through. (lisp/faces.el maintains these lists.)
87 (assq FACE-NAME global-face-data) returns a vector describing the
88 global parameters for that face.
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.)
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.
115 Each computed face belongs to a particular frame.
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.
124 Constraints:
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.
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.
135 Some subtleties:
137 Why do we keep param_faces and computed_faces separate?
138 computed_faces contains an element for every combination of facial
139 parameters we have ever displayed. indices into param_faces have
140 to be valid on all frames. If they were the same array, then that
141 array would grow very large on all frames, because any facial
142 combination displayed on any frame would need to be a valid entry
143 on all frames. */
145 /* Definitions and declarations. */
147 /* The number of face-id's in use (same for all frames). */
148 int next_face_id;
150 /* The number of the face to use to indicate the region. */
151 int region_face;
153 /* This is what appears in a slot in a face to signify that the face
154 does not specify that display aspect. */
155 #define FACE_DEFAULT (~0)
157 Lisp_Object Qface, Qmouse_face;
158 Lisp_Object Qpixmap_spec_p;
160 int face_name_id_number ( /* FRAME_PTR, Lisp_Object name */ );
162 struct face *intern_face ( /* FRAME_PTR, struct face * */ );
163 static int new_computed_face ( /* FRAME_PTR, struct face * */ );
164 static int intern_computed_face ( /* FRAME_PTR, struct face * */ );
165 static void ensure_face_ready ( /* FRAME_PTR, int id */ );
166 void recompute_basic_faces ( /* FRAME_PTR f */ );
168 /* Allocating, copying, and comparing struct faces. */
170 /* Allocate a new face */
171 static struct face *
172 allocate_face ()
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;
183 /* Make a new face that's a copy of an existing one. */
184 static struct face *
185 copy_face (face)
186 struct face *face;
188 struct face *result = allocate_face ();
190 result->font = face->font;
191 result->foreground = face->foreground;
192 result->background = face->background;
193 result->stipple = face->stipple;
194 result->underline = face->underline;
195 result->pixmap_h = face->pixmap_h;
196 result->pixmap_w = face->pixmap_w;
198 return result;
201 static int
202 face_eql (face1, face2)
203 struct face *face1, *face2;
205 return ( face1->font == face2->font
206 && face1->foreground == face2->foreground
207 && face1->background == face2->background
208 && face1->stipple == face2->stipple
209 && face1->underline == face2->underline);
212 /* Managing graphics contexts of faces. */
214 #ifdef HAVE_X_WINDOWS
215 /* Given a computed face, construct its graphics context if necessary. */
217 struct face *
218 intern_face (f, face)
219 struct frame *f;
220 struct face *face;
222 GC gc;
223 XGCValues xgcv;
224 unsigned long mask;
226 if (face->gc)
227 return face;
229 BLOCK_INPUT;
231 if (face->foreground != FACE_DEFAULT)
232 xgcv.foreground = face->foreground;
233 else
234 xgcv.foreground = f->display.x->foreground_pixel;
236 if (face->background != FACE_DEFAULT)
237 xgcv.background = face->background;
238 else
239 xgcv.background = f->display.x->background_pixel;
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;
246 xgcv.graphics_exposures = 0;
248 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
249 if (face->stipple && face->stipple != FACE_DEFAULT)
251 xgcv.fill_style = FillStippled;
252 xgcv.stipple = x_bitmap_pixmap (f, face->stipple);
253 mask |= GCFillStyle | GCStipple;
256 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
257 mask, &xgcv);
259 face->gc = gc;
261 UNBLOCK_INPUT;
263 return face;
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. */
271 void
272 clear_face_cache ()
274 Lisp_Object tail, frame;
276 BLOCK_INPUT;
277 FOR_EACH_FRAME (tail, frame)
279 FRAME_PTR f = XFRAME (frame);
280 if (FRAME_X_P (f))
282 int i;
283 Display *dpy = FRAME_X_DISPLAY (f);
285 for (i = 2; i < FRAME_N_COMPUTED_FACES (f); i++)
287 struct face *face = FRAME_COMPUTED_FACES (f) [i];
288 if (face->gc)
289 XFreeGC (dpy, face->gc);
290 face->gc = 0;
295 UNBLOCK_INPUT;
298 /* Allocating, freeing, and duplicating fonts, colors, and pixmaps.
300 These functions operate on param faces only.
301 Computed faces get their fonts, colors and pixmaps
302 by merging param faces. */
304 static XFontStruct *
305 load_font (f, name)
306 struct frame *f;
307 Lisp_Object name;
309 XFontStruct *font;
311 if (NILP (name))
312 return (XFontStruct *) FACE_DEFAULT;
314 CHECK_STRING (name, 0);
315 BLOCK_INPUT;
316 font = XLoadQueryFont (FRAME_X_DISPLAY (f), (char *) XSTRING (name)->data);
317 UNBLOCK_INPUT;
319 if (! font)
320 Fsignal (Qerror, Fcons (build_string ("undefined font"),
321 Fcons (name, Qnil)));
322 return font;
325 static void
326 unload_font (f, font)
327 struct frame *f;
328 XFontStruct *font;
330 if (!font || font == ((XFontStruct *) FACE_DEFAULT))
331 return;
333 BLOCK_INPUT;
334 XFreeFont (FRAME_X_DISPLAY (f), font);
335 UNBLOCK_INPUT;
338 static unsigned long
339 load_color (f, name)
340 struct frame *f;
341 Lisp_Object name;
343 XColor color;
344 int result;
346 if (NILP (name))
347 return FACE_DEFAULT;
349 CHECK_STRING (name, 0);
350 result = defined_color(f, (char *) XSTRING (name)->data, &color, 1);
351 if (! result)
352 Fsignal (Qerror, Fcons (build_string ("undefined color"),
353 Fcons (name, Qnil)));
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. */
357 return (unsigned long) color.pixel;
360 static void
361 unload_color (f, pixel)
362 struct frame *f;
363 unsigned long pixel;
365 Colormap cmap;
366 Display *dpy = FRAME_X_DISPLAY (f);
367 if (pixel == FACE_DEFAULT
368 || pixel == BLACK_PIX_DEFAULT (f)
369 || pixel == WHITE_PIX_DEFAULT (f))
370 return;
371 cmap = DefaultColormapOfScreen (DefaultScreenOfDisplay (dpy));
372 BLOCK_INPUT;
373 XFreeColors (dpy, cmap, &pixel, 1, 0);
374 UNBLOCK_INPUT;
377 DEFUN ("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;
382 Lisp_Object height, width;
384 return ((STRINGP (arg)
385 || (CONSP (arg)
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)
392 && XINT (width) > 0
393 && XINT (height) > 0
394 /* The string must have enough bits for width * height. */
395 && ((XSTRING (XCONS (XCONS (XCONS (arg)->cdr)->cdr)->car)->size
396 * (INTBITS / sizeof (int)))
397 >= XFASTINT (width) * XFASTINT (height))))
398 ? Qt : Qnil);
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.
405 Store the bitmap width in *W_PTR and height in *H_PTR. */
407 static long
408 load_pixmap (f, name, w_ptr, h_ptr)
409 FRAME_PTR f;
410 Lisp_Object name;
411 unsigned int *w_ptr, *h_ptr;
413 int bitmap_id;
414 Lisp_Object tem;
416 if (NILP (name))
417 return FACE_DEFAULT;
419 tem = Fpixmap_spec_p (name);
420 if (NILP (tem))
421 wrong_type_argument (Qpixmap_spec_p, name);
423 BLOCK_INPUT;
425 if (CONSP (name))
427 /* Decode a bitmap spec into a bitmap. */
429 int h, w;
430 Lisp_Object bits;
432 w = XINT (Fcar (name));
433 h = XINT (Fcar (Fcdr (name)));
434 bits = Fcar (Fcdr (Fcdr (name)));
436 bitmap_id = x_create_bitmap_from_data (f, XSTRING (bits)->data,
437 w, h);
439 else
441 /* It must be a string -- a file name. */
442 bitmap_id = x_create_bitmap_from_file (f, name);
444 UNBLOCK_INPUT;
446 if (bitmap_id < 0)
447 Fsignal (Qerror, Fcons (build_string ("invalid or undefined bitmap"),
448 Fcons (name, Qnil)));
450 *w_ptr = x_bitmap_width (f, bitmap_id);
451 *h_ptr = x_bitmap_height (f, bitmap_id);
453 return bitmap_id;
456 #else /* !HAVE_X_WINDOWS */
458 /* Stubs for MSDOS when not under X. */
460 struct face *
461 intern_face (f, face)
462 struct frame *f;
463 struct face *face;
465 return face;
468 void
469 clear_face_cache ()
471 /* No action. */
474 static void
475 build_face (f, face)
476 struct frame *f;
477 struct face *face;
479 face->gc = 1;
482 #ifdef MSDOS
483 unsigned long
484 load_color (f, name)
485 FRAME_PTR f;
486 Lisp_Object name;
488 Lisp_Object result;
490 if (NILP (name))
491 return FACE_DEFAULT;
493 CHECK_STRING (name, 0);
494 result = call1 (Qmsdos_color_translate, name);
495 if (INTEGERP (result))
496 return XINT (result);
497 else
498 Fsignal (Qerror, Fcons (build_string ("undefined color"),
499 Fcons (name, Qnil)));
501 #endif
502 #endif /* !HAVE_X_WINDOWS */
505 /* Managing parameter face arrays for frames. */
507 void
508 init_frame_faces (f)
509 FRAME_PTR f;
511 ensure_face_ready (f, 0);
512 ensure_face_ready (f, 1);
514 FRAME_N_COMPUTED_FACES (f) = 0;
515 FRAME_SIZE_COMPUTED_FACES (f) = 0;
517 new_computed_face (f, FRAME_PARAM_FACES (f)[0]);
518 new_computed_face (f, FRAME_PARAM_FACES (f)[1]);
519 recompute_basic_faces (f);
521 #ifdef MULTI_FRAME
522 /* Find another X frame. */
524 Lisp_Object tail, frame, result;
526 result = Qnil;
527 FOR_EACH_FRAME (tail, frame)
528 if (FRAME_X_P (XFRAME (frame))
529 && XFRAME (frame) != f)
531 result = frame;
532 break;
535 /* If we didn't find any X frames other than f, then we don't need
536 any faces other than 0 and 1, so we're okay. Otherwise, make
537 sure that all faces valid on the selected frame are also valid
538 on this new frame. */
539 if (FRAMEP (result))
541 int i;
542 int n_faces = FRAME_N_PARAM_FACES (XFRAME (result));
543 struct face **faces = FRAME_PARAM_FACES (XFRAME (result));
545 for (i = 2; i < n_faces; i++)
546 if (faces[i])
547 ensure_face_ready (f, i);
550 #endif /* MULTI_FRAME */
554 /* Called from Fdelete_frame. */
556 void
557 free_frame_faces (f)
558 struct frame *f;
560 Display *dpy = FRAME_X_DISPLAY (f);
561 int i;
563 BLOCK_INPUT;
565 for (i = 0; i < FRAME_N_PARAM_FACES (f); i++)
567 struct face *face = FRAME_PARAM_FACES (f) [i];
568 if (face)
570 unload_font (f, face->font);
571 unload_color (f, face->foreground);
572 unload_color (f, face->background);
573 x_destroy_bitmap (f, face->stipple);
574 xfree (face);
577 xfree (FRAME_PARAM_FACES (f));
578 FRAME_PARAM_FACES (f) = 0;
579 FRAME_N_PARAM_FACES (f) = 0;
581 /* All faces in FRAME_COMPUTED_FACES use resources copied from
582 FRAME_PARAM_FACES; we can free them without fuss.
583 But we do free the GCs and the face objects themselves. */
584 for (i = 0; i < FRAME_N_COMPUTED_FACES (f); i++)
586 struct face *face = FRAME_COMPUTED_FACES (f) [i];
587 if (face)
589 if (face->gc)
590 XFreeGC (dpy, face->gc);
591 xfree (face);
594 xfree (FRAME_COMPUTED_FACES (f));
595 FRAME_COMPUTED_FACES (f) = 0;
596 FRAME_N_COMPUTED_FACES (f) = 0;
598 UNBLOCK_INPUT;
601 /* Interning faces in a frame's face array. */
603 static int
604 new_computed_face (f, new_face)
605 struct frame *f;
606 struct face *new_face;
608 int i = FRAME_N_COMPUTED_FACES (f);
610 if (i >= FRAME_SIZE_COMPUTED_FACES (f))
612 int new_size = i + 32;
614 FRAME_COMPUTED_FACES (f)
615 = (struct face **) (FRAME_SIZE_COMPUTED_FACES (f) == 0
616 ? xmalloc (new_size * sizeof (struct face *))
617 : xrealloc (FRAME_COMPUTED_FACES (f),
618 new_size * sizeof (struct face *)));
619 FRAME_SIZE_COMPUTED_FACES (f) = new_size;
622 i = FRAME_N_COMPUTED_FACES (f)++;
623 FRAME_COMPUTED_FACES (f)[i] = copy_face (new_face);
624 return i;
628 /* Find a match for NEW_FACE in a FRAME's computed face array, and add
629 it if we don't find one. */
630 static int
631 intern_computed_face (f, new_face)
632 struct frame *f;
633 struct face *new_face;
635 int len = FRAME_N_COMPUTED_FACES (f);
636 int i;
638 /* Search for a computed face already on F equivalent to FACE. */
639 for (i = 0; i < len; i++)
641 if (! FRAME_COMPUTED_FACES (f)[i])
642 abort ();
643 if (face_eql (new_face, FRAME_COMPUTED_FACES (f)[i]))
644 return i;
647 /* We didn't find one; add a new one. */
648 return new_computed_face (f, new_face);
651 /* Make parameter face id ID valid on frame F. */
653 static void
654 ensure_face_ready (f, id)
655 struct frame *f;
656 int id;
658 if (FRAME_N_PARAM_FACES (f) <= id)
660 int n = id + 10;
661 int i;
662 if (!FRAME_N_PARAM_FACES (f))
663 FRAME_PARAM_FACES (f)
664 = (struct face **) xmalloc (sizeof (struct face *) * n);
665 else
666 FRAME_PARAM_FACES (f)
667 = (struct face **) xrealloc (FRAME_PARAM_FACES (f),
668 sizeof (struct face *) * n);
670 bzero (FRAME_PARAM_FACES (f) + FRAME_N_PARAM_FACES (f),
671 (n - FRAME_N_PARAM_FACES (f)) * sizeof (struct face *));
672 FRAME_N_PARAM_FACES (f) = n;
675 if (FRAME_PARAM_FACES (f) [id] == 0)
676 FRAME_PARAM_FACES (f) [id] = allocate_face ();
679 #ifdef HAVE_X_WINDOWS
680 /* Return non-zero if FONT1 and FONT2 have the same width.
681 We do not check the height, because we can now deal with
682 different heights.
683 We assume that they're both character-cell fonts. */
686 same_size_fonts (font1, font2)
687 XFontStruct *font1, *font2;
689 XCharStruct *bounds1 = &font1->min_bounds;
690 XCharStruct *bounds2 = &font2->min_bounds;
692 return (bounds1->width == bounds2->width);
695 /* Update the line_height of frame F according to the biggest font in
696 any face. Return nonzero if if line_height changes. */
699 frame_update_line_height (f)
700 FRAME_PTR f;
702 int i;
703 int biggest = FONT_HEIGHT (f->display.x->font);
705 for (i = 0; i < f->display.x->n_param_faces; i++)
706 if (f->display.x->param_faces[i] != 0
707 && f->display.x->param_faces[i]->font != (XFontStruct *) FACE_DEFAULT)
709 int height = FONT_HEIGHT (f->display.x->param_faces[i]->font);
710 if (height > biggest)
711 biggest = height;
714 if (biggest == f->display.x->line_height)
715 return 0;
717 f->display.x->line_height = biggest;
718 return 1;
720 #endif /* not HAVE_X_WINDOWS */
722 /* Modify face TO by copying from FROM all properties which have
723 nondefault settings. */
725 static void
726 merge_faces (from, to)
727 struct face *from, *to;
729 /* Only merge the font if it's the same width as the base font.
730 Otherwise ignore it, since we can't handle it properly. */
731 if (from->font != (XFontStruct *) FACE_DEFAULT
732 && same_size_fonts (from->font, to->font))
733 to->font = from->font;
734 if (from->foreground != FACE_DEFAULT)
735 to->foreground = from->foreground;
736 if (from->background != FACE_DEFAULT)
737 to->background = from->background;
738 if (from->stipple != FACE_DEFAULT)
740 to->stipple = from->stipple;
741 to->pixmap_h = from->pixmap_h;
742 to->pixmap_w = from->pixmap_w;
744 if (from->underline)
745 to->underline = from->underline;
748 /* Set up the basic set of facial parameters, based on the frame's
749 data; all faces are deltas applied to this. */
751 static void
752 compute_base_face (f, face)
753 FRAME_PTR f;
754 struct face *face;
756 face->gc = 0;
757 face->foreground = FRAME_FOREGROUND_PIXEL (f);
758 face->background = FRAME_BACKGROUND_PIXEL (f);
759 face->font = FRAME_FONT (f);
760 face->stipple = 0;
761 face->underline = 0;
764 /* Return the face ID to use to display a special glyph which selects
765 FACE_CODE as the face ID, assuming that ordinarily the face would
766 be CURRENT_FACE. F is the frame. */
769 compute_glyph_face (f, face_code, current_face)
770 struct frame *f;
771 int face_code, current_face;
773 struct face face;
775 face = *FRAME_COMPUTED_FACES (f)[current_face];
777 if (face_code >= 0 && face_code < FRAME_N_PARAM_FACES (f)
778 && FRAME_PARAM_FACES (f) [face_code] != 0)
779 merge_faces (FRAME_PARAM_FACES (f) [face_code], &face);
781 return intern_computed_face (f, &face);
784 /* Return the face ID to use to display a special glyph which selects
785 FACE_CODE as the face ID, assuming that ordinarily the face would
786 be CURRENT_FACE. F is the frame. */
789 compute_glyph_face_1 (f, face_name, current_face)
790 struct frame *f;
791 Lisp_Object face_name;
792 int current_face;
794 struct face face;
796 face = *FRAME_COMPUTED_FACES (f)[current_face];
798 if (!NILP (face_name))
800 int facecode = face_name_id_number (f, face_name);
801 if (facecode >= 0 && facecode < FRAME_N_PARAM_FACES (f)
802 && FRAME_PARAM_FACES (f) [facecode] != 0)
803 merge_faces (FRAME_PARAM_FACES (f) [facecode], &face);
806 return intern_computed_face (f, &face);
809 /* Return the face ID associated with a buffer position POS.
810 Store into *ENDPTR the position at which a different face is needed.
811 This does not take account of glyphs that specify their own face codes.
812 F is the frame in use for display, and W is a window displaying
813 the current buffer.
815 REGION_BEG, REGION_END delimit the region, so it can be highlighted.
817 LIMIT is a position not to scan beyond. That is to limit
818 the time this function can take.
820 If MOUSE is nonzero, use the character's mouse-face, not its face. */
823 compute_char_face (f, w, pos, region_beg, region_end, endptr, limit, mouse)
824 struct frame *f;
825 struct window *w;
826 int pos;
827 int region_beg, region_end;
828 int *endptr;
829 int limit;
830 int mouse;
832 struct face face;
833 Lisp_Object prop, position;
834 int i, j, noverlays;
835 int facecode;
836 Lisp_Object *overlay_vec;
837 Lisp_Object frame;
838 int endpos;
839 Lisp_Object propname;
841 /* W must display the current buffer. We could write this function
842 to use the frame and buffer of W, but right now it doesn't. */
843 if (XBUFFER (w->buffer) != current_buffer)
844 abort ();
846 XSETFRAME (frame, f);
848 endpos = ZV;
849 if (pos < region_beg && region_beg < endpos)
850 endpos = region_beg;
852 XSETFASTINT (position, pos);
854 if (mouse)
855 propname = Qmouse_face;
856 else
857 propname = Qface;
859 prop = Fget_text_property (position, propname, w->buffer);
862 Lisp_Object limit1, end;
864 XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
865 end = Fnext_single_property_change (position, propname, w->buffer, limit1);
866 if (INTEGERP (end))
867 endpos = XINT (end);
871 int next_overlay;
872 int len;
874 /* First try with room for 40 overlays. */
875 len = 40;
876 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
878 noverlays = overlays_at (pos, 0, &overlay_vec, &len, &next_overlay, NULL);
880 /* If there are more than 40,
881 make enough space for all, and try again. */
882 if (noverlays > len)
884 len = noverlays;
885 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
886 noverlays = overlays_at (pos, 0, &overlay_vec, &len,
887 &next_overlay, NULL);
890 if (next_overlay < endpos)
891 endpos = next_overlay;
894 *endptr = endpos;
896 /* Optimize the default case. */
897 if (noverlays == 0 && NILP (prop)
898 && !(pos >= region_beg && pos < region_end))
899 return 0;
901 compute_base_face (f, &face);
903 if (CONSP (prop))
905 /* We have a list of faces, merge them in reverse order */
906 Lisp_Object length = Flength (prop);
907 int len = XINT (length);
908 Lisp_Object *faces;
910 /* Put them into an array */
911 faces = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
912 for (j = 0; j < len; j++)
914 faces[j] = Fcar (prop);
915 prop = Fcdr (prop);
917 /* So that we can merge them in the reverse order */
918 for (j = len - 1; j >= 0; j--)
920 facecode = face_name_id_number (f, faces[j]);
921 if (facecode >= 0 && facecode < FRAME_N_PARAM_FACES (f)
922 && FRAME_PARAM_FACES (f) [facecode] != 0)
923 merge_faces (FRAME_PARAM_FACES (f) [facecode], &face);
926 else if (!NILP (prop))
928 facecode = face_name_id_number (f, prop);
929 if (facecode >= 0 && facecode < FRAME_N_PARAM_FACES (f)
930 && FRAME_PARAM_FACES (f) [facecode] != 0)
931 merge_faces (FRAME_PARAM_FACES (f) [facecode], &face);
934 noverlays = sort_overlays (overlay_vec, noverlays, w);
936 /* Now merge the overlay data in that order. */
937 for (i = 0; i < noverlays; i++)
939 prop = Foverlay_get (overlay_vec[i], propname);
940 if (CONSP (prop))
942 /* We have a list of faces, merge them in reverse order */
943 Lisp_Object length = Flength (prop);
944 int len = XINT (length);
945 Lisp_Object *faces;
946 int i;
948 /* Put them into an array */
949 faces = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
950 for (j = 0; j < len; j++)
952 faces[j] = Fcar (prop);
953 prop = Fcdr (prop);
955 /* So that we can merge them in the reverse order */
956 for (j = len - 1; j >= 0; j--)
958 facecode = face_name_id_number (f, faces[j]);
959 if (facecode >= 0 && facecode < FRAME_N_PARAM_FACES (f)
960 && FRAME_PARAM_FACES (f) [facecode] != 0)
961 merge_faces (FRAME_PARAM_FACES (f) [facecode], &face);
964 else if (!NILP (prop))
966 Lisp_Object oend;
967 int oendpos;
969 facecode = face_name_id_number (f, prop);
970 if (facecode >= 0 && facecode < FRAME_N_PARAM_FACES (f)
971 && FRAME_PARAM_FACES (f) [facecode] != 0)
972 merge_faces (FRAME_PARAM_FACES (f)[facecode], &face);
974 oend = OVERLAY_END (overlay_vec[i]);
975 oendpos = OVERLAY_POSITION (oend);
976 if (oendpos < endpos)
977 endpos = oendpos;
981 if (pos >= region_beg && pos < region_end)
983 if (region_end < endpos)
984 endpos = region_end;
985 if (region_face >= 0 && region_face < next_face_id)
986 merge_faces (FRAME_PARAM_FACES (f)[region_face], &face);
989 *endptr = endpos;
991 return intern_computed_face (f, &face);
994 /* Recompute the GC's for the default and modeline faces.
995 We call this after changing frame parameters on which those GC's
996 depend. */
998 void
999 recompute_basic_faces (f)
1000 FRAME_PTR f;
1002 /* If the frame's faces haven't been initialized yet, don't worry about
1003 this stuff. */
1004 if (FRAME_N_PARAM_FACES (f) < 2)
1005 return;
1007 BLOCK_INPUT;
1009 if (FRAME_DEFAULT_FACE (f)->gc)
1010 XFreeGC (FRAME_X_DISPLAY (f), FRAME_DEFAULT_FACE (f)->gc);
1011 if (FRAME_MODE_LINE_FACE (f)->gc)
1012 XFreeGC (FRAME_X_DISPLAY (f), FRAME_MODE_LINE_FACE (f)->gc);
1014 compute_base_face (f, FRAME_DEFAULT_FACE (f));
1015 compute_base_face (f, FRAME_MODE_LINE_FACE (f));
1017 merge_faces (FRAME_DEFAULT_PARAM_FACE (f), FRAME_DEFAULT_FACE (f));
1018 merge_faces (FRAME_MODE_LINE_PARAM_FACE (f), FRAME_MODE_LINE_FACE (f));
1020 intern_face (f, FRAME_DEFAULT_FACE (f));
1021 intern_face (f, FRAME_MODE_LINE_FACE (f));
1023 UNBLOCK_INPUT;
1028 /* Lisp interface. */
1030 DEFUN ("frame-face-alist", Fframe_face_alist, Sframe_face_alist, 1, 1, 0,
1032 (frame)
1033 Lisp_Object frame;
1035 CHECK_FRAME (frame, 0);
1036 return XFRAME (frame)->face_alist;
1039 DEFUN ("set-frame-face-alist", Fset_frame_face_alist, Sset_frame_face_alist,
1040 2, 2, 0, "")
1041 (frame, value)
1042 Lisp_Object frame, value;
1044 CHECK_FRAME (frame, 0);
1045 XFRAME (frame)->face_alist = value;
1046 return value;
1050 DEFUN ("make-face-internal", Fmake_face_internal, Smake_face_internal, 1, 1, 0,
1051 "Create face number FACE-ID on all frames.")
1052 (face_id)
1053 Lisp_Object face_id;
1055 Lisp_Object rest, frame;
1056 int id = XINT (face_id);
1058 CHECK_NUMBER (face_id, 0);
1059 if (id < 0 || id >= next_face_id)
1060 error ("Face id out of range");
1062 FOR_EACH_FRAME (rest, frame)
1064 if (FRAME_X_P (XFRAME (frame)))
1065 ensure_face_ready (XFRAME (frame), id);
1067 return Qnil;
1071 DEFUN ("set-face-attribute-internal", Fset_face_attribute_internal,
1072 Sset_face_attribute_internal, 4, 4, 0, "")
1073 (face_id, attr_name, attr_value, frame)
1074 Lisp_Object face_id, attr_name, attr_value, frame;
1076 struct face *face;
1077 struct frame *f;
1078 int magic_p;
1079 int id;
1080 int garbaged = 0;
1082 CHECK_FRAME (frame, 0);
1083 CHECK_NUMBER (face_id, 0);
1084 CHECK_SYMBOL (attr_name, 0);
1086 f = XFRAME (frame);
1087 id = XINT (face_id);
1088 if (id < 0 || id >= next_face_id)
1089 error ("Face id out of range");
1091 if (! FRAME_X_P (f))
1092 return Qnil;
1094 ensure_face_ready (f, id);
1095 face = FRAME_PARAM_FACES (f) [XFASTINT (face_id)];
1097 if (EQ (attr_name, intern ("font")))
1099 #if defined (MSDOS) && !defined (HAVE_X_WINDOWS)
1100 face->font = 0; /* The one and only font. */
1101 #else
1102 XFontStruct *font = load_font (f, attr_value);
1103 if (face->font != f->display.x->font)
1104 unload_font (f, face->font);
1105 face->font = font;
1106 if (frame_update_line_height (f))
1107 x_set_window_size (f, 0, f->width, f->height);
1108 /* Must clear cache, since it might contain the font
1109 we just got rid of. */
1110 garbaged = 1;
1111 #endif
1113 else if (EQ (attr_name, intern ("foreground")))
1115 unsigned long new_color = load_color (f, attr_value);
1116 unload_color (f, face->foreground);
1117 face->foreground = new_color;
1118 garbaged = 1;
1120 else if (EQ (attr_name, intern ("background")))
1122 unsigned long new_color = load_color (f, attr_value);
1123 unload_color (f, face->background);
1124 #if defined (MSDOS) && !defined (HAVE_X_WINDOWS)
1125 new_color &= ~8; /* Bright would give blinking characters. */
1126 #endif
1127 face->background = new_color;
1128 garbaged = 1;
1130 else if (EQ (attr_name, intern ("background-pixmap")))
1132 unsigned int w, h;
1133 unsigned long new_pixmap = load_pixmap (f, attr_value, &w, &h);
1134 x_destroy_bitmap (f, face->stipple);
1135 face->stipple = new_pixmap;
1136 face->pixmap_w = w;
1137 face->pixmap_h = h;
1138 garbaged = 1;
1140 else if (EQ (attr_name, intern ("underline")))
1142 int new = !NILP (attr_value);
1143 face->underline = new;
1145 else
1146 error ("unknown face attribute");
1148 if (id == 0 || id == 1)
1149 recompute_basic_faces (f);
1151 /* We must redraw the frame whenever any face font or color changes,
1152 because it's possible that a merged (display) face
1153 contains the font or color we just replaced.
1154 And we must inhibit any Expose events until the redraw is done,
1155 since they would try to use the invalid display faces. */
1156 if (garbaged)
1157 SET_FRAME_GARBAGED (f);
1159 return Qnil;
1162 DEFUN ("internal-next-face-id", Finternal_next_face_id, Sinternal_next_face_id,
1163 0, 0, 0, "")
1166 return make_number (next_face_id++);
1169 /* Return the face id for name NAME on frame FRAME.
1170 (It should be the same for all frames,
1171 but it's as easy to use the "right" frame to look it up
1172 as to use any other one.) */
1175 face_name_id_number (f, name)
1176 FRAME_PTR f;
1177 Lisp_Object name;
1179 Lisp_Object tem;
1181 tem = Fcdr (assq_no_quit (name, f->face_alist));
1182 if (NILP (tem))
1183 return 0;
1184 CHECK_VECTOR (tem, 0);
1185 tem = XVECTOR (tem)->contents[2];
1186 CHECK_NUMBER (tem, 0);
1187 return XINT (tem);
1190 /* Emacs initialization. */
1192 void
1193 syms_of_xfaces ()
1195 Qface = intern ("face");
1196 staticpro (&Qface);
1197 Qmouse_face = intern ("mouse-face");
1198 staticpro (&Qmouse_face);
1199 Qpixmap_spec_p = intern ("pixmap-spec-p");
1200 staticpro (&Qpixmap_spec_p);
1202 DEFVAR_INT ("region-face", &region_face,
1203 "Face number to use to highlight the region\n\
1204 The region is highlighted with this face\n\
1205 when Transient Mark mode is enabled and the mark is active.");
1207 #ifdef HAVE_X_WINDOWS
1208 defsubr (&Spixmap_spec_p);
1209 #endif
1210 defsubr (&Sframe_face_alist);
1211 defsubr (&Sset_frame_face_alist);
1212 defsubr (&Smake_face_internal);
1213 defsubr (&Sset_face_attribute_internal);
1214 defsubr (&Sinternal_next_face_id);
1217 #endif /* HAVE_FACES */