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)
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, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* This is derived from work by Lucid (some parts very loosely so). */
23 #include <sys/types.h>
38 #include "dispextern.h"
40 #include "blockinput.h"
42 #include "intervals.h"
45 /* Compensate for bug in Xos.h on some systems, on which it requires
46 time.h. On some such systems, Xos.h tries to redefine struct
47 timeval and struct timezone if USG is #defined while it is
49 #ifdef XOS_NEEDS_TIME_H
62 #endif /* HAVE_X_WINDOWS */
64 /* An explanation of the face data structures. */
66 /* ========================= Face Data Structures =========================
68 Let FACE-NAME be a symbol naming a face.
70 Let FACE-VECTOR be (assq FACE-NAME (frame-face-alist FRAME))
71 FACE-VECTOR is either nil, or a vector of the form
72 [face NAME ID FONT FOREGROUND BACKGROUND BACKGROUND-PIXMAP UNDERLINE-P]
74 face is the symbol `face',
75 NAME is the symbol with which this vector is associated (a backpointer),
76 ID is the face ID, an integer used internally by the C code to identify
78 FONT, FOREGROUND, and BACKGROUND are strings naming the fonts and colors
80 BACKGROUND-PIXMAP is the name of an x bitmap filename, which we don't
82 UNDERLINE-P is non-nil if the face should be underlined.
83 If any of these elements are nil, that parameter is considered
84 unspecified; parameters from faces specified by lower-priority
85 overlays or text properties, or the parameters of the frame itself,
86 can show through. (lisp/faces.el maintains these lists.)
88 (assq FACE-NAME global-face-data) returns a vector describing the
89 global parameters for that face.
91 Let PARAM-FACE be FRAME->output_data.x->param_faces[Faref (FACE-VECTOR, 2)].
92 PARAM_FACE is a struct face whose members are the Xlib analogues of
93 the parameters in FACE-VECTOR. If an element of FACE-VECTOR is
94 nil, then the corresponding member of PARAM_FACE is FACE_DEFAULT.
95 These faces are called "parameter faces", because they're the ones
96 lisp manipulates to control what gets displayed. Elements 0 and 1
97 of FRAME->output_data.x->param_faces are special - they describe the
98 default and mode line faces. None of the faces in param_faces have
99 GC's. (See src/dispextern.h for the definition of struct face.
100 lisp/faces.el maintains the isomorphism between face_alist and
103 The functions compute_char_face and compute_glyph_face find and
104 combine the parameter faces associated with overlays and text
105 properties. The resulting faces are called "computed faces"; none
106 of their members are FACE_DEFAULT; they are completely specified.
107 They then call intern_compute_face to search
108 FRAME->output_data.x->computed_faces for a matching face, add one if
109 none is found, and return the index into
110 FRAME->output_data.x->computed_faces. FRAME's glyph matrices use these
111 indices to record the faces of the matrix characters, and the X
112 display hooks consult compute_faces to decide how to display these
113 characters. Elements 0 and 1 of computed_faces always describe the
114 default and mode-line faces.
116 Each computed face belongs to a particular frame.
118 Computed faces have graphics contexts some of the time.
119 intern_face builds a GC for a specified computed face
120 if it doesn't have one already.
121 clear_face_cache clears out the GCs of all computed faces.
122 This is done from time to time so that we don't hold on to
123 lots of GCs that are no longer needed.
125 If a computed face has 0 as its font,
126 it is unused, and can be reused by new_computed_face.
130 Symbols naming faces must have associations on all frames; for any
131 FRAME, for all FACE-NAME, if (assq FACE-NAME (frame-face-alist
132 FRAME)) is non-nil, it must be non-nil for all frames.
134 Analogously, indices into param_faces must be valid on all frames;
135 if param_faces[i] is a non-zero face pointer on one frame, then it
136 must be filled in on all frames. Code assumes that face ID's can
137 be used on any frame.
141 Why do we keep param_faces and computed_faces separate?
142 computed_faces contains an element for every combination of facial
143 parameters we have ever displayed. indices into param_faces have
144 to be valid on all frames. If they were the same array, then that
145 array would grow very large on all frames, because any facial
146 combination displayed on any frame would need to be a valid entry
149 /* Definitions and declarations. */
151 /* The number of face-id's in use (same for all frames). */
152 static int next_face_id
;
154 /* The number of the face to use to indicate the region. */
155 static int region_face
;
157 /* This is what appears in a slot in a face to signify that the face
158 does not specify that display aspect. */
159 #define FACE_DEFAULT (~0)
161 Lisp_Object Qface
, Qmouse_face
;
162 Lisp_Object Qpixmap_spec_p
;
164 int face_name_id_number ( /* FRAME_PTR, Lisp_Object name */ );
166 struct face
*intern_face ( /* FRAME_PTR, struct face * */ );
167 static int new_computed_face ( /* FRAME_PTR, struct face * */ );
168 static int intern_computed_face ( /* FRAME_PTR, struct face * */ );
169 static void ensure_face_ready ( /* FRAME_PTR, int id */ );
170 void recompute_basic_faces ( /* FRAME_PTR f */ );
172 /* Allocating, copying, and comparing struct faces. */
174 /* Allocate a new face */
178 struct face
*result
= (struct face
*) xmalloc (sizeof (struct face
));
179 bzero (result
, sizeof (struct face
));
180 result
->font
= (XFontStruct
*) FACE_DEFAULT
;
181 result
->foreground
= FACE_DEFAULT
;
182 result
->background
= FACE_DEFAULT
;
183 result
->stipple
= FACE_DEFAULT
;
187 /* Make a new face that's a copy of an existing one. */
192 struct face
*result
= allocate_face ();
194 result
->font
= face
->font
;
195 result
->foreground
= face
->foreground
;
196 result
->background
= face
->background
;
197 result
->stipple
= face
->stipple
;
198 result
->underline
= face
->underline
;
199 result
->pixmap_h
= face
->pixmap_h
;
200 result
->pixmap_w
= face
->pixmap_w
;
206 face_eql (face1
, face2
)
207 struct face
*face1
, *face2
;
209 return ( face1
->font
== face2
->font
210 && face1
->foreground
== face2
->foreground
211 && face1
->background
== face2
->background
212 && face1
->stipple
== face2
->stipple
213 && face1
->underline
== face2
->underline
);
216 /* Managing graphics contexts of faces. */
218 #ifdef HAVE_X_WINDOWS
219 /* Given a computed face, construct its graphics context if necessary. */
222 intern_face (f
, face
)
235 if (face
->foreground
!= FACE_DEFAULT
)
236 xgcv
.foreground
= face
->foreground
;
238 xgcv
.foreground
= f
->output_data
.x
->foreground_pixel
;
240 if (face
->background
!= FACE_DEFAULT
)
241 xgcv
.background
= face
->background
;
243 xgcv
.background
= f
->output_data
.x
->background_pixel
;
245 if (face
->font
&& face
->font
!= (XFontStruct
*) FACE_DEFAULT
)
246 xgcv
.font
= face
->font
->fid
;
248 xgcv
.font
= f
->output_data
.x
->font
->fid
;
250 xgcv
.graphics_exposures
= 0;
252 mask
= GCForeground
| GCBackground
| GCFont
| GCGraphicsExposures
;
253 if (face
->stipple
&& face
->stipple
!= FACE_DEFAULT
)
255 xgcv
.fill_style
= FillStippled
;
256 xgcv
.stipple
= x_bitmap_pixmap (f
, face
->stipple
);
257 mask
|= GCFillStyle
| GCStipple
;
260 gc
= XCreateGC (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
270 /* Clear out all graphics contexts for all computed faces
271 except for the default and mode line faces.
272 This should be done from time to time just to avoid
273 keeping too many graphics contexts that are no longer needed. */
278 Lisp_Object tail
, frame
;
281 FOR_EACH_FRAME (tail
, frame
)
283 FRAME_PTR f
= XFRAME (frame
);
287 Display
*dpy
= FRAME_X_DISPLAY (f
);
289 for (i
= 2; i
< FRAME_N_COMPUTED_FACES (f
); i
++)
291 struct face
*face
= FRAME_COMPUTED_FACES (f
) [i
];
293 XFreeGC (dpy
, face
->gc
);
302 /* Allocating, freeing, and duplicating fonts, colors, and pixmaps.
304 These functions operate on param faces only.
305 Computed faces get their fonts, colors and pixmaps
306 by merging param faces. */
316 return (XFontStruct
*) FACE_DEFAULT
;
318 CHECK_STRING (name
, 0);
320 font
= XLoadQueryFont (FRAME_X_DISPLAY (f
), (char *) XSTRING (name
)->data
);
324 Fsignal (Qerror
, Fcons (build_string ("undefined font"),
325 Fcons (name
, Qnil
)));
330 unload_font (f
, font
)
334 int len
= FRAME_N_COMPUTED_FACES (f
);
337 if (!font
|| font
== ((XFontStruct
*) FACE_DEFAULT
))
341 /* Invalidate any computed faces which use this font,
342 and free their GC's if they have any. */
343 for (i
= 2; i
< len
; i
++)
345 struct face
*face
= FRAME_COMPUTED_FACES (f
)[i
];
346 if (face
->font
== font
)
348 Display
*dpy
= FRAME_X_DISPLAY (f
);
350 XFreeGC (dpy
, face
->gc
);
352 /* This marks the computed face as available to reuse. */
357 XFreeFont (FRAME_X_DISPLAY (f
), font
);
372 CHECK_STRING (name
, 0);
373 /* if the colormap is full, defined_color will return a best match
374 to the values in an an existing cell. */
375 result
= defined_color(f
, (char *) XSTRING (name
)->data
, &color
, 1);
377 Fsignal (Qerror
, Fcons (build_string ("undefined color"),
378 Fcons (name
, Qnil
)));
379 return (unsigned long) color
.pixel
;
383 unload_color (f
, pixel
)
388 Display
*dpy
= FRAME_X_DISPLAY (f
);
389 int class = FRAME_X_DISPLAY_INFO (f
)->visual
->class;
391 if (pixel
== FACE_DEFAULT
392 || pixel
== BLACK_PIX_DEFAULT (f
)
393 || pixel
== WHITE_PIX_DEFAULT (f
))
395 cmap
= DefaultColormapOfScreen (DefaultScreenOfDisplay (dpy
));
397 /* If display has an immutable color map, freeing colors is not
398 necessary and some servers don't allow it. So don't do it. */
399 if (! (class == StaticColor
|| class == StaticGray
|| class == TrueColor
))
401 int len
= FRAME_N_COMPUTED_FACES (f
);
405 /* Invalidate any computed faces which use this color,
406 and free their GC's if they have any. */
407 for (i
= 2; i
< len
; i
++)
409 struct face
*face
= FRAME_COMPUTED_FACES (f
)[i
];
410 if (face
->foreground
== pixel
411 || face
->background
== pixel
)
413 Display
*dpy
= FRAME_X_DISPLAY (f
);
415 XFreeGC (dpy
, face
->gc
);
417 /* This marks the computed face as available to reuse. */
422 XFreeColors (dpy
, cmap
, &pixel
, 1, (unsigned long)0);
427 DEFUN ("pixmap-spec-p", Fpixmap_spec_p
, Spixmap_spec_p
, 1, 1, 0,
428 "Return t if OBJECT is a valid pixmap specification.")
432 Lisp_Object height
, width
;
434 return ((STRINGP (object
)
436 && CONSP (XCONS (object
)->cdr
)
437 && CONSP (XCONS (XCONS (object
)->cdr
)->cdr
)
438 && NILP (XCONS (XCONS (XCONS (object
)->cdr
)->cdr
)->cdr
)
439 && (width
= XCONS (object
)->car
, INTEGERP (width
))
440 && (height
= XCONS (XCONS (object
)->cdr
)->car
, INTEGERP (height
))
441 && STRINGP (XCONS (XCONS (XCONS (object
)->cdr
)->cdr
)->car
)
444 /* The string must have enough bits for width * height. */
445 && ((XSTRING (XCONS (XCONS (XCONS (object
)->cdr
)->cdr
)->car
)->size
446 * (BITS_PER_INT
/ sizeof (int)))
447 >= XFASTINT (width
) * XFASTINT (height
))))
451 /* Load a bitmap according to NAME (which is either a file name
452 or a pixmap spec). Return the bitmap_id (see xfns.c)
453 or get an error if NAME is invalid.
455 Store the bitmap width in *W_PTR and height in *H_PTR. */
458 load_pixmap (f
, name
, w_ptr
, h_ptr
)
461 unsigned int *w_ptr
, *h_ptr
;
469 tem
= Fpixmap_spec_p (name
);
471 wrong_type_argument (Qpixmap_spec_p
, name
);
477 /* Decode a bitmap spec into a bitmap. */
482 w
= XINT (Fcar (name
));
483 h
= XINT (Fcar (Fcdr (name
)));
484 bits
= Fcar (Fcdr (Fcdr (name
)));
486 bitmap_id
= x_create_bitmap_from_data (f
, XSTRING (bits
)->data
,
491 /* It must be a string -- a file name. */
492 bitmap_id
= x_create_bitmap_from_file (f
, name
);
497 Fsignal (Qerror
, Fcons (build_string ("invalid or undefined bitmap"),
498 Fcons (name
, Qnil
)));
500 *w_ptr
= x_bitmap_width (f
, bitmap_id
);
501 *h_ptr
= x_bitmap_height (f
, bitmap_id
);
506 #else /* !HAVE_X_WINDOWS */
508 /* Stubs for MSDOS when not under X. */
511 intern_face (f
, face
)
535 CHECK_STRING (name
, 0);
536 result
= call1 (Qmsdos_color_translate
, name
);
537 if (INTEGERP (result
))
538 return XINT (result
);
540 Fsignal (Qerror
, Fcons (build_string ("undefined color"),
541 Fcons (name
, Qnil
)));
544 #endif /* !HAVE_X_WINDOWS */
547 /* Managing parameter face arrays for frames. */
553 ensure_face_ready (f
, 0);
554 ensure_face_ready (f
, 1);
556 FRAME_N_COMPUTED_FACES (f
) = 0;
557 FRAME_SIZE_COMPUTED_FACES (f
) = 0;
559 new_computed_face (f
, FRAME_PARAM_FACES (f
)[0]);
560 new_computed_face (f
, FRAME_PARAM_FACES (f
)[1]);
561 recompute_basic_faces (f
);
564 /* Find another X frame. */
566 Lisp_Object tail
, frame
, result
;
569 FOR_EACH_FRAME (tail
, frame
)
570 if ((FRAME_MSDOS_P (XFRAME (frame
)) || FRAME_X_P (XFRAME (frame
)))
571 && XFRAME (frame
) != f
)
577 /* If we didn't find any X frames other than f, then we don't need
578 any faces other than 0 and 1, so we're okay. Otherwise, make
579 sure that all faces valid on the selected frame are also valid
580 on this new frame. */
584 int n_faces
= FRAME_N_PARAM_FACES (XFRAME (result
));
585 struct face
**faces
= FRAME_PARAM_FACES (XFRAME (result
));
587 for (i
= 2; i
< n_faces
; i
++)
589 ensure_face_ready (f
, i
);
592 #endif /* MULTI_FRAME */
596 /* Called from Fdelete_frame. */
602 Display
*dpy
= FRAME_X_DISPLAY (f
);
607 for (i
= 0; i
< FRAME_N_PARAM_FACES (f
); i
++)
609 struct face
*face
= FRAME_PARAM_FACES (f
) [i
];
612 unload_font (f
, face
->font
);
613 unload_color (f
, face
->foreground
);
614 unload_color (f
, face
->background
);
615 x_destroy_bitmap (f
, face
->stipple
);
619 xfree (FRAME_PARAM_FACES (f
));
620 FRAME_PARAM_FACES (f
) = 0;
621 FRAME_N_PARAM_FACES (f
) = 0;
623 /* All faces in FRAME_COMPUTED_FACES use resources copied from
624 FRAME_PARAM_FACES; we can free them without fuss.
625 But we do free the GCs and the face objects themselves. */
626 for (i
= 0; i
< FRAME_N_COMPUTED_FACES (f
); i
++)
628 struct face
*face
= FRAME_COMPUTED_FACES (f
) [i
];
632 XFreeGC (dpy
, face
->gc
);
636 xfree (FRAME_COMPUTED_FACES (f
));
637 FRAME_COMPUTED_FACES (f
) = 0;
638 FRAME_N_COMPUTED_FACES (f
) = 0;
643 /* Interning faces in a frame's face array. */
646 new_computed_face (f
, new_face
)
648 struct face
*new_face
;
650 int len
= FRAME_N_COMPUTED_FACES (f
);
653 /* Search for an unused computed face in the middle of the table. */
654 for (i
= 0; i
< len
; i
++)
656 struct face
*face
= FRAME_COMPUTED_FACES (f
)[i
];
659 FRAME_COMPUTED_FACES (f
)[i
] = copy_face (new_face
);
664 if (i
>= FRAME_SIZE_COMPUTED_FACES (f
))
666 int new_size
= i
+ 32;
668 FRAME_COMPUTED_FACES (f
)
669 = (struct face
**) (FRAME_SIZE_COMPUTED_FACES (f
) == 0
670 ? xmalloc (new_size
* sizeof (struct face
*))
671 : xrealloc (FRAME_COMPUTED_FACES (f
),
672 new_size
* sizeof (struct face
*)));
673 FRAME_SIZE_COMPUTED_FACES (f
) = new_size
;
676 i
= FRAME_N_COMPUTED_FACES (f
)++;
677 FRAME_COMPUTED_FACES (f
)[i
] = copy_face (new_face
);
682 /* Find a match for NEW_FACE in a FRAME's computed face array, and add
683 it if we don't find one. */
685 intern_computed_face (f
, new_face
)
687 struct face
*new_face
;
689 int len
= FRAME_N_COMPUTED_FACES (f
);
692 /* Search for a computed face already on F equivalent to FACE. */
693 for (i
= 0; i
< len
; i
++)
695 if (! FRAME_COMPUTED_FACES (f
)[i
])
697 if (face_eql (new_face
, FRAME_COMPUTED_FACES (f
)[i
]))
701 /* We didn't find one; add a new one. */
702 return new_computed_face (f
, new_face
);
705 /* Make parameter face id ID valid on frame F. */
708 ensure_face_ready (f
, id
)
712 if (FRAME_N_PARAM_FACES (f
) <= id
)
716 if (!FRAME_N_PARAM_FACES (f
))
717 FRAME_PARAM_FACES (f
)
718 = (struct face
**) xmalloc (sizeof (struct face
*) * n
);
720 FRAME_PARAM_FACES (f
)
721 = (struct face
**) xrealloc (FRAME_PARAM_FACES (f
),
722 sizeof (struct face
*) * n
);
724 bzero (FRAME_PARAM_FACES (f
) + FRAME_N_PARAM_FACES (f
),
725 (n
- FRAME_N_PARAM_FACES (f
)) * sizeof (struct face
*));
726 FRAME_N_PARAM_FACES (f
) = n
;
729 if (FRAME_PARAM_FACES (f
) [id
] == 0)
730 FRAME_PARAM_FACES (f
) [id
] = allocate_face ();
733 #ifdef HAVE_X_WINDOWS
734 /* Return non-zero if FONT1 and FONT2 have the same width.
735 We do not check the height, because we can now deal with
737 We assume that they're both character-cell fonts. */
740 same_size_fonts (font1
, font2
)
741 XFontStruct
*font1
, *font2
;
743 XCharStruct
*bounds1
= &font1
->min_bounds
;
744 XCharStruct
*bounds2
= &font2
->min_bounds
;
746 return (bounds1
->width
== bounds2
->width
);
749 /* Update the line_height of frame F according to the biggest font in
750 any face. Return nonzero if if line_height changes. */
753 frame_update_line_height (f
)
757 int biggest
= FONT_HEIGHT (f
->output_data
.x
->font
);
759 for (i
= 0; i
< f
->output_data
.x
->n_param_faces
; i
++)
760 if (f
->output_data
.x
->param_faces
[i
] != 0
761 && f
->output_data
.x
->param_faces
[i
]->font
!= (XFontStruct
*) FACE_DEFAULT
)
763 int height
= FONT_HEIGHT (f
->output_data
.x
->param_faces
[i
]->font
);
764 if (height
> biggest
)
768 if (biggest
== f
->output_data
.x
->line_height
)
771 f
->output_data
.x
->line_height
= biggest
;
774 #endif /* not HAVE_X_WINDOWS */
776 /* Modify face TO by copying from FROM all properties which have
777 nondefault settings. */
780 merge_faces (from
, to
)
781 struct face
*from
, *to
;
783 /* Only merge the font if it's the same width as the base font.
784 Otherwise ignore it, since we can't handle it properly. */
785 if (from
->font
!= (XFontStruct
*) FACE_DEFAULT
786 && same_size_fonts (from
->font
, to
->font
))
787 to
->font
= from
->font
;
788 if (from
->foreground
!= FACE_DEFAULT
)
789 to
->foreground
= from
->foreground
;
790 if (from
->background
!= FACE_DEFAULT
)
791 to
->background
= from
->background
;
792 if (from
->stipple
!= FACE_DEFAULT
)
794 to
->stipple
= from
->stipple
;
795 to
->pixmap_h
= from
->pixmap_h
;
796 to
->pixmap_w
= from
->pixmap_w
;
799 to
->underline
= from
->underline
;
802 /* Set up the basic set of facial parameters, based on the frame's
803 data; all faces are deltas applied to this. */
806 compute_base_face (f
, face
)
811 face
->foreground
= FRAME_FOREGROUND_PIXEL (f
);
812 face
->background
= FRAME_BACKGROUND_PIXEL (f
);
813 face
->font
= FRAME_FONT (f
);
818 /* Return the face ID to use to display a special glyph which selects
819 FACE_CODE as the face ID, assuming that ordinarily the face would
820 be CURRENT_FACE. F is the frame. */
823 compute_glyph_face (f
, face_code
, current_face
)
825 int face_code
, current_face
;
829 face
= *FRAME_COMPUTED_FACES (f
)[current_face
];
831 if (face_code
>= 0 && face_code
< FRAME_N_PARAM_FACES (f
)
832 && FRAME_PARAM_FACES (f
) [face_code
] != 0)
833 merge_faces (FRAME_PARAM_FACES (f
) [face_code
], &face
);
835 return intern_computed_face (f
, &face
);
838 /* Return the face ID to use to display a special glyph which selects
839 FACE_CODE as the face ID, assuming that ordinarily the face would
840 be CURRENT_FACE. F is the frame. */
843 compute_glyph_face_1 (f
, face_name
, current_face
)
845 Lisp_Object face_name
;
850 face
= *FRAME_COMPUTED_FACES (f
)[current_face
];
852 if (!NILP (face_name
))
854 int facecode
= face_name_id_number (f
, face_name
);
855 if (facecode
>= 0 && facecode
< FRAME_N_PARAM_FACES (f
)
856 && FRAME_PARAM_FACES (f
) [facecode
] != 0)
857 merge_faces (FRAME_PARAM_FACES (f
) [facecode
], &face
);
860 return intern_computed_face (f
, &face
);
863 /* Return the face ID associated with a buffer position POS.
864 Store into *ENDPTR the position at which a different face is needed.
865 This does not take account of glyphs that specify their own face codes.
866 F is the frame in use for display, and W is a window displaying
869 REGION_BEG, REGION_END delimit the region, so it can be highlighted.
871 LIMIT is a position not to scan beyond. That is to limit
872 the time this function can take.
874 If MOUSE is nonzero, use the character's mouse-face, not its face. */
877 compute_char_face (f
, w
, pos
, region_beg
, region_end
, endptr
, limit
, mouse
)
881 int region_beg
, region_end
;
887 Lisp_Object prop
, position
;
890 Lisp_Object
*overlay_vec
;
893 Lisp_Object propname
;
895 /* W must display the current buffer. We could write this function
896 to use the frame and buffer of W, but right now it doesn't. */
897 if (XBUFFER (w
->buffer
) != current_buffer
)
900 XSETFRAME (frame
, f
);
903 if (pos
< region_beg
&& region_beg
< endpos
)
906 XSETFASTINT (position
, pos
);
909 propname
= Qmouse_face
;
913 prop
= Fget_text_property (position
, propname
, w
->buffer
);
916 Lisp_Object limit1
, end
;
918 XSETFASTINT (limit1
, (limit
< endpos
? limit
: endpos
));
919 end
= Fnext_single_property_change (position
, propname
, w
->buffer
, limit1
);
928 /* First try with room for 40 overlays. */
930 overlay_vec
= (Lisp_Object
*) alloca (len
* sizeof (Lisp_Object
));
932 noverlays
= overlays_at (pos
, 0, &overlay_vec
, &len
,
933 &next_overlay
, (int *) 0);
935 /* If there are more than 40,
936 make enough space for all, and try again. */
940 overlay_vec
= (Lisp_Object
*) alloca (len
* sizeof (Lisp_Object
));
941 noverlays
= overlays_at (pos
, 0, &overlay_vec
, &len
,
942 &next_overlay
, (int *) 0);
945 if (next_overlay
< endpos
)
946 endpos
= next_overlay
;
951 /* Optimize the default case. */
952 if (noverlays
== 0 && NILP (prop
)
953 && !(pos
>= region_beg
&& pos
< region_end
))
956 compute_base_face (f
, &face
);
960 /* We have a list of faces, merge them in reverse order */
965 length
= Fsafe_length (prop
);
966 len
= XFASTINT (length
);
968 /* Put them into an array */
969 faces
= (Lisp_Object
*) alloca (len
* sizeof (Lisp_Object
));
970 for (j
= 0; j
< len
; j
++)
972 faces
[j
] = Fcar (prop
);
975 /* So that we can merge them in the reverse order */
976 for (j
= len
- 1; j
>= 0; j
--)
978 facecode
= face_name_id_number (f
, faces
[j
]);
979 if (facecode
>= 0 && facecode
< FRAME_N_PARAM_FACES (f
)
980 && FRAME_PARAM_FACES (f
) [facecode
] != 0)
981 merge_faces (FRAME_PARAM_FACES (f
) [facecode
], &face
);
984 else if (!NILP (prop
))
986 facecode
= face_name_id_number (f
, prop
);
987 if (facecode
>= 0 && facecode
< FRAME_N_PARAM_FACES (f
)
988 && FRAME_PARAM_FACES (f
) [facecode
] != 0)
989 merge_faces (FRAME_PARAM_FACES (f
) [facecode
], &face
);
992 noverlays
= sort_overlays (overlay_vec
, noverlays
, w
);
994 /* Now merge the overlay data in that order. */
995 for (i
= 0; i
< noverlays
; i
++)
997 prop
= Foverlay_get (overlay_vec
[i
], propname
);
1000 /* We have a list of faces, merge them in reverse order */
1005 length
= Fsafe_length (prop
);
1006 len
= XFASTINT (length
);
1008 /* Put them into an array */
1009 faces
= (Lisp_Object
*) alloca (len
* sizeof (Lisp_Object
));
1010 for (j
= 0; j
< len
; j
++)
1012 faces
[j
] = Fcar (prop
);
1015 /* So that we can merge them in the reverse order */
1016 for (j
= len
- 1; j
>= 0; j
--)
1018 facecode
= face_name_id_number (f
, faces
[j
]);
1019 if (facecode
>= 0 && facecode
< FRAME_N_PARAM_FACES (f
)
1020 && FRAME_PARAM_FACES (f
) [facecode
] != 0)
1021 merge_faces (FRAME_PARAM_FACES (f
) [facecode
], &face
);
1024 else if (!NILP (prop
))
1029 facecode
= face_name_id_number (f
, prop
);
1030 if (facecode
>= 0 && facecode
< FRAME_N_PARAM_FACES (f
)
1031 && FRAME_PARAM_FACES (f
) [facecode
] != 0)
1032 merge_faces (FRAME_PARAM_FACES (f
)[facecode
], &face
);
1034 oend
= OVERLAY_END (overlay_vec
[i
]);
1035 oendpos
= OVERLAY_POSITION (oend
);
1036 if (oendpos
< endpos
)
1041 if (pos
>= region_beg
&& pos
< region_end
)
1043 if (region_end
< endpos
)
1044 endpos
= region_end
;
1045 if (region_face
>= 0 && region_face
< next_face_id
)
1046 merge_faces (FRAME_PARAM_FACES (f
)[region_face
], &face
);
1051 return intern_computed_face (f
, &face
);
1054 /* Recompute the GC's for the default and modeline faces.
1055 We call this after changing frame parameters on which those GC's
1059 recompute_basic_faces (f
)
1062 /* If the frame's faces haven't been initialized yet, don't worry about
1064 if (FRAME_N_PARAM_FACES (f
) < 2)
1069 if (FRAME_DEFAULT_FACE (f
)->gc
)
1070 XFreeGC (FRAME_X_DISPLAY (f
), FRAME_DEFAULT_FACE (f
)->gc
);
1071 if (FRAME_MODE_LINE_FACE (f
)->gc
)
1072 XFreeGC (FRAME_X_DISPLAY (f
), FRAME_MODE_LINE_FACE (f
)->gc
);
1074 compute_base_face (f
, FRAME_DEFAULT_FACE (f
));
1075 compute_base_face (f
, FRAME_MODE_LINE_FACE (f
));
1077 merge_faces (FRAME_DEFAULT_PARAM_FACE (f
), FRAME_DEFAULT_FACE (f
));
1078 merge_faces (FRAME_MODE_LINE_PARAM_FACE (f
), FRAME_MODE_LINE_FACE (f
));
1080 intern_face (f
, FRAME_DEFAULT_FACE (f
));
1081 intern_face (f
, FRAME_MODE_LINE_FACE (f
));
1088 /* Lisp interface. */
1090 DEFUN ("frame-face-alist", Fframe_face_alist
, Sframe_face_alist
, 1, 1, 0,
1095 CHECK_FRAME (frame
, 0);
1096 return XFRAME (frame
)->face_alist
;
1099 DEFUN ("set-frame-face-alist", Fset_frame_face_alist
, Sset_frame_face_alist
,
1102 Lisp_Object frame
, value
;
1104 CHECK_FRAME (frame
, 0);
1105 XFRAME (frame
)->face_alist
= value
;
1110 DEFUN ("make-face-internal", Fmake_face_internal
, Smake_face_internal
, 1, 1, 0,
1111 "Create face number FACE-ID on all frames.")
1113 Lisp_Object face_id
;
1115 Lisp_Object rest
, frame
;
1116 int id
= XINT (face_id
);
1118 CHECK_NUMBER (face_id
, 0);
1119 if (id
< 0 || id
>= next_face_id
)
1120 error ("Face id out of range");
1122 FOR_EACH_FRAME (rest
, frame
)
1124 if (FRAME_MSDOS_P (XFRAME (frame
)) || FRAME_X_P (XFRAME (frame
)))
1125 ensure_face_ready (XFRAME (frame
), id
);
1131 DEFUN ("set-face-attribute-internal", Fset_face_attribute_internal
,
1132 Sset_face_attribute_internal
, 4, 4, 0, "")
1133 (face_id
, attr_name
, attr_value
, frame
)
1134 Lisp_Object face_id
, attr_name
, attr_value
, frame
;
1142 CHECK_FRAME (frame
, 0);
1143 CHECK_NUMBER (face_id
, 0);
1144 CHECK_SYMBOL (attr_name
, 0);
1147 id
= XINT (face_id
);
1148 if (id
< 0 || id
>= next_face_id
)
1149 error ("Face id out of range");
1151 if (! FRAME_X_P (f
) && ! FRAME_MSDOS_P (f
))
1154 ensure_face_ready (f
, id
);
1155 face
= FRAME_PARAM_FACES (f
) [XFASTINT (face_id
)];
1157 if (EQ (attr_name
, intern ("font")))
1159 #if defined (MSDOS) && !defined (HAVE_X_WINDOWS)
1160 /* The one and only font. Must *not* be zero (which
1161 is taken to mean an unused face nowadays). */
1162 face
->font
= (XFontStruct
*)1 ;
1164 XFontStruct
*font
= load_font (f
, attr_value
);
1165 if (face
->font
!= f
->output_data
.x
->font
)
1166 unload_font (f
, face
->font
);
1168 if (frame_update_line_height (f
))
1169 x_set_window_size (f
, 0, f
->width
, f
->height
);
1170 /* Must clear cache, since it might contain the font
1171 we just got rid of. */
1175 else if (EQ (attr_name
, intern ("foreground")))
1177 unsigned long new_color
= load_color (f
, attr_value
);
1178 unload_color (f
, face
->foreground
);
1179 face
->foreground
= new_color
;
1182 else if (EQ (attr_name
, intern ("background")))
1184 unsigned long new_color
= load_color (f
, attr_value
);
1185 unload_color (f
, face
->background
);
1186 face
->background
= new_color
;
1189 else if (EQ (attr_name
, intern ("background-pixmap")))
1192 unsigned long new_pixmap
= load_pixmap (f
, attr_value
, &w
, &h
);
1193 x_destroy_bitmap (f
, face
->stipple
);
1194 face
->stipple
= new_pixmap
;
1199 else if (EQ (attr_name
, intern ("underline")))
1201 int new = !NILP (attr_value
);
1202 face
->underline
= new;
1205 error ("unknown face attribute");
1207 if (id
== 0 || id
== 1)
1208 recompute_basic_faces (f
);
1210 /* We must redraw the frame whenever any face font or color changes,
1211 because it's possible that a merged (display) face
1212 contains the font or color we just replaced.
1213 And we must inhibit any Expose events until the redraw is done,
1214 since they would try to use the invalid display faces. */
1216 SET_FRAME_GARBAGED (f
);
1221 DEFUN ("internal-next-face-id", Finternal_next_face_id
, Sinternal_next_face_id
,
1225 return make_number (next_face_id
++);
1228 /* Return the face id for name NAME on frame FRAME.
1229 (It should be the same for all frames,
1230 but it's as easy to use the "right" frame to look it up
1231 as to use any other one.) */
1234 face_name_id_number (f
, name
)
1240 tem
= Fcdr (assq_no_quit (name
, f
->face_alist
));
1243 CHECK_VECTOR (tem
, 0);
1244 tem
= XVECTOR (tem
)->contents
[2];
1245 CHECK_NUMBER (tem
, 0);
1249 /* Emacs initialization. */
1254 Qface
= intern ("face");
1256 Qmouse_face
= intern ("mouse-face");
1257 staticpro (&Qmouse_face
);
1258 Qpixmap_spec_p
= intern ("pixmap-spec-p");
1259 staticpro (&Qpixmap_spec_p
);
1261 DEFVAR_INT ("region-face", ®ion_face
,
1262 "Face number to use to highlight the region\n\
1263 The region is highlighted with this face\n\
1264 when Transient Mark mode is enabled and the mark is active.");
1266 #ifdef HAVE_X_WINDOWS
1267 defsubr (&Spixmap_spec_p
);
1269 defsubr (&Sframe_face_alist
);
1270 defsubr (&Sset_frame_face_alist
);
1271 defsubr (&Smake_face_internal
);
1272 defsubr (&Sset_face_attribute_internal
);
1273 defsubr (&Sinternal_next_face_id
);
1276 #endif /* HAVE_FACES */