(unload_color): Don't free colors if immutable colormap.
[bpt/emacs.git] / src / xfaces.c
index 2b235e0..59e09d9 100644 (file)
@@ -145,10 +145,10 @@ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
 /* Definitions and declarations.  */
 
 /* The number of face-id's in use (same for all frames).  */
-int next_face_id;
+static int next_face_id;
 
 /* The number of the face to use to indicate the region.  */
-int region_face;
+static int region_face;
 
 /* This is what appears in a slot in a face to signify that the face
    does not specify that display aspect.  */
@@ -238,7 +238,7 @@ intern_face (f, face)
   else
     xgcv.background = f->display.x->background_pixel;
 
-  if (face->font && (int) face->font != FACE_DEFAULT)
+  if (face->font && face->font != (XFontStruct *) FACE_DEFAULT)
     xgcv.font = face->font->fid;
   else
     xgcv.font = f->display.x->font->fid;
@@ -340,29 +340,19 @@ load_color (f, name)
      struct frame *f;
      Lisp_Object name;
 {
-  Display *dpy = FRAME_X_DISPLAY (f);
-  Colormap cmap;
   XColor color;
   int result;
 
   if (NILP (name))
     return FACE_DEFAULT;
 
-  cmap = DefaultColormapOfScreen (DefaultScreenOfDisplay (dpy));
-
   CHECK_STRING (name, 0);
-  BLOCK_INPUT;
-  result = XParseColor (dpy, cmap, (char *) XSTRING (name)->data, &color);
-  UNBLOCK_INPUT;
+  /* if the colormap is full, defined_color will return a best match
+     to the values in an an existing cell. */
+  result = defined_color(f, (char *) XSTRING (name)->data, &color, 1);
   if (! result)
     Fsignal (Qerror, Fcons (build_string ("undefined color"),
                            Fcons (name, Qnil)));
-  BLOCK_INPUT;
-  result = XAllocColor (dpy, cmap, &color);
-  UNBLOCK_INPUT;
-  /* Ignore the return value of XallocColor, so that
-     we use a color close to the one requested
-     if we can't get the exact request.  */
   return (unsigned long) color.pixel;
 }
 
@@ -373,14 +363,22 @@ unload_color (f, pixel)
 {
   Colormap cmap;
   Display *dpy = FRAME_X_DISPLAY (f);
+  int class = FRAME_X_DISPLAY_INFO (f)->visual->class;
+
   if (pixel == FACE_DEFAULT
       || pixel == BLACK_PIX_DEFAULT (f)
       || pixel == WHITE_PIX_DEFAULT (f))
     return;
   cmap = DefaultColormapOfScreen (DefaultScreenOfDisplay (dpy));
-  BLOCK_INPUT;
-  XFreeColors (dpy, cmap, &pixel, 1, 0);
-  UNBLOCK_INPUT;
+  
+  /* If display has an immutable color map, freeing colors is not
+     necessary and some servers don't allow it.  So don't do it.  */
+  if (! (class == StaticColor || class == StaticGray || class == TrueColor))
+    {
+      BLOCK_INPUT;
+      XFreeColors (dpy, cmap, &pixel, 1, (unsigned long)0);
+      UNBLOCK_INPUT;
+    }
 }
 
 DEFUN ("pixmap-spec-p", Fpixmap_spec_p, Spixmap_spec_p, 1, 1, 0,
@@ -480,14 +478,6 @@ clear_face_cache ()
   /* No action.  */
 }
 
-static void
-build_face (f, face)
-     struct frame *f;
-     struct face *face;
-{
-  face->gc = 1;
-}
-
 #ifdef MSDOS
 unsigned long
 load_color (f, name)
@@ -884,7 +874,8 @@ compute_char_face (f, w, pos, region_beg, region_end, endptr, limit, mouse)
     len = 40;
     overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
     
-    noverlays = overlays_at (pos, 0, &overlay_vec, &len, &next_overlay, NULL);
+    noverlays = overlays_at (pos, 0, &overlay_vec, &len,
+                            &next_overlay, (int *) 0);
 
     /* If there are more than 40,
        make enough space for all, and try again.  */
@@ -893,7 +884,7 @@ compute_char_face (f, w, pos, region_beg, region_end, endptr, limit, mouse)
        len = noverlays;
        overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
        noverlays = overlays_at (pos, 0, &overlay_vec, &len,
-                                &next_overlay, NULL);
+                                &next_overlay, (int *) 0);
       }
 
     if (next_overlay < endpos)
@@ -912,10 +903,13 @@ compute_char_face (f, w, pos, region_beg, region_end, endptr, limit, mouse)
   if (CONSP (prop))
     {
       /* We have a list of faces, merge them in reverse order */
-      Lisp_Object length = Flength (prop);
-      int len = XINT (length);
+      Lisp_Object length;
+      int len;
       Lisp_Object *faces;
 
+      length = Fsafe_length (prop);
+      len = XFASTINT (length);
+
       /* Put them into an array */
       faces = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
       for (j = 0; j < len; j++)
@@ -949,10 +943,12 @@ compute_char_face (f, w, pos, region_beg, region_end, endptr, limit, mouse)
       if (CONSP (prop))
        {
          /* We have a list of faces, merge them in reverse order */
-         Lisp_Object length = Flength (prop);
-         int len = XINT (length);
+         Lisp_Object length;
+         int len;
          Lisp_Object *faces;
-         int i;
+
+         length = Fsafe_length (prop);
+         len = XFASTINT (length);
 
          /* Put them into an array */
          faces = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));