]> code.delx.au - gnu-emacs/blobdiff - src/xterm.c
(w32-charset-info-alist): Map vietnamese to windows-1258.
[gnu-emacs] / src / xterm.c
index 68f5b266810a7b3bded3deb7aeff2abf695f7d02..5dda7ed2c5d3fe0121328a5b2cbe228c7ef8e45b 100644 (file)
@@ -1,6 +1,7 @@
 /* X Communication module for terminals which understand the X protocol.
    Copyright (C) 1989, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
-                 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
+                 2002, 2003, 2004, 2005, 2006, 2007, 2008
+                 Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
@@ -67,6 +68,7 @@ Boston, MA 02110-1301, USA.  */
 /* #include <sys/param.h>  */
 
 #include "charset.h"
+#include "character.h"
 #include "coding.h"
 #include "ccl.h"
 #include "frame.h"
@@ -75,7 +77,7 @@ Boston, MA 02110-1301, USA.  */
 #include "termhooks.h"
 #include "termopts.h"
 #include "termchar.h"
-#include "gnu.h"
+#include "emacs-icon.h"
 #include "disptab.h"
 #include "buffer.h"
 #include "window.h"
@@ -100,6 +102,10 @@ Boston, MA 02110-1301, USA.  */
 #include "gtkutil.h"
 #endif
 
+#ifdef USE_FONT_BACKEND
+#include "font.h"
+#endif /* USE_FONT_BACKEND */
+
 #ifdef USE_LUCID
 extern int xlwmenu_window_p P_ ((Widget w, Window window));
 extern void xlwmenu_redisplay P_ ((Widget));
@@ -154,8 +160,6 @@ extern void _XEditResCheckMessages ();
 #endif
 #endif
 
-#define abs(x) ((x) < 0 ? -(x) : (x))
-
 /* Default to using XIM if available.  */
 #ifdef USE_XIM
 int use_xim = 1;
@@ -874,7 +878,8 @@ XTreset_terminal_modes (struct terminal *terminal)
 
 /* Function prototypes of this page.  */
 
-static int x_encode_char P_ ((int, XChar2b *, struct font_info *, int *));
+static int x_encode_char P_ ((int, XChar2b *, struct font_info *,
+                             struct charset *, int *));
 
 
 /* Get metrics of character CHAR2B in FONT.  Value is null if CHAR2B
@@ -953,13 +958,13 @@ x_per_char_metric (font, char2b, font_type)
    the two-byte form of C.  Encoding is returned in *CHAR2B.  */
 
 static int
-x_encode_char (c, char2b, font_info, two_byte_p)
+x_encode_char (c, char2b, font_info, charset, two_byte_p)
      int c;
      XChar2b *char2b;
      struct font_info *font_info;
+     struct charset *charset;
      int *two_byte_p;
 {
-  int charset = CHAR_CHARSET (c);
   XFontStruct *font = font_info->font;
 
   /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
@@ -973,31 +978,31 @@ x_encode_char (c, char2b, font_info, two_byte_p)
       check_ccl_update (ccl);
       if (CHARSET_DIMENSION (charset) == 1)
        {
-         ccl->reg[0] = charset;
+         ccl->reg[0] = CHARSET_ID (charset);
          ccl->reg[1] = char2b->byte2;
          ccl->reg[2] = -1;
        }
       else
        {
-         ccl->reg[0] = charset;
+         ccl->reg[0] = CHARSET_ID (charset);
          ccl->reg[1] = char2b->byte1;
          ccl->reg[2] = char2b->byte2;
        }
 
-      ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
+      ccl_driver (ccl, NULL, NULL, 0, 0, Qnil);
 
       /* We assume that MSBs are appropriately set/reset by CCL
         program.  */
       if (font->max_byte1 == 0)        /* 1-byte font */
-       char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
+       STORE_XCHAR2B (char2b, 0, ccl->reg[1]);
       else
-       char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
+       STORE_XCHAR2B (char2b, ccl->reg[1], ccl->reg[2]);
     }
-  else if (font_info->encoding[charset])
+  else if (font_info->encoding_type)
     {
       /* Fixed encoding scheme.  See fontset.h for the meaning of the
         encoding numbers.  */
-      int enc = font_info->encoding[charset];
+      unsigned char enc = font_info->encoding_type;
 
       if ((enc == 1 || enc == 2)
          && CHARSET_DIMENSION (charset) == 2)
@@ -1093,6 +1098,11 @@ x_set_cursor_gc (s)
        }
 
       IF_DEBUG (x_check_font (s->f, s->font));
+#ifdef USE_FONT_BACKEND
+      if (enable_font_backend)
+       xgcv.font = FRAME_X_DISPLAY_INFO (s->f)->font->fid;
+      else
+#endif
       xgcv.font = s->font->fid;
       xgcv.graphics_exposures = False;
       mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
@@ -1125,15 +1135,20 @@ x_set_mouse_face_gc (s)
     face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
 
   if (s->first_glyph->type == CHAR_GLYPH)
-    face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
+    face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch, -1, Qnil);
   else
-    face_id = FACE_FOR_CHAR (s->f, face, 0);
+    face_id = FACE_FOR_CHAR (s->f, face, 0, -1, Qnil);
   s->face = FACE_FROM_ID (s->f, face_id);
   PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
 
   /* If font in this face is same as S->font, use it.  */
   if (s->font == s->face->font)
     s->gc = s->face->gc;
+#ifdef USE_FONT_BACKEND
+  else if (enable_font_backend)
+    /* No need of setting a font for s->gc.  */
+    s->gc = s->face->gc;
+#endif /* USE_FONT_BACKEND */
   else
     {
       /* Otherwise construct scratch_cursor_gc with values from FACE
@@ -1144,6 +1159,11 @@ x_set_mouse_face_gc (s)
       xgcv.background = s->face->background;
       xgcv.foreground = s->face->foreground;
       IF_DEBUG (x_check_font (s->f, s->font));
+#ifdef USE_FONT_BACKEND
+      if (enable_font_backend)
+       xgcv.font = FRAME_X_DISPLAY_INFO (s->f)->font->fid;
+      else
+#endif
       xgcv.font = s->font->fid;
       xgcv.graphics_exposures = False;
       mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
@@ -1227,16 +1247,61 @@ x_set_glyph_string_gc (s)
 static INLINE void
 x_set_glyph_string_clipping (s)
      struct glyph_string *s;
+{
+#ifdef USE_FONT_BACKEND
+  XRectangle *r = s->clip;
+#else
+  XRectangle r[2];
+#endif
+  int n = get_glyph_string_clip_rects (s, r, 2);
+
+  if (n > 0)
+    XSetClipRectangles (s->display, s->gc, 0, 0, r, n, Unsorted);
+#ifdef USE_FONT_BACKEND
+  s->num_clips = n;
+#endif
+}
+
+
+/* Set SRC's clipping for output of glyph string DST.  This is called
+   when we are drawing DST's left_overhang or right_overhang only in
+   the area of SRC.  */
+
+static void
+x_set_glyph_string_clipping_exactly (src, dst)
+     struct glyph_string *src, *dst;
 {
   XRectangle r;
-  get_glyph_string_clip_rect (s, &r);
-  XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
+
+#ifdef USE_FONT_BACKEND
+  if (enable_font_backend)
+    {
+      r.x = src->x;
+      r.width = src->width;
+      r.y = src->y;
+      r.height = src->height;
+      dst->clip[0] = r;
+      dst->num_clips = 1;
+    }
+  else
+    {
+#endif /* USE_FONT_BACKEND */
+  struct glyph_string *clip_head = src->clip_head;
+  struct glyph_string *clip_tail = src->clip_tail;
+
+  /* This foces clipping just this glyph string.  */
+  src->clip_head = src->clip_tail = src;
+  get_glyph_string_clip_rect (src, &r);
+  src->clip_head = clip_head, src->clip_tail = clip_tail;
+#ifdef USE_FONT_BACKEND
+    }
+#endif /* USE_FONT_BACKEND */
+  XSetClipRectangles (dst->display, dst->gc, 0, 0, &r, 1, Unsorted);
 }
 
 
 /* RIF:
-   Compute left and right overhang of glyph string S.  If S is a glyph
-   string for a composition, assume overhangs don't exist.  */
+   Compute left and right overhang of glyph string S.  */
 
 static void
 x_compute_glyph_string_overhangs (s)
@@ -1247,11 +1312,34 @@ x_compute_glyph_string_overhangs (s)
     {
       XCharStruct cs;
       int direction, font_ascent, font_descent;
+
+#ifdef USE_FONT_BACKEND
+      if (enable_font_backend)
+       {
+         unsigned *code = alloca (sizeof (unsigned) * s->nchars);
+         struct font *font = (struct font *) s->font_info;
+         struct font_metrics metrics;
+         int i;
+
+         for (i = 0; i < s->nchars; i++)
+           code[i] = (s->char2b[i].byte1 << 8) | s->char2b[i].byte2;
+         font->driver->text_extents (font, code, s->nchars, &metrics);
+         cs.rbearing = metrics.rbearing;
+         cs.lbearing = metrics.lbearing;
+         cs.width = metrics.width;
+       }
+      else
+#endif /* USE_FONT_BACKEND */
       XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
                      &font_ascent, &font_descent, &cs);
       s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
       s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
     }
+  else if (s->cmp)
+    {
+      s->right_overhang = s->cmp->rbearing - s->cmp->pixel_width;
+      s->left_overhang = - s->cmp->lbearing;
+    }
 }
 
 
@@ -1324,7 +1412,7 @@ x_draw_glyph_string_foreground (s)
      of S to the right of that box line.  */
   if (s->face->box != FACE_NO_BOX
       && s->first_glyph->left_box_line_p)
-    x = s->x + abs (s->face->box_line_width);
+    x = s->x + eabs (s->face->box_line_width);
   else
     x = s->x;
 
@@ -1341,6 +1429,26 @@ x_draw_glyph_string_foreground (s)
          x += g->pixel_width;
        }
     }
+#ifdef USE_FONT_BACKEND
+  else if (enable_font_backend)
+    {
+      int boff = s->font_info->baseline_offset;
+      struct font *font = (struct font *) s->font_info;
+      int y;
+
+      if (s->font_info->vertical_centering)
+       boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
+
+      y = s->ybase - boff;
+      if (s->for_overlaps
+         || (s->background_filled_p && s->hl != DRAW_CURSOR))
+       font->driver->draw (s, 0, s->nchars, x, y, 0);
+      else
+       font->driver->draw (s, 0, s->nchars, x, y, 1);
+      if (s->face->overstrike)
+       font->driver->draw (s, 0, s->nchars, x + 1, y, 0);
+    }
+#endif /* USE_FONT_BACKEND */
   else
     {
       char *char1b = (char *) s->char2b;
@@ -1400,13 +1508,13 @@ static void
 x_draw_composite_glyph_string_foreground (s)
      struct glyph_string *s;
 {
-  int i, x;
+  int i, j, x;
 
   /* If first glyph of S has a left box line, start drawing the text
      of S to the right of that box line.  */
-  if (s->face->box != FACE_NO_BOX
+  if (s->face && s->face->box != FACE_NO_BOX
       && s->first_glyph->left_box_line_p)
-    x = s->x + abs (s->face->box_line_width);
+    x = s->x + eabs (s->face->box_line_width);
   else
     x = s->x;
 
@@ -1423,20 +1531,78 @@ x_draw_composite_glyph_string_foreground (s)
        XDrawRectangle (s->display, s->window, s->gc, x, s->y,
                        s->width - 1, s->height - 1);
     }
-  else
+#ifdef USE_FONT_BACKEND
+  else if (enable_font_backend)
     {
-      for (i = 0; i < s->nchars; i++, ++s->gidx)
+      struct font *font = (struct font *) s->font_info;
+      int y = s->ybase;
+      int width = 0;
+
+      if (s->cmp->method == COMPOSITION_WITH_GLYPH_STRING)
        {
-         XDrawString16 (s->display, s->window, s->gc,
-                        x + s->cmp->offsets[s->gidx * 2],
-                        s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
-                        s->char2b + i, 1);
-         if (s->face->overstrike)
-           XDrawString16 (s->display, s->window, s->gc,
-                          x + s->cmp->offsets[s->gidx * 2] + 1,
-                          s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
-                          s->char2b + i, 1);
+         Lisp_Object gstring = AREF (XHASH_TABLE (composition_hash_table)
+                                     ->key_and_value,
+                                     s->cmp->hash_index * 2);
+         int from;
+
+         for (i = from = 0; i < s->nchars; i++)
+           {
+             Lisp_Object g = LGSTRING_GLYPH (gstring, i);
+             Lisp_Object adjustment = LGLYPH_ADJUSTMENT (g);
+             int xoff, yoff, wadjust;
+
+             if (! VECTORP (adjustment))
+               {
+                 width += LGLYPH_WIDTH (g);
+                 continue;
+               }
+             if (from < i)
+               {
+                 font->driver->draw (s, from, i, x, y, 0);
+                 x += width;
+               }
+             xoff = XINT (AREF (adjustment, 0));
+             yoff = XINT (AREF (adjustment, 1));
+             wadjust = XINT (AREF (adjustment, 2));
+
+             font->driver->draw (s, i, i + 1, x + xoff, y + yoff, 0);
+             x += wadjust;
+             from = i + 1;
+             width = 0;
+           }
+         if (from < i)
+           font->driver->draw (s, from, i, x, y, 0);
        }
+      else
+       {
+         for (i = 0, j = s->gidx; i < s->nchars; i++, j++)
+           if (COMPOSITION_GLYPH (s->cmp, j) != '\t')
+             {
+               int xx = x + s->cmp->offsets[j * 2];
+               int yy = y - s->cmp->offsets[j * 2 + 1];
+
+               font->driver->draw (s, j, j + 1, xx, yy, 0);
+               if (s->face->overstrike)
+                 font->driver->draw (s, j, j + 1, xx + 1, yy, 0);
+             }
+       }
+    }
+#endif /* USE_FONT_BACKEND */
+  else
+    {
+      for (i = 0, j = s->gidx; i < s->nchars; i++, j++)
+       if (s->face)
+         {
+           XDrawString16 (s->display, s->window, s->gc,
+                          x + s->cmp->offsets[j * 2],
+                          s->ybase - s->cmp->offsets[j * 2 + 1],
+                          s->char2b + j, 1);
+           if (s->face->overstrike)
+             XDrawString16 (s->display, s->window, s->gc,
+                            x + s->cmp->offsets[j * 2] + 1,
+                            s->ybase - s->cmp->offsets[j * 2 + 1],
+                            s->char2b + j, 1);
+         }
     }
 }
 
@@ -1472,8 +1638,8 @@ x_frame_of_widget (widget)
 
   /* Look for a frame with that top-level widget.  Allocate the color
      on that frame to get the right gamma correction value.  */
-  for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
-    if (GC_FRAMEP (XCAR (tail))
+  for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
+    if (FRAMEP (XCAR (tail))
        && (f = XFRAME (XCAR (tail)),
            (FRAME_X_P (f)
              && f->output_data.nothing != 1
@@ -2202,7 +2368,7 @@ x_draw_glyph_string_box (s)
                ? s->first_glyph
                : s->first_glyph + s->nchars - 1);
 
-  width = abs (s->face->box_line_width);
+  width = eabs (s->face->box_line_width);
   raised_p = s->face->box == FACE_RAISED_BOX;
   left_x = s->x;
   right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
@@ -2248,7 +2414,7 @@ x_draw_image_foreground (s)
   if (s->face->box != FACE_NO_BOX
       && s->first_glyph->left_box_line_p
       && s->slice.x == 0)
-    x += abs (s->face->box_line_width);
+    x += eabs (s->face->box_line_width);
 
   /* If there is a margin around the image, adjust x- and y-position
      by that margin.  */
@@ -2341,7 +2507,7 @@ x_draw_image_relief (s)
   if (s->face->box != FACE_NO_BOX
       && s->first_glyph->left_box_line_p
       && s->slice.x == 0)
-    x += abs (s->face->box_line_width);
+    x += eabs (s->face->box_line_width);
 
   /* If there is a margin around the image, adjust x- and y-position
      by that margin.  */
@@ -2358,7 +2524,7 @@ x_draw_image_relief (s)
     }
   else
     {
-      thick = abs (s->img->relief);
+      thick = eabs (s->img->relief);
       raised_p = s->img->relief > 0;
     }
 
@@ -2393,7 +2559,7 @@ x_draw_image_foreground_1 (s, pixmap)
   if (s->face->box != FACE_NO_BOX
       && s->first_glyph->left_box_line_p
       && s->slice.x == 0)
-    x += abs (s->face->box_line_width);
+    x += eabs (s->face->box_line_width);
 
   /* If there is a margin around the image, adjust x- and y-position
      by that margin.  */
@@ -2493,7 +2659,7 @@ static void
 x_draw_image_glyph_string (s)
      struct glyph_string *s;
 {
-  int box_line_hwidth = abs (s->face->box_line_width);
+  int box_line_hwidth = eabs (s->face->box_line_width);
   int box_line_vwidth = max (s->face->box_line_width, 0);
   int height;
   Pixmap pixmap = None;
@@ -2599,7 +2765,6 @@ x_draw_stretch_glyph_string (s)
      struct glyph_string *s;
 {
   xassert (s->first_glyph->type == STRETCH_GLYPH);
-  s->stippled_p = s->face->stipple != 0;
 
   if (s->hl == DRAW_CURSOR
       && !x_stretch_cursor_p)
@@ -2685,15 +2850,25 @@ x_draw_glyph_string (s)
 {
   int relief_drawn_p = 0;
 
-  /* If S draws into the background of its successor, draw the
-     background of the successor first so that S can draw into it.
+  /* If S draws into the background of its successors, draw the
+     background of the successors first so that S can draw into it.
      This makes S->next use XDrawString instead of XDrawImageString.  */
   if (s->next && s->right_overhang && !s->for_overlaps)
     {
-      xassert (s->next->img == NULL);
-      x_set_glyph_string_gc (s->next);
-      x_set_glyph_string_clipping (s->next);
-      x_draw_glyph_string_background (s->next, 1);
+      int width;
+      struct glyph_string *next;
+
+      for (width = 0, next = s->next; next;
+          width += next->width, next = next->next)
+       if (next->first_glyph->type != IMAGE_GLYPH)
+         {
+           x_set_glyph_string_gc (next);
+           x_set_glyph_string_clipping (next);
+           x_draw_glyph_string_background (next, 1);
+#ifdef USE_FONT_BACKEND
+           next->num_clips = 0;
+#endif /* USE_FONT_BACKEND */
+         }
     }
 
   /* Set up S->gc, set clipping and draw S.  */
@@ -2713,6 +2888,12 @@ x_draw_glyph_string (s)
       x_set_glyph_string_clipping (s);
       relief_drawn_p = 1;
     }
+  else if ((s->prev && s->prev->hl != s->hl && s->left_overhang)
+          || (s->next && s->next->hl != s->hl && s->right_overhang))
+    /* We must clip just this glyph.  left_overhang part has already
+       drawn when s->prev was drawn, and right_overhang part will be
+       drawn later when s->next is drawn. */
+    x_set_glyph_string_clipping_exactly (s, s);
   else
     x_set_glyph_string_clipping (s);
 
@@ -2755,26 +2936,45 @@ x_draw_glyph_string (s)
          int y;
 
          /* Get the underline thickness.  Default is 1 pixel.  */
+#ifdef USE_FONT_BACKEND
+         if (enable_font_backend)
+           /* In the future, we must use information of font.  */
+           h = 1;
+         else
+#endif /* USE_FONT_BACKEND */
          if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
            h = 1;
 
-         y = s->y + s->height - h;
-         if (!x_underline_at_descent_line)
-            {
-             /* Get the underline position.  This is the recommended
-                 vertical offset in pixels from the baseline to the top of
-                 the underline.  This is a signed value according to the
-                 specs, and its default is
-
-                ROUND ((maximum descent) / 2), with
-                ROUND(x) = floor (x + 0.5)  */
-
-              if (x_use_underline_position_properties
-                  && XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &tem))
-                y = s->ybase + (long) tem;
-              else if (s->face->font)
-                y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
-            }
+#ifdef USE_FONT_BACKEND
+         if (enable_font_backend)
+           {
+             if (s->face->font)
+               /* In the future, we must use information of font.  */
+               y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
+             else
+               y = s->y + s->height - h;
+           }
+         else
+#endif
+           {
+             y = s->y + s->height - h;
+             if (!x_underline_at_descent_line)
+               {
+                 /* Get the underline position.  This is the recommended
+                    vertical offset in pixels from the baseline to the top of
+                    the underline.  This is a signed value according to the
+                    specs, and its default is
+
+                    ROUND ((maximum descent) / 2), with
+                    ROUND(x) = floor (x + 0.5)  */
+
+                 if (x_use_underline_position_properties
+                     && XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &tem))
+                   y = s->ybase + (long) tem;
+                 else if (s->face->font)
+                   y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
+               }
+           }
 
          if (s->face->underline_defaulted_p)
            XFillRectangle (s->display, s->window, s->gc,
@@ -2832,10 +3032,67 @@ x_draw_glyph_string (s)
       /* Draw relief if not yet drawn.  */
       if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
        x_draw_glyph_string_box (s);
+
+      if (s->prev)
+       {
+         struct glyph_string *prev;
+
+         for (prev = s->prev; prev; prev = prev->prev)
+           if (prev->hl != s->hl
+               && prev->x + prev->width + prev->right_overhang > s->x)
+             {
+               /* As prev was drawn while clipped to its own area, we
+                  must draw the right_overhang part using s->hl now.  */
+               enum draw_glyphs_face save = prev->hl;
+
+               prev->hl = s->hl;
+               x_set_glyph_string_gc (prev);
+               x_set_glyph_string_clipping_exactly (s, prev);
+               if (prev->first_glyph->type == CHAR_GLYPH)
+                 x_draw_glyph_string_foreground (prev);
+               else
+                 x_draw_composite_glyph_string_foreground (prev);
+               XSetClipMask (prev->display, prev->gc, None);
+               prev->hl = save;
+#ifdef USE_FONT_BACKEND
+               prev->num_clips = 0;
+#endif /* USE_FONT_BACKEND */
+             }
+       }
+
+      if (s->next)
+       {
+         struct glyph_string *next;
+
+         for (next = s->next; next; next = next->next)
+           if (next->hl != s->hl
+               && next->x - next->left_overhang < s->x + s->width)
+             {
+               /* As next will be drawn while clipped to its own area,
+                  we must draw the left_overhang part using s->hl now.  */
+               enum draw_glyphs_face save = next->hl;
+
+               next->hl = s->hl;
+               x_set_glyph_string_gc (next);
+               x_set_glyph_string_clipping_exactly (s, next);
+               if (next->first_glyph->type == CHAR_GLYPH)
+                 x_draw_glyph_string_foreground (next);
+               else
+                 x_draw_composite_glyph_string_foreground (next);
+               XSetClipMask (next->display, next->gc, None);
+               next->hl = save;
+#ifdef USE_FONT_BACKEND
+               next->num_clips = 0;
+#endif /* USE_FONT_BACKEND */
+             }
+       }
     }
 
   /* Reset clipping.  */
   XSetClipMask (s->display, s->gc, None);
+#ifdef USE_FONT_BACKEND
+  s->num_clips = 0;
+#endif /* USE_FONT_BACKEND */
 }
 
 /* Shift display to make room for inserted glyphs.   */
@@ -3288,9 +3545,9 @@ x_focus_changed (type, state, dpyinfo, frame, bufp)
 
           /* Don't stop displaying the initial startup message
              for a switch-frame event we don't need.  */
-          if (GC_NILP (Vterminal_frame)
-              && GC_CONSP (Vframe_list)
-              && !GC_NILP (XCDR (Vframe_list)))
+          if (NILP (Vterminal_frame)
+              && CONSP (Vframe_list)
+              && !NILP (XCDR (Vframe_list)))
             {
               bufp->kind = FOCUS_IN_EVENT;
               XSETFRAME (bufp->frame_or_window, frame);
@@ -3363,6 +3620,15 @@ x_detect_focus_change (dpyinfo, event, bufp)
                        FOCUS_IMPLICIT : FOCUS_EXPLICIT),
                       dpyinfo, frame, bufp);
       break;
+
+    case ClientMessage:
+      if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
+       {
+         enum xembed_message msg = event->xclient.data.l[1];
+         x_focus_changed ((msg == XEMBED_FOCUS_IN ? FocusIn : FocusOut),
+                          FOCUS_EXPLICIT, dpyinfo, frame, bufp);
+       }
+      break;
     }
 }
 
@@ -3400,7 +3666,7 @@ x_frame_rehighlight (dpyinfo)
   if (dpyinfo->x_focus_frame)
     {
       dpyinfo->x_highlight_frame
-       = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
+       = ((FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
           ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
           : dpyinfo->x_focus_frame);
       if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
@@ -3441,12 +3707,7 @@ x_find_modifier_meanings (dpyinfo)
   dpyinfo->super_mod_mask = 0;
   dpyinfo->hyper_mod_mask = 0;
 
-#ifdef HAVE_X11R4
   XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
-#else
-  min_code = dpyinfo->display->min_keycode;
-  max_code = dpyinfo->display->max_keycode;
-#endif
 
   syms = XGetKeyboardMapping (dpyinfo->display,
                              min_code, max_code - min_code + 1,
@@ -3946,15 +4207,13 @@ x_window_to_scroll_bar (display, window_id)
   window_id = (Window) xg_get_scroll_id_for_window (display, window_id);
 #endif /* USE_GTK  && USE_TOOLKIT_SCROLL_BARS */
 
-  for (tail = Vframe_list;
-       XGCTYPE (tail) == Lisp_Cons;
-       tail = XCDR (tail))
+  for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
     {
       Lisp_Object frame, bar, condemned;
 
       frame = XCAR (tail);
       /* All elements of Vframe_list should be frames.  */
-      if (! GC_FRAMEP (frame))
+      if (! FRAMEP (frame))
        abort ();
 
       if (! FRAME_X_P (XFRAME (frame)))
@@ -3966,11 +4225,11 @@ x_window_to_scroll_bar (display, window_id)
       for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
           /* This trick allows us to search both the ordinary and
               condemned scroll bar lists with one loop.  */
-          ! GC_NILP (bar) || (bar = condemned,
+          ! NILP (bar) || (bar = condemned,
                               condemned = Qnil,
-                              ! GC_NILP (bar));
+                              ! NILP (bar));
           bar = XSCROLL_BAR (bar)->next)
-       if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id &&
+       if (XSCROLL_BAR (bar)->x_window == window_id &&
             FRAME_X_DISPLAY (XFRAME (frame)) == display)
          return XSCROLL_BAR (bar);
     }
@@ -3990,9 +4249,7 @@ x_window_to_menu_bar (window)
 {
   Lisp_Object tail;
 
-  for (tail = Vframe_list;
-       XGCTYPE (tail) == Lisp_Cons;
-       tail = XCDR (tail))
+  for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
     {
       if (FRAME_X_P (XFRAME (XCAR (tail))))
         {
@@ -4288,9 +4545,8 @@ xm_scroll_callback (widget, client_data, call_data)
     }
 }
 
+#elif defined USE_GTK
 
-#else /* !USE_MOTIF, i.e. Xaw or GTK */
-#ifdef USE_GTK
 /* Scroll bar callback for GTK scroll bars.  WIDGET is the scroll
    bar widget.  DATA is a pointer to the scroll_bar structure. */
 
@@ -4361,7 +4617,7 @@ xg_scroll_callback (widget, data)
     }
 }
 
-#else /* not USE_GTK */
+#else /* not USE_GTK and not USE_MOTIF */
 
 /* Xaw scroll bar callback.  Invoked when the thumb is dragged.
    WIDGET is the scroll bar widget.  CLIENT_DATA is a pointer to the
@@ -4387,7 +4643,7 @@ xaw_jump_callback (widget, client_data, call_data)
   whole = 10000000;
   portion = shown < 1 ? top * whole : 0;
 
-  if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
+  if (shown < 1 && (eabs (top + shown - 1) < 1.0/height))
     /* Some derivatives of Xaw refuse to shrink the thumb when you reach
        the bottom, so we force the scrolling whenever we see that we're
        too close to the bottom (in x_set_toolkit_scroll_bar_thumb
@@ -4428,12 +4684,12 @@ xaw_scroll_callback (widget, client_data, call_data)
   XtVaGetValues (widget, XtNheight, &height, NULL);
   UNBLOCK_INPUT;
 
-  if (abs (position) >= height)
+  if (eabs (position) >= height)
     part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
 
   /* If Xaw3d was compiled with ARROW_SCROLLBAR,
      it maps line-movement to call_data = max(5, height/20).  */
-  else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
+  else if (xaw3d_arrow_scroll && eabs (position) <= max (5, height / 20))
     part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
   else
     part = scroll_bar_move_ratio;
@@ -4444,8 +4700,7 @@ xaw_scroll_callback (widget, client_data, call_data)
   x_send_scroll_bar_event (bar->window, part, position, height);
 }
 
-#endif /* not USE_GTK */
-#endif /* not USE_MOTIF */
+#endif /* not USE_GTK and not USE_MOTIF */
 
 #define SCROLL_BAR_NAME "verticalScrollBar"
 
@@ -4651,7 +4906,7 @@ x_create_toolkit_scroll_bar (f, bar)
   /* Remember X window and widget in the scroll bar vector.  */
   SET_SCROLL_BAR_X_WIDGET (bar, widget);
   xwindow = XtWindow (widget);
-  SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
+  bar->x_window = xwindow;
 
   UNBLOCK_INPUT;
 }
@@ -4795,7 +5050,7 @@ x_scroll_bar_create (w, top, left, width, height)
 {
   struct frame *f = XFRAME (w->frame);
   struct scroll_bar *bar
-    = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
+    = ALLOCATE_PSEUDOVECTOR (struct scroll_bar, x_window, PVEC_OTHER);
 
   BLOCK_INPUT;
 
@@ -4839,21 +5094,19 @@ x_scroll_bar_create (w, top, left, width, height)
                            CopyFromParent,
                             /* Attributes.  */
                            mask, &a);
-    SET_SCROLL_BAR_X_WINDOW (bar, window);
+    bar->x_window = window;
   }
 #endif /* not USE_TOOLKIT_SCROLL_BARS */
 
   XSETWINDOW (bar->window, w);
-  XSETINT (bar->top, top);
-  XSETINT (bar->left, left);
-  XSETINT (bar->width, width);
-  XSETINT (bar->height, height);
-  XSETINT (bar->start, 0);
-  XSETINT (bar->end, 0);
+  bar->top = top;
+  bar->left = left;
+  bar->width = width;
+  bar->height = height;
+  bar->start = 0;
+  bar->end = 0;
   bar->dragging = Qnil;
-#ifdef USE_TOOLKIT_SCROLL_BARS
-  bar->fringe_extended_p = Qnil;
-#endif
+  bar->fringe_extended_p = 0;
 
   /* Add bar to its frame's list of scroll bars.  */
   bar->next = FRAME_SCROLL_BARS (f);
@@ -4867,12 +5120,12 @@ x_scroll_bar_create (w, top, left, width, height)
   {
 #ifdef USE_GTK
     xg_update_scrollbar_pos (f,
-                             SCROLL_BAR_X_WINDOW (bar),
+                             bar->x_window,
                              top,
                              left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
                              width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
                              max (height, 1));
-    xg_show_scroll_bar (SCROLL_BAR_X_WINDOW (bar));
+    xg_show_scroll_bar (bar->x_window);
 #else /* not USE_GTK */
     Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
     XtConfigureWidget (scroll_bar,
@@ -4884,7 +5137,7 @@ x_scroll_bar_create (w, top, left, width, height)
 #endif /* not USE_GTK */
     }
 #else /* not USE_TOOLKIT_SCROLL_BARS */
-  XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
+  XMapRaised (FRAME_X_DISPLAY (f), bar->x_window);
 #endif /* not USE_TOOLKIT_SCROLL_BARS */
 
   UNBLOCK_INPUT;
@@ -4892,6 +5145,8 @@ x_scroll_bar_create (w, top, left, width, height)
 }
 
 
+#ifndef USE_TOOLKIT_SCROLL_BARS
+
 /* Draw BAR's handle in the proper position.
 
    If the handle is already drawn from START to END, don't bother
@@ -4905,8 +5160,6 @@ x_scroll_bar_create (w, top, left, width, height)
    the bar's top is as far down as it goes; otherwise, there's no way
    to move to the very end of the buffer.  */
 
-#ifndef USE_TOOLKIT_SCROLL_BARS
-
 static void
 x_scroll_bar_set_handle (bar, start, end, rebuild)
      struct scroll_bar *bar;
@@ -4914,22 +5167,22 @@ x_scroll_bar_set_handle (bar, start, end, rebuild)
      int rebuild;
 {
   int dragging = ! NILP (bar->dragging);
-  Window w = SCROLL_BAR_X_WINDOW (bar);
+  Window w = bar->x_window;
   FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
   GC gc = f->output_data.x->normal_gc;
 
   /* If the display is already accurate, do nothing.  */
   if (! rebuild
-      && start == XINT (bar->start)
-      && end == XINT (bar->end))
+      && start == bar->start
+      && end == bar->end)
     return;
 
   BLOCK_INPUT;
 
   {
-    int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
-    int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
-    int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
+    int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, bar->width);
+    int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, bar->height);
+    int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
 
     /* Make sure the values are reasonable, and try to preserve
        the distance between start and end.  */
@@ -4949,8 +5202,8 @@ x_scroll_bar_set_handle (bar, start, end, rebuild)
     }
 
     /* Store the adjusted setting in the scroll bar.  */
-    XSETINT (bar->start, start);
-    XSETINT (bar->end, end);
+    bar->start = start;
+    bar->end = end;
 
     /* Clip the end position, just for display.  */
     if (end > top_range)
@@ -5017,12 +5270,12 @@ x_scroll_bar_remove (bar)
 
 #ifdef USE_TOOLKIT_SCROLL_BARS
 #ifdef USE_GTK
-  xg_remove_scroll_bar (f, SCROLL_BAR_X_WINDOW (bar));
+  xg_remove_scroll_bar (f, bar->x_window);
 #else /* not USE_GTK */
   XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
 #endif /* not USE_GTK */
 #else
-  XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
+  XDestroyWindow (FRAME_X_DISPLAY (f), bar->x_window);
 #endif
 
   /* Disassociate this scroll bar from its window.  */
@@ -5120,19 +5373,19 @@ XTset_vertical_scroll_bar (w, portion, whole, position)
 
       BLOCK_INPUT;
 
-      if (sb_left != XINT (bar->left))
+      if (sb_left != bar->left)
        mask |= CWX;
-      if (top != XINT (bar->top))
+      if (top != bar->top)
        mask |= CWY;
-      if (sb_width != XINT (bar->width))
+      if (sb_width != bar->width)
        mask |= CWWidth;
-      if (height != XINT (bar->height))
+      if (height != bar->height)
        mask |= CWHeight;
 
 #ifdef USE_TOOLKIT_SCROLL_BARS
 
       /* Move/size the scroll bar widget.  */
-      if (mask || !NILP (bar->fringe_extended_p) != fringe_extended_p)
+      if (mask || bar->fringe_extended_p != fringe_extended_p)
        {
          /* Since toolkit scroll bars are smaller than the space reserved
             for them on the frame, we have to clear "under" them.  */
@@ -5147,7 +5400,7 @@ XTset_vertical_scroll_bar (w, portion, whole, position)
            }
 #ifdef USE_GTK
           xg_update_scrollbar_pos (f,
-                                   SCROLL_BAR_X_WINDOW (bar),
+                                   bar->x_window,
                                    top,
                                    sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
                                    sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM *2,
@@ -5203,23 +5456,23 @@ XTset_vertical_scroll_bar (w, portion, whole, position)
          wc.y = top;
          wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
          wc.height = height;
-         XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
+         XConfigureWindow (FRAME_X_DISPLAY (f), bar->x_window,
                            mask, &wc);
        }
 
 #endif /* not USE_TOOLKIT_SCROLL_BARS */
 
       /* Remember new settings.  */
-      XSETINT (bar->left, sb_left);
-      XSETINT (bar->top, top);
-      XSETINT (bar->width, sb_width);
-      XSETINT (bar->height, height);
+      bar->left = sb_left;
+      bar->top = top;
+      bar->width = sb_width;
+      bar->height = height;
 
       UNBLOCK_INPUT;
     }
 
 #ifdef USE_TOOLKIT_SCROLL_BARS
-  bar->fringe_extended_p = fringe_extended_p ? Qt : Qnil;
+  bar->fringe_extended_p = fringe_extended_p;
 
   x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
 #else /* not USE_TOOLKIT_SCROLL_BARS */
@@ -5363,14 +5616,14 @@ x_scroll_bar_expose (bar, event)
      struct scroll_bar *bar;
      XEvent *event;
 {
-  Window w = SCROLL_BAR_X_WINDOW (bar);
+  Window w = bar->x_window;
   FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
   GC gc = f->output_data.x->normal_gc;
   int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
 
   BLOCK_INPUT;
 
-  x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
+  x_scroll_bar_set_handle (bar, bar->start, bar->end, 1);
 
   /* Switch to scroll bar foreground color.  */
   if (f->output_data.x->scroll_bar_foreground_pixel != -1)
@@ -5382,8 +5635,8 @@ x_scroll_bar_expose (bar, event)
 
                  /* x, y, width, height */
                  0, 0,
-                 XINT (bar->width) - 1 - width_trim - width_trim,
-                 XINT (bar->height) - 1);
+                 bar->width - 1 - width_trim - width_trim,
+                 bar->height - 1);
 
    /* Restore the foreground color of the GC if we changed it above.  */
    if (f->output_data.x->scroll_bar_foreground_pixel != -1)
@@ -5408,7 +5661,7 @@ x_scroll_bar_handle_click (bar, event, emacs_event)
      XEvent *event;
      struct input_event *emacs_event;
 {
-  if (! GC_WINDOWP (bar->window))
+  if (! WINDOWP (bar->window))
     abort ();
 
   emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
@@ -5427,18 +5680,18 @@ x_scroll_bar_handle_click (bar, event, emacs_event)
 #if 0
     FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
     int internal_height
-      = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
+      = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, bar->height);
 #endif
     int top_range
-      = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
+      = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
     int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
 
     if (y < 0) y = 0;
     if (y > top_range) y = top_range;
 
-    if (y < XINT (bar->start))
+    if (y < bar->start)
       emacs_event->part = scroll_bar_above_handle;
-    else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
+    else if (y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
       emacs_event->part = scroll_bar_handle;
     else
       emacs_event->part = scroll_bar_below_handle;
@@ -5451,7 +5704,7 @@ x_scroll_bar_handle_click (bar, event, emacs_event)
        holding it.  */
     if (event->type == ButtonPress
        && emacs_event->part == scroll_bar_handle)
-      XSETINT (bar->dragging, y - XINT (bar->start));
+      XSETINT (bar->dragging, y - bar->start);
 #endif
 
 #ifndef USE_TOOLKIT_SCROLL_BARS
@@ -5460,7 +5713,7 @@ x_scroll_bar_handle_click (bar, event, emacs_event)
        && ! NILP (bar->dragging))
       {
        int new_start = y - XINT (bar->dragging);
-       int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
+       int new_end = new_start + bar->end - bar->start;
 
        x_scroll_bar_set_handle (bar, new_start, new_end, 0);
        bar->dragging = Qnil;
@@ -5503,14 +5756,14 @@ x_scroll_bar_note_movement (bar, event)
   XSETVECTOR (last_mouse_scroll_bar, bar);
 
   /* If we're dragging the bar, display it.  */
-  if (! GC_NILP (bar->dragging))
+  if (! NILP (bar->dragging))
     {
       /* Where should the handle be now?  */
       int new_start = event->xmotion.y - XINT (bar->dragging);
 
-      if (new_start != XINT (bar->start))
+      if (new_start != bar->start)
        {
-         int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
+         int new_end = new_start + bar->end - bar->start;
 
          x_scroll_bar_set_handle (bar, new_start, new_end, 0);
        }
@@ -5531,7 +5784,7 @@ x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
      unsigned long *time;
 {
   struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
-  Window w = SCROLL_BAR_X_WINDOW (bar);
+  Window w = bar->x_window;
   FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
   int win_x, win_y;
   Window dummy_window;
@@ -5558,10 +5811,10 @@ x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
     {
 #if 0
       int inside_height
-       = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
+       = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, bar->height);
 #endif
       int top_range
-       = VERTICAL_SCROLL_BAR_TOP_RANGE     (f, XINT (bar->height));
+       = VERTICAL_SCROLL_BAR_TOP_RANGE     (f, bar->height);
 
       win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
 
@@ -5578,9 +5831,9 @@ x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
 
       if (! NILP (bar->dragging))
        *part = scroll_bar_handle;
-      else if (win_y < XINT (bar->start))
+      else if (win_y < bar->start)
        *part = scroll_bar_above_handle;
-      else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
+      else if (win_y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
        *part = scroll_bar_handle;
       else
        *part = scroll_bar_below_handle;
@@ -5617,7 +5870,7 @@ x_scroll_bar_clear (f)
     for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
         bar = XSCROLL_BAR (bar)->next)
       XClearArea (FRAME_X_DISPLAY (f),
-                 SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
+                 XSCROLL_BAR (bar)->x_window,
                  0, 0, 0, 0, True);
 #endif /* not USE_TOOLKIT_SCROLL_BARS */
 }
@@ -5967,6 +6220,18 @@ handle_one_xevent (dpyinfo, eventp, finish, hold_quit)
           }
 #endif /* USE_TOOLKIT_SCROLL_BARS */
 
+       /* XEmbed messages from the embedder (if any).  */
+        if (event.xclient.message_type
+           == dpyinfo->Xatom_XEMBED)
+          {
+           enum xembed_message msg = event.xclient.data.l[1];
+           if (msg == XEMBED_FOCUS_IN || msg == XEMBED_FOCUS_OUT)
+             x_detect_focus_change (dpyinfo, &event, &inev.ie);
+
+           *finish = X_EVENT_GOTO_OUT;
+            goto done;
+          }
+
        f = x_any_window_to_frame (dpyinfo, event.xclient.window);
        if (!f)
          goto OTHER;
@@ -6380,41 +6645,14 @@ handle_one_xevent (dpyinfo, eventp, finish, hold_quit)
              goto done_keysym;
            }
 
-         /* Keysyms directly mapped to supported Unicode characters.  */
-         if ((keysym >= 0x01000000 && keysym <= 0x010033ff)
-             || (keysym >= 0x0100e000 && keysym <= 0x0100ffff))
+         /* Keysyms directly mapped to Unicode characters.  */
+         if (keysym >= 0x01000000 && keysym <= 0x0110FFFF)
            {
-             int code = keysym & 0xFFFF, charset_id, c1, c2;
-
-             if (code < 0x80)
-               {
-                 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
-                 inev.ie.code = code;
-               }
-             else if (code < 0x100)
-               {
-                 if (code < 0xA0)
-                   charset_id = CHARSET_8_BIT_CONTROL;
-                 else
-                   charset_id = charset_latin_iso8859_1;
-                 inev.ie.kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
-                 inev.ie.code = MAKE_CHAR (charset_id, code, 0);
-               }
+             if (keysym < 0x01000080)
+               inev.ie.kind = ASCII_KEYSTROKE_EVENT;
              else
-               {
-                 if (code < 0x2500)
-                   charset_id = charset_mule_unicode_0100_24ff,
-                     code -= 0x100;
-                 else if (code < 0xE000)
-                   charset_id = charset_mule_unicode_2500_33ff,
-                     code -= 0x2500;
-                 else
-                   charset_id = charset_mule_unicode_e000_ffff,
-                     code -= 0xE000;
-                 c1 = (code / 96) + 32, c2 = (code % 96) + 32;
-                 inev.ie.kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
-                 inev.ie.code = MAKE_CHAR (charset_id, c1, c2);
-               }
+               inev.ie.kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
+             inev.ie.code = keysym & 0xFFFFFF;
              goto done_keysym;
            }
 
@@ -6493,14 +6731,6 @@ handle_one_xevent (dpyinfo, eventp, finish, hold_quit)
                         || (orig_keysym & (1 << 28))
                         || (keysym != NoSymbol && nbytes == 0))
                        && ! (IsModifierKey (orig_keysym)
-#ifndef HAVE_X11R5
-#ifdef XK_Mode_switch
-                             || ((unsigned)(orig_keysym) == XK_Mode_switch)
-#endif
-#ifdef XK_Num_Lock
-                             || ((unsigned)(orig_keysym) == XK_Num_Lock)
-#endif
-#endif /* not HAVE_X11R5 */
                              /* The symbols from XK_ISO_Lock
                                 to XK_ISO_Last_Group_Lock
                                 don't have real modifiers but
@@ -6527,59 +6757,57 @@ handle_one_xevent (dpyinfo, eventp, finish, hold_quit)
            register int c;
            int nchars, len;
 
-           /* The input should be decoded with `coding_system'
-              which depends on which X*LookupString function
-              we used just above and the locale.  */
-           setup_coding_system (coding_system, &coding);
-           coding.src_multibyte = 0;
-           coding.dst_multibyte = 1;
-           /* The input is converted to events, thus we can't
-              handle composition.  Anyway, there's no XIM that
-              gives us composition information.  */
-           coding.composing = COMPOSITION_DISABLED;
-
-           if (nbytes > 0)
+           for (i = 0, nchars = 0; i < nbytes; i++)
+             {
+               if (ASCII_BYTE_P (copy_bufptr[i]))
+                 nchars++;
+               STORE_KEYSYM_FOR_DEBUG (copy_bufptr[i]);
+             }
+
+           if (nchars < nbytes)
              {
                /* Decode the input data.  */
                int require;
                unsigned char *p;
 
-               for (i = 0; i < nbytes; i++)
-                 {
-                   STORE_KEYSYM_FOR_DEBUG (copy_bufptr[i]);
-                 }
-
-               require = decoding_buffer_size (&coding, nbytes);
-               p = (unsigned char *) alloca (require);
+               /* The input should be decoded with `coding_system'
+                  which depends on which X*LookupString function
+                  we used just above and the locale.  */
+               setup_coding_system (coding_system, &coding);
+               coding.src_multibyte = 0;
+               coding.dst_multibyte = 1;
+               /* The input is converted to events, thus we can't
+                  handle composition.  Anyway, there's no XIM that
+                  gives us composition information.  */
+               coding.common_flags &= ~CODING_ANNOTATION_MASK;
+
+               require = MAX_MULTIBYTE_LENGTH * nbytes;
+               coding.destination = alloca (require);
+               coding.dst_bytes = require;
                coding.mode |= CODING_MODE_LAST_BLOCK;
-               /* We explicitly disable composition handling because
-                  key data should not contain any composition sequence.  */
-               coding.composing = COMPOSITION_DISABLED;
-               decode_coding (&coding, copy_bufptr, p, nbytes, require);
+               decode_coding_c_string (&coding, copy_bufptr, nbytes, Qnil);
                nbytes = coding.produced;
                nchars = coding.produced_char;
-               copy_bufptr = p;
+               copy_bufptr = coding.destination;
+             }
 
-               /* Convert the input data to a sequence of
-                  character events.  */
-               for (i = 0; i < nbytes; i += len)
-                 {
-                   if (nchars == nbytes)
-                     c = copy_bufptr[i], len = 1;
-                   else
-                     c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
-                                                 nbytes - i, len);
-                   inev.ie.kind = (SINGLE_BYTE_CHAR_P (c)
-                                   ? ASCII_KEYSTROKE_EVENT
-                                   : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
-                   inev.ie.code = c;
-                   kbd_buffer_store_event_hold (&inev.ie, hold_quit);
-                 }
+           /* Convert the input data to a sequence of
+              character events.  */
+           for (i = 0; i < nbytes; i += len)
+             {
+               if (nchars == nbytes)
+                 c = copy_bufptr[i], len = 1;
+               else
+                 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
+                                             nbytes - i, len);
+               inev.ie.kind = (SINGLE_BYTE_CHAR_P (c)
+                               ? ASCII_KEYSTROKE_EVENT
+                               : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
+               inev.ie.code = c;
+               kbd_buffer_store_event_hold (&inev.ie, hold_quit);
              }
 
-           /* Previous code updated count by nchars rather than nbytes,
-              but that seems bogus to me.  ++kfs  */
-           count += nbytes;
+           count += nchars;
 
            inev.ie.kind = NO_EVENT;  /* Already stored above.  */
 
@@ -6717,7 +6945,13 @@ handle_one_xevent (dpyinfo, eventp, finish, hold_quit)
                    will be selected only when it is active.  */
                 if (WINDOWP (window)
                     && !EQ (window, last_window)
-                    && !EQ (window, selected_window))
+                   && !EQ (window, selected_window)
+                   /* For click-to-focus window managers
+                      create event iff we don't leave the
+                      selected frame.  */
+                   && (focus_follows_mouse
+                       || (EQ (XWINDOW (window)->frame,
+                               XWINDOW (selected_window)->frame))))
                   {
                     inev.ie.kind = SELECT_WINDOW_EVENT;
                     inev.ie.frame_or_window = window;
@@ -6754,13 +6988,20 @@ handle_one_xevent (dpyinfo, eventp, finish, hold_quit)
 
     case ConfigureNotify:
       f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
+#ifdef USE_GTK
+      if (!f
+          && (f = x_any_window_to_frame (dpyinfo, event.xconfigure.window))
+          && event.xconfigure.window == FRAME_X_WINDOW (f))
+        {
+          xg_frame_resized (f, event.xconfigure.width,
+                            event.xconfigure.height);
+          f = 0;
+        }
+#endif  
       if (f)
         {
 #ifndef USE_X_TOOLKIT
-#ifdef USE_GTK
-          xg_resize_widgets (f, event.xconfigure.width,
-                             event.xconfigure.height);
-#else /* not USE_GTK */
+#ifndef USE_GTK
           /* If there is a pending resize for fullscreen, don't
              do this one, the right one will come later.
              The toolkit version doesn't seem to need this, but we
@@ -6790,11 +7031,11 @@ handle_one_xevent (dpyinfo, eventp, finish, hold_quit)
               SET_FRAME_GARBAGED (f);
               cancel_mouse_face (f);
             }
-#endif /* not USE_GTK */
-#endif
 
           FRAME_PIXEL_WIDTH (f) = event.xconfigure.width;
           FRAME_PIXEL_HEIGHT (f) = event.xconfigure.height;
+#endif /* not USE_GTK */
+#endif
 
 #ifdef USE_GTK
           /* GTK creates windows but doesn't map them.
@@ -6857,12 +7098,10 @@ handle_one_xevent (dpyinfo, eventp, finish, hold_quit)
 
                 if (tool_bar_p && event.xbutton.button < 4)
                   {
-                    if (event.xbutton.type == ButtonPress)
-                      handle_tool_bar_click (f, x, y, 1, 0);
-                    else
-                      handle_tool_bar_click (f, x, y, 0,
-                                             x_x_to_emacs_modifiers (dpyinfo,
-                                                                    event.xbutton.state));
+                   handle_tool_bar_click (f, x, y,
+                                          event.xbutton.type == ButtonPress,
+                                          x_x_to_emacs_modifiers (dpyinfo,
+                                                                  event.xbutton.state));
                   }
               }
 
@@ -6885,6 +7124,9 @@ handle_one_xevent (dpyinfo, eventp, finish, hold_quit)
                   else
                     construct_mouse_click (&inev.ie, &event.xbutton, f);
                 }
+            if (FRAME_X_EMBEDDED_P (f))
+              xembed_send_message (f, event.xbutton.time,
+                                   XEMBED_REQUEST_FOCUS, 0, 0, 0);
           }
         else
           {
@@ -7072,7 +7314,9 @@ x_dispatch_event (event, display)
    We return as soon as there are no more events to be read.
 
    We return the number of characters stored into the buffer,
-   thus pretending to be `read'.
+   thus pretending to be `read' (except the characters we store
+   in the keyboard buffer can be multibyte, so are not necessarily
+   C chars).
 
    EXPECTED is nonzero if the caller knows input is available.  */
 
@@ -7085,7 +7329,9 @@ XTread_socket (terminal, expected, hold_quit)
   int count = 0;
   XEvent event;
   int event_found = 0;
+#if 0
   struct x_display_info *dpyinfo;
+#endif
 
   if (interrupt_input_blocked)
     {
@@ -7122,7 +7368,7 @@ XTread_socket (terminal, expected, hold_quit)
   if (terminal->display_info.x == XTread_socket_fake_io_error)
     {
       XTread_socket_fake_io_error = 0;
-      x_io_error_quitter (dpyinfo->display);
+      x_io_error_quitter (terminal->display_info.x->display);
     }
   
 #if 0 /* This loop is a noop now.  */
@@ -7584,7 +7830,6 @@ x_text_icon (f, icon_name)
   if (FRAME_X_WINDOW (f) == 0)
     return 1;
 
-#ifdef HAVE_X11R4
   {
     XTextProperty text;
     text.value = (unsigned char *) icon_name;
@@ -7593,9 +7838,6 @@ x_text_icon (f, icon_name)
     text.nitems = strlen (icon_name);
     XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
   }
-#else /* not HAVE_X11R4 */
-  XSetIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), icon_name);
-#endif /* not HAVE_X11R4 */
 
   if (f->output_data.x->icon_bitmap > 0)
     x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
@@ -7839,7 +8081,7 @@ x_connection_closed (dpy, error_message)
          && FRAME_X_P (XFRAME (minibuf_frame))
          && ! EQ (frame, minibuf_frame)
          && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
-       Fdelete_frame (frame, Qt);
+       Fdelete_frame (frame, Qnoelisp);
     }
 
   /* Now delete all remaining frames on the dead display.
@@ -7852,7 +8094,7 @@ x_connection_closed (dpy, error_message)
        /* Set this to t so that Fdelete_frame won't get confused
           trying to find a replacement.  */
        FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
-       Fdelete_frame (frame, Qt);
+       Fdelete_frame (frame, Qnoelisp);
       }
 
   /* We have to close the display to inform Xt that it doesn't
@@ -7864,31 +8106,37 @@ x_connection_closed (dpy, error_message)
      M-x make-frame-on-display RET :1 RET
 
      will indefinitely wait in Xt for events for display `:1', opened
-     in the first class to make-frame-on-display.
+     in the first call to make-frame-on-display.
 
      Closing the display is reported to lead to a bus error on
      OpenWindows in certain situations.  I suspect that is a bug
-     in OpenWindows.  I don't know how to cicumvent it here.  */
+     in OpenWindows.  I don't know how to circumvent it here.  */
 
-#ifdef USE_X_TOOLKIT
-  /* If DPYINFO is null, this means we didn't open the display
-     in the first place, so don't try to close it.  */
   if (dpyinfo)
     {
-      extern void (*fatal_error_signal_hook) P_ ((void));
-      fatal_error_signal_hook = x_fatal_error_signal;
-      XtCloseDisplay (dpy);
-      fatal_error_signal_hook = NULL;
-    }
+#ifdef USE_X_TOOLKIT
+      /* If DPYINFO is null, this means we didn't open the display
+        in the first place, so don't try to close it.  */
+      {
+       extern void (*fatal_error_signal_hook) P_ ((void));
+       fatal_error_signal_hook = x_fatal_error_signal;
+       XtCloseDisplay (dpy);
+       fatal_error_signal_hook = NULL;
+      }
 #endif
 
 #ifdef USE_GTK
-  if (dpyinfo)
-    xg_display_close (dpyinfo->display);
+      /* Due to bugs in some Gtk+ versions, just exit here if this
+         is the last display/terminal. */
+      if (terminal_list->next_terminal == NULL)
+        {
+          fprintf (stderr, "%s\n", error_msg);
+          shut_down_emacs (0, 0, Qnil);
+          exit (70);
+        }
+      xg_display_close (dpyinfo->display);
 #endif
 
-  if (dpyinfo)
-    {
       /* Indicate that this display is dead.  */
       dpyinfo->display = 0;
 
@@ -7898,7 +8146,11 @@ x_connection_closed (dpy, error_message)
         /* We have just closed all frames on this display. */
         abort ();
 
-      x_delete_display (dpyinfo);
+      {
+       Lisp_Object tmp;
+       XSETTERMINAL (tmp, dpyinfo->terminal);
+       Fdelete_terminal (tmp, Qnoelisp);
+      }
     }
 
   x_uncatch_errors ();
@@ -7919,6 +8171,9 @@ x_connection_closed (dpy, error_message)
 
   unbind_to (index, Qnil);
   clear_waiting_for_input ();
+  /* Here, we absolutely have to use a non-local exit (e.g. signal, throw,
+     longjmp), because returning from this function would get us back into
+     Xlib's code which will directly call `exit'.  */
   error ("%s", error_msg);
 }
 
@@ -8013,11 +8268,16 @@ x_new_font (f, fontname)
      register char *fontname;
 {
   struct font_info *fontp
-    = FS_LOAD_FONT (f, 0, fontname, -1);
+    = FS_LOAD_FONT (f, fontname);
 
   if (!fontp)
     return Qnil;
 
+  if (FRAME_FONT (f) == (XFontStruct *) (fontp->font))
+    /* This font is already set in frame F.  There's nothing more to
+       do.  */
+    return build_string (fontp->full_name);
+
   FRAME_FONT (f) = (XFontStruct *) (fontp->font);
   FRAME_BASELINE_OFFSET (f) = fontp->baseline_offset;
   FRAME_FONTSET (f) = -1;
@@ -8044,12 +8304,17 @@ x_new_font (f, fontname)
   /* Now make the frame display the given font.  */
   if (FRAME_X_WINDOW (f) != 0)
     {
-      XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
-               FRAME_FONT (f)->fid);
-      XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
-               FRAME_FONT (f)->fid);
-      XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
-               FRAME_FONT (f)->fid);
+      Font fid;
+
+#ifdef USE_FONT_BACKEND
+      if (enable_font_backend)
+       fid = FRAME_X_DISPLAY_INFO (f)->font->fid;
+      else
+#endif
+      fid = FRAME_FONT (f)->fid;
+      XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc, fid);
+      XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc, fid);
+      XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc, fid);
 
       /* Don't change the size of a tip frame; there's no point in
         doing it because it's done in Fx_show_tip, and it leads to
@@ -8061,33 +8326,45 @@ x_new_font (f, fontname)
   return build_string (fontp->full_name);
 }
 
-/* Give frame F the fontset named FONTSETNAME as its default font, and
-   return the full name of that fontset.  FONTSETNAME may be a wildcard
-   pattern; in that case, we choose some fontset that fits the pattern.
-   The return value shows which fontset we chose.  */
+/* Give frame F the fontset named FONTSETNAME as its default fontset,
+   and return the full name of that fontset.  FONTSETNAME may be a
+   wildcard pattern; in that case, we choose some fontset that fits
+   the pattern.  FONTSETNAME may be a font name for ASCII characters;
+   in that case, we create a fontset from that font name.
+
+   The return value shows which fontset we chose.
+   If FONTSETNAME specifies the default fontset, return Qt.
+   If an ASCII font in the specified fontset can't be loaded, return
+   Qnil.  */
 
 Lisp_Object
 x_new_fontset (f, fontsetname)
      struct frame *f;
-     char *fontsetname;
+     Lisp_Object fontsetname;
 {
-  int fontset = fs_query_fontset (build_string (fontsetname), 0);
+  int fontset = fs_query_fontset (fontsetname, 0);
   Lisp_Object result;
 
-  if (fontset < 0)
-    return Qnil;
-
-  if (FRAME_FONTSET (f) == fontset)
+  if (fontset > 0 && f->output_data.x->fontset == fontset)
     /* This fontset is already set in frame F.  There's nothing more
        to do.  */
     return fontset_name (fontset);
+  else if (fontset == 0)
+    /* The default fontset can't be the default font.   */
+    return Qt;
 
-  result = x_new_font (f, (SDATA (fontset_ascii (fontset))));
+  if (fontset > 0)
+    result = x_new_font (f, (SDATA (fontset_ascii (fontset))));
+  else
+    result = x_new_font (f, SDATA (fontsetname));
 
   if (!STRINGP (result))
     /* Can't load ASCII font.  */
     return Qnil;
 
+  if (fontset < 0)
+    fontset = new_fontset_from_font_name (result);
+
   /* Since x_new_font doesn't update any fontset information, do it now.  */
   FRAME_FONTSET (f) = fontset;
 
@@ -8097,8 +8374,70 @@ x_new_fontset (f, fontsetname)
     xic_set_xfontset (f, SDATA (fontset_ascii (fontset)));
 #endif
 
-  return build_string (fontsetname);
+  return fontset_name (fontset);
+}
+
+#ifdef USE_FONT_BACKEND
+Lisp_Object
+x_new_fontset2 (f, fontset, font_object)
+     struct frame *f;
+     int fontset;
+     Lisp_Object font_object;
+{
+  struct font *font = XSAVE_VALUE (font_object)->pointer;
+
+  if (FRAME_FONT_OBJECT (f) == font)
+    /* This font is already set in frame F.  There's nothing more to
+       do.  */
+    return fontset_name (fontset);
+
+  BLOCK_INPUT;
+
+  FRAME_FONT_OBJECT (f) = font;
+  FRAME_FONT (f) = font->font.font;
+  FRAME_BASELINE_OFFSET (f) = font->font.baseline_offset;
+  FRAME_FONTSET (f) = fontset;
+
+  FRAME_COLUMN_WIDTH (f) = font->font.average_width;
+  FRAME_SPACE_WIDTH (f) = font->font.space_width;
+  FRAME_LINE_HEIGHT (f) = font->font.height;
+
+  compute_fringe_widths (f, 1);
+
+  /* Compute the scroll bar width in character columns.  */
+  if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
+    {
+      int wid = FRAME_COLUMN_WIDTH (f);
+      FRAME_CONFIG_SCROLL_BAR_COLS (f)
+       = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + wid - 1) / wid;
+    }
+  else
+    {
+      int wid = FRAME_COLUMN_WIDTH (f);
+      FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
+    }
+
+  /* Now make the frame display the given font.  */
+  if (FRAME_X_WINDOW (f) != 0)
+    {
+      /* Don't change the size of a tip frame; there's no point in
+        doing it because it's done in Fx_show_tip, and it leads to
+        problems because the tip frame has no widget.  */
+      if (NILP (tip_frame) || XFRAME (tip_frame) != f)
+       x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
+    }
+
+#ifdef HAVE_X_I18N
+  if (FRAME_XIC (f)
+      && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
+    xic_set_xfontset (f, SDATA (fontset_ascii (fontset)));
+#endif
+
+  UNBLOCK_INPUT;
+
+  return fontset_name (fontset);
 }
+#endif /* USE_FONT_BACKEND */
 
 \f
 /***********************************************************************
@@ -8381,7 +8720,7 @@ x_set_offset (f, xoff, yoff, change_gravity)
 {
   int modified_top, modified_left;
 
-  if (change_gravity != 0)
+  if (change_gravity > 0)
     {
       FRAME_X_OUTPUT (f)->left_before_move = f->left_pos;
       FRAME_X_OUTPUT (f)->top_before_move = f->top_pos;
@@ -8443,7 +8782,7 @@ x_set_offset (f, xoff, yoff, change_gravity)
    on the root window for frame F contains ATOMNAME.
    This is how a WM check shall be done according to the Window Manager
    Specification/Extended Window Manager Hints at
-   http://freedesktop.org/wiki/Standards_2fwm_2dspec.  */
+   http://freedesktop.org/wiki/Specifications/wm-spec.  */
 
 static int
 wm_supports (f, atomname)
@@ -8743,7 +9082,8 @@ x_sync_with_move (f, left, top, fuzzy)
           /* The left fuzz-factor is 10 pixels.  The top fuzz-factor is 40
              pixels.  */
 
-          if (abs (current_left - left) <= 10 && abs (current_top - top) <= 40)
+          if (eabs (current_left - left) <= 10
+             && eabs (current_top - top) <= 40)
             return;
   }
       else if (current_left == left && current_top == top)
@@ -8975,7 +9315,7 @@ x_ewmh_activate_frame (f)
      FRAME_PTR f;
 {
   /* See Window Manager Specification/Extended Window Manager Hints at
-     http://freedesktop.org/wiki/Standards_2fwm_2dspec  */
+     http://freedesktop.org/wiki/Specifications/wm-spec  */
 
   const char *atom = "_NET_ACTIVE_WINDOW";
   if (f->async_visible && wm_supports (f, atom))
@@ -9002,6 +9342,51 @@ XTframe_raise_lower (f, raise_flag)
     x_lower_frame (f);
 }
 \f
+/* XEmbed implementation.  */
+
+void
+xembed_set_info (f, flags)
+     struct frame *f;
+     enum xembed_info flags;
+{
+  Atom atom;
+  unsigned long data[2];
+
+  atom = XInternAtom (FRAME_X_DISPLAY (f), "_XEMBED_INFO", False);
+
+  data[0] = XEMBED_VERSION;
+  data[1] = flags;
+
+  XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), atom, atom,
+                  32, PropModeReplace, (unsigned char *) data, 2);
+}
+
+void
+xembed_send_message (f, time, message, detail, data1, data2)
+     struct frame *f;
+     Time time;
+     enum xembed_message message;
+     long detail;
+     long data1;
+     long data2;
+{
+  XEvent event;
+
+  event.xclient.type = ClientMessage;
+  event.xclient.window = FRAME_X_OUTPUT (f)->parent_desc;
+  event.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_XEMBED;
+  event.xclient.format = 32;
+  event.xclient.data.l[0] = time;
+  event.xclient.data.l[1] = message;
+  event.xclient.data.l[2] = detail;
+  event.xclient.data.l[3] = data1;
+  event.xclient.data.l[4] = data2;
+
+  XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_OUTPUT (f)->parent_desc,
+             False, NoEventMask, &event);
+  XSync (FRAME_X_DISPLAY (f), False);
+}
+\f
 /* Change of visibility.  */
 
 /* This tries to wait until the frame is really visible.
@@ -9034,6 +9419,7 @@ x_make_frame_visible (f)
         if we get to x_make_frame_visible a second time
         before the window gets really visible.  */
       if (! FRAME_ICONIFIED_P (f)
+         && ! FRAME_X_EMBEDDED_P (f)
          && ! f->output_data.x->asked_for_visible)
        x_set_offset (f, f->left_pos, f->top_pos, 0);
 
@@ -9042,14 +9428,22 @@ x_make_frame_visible (f)
       if (! EQ (Vx_no_window_manager, Qt))
        x_wm_set_window_state (f, NormalState);
 #ifdef USE_X_TOOLKIT
-      /* This was XtPopup, but that did nothing for an iconified frame.  */
-      XtMapWidget (f->output_data.x->widget);
+      if (FRAME_X_EMBEDDED_P (f))
+       xembed_set_info (f, XEMBED_MAPPED);
+      else
+       {
+         /* This was XtPopup, but that did nothing for an iconified frame.  */
+         XtMapWidget (f->output_data.x->widget);
+       }
 #else /* not USE_X_TOOLKIT */
 #ifdef USE_GTK
       gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
       gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
 #else
-      XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
+      if (FRAME_X_EMBEDDED_P (f))
+       xembed_set_info (f, XEMBED_MAPPED);
+      else
+       XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
 #endif /* not USE_GTK */
 #endif /* not USE_X_TOOLKIT */
 #if 0 /* This seems to bring back scroll bars in the wrong places
@@ -9090,7 +9484,9 @@ x_make_frame_visible (f)
        because the window manager may choose the position
        and we don't want to override it.  */
 
-    if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
+    if (! FRAME_VISIBLE_P (f)
+       && ! FRAME_ICONIFIED_P (f)
+       && ! FRAME_X_EMBEDDED_P (f)
        && f->win_gravity == NorthWestGravity
        && previously_visible)
       {
@@ -9201,9 +9597,12 @@ x_make_frame_invisible (f)
   if (FRAME_GTK_OUTER_WIDGET (f))
     gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
   else
+#else
+  if (FRAME_X_EMBEDDED_P (f))
+    xembed_set_info (f, 0);
+  else
 #endif
   {
-#ifdef HAVE_X11R4
 
   if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
                         DefaultScreen (FRAME_X_DISPLAY (f))))
@@ -9211,31 +9610,6 @@ x_make_frame_invisible (f)
       UNBLOCK_INPUT_RESIGNAL;
       error ("Can't notify window manager of window withdrawal");
     }
-#else /* ! defined (HAVE_X11R4) */
-
-  /*  Tell the window manager what we're going to do.  */
-  if (! EQ (Vx_no_window_manager, Qt))
-    {
-      XEvent unmap;
-
-      unmap.xunmap.type = UnmapNotify;
-      unmap.xunmap.window = window;
-      unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
-      unmap.xunmap.from_configure = False;
-      if (! XSendEvent (FRAME_X_DISPLAY (f),
-                       DefaultRootWindow (FRAME_X_DISPLAY (f)),
-                       False,
-                       SubstructureRedirectMaskSubstructureNotifyMask,
-                       &unmap))
-       {
-         UNBLOCK_INPUT_RESIGNAL;
-         error ("Can't notify window manager of withdrawal");
-       }
-    }
-
-  /* Unmap the window ourselves.  Cheeky!  */
-  XUnmapWindow (FRAME_X_DISPLAY (f), window);
-#endif /* ! defined (HAVE_X11R4) */
   }
 
   /* We can't distinguish this from iconification
@@ -9331,7 +9705,9 @@ x_iconify_frame (f)
 
   /* Make sure the X server knows where the window should be positioned,
      in case the user deiconifies with the window manager.  */
-  if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
+  if (! FRAME_VISIBLE_P (f)
+      && ! FRAME_ICONIFIED_P (f)
+      && ! FRAME_X_EMBEDDED_P (f))
     x_set_offset (f, f->left_pos, f->top_pos, 0);
 
   /* Since we don't know which revision of X we're running, we'll use both
@@ -9394,6 +9770,15 @@ x_free_frame_resources (f)
      commands to the X server.  */
   if (dpyinfo->display)
     {
+#ifdef USE_FONT_BACKEND
+      /* We must free faces before destroying windows because some
+        font-driver (e.g. xft) access a window while finishing a
+        face.  */
+      if (enable_font_backend
+         && FRAME_FACE_CACHE (f))
+       free_frame_faces (f);
+#endif /* USE_FONT_BACKEND */
+
       if (f->output_data.x->icon_desc)
        XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
 
@@ -9592,16 +9977,11 @@ x_wm_set_size_hint (f, flags, user_position)
        them; otherwise, we set the min_width and min_height members
        to the size for a zero x zero frame.  */
 
-#ifdef HAVE_X11R4
     size_hints.flags |= PBaseSize;
     size_hints.base_width = base_width;
     size_hints.base_height = base_height;
     size_hints.min_width  = base_width + min_cols * size_hints.width_inc;
     size_hints.min_height = base_height + min_rows * size_hints.height_inc;
-#else
-    size_hints.min_width = base_width;
-    size_hints.min_height = base_height;
-#endif
   }
 
   /* If we don't need the old flags, we don't need the old hint at all.  */
@@ -9617,12 +9997,8 @@ x_wm_set_size_hint (f, flags, user_position)
     long supplied_return;
     int value;
 
-#ifdef HAVE_X11R4
     value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
                               &supplied_return);
-#else
-    value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
-#endif
 
 #ifdef USE_X_TOOLKIT
     size_hints.base_height = hints.base_height;
@@ -9663,11 +10039,7 @@ x_wm_set_size_hint (f, flags, user_position)
     }
 #endif /* PWinGravity */
 
-#ifdef HAVE_X11R4
   XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
-#else
-  XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
-#endif
 }
 #endif /* not USE_GTK */
 
@@ -9788,7 +10160,7 @@ x_get_font_info (f, font_idx)
    If SIZE is > 0, it is the size (maximum bounds width) of fonts
    to be listed.
 
-   SIZE < 0 means include scalable fonts.
+   SIZE < 0 means include auto scaled fonts.
 
    Frame F null means we have not yet created any frame on X, and
    consult the first display in x_display_list.  MAXNAMES sets a limit
@@ -10097,6 +10469,11 @@ x_check_font (f, font)
 
   xassert (font != NULL);
 
+#ifdef USE_FONT_BACKEND
+  if (enable_font_backend)
+    /* Fixme: Perhaps we should check all cached fonts.  */
+    return;
+#endif
   for (i = 0; i < dpyinfo->n_fonts; i++)
     if (dpyinfo->font_table[i].name
        && font == dpyinfo->font_table[i].font)
@@ -10261,6 +10638,7 @@ x_load_font (f, fontname, size)
     bzero (fontp, sizeof (*fontp));
     fontp->font = font;
     fontp->font_idx = i;
+    fontp->charset = -1;       /* fs_load_font sets it.  */
     fontp->name = (char *) xmalloc (strlen (fontname) + 1);
     bcopy (fontname, fontp->name, strlen (fontname) + 1);
 
@@ -10372,10 +10750,10 @@ x_load_font (f, fontname, size)
        the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
        (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
        2:0xA020..0xFF7F).  For the moment, we don't know which charset
-       uses this font.  So, we set information in fontp->encoding[1]
+       uses this font.  So, we set information in fontp->encoding_type
        which is never used by any charset.  If mapping can't be
        decided, set FONT_ENCODING_NOT_DECIDED.  */
-    fontp->encoding[1]
+    fontp->encoding_type
       = (font->max_byte1 == 0
         /* 1-byte font */
         ? (font->min_char_or_byte2 < 0x80
@@ -10475,6 +10853,160 @@ x_find_ccl_program (fontp)
 }
 
 
+/* Return a char-table whose elements are t if the font FONT_INFO
+   contains a glyph for the corresponding character, and nil if
+   not.  */
+
+Lisp_Object
+x_get_font_repertory (f, font_info)
+     FRAME_PTR f;
+     struct font_info *font_info;
+{
+  XFontStruct *font = (XFontStruct *) font_info->font;
+  Lisp_Object table;
+  int min_byte1, max_byte1, min_byte2, max_byte2;
+  int c;
+  struct charset *charset = CHARSET_FROM_ID (font_info->charset);
+  int offset = CHARSET_OFFSET (charset);
+
+  table = Fmake_char_table (Qnil, Qnil);
+
+  min_byte1 = font->min_byte1;
+  max_byte1 = font->max_byte1;
+  min_byte2 = font->min_char_or_byte2;
+  max_byte2 = font->max_char_or_byte2;
+  if (min_byte1 == 0 && max_byte1 == 0)
+    {
+      if (! font->per_char || font->all_chars_exist == True)
+       {
+         if (offset >= 0)
+           char_table_set_range (table, offset + min_byte2,
+                                 offset + max_byte2, Qt);
+         else
+           for (; min_byte2 <= max_byte2; min_byte2++)
+             {
+               c = DECODE_CHAR (charset, min_byte2);
+               CHAR_TABLE_SET (table, c, Qt);
+             }
+       }
+      else
+       {
+         XCharStruct *pcm = font->per_char;
+         int from = -1;
+         int i;
+
+         for (i = min_byte2; i <= max_byte2; i++, pcm++)
+           {
+             if (pcm->width == 0 && pcm->rbearing == pcm->lbearing)
+               {
+                 if (from >= 0)
+                   {
+                     if (offset >= 0)
+                       char_table_set_range (table, offset + from,
+                                             offset + i - 1, Qt);
+                     else
+                       for (; from < i; from++)
+                         {
+                           c = DECODE_CHAR (charset, from);
+                           CHAR_TABLE_SET (table, c, Qt);
+                         }
+                     from = -1;
+                   }
+               }
+             else if (from < 0)
+               from = i;
+           }
+         if (from >= 0)
+           {
+             if (offset >= 0)
+               char_table_set_range (table, offset + from, offset + i - 1,
+                                     Qt);
+             else
+               for (; from < i; from++)
+                 {
+                   c = DECODE_CHAR (charset, from);
+                   CHAR_TABLE_SET (table, c, Qt);
+                 }
+           }
+       }
+    }
+  else
+    {
+      if (! font->per_char || font->all_chars_exist == True)
+       {
+         int i, j;
+
+         if (offset >= 0)
+           for (i = min_byte1; i <= max_byte1; i++)
+             char_table_set_range
+               (table, offset + ((i << 8) | min_byte2),
+                offset + ((i << 8) | max_byte2), Qt);
+         else
+           for (i = min_byte1; i <= max_byte1; i++)        
+             for (j = min_byte2; j <= max_byte2; j++)
+               {
+                 unsigned code = (i << 8) | j;
+                 c = DECODE_CHAR (charset, code);
+                 CHAR_TABLE_SET (table, c, Qt);
+               }
+       }
+      else
+       {
+         XCharStruct *pcm = font->per_char;
+         int i;
+
+         for (i = min_byte1; i <= max_byte1; i++)
+           {
+             int from = -1;
+             int j;
+
+             for (j = min_byte2; j <= max_byte2; j++, pcm++)
+               {
+                 if (pcm->width == 0 && pcm->rbearing == pcm->lbearing)
+                   {
+                     if (from >= 0)
+                       {
+                         if (offset >= 0)
+                           char_table_set_range
+                             (table, offset + ((i << 8) | from),
+                              offset + ((i << 8) | (j - 1)), Qt);
+                         else
+                           {
+                             for (; from < j; from++)
+                               {
+                                 unsigned code = (i << 8) | from;
+                                 c = ENCODE_CHAR (charset, code);
+                                 CHAR_TABLE_SET (table, c, Qt);
+                               }
+                           }
+                         from = -1;
+                       }
+                   }
+                 else if (from < 0)
+                   from = j;
+               }
+             if (from >= 0)
+               {
+                 if (offset >= 0)
+                   char_table_set_range
+                     (table, offset + ((i << 8) | from),
+                      offset + ((i << 8) | (j - 1)), Qt);
+                 else
+                   {
+                     for (; from < j; from++)
+                       {
+                         unsigned code = (i << 8) | from;
+                         c = DECODE_CHAR (charset, code);
+                         CHAR_TABLE_SET (table, c, Qt);
+                       }
+                   }
+               }
+           }
+       }
+    }
+
+  return table;
+}
 \f
 /***********************************************************************
                            Initialization
@@ -10672,9 +11204,7 @@ x_term_init (display_name, xrm_option, resource_name)
         argv[argc++] = "--name";
         argv[argc++] = resource_name;
 
-#ifdef HAVE_X11R5
         XSetLocaleModifiers ("");
-#endif
 
         gtk_init (&argc, &argv2);
 
@@ -10739,9 +11269,7 @@ x_term_init (display_name, xrm_option, resource_name)
   }
 
 #else /* not USE_X_TOOLKIT */
-#ifdef HAVE_X11R5
   XSetLocaleModifiers ("");
-#endif
   dpy = XOpenDisplay (SDATA (display_name));
 #endif /* not USE_X_TOOLKIT */
 #endif /* not USE_GTK*/
@@ -10776,6 +11304,7 @@ x_term_init (display_name, xrm_option, resource_name)
       {
        terminal->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
        init_kboard (terminal->kboard);
+       terminal->kboard->Vwindow_system = intern ("x");
        if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
          {
            char *vendor = ServerVendor (dpy);
@@ -10862,6 +11391,13 @@ x_term_init (display_name, xrm_option, resource_name)
   dpyinfo->font_table = NULL;
   dpyinfo->n_fonts = 0;
   dpyinfo->font_table_size = 0;
+#ifdef USE_FONT_BACKEND
+  dpyinfo->font = XLoadQueryFont (dpyinfo->display, "fixed");
+  if (! dpyinfo->font)
+    dpyinfo->font = XLoadQueryFont (dpyinfo->display, "*");
+  if (! dpyinfo->font)
+    abort ();
+#endif /* USE_FONT_BACKEND */
   dpyinfo->bitmaps = 0;
   dpyinfo->bitmaps_size = 0;
   dpyinfo->bitmaps_last = 0;
@@ -10879,7 +11415,7 @@ x_term_init (display_name, xrm_option, resource_name)
   dpyinfo->x_focus_frame = 0;
   dpyinfo->x_focus_event_frame = 0;
   dpyinfo->x_highlight_frame = 0;
-  dpyinfo->image_cache = make_image_cache ();
+  dpyinfo->terminal->image_cache = make_image_cache ();
   dpyinfo->wm_type = X_WMTYPE_UNKNOWN;
 
   /* See if we can construct pixel values from RGB values.  */
@@ -10923,8 +11459,8 @@ x_term_init (display_name, xrm_option, resource_name)
     /* Mac OS X 10.3's Xserver sometimes reports 0.0mm.  */
     dpyinfo->resy = (mm < 1) ? 100 : pixels * 25.4 / mm;
     pixels = DisplayWidth (dpyinfo->display, screen_number);
-    /* Mac OS X 10.3's Xserver sometimes reports 0.0mm.  */
     mm = DisplayWidthMM (dpyinfo->display, screen_number);
+    /* Mac OS X 10.3's Xserver sometimes reports 0.0mm.  */
     dpyinfo->resx = (mm < 1) ? 100 : pixels * 25.4 / mm;
   }
 
@@ -10989,6 +11525,9 @@ x_term_init (display_name, xrm_option, resource_name)
   dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
                                          False);
 
+  dpyinfo->Xatom_XEMBED = XInternAtom (dpyinfo->display, "_XEMBED",
+                                      False);
+
   dpyinfo->cut_buffers_initialized = 0;
 
   dpyinfo->x_dnd_atoms_size = 8;
@@ -11034,16 +11573,9 @@ x_term_init (display_name, xrm_option, resource_name)
     add_keyboard_wait_descriptor (connection);
 #endif
 
-#ifndef F_SETOWN_BUG
 #ifdef F_SETOWN
-#ifdef F_SETOWN_SOCK_NEG
-  /* stdin is a socket here */
-  fcntl (connection, F_SETOWN, -getpid ());
-#else /* ! defined (F_SETOWN_SOCK_NEG) */
   fcntl (connection, F_SETOWN, getpid ());
-#endif /* ! defined (F_SETOWN_SOCK_NEG) */
 #endif /* ! defined (F_SETOWN) */
-#endif /* F_SETOWN_BUG */
 
 #ifdef SIGIO
   if (interrupt_input)
@@ -11051,9 +11583,6 @@ x_term_init (display_name, xrm_option, resource_name)
 #endif /* ! defined (SIGIO) */
 
 #ifdef USE_LUCID
-#ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer.  */
-  /* Make sure that we have a valid font for dialog boxes
-     so that Xt does not crash.  */
   {
     Display *dpy = dpyinfo->display;
     XrmValue d, fr, to;
@@ -11072,7 +11601,6 @@ x_term_init (display_name, xrm_option, resource_name)
       XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
     x_uncatch_errors ();
   }
-#endif
 #endif
 
   /* See if we should run in synchronous mode.  This is useful
@@ -11097,13 +11625,13 @@ x_term_init (display_name, xrm_option, resource_name)
                                    Qnil, Qnil);
 #ifdef USE_XIM
     if (STRINGP (value)
-       && (!strcmp (XSTRING (value)->data, "false")
-           || !strcmp (XSTRING (value)->data, "off")))
+       && (!strcmp (SDATA (value), "false")
+           || !strcmp (SDATA (value), "off")))
       use_xim = 0;
 #else
     if (STRINGP (value)
-       && (!strcmp (XSTRING (value)->data, "true")
-           || !strcmp (XSTRING (value)->data, "on")))
+       && (!strcmp (SDATA (value), "true")
+           || !strcmp (SDATA (value), "on")))
       use_xim = 1;
 #endif
   }
@@ -11136,10 +11664,11 @@ x_delete_display (dpyinfo)
   for (t = terminal_list; t; t = t->next_terminal)
     if (t->type == output_x_window && t->display_info.x == dpyinfo)
       {
+#ifdef HAVE_X_SM
         /* Close X session management when we close its display.  */
         if (t->id == 1 && x_session_have_connection ())
           x_session_close();
-
+#endif
         delete_terminal (t);
         break;
       }
@@ -11191,6 +11720,10 @@ x_delete_display (dpyinfo)
     xim_close_dpy (dpyinfo);
 #endif
 
+#ifdef USE_FONT_BACKEND
+  if (! enable_font_backend)
+    {
+#endif
   /* Free the font names in the font table.  */
   for (i = 0; i < dpyinfo->n_fonts; i++)
     if (dpyinfo->font_table[i].name)
@@ -11206,6 +11739,10 @@ x_delete_display (dpyinfo)
        xfree (dpyinfo->font_table->font_encoder);
       xfree (dpyinfo->font_table);
     }
+#ifdef USE_FONT_BACKEND
+    }
+#endif
+
   if (dpyinfo->x_id_name)
     xfree (dpyinfo->x_id_name);
   if (dpyinfo->color_cells)
@@ -11311,29 +11848,39 @@ x_delete_terminal (struct terminal *terminal)
 
   /* Protect against recursive calls.  Fdelete_frame in
      delete_terminal calls us back when it deletes our last frame.  */
-  if (terminal->deleted)
+  if (!terminal->name)
     return;
 
   BLOCK_INPUT;
-  /* Free the fonts in the font table.  */
-  for (i = 0; i < dpyinfo->n_fonts; i++)
-    if (dpyinfo->font_table[i].name)
-      {
-       XFreeFont (dpyinfo->display, dpyinfo->font_table[i].font);
-      }
+  /* If called from x_connection_closed, the display may already be closed
+     and dpyinfo->display was set to 0 to indicate that.  */
+  if (dpyinfo->display)
+    {
+#ifdef USE_FONT_BACKEND
+      if (enable_font_backend)
+       XFreeFont (dpyinfo->display, dpyinfo->font);
+      else
+#endif
+       /* Free the fonts in the font table.  */
+       for (i = 0; i < dpyinfo->n_fonts; i++)
+         if (dpyinfo->font_table[i].name)
+           {
+             XFreeFont (dpyinfo->display, dpyinfo->font_table[i].font);
+           }
 
-  x_destroy_all_bitmaps (dpyinfo);
-  XSetCloseDownMode (dpyinfo->display, DestroyAll);
+      x_destroy_all_bitmaps (dpyinfo);
+      XSetCloseDownMode (dpyinfo->display, DestroyAll);
 
 #ifdef USE_GTK
-  xg_display_close (dpyinfo->display);
+      xg_display_close (dpyinfo->display);
 #else
 #ifdef USE_X_TOOLKIT
-  XtCloseDisplay (dpyinfo->display);
+      XtCloseDisplay (dpyinfo->display);
 #else
-  XCloseDisplay (dpyinfo->display);
+      XCloseDisplay (dpyinfo->display);
 #endif
 #endif /* ! USE_GTK */
+    }
 
   x_delete_display (dpyinfo);
   UNBLOCK_INPUT;
@@ -11462,8 +12009,6 @@ syms_of_xterm ()
   staticpro (&Qvendor_specific_keysyms);
   Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
 
-  staticpro (&Qutf_8);
-  Qutf_8 = intern ("utf-8");
   staticpro (&Qlatin_1);
   Qlatin_1 = intern ("latin-1");