home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / e20313sr.zip / emacs / 20.3.1 / src / xfaces.c < prev    next >
C/C++ Source or Header  |  1999-07-31  |  39KB  |  1,424 lines

  1. /* "Face" primitives.
  2.    Copyright (C) 1993, 1994 Free Software Foundation.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  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.
  10.  
  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.
  15.  
  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.  */
  20.  
  21. /* This is derived from work by Lucid (some parts very loosely so).  */
  22.  
  23. /* Modified for emx by Jeremy Bowen, May 1999 based on patches
  24.    to v19.33 by Eberhard Mattes */
  25.  
  26. #include <sys/types.h>
  27. #include <sys/stat.h>
  28.  
  29. #include <config.h>
  30. #include "lisp.h"
  31.  
  32. #include "charset.h"
  33.  
  34. #include "frame.h"
  35.  
  36. /* The number of face-id's in use (same for all frames).  */
  37. static int next_face_id;
  38.  
  39. #ifdef HAVE_FACES
  40.  
  41. #ifdef HAVE_X_WINDOWS
  42. #ifdef HAVE_PM
  43. #include "pmterm.h"
  44. #include "pmemacs.h"
  45. #else /* not HAVE_PM */
  46. #include "xterm.h"
  47. #include "fontset.h"
  48. #endif /* not HAVE_PM */
  49. #endif
  50. #ifdef MSDOS
  51. #include "dosfns.h"
  52. #endif
  53. #include "buffer.h"
  54. #include "dispextern.h"
  55. #include "blockinput.h"
  56. #include "window.h"
  57. #include "intervals.h"
  58.  
  59. #ifdef HAVE_X_WINDOWS
  60. /* Compensate for bug in Xos.h on some systems, on which it requires
  61.    time.h.  On some such systems, Xos.h tries to redefine struct
  62.    timeval and struct timezone if USG is #defined while it is
  63.    #included.  */
  64. #ifdef XOS_NEEDS_TIME_H
  65.  
  66. #include <time.h>
  67. #undef USG
  68. #include <X11/Xos.h>
  69. #define USG
  70. #define __TIMEVAL__
  71.  
  72. #else
  73.  
  74. #ifndef HAVE_PM
  75. #include <X11/Xos.h>
  76. #endif /* not HAVE_PM */
  77.  
  78. #endif
  79. #endif /* HAVE_X_WINDOWS */
  80.  
  81. #ifndef NULL
  82. #define NULL (void *)0
  83. #endif
  84.  
  85.  
  86.  
  87. /* An explanation of the face data structures.  */
  88.  
  89. /* ========================= Face Data Structures =========================
  90.  
  91.    Let FACE-NAME be a symbol naming a face.
  92.  
  93.    Let FACE-VECTOR be (assq FACE-NAME (frame-face-alist FRAME))
  94.    FACE-VECTOR is either nil, or a vector of the form
  95.        [face NAME ID FONT FOREGROUND BACKGROUND BACKGROUND-PIXMAP UNDERLINE-P]
  96.    where
  97.        face is the symbol `face',
  98.        NAME is the symbol with which this vector is associated (a backpointer),
  99.        ID is the face ID, an integer used internally by the C code to identify
  100.            the face,
  101.        FONT, FOREGROUND, and BACKGROUND are strings naming the fonts and colors
  102.            to use with the face, FONT may name fontsets,
  103.        BACKGROUND-PIXMAP is the name of an x bitmap filename, which we don't
  104.            use right now, and
  105.        UNDERLINE-P is non-nil if the face should be underlined.
  106.    If any of these elements are nil, that parameter is considered
  107.    unspecified; parameters from faces specified by lower-priority
  108.    overlays or text properties, or the parameters of the frame itself,
  109.    can show through.  (lisp/faces.el maintains these lists.)
  110.  
  111.    (assq FACE-NAME global-face-data) returns a vector describing the
  112.    global parameters for that face.
  113.  
  114.    Let PARAM-FACE be FRAME->output_data.x->param_faces[Faref (FACE-VECTOR, 2)].
  115.    PARAM_FACE is a struct face whose members are the Xlib analogues of
  116.    the parameters in FACE-VECTOR.  If an element of FACE-VECTOR is
  117.    nil, then the corresponding member of PARAM_FACE is FACE_DEFAULT.
  118.    These faces are called "parameter faces", because they're the ones
  119.    lisp manipulates to control what gets displayed.  Elements 0 and 1
  120.    of FRAME->output_data.x->param_faces are special - they describe the
  121.    default and mode line faces.  None of the faces in param_faces have
  122.    GC's.  (See src/dispextern.h for the definition of struct face.
  123.    lisp/faces.el maintains the isomorphism between face_alist and
  124.    param_faces.)
  125.  
  126.    The functions compute_char_face and compute_glyph_face find and
  127.    combine the parameter faces associated with overlays and text
  128.    properties.  The resulting faces are called "computed faces"; none
  129.    of their members are FACE_DEFAULT; they are completely specified.
  130.    They then call intern_compute_face to search
  131.    FRAME->output_data.x->computed_faces for a matching face, add one if
  132.    none is found, and return the index into
  133.    FRAME->output_data.x->computed_faces.  FRAME's glyph matrices use these
  134.    indices to record the faces of the matrix characters, and the X
  135.    display hooks consult compute_faces to decide how to display these
  136.    characters.  Elements 0 and 1 of computed_faces always describe the
  137.    default and mode-line faces.
  138.  
  139.    Each computed face belongs to a particular frame.
  140.  
  141.    Computed faces have graphics contexts some of the time.
  142.    intern_face builds a GC for a specified computed face
  143.    if it doesn't have one already.
  144.    clear_face_cache clears out the GCs of all computed faces.
  145.    This is done from time to time so that we don't hold on to
  146.    lots of GCs that are no longer needed.
  147.  
  148.    If a computed face has 0 as its font,
  149.    it is unused, and can be reused by new_computed_face.
  150.  
  151.    Constraints:
  152.  
  153.    Symbols naming faces must have associations on all frames; for any
  154.    FRAME, for all FACE-NAME, if (assq FACE-NAME (frame-face-alist
  155.    FRAME)) is non-nil, it must be non-nil for all frames.
  156.  
  157.    Analogously, indices into param_faces must be valid on all frames;
  158.    if param_faces[i] is a non-zero face pointer on one frame, then it
  159.    must be filled in on all frames.  Code assumes that face ID's can
  160.    be used on any frame.
  161.  
  162.    Some subtleties:
  163.    
  164.    Why do we keep param_faces and computed_faces separate?
  165.    computed_faces contains an element for every combination of facial
  166.    parameters we have ever displayed.  indices into param_faces have
  167.    to be valid on all frames.  If they were the same array, then that
  168.    array would grow very large on all frames, because any facial
  169.    combination displayed on any frame would need to be a valid entry
  170.    on all frames.  */
  171.  
  172. /* Definitions and declarations.  */
  173.  
  174. /* The number of the face to use to indicate the region.  */
  175. static int region_face;
  176.  
  177. /* This is what appears in a slot in a face to signify that the face
  178.    does not specify that display aspect.  */
  179. #define FACE_DEFAULT (~0)
  180.  
  181. Lisp_Object Qface, Qmouse_face;
  182. Lisp_Object Qpixmap_spec_p;
  183.  
  184. int face_name_id_number ( /* FRAME_PTR, Lisp_Object name */ );
  185.  
  186. struct face *intern_face ( /* FRAME_PTR, struct face * */ );
  187. static int new_computed_face ( /* FRAME_PTR, struct face * */ );
  188. static int intern_computed_face ( /* FRAME_PTR, struct face * */ );
  189. static void ensure_face_ready ( /* FRAME_PTR, int id */ );
  190. void recompute_basic_faces ( /* FRAME_PTR f */ );
  191. static void merge_face_list ( /* FRAME_PTR, struct face *, Lisp_Object */ );
  192.  
  193. extern Lisp_Object Qforeground_color, Qbackground_color;
  194.  
  195. /* Allocating, copying, and comparing struct faces.  */
  196.  
  197. /* Allocate a new face */
  198. static struct face *
  199. allocate_face ()
  200. {
  201.   struct face *result = (struct face *) xmalloc (sizeof (struct face));
  202.   bzero (result, sizeof (struct face));
  203.   result->font = (XFontStruct *) FACE_DEFAULT;
  204.   result->fontset = -1;
  205.   result->foreground = FACE_DEFAULT;
  206.   result->background = FACE_DEFAULT;
  207.   result->stipple = FACE_DEFAULT;
  208.   return result;
  209. }
  210.  
  211. /* Make a new face that's a copy of an existing one.  */
  212. static struct face *
  213. copy_face (face)
  214.      struct face *face;
  215. {
  216.   struct face *result = allocate_face ();
  217.  
  218.   result->font = face->font;
  219.   result->fontset = face->fontset;
  220.   result->foreground = face->foreground;
  221.   result->background = face->background;
  222.   result->stipple = face->stipple;
  223.   result->underline = face->underline;
  224.   result->pixmap_h = face->pixmap_h;
  225.   result->pixmap_w = face->pixmap_w;
  226.  
  227.   return result;
  228. }
  229.  
  230. static int
  231. face_eql (face1, face2)
  232.      struct face *face1, *face2;
  233. {
  234.   return (   face1->font       == face2->font
  235.       && face1->fontset == face2->fontset
  236.       && face1->foreground == face2->foreground
  237.       && face1->background == face2->background
  238.       && face1->stipple    == face2->stipple
  239.       && face1->underline  == face2->underline);
  240. }
  241.  
  242. /* Managing graphics contexts of faces.  */
  243.  
  244. #ifdef HAVE_X_WINDOWS
  245. /* Given a computed face, construct its graphics context if necessary.  */
  246.  
  247. #ifndef HAVE_PM
  248.  
  249. struct face *
  250. intern_face (f, face)
  251.      struct frame *f;
  252.      struct face *face;
  253. {
  254.   GC gc;
  255.   XGCValues xgcv;
  256.   unsigned long mask;
  257.  
  258.   if (face->gc)
  259.     return face;
  260.  
  261.   BLOCK_INPUT;
  262.  
  263.   if (face->foreground != FACE_DEFAULT)
  264.     xgcv.foreground = face->foreground;
  265.   else
  266.     xgcv.foreground = f->output_data.x->foreground_pixel;
  267.  
  268.   if (face->background != FACE_DEFAULT)
  269.     xgcv.background = face->background;
  270.   else
  271.     xgcv.background = f->output_data.x->background_pixel;
  272.  
  273.   if (face->font && face->font != (XFontStruct *) FACE_DEFAULT)
  274.     xgcv.font = face->font->fid;
  275.   else
  276.     xgcv.font = f->output_data.x->font->fid;
  277.  
  278.   xgcv.graphics_exposures = 0;
  279.  
  280.   mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
  281.   if (face->stipple && face->stipple != FACE_DEFAULT)
  282.     {
  283.       xgcv.fill_style = FillStippled;
  284.       xgcv.stipple = x_bitmap_pixmap (f, face->stipple);
  285.       mask |= GCFillStyle | GCStipple;
  286.     }
  287.  
  288.   gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
  289.           mask, &xgcv);
  290.  
  291.   face->gc = gc;
  292.   /* We used the following GC for all non-ASCII characters by changing
  293.      only GCfont each time.  */
  294.   face->non_ascii_gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
  295.                   mask, &xgcv);
  296.  
  297.   UNBLOCK_INPUT;
  298.  
  299.   return face;
  300. }
  301.  
  302. #else /* HAVE_PM */
  303.  
  304. struct face *
  305. intern_face (f, face)
  306.      struct frame *f;
  307.      struct face *face;
  308. {
  309.   pm_request pmr;
  310.   char *name;
  311.  
  312.   if (face->gc)
  313.     return face;
  314.  
  315.   BLOCK_INPUT;
  316.  
  317.   if (face->foreground != FACE_DEFAULT)
  318.     pmr.face.foreground = face->foreground;
  319.   else
  320.     pmr.face.foreground = f->output_data.x->foreground_color;
  321.  
  322.   if (face->background != FACE_DEFAULT)
  323.     pmr.face.background = face->background;
  324.   else
  325.     pmr.face.background = f->output_data.x->background_color;
  326.  
  327.   if (face->font && (int)face->font != FACE_DEFAULT)
  328.     name = face->font->name;
  329.   else
  330.     name = f->output_data.x->font->name;
  331.  
  332.   pmr.face.name_length = strlen (name);
  333.   pmr.face.underline = face->underline;
  334.   pmr.face.serial = pm_serial++;
  335.   pmr.face.header.type = PMR_FACE;
  336.   pmr.face.header.frame = (unsigned long)f;
  337.   pm_send (&pmr, sizeof (pmr));
  338.   pm_send (name, pmr.face.name_length);
  339.   if (pm_receive (pmr.face.serial, &face->gc, 0, 0) == 0)
  340.     face->gc = 0;
  341.   UNBLOCK_INPUT;
  342.   return face;
  343. }
  344.  
  345. #endif /* HAVE_PM */
  346.  
  347. /* Clear out all graphics contexts for all computed faces
  348.    except for the default and mode line faces.
  349.    This should be done from time to time just to avoid
  350.    keeping too many graphics contexts that are no longer needed.  */
  351.  
  352. void
  353. clear_face_cache ()
  354. {
  355.   Lisp_Object tail, frame;
  356.  
  357.   BLOCK_INPUT;
  358.   FOR_EACH_FRAME (tail, frame)
  359.     {
  360.       FRAME_PTR f = XFRAME (frame);
  361.       if (FRAME_X_P (f))
  362.     {
  363.       int i;
  364.       Display *dpy = FRAME_X_DISPLAY (f);
  365.  
  366.       for (i = 2; i < FRAME_N_COMPUTED_FACES (f); i++)
  367.         {
  368.           struct face *face = FRAME_COMPUTED_FACES (f) [i];
  369.           if (face->gc)
  370.         {
  371.           XFreeGC (dpy, face->gc);
  372.           XFreeGC (dpy, face->non_ascii_gc);
  373.         }
  374.           face->gc = 0;
  375.         }
  376.     }
  377.     }
  378.  
  379.   UNBLOCK_INPUT;
  380. }
  381.  
  382. /* Allocating, freeing, and duplicating fonts, colors, and pixmaps.
  383.  
  384.    These functions operate on param faces only.
  385.    Computed faces get their fonts, colors and pixmaps
  386.    by merging param faces.  */
  387.  
  388. static XFontStruct *
  389. load_font (f, name)
  390.      struct frame *f;
  391.      Lisp_Object name;
  392. {
  393.   XFontStruct *font;
  394.  
  395.   if (NILP (name))
  396.     return (XFontStruct *) FACE_DEFAULT;
  397.  
  398.   CHECK_STRING (name, 0);
  399.   BLOCK_INPUT;
  400.   font = XLoadQueryFont (FRAME_X_DISPLAY (f), (char *) XSTRING (name)->data);
  401.   UNBLOCK_INPUT;
  402.  
  403.   if (! font)
  404.     Fsignal (Qerror, Fcons (build_string ("undefined font"),
  405.                 Fcons (name, Qnil)));
  406.   return font;
  407. }
  408.  
  409. static void
  410. unload_font (f, font)
  411.      struct frame *f;
  412.      XFontStruct *font;
  413. {
  414.   int len = FRAME_N_COMPUTED_FACES (f);
  415.   int i;
  416.  
  417.   if (!font || font == ((XFontStruct *) FACE_DEFAULT))
  418.     return;
  419.  
  420.   BLOCK_INPUT;
  421.   /* Invalidate any computed faces which use this font,
  422.      and free their GC's if they have any.  */
  423.   for (i = 2; i < len; i++)
  424.     {
  425.       struct face *face = FRAME_COMPUTED_FACES (f)[i];
  426.       if (face->font == font)
  427.     {
  428.       Display *dpy = FRAME_X_DISPLAY (f);
  429.       if (face->gc)
  430.         XFreeGC (dpy, face->gc);
  431.       face->gc = 0;
  432.       /* This marks the computed face as available to reuse.  */
  433.       face->font = 0;
  434.     }
  435.     }
  436.  
  437.   XFreeFont (FRAME_X_DISPLAY (f), font);
  438.   UNBLOCK_INPUT;
  439. }
  440.  
  441. static unsigned long
  442. load_color (f, name)
  443.      struct frame *f;
  444.      Lisp_Object name;
  445. {
  446.   XColor color;
  447.   int result;
  448.  
  449.   if (NILP (name))
  450.     return FACE_DEFAULT;
  451.  
  452.   CHECK_STRING (name, 0);
  453.   /* if the colormap is full, defined_color will return a best match
  454.      to the values in an an existing cell. */
  455.   result = defined_color (f, (char *) XSTRING (name)->data, &color, 1);
  456.   if (! result)
  457.     Fsignal (Qerror, Fcons (build_string ("undefined color"),
  458.                 Fcons (name, Qnil)));
  459.   return (unsigned long) color.pixel;
  460. }
  461.  
  462. void
  463. unload_color (f, pixel)
  464.      struct frame *f;
  465.      unsigned long pixel;
  466. {
  467. #ifndef HAVE_PM
  468.   Colormap cmap;
  469.   Display *dpy = FRAME_X_DISPLAY (f);
  470.   int class = FRAME_X_DISPLAY_INFO (f)->visual->class;
  471.  
  472.   if (pixel == FACE_DEFAULT
  473.       || pixel == BLACK_PIX_DEFAULT (f)
  474.       || pixel == WHITE_PIX_DEFAULT (f))
  475.     return;
  476.   cmap = DefaultColormapOfScreen (DefaultScreenOfDisplay (dpy));
  477.   
  478.   /* If display has an immutable color map, freeing colors is not
  479.      necessary and some servers don't allow it.  So don't do it.  */
  480.   if (! (class == StaticColor || class == StaticGray || class == TrueColor))
  481.     {
  482.       int len = FRAME_N_COMPUTED_FACES (f);
  483.       int i;
  484.  
  485.       BLOCK_INPUT;
  486.       /* Invalidate any computed faces which use this color,
  487.      and free their GC's if they have any.  */
  488.       for (i = 2; i < len; i++)
  489.     {
  490.       struct face *face = FRAME_COMPUTED_FACES (f)[i];
  491.       if (face->foreground == pixel
  492.           || face->background == pixel)
  493.         {
  494.           Display *dpy = FRAME_X_DISPLAY (f);
  495.           if (face->gc)
  496.         XFreeGC (dpy, face->gc);
  497.           face->gc = 0;
  498.           /* This marks the computed face as available to reuse.  */
  499.           face->font = 0;
  500.         }
  501.     }
  502.  
  503.       XFreeColors (dpy, cmap, &pixel, 1, (unsigned long)0);
  504.       UNBLOCK_INPUT;
  505.     }
  506. #endif /* not HAVE_PM */
  507. }
  508.  
  509. DEFUN ("pixmap-spec-p", Fpixmap_spec_p, Spixmap_spec_p, 1, 1, 0,
  510.   "Return t if OBJECT is a valid pixmap specification.")
  511.   (object)
  512.      Lisp_Object object;
  513. {
  514.   Lisp_Object height, width;
  515.  
  516.   return ((STRINGP (object)
  517.        || (CONSP (object)
  518.            && CONSP (XCONS (object)->cdr)
  519.            && CONSP (XCONS (XCONS (object)->cdr)->cdr)
  520.            && NILP (XCONS (XCONS (XCONS (object)->cdr)->cdr)->cdr)
  521.            && (width = XCONS (object)->car, INTEGERP (width))
  522.            && (height = XCONS (XCONS (object)->cdr)->car, INTEGERP (height))
  523.            && STRINGP (XCONS (XCONS (XCONS (object)->cdr)->cdr)->car)
  524.            && XINT (width) > 0
  525.            && XINT (height) > 0
  526.            /* The string must have enough bits for width * height.  */
  527.            && ((XSTRING (XCONS (XCONS (XCONS (object)->cdr)->cdr)->car)->size
  528.             * (BITS_PER_INT / sizeof (int)))
  529.            >= XFASTINT (width) * XFASTINT (height))))
  530.       ? Qt : Qnil);
  531. }
  532.  
  533. /* Load a bitmap according to NAME (which is either a file name
  534.    or a pixmap spec).  Return the bitmap_id (see xfns.c)
  535.    or get an error if NAME is invalid.
  536.  
  537.    Store the bitmap width in *W_PTR and height in *H_PTR.  */
  538.  
  539. static long
  540. load_pixmap (f, name, w_ptr, h_ptr)
  541.      FRAME_PTR f;
  542.      Lisp_Object name;
  543.      unsigned int *w_ptr, *h_ptr;
  544. {
  545. #ifdef HAVE_PM
  546.   return FACE_DEFAULT;
  547. #else /* not HAVE_PM */
  548.   int bitmap_id;
  549.   Lisp_Object tem;
  550.  
  551.   if (NILP (name))
  552.     return FACE_DEFAULT;
  553.  
  554.   tem = Fpixmap_spec_p (name);
  555.   if (NILP (tem))
  556.     wrong_type_argument (Qpixmap_spec_p, name);
  557.  
  558.   BLOCK_INPUT;
  559.  
  560.   if (CONSP (name))
  561.     {
  562.       /* Decode a bitmap spec into a bitmap.  */
  563.  
  564.       int h, w;
  565.       Lisp_Object bits;
  566.  
  567.       w = XINT (Fcar (name));
  568.       h = XINT (Fcar (Fcdr (name)));
  569.       bits = Fcar (Fcdr (Fcdr (name)));
  570.  
  571.       bitmap_id = x_create_bitmap_from_data (f, XSTRING (bits)->data,
  572.                          w, h);
  573.     }
  574.   else
  575.     {
  576.       /* It must be a string -- a file name.  */
  577.       bitmap_id = x_create_bitmap_from_file (f, name);
  578.     }
  579.   UNBLOCK_INPUT;
  580.  
  581.   if (bitmap_id < 0)
  582.     Fsignal (Qerror, Fcons (build_string ("invalid or undefined bitmap"),
  583.                 Fcons (name, Qnil)));
  584.  
  585.   *w_ptr = x_bitmap_width (f, bitmap_id);
  586.   *h_ptr = x_bitmap_height (f, bitmap_id);
  587.  
  588.   return bitmap_id;
  589. #endif /* not HAVE_PM */
  590. }
  591.  
  592. #else /* !HAVE_X_WINDOWS */
  593.  
  594. /* Stubs for MSDOS when not under X.  */
  595.  
  596. struct face *
  597. intern_face (f, face)
  598.      struct frame *f;
  599.      struct face *face;
  600. {
  601.   return face;
  602. }
  603.  
  604. void
  605. clear_face_cache ()
  606. {
  607.   /* No action.  */
  608. }
  609.  
  610. #ifdef MSDOS
  611. unsigned long
  612. load_color (f, name)
  613.      FRAME_PTR f;
  614.      Lisp_Object name;
  615. {
  616.   Lisp_Object result;
  617.  
  618.   if (NILP (name))
  619.     return FACE_DEFAULT;
  620.  
  621.   CHECK_STRING (name, 0);
  622.   result = call1 (Qmsdos_color_translate, name);
  623.   if (INTEGERP (result))
  624.     return XINT (result);
  625.   else
  626.     Fsignal (Qerror, Fcons (build_string ("undefined color"),
  627.                 Fcons (name, Qnil)));
  628. }
  629. #endif
  630. #endif /* !HAVE_X_WINDOWS */
  631.  
  632.  
  633. /* Managing parameter face arrays for frames. */
  634.  
  635. void
  636. init_frame_faces (f)
  637.      FRAME_PTR f;
  638. {
  639.   ensure_face_ready (f, 0);
  640.   ensure_face_ready (f, 1);
  641.  
  642.   FRAME_N_COMPUTED_FACES (f) = 0;
  643.   FRAME_SIZE_COMPUTED_FACES (f) = 0;
  644.  
  645.   new_computed_face (f, FRAME_PARAM_FACES (f)[0]);
  646.   new_computed_face (f, FRAME_PARAM_FACES (f)[1]);
  647.   recompute_basic_faces (f);
  648.  
  649.   /* Find another X frame.  */
  650.   {
  651.     Lisp_Object tail, frame, result;
  652.     
  653.     result = Qnil;
  654.     FOR_EACH_FRAME (tail, frame)
  655.       if ((FRAME_MSDOS_P (XFRAME (frame)) || FRAME_X_P (XFRAME (frame)))
  656.       && XFRAME (frame) != f)
  657.     {
  658.       result = frame;
  659.       break;
  660.     }
  661.  
  662.     /* If we didn't find any X frames other than f, then we don't need
  663.        any faces other than 0 and 1, so we're okay.  Otherwise, make
  664.        sure that all faces valid on the selected frame are also valid
  665.        on this new frame.  */
  666.     if (FRAMEP (result))
  667.       {
  668.     int i;
  669.     int n_faces = FRAME_N_PARAM_FACES (XFRAME (result));
  670.     struct face **faces = FRAME_PARAM_FACES (XFRAME (result));
  671.  
  672.     for (i = 2; i < n_faces; i++)
  673.       if (faces[i])
  674.         ensure_face_ready (f, i);
  675.       }
  676.   }
  677. }
  678.  
  679.  
  680. /* Called from Fdelete_frame.  */
  681.  
  682. void
  683. free_frame_faces (f)
  684.      struct frame *f;
  685. {
  686.   Display *dpy = FRAME_X_DISPLAY (f);
  687.   int i;
  688.  
  689.   BLOCK_INPUT;
  690.  
  691.   for (i = 0; i < FRAME_N_PARAM_FACES (f); i++)
  692.     {
  693.       struct face *face = FRAME_PARAM_FACES (f) [i];
  694.       if (face)
  695.     {
  696.       if (face->fontset < 0)
  697.         unload_font (f, face->font);
  698.       unload_color (f, face->foreground);
  699.       unload_color (f, face->background);
  700.       x_destroy_bitmap (f, face->stipple);
  701.       xfree (face);
  702.     }
  703.     }
  704.   xfree (FRAME_PARAM_FACES (f));
  705.   FRAME_PARAM_FACES (f) = 0;
  706.   FRAME_N_PARAM_FACES (f) = 0;
  707.  
  708.   /* All faces in FRAME_COMPUTED_FACES use resources copied from
  709.      FRAME_PARAM_FACES; we can free them without fuss.
  710.      But we do free the GCs and the face objects themselves.  */
  711.   for (i = 0; i < FRAME_N_COMPUTED_FACES (f); i++)
  712.     {
  713.       struct face *face = FRAME_COMPUTED_FACES (f) [i];
  714.       if (face)
  715.     {
  716.       if (face->gc)
  717.         {
  718.           XFreeGC (dpy, face->gc);
  719.           XFreeGC (dpy, face->non_ascii_gc);
  720.         }
  721.       xfree (face);
  722.     }
  723.     }
  724.   xfree (FRAME_COMPUTED_FACES (f));
  725.   FRAME_COMPUTED_FACES (f) = 0;
  726.   FRAME_N_COMPUTED_FACES (f) = 0;
  727.  
  728.   UNBLOCK_INPUT;
  729. }
  730.  
  731. /* Interning faces in a frame's face array.  */
  732.  
  733. static int
  734. new_computed_face (f, new_face)
  735.      struct frame *f;
  736.      struct face *new_face;
  737. {
  738.   int len = FRAME_N_COMPUTED_FACES (f);
  739.   int i;
  740.  
  741.   /* Search for an unused computed face in the middle of the table.  */
  742.   for (i = 0; i < len; i++)
  743.     {
  744.       struct face *face = FRAME_COMPUTED_FACES (f)[i];
  745.       if (face->font == 0)
  746.     {
  747.       FRAME_COMPUTED_FACES (f)[i] = copy_face (new_face);
  748.       return i;
  749.     }
  750.     }
  751.  
  752.   if (i >= FRAME_SIZE_COMPUTED_FACES (f))
  753.     {
  754.       int new_size = i + 32;
  755.  
  756.       FRAME_COMPUTED_FACES (f)
  757.     = (struct face **) (FRAME_SIZE_COMPUTED_FACES (f) == 0
  758.                 ? xmalloc (new_size * sizeof (struct face *))
  759.                 : xrealloc (FRAME_COMPUTED_FACES (f),
  760.                     new_size * sizeof (struct face *)));
  761.       FRAME_SIZE_COMPUTED_FACES (f) = new_size;
  762.     }
  763.  
  764.   i = FRAME_N_COMPUTED_FACES (f)++;
  765.   FRAME_COMPUTED_FACES (f)[i] = copy_face (new_face);
  766.   return i;
  767. }
  768.  
  769.  
  770. /* Find a match for NEW_FACE in a FRAME's computed face array, and add
  771.    it if we don't find one.  */
  772. static int
  773. intern_computed_face (f, new_face)
  774.      struct frame *f;
  775.      struct face *new_face;
  776. {
  777.   int len = FRAME_N_COMPUTED_FACES (f);
  778.   int i;
  779.  
  780.   /* Search for a computed face already on F equivalent to FACE.  */
  781.   for (i = 0; i < len; i++)
  782.     {
  783.       if (! FRAME_COMPUTED_FACES (f)[i])
  784.     abort ();
  785.       if (face_eql (new_face, FRAME_COMPUTED_FACES (f)[i]))
  786.     return i;
  787.     }
  788.  
  789.   /* We didn't find one; add a new one.  */
  790.   return new_computed_face (f, new_face);
  791. }
  792.  
  793. /* Make parameter face id ID valid on frame F.  */
  794.  
  795. static void
  796. ensure_face_ready (f, id)
  797.      struct frame *f;
  798.      int id;
  799. {
  800.   if (FRAME_N_PARAM_FACES (f) <= id)
  801.     {
  802.       int n = id + 10;
  803.       int i;
  804.       if (!FRAME_N_PARAM_FACES (f))
  805.     FRAME_PARAM_FACES (f)
  806.       = (struct face **) xmalloc (sizeof (struct face *) * n);
  807.       else
  808.     FRAME_PARAM_FACES (f)
  809.       = (struct face **) xrealloc (FRAME_PARAM_FACES (f),
  810.                        sizeof (struct face *) * n);
  811.  
  812.       bzero (FRAME_PARAM_FACES (f) + FRAME_N_PARAM_FACES (f),
  813.          (n - FRAME_N_PARAM_FACES (f)) * sizeof (struct face *));
  814.       FRAME_N_PARAM_FACES (f) = n;
  815.     }
  816.  
  817.   if (FRAME_PARAM_FACES (f) [id] == 0)
  818.     FRAME_PARAM_FACES (f) [id] = allocate_face ();
  819. }
  820.  
  821. #ifdef HAVE_X_WINDOWS
  822. /* Return non-zero if FONT1 and FONT2 have the same width.
  823.    We do not check the height, because we can now deal with
  824.    different heights.
  825.    We assume that they're both character-cell fonts.  */
  826.  
  827. int
  828. same_size_fonts (font1, font2)
  829.      XFontStruct *font1, *font2;
  830. {
  831. #ifndef HAVE_PM
  832.   XCharStruct *bounds1 = &font1->min_bounds;
  833.   XCharStruct *bounds2 = &font2->min_bounds;
  834.  
  835.   return (bounds1->width == bounds2->width);
  836. #else /* HAVE_PM */
  837.   return 1;
  838. #endif /* HAVE_PM */
  839. }
  840.  
  841. /* Update the line_height of frame F according to the biggest font in
  842.    any face.  Return nonzero if if line_height changes.  */
  843.  
  844. int
  845. frame_update_line_height (f)
  846.      FRAME_PTR f;
  847. {
  848.     int i;
  849.     int fontset = f->output_data.x->fontset;
  850.     int biggest = (
  851. #ifndef HAVE_PM
  852.         fontset > 0
  853.         ? FRAME_FONTSET_DATA (f)->fontset_table[fontset]->height
  854.         :
  855. #else
  856.         FONT_HEIGHT (f->output_data.x->font))
  857. #endif
  858.         ;
  859.  
  860.     for (i = 0; i < f->output_data.x->n_param_faces; i++)
  861.         if (f->output_data.x->param_faces[i] != 0
  862.             && f->output_data.x->param_faces[i]->font != (XFontStruct *) FACE_DEFAULT)
  863.         {
  864.             int height = (
  865. #ifndef HAVE_PM
  866.                 (fontset = f->output_data.x->param_faces[i]->fontset) > 0
  867.                 ? FRAME_FONTSET_DATA (f)->fontset_table[fontset]->height
  868.                 :
  869. #else
  870.                 FONT_HEIGHT (f->output_data.x->param_faces[i]->font)
  871. #endif
  872.                 );
  873.  
  874.             if (height > biggest)
  875.                 biggest = height;
  876.         }
  877.  
  878.     if (biggest == f->output_data.x->line_height)
  879.         return 0;
  880.  
  881.     f->output_data.x->line_height = biggest;
  882.     return 1;
  883. }
  884. #endif /* not HAVE_X_WINDOWS */
  885.  
  886. /* Modify face TO by copying from FROM all properties which have
  887.    nondefault settings.  */
  888.  
  889. static void 
  890. merge_faces (from, to)
  891.      struct face *from, *to;
  892. {
  893.   /* Only merge the font if it's the same width as the base font.
  894.      Otherwise ignore it, since we can't handle it properly.  */
  895.   if (from->font != (XFontStruct *) FACE_DEFAULT
  896.       && same_size_fonts (from->font, to->font))
  897.     to->font = from->font;
  898.   if (from->fontset != -1)
  899.     to->fontset = from->fontset;
  900.   if (from->foreground != FACE_DEFAULT)
  901.     to->foreground = from->foreground;
  902.   if (from->background != FACE_DEFAULT)
  903.     to->background = from->background;
  904.   if (from->stipple != FACE_DEFAULT)
  905.     {
  906.       to->stipple = from->stipple;
  907.       to->pixmap_h = from->pixmap_h;
  908.       to->pixmap_w = from->pixmap_w;
  909.     }
  910.   if (from->underline)
  911.     to->underline = from->underline;
  912. }
  913.  
  914. /* Set up the basic set of facial parameters, based on the frame's
  915.    data; all faces are deltas applied to this.  */
  916.  
  917. static void
  918. compute_base_face (f, face)
  919.      FRAME_PTR f;
  920.      struct face *face;
  921. {
  922.   face->gc = 0;
  923.   face->foreground = FRAME_FOREGROUND_PIXEL (f);
  924.   face->background = FRAME_BACKGROUND_PIXEL (f);
  925.   face->font = FRAME_FONT (f);
  926.   face->fontset = -1;
  927.   face->stipple = 0;
  928.   face->underline = 0;
  929. }
  930.  
  931. /* Return the face ID to use to display a special glyph which selects
  932.    FACE_CODE as the face ID, assuming that ordinarily the face would
  933.    be CURRENT_FACE.  F is the frame.  */
  934.  
  935. int
  936. compute_glyph_face (f, face_code, current_face)
  937.      struct frame *f;
  938.      int face_code, current_face;
  939. {
  940.   struct face face;
  941.  
  942.   face = *FRAME_COMPUTED_FACES (f)[current_face];
  943.  
  944.   if (face_code >= 0 && face_code < FRAME_N_PARAM_FACES (f)
  945.       && FRAME_PARAM_FACES (f) [face_code] != 0)
  946.     merge_faces (FRAME_PARAM_FACES (f) [face_code], &face);
  947.  
  948.   return intern_computed_face (f, &face);
  949. }
  950.  
  951. /* Return the face ID to use to display a special glyph which selects
  952.    FACE_CODE as the face ID, assuming that ordinarily the face would
  953.    be CURRENT_FACE.  F is the frame.  */
  954.  
  955. int
  956. compute_glyph_face_1 (f, face_name, current_face)
  957.      struct frame *f;
  958.      Lisp_Object face_name;
  959.      int current_face;
  960. {
  961.   struct face face;
  962.  
  963.   face = *FRAME_COMPUTED_FACES (f)[current_face];
  964.  
  965.   if (!NILP (face_name))
  966.     {
  967.       int facecode = face_name_id_number (f, face_name);
  968.       if (facecode >= 0 && facecode < FRAME_N_PARAM_FACES (f)
  969.       && FRAME_PARAM_FACES (f) [facecode] != 0)
  970.     merge_faces (FRAME_PARAM_FACES (f) [facecode], &face);
  971.     }
  972.  
  973.   return intern_computed_face (f, &face);
  974. }
  975.  
  976. /* Return the face ID associated with a buffer position POS.
  977.    Store into *ENDPTR the position at which a different face is needed.
  978.    This does not take account of glyphs that specify their own face codes.
  979.    F is the frame in use for display, and W is a window displaying
  980.    the current buffer.
  981.  
  982.    REGION_BEG, REGION_END delimit the region, so it can be highlighted.
  983.  
  984.    LIMIT is a position not to scan beyond.  That is to limit
  985.    the time this function can take.
  986.  
  987.    If MOUSE is nonzero, use the character's mouse-face, not its face.  */
  988.  
  989. int
  990. compute_char_face (f, w, pos, region_beg, region_end, endptr, limit, mouse)
  991.      struct frame *f;
  992.      struct window *w;
  993.      int pos;
  994.      int region_beg, region_end;
  995.      int *endptr;
  996.      int limit;
  997.      int mouse;
  998. {
  999.   struct face face;
  1000.   Lisp_Object prop, position;
  1001.   int i, j, noverlays;
  1002.   int facecode;
  1003.   Lisp_Object *overlay_vec;
  1004.   Lisp_Object frame;
  1005.   int endpos;
  1006.   Lisp_Object propname;
  1007.  
  1008.   /* W must display the current buffer.  We could write this function
  1009.      to use the frame and buffer of W, but right now it doesn't.  */
  1010.   if (XBUFFER (w->buffer) != current_buffer)
  1011.     abort ();
  1012.  
  1013.   XSETFRAME (frame, f);
  1014.  
  1015.   endpos = ZV;
  1016.   if (pos < region_beg && region_beg < endpos)
  1017.     endpos = region_beg;
  1018.  
  1019.   XSETFASTINT (position, pos);
  1020.  
  1021.   if (mouse)
  1022.     propname = Qmouse_face;
  1023.   else
  1024.     propname = Qface;
  1025.  
  1026.   prop = Fget_text_property (position, propname, w->buffer);
  1027.  
  1028.   {
  1029.     Lisp_Object limit1, end;
  1030.  
  1031.     XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
  1032.     end = Fnext_single_property_change (position, propname, w->buffer, limit1);
  1033.     if (INTEGERP (end))
  1034.       endpos = XINT (end);
  1035.   }
  1036.  
  1037.   {
  1038.     int next_overlay;
  1039.     int len;
  1040.  
  1041.     /* First try with room for 40 overlays.  */
  1042.     len = 40;
  1043.     overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
  1044.     
  1045.     noverlays = overlays_at (pos, 0, &overlay_vec, &len,
  1046.                  &next_overlay, (int *) 0);
  1047.  
  1048.     /* If there are more than 40,
  1049.        make enough space for all, and try again.  */
  1050.     if (noverlays > len)
  1051.       {
  1052.     len = noverlays;
  1053.     overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
  1054.     noverlays = overlays_at (pos, 0, &overlay_vec, &len,
  1055.                  &next_overlay, (int *) 0);
  1056.       }
  1057.  
  1058.     if (next_overlay < endpos)
  1059.       endpos = next_overlay;
  1060.   }
  1061.  
  1062.   *endptr = endpos;
  1063.  
  1064.   /* Optimize the default case.  */
  1065.   if (noverlays == 0 && NILP (prop)
  1066.       && !(pos >= region_beg && pos < region_end))
  1067.     return 0;
  1068.  
  1069.   compute_base_face (f, &face);
  1070.  
  1071.   merge_face_list (f, &face, prop);
  1072.  
  1073.   noverlays = sort_overlays (overlay_vec, noverlays, w);
  1074.  
  1075.   /* Now merge the overlay data in that order.  */
  1076.   for (i = 0; i < noverlays; i++)
  1077.     {
  1078.       Lisp_Object oend;
  1079.       int oendpos;
  1080.  
  1081.       prop = Foverlay_get (overlay_vec[i], propname);
  1082.       merge_face_list (f, &face, prop);
  1083.  
  1084.       oend = OVERLAY_END (overlay_vec[i]);
  1085.       oendpos = OVERLAY_POSITION (oend);
  1086.       if (oendpos < endpos)
  1087.     endpos = oendpos;
  1088.     }
  1089.  
  1090.   if (pos >= region_beg && pos < region_end)
  1091.     {
  1092.       if (region_end < endpos)
  1093.     endpos = region_end;
  1094.       if (region_face >= 0 && region_face < next_face_id)
  1095.     merge_faces (FRAME_PARAM_FACES (f)[region_face], &face);
  1096.     }
  1097.  
  1098.   *endptr = endpos;
  1099.  
  1100.   return intern_computed_face (f, &face);
  1101. }
  1102.  
  1103. static void
  1104. merge_face_list (f, face, prop)
  1105.      FRAME_PTR f;
  1106.      struct face *face;
  1107.      Lisp_Object prop;
  1108. {
  1109.   Lisp_Object length;
  1110.   int len;
  1111.   Lisp_Object *faces;
  1112.   int j;
  1113.  
  1114.   if (CONSP (prop)
  1115.       && ! STRINGP (XCONS (prop)->cdr))
  1116.     {
  1117.       /* We have a list of faces, merge them in reverse order.  */
  1118.  
  1119.       length = Fsafe_length (prop);
  1120.       len = XFASTINT (length);
  1121.  
  1122.       /* Put them into an array.  */
  1123.       faces = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
  1124.       for (j = 0; j < len; j++)
  1125.     {
  1126.       faces[j] = Fcar (prop);
  1127.       prop = Fcdr (prop);
  1128.     }
  1129.       /* So that we can merge them in the reverse order.  */
  1130.     }
  1131.   else
  1132.     {
  1133.       faces = (Lisp_Object *) alloca (sizeof (Lisp_Object));
  1134.       faces[0] = prop;
  1135.       len = 1;
  1136.     }
  1137.  
  1138.   for (j = len - 1; j >= 0; j--)
  1139.     {
  1140.       if (CONSP (faces[j]))
  1141.     {
  1142.       if (EQ (XCONS (faces[j])->car, Qbackground_color))
  1143.         face->background = load_color (f, XCONS (faces[j])->cdr);
  1144.       if (EQ (XCONS (faces[j])->car, Qforeground_color))
  1145.         face->foreground = load_color (f, XCONS (faces[j])->cdr);
  1146.     }
  1147.       else
  1148.     {
  1149.       int facecode = face_name_id_number (f, faces[j]);
  1150.       if (facecode >= 0 && facecode < FRAME_N_PARAM_FACES (f)
  1151.           && FRAME_PARAM_FACES (f) [facecode] != 0)
  1152.         merge_faces (FRAME_PARAM_FACES (f) [facecode], face);
  1153.     }
  1154.     }
  1155. }
  1156.  
  1157.  
  1158. /* Recompute the GC's for the default and modeline faces.
  1159.    We call this after changing frame parameters on which those GC's
  1160.    depend.  */
  1161.  
  1162. void
  1163. recompute_basic_faces (f)
  1164.      FRAME_PTR f;
  1165. {
  1166.   /* If the frame's faces haven't been initialized yet, don't worry about
  1167.      this stuff.  */
  1168.   if (FRAME_N_PARAM_FACES (f) < 2)
  1169.     return;
  1170.  
  1171.   BLOCK_INPUT;
  1172.  
  1173.   if (FRAME_DEFAULT_FACE (f)->gc)
  1174.     {
  1175.       XFreeGC (FRAME_X_DISPLAY (f), FRAME_DEFAULT_FACE (f)->gc);
  1176.       XFreeGC (FRAME_X_DISPLAY (f), FRAME_DEFAULT_FACE (f)->non_ascii_gc);
  1177.     }
  1178.   if (FRAME_MODE_LINE_FACE (f)->gc)
  1179.     {
  1180.       XFreeGC (FRAME_X_DISPLAY (f), FRAME_MODE_LINE_FACE (f)->gc);
  1181.       XFreeGC (FRAME_X_DISPLAY (f), FRAME_MODE_LINE_FACE (f)->non_ascii_gc);
  1182.     }
  1183.   compute_base_face (f, FRAME_DEFAULT_FACE (f));
  1184.   compute_base_face (f, FRAME_MODE_LINE_FACE (f));
  1185.  
  1186.   merge_faces (FRAME_DEFAULT_PARAM_FACE (f), FRAME_DEFAULT_FACE (f));
  1187.   merge_faces (FRAME_MODE_LINE_PARAM_FACE (f), FRAME_MODE_LINE_FACE (f));
  1188.   
  1189.   intern_face (f, FRAME_DEFAULT_FACE (f));
  1190.   intern_face (f, FRAME_MODE_LINE_FACE (f));
  1191.  
  1192.   UNBLOCK_INPUT;
  1193. }
  1194.  
  1195.  
  1196.  
  1197. /* Lisp interface. */
  1198.  
  1199. DEFUN ("make-face-internal", Fmake_face_internal, Smake_face_internal, 1, 1, 0,
  1200.   "Create face number FACE-ID on all frames.")
  1201.   (face_id)
  1202.      Lisp_Object face_id;
  1203. {
  1204.   Lisp_Object rest, frame;
  1205.   int id = XINT (face_id);
  1206.  
  1207.   CHECK_NUMBER (face_id, 0);
  1208.   if (id < 0 || id >= next_face_id)
  1209.     error ("Face id out of range");
  1210.  
  1211.   FOR_EACH_FRAME (rest, frame)
  1212.     {
  1213.       if (FRAME_MSDOS_P (XFRAME (frame)) || FRAME_X_P (XFRAME (frame)))
  1214.     ensure_face_ready (XFRAME (frame), id);
  1215.     }
  1216.   return Qnil;
  1217. }
  1218.  
  1219.  
  1220. DEFUN ("set-face-attribute-internal", Fset_face_attribute_internal,
  1221.        Sset_face_attribute_internal, 4, 4, 0, "")
  1222.      (face_id, attr_name, attr_value, frame)
  1223.      Lisp_Object face_id, attr_name, attr_value, frame;
  1224. {
  1225.     struct face *face;
  1226.     struct frame *f;
  1227.     int magic_p;
  1228.     int id;
  1229.     int garbaged = 0;
  1230.  
  1231.     CHECK_FRAME (frame, 0);
  1232.     CHECK_NUMBER (face_id, 0);
  1233.     CHECK_SYMBOL (attr_name, 0);
  1234.  
  1235.     f = XFRAME (frame);
  1236.     id = XINT (face_id);
  1237.     if (id < 0 || id >= next_face_id)
  1238.         error ("Face id out of range");
  1239.  
  1240.     if (! FRAME_X_P (f) && ! FRAME_MSDOS_P (f))
  1241.         return Qnil;
  1242.  
  1243.     ensure_face_ready (f, id);
  1244.     face = FRAME_PARAM_FACES (f) [XFASTINT (face_id)];
  1245.  
  1246.     if (EQ (attr_name, intern ("font")))
  1247.     {
  1248. #if defined (MSDOS) && !defined (HAVE_X_WINDOWS)
  1249.         /* The one and only font.  Must *not* be zero (which
  1250.            is taken to mean an unused face nowadays).  */
  1251.         face->font = (XFontStruct *)1 ;
  1252. #else
  1253.         XFontStruct *font;
  1254.         int fontset;
  1255.  
  1256.         if (NILP (attr_value))
  1257.         {
  1258.             font = (XFontStruct *) FACE_DEFAULT;
  1259.             fontset = -1;
  1260.         }
  1261.         else
  1262.         {
  1263. #ifndef HAVE_PM
  1264.             CHECK_STRING (attr_value, 0);
  1265.             fontset = fs_query_fontset (f, XSTRING (attr_value)->data);
  1266.             if (fontset >= 0)
  1267.             {
  1268.                 struct font_info *fontp;
  1269.           
  1270.                 if (!(fontp = FS_LOAD_FONT (f, FRAME_X_FONT_TABLE (f),
  1271.                                             CHARSET_ASCII, NULL, fontset)))
  1272.                     Fsignal (Qerror,
  1273.                              Fcons (build_string ("ASCII font can't be loaded"),
  1274.                                     Fcons (attr_value, Qnil)));
  1275.                 font = (XFontStruct *) (fontp->font);
  1276.             }
  1277.             else
  1278. #endif
  1279.                 font = load_font (f, attr_value);
  1280.         }
  1281. #ifndef HAVE_PM
  1282.         if (face->fontset == -1 && face->font != f->output_data.x->font)
  1283.             unload_font (f, face->font);
  1284. #endif
  1285.         face->font = font;
  1286.         face->fontset = fontset;
  1287.         if (frame_update_line_height (f))
  1288.             x_set_window_size (f, 0, f->width, f->height);
  1289.         /* Must clear cache, since it might contain the font
  1290.            we just got rid of.  */
  1291.         garbaged = 1;
  1292. #endif
  1293.     }
  1294.     else if (EQ (attr_name, intern ("foreground")))
  1295.     {
  1296.         unsigned long new_color = load_color (f, attr_value);
  1297.         unload_color (f, face->foreground);
  1298.         face->foreground = new_color;
  1299.         garbaged = 1;
  1300.     }
  1301.     else if (EQ (attr_name, intern ("background")))
  1302.     {
  1303.         unsigned long new_color = load_color (f, attr_value);
  1304.         unload_color (f, face->background);
  1305.         face->background = new_color;
  1306.         garbaged = 1;
  1307.     }
  1308.     else if (EQ (attr_name, intern ("background-pixmap")))
  1309.     {
  1310.         unsigned int w, h;
  1311.         unsigned long new_pixmap = load_pixmap (f, attr_value, &w, &h);
  1312.         x_destroy_bitmap (f, face->stipple);
  1313.         face->stipple = new_pixmap;
  1314.         face->pixmap_w = w;
  1315.         face->pixmap_h = h;
  1316.         garbaged = 1;
  1317.     }
  1318.     else if (EQ (attr_name, intern ("underline")))
  1319.     {
  1320.         int new = !NILP (attr_value);
  1321.         face->underline = new;
  1322.     }
  1323.     else
  1324.         error ("unknown face attribute");
  1325.  
  1326.     if (id == 0 || id == 1)
  1327.         recompute_basic_faces (f);
  1328.  
  1329.     /* We must redraw the frame whenever any face font or color changes,
  1330.        because it's possible that a merged (display) face
  1331.        contains the font or color we just replaced.
  1332.        And we must inhibit any Expose events until the redraw is done,
  1333.        since they would try to use the invalid display faces.  */
  1334.     if (garbaged)
  1335.     {
  1336.         SET_FRAME_GARBAGED (f);
  1337. #ifdef HAVE_X_WINDOWS
  1338.         FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 1;
  1339. #endif
  1340.     }
  1341.  
  1342.     return Qnil;
  1343. }
  1344. /* Return the face id for name NAME on frame FRAME.
  1345.    (It should be the same for all frames,
  1346.    but it's as easy to use the "right" frame to look it up
  1347.    as to use any other one.)  */
  1348.  
  1349. int
  1350. face_name_id_number (f, name)
  1351.      FRAME_PTR f;
  1352.      Lisp_Object name;
  1353. {
  1354.   Lisp_Object tem;
  1355.  
  1356.   tem = Fcdr (assq_no_quit (name, f->face_alist));
  1357.   if (NILP (tem))
  1358.     return 0;
  1359.   CHECK_VECTOR (tem, 0);
  1360.   tem = XVECTOR (tem)->contents[2];
  1361.   CHECK_NUMBER (tem, 0);
  1362.   return XINT (tem);
  1363. }
  1364.  
  1365. #endif /* HAVE_FACES */
  1366.  
  1367.  
  1368. DEFUN ("frame-face-alist", Fframe_face_alist, Sframe_face_alist, 1, 1, 0,
  1369.        "")
  1370.      (frame)
  1371.      Lisp_Object frame;
  1372. {
  1373.   CHECK_FRAME (frame, 0);
  1374.   return XFRAME (frame)->face_alist;
  1375. }
  1376.  
  1377. DEFUN ("set-frame-face-alist", Fset_frame_face_alist, Sset_frame_face_alist,
  1378.        2, 2, 0, "")
  1379.      (frame, value)
  1380.      Lisp_Object frame, value;
  1381. {
  1382.   CHECK_FRAME (frame, 0);
  1383.   XFRAME (frame)->face_alist = value;
  1384.   return value;
  1385. }
  1386.  
  1387. DEFUN ("internal-next-face-id", Finternal_next_face_id, Sinternal_next_face_id,
  1388.   0, 0, 0, "")
  1389.   ()
  1390. {
  1391.   return make_number (next_face_id++);
  1392. }
  1393.  
  1394. /* Emacs initialization.  */
  1395.  
  1396. void
  1397. syms_of_xfaces ()
  1398. {
  1399. #ifdef HAVE_FACES
  1400.   Qface = intern ("face");
  1401.   staticpro (&Qface);
  1402.   Qmouse_face = intern ("mouse-face");
  1403.   staticpro (&Qmouse_face);
  1404.   Qpixmap_spec_p = intern ("pixmap-spec-p");
  1405.   staticpro (&Qpixmap_spec_p);
  1406.  
  1407.   DEFVAR_INT ("region-face", ®ion_face,
  1408.     "Face number to use to highlight the region\n\
  1409. The region is highlighted with this face\n\
  1410. when Transient Mark mode is enabled and the mark is active.");
  1411.  
  1412.   defsubr (&Smake_face_internal);
  1413.   defsubr (&Sset_face_attribute_internal);
  1414. #endif /* HAVE_FACES */
  1415.  
  1416. #ifdef HAVE_X_WINDOWS
  1417.   defsubr (&Spixmap_spec_p);
  1418. #endif
  1419.  
  1420.   defsubr (&Sframe_face_alist);
  1421.   defsubr (&Sset_frame_face_alist);
  1422.   defsubr (&Sinternal_next_face_id);
  1423. }
  1424.