(input_signal_count): New variable.
[bpt/emacs.git] / src / xterm.c
index 9ab6c0b..bb29073 100644 (file)
@@ -52,11 +52,6 @@ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
 
 #ifdef BSD
 #include <sys/ioctl.h>
-#include <strings.h>
-#else /* ! defined (BSD) */
-#ifndef VMS
-#include <string.h>
-#endif
 #endif /* ! defined (BSD) */
 
 #include "systty.h"
@@ -84,6 +79,8 @@ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
 #include "disptab.h"
 #include "buffer.h"
 #include "window.h"
+#include "keyboard.h"
+#include "intervals.h"
 
 #ifdef USE_X_TOOLKIT
 extern XtAppContext Xt_app_con;
@@ -103,12 +100,7 @@ extern void _XEditResCheckMessages ();
 #endif
 #endif
 
-#ifdef HAVE_X11
 #define XMapWindow XMapRaised          /* Raise them when mapping. */
-#else /* ! defined (HAVE_X11) */
-#include <X/Xkeyboard.h>
-/*#include <X/Xproto.h>        */
-#endif /* ! defined (HAVE_X11) */
 
 #ifdef FD_SET
 /* We could get this from param.h, but better not to depend on finding that.
@@ -148,18 +140,6 @@ static int expose_all_windows;
 
 static int expose_all_icons;
 
-#ifndef HAVE_X11
-/* ExposeRegion events, when received, are copied into this queue
-   for later processing.  */
-
-static struct event_queue x_expose_queue;
-
-/* ButtonPress and ButtonReleased events, when received,
-   are copied into this queue for later processing.  */
-
-struct event_queue x_mouse_queue;
-#endif /* HAVE_X11 */
-
 #if defined (SIGIO) && defined (FIONREAD)
 int BLOCK_INPUT_mask;
 #endif /* ! defined (SIGIO) && defined (FIONREAD) */
@@ -308,7 +288,13 @@ static int mouse_face_mouse_x, mouse_face_mouse_y;
 /* Nonzero means defer mouse-motion highlighting.  */
 static int mouse_face_defer;
 
-#ifdef HAVE_X11
+/* Incremented by XTread_socket whenever it really tries to read events.  */
+#ifdef __STDC__
+static int volatile input_signal_count;
+#else
+static int input_signal_count;
+#endif
+
 /* `t' if a mouse button is depressed. */
 
 extern Lisp_Object Vmouse_depressed;
@@ -328,31 +314,7 @@ extern int _Xdebug;
 
 extern Lisp_Object Qface, Qmouse_face;
 
-#else /* ! defined (HAVE_X11) */
-
-/* Bit patterns for the mouse cursor.  */
-
-short MouseCursor[] = {
-  0x0000, 0x0008, 0x0018, 0x0038,
-  0x0078, 0x00f8, 0x01f8, 0x03f8,
-  0x07f8, 0x00f8, 0x00d8, 0x0188,
-  0x0180, 0x0300, 0x0300, 0x0000};
-
-short MouseMask[] = {
-  0x000c, 0x001c, 0x003c, 0x007c,
-  0x00fc, 0x01fc, 0x03fc, 0x07fc,
-  0x0ffc, 0x0ffc, 0x01fc, 0x03dc,
-  0x03cc, 0x0780, 0x0780, 0x0300};
-
-static short grey_bits[] = {
-  0x0005, 0x000a, 0x0005, 0x000a};
-
-static Pixmap GreyPixmap = 0;
-#endif /* ! defined (HAVE_X11) */
-
-#ifdef X_IO_BUG
 static int x_noop_count;
-#endif
 
 
 /* From time to time we get info on an Emacs window, here.  */
@@ -380,10 +342,6 @@ static void note_mouse_highlight ();
 static void clear_mouse_face ();
 static void show_mouse_face ();
 
-#ifndef HAVE_X11
-static void dumpqueue ();
-#endif /* HAVE_X11 */
-
 void dumpborder ();
 static int XTcursor_to ();
 static int XTclear_end_of_line ();
@@ -445,16 +403,10 @@ XTupdate_begin (f)
            clear_mouse_face ();
        }
     }
-#ifndef HAVE_X11
-  dumpqueue ();
-#endif /* HAVE_X11 */
+
   UNBLOCK_INPUT;
 }
 
-#ifndef HAVE_X11
-static void x_do_pending_expose ();
-#endif
-
 static
 XTupdate_end (f)
      struct frame *f;
@@ -462,10 +414,6 @@ XTupdate_end (f)
   int mask;
 
   BLOCK_INPUT;
-#ifndef HAVE_X11
-  dumpqueue ();
-  x_do_pending_expose ();
-#endif /* HAVE_X11 */
 
   x_display_cursor (f, 1);
 
@@ -890,7 +838,6 @@ XTclear_end_of_line (first_unused)
       && f->phys_cursor_x < first_unused)
     f->phys_cursor_x = -1;
 
-#ifdef HAVE_X11
   XClearArea (x_current_display, FRAME_X_WINDOW (f),
              CHAR_TO_PIXEL_COL (f, curs_x),
              CHAR_TO_PIXEL_ROW (f, curs_y),
@@ -899,14 +846,6 @@ XTclear_end_of_line (first_unused)
 #if 0
   redraw_previous_char (f, curs_x, curs_y, highlight);
 #endif
-#else /* ! defined (HAVE_X11) */
-  XPixSet (FRAME_X_WINDOW (f),
-          CHAR_TO_PIXEL_COL (f, curs_x),
-          CHAR_TO_PIXEL_ROW (f, curs_y),
-          FONT_WIDTH (f->display.x->font) * (first_unused - curs_x),
-          f->display.x->line_height,
-          f->display.x->background_pixel);     
-#endif /* ! defined (HAVE_X11) */
 
   UNBLOCK_INPUT;
 }
@@ -932,10 +871,6 @@ XTclear_frame ()
      colors or something like that, then they should be notified.  */
   x_scroll_bar_clear (f);
 
-#ifndef HAVE_X11
-  dumpborder (f, 0);
-#endif /* HAVE_X11 */
-
   XFlushQueue ();
   UNBLOCK_INPUT;
 }
@@ -1222,11 +1157,7 @@ XTflash (f)
 
 /* Make audible bell.  */
 
-#ifdef HAVE_X11
 #define XRINGBELL XBell (x_current_display, 0)
-#else /* ! defined (HAVE_X11) */
-#define XRINGBELL XFeep (0);
-#endif /* ! defined (HAVE_X11) */
 
 XTring_bell ()
 {
@@ -1303,51 +1234,21 @@ stufflines (n)
   newtop = topregion + n;
   length = (bottomregion - topregion) + 1;
 
-#ifndef HAVE_X11
-  dumpqueue ();
-#endif /* HAVE_X11 */
-
   if ((length > 0) && (newtop <= flexlines))
-    {
-#ifdef HAVE_X11
-      XCopyArea (x_current_display, FRAME_X_WINDOW (f),
-                FRAME_X_WINDOW (f), f->display.x->normal_gc,
-                intborder, CHAR_TO_PIXEL_ROW (f, topregion),
-                f->width * FONT_WIDTH (f->display.x->font),
-                length * f->display.x->line_height, intborder,
-                CHAR_TO_PIXEL_ROW (f, newtop));
-#else /* ! defined (HAVE_X11) */
-      XMoveArea (FRAME_X_WINDOW (f),
-                intborder, CHAR_TO_PIXEL_ROW (f, topregion),
-                intborder, CHAR_TO_PIXEL_ROW (f, newtop),
-                f->width * FONT_WIDTH (f->display.x->font),
-                length * f->display.x->line_height);
-      /* Now we must process any ExposeRegion events that occur
-        if the area being copied from is obscured.
-        We can't let it wait because further i/d operations
-        may want to copy this area to another area.  */
-      x_read_exposes ();
-#endif /* ! defined (HAVE_X11) */
-    }
+    XCopyArea (x_current_display, FRAME_X_WINDOW (f),
+              FRAME_X_WINDOW (f), f->display.x->normal_gc,
+              intborder, CHAR_TO_PIXEL_ROW (f, topregion),
+              f->width * FONT_WIDTH (f->display.x->font),
+              length * f->display.x->line_height, intborder,
+              CHAR_TO_PIXEL_ROW (f, newtop));
 
   newtop = min (newtop, (flexlines - 1));
   length = newtop - topregion;
   if (length > 0)
-    {
-#ifdef HAVE_X11
-      XClearArea (x_current_display, FRAME_X_WINDOW (f), intborder, 
-                 CHAR_TO_PIXEL_ROW (f, topregion),
-                 f->width * FONT_WIDTH (f->display.x->font),
-                 n * f->display.x->line_height, False);
-#else /* ! defined (HAVE_X11) */
-      XPixSet (FRAME_X_WINDOW (f),
-              intborder,
-              CHAR_TO_PIXEL_ROW (f, topregion),
-              f->width * FONT_WIDTH (f->display.x->font),
-              n * f->display.x->line_height,
-              f->display.x->background_pixel);
-#endif /* ! defined (HAVE_X11) */
-    }
+    XClearArea (x_current_display, FRAME_X_WINDOW (f), intborder, 
+               CHAR_TO_PIXEL_ROW (f, topregion),
+               f->width * FONT_WIDTH (f->display.x->font),
+               n * f->display.x->line_height, False);
 }
 
 /* Perform a delete-lines operation, deleting N lines
@@ -1364,31 +1265,16 @@ scraplines (n)
   if (curs_y >= flexlines)
     return;
 
-#ifndef HAVE_X11
-  dumpqueue ();
-#endif /* HAVE_X11 */
-
   if ((curs_y + n) >= flexlines)
     {
       if (flexlines >= (curs_y + 1))
-       {
-#ifdef HAVE_X11
-         XClearArea (x_current_display, FRAME_X_WINDOW (f), intborder,
-                     CHAR_TO_PIXEL_ROW (f, curs_y),
-                     f->width * FONT_WIDTH (f->display.x->font),
-                     (flexlines - curs_y) * f->display.x->line_height, False);
-#else /* ! defined (HAVE_X11) */
-         XPixSet (FRAME_X_WINDOW (f),
-                  intborder, CHAR_TO_PIXEL_ROW (f, curs_y),
-                  f->width * FONT_WIDTH (f->display.x->font),
-                  (flexlines - curs_y) * f->display.x->line_height,
-                  f->display.x->background_pixel);
-#endif /* ! defined (HAVE_X11) */
-       }
+       XClearArea (x_current_display, FRAME_X_WINDOW (f), intborder,
+                   CHAR_TO_PIXEL_ROW (f, curs_y),
+                   f->width * FONT_WIDTH (f->display.x->font),
+                   (flexlines - curs_y) * f->display.x->line_height, False);
     }
   else
     {
-#ifdef HAVE_X11
       XCopyArea (x_current_display, FRAME_X_WINDOW (f),
                 FRAME_X_WINDOW (f), f->display.x->normal_gc,
                 intborder,
@@ -1401,23 +1287,6 @@ scraplines (n)
                  CHAR_TO_PIXEL_ROW (f, flexlines - n),
                  f->width * FONT_WIDTH (f->display.x->font),
                  n * f->display.x->line_height, False);
-#else /* ! defined (HAVE_X11) */
-      XMoveArea (FRAME_X_WINDOW (f),
-                intborder,
-                CHAR_TO_PIXEL_ROW (f, curs_y + n),
-                intborder, CHAR_TO_PIXEL_ROW (f, curs_y),
-                f->width * FONT_WIDTH (f->display.x->font),
-                (flexlines - (curs_y + n)) * f->display.x->line_height);
-      /* Now we must process any ExposeRegion events that occur
-        if the area being copied from is obscured.
-        We can't let it wait because further i/d operations
-        may want to copy this area to another area.  */
-      x_read_exposes ();
-      XPixSet (FRAME_X_WINDOW (f), intborder,
-              CHAR_TO_PIXEL_ROW (f, flexlines - n),
-              f->width * FONT_WIDTH (f->display.x->font),
-              n * f->display.x->line_height, f->display.x->background_pixel);
-#endif /* ! defined (HAVE_X11) */
     }
 }
 
@@ -1469,19 +1338,6 @@ dumprectangle (f, left, top, cols, rows)
   /* Express rectangle as four edges, instead of position-and-size.  */
   bottom = top + rows;
   right = left + cols;
-
-#ifndef HAVE_X11               /* Window manger does this for X11. */
-  {
-    int intborder = f->display.x->internal_border_width;
-
-    /* If the rectangle includes any of the internal border area,
-       redisplay the border emphasis.  */
-    if (top < intborder || left < intborder
-       || bottom > intborder + f->height * f->display.x->line_height
-       || right > intborder + f->width * f->display.x->line_height)
-      dumpborder (f, 0);
-  }
-#endif /* not HAVE_X11         Window manger does this for X11. */
   
   /* Convert rectangle edges in pixels to edges in chars.
      Round down for left and top, up for right and bottom.  */
@@ -1540,99 +1396,7 @@ dumprectangle (f, left, top, cols, rows)
   if (cursor_cleared)
     x_display_cursor (f, 1);
 }
-
-#ifndef HAVE_X11
-/* Process all queued ExposeRegion events. */
-
-static void
-dumpqueue ()
-{
-  register int i;
-  XExposeRegionEvent r;
-
-  while (dequeue_event (&r, &x_expose_queue))
-    {
-      struct frame *f = x_window_to_frame (r.window);
-      if (f->display.x->icon_desc == r.window)
-       refreshicon (f);
-      else
-       dumprectangle (f, r.x, r.y, r.width, r.height);
-    }
-  XFlushQueue ();
-}
-#endif /* HAVE_X11 */
 \f
-/* Process all expose events that are pending, for X10.
-   Redraws the cursor if necessary on any frame that
-   is not in the process of being updated with update_frame.  */
-
-#ifndef HAVE_X11
-static void
-x_do_pending_expose ()
-{
-  int mask;
-  struct frame *f;
-  Lisp_Object tail, frame;
-
-  if (expose_all_windows)
-    {
-      expose_all_windows = 0;
-      for (tail = Vframe_list; CONSP (tail); tail = XCONS (tail)->cdr)
-       {
-         register int temp_width, temp_height;
-         int intborder;
-
-         frame = XCONS (tail)->car;
-         if (XGCTYPE (frame) != Lisp_Frame)
-           continue;
-         f = XFRAME (frame);
-         if (! FRAME_X_P (f))
-           continue;
-         if (!f->async_visible)
-           continue;
-         if (!f->display.x->needs_exposure)
-           continue;
-
-         intborder = f->display.x->internal_border_width;
-
-         clear_cursor (f);
-         XGetWindowInfo (FRAME_X_WINDOW (f), &windowinfo);
-         temp_width = ((windowinfo.width - 2 * intborder
-                        - f->display.x->v_scroll_bar_width)
-                       / FONT_WIDTH (f->display.x->font));
-         temp_height = ((windowinfo.height- 2 * intborder
-                         - f->display.x->h_scroll_bar_height)
-                        / f->display.x->line_height);
-         if (temp_width != f->width || temp_height != f->height)
-           {
-             change_frame_size (f, max (1, temp_height),
-                                 max (1, temp_width), 0, 1);
-             x_resize_scroll_bars (f);
-           }
-         f->display.x->left_pos = windowinfo.x;
-         f->display.x->top_pos = windowinfo.y;
-         dumprectangle (f, 0, 0, PIXEL_WIDTH (f), PIXEL_HEIGHT (f));
-#if 0
-         dumpborder (f, 0);
-#endif /* ! 0 */
-         f->display.x->needs_exposure = 0;
-         if (updating_frame != f)
-           x_display_cursor (f, 1);
-         XFlushQueue ();
-       }
-    }
-  else
-    /* Handle any individual-rectangle expose events queued
-       for various windows.  */
-#ifdef HAVE_X11
-    ;
-#else /* ! defined (HAVE_X11) */
-    dumpqueue ();
-#endif /* ! defined (HAVE_X11) */
-}
-#endif
-
-#ifdef HAVE_X11
 static void
 frame_highlight (frame)
      struct frame *frame;
@@ -1662,50 +1426,6 @@ frame_unhighlight (frame)
   UNBLOCK_INPUT;
   x_display_cursor (frame, 1);
 }
-#else /* ! defined (HAVE_X11) */
-/* Dump the border-emphasis of frame F.
-   If F is selected, this is a lining of the same color as the border,
-   just within the border, occupying a portion of the internal border.
-   If F is not selected, it is background in the same place.
-   If ALWAYS is 0, don't bother explicitly drawing if it's background.
-
-   ALWAYS = 1 is used when a frame becomes selected or deselected.
-   In that case, we also turn the cursor off and on again
-   so it will appear in the proper shape (solid if selected; else hollow.)  */
-
-static void
-dumpborder (f, always)
-     struct frame *f;
-     int always;
-{
-  int thickness = f->display.x->internal_border_width / 2;
-  int width = PIXEL_WIDTH (f);
-  int height = PIXEL_HEIGHT (f);
-  int pixel;
-
-  if (f != selected_frame)
-    {
-      if (!always)
-       return;
-
-      pixel = f->display.x->background_pixel;
-    }
-  else
-    {
-      pixel = f->display.x->border_pixel;
-    }
-
-  XPixSet (FRAME_X_WINDOW (f), 0, 0, width, thickness, pixel);
-  XPixSet (FRAME_X_WINDOW (f), 0, 0, thickness, height, pixel);
-  XPixSet (FRAME_X_WINDOW (f), 0, height - thickness, width,
-          thickness, pixel);
-  XPixSet (FRAME_X_WINDOW (f), width - thickness, 0, thickness,
-          height, pixel);
-
-  if (always)
-    x_display_cursor (f, 1);
-}
-#endif /* ! defined (HAVE_X11) */
 
 static void XTframe_rehighlight ();
 
@@ -1935,7 +1655,6 @@ x_emacs_to_x_modifiers (state)
 }
 \f
 /* Mouse clicks and mouse movement.  Rah.  */
-#ifdef HAVE_X11
 
 /* Given a pixel position (PIX_X, PIX_Y) on the frame F, return
    glyph co-ordinates in (*X, *Y).  Set *BOUNDS to the rectangle
@@ -2016,21 +1735,6 @@ construct_mouse_click (result, event, f)
                          ? up_modifier 
                          : down_modifier));
 
-  /* Notice if the mouse is still grabbed.  */
-  if (event->type == ButtonPress)
-    {
-      if (! x_mouse_grabbed)
-       Vmouse_depressed = Qt;
-      x_mouse_grabbed |= (1 << event->button);
-      last_mouse_frame = f;
-    }
-  else if (event->type == ButtonRelease)
-    {
-      x_mouse_grabbed &= ~(1 << event->button);
-      if (!x_mouse_grabbed)
-       Vmouse_depressed = Qnil;
-    }
-
   {
     int row, column;
 
@@ -2094,9 +1798,10 @@ note_mouse_movement (frame, event)
       /* Ask for another mouse motion event.  */
       {
        int dummy;
+       Window dummy_window;
 
        XQueryPointer (event->display, FRAME_X_WINDOW (frame),
-                      (Window *) &dummy, (Window *) &dummy,
+                      &dummy_window, &dummy_window,
                       &dummy, &dummy, &dummy, &dummy,
                       (unsigned int *) &dummy);
       }
@@ -2116,9 +1821,10 @@ note_mouse_movement (frame, event)
       /* Ask for another mouse motion event.  */
       {
        int dummy;
+       Window dummy_window;
 
        XQueryPointer (event->display, FRAME_X_WINDOW (frame),
-                      (Window *) &dummy, (Window *) &dummy,
+                      &dummy_window, &dummy_window,
                       &dummy, &dummy, &dummy, &dummy,
                       (unsigned int *) &dummy);
       }
@@ -2129,9 +1835,10 @@ note_mouse_movement (frame, event)
         event the next time the mouse moves and we can see if it's
         *still* on the same glyph.  */
       int dummy;
+      Window dummy_window;
       
       XQueryPointer (event->display, FRAME_X_WINDOW (frame),
-                    (Window *) &dummy, (Window *) &dummy,
+                    &dummy_window, &dummy_window,
                     &dummy, &dummy, &dummy, &dummy,
                     (unsigned int *) &dummy);
     }
@@ -2237,7 +1944,8 @@ note_mouse_highlight (f, x, y)
 
          /* Put all the overlays we want in a vector in overlay_vec.
             Store the length in len.  */
-         noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len, &ignor1);
+         noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
+                                  NULL, NULL);
          noverlays = sort_overlays (overlay_vec, noverlays, w);
 
          /* Find the highest priority overlay that has a mouse-face prop.  */
@@ -2390,15 +2098,21 @@ show_mouse_face (hl)
   int width = window_internal_width (w);
   FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
   int i;
-  int curs_x = f->phys_cursor_x;
-  int curs_y = f->phys_cursor_y;
   int cursor_off = 0;
+  int old_curs_x = curs_x;
+  int old_curs_y = curs_y;
+
+  /* Set these variables temporarily
+     so that if we have to turn the cursor off and on again
+     we will put it back at the same place.  */
+  curs_x = f->phys_cursor_x;
+  curs_y = f->phys_cursor_y;
 
   for (i = mouse_face_beg_row; i <= mouse_face_end_row; i++)
     {
       int column = (i == mouse_face_beg_row ? mouse_face_beg_col : w->left);
       int endcolumn = (i == mouse_face_end_row ? mouse_face_end_col : w->left + width);
-      endcolumn = min (endcolumn, FRAME_CURRENT_GLYPHS (f)->used[i] - w->left);
+      endcolumn = min (endcolumn, FRAME_CURRENT_GLYPHS (f)->used[i]);
 
       /* If the cursor's in the text we are about to rewrite,
         turn the cursor off.  */
@@ -2422,6 +2136,9 @@ show_mouse_face (hl)
   if (cursor_off)
     x_display_cursor (f, 1);
 
+  curs_x = old_curs_x;
+  curs_y = old_curs_y;
+
   /* Change the mouse cursor according to the value of HL.  */
   if (hl > 0)
     XDefineCursor (XDISPLAY FRAME_X_WINDOW (f), f->display.x->cross_cursor);
@@ -2522,7 +2239,8 @@ XTmouse_position (f, bar_window, part, x, y, time)
 
        win = root;
 
-       if (x_mouse_grabbed && FRAME_LIVE_P (last_mouse_frame))
+       if (x_mouse_grabbed && last_mouse_frame
+           && FRAME_LIVE_P (last_mouse_frame))
          {
            /* If mouse was grabbed on a frame, give coords for that frame
               even if the mouse is now outside it.  */
@@ -2610,10 +2328,6 @@ XTmouse_position (f, bar_window, part, x, y, time)
 
   UNBLOCK_INPUT;
 }
-
-#else /* ! defined (HAVE_X11) */
-#define XEvent XKeyPressedEvent
-#endif /* ! defined (HAVE_X11) */
 \f
 /* Scroll bar support.  */
 
@@ -2901,7 +2615,10 @@ XTset_vertical_scroll_bar (window, portion, whole, position)
   /* Where should this scroll bar be, pixelwise?  */
   int pixel_top  = CHAR_TO_PIXEL_ROW (f, top);
   int pixel_left = CHAR_TO_PIXEL_COL (f, left);
-  int pixel_width = VERTICAL_SCROLL_BAR_PIXEL_WIDTH (f);
+  int pixel_width
+    = (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
+       ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
+       : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->display.x->font)));
   int pixel_height = VERTICAL_SCROLL_BAR_PIXEL_HEIGHT (f, height);
 
   struct scroll_bar *bar;
@@ -3177,9 +2894,10 @@ x_scroll_bar_note_movement (bar, event)
      moves and we can see *still* on the same position.  */
   {
     int dummy;
+    Window dummy_window;
       
     XQueryPointer (event->xmotion.display, event->xmotion.window,
-                  (Window *) &dummy, (Window *) &dummy,
+                  &dummy_window, &dummy_window,
                   &dummy, &dummy, &dummy, &dummy,
                   (unsigned int *) &dummy);
   }
@@ -3270,8 +2988,7 @@ x_scroll_bar_clear (f)
 {
   Lisp_Object bar;
 
-  for (bar = FRAME_SCROLL_BARS (f);
-       XTYPE (bar) == Lisp_Vector;
+  for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
        bar = XSCROLL_BAR (bar)->next)
     XClearArea (x_current_display, SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
                0, 0, 0, 0, True);
@@ -3316,7 +3033,68 @@ process_expose_from_menu (event)
 
   UNBLOCK_INPUT;
 }
+\f
+/* Define a queue to save up SelectionRequest events for later handling.  */
+
+struct selection_event_queue
+  {
+    XEvent event;
+    struct selection_event_queue *next;
+  };
+
+static struct selection_event_queue *queue;
+
+/* Nonzero means queue up certain events--don't process them yet.  */
+static int x_queue_selection_requests;
+
+/* Queue up an X event *EVENT, to be processed later.  */
+
+static void
+x_queue_event (event)
+     XEvent *event;
+{
+  struct selection_event_queue *queue_tmp
+    = (struct selection_event_queue *) malloc (sizeof (struct selection_event_queue));
+
+  if (queue_tmp != NULL) 
+    {
+      queue_tmp->event = *event;
+      queue_tmp->next = queue;
+      queue = queue_tmp;
+    }
+}
+
+/* Take all the queued events and put them back
+   so that they get processed afresh.  */
+
+static void
+x_unqueue_events ()
+{
+  while (queue != NULL) 
+    {
+      struct selection_event_queue *queue_tmp = queue;
+      XPutBackEvent (XDISPLAY &queue_tmp->event);
+      queue = queue_tmp->next;
+      free ((char *)queue_tmp);
+    }
+}
+
+/* Start queuing SelectionRequest events.  */
+
+void
+x_start_queuing_selection_requests ()
+{
+  x_queue_selection_requests++;
+}
 
+/* Stop queuing SelectionRequest events.  */
+
+void
+x_stop_queuing_selection_requests ()
+{
+  x_queue_selection_requests--;
+  x_unqueue_events ();
+}
 \f
 /* The main X event-reading loop - XTread_socket.  */
 
@@ -3395,7 +3173,10 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
 
   interrupt_input_pending = 0;
   BLOCK_INPUT;
-       
+
+  /* So people can tell when we have read the available input.  */
+  input_signal_count++;
+
   if (numchars <= 0)
     abort ();                  /* Don't think this happens. */
 
@@ -3426,7 +3207,6 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
 
       switch (event.type)
        {
-#ifdef HAVE_X11
        case ClientMessage:
          {
            if (event.xclient.message_type == Xatom_wm_protocols
@@ -3547,24 +3327,27 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
          if (!x_window_to_frame (event.xselectionrequest.owner))
            goto OTHER;
 #endif /* USE_X_TOOLKIT */
-         {
-           XSelectionRequestEvent *eventp = (XSelectionRequestEvent *) &event;
+         if (x_queue_selection_requests)
+           x_queue_event (&event);
+         else
+           {
+             XSelectionRequestEvent *eventp = (XSelectionRequestEvent *) &event;
 
-           if (numchars == 0)
-             abort ();
+             if (numchars == 0)
+               abort ();
 
-           bufp->kind = selection_request_event;
-           SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
-           SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
-           SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
-           SELECTION_EVENT_TARGET (bufp) = eventp->target;
-           SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
-           SELECTION_EVENT_TIME (bufp) = eventp->time;
-           bufp++;
+             bufp->kind = selection_request_event;
+             SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
+             SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
+             SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
+             SELECTION_EVENT_TARGET (bufp) = eventp->target;
+             SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
+             SELECTION_EVENT_TIME (bufp) = eventp->time;
+             bufp++;
 
-           count += 1;
-           numchars -= 1;
-         }
+             count += 1;
+             numchars -= 1;
+           }
          break;
 
        case PropertyNotify:
@@ -3637,55 +3420,7 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
                                  source area was completely
                                  available */
          break;
-#else /* ! defined (HAVE_X11) */
-       case ExposeWindow:
-         if (event.subwindow != 0)
-           break;              /* duplicate event */
-         f = x_window_to_frame (event.window);
-         if (event.window == f->display.x->icon_desc)
-           {
-             refreshicon (f);
-             f->async_iconified = 1;
-           }
-         if (event.window == FRAME_X_WINDOW (f))
-           {
-             /* Say must check all windows' needs_exposure flags.  */
-             expose_all_windows = 1;
-             f->display.x->needs_exposure = 1;
-             f->async_visible = 1;
-           }
-         break;
 
-       case ExposeRegion:
-         if (event.subwindow != 0)
-           break;              /* duplicate event */
-         f = x_window_to_frame (event.window);
-         if (event.window == f->display.x->icon_desc)
-           {
-             refreshicon (f);
-             break;
-           }
-         /* If window already needs full redraw, ignore this rectangle.  */
-         if (expose_all_windows && f->display.x->needs_exposure)
-           break;
-         /* Put the event on the queue of rectangles to redraw.  */
-         if (enqueue_event (&event, &x_expose_queue))
-           /* If it is full, we can't record the rectangle,
-              so redraw this entire window.  */
-           {
-             /* Say must check all windows' needs_exposure flags.  */
-             expose_all_windows = 1;
-             f->display.x->needs_exposure = 1;
-           }
-         break;
-
-       case ExposeCopy:
-         /* This should happen only when we are expecting it,
-            in x_read_exposes.  */
-         abort ();
-#endif /* ! defined (HAVE_X11) */
-
-#ifdef HAVE_X11
        case UnmapNotify:
          f = x_any_window_to_frame (event.xunmap.window);
          if (f)                /* F may no longer exist if
@@ -3729,17 +3464,6 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
        case VisibilityNotify:
          break;
 
-#else /* ! defined (HAVE_X11) */
-       case UnmapWindow:
-         f = x_window_to_frame (event.window);
-         if (event.window == f->display.x->icon_desc)
-           f->async_iconified = 0;
-         if (event.window == FRAME_X_WINDOW (f))
-           f->async_visible = 0;
-         break;
-#endif /* ! defined (HAVE_X11) */
-
-#ifdef HAVE_X11
        case KeyPress:
          f = x_any_window_to_frame (event.xkey.window);
 
@@ -3873,56 +3597,6 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
                abort ();
            }
          break;
-#else /* ! defined (HAVE_X11) */
-       case KeyPressed:
-         {
-           register char *where_mapping;
-
-           f = x_window_to_frame (event.window);
-           /* Ignore keys typed on icon windows.  */
-           if (f != 0 && event.window == f->display.x->icon_desc)
-             break;
-           where_mapping = XLookupMapping (&event, &nbytes);
-           /* Nasty fix for arrow keys */
-           if (!nbytes && IsCursorKey (event.detail & 0xff))
-             {
-               switch (event.detail & 0xff)
-                 {
-                 case KC_CURSOR_LEFT:
-                   where_mapping = "\002";
-                   break;
-                 case KC_CURSOR_RIGHT:
-                   where_mapping = "\006";
-                   break;
-                 case KC_CURSOR_UP:
-                   where_mapping = "\020";
-                   break;
-                 case KC_CURSOR_DOWN:
-                   where_mapping = "\016";
-                   break;
-                 }
-               nbytes = 1;
-             }
-           if (numchars - nbytes > 0)
-             {
-               register int i;
-
-               for (i = 0; i < nbytes; i++)
-                 {
-                   bufp->kind = ascii_keystroke;
-                   bufp->code = where_mapping[i];
-                   XSET (bufp->time, Lisp_Int, event.xkey.time);
-                   XSET (bufp->frame_or_window, Lisp_Frame, f);
-                   bufp++;
-                 }
-               count += nbytes;
-               numchars -= nbytes;
-             }
-         }
-         break;
-#endif /* ! defined (HAVE_X11) */
-
-#ifdef HAVE_X11
 
          /* Here's a possible interpretation of the whole
             FocusIn-EnterNotify FocusOut-LeaveNotify mess.  If you get a
@@ -4008,46 +3682,10 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
 #endif /* USE_X_TOOLKIT */
          break;
 
-#else /* ! defined (HAVE_X11) */
-
-       case EnterWindow:
-         if ((event.detail & 0xFF) == 1)
-           break;              /* Coming from our own subwindow */
-         if (event.subwindow != 0)
-           break;              /* Entering our own subwindow.  */
-
-         {
-           f = x_window_to_frame (event.window);
-           x_mouse_frame = f;
-
-           x_new_focus_frame (f);
-         }
-         break;
-
-       case LeaveWindow:
-         if ((event.detail & 0xFF) == 1)
-           break;              /* Entering our own subwindow */
-         if (event.subwindow != 0)
-           break;              /* Leaving our own subwindow.  */
-
-         x_mouse_frame = 0;
-         if (x_focus_frame == 0
-             && x_input_frame != 0
-             && x_input_frame == x_window_to_frame (event.window)
-             && event.window == FRAME_X_WINDOW (x_input_frame))
-           {
-             f = x_input_frame;
-             x_input_frame = 0;
-             if (f)
-               frame_unhighlight (f);
-           }
-         break;
-#endif /* ! defined (HAVE_X11) */
-
-#ifdef HAVE_X11
        case MotionNotify:
          {
-           if (x_mouse_grabbed && FRAME_LIVE_P (last_mouse_frame))
+           if (x_mouse_grabbed && last_mouse_frame
+               && FRAME_LIVE_P (last_mouse_frame))
              f = last_mouse_frame;
            else
              f = x_window_to_frame (event.xmotion.window);
@@ -4211,22 +3849,35 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
              }
            else
              {
-               struct scroll_bar *bar =
-                 x_window_to_scroll_bar (event.xbutton.window);
+               struct scroll_bar *bar
+                 x_window_to_scroll_bar (event.xbutton.window);
 
                if (bar)
                  x_scroll_bar_handle_click (bar, &event, &emacs_event);
 #ifdef USE_X_TOOLKIT
                else
                  {
-                   f = x_any_window_to_frame (event.xbutton.window);
-                   if (f && event.type == ButtonPress)
-                     construct_menu_click (&emacs_event,
-                                           &event, f);
+                   /* Assume we have a menubar button press. A bad
+                       assumption should behave benignly. */
+                   popup_get_selection (&event);
+                   break;
                  }
 #endif /* USE_X_TOOLKIT */
              }
 
+           if (event.type == ButtonPress)
+             {
+               x_mouse_grabbed |= (1 << event.xbutton.button);
+               Vmouse_depressed = Qt;
+               last_mouse_frame = f;
+             }
+           else
+             {
+               x_mouse_grabbed &= ~(1 << event.xbutton.button);
+               if (!x_mouse_grabbed)
+                 Vmouse_depressed = Qnil;
+             }
+
            if (numchars >= 1 && emacs_event.kind != no_event)
              {
                bcopy (&emacs_event, bufp, sizeof (struct input_event));
@@ -4241,56 +3892,11 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
          }
          break;
 
-#else /* ! defined (HAVE_X11) */
-       case ButtonPressed:
-       case ButtonReleased:
-         f = x_window_to_frame (event.window);
-         if (f)
-           {
-             if (event.window == f->display.x->icon_desc)
-               {
-                 x_make_frame_visible (f);
-
-                 if (warp_mouse_on_deiconify)
-                   XWarpMouse (FRAME_X_WINDOW (f), 10, 10);
-                 break;
-               }
-             if (event.window == FRAME_X_WINDOW (f))
-               {
-                 if (f->auto_raise)
-                   x_raise_frame (f);
-               }
-           }
-         enqueue_event (&event, &x_mouse_queue);
-         if (numchars >= 2)
-           {
-             bufp->kind = ascii_keystroke;
-             bufp->code = 'X' & 037; /* C-x */
-             XSET (bufp->frame_or_window, Lisp_Frame, f);
-             XSET (bufp->time, Lisp_Int, event.xkey.time);
-             bufp++;
-
-             bufp->kind = ascii_keystroke;
-             bufp->code = 0; /* C-@ */
-             XSET (bufp->frame_or_window, Lisp_Frame, f);
-             XSET (bufp->time, Lisp_Int, event.xkey.time);
-             bufp++;
-
-             count += 2;
-             numchars -= 2;
-           }
-         break;
-#endif /* ! defined (HAVE_X11) */
-
-#ifdef HAVE_X11
-
        case CirculateNotify:
          break;
        case CirculateRequest:
          break;
 
-#endif /* ! defined (HAVE_X11) */
-
        case MappingNotify:
          /* Someone has changed the keyboard mapping - update the
             local cache.  */
@@ -4318,11 +3924,10 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
        }
     }
 
-#ifdef X_IO_BUG
+  /* On some systems, an X bug causes Emacs to get no more events
+     when the window is destroyed.  Detect that.  (1994.)  */
   if (! event_found) 
     {
-      /* On some systems, an X bug causes Emacs to get no more events
-        when the window is destroyed.  Detect that.  (1994.)  */
       /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
         One XNOOP in 100 loops will make Emacs terminate.
         B. Bretthauer, 1994 */
@@ -4333,7 +3938,6 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
          XNoOp (x_current_display);
        }
     }
-#endif /* X_IO_BUG */
 
 #if 0 /* This fails for serial-line connections to the X server, 
         because the characters arrive one by one, and a partial
@@ -4362,90 +3966,17 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
 #endif /* HAVE_SELECT */
 #endif /* 0 */
 
-#ifndef HAVE_X11
-  if (updating_frame == 0)
-    x_do_pending_expose ();
-#endif
-
   /* If the focus was just given to an autoraising frame,
      raise it now.  */
-#ifdef HAVE_X11
   if (pending_autoraise_frame)
     {
       x_raise_frame (pending_autoraise_frame);
       pending_autoraise_frame = 0;
     }
-#endif
 
   UNBLOCK_INPUT;
   return count;
 }
-
-#ifndef HAVE_X11
-/* Read and process only Expose events
-   until we get an ExposeCopy event; then return.
-   This is used in insert/delete line.
-   We assume input is already blocked.  */
-
-static void
-x_read_exposes ()
-{
-  struct frame *f;
-  XKeyPressedEvent event;
-
-  while (1)
-    {
-      /* while there are more events*/
-      XMaskEvent (ExposeWindow | ExposeRegion | ExposeCopy, &event);
-      switch (event.type)
-       {
-       case ExposeWindow:
-         if (event.subwindow != 0)
-           break;                      /* duplicate event */
-         f = x_window_to_frame (event.window);
-         if (event.window == f->display.x->icon_desc)
-           {
-             refreshicon (f);
-             break;
-           }
-         if (event.window == FRAME_X_WINDOW (f))
-           {
-             expose_all_windows = 1;
-             f->display.x->needs_exposure = 1;
-             break;
-           }
-         break;
-
-       case ExposeRegion:
-         if (event.subwindow != 0)
-           break;                      /* duplicate event */
-         f = x_window_to_frame (event.window);
-         if (event.window == f->display.x->icon_desc)
-           {
-             refreshicon (f);
-             break;
-           }
-         /* If window already needs full redraw, ignore this rectangle.  */
-         if (expose_all_windows && f->display.x->needs_exposure)
-           break;
-         /* Put the event on the queue of rectangles to redraw.  */
-         if (enqueue_event (&event, &x_expose_queue))
-           /* If it is full, we can't record the rectangle,
-              so redraw this entire window.  */
-           {
-             /* Say must check all windows' needs_exposure flags.  */
-             expose_all_windows = 1;
-             f->display.x->needs_exposure = 1;
-           }
-         break;
-
-       case ExposeCopy:
-         return;
-       }
-    }
-}
-#endif /* HAVE_X11 */
-
 \f
 /* Drawing the cursor.  */
 
@@ -4456,32 +3987,14 @@ static void
 x_draw_box (f)
      struct frame *f;
 {
-  int left = CHAR_TO_PIXEL_COL (f, f->cursor_x);
-  int top  = CHAR_TO_PIXEL_ROW (f, f->cursor_y);
+  int left = CHAR_TO_PIXEL_COL (f, curs_x);
+  int top  = CHAR_TO_PIXEL_ROW (f, curs_y);
   int width = FONT_WIDTH (f->display.x->font);
   int height = f->display.x->line_height;
 
-#ifdef HAVE_X11
   XDrawRectangle (x_current_display, FRAME_X_WINDOW (f),
                  f->display.x->cursor_gc,
                  left, top, width - 1, height - 1);
-#else /* ! defined (HAVE_X11) */
-  XPixSet (FRAME_X_WINDOW (f),
-          left, top, width, 1,
-          f->display.x->cursor_pixel);
-
-  XPixSet (FRAME_X_WINDOW (f),
-          left, top, 1, height,
-          f->display.x->cursor_pixel);
-
-  XPixSet (FRAME_X_WINDOW (f),
-          left+width-1, top, 1, height,
-          f->display.x->cursor_pixel);
-
-  XPixSet (FRAME_X_WINDOW (f),
-          left, top+height-1, width, 1,
-          f->display.x->cursor_pixel);
-#endif /* ! defined (HAVE_X11) */
 }
 
 /* Clear the cursor of frame F to background color,
@@ -4499,15 +4012,7 @@ clear_cursor (f)
       || f->phys_cursor_x < 0)
     return;
 
-#ifdef HAVE_X11
   x_display_cursor (f, 0);
-#else /* ! defined (HAVE_X11) */
-  XPixSet (FRAME_X_WINDOW (f),
-          CHAR_TO_PIXEL_COL (f, f->phys_cursor_x),
-          CHAR_TO_PIXEL_ROW (f, f->phys_cursor_y),
-          FONT_WIDTH (f->display.x->font), f->display.x->line_height,
-          f->display.x->background_pixel);
-#endif /* ! defined (HAVE_X11) */
   f->phys_cursor_x = -1;
 }
 
@@ -4723,38 +4228,7 @@ x_display_cursor (f, on)
 refreshicon (f)
      struct frame *f;
 {
-#ifdef HAVE_X11
   /* Normally, the window manager handles this function. */
-#else /* ! defined (HAVE_X11) */
-  int mask;
-
-  if (f->display.x->icon_bitmap_flag)
-    XBitmapBitsPut (f->display.x->icon_desc, 0,  0, sink_width, sink_height,
-                   sink_bits, BlackPixel, WHITE_PIX_DEFAULT, 
-                   icon_bitmap, GXcopy, AllPlanes);
-  else
-    {
-      extern struct frame *selected_frame;
-      struct Lisp_String *str;
-      unsigned char *string;
-
-      string
-       = XSTRING (XBUFFER (XWINDOW (f->selected_window)->buffer)->name)->data;
-
-      if (f->display.x->icon_label != string)
-       {
-         f->display.x->icon_label = string;
-         XChangeWindow (f->display.x->icon_desc,
-                        XQueryWidth (string, icon_font_info->id) + 10,
-                        icon_font_info->height + 10);
-       }
-
-      XText (f->display.x->icon_desc, 5, 5, string,
-            str->size, icon_font_info->id,
-            BLACK_PIX_DEFAULT, WHITE_PIX_DEFAULT);
-    }
-  XFlushQueue ();
-#endif /* ! defined (HAVE_X11) */
 }
 
 /* Make the x-window of frame F use the gnu icon bitmap.  */
@@ -4769,37 +4243,12 @@ x_bitmap_icon (f)
   if (FRAME_X_WINDOW (f) == 0)
     return 1;
 
-#ifdef HAVE_X11
   if (! icon_bitmap)
     icon_bitmap =
       XCreateBitmapFromData (x_current_display, FRAME_X_WINDOW (f),
                             gnu_bits, gnu_width, gnu_height);
   x_wm_set_icon_pixmap (f, icon_bitmap);
   f->display.x->icon_bitmap_flag = 1;
-#else /* ! defined (HAVE_X11) */
-  if (f->display.x->icon_desc)
-    {
-      XClearIconWindow (FRAME_X_WINDOW (f));
-      XDestroyWindow (f->display.x->icon_desc);
-    }
-
-  icon_window = XCreateWindow (f->display.x->parent_desc,
-                              0, 0, sink_width, sink_height,
-                              2, WhitePixmap, (Pixmap) NULL);
-
-  if (icon_window == 0)
-    return 1;
-
-  XSetIconWindow (FRAME_X_WINDOW (f), icon_window);
-  XSelectInput (icon_window, ExposeWindow | UnmapWindow);
-
-  f->display.x->icon_desc = icon_window;
-  f->display.x->icon_bitmap_flag = 1;
-
-  if (icon_bitmap == 0)
-    icon_bitmap
-      = XStoreBitmap (sink_mask_width, sink_mask_height, sink_mask_bits);
-#endif /* ! defined (HAVE_X11) */
 
   return 0;
 }
@@ -4812,26 +4261,9 @@ x_text_icon (f, icon_name)
      struct frame *f;
      char *icon_name;
 {
-#ifndef HAVE_X11
-  int mask;
-  int width;
-  Window icon_window;
-  char *X_DefaultValue;
-  Bitmap b1;
-
-#ifndef WhitePixel
-#define WhitePixel 1
-#endif /* WhitePixel */
-
-#ifndef BlackPixel
-#define BlackPixel 0
-#endif /* BlackPixel */
-#endif /* HAVE_X11 */
-  
   if (FRAME_X_WINDOW (f) == 0)
     return 1;
 
-#ifdef HAVE_X11
   if (icon_name)
     f->display.x->icon_label = icon_name;
   else
@@ -4845,42 +4277,6 @@ x_text_icon (f, icon_name)
   
   f->display.x->icon_bitmap_flag = 0;
   x_wm_set_icon_pixmap (f, 0);
-#else /* ! defined (HAVE_X11) */
-  if (icon_font_info == 0)
-    icon_font_info
-      = XGetFont (XGetDefault (XDISPLAY
-                              (char *) XSTRING (Vinvocation_name)->data,
-                              "BodyFont"));
-
-  if (f->display.x->icon_desc)
-    {
-      XClearIconWindow (XDISPLAY FRAME_X_WINDOW (f));
-      XDestroyWindow (XDISPLAY f->display.x->icon_desc);
-    }
-
-  if (icon_name)
-    f->display.x->icon_label = (unsigned char *) icon_name;
-  else
-    if (! f->display.x->icon_label)
-      f->display.x->icon_label = XSTRING (f->name)->data;
-
-  width = XStringWidth (f->display.x->icon_label, icon_font_info, 0, 0);
-  icon_window = XCreateWindow (f->display.x->parent_desc,
-                              f->display.x->left_pos,
-                              f->display.x->top_pos,
-                              width + 10, icon_font_info->height + 10,
-                              2, BlackPixmap, WhitePixmap);
-
-  if (icon_window == 0)
-    return 1;
-
-  XSetIconWindow (FRAME_X_WINDOW (f), icon_window);
-  XSelectInput (icon_window, ExposeWindow | ExposeRegion | UnmapWindow | ButtonPressed);
-
-  f->display.x->icon_desc = icon_window;
-  f->display.x->icon_bitmap_flag = 0;
-  f->display.x->icon_label = 0;
-#endif /* ! defined (HAVE_X11) */
 
   return 0;
 }
@@ -5047,12 +4443,11 @@ x_trace_wire ()
    to the font named NEWNAME.  This is safe to use
    even before F has an actual x-window.  */
 
-#ifdef HAVE_X11
-
 struct font_info
 {
   XFontStruct *font;
   char *name;
+  char *full_name;
 };
 
 /* A table of all the fonts we have already loaded.  */
@@ -5066,6 +4461,11 @@ static int x_font_table_size;
    0 <= n_fonts <= x_font_table_size.  */
 static int n_fonts;
 
+/* Give frame F the font named FONTNAME as its default font, and
+   return the full name of that font.  FONTNAME may be a wildcard
+   pattern; in that case, we choose some font that fits the pattern.
+   The return value shows which font we chose.  */
+
 Lisp_Object
 x_new_font (f, fontname)
      struct frame *f;
@@ -5098,10 +4498,11 @@ x_new_font (f, fontname)
 
       for (i = 0; i < n_fonts; i++)
        for (j = 0; j < n_matching_fonts; j++)
-         if (!strcmp (x_font_table[i].name, font_names[j]))
+         if (!strcmp (x_font_table[i].name, font_names[j])
+             || !strcmp (x_font_table[i].full_name, font_names[j]))
            {
              already_loaded = i;
-             fontname = font_names[j];
+             fontname = x_font_table[i].full_name;
              goto found_font;
            }
     }
@@ -5110,11 +4511,11 @@ x_new_font (f, fontname)
   /* If we have, just return it from the table.  */
   if (already_loaded >= 0)
     f->display.x->font = x_font_table[already_loaded].font;
-  
   /* Otherwise, load the font and add it to the table.  */
   else
     {
       int i;
+      char *full_name;
       XFontStruct *font;
 
       /* Try to find a character-cell font in the list.  */
@@ -5158,10 +4559,58 @@ x_new_font (f, fontname)
                                              * sizeof (x_font_table[0])));
        }
 
+      /* Try to get the full name of FONT.  Put it in full_name.  */
+      full_name = 0;
+      for (i = 0; i < font->n_properties; i++)
+       {
+         char *atom
+           = XGetAtomName (x_current_display, font->properties[i].name);
+         if (!strcmp (atom, "FONT"))
+           {
+             char *name = XGetAtomName (x_current_display,
+                                        (Atom) (font->properties[i].card32));
+             char *p = name;
+             int dashes = 0;
+
+             /* Count the number of dashes in the "full name".
+                If it is too few, this isn't really the font's full name,
+                so don't use it.
+                In X11R4, the fonts did not come with their canonical names
+                stored in them.  */
+             while (*p)
+               {
+                 if (*p == '-')
+                   dashes++;
+                 p++;
+               }
+
+             if (dashes >= 13)
+               full_name = name;
+           }
+
+         XFree (atom);
+       }
+
       x_font_table[n_fonts].name = (char *) xmalloc (strlen (fontname) + 1);
       bcopy (fontname, x_font_table[n_fonts].name, strlen (fontname) + 1);
+      if (full_name != 0)
+       x_font_table[n_fonts].full_name = full_name;
+      else
+       x_font_table[n_fonts].full_name = x_font_table[n_fonts].name;
       f->display.x->font = x_font_table[n_fonts++].font = font;
+
+      if (full_name)
+       fontname = full_name;
+    }
+
+  /* Compute the scroll bar width in character columns.  */
+  if (f->scroll_bar_pixel_width > 0)
+    {
+      int wid = FONT_WIDTH (f->display.x->font);
+      f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
     }
+  else
+    f->scroll_bar_cols = 2;
 
   /* Now make the frame display the given font.  */
   if (FRAME_X_WINDOW (f) != 0)
@@ -5193,34 +4642,10 @@ x_new_font (f, fontname)
     return lispy_name;
   }
 }
-#else /* ! defined (HAVE_X11) */
-x_new_font (f, newname)
-     struct frame *f;
-     register char *newname;
-{
-  FONT_TYPE *temp;
-  int mask;
-
-  temp = XGetFont (newname);
-  if (temp == (FONT_TYPE *) 0)
-    return 1;
-
-  if (f->display.x->font)
-    XLoseFont (f->display.x->font);
-
-  f->display.x->font = temp;
-
-  if (FRAME_X_WINDOW (f) != 0)
-    x_set_window_size (f, 0, f->width, f->height);
-
-  return 0;
-}
-#endif /* ! defined (HAVE_X11) */
 \f
 x_calc_absolute_position (f)
      struct frame *f;
 {
-#ifdef HAVE_X11
   Window win, child;
   int win_x = 0, win_y = 0;
   int flags = f->display.x->size_hint_flags;
@@ -5261,19 +4686,6 @@ x_calc_absolute_position (f)
      are now relative to the top and left screen edges,
      so the flags should correspond.  */
   f->display.x->size_hint_flags &= ~ (XNegative | YNegative);
-#else /* ! defined (HAVE_X11) */
-  WINDOWINFO_TYPE parentinfo;
-
-  XGetWindowInfo (FRAME_X_WINDOW (f), &parentinfo);
-
-  if (f->display.x->left_pos < 0)
-    f->display.x->left_pos = parentinfo.width + (f->display.x->left_pos + 1)
-      - PIXEL_WIDTH (f) - 2 * f->display.x->internal_border_width;
-
-  if (f->display.x->top_pos < 0)
-    f->display.x->top_pos = parentinfo.height + (f->display.x->top_pos + 1)
-      - PIXEL_HEIGHT (f) - 2 * f->display.x->internal_border_width;
-#endif /* ! defined (HAVE_X11) */
 }
 
 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
@@ -5346,16 +4758,17 @@ x_set_window_size (f, change_gravity, cols, rows)
 
   check_frame_size (f, &rows, &cols);
   f->display.x->vertical_scroll_bar_extra
-    = (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
-       ? VERTICAL_SCROLL_BAR_PIXEL_WIDTH (f)
-       : 0);
+    = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
+       ? 0
+       : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
+       ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
+       : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->display.x->font)));
   pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
   pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
 
-#ifdef HAVE_X11
   f->display.x->win_gravity = NorthWestGravity;
   x_wm_set_size_hint (f, 0, 0);
-#endif /* ! defined (HAVE_X11) */
+
   XSync (x_current_display, False);
   XChangeWindowSize (FRAME_X_WINDOW (f), pixelwidth, pixelheight);
 
@@ -5391,18 +4804,6 @@ x_set_window_size (f, change_gravity, cols, rows)
   UNBLOCK_INPUT;
 #endif /* not USE_X_TOOLKIT */
 }
-
-#ifndef HAVE_X11
-x_set_resize_hint (f)
-     struct frame *f;
-{
-  XSetResizeHint (FRAME_X_WINDOW (f),
-                 2 * f->display.x->internal_border_width,
-                 2 * f->display.x->internal_border_width,
-                 FONT_WIDTH (f->display.x->font),
-                 f->display.x->line_height);
-}
-#endif /* HAVE_X11 */
 \f
 /* Mouse warping, focus shifting, raising and lowering.  */
 
@@ -5441,7 +4842,6 @@ x_set_mouse_pixel_position (f, pix_x, pix_y)
   UNBLOCK_INPUT;
 }
 
-#ifdef HAVE_X11
 x_focus_on_frame (f)
      struct frame *f;
 {
@@ -5468,8 +4868,6 @@ x_unfocus_frame (f)
 #endif /* ! 0 */
 }
 
-#endif /* ! defined (HAVE_X11) */
-
 /* Raise frame F.  */
 
 x_raise_frame (f)
@@ -5517,7 +4915,6 @@ XTframe_raise_lower (f, raise)
     x_lower_frame (f);
 }
 
-
 /* Change from withdrawn state to mapped state,
    or deiconify. */
 
@@ -5530,7 +4927,6 @@ x_make_frame_visible (f)
 
   if (! FRAME_VISIBLE_P (f))
     {
-#ifdef HAVE_X11
 #ifndef USE_X_TOOLKIT
       if (! FRAME_ICONIFIED_P (f))
        x_set_offset (f, f->display.x->left_pos, f->display.x->top_pos, 0);
@@ -5550,32 +4946,52 @@ x_make_frame_visible (f)
       if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
        XMapSubwindows (x_current_display, FRAME_X_WINDOW (f));
 #endif
-#else /* ! defined (HAVE_X11) */
-      XMapWindow (XDISPLAY FRAME_X_WINDOW (f));
-      if (f->display.x->icon_desc != 0)
-       XUnmapWindow (f->display.x->icon_desc);
-
-      /* Handled by the MapNotify event for X11 */
-      f->async_visible = 1;
-      f->async_iconified = 0;
-
-      /* NOTE: this may cause problems for the first frame. */
-      XTcursor_to (0, 0);
-#endif /* ! defined (HAVE_X11) */
     }
 
   XFlushQueue ();
 
-  UNBLOCK_INPUT;
-
   /* Synchronize to ensure Emacs knows the frame is visible
      before we do anything else.  We do this loop with input not blocked
      so that incoming events are handled.  */
   {
     Lisp_Object frame;
+    int count = input_signal_count;
+
+    /* This must come after we set COUNT.  */
+    UNBLOCK_INPUT;
+
     XSET (frame, Lisp_Frame, f);
-    while (! f->async_visible)
-      x_sync (frame);
+
+    while (1)
+      {
+       x_sync (frame);
+       /* Once we have handled input events,
+          we should have received the MapNotify if one is coming.
+          So if we have not got it yet, stop looping.
+          Some window managers make their own decisions
+          about visibility.  */
+       if (input_signal_count != count)
+         break;
+       /* Machines that do polling rather than SIGIO have been observed
+          to go into a busy-wait here.  So we'll fake an alarm signal
+          to let the handler know that there's something to be read.
+          We used to raise a real alarm, but it seems that the handler
+          isn't always enabled here.  This is probably a bug.  */
+       if (input_polling_used ())
+         {
+           /* It could be confusing if a real alarm arrives while processing
+              the fake one.  Turn it off and let the handler reset it.  */
+           alarm (0);
+           input_poll_signal ();
+         }
+       /* Once we have handled input events,
+          we should have received the MapNotify if one is coming.
+          So if we have not got it yet, stop looping.
+          Some window managers make their own decisions
+          about visibility.  */
+       if (input_signal_count != count)
+         break;
+      }
     FRAME_SAMPLE_VISIBILITY (f);
   }
 }
@@ -5622,7 +5038,6 @@ x_make_frame_invisible (f)
       error ("Can't notify window manager of window withdrawal");
     }
 #else /* ! defined (HAVE_X11R4) */
-#ifdef HAVE_X11
 
   /*  Tell the window manager what we're going to do.  */
   if (! EQ (Vx_no_window_manager, Qt))
@@ -5646,13 +5061,6 @@ x_make_frame_invisible (f)
 
   /* Unmap the window ourselves.  Cheeky!  */
   XUnmapWindow (x_current_display, window);
-#else /* ! defined (HAVE_X11) */
-
-  XUnmapWindow (FRAME_X_WINDOW (f));
-  if (f->display.x->icon_desc != 0)
-    XUnmapWindow (f->display.x->icon_desc);
-
-#endif /* ! defined (HAVE_X11) */
 #endif /* ! defined (HAVE_X11R4) */
 
   /* We can't distinguish this from iconification
@@ -5715,7 +5123,6 @@ x_iconify_frame (f)
 
   BLOCK_INPUT;
 
-#ifdef HAVE_X11
   /* 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))
@@ -5761,16 +5168,6 @@ x_iconify_frame (f)
     }
 
   f->async_iconified = 1;
-#else /* ! defined (HAVE_X11) */
-  XUnmapWindow (XDISPLAY FRAME_X_WINDOW (f));
-
-  f->async_visible = 0;                /* Handled in the UnMap event for X11. */
-  if (f->display.x->icon_desc != 0)
-    {
-      XMapWindow (XDISPLAY f->display.x->icon_desc);
-      refreshicon (f);
-    }
-#endif /* ! defined (HAVE_X11) */
 
   XFlushQueue ();
   UNBLOCK_INPUT;
@@ -5795,6 +5192,12 @@ x_destroy_window (f)
   free_frame_faces (f);
   XFlushQueue ();
 
+  FRAME_X_SCREEN (f)->reference_count--;
+#if 0
+  if (FRAME_X_SCREEN (f)->reference_count == 0)
+    free (FRAME_X_SCREEN (f));
+#endif
+
   xfree (f->display.x);
   f->display.x = 0;
   if (f == x_focus_frame)
@@ -5812,84 +5215,8 @@ x_destroy_window (f)
   UNBLOCK_INPUT;
 }
 \f
-/* Manage event queues for X10.  */
-
-#ifndef HAVE_X11
-
-/* Manage event queues.
-
-   This code is only used by the X10 support.
-
-   We cannot leave events in the X queue and get them when we are ready
-   because X does not provide a subroutine to get only a certain kind
-   of event but not block if there are no queued events of that kind.
-
-   Therefore, we must examine events as they come in and copy events
-   of certain kinds into our private queues.
-
-   All ExposeRegion events are put in x_expose_queue.
-   All ButtonPress and ButtonRelease events are put in x_mouse_queue.  */
-
-
-/* Write the event *P_XREP into the event queue *QUEUE.
-   If the queue is full, do nothing, but return nonzero.  */
-
-int
-enqueue_event (p_xrep, queue)
-     register XEvent *p_xrep;
-     register struct event_queue *queue;
-{
-  int newindex = queue->windex + 1;
-  if (newindex == EVENT_BUFFER_SIZE)
-    newindex = 0;
-  if (newindex == queue->rindex)
-    return -1;
-  queue->xrep[queue->windex] = *p_xrep;
-  queue->windex = newindex;
-  return 0;
-}
-
-/* Fetch the next event from queue *QUEUE and store it in *P_XREP.
-   If *QUEUE is empty, do nothing and return 0.  */
-
-int
-dequeue_event (p_xrep, queue)
-     register XEvent *p_xrep;
-     register struct event_queue *queue;
-{
-  if (queue->windex == queue->rindex)
-    return 0;
-  *p_xrep = queue->xrep[queue->rindex++];
-  if (queue->rindex == EVENT_BUFFER_SIZE)
-    queue->rindex = 0;
-  return 1;
-}
-
-/* Return the number of events buffered in *QUEUE.  */
-
-int
-queue_event_count (queue)
-     register struct event_queue *queue;
-{
-  int tem = queue->windex - queue->rindex;
-  if (tem >= 0)
-    return tem;
-  return EVENT_BUFFER_SIZE + tem;
-}
-
-/* Return nonzero if mouse input is pending.  */
-
-int
-mouse_event_pending_p ()
-{
-  return queue_event_count (&x_mouse_queue);
-}
-#endif /* HAVE_X11 */
-\f
 /* Setting window manager hints.  */
 
-#ifdef HAVE_X11
-
 /* Set the normal size hints for the window manager, for frame F.
    FLAGS is the flags word to use--or 0 meaning preserve the flags
    that the window now has.
@@ -6108,9 +5435,7 @@ x_term_init (display_name, xrm_option, resource_name)
 #endif /* ! defined (F_SETOWN) */
 #endif /* F_SETOWN_BUG */
   
-#ifdef X_IO_BUG
   x_noop_count = 0;
-#endif
 
   x_focus_frame = x_highlight_frame = 0;
 
@@ -6143,7 +5468,6 @@ x_term_init (display_name, xrm_option, resource_name)
 Check the DISPLAY environment variable or use \"-d\"\n",
           display_name);
 
-#ifdef HAVE_X11
   {
 #if 0
     XSetAfterFunction (x_current_display, x_trace_wire);
@@ -6172,8 +5496,6 @@ Check the DISPLAY environment variable or use \"-d\"\n",
     change_keyboard_wait_descriptor (ConnectionNumber (x_current_display));
   change_input_fd (ConnectionNumber (x_current_display));
 
-#endif /* ! defined (HAVE_X11) */
-  
 #ifndef F_SETOWN_BUG
 #ifdef F_SETOWN
   old_fcntl_owner = fcntl (ConnectionNumber (x_current_display), F_GETOWN, 0);
@@ -6249,5 +5571,4 @@ syms_of_xterm ()
   staticpro (&mouse_face_window);
   mouse_face_window = Qnil;
 }
-#endif /* ! defined (HAVE_X11) */
 #endif /* ! defined (HAVE_X_WINDOWS) */