(read_minibuf_unwind): Clear last_overlay_modified field.
[bpt/emacs.git] / src / xterm.c
index 5b8e9e3..2be321e 100644 (file)
@@ -1,5 +1,5 @@
 /* X Communication module for terminals which understand the X protocol.
-   Copyright (C) 1989, 1993, 1994 Free Software Foundation, Inc.
+   Copyright (C) 1989, 93, 94, 95, 1996 Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
@@ -15,7 +15,8 @@ GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with GNU Emacs; see the file COPYING.  If not, write to
-the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
+the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+Boston, MA 02111-1307, USA.  */
 
 /* Xt features made by Fred Pierresteguy.  */
 
@@ -83,9 +84,17 @@ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
 #include "keyboard.h"
 #include "intervals.h"
 
+#ifdef USE_X_TOOLKIT
+#include <X11/Shell.h>
+#endif
+
 #ifdef USE_X_TOOLKIT
 extern void free_frame_menubar ();
+extern FRAME_PTR x_menubar_window_to_frame ();
+#if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
+#define HACK_EDITRES
 extern void _XEditResCheckMessages ();
+#endif /* not NO_EDITRES */
 #endif /* USE_X_TOOLKIT */
 
 #ifndef USE_X_TOOLKIT
@@ -94,13 +103,30 @@ extern void _XEditResCheckMessages ();
 #endif
 
 #ifdef USE_X_TOOLKIT
+#include "widget.h"
 #ifndef XtNinitialState
 #define XtNinitialState "initialState"
 #endif
 #endif
 
+#ifdef HAVE_SETLOCALE
+/* So we can do setlocale.  */
+#include <locale.h>
+#endif
+
+#ifdef SOLARIS2
+/* memmove will be defined as a macro in Xfuncs.h unless
+   <string.h> is included beforehand.  The declaration for memmove in
+   <string.h> will cause a syntax error when Xfuncs.h later includes it.  */
+#include <string.h>
+#endif
+
+#ifndef min
 #define min(a,b) ((a)<(b) ? (a) : (b))
+#endif
+#ifndef max
 #define max(a,b) ((a)>(b) ? (a) : (b))
+#endif
 \f
 /* This is a chain of structures for all the X displays currently in use.  */
 struct x_display_info *x_display_list;
@@ -118,32 +144,19 @@ Lisp_Object x_display_name_list;
    is the frame to apply to.  */
 extern struct frame *updating_frame;
 
-/* The frame (if any) which has the X window that has keyboard focus.
-   Zero if none.  This is examined by Ffocus_frame in frame.c.  Note
-   that a mere EnterNotify event can set this; if you need to know the
-   last frame specified in a FocusIn or FocusOut event, use
-   x_focus_event_frame.  */
-struct frame *x_focus_frame;
+extern waiting_for_input;
 
 /* This is a frame waiting to be autoraised, within XTread_socket.  */
 struct frame *pending_autoraise_frame;
 
-/* The last frame mentioned in a FocusIn or FocusOut event.  This is
-   separate from x_focus_frame, because whether or not LeaveNotify
-   events cause us to lose focus depends on whether or not we have
-   received a FocusIn event for it.  */
-struct frame *x_focus_event_frame;
-
-/* The frame which currently has the visual highlight, and should get
-   keyboard input (other sorts of input have the frame encoded in the
-   event).  It points to the X focus frame's selected window's
-   frame.  It differs from x_focus_frame when we're using a global
-   minibuffer.  */
-static struct frame *x_highlight_frame;
-
 #ifdef USE_X_TOOLKIT
 /* The application context for Xt use.  */
 XtAppContext Xt_app_con;
+
+static String Xt_default_resources[] =
+{
+  0
+};
 #endif
 
 /* During an update, maximum vpos for ins/del line operations to affect.  */
@@ -162,6 +175,13 @@ static int curs_y;
 
 /* Mouse movement.
 
+   Formerly, we used PointerMotionHintMask (in STANDARD_EVENT_MASK)
+   so that we would have to call XQueryPointer after each MotionNotify
+   event to ask for another such event.  However, this made mouse tracking
+   slow, and there was a bug that made it eventually stop.
+
+   Simply asking for MotionNotify all the time seems to work better.
+
    In order to avoid asking for motion events and then throwing most
    of them away or busy-polling the server for mouse positions, we ask
    the server for pointer motion hints.  This means that we get only
@@ -171,18 +191,14 @@ static int curs_y;
    get another MotionNotify event the next time the mouse moves.  This
    is at least as efficient as getting motion events when mouse
    tracking is on, and I suspect only negligibly worse when tracking
-   is off.
-
-   The silly O'Reilly & Associates Nutshell guides barely document
-   pointer motion hints at all (I think you have to infer how they
-   work from an example), and the description of XQueryPointer doesn't
-   mention that calling it causes you to get another motion hint from
-   the server, which is very important.  */
+   is off.  */
 
 /* Where the mouse was last time we reported a mouse event.  */
 static FRAME_PTR last_mouse_frame;
 static XRectangle last_mouse_glyph;
 
+static Lisp_Object last_mouse_press_frame;
+
 /* The scroll bar in which the last X motion event occurred.
 
    If the last X motion event occurred in a scroll bar, we set this
@@ -221,9 +237,6 @@ extern Lisp_Object Vcommand_line_args, Vsystem_name;
 
 extern Lisp_Object Vx_no_window_manager;
 
-/* Nonzero enables some debugging for the X interface code. */
-extern int _Xdebug;
-
 extern Lisp_Object Qface, Qmouse_face;
 
 extern int errno;
@@ -231,11 +244,14 @@ extern int errno;
 /* A mask of extra modifier bits to put into every keyboard char.  */
 extern int extra_keyboard_modifiers;
 
+static Lisp_Object Qvendor_specific_keysyms;
+
 extern XrmDatabase x_load_resources ();
 
+extern Lisp_Object x_icon_type ();
+
 void x_delete_display ();
 
-static void flashback ();
 static void redraw_previous_char ();
 static void redraw_following_char ();
 static unsigned int x_x_to_emacs_modifiers ();
@@ -249,6 +265,36 @@ static void do_line_dance ();
 static int XTcursor_to ();
 static int XTclear_end_of_line ();
 static int x_io_error_quitter ();
+void x_catch_errors ();
+void x_uncatch_errors ();
+\f
+#if 0
+/* This is a function useful for recording debugging information
+   about the sequence of occurrences in this file.  */
+
+struct record 
+{
+  char *locus;
+  int type;
+};
+
+struct record event_record[100];
+
+int event_record_index;
+
+record_event (locus, type)
+     char *locus;
+     int type;
+{
+  if (event_record_index == sizeof (event_record) / sizeof (struct record))
+    event_record_index = 0;
+
+  event_record[event_record_index].locus = locus;
+  event_record[event_record_index].type = type;
+  event_record_index++;
+}
+
+#endif /* 0 */
 \f
 /* Return the struct x_display_info corresponding to DPY.  */
 
@@ -288,6 +334,9 @@ XTupdate_begin (f)
 
   BLOCK_INPUT;
 
+  curs_x = FRAME_CURSOR_X (f);
+  curs_y = FRAME_CURSOR_Y (f);
+
   if (f == FRAME_X_DISPLAY_INFO (f)->mouse_face_mouse_frame)
     {
       /* Don't do highlighting for mouse motion during the update.  */
@@ -340,10 +389,9 @@ XTupdate_end (f)
   BLOCK_INPUT;
 
   do_line_dance ();
-  x_display_cursor (f, 1);
+  x_display_cursor (f, 1, curs_x, curs_y);
 
-  if (f == FRAME_X_DISPLAY_INFO (f)->mouse_face_mouse_frame)
-    FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
+  FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
 #if 0
   /* This fails in the case of having updated only the echo area
      if we have switched buffers.  In that case, FRAME_CURRENT_GLYPHS
@@ -366,6 +414,7 @@ static
 XTframe_up_to_date (f)
      FRAME_PTR f;
 {
+  BLOCK_INPUT;
   if (FRAME_X_DISPLAY_INFO (f)->mouse_face_deferred_gc
       || f == FRAME_X_DISPLAY_INFO (f)->mouse_face_mouse_frame)
     {
@@ -374,6 +423,7 @@ XTframe_up_to_date (f)
                            FRAME_X_DISPLAY_INFO (f)->mouse_face_mouse_y);
       FRAME_X_DISPLAY_INFO (f)->mouse_face_deferred_gc = 0;
     }
+  UNBLOCK_INPUT;
 }
 \f
 /* External interface to control of standout mode.
@@ -434,7 +484,7 @@ XTcursor_to (row, col)
   if (updating_frame == 0)
     {
       BLOCK_INPUT;
-      x_display_cursor (selected_frame, 1);
+      x_display_cursor (selected_frame, 1, curs_x, curs_y);
       XFlush (FRAME_X_DISPLAY (selected_frame));
       UNBLOCK_INPUT;
     }
@@ -555,12 +605,12 @@ dumpglyphs (f, left, top, gp, n, hl, just_foreground)
            stippled = 0;
 
            if ((!face->font
-                || (int) face->font == FACE_DEFAULT
-                || face->font == f->display.x->font)
-               && face->background == f->display.x->background_pixel
-               && face->foreground == f->display.x->foreground_pixel)
+                || face->font == (XFontStruct *) FACE_DEFAULT
+                || face->font == f->output_data.x->font)
+               && face->background == f->output_data.x->background_pixel
+               && face->foreground == f->output_data.x->foreground_pixel)
              {
-               gc = f->display.x->cursor_gc;
+               gc = f->output_data.x->cursor_gc;
              }
            /* Cursor on non-default face: must merge.  */
            else
@@ -568,14 +618,14 @@ dumpglyphs (f, left, top, gp, n, hl, just_foreground)
                XGCValues xgcv;
                unsigned long mask;
 
-               xgcv.background = f->display.x->cursor_pixel;
+               xgcv.background = f->output_data.x->cursor_pixel;
                xgcv.foreground = face->background;
                /* If the glyph would be invisible,
                   try a different foreground.  */
                if (xgcv.foreground == xgcv.background)
                  xgcv.foreground = face->foreground;
                if (xgcv.foreground == xgcv.background)
-                 xgcv.foreground = f->display.x->cursor_foreground_pixel;
+                 xgcv.foreground = f->output_data.x->cursor_foreground_pixel;
                if (xgcv.foreground == xgcv.background)
                  xgcv.foreground = face->foreground;
                /* Make sure the cursor is distinct from text in this face.  */
@@ -605,8 +655,8 @@ dumpglyphs (f, left, top, gp, n, hl, just_foreground)
              }
          }
 
-       if ((int) font == FACE_DEFAULT)
-         font = f->display.x->font;
+       if (font == (XFontStruct *) FACE_DEFAULT)
+         font = f->output_data.x->font;
 
        if (just_foreground)
          XDrawString (FRAME_X_DISPLAY (f), window, gc,
@@ -636,13 +686,13 @@ dumpglyphs (f, left, top, gp, n, hl, just_foreground)
                                left, top + FONT_BASE (font), buf, len);
 
            /* Clear the rest of the line's height.  */
-           if (f->display.x->line_height != FONT_HEIGHT (font))
+           if (f->output_data.x->line_height != FONT_HEIGHT (font))
              XClearArea (FRAME_X_DISPLAY (f), window, left,
                          top + FONT_HEIGHT (font),
                          FONT_WIDTH (font) * len,
                          /* This is how many pixels of height
                             we have to clear.  */
-                         f->display.x->line_height - FONT_HEIGHT (font),
+                         f->output_data.x->line_height - FONT_HEIGHT (font),
                          False);
          }
 
@@ -697,9 +747,9 @@ dumpglyphs (f, left, top, gp, n, hl, font)
 {
   register int len;
   Window window = FRAME_X_WINDOW (f);
-  GC drawing_gc =   (hl == 2 ? f->display.x->cursor_gc
-                            : (hl ? f->display.x->reverse_gc
-                                  : f->display.x->normal_gc));
+  GC drawing_gc =   (hl == 2 ? f->output_data.x->cursor_gc
+                            : (hl ? f->output_data.x->reverse_gc
+                                  : f->output_data.x->normal_gc));
 
   if (sizeof (GLYPH) == sizeof (XChar2b))
     XDrawImageString16 (FRAME_X_DISPLAY (f), window, drawing_gc,
@@ -755,11 +805,7 @@ XTwrite_glyphs (start, len)
     f->phys_cursor_x = -1;
 
   if (updating_frame == 0)
-    {
-      f->cursor_x += len;
-      x_display_cursor (f, 1);
-      f->cursor_x -= len;
-    }
+    x_display_cursor (f, 1, FRAME_CURSOR_X (f) + len, FRAME_CURSOR_Y (f));
   else
     curs_x += len;
 
@@ -771,7 +817,7 @@ XTwrite_glyphs (start, len)
    to column FIRST_UNUSED (exclusive).  The idea is that everything
    from FIRST_UNUSED onward is already erased.  */
 
-static int
+static
 XTclear_end_of_line (first_unused)
      register int first_unused;
 {
@@ -802,8 +848,8 @@ XTclear_end_of_line (first_unused)
   XClearArea (FRAME_X_DISPLAY (f), 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, False);
+             FONT_WIDTH (f->output_data.x->font) * (first_unused - curs_x),
+             f->output_data.x->line_height, False);
 #if 0
   redraw_previous_char (f, curs_x, curs_y, highlight);
 #endif
@@ -869,8 +915,8 @@ redraw_previous_char (f, x, y, highlight_flag)
       XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
                  CHAR_TO_PIXEL_COL (f, x - 1),
                  CHAR_TO_PIXEL_ROW (f, y),
-                 FONT_WIDTH (f->display.x->font),
-                 f->display.x->line_height, False);
+                 FONT_WIDTH (f->output_data.x->font),
+                 f->output_data.x->line_height, False);
 
       dumpglyphs (f, CHAR_TO_PIXEL_COL (f, start_x),
                  CHAR_TO_PIXEL_ROW (f, y),
@@ -905,8 +951,8 @@ redraw_following_char (f, x, y, highlight_flag)
       XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
                  CHAR_TO_PIXEL_COL (f, x),
                  CHAR_TO_PIXEL_ROW (f, y),
-                 FONT_WIDTH (f->display.x->font),
-                 f->display.x->line_height, False);
+                 FONT_WIDTH (f->output_data.x->font),
+                 f->output_data.x->line_height, False);
 
       dumpglyphs (f, CHAR_TO_PIXEL_COL (f, x),
                  CHAR_TO_PIXEL_ROW (f, y),
@@ -945,7 +991,7 @@ font_char_overlap_left (font, c)
        {
          /* If char is out of range, try the font's default char instead.  */
          c = font->default_char;
-         row = c >> (INTBITS - 8);
+         row = c >> (BITS_PER_INT - 8);
          within = c & 0177;
        }
       if (!(within >= font->min_char_or_byte2
@@ -990,7 +1036,7 @@ font_char_overlap_right (font, c)
        {
          /* If char is out of range, try the font's default char instead.  */
          c = font->default_char;
-         row = c >> (INTBITS - 8);
+         row = c >> (BITS_PER_INT - 8);
          within = c & 0177;
        }
       if (!(within >= font->min_char_or_byte2
@@ -1061,8 +1107,8 @@ XTflash (f)
       XGCValues values;
 
       values.function = GXxor;
-      values.foreground = (f->display.x->foreground_pixel
-                          ^ f->display.x->background_pixel);
+      values.foreground = (f->output_data.x->foreground_pixel
+                          ^ f->output_data.x->background_pixel);
 
       gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
                      GCFunction | GCForeground, &values);
@@ -1099,7 +1145,7 @@ XTflash (f)
              break;
 
            /* Try to wait that long--but we might wake up sooner.  */
-           select (0, 0, 0, 0, &timeout);
+           select (0, NULL, NULL, NULL, &timeout);
          }
       }
 
@@ -1255,9 +1301,9 @@ do_line_dance ()
     abort ();
 
   ht = f->height;
-  intborder = f->display.x->internal_border_width;
+  intborder = f->output_data.x->internal_border_width;
 
-  x_display_cursor (updating_frame, 0);
+  x_update_cursor (updating_frame, 0);
 
   for (i = 0; i < ht; ++i)
     if (line_dance[i] != -1 && (distance = line_dance[i]-i) > 0)
@@ -1266,10 +1312,10 @@ do_line_dance ()
                     && line_dance[j]-j == distance); ++j);
        /* Copy [i,j) upward from [i+distance,j+distance) */
        XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
-                  FRAME_X_WINDOW (f), f->display.x->normal_gc,
+                  FRAME_X_WINDOW (f), f->output_data.x->normal_gc,
                   intborder, CHAR_TO_PIXEL_ROW (f, i+distance),
-                  f->width * FONT_WIDTH (f->display.x->font),
-                  (j-i) * f->display.x->line_height,
+                  f->width * FONT_WIDTH (f->output_data.x->font),
+                  (j-i) * f->output_data.x->line_height,
                   intborder, CHAR_TO_PIXEL_ROW (f, i));
        i = j-1;
       }
@@ -1281,10 +1327,10 @@ do_line_dance ()
                     && line_dance[j]-j == distance););
        /* Copy (j,i] downward from (j+distance, i+distance] */
        XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
-                  FRAME_X_WINDOW (f), f->display.x->normal_gc,
+                  FRAME_X_WINDOW (f), f->output_data.x->normal_gc,
                   intborder, CHAR_TO_PIXEL_ROW (f, j+1+distance),
-                  f->width * FONT_WIDTH (f->display.x->font),
-                  (i-j) * f->display.x->line_height,
+                  f->width * FONT_WIDTH (f->output_data.x->font),
+                  (i-j) * f->output_data.x->line_height,
                   intborder, CHAR_TO_PIXEL_ROW (f, j+1));
        i = j+1;
       }
@@ -1296,8 +1342,8 @@ do_line_dance ()
        /* Clear [i,j) */
        XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
                    intborder, CHAR_TO_PIXEL_ROW (f, i),
-                   f->width * FONT_WIDTH (f->display.x->font),
-                   (j-i) * f->display.x->line_height, False);
+                   f->width * FONT_WIDTH (f->output_data.x->font),
+                   (j-i) * f->output_data.x->line_height, False);
        i = j-1;
       }
   line_dance_in_progress = 0;
@@ -1333,8 +1379,8 @@ dumprectangle (f, left, top, cols, rows)
      Round down for left and top, up for right and bottom.  */
   top  = PIXEL_TO_CHAR_ROW (f, top);
   left = PIXEL_TO_CHAR_COL (f, left);
-  bottom += (f->display.x->line_height - 1);
-  right += (FONT_WIDTH (f->display.x->font) - 1);
+  bottom += (f->output_data.x->line_height - 1);
+  right += (FONT_WIDTH (f->output_data.x->font) - 1);
   bottom = PIXEL_TO_CHAR_ROW (f, bottom);
   right = PIXEL_TO_CHAR_COL (f, right);
 
@@ -1384,7 +1430,7 @@ dumprectangle (f, left, top, cols, rows)
   /* Turn the cursor on if we turned it off.  */
 
   if (cursor_cleared)
-    x_display_cursor (f, 1);
+    x_update_cursor (f, 1);
 }
 \f
 static void
@@ -1397,9 +1443,9 @@ frame_highlight (f)
      client", so we can always change it to whatever we want.  */
   BLOCK_INPUT;
   XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
-                   f->display.x->border_pixel);
+                   f->output_data.x->border_pixel);
   UNBLOCK_INPUT;
-  x_display_cursor (f, 1);
+  x_update_cursor (f, 1);
 }
 
 static void
@@ -1412,12 +1458,13 @@ frame_unhighlight (f)
      client", so we can always change it to whatever we want.  */
   BLOCK_INPUT;
   XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
-                         f->display.x->border_tile);
+                         f->output_data.x->border_tile);
   UNBLOCK_INPUT;
-  x_display_cursor (f, 1);
+  x_update_cursor (f, 1);
 }
 
 static void XTframe_rehighlight ();
+static void x_frame_rehighlight ();
 
 /* The focus has changed.  Update the frames as necessary to reflect
    the new situation.  Note that we can't change the selected frame
@@ -1426,17 +1473,18 @@ static void XTframe_rehighlight ();
    Lisp code can tell when the switch took place by examining the events.  */
 
 static void
-x_new_focus_frame (frame)
+x_new_focus_frame (dpyinfo, frame)
+     struct x_display_info *dpyinfo;
      struct frame *frame;
 {
-  struct frame *old_focus = x_focus_frame;
+  struct frame *old_focus = dpyinfo->x_focus_frame;
   int events_enqueued = 0;
 
-  if (frame != x_focus_frame)
+  if (frame != dpyinfo->x_focus_frame)
     {
       /* Set this before calling other routines, so that they see
         the correct value of x_focus_frame.  */
-      x_focus_frame = frame;
+      dpyinfo->x_focus_frame = frame;
 
       if (old_focus && old_focus->auto_lower)
        x_lower_frame (old_focus);
@@ -1449,55 +1497,65 @@ x_new_focus_frame (frame)
       choose_minibuf_frame ();
 #endif /* ! 0 */
 
-      if (x_focus_frame && x_focus_frame->auto_raise)
-       pending_autoraise_frame = x_focus_frame;
+      if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
+       pending_autoraise_frame = dpyinfo->x_focus_frame;
       else
        pending_autoraise_frame = 0;
     }
 
-  XTframe_rehighlight ();
+  x_frame_rehighlight (dpyinfo);
 }
 
 /* Handle an event saying the mouse has moved out of an Emacs frame.  */
 
 void
-x_mouse_leave ()
+x_mouse_leave (dpyinfo)
+     struct x_display_info *dpyinfo;
 {
-  if (! x_focus_event_frame)
-    x_new_focus_frame (NULL);
-  else
-    x_new_focus_frame (x_focus_event_frame);  /* Was f, but that seems wrong.  */
+  x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
 }
 
 /* The focus has changed, or we have redirected a frame's focus to
    another frame (this happens when a frame uses a surrogate
-   minibuffer frame).  Shift the highlight as appropriate.  */
+   minibuffer frame).  Shift the highlight as appropriate.
+
+   The FRAME argument doesn't necessarily have anything to do with which
+   frame is being highlighted or unhighlighted; we only use it to find
+   the appropriate X display info.  */
 static void
-XTframe_rehighlight ()
+XTframe_rehighlight (frame)
+     struct frame *frame;
 {
-  struct frame *old_highlight = x_highlight_frame;
+  x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
+}
+
+static void
+x_frame_rehighlight (dpyinfo)
+     struct x_display_info *dpyinfo;
+{
+  struct frame *old_highlight = dpyinfo->x_highlight_frame;
 
-  if (x_focus_frame)
+  if (dpyinfo->x_focus_frame)
     {
-      x_highlight_frame
-       = ((GC_FRAMEP (FRAME_FOCUS_FRAME (x_focus_frame)))
-          ? XFRAME (FRAME_FOCUS_FRAME (x_focus_frame))
-          : x_focus_frame);
-      if (! FRAME_LIVE_P (x_highlight_frame))
+      dpyinfo->x_highlight_frame
+       = ((GC_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))
        {
-         FRAME_FOCUS_FRAME (x_focus_frame) = Qnil;
-         x_highlight_frame = x_focus_frame;
+         FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
+         dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
        }
     }
   else
-    x_highlight_frame = 0;
+    dpyinfo->x_highlight_frame = 0;
 
-  if (x_highlight_frame != old_highlight)
+  if (dpyinfo->x_highlight_frame != old_highlight)
     {
       if (old_highlight)
        frame_unhighlight (old_highlight);
-      if (x_highlight_frame)
-       frame_highlight (x_highlight_frame);
+      if (dpyinfo->x_highlight_frame)
+       frame_highlight (dpyinfo->x_highlight_frame);
     }
 }
 \f
@@ -1522,8 +1580,8 @@ x_find_modifier_meanings (dpyinfo)
 #ifdef HAVE_X11R4
   XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
 #else
-  min_code = display->min_keycode;
-  max_code = display->max_keycode;
+  min_code = dpyinfo->display->min_keycode;
+  max_code = dpyinfo->display->max_keycode;
 #endif
 
   syms = XGetKeyboardMapping (dpyinfo->display,
@@ -1632,6 +1690,21 @@ x_emacs_to_x_modifiers (dpyinfo, state)
          | ((state & ctrl_modifier)    ? ControlMask      : 0)
          | ((state & meta_modifier)    ? dpyinfo->meta_mod_mask  : 0));
 }
+
+/* Convert a keysym to its name.  */
+
+char *
+x_get_keysym_name (keysym)
+     KeySym keysym;
+{
+  char *value;
+
+  BLOCK_INPUT;
+  value = XKeysymToString (keysym);
+  UNBLOCK_INPUT;
+
+  return value;
+}
 \f
 /* Mouse clicks and mouse movement.  Rah.  */
 
@@ -1651,17 +1724,17 @@ pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
   /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
      even for negative values.  */
   if (pix_x < 0)
-    pix_x -= FONT_WIDTH ((f)->display.x->font) - 1;
+    pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
   if (pix_y < 0)
-    pix_y -= (f)->display.x->line_height - 1;
+    pix_y -= (f)->output_data.x->line_height - 1;
 
   pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
   pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
 
   if (bounds)
     {
-      bounds->width  = FONT_WIDTH  (f->display.x->font);
-      bounds->height = f->display.x->line_height;
+      bounds->width  = FONT_WIDTH  (f->output_data.x->font);
+      bounds->height = f->output_data.x->line_height;
       bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
       bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
     }
@@ -1765,27 +1838,15 @@ static void
 note_mouse_movement (frame, event)
      FRAME_PTR frame;
      XMotionEvent *event;
-
 {
   last_mouse_movement_time = event->time;
 
   if (event->window != FRAME_X_WINDOW (frame))
     {
-      mouse_moved = 1;
+      frame->mouse_moved = 1;
       last_mouse_scroll_bar = Qnil;
 
       note_mouse_highlight (frame, -1, -1);
-
-      /* Ask for another mouse motion event.  */
-      {
-       int dummy;
-       Window dummy_window;
-
-       XQueryPointer (event->display, FRAME_X_WINDOW (frame),
-                      &dummy_window, &dummy_window,
-                      &dummy, &dummy, &dummy, &dummy,
-                      (unsigned int *) &dummy);
-      }
     }
 
   /* Has the mouse moved off the glyph it was on at the last sighting?  */
@@ -1794,34 +1855,10 @@ note_mouse_movement (frame, event)
           || event->y < last_mouse_glyph.y
           || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
     {
-      mouse_moved = 1;
+      frame->mouse_moved = 1;
       last_mouse_scroll_bar = Qnil;
 
       note_mouse_highlight (frame, event->x, event->y);
-
-      /* Ask for another mouse motion event.  */
-      {
-       int dummy;
-       Window dummy_window;
-
-       XQueryPointer (event->display, FRAME_X_WINDOW (frame),
-                      &dummy_window, &dummy_window,
-                      &dummy, &dummy, &dummy, &dummy,
-                      (unsigned int *) &dummy);
-      }
-    }
-  else
-    {
-      /* It's on the same glyph.  Call XQueryPointer so we'll get an
-        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),
-                    &dummy_window, &dummy_window,
-                    &dummy, &dummy, &dummy, &dummy,
-                    (unsigned int *) &dummy);
     }
 }
 
@@ -1836,6 +1873,7 @@ static int disable_mouse_highlight;
 static void
 note_mouse_highlight (f, x, y)
      FRAME_PTR f;
+     int x, y;
 {
   int row, column, portion;
   XRectangle new_glyph;
@@ -1875,7 +1913,8 @@ note_mouse_highlight (f, x, y)
   if (WINDOWP (window) && portion == 0 && row >= 0 && column >= 0
       && row < FRAME_HEIGHT (f) && column < FRAME_WIDTH (f)
       && EQ (w->window_end_valid, w->buffer)
-      && w->last_modified == BUF_MODIFF (XBUFFER (w->buffer)))
+      && w->last_modified == BUF_MODIFF (XBUFFER (w->buffer))
+      && w->last_overlay_modified == BUF_OVERLAY_MODIFF (XBUFFER (w->buffer)))
     {
       int *ptr = FRAME_CURRENT_GLYPHS (f)->charstarts[row];
       int i, pos;
@@ -1958,11 +1997,11 @@ note_mouse_highlight (f, x, y)
              before = Foverlay_start (overlay);
              after = Foverlay_end (overlay);
              /* Record this as the current active region.  */
-             fast_find_position (window, before,
+             fast_find_position (window, XFASTINT (before),
                                  &FRAME_X_DISPLAY_INFO (f)->mouse_face_beg_col,
                                  &FRAME_X_DISPLAY_INFO (f)->mouse_face_beg_row);
              FRAME_X_DISPLAY_INFO (f)->mouse_face_past_end
-               = !fast_find_position (window, after,
+               = !fast_find_position (window, XFASTINT (after),
                                       &FRAME_X_DISPLAY_INFO (f)->mouse_face_end_col,
                                       &FRAME_X_DISPLAY_INFO (f)->mouse_face_end_row);
              FRAME_X_DISPLAY_INFO (f)->mouse_face_window = window;
@@ -1992,11 +2031,11 @@ note_mouse_highlight (f, x, y)
                = Fnext_single_property_change (position, Qmouse_face,
                                                w->buffer, end);
              /* Record this as the current active region.  */
-             fast_find_position (window, before,
+             fast_find_position (window, XFASTINT (before),
                                  &FRAME_X_DISPLAY_INFO (f)->mouse_face_beg_col,
                                  &FRAME_X_DISPLAY_INFO (f)->mouse_face_beg_row);
              FRAME_X_DISPLAY_INFO (f)->mouse_face_past_end
-               = !fast_find_position (window, after,
+               = !fast_find_position (window, XFASTINT (after),
                                       &FRAME_X_DISPLAY_INFO (f)->mouse_face_end_col,
                                       &FRAME_X_DISPLAY_INFO (f)->mouse_face_end_row);
              FRAME_X_DISPLAY_INFO (f)->mouse_face_window = window;
@@ -2083,7 +2122,7 @@ fast_find_position (window, pos, columnp, rowp)
   if (maybe_next_line)
     {
       row++;
-      i = 0;
+      lastcol = left;
     }
 
   *rowp = row + top;
@@ -2104,14 +2143,6 @@ show_mouse_face (dpyinfo, hl)
   FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
   int i;
   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 = FRAME_X_DISPLAY_INFO (f)->mouse_face_beg_row;
        i <= FRAME_X_DISPLAY_INFO (f)->mouse_face_end_row; i++)
@@ -2127,10 +2158,10 @@ show_mouse_face (dpyinfo, hl)
       /* If the cursor's in the text we are about to rewrite,
         turn the cursor off.  */
       if (i == curs_y
-         && curs_x >= FRAME_X_DISPLAY_INFO (f)->mouse_face_beg_col - 1
-         && curs_x <= FRAME_X_DISPLAY_INFO (f)->mouse_face_end_col)
+         && curs_x >= column - 1
+         && curs_x <= endcolumn)
        {
-         x_display_cursor (f, 0);
+         x_update_cursor (f, 0);
          cursor_off = 1;
        }
 
@@ -2145,18 +2176,15 @@ show_mouse_face (dpyinfo, hl)
 
   /* If we turned the cursor off, turn it back on.  */
   if (cursor_off)
-    x_display_cursor (f, 1);
-
-  curs_x = old_curs_x;
-  curs_y = old_curs_y;
+    x_update_cursor (f, 1);
 
   /* Change the mouse cursor according to the value of HL.  */
   if (hl > 0)
     XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
-                  f->display.x->cross_cursor);
+                  f->output_data.x->cross_cursor);
   else
     XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
-                  f->display.x->text_cursor);
+                  f->output_data.x->text_cursor);
 }
 
 /* Clear out the mouse-highlighted active region.
@@ -2178,13 +2206,14 @@ static struct scroll_bar *x_window_to_scroll_bar ();
 static void x_scroll_bar_report_motion ();
 
 /* Return the current position of the mouse.
+   *fp should be a frame which indicates which display to ask about.
 
-   If the mouse movement started in a scroll bar, set *f, *bar_window,
+   If the mouse movement started in a scroll bar, set *fp, *bar_window,
    and *part to the frame, window, and scroll bar part that the mouse
    is over.  Set *x and *y to the portion and whole of the mouse's
    position on the scroll bar.
 
-   If the mouse movement started elsewhere, set *f to the frame the
+   If the mouse movement started elsewhere, set *fp to the frame the
    mouse is on, *bar_window to nil, and *x and *y to the character cell
    the mouse is over.
 
@@ -2194,9 +2223,7 @@ static void x_scroll_bar_report_motion ();
    Don't store anything if we don't have a valid set of values to report.
 
    This clears the mouse_moved flag, so we can wait for the next mouse
-   movement.  This also calls XQueryPointer, which will cause the
-   server to give us another MotionNotify when the mouse moves
-   again. */
+   movement.  */
 
 static void
 XTmouse_position (fp, insist, bar_window, part, x, y, time)
@@ -2221,7 +2248,13 @@ XTmouse_position (fp, insist, bar_window, part, x, y, time)
       Window dummy_window;
       int dummy;
 
-      mouse_moved = 0;
+      Lisp_Object frame, tail;
+
+      /* Clear the mouse-moved flag for every frame on this display.  */
+      FOR_EACH_FRAME (tail, frame)
+       if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
+         XFRAME (frame)->mouse_moved = 0;
+
       last_mouse_scroll_bar = Qnil;
 
       /* Figure out which root window we're on.  */
@@ -2254,6 +2287,12 @@ XTmouse_position (fp, insist, bar_window, part, x, y, time)
 
        win = root;
 
+       /* XTranslateCoordinates can get errors if the window
+          structure is changing at the same time this function
+          is running.  So at least we must not crash from them.  */
+
+       x_catch_errors (FRAME_X_DISPLAY (*fp));
+
        if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
            && FRAME_LIVE_P (last_mouse_frame))
          {
@@ -2286,7 +2325,7 @@ XTmouse_position (fp, insist, bar_window, part, x, y, time)
                                       /* Child of win.  */
                                       &child);
 
-               if (child == None)
+               if (child == None || child == win)
                  break;
 
                win = child;
@@ -2306,9 +2345,14 @@ XTmouse_position (fp, insist, bar_window, part, x, y, time)
               never use them in that case.)  */
 
            /* Is win one of our frames?  */
-           f1 = x_any_window_to_frame (win);
+           f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
          }
 
+       if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
+         f1 = 0;
+
+       x_uncatch_errors (FRAME_X_DISPLAY (*fp));
+
        /* If not, is it one of our scroll bars?  */
        if (! f1)
          {
@@ -2404,7 +2448,7 @@ x_scroll_bar_create (window, top, left, width, height)
   {
     XSetWindowAttributes a;
     unsigned long mask;
-    a.background_pixel = f->display.x->background_pixel;
+    a.background_pixel = f->output_data.x->background_pixel;
     a.event_mask = (ButtonPressMask | ButtonReleaseMask
                    | ButtonMotionMask | PointerMotionHintMask
                    | ExposureMask);
@@ -2422,7 +2466,7 @@ x_scroll_bar_create (window, top, left, width, height)
     XtSetArg (al[ac], XtNborderWidth, 0); ac++;
     sb_widget = XtCreateManagedWidget ("box",
                                       boxWidgetClass,
-                                      f->display.x->edit_widget, al, ac);
+                                      f->output_data.x->edit_widget, al, ac);
     SET_SCROLL_BAR_X_WINDOW
       (bar, sb_widget->core.window);
 #endif
@@ -2483,7 +2527,7 @@ x_scroll_bar_set_handle (bar, start, end, rebuild)
   int dragging = ! NILP (bar->dragging);
   Window w = SCROLL_BAR_X_WINDOW (bar);
   FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
-  GC gc = f->display.x->normal_gc;
+  GC gc = f->output_data.x->normal_gc;
 
   /* If the display is already accurate, do nothing.  */
   if (! rebuild
@@ -2642,7 +2686,7 @@ XTset_vertical_scroll_bar (window, portion, whole, position)
   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)));
+       : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
   int pixel_height = VERTICAL_SCROLL_BAR_PIXEL_HEIGHT (f, height);
 
   struct scroll_bar *bar;
@@ -2695,15 +2739,18 @@ static void
 XTcondemn_scroll_bars (frame)
      FRAME_PTR frame;
 {
-  /* The condemned list should be empty at this point; if it's not,
-     then the rest of Emacs isn't using the condemn/redeem/judge
-     protocol correctly.  */
-  if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
-    abort ();
-
-  /* Move them all to the "condemned" list.  */
-  FRAME_CONDEMNED_SCROLL_BARS (frame) = FRAME_SCROLL_BARS (frame);
-  FRAME_SCROLL_BARS (frame) = Qnil;
+  /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS.  */
+  while (! NILP (FRAME_SCROLL_BARS (frame)))
+    {
+      Lisp_Object bar;
+      bar = FRAME_SCROLL_BARS (frame);
+      FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
+      XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
+      XSCROLL_BAR (bar)->prev = Qnil;
+      if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
+       XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
+      FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
+    }
 }
 
 /* Unmark WINDOW's scroll bar for deletion in this judgement cycle.
@@ -2793,7 +2840,7 @@ x_scroll_bar_expose (bar, event)
 {
   Window w = SCROLL_BAR_X_WINDOW (bar);
   FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
-  GC gc = f->display.x->normal_gc;
+  GC gc = f->output_data.x->normal_gc;
 
   BLOCK_INPUT;
 
@@ -2897,9 +2944,11 @@ x_scroll_bar_note_movement (bar, event)
      struct scroll_bar *bar;
      XEvent *event;
 {
+  FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
+
   last_mouse_movement_time = event->xmotion.time;
 
-  mouse_moved = 1;
+  f->mouse_moved = 1;
   XSETVECTOR (last_mouse_scroll_bar, bar);
 
   /* If we're dragging the bar, display it.  */
@@ -2915,18 +2964,6 @@ x_scroll_bar_note_movement (bar, event)
          x_scroll_bar_set_handle (bar, new_start, new_end, 0);
        }
     }
-
-  /* Call XQueryPointer so we'll get an event the next time the mouse
-     moves and we can see *still* on the same position.  */
-  {
-    int dummy;
-    Window dummy_window;
-
-    XQueryPointer (event->xmotion.display, event->xmotion.window,
-                  &dummy_window, &dummy_window,
-                  &dummy, &dummy, &dummy, &dummy,
-                  (unsigned int *) &dummy);
-  }
 }
 
 /* Return information to the user about the current position of the mouse
@@ -2995,7 +3032,7 @@ x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
       XSETINT (*x, win_y);
       XSETINT (*y, top_range);
 
-      mouse_moved = 0;
+      f->mouse_moved = 0;
       last_mouse_scroll_bar = Qnil;
     }
 
@@ -3015,14 +3052,18 @@ x_scroll_bar_clear (f)
 {
   Lisp_Object bar;
 
-  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)),
-               0, 0, 0, 0, True);
+  /* We can have scroll bars even if this is 0,
+     if we just turned off scroll bar mode.
+     But in that case we should not clear them.  */
+  if (FRAME_HAS_VERTICAL_SCROLL_BARS (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)),
+                 0, 0, 0, 0, True);
 }
 
 /* This processes Expose events from the menubar specific X event
-   loop in menubar.c.  This allows to redisplay the frame if necessary
+   loop in xmenu.c.  This allows to redisplay the frame if necessary
    when handling menubar or popup items.  */
 
 void
@@ -3030,10 +3071,12 @@ process_expose_from_menu (event)
      XEvent event;
 {
   FRAME_PTR f;
+  struct x_display_info *dpyinfo;
 
   BLOCK_INPUT;
 
-  f = x_window_to_frame (event.xexpose.window);
+  dpyinfo = x_display_info_for_display (event.xexpose.display);
+  f = x_window_to_frame (dpyinfo, event.xexpose.window);
   if (f)
     {
       if (f->async_visible == 0)
@@ -3044,7 +3087,7 @@ process_expose_from_menu (event)
        }
       else
        {
-         dumprectangle (x_window_to_frame (event.xexpose.window),
+         dumprectangle (x_window_to_frame (dpyinfo, event.xexpose.window),
                         event.xexpose.x, event.xexpose.y,
                         event.xexpose.width, event.xexpose.height);
        }
@@ -3145,13 +3188,35 @@ static XComposeStatus compose_status;
 
 /* Record the last 100 characters stored
    to help debug the loss-of-chars-during-GC problem.  */
-int temp_index;
-short temp_buffer[100];
+static int temp_index;
+static short temp_buffer[100];
 
 /* Set this to nonzero to fake an "X I/O error"
    on a particular display.  */
 struct x_display_info *XTread_socket_fake_io_error;
 
+/* When we find no input here, we occasionally do a no-op command
+   to verify that the X server is still running and we can still talk with it.
+   We try all the open displays, one by one.
+   This variable is used for cycling thru the displays.  */
+static struct x_display_info *next_noop_dpyinfo;
+
+#define SET_SAVED_MENU_EVENT(size) { \
+  if (f->output_data.x->saved_menu_event == 0) \
+    f->output_data.x->saved_menu_event = (XEvent*)xmalloc (sizeof (XEvent)); \
+  bcopy (&event, f->output_data.x->saved_menu_event, size); \
+  if (numchars >= 1) \
+    { \
+      bufp->kind = menu_bar_activate_event; \
+      XSETFRAME (bufp->frame_or_window, f); \
+      bufp++; \
+      count++; \
+      numchars--; \
+    } \
+  }
+#define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
+#define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
+
 /* Read events coming from the X server.
    This routine is called by the SIGIO handler.
    We return as soon as there are no more events to be read.
@@ -3167,8 +3232,8 @@ struct x_display_info *XTread_socket_fake_io_error;
 int
 XTread_socket (sd, bufp, numchars, waitp, expected)
      register int sd;
-     register struct input_event *bufp;
-     register int numchars;
+     /* register */ struct input_event *bufp;
+     /* register */ int numchars;
      int waitp;
      int expected;
 {
@@ -3182,6 +3247,9 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
   int prefix;
   Lisp_Object part;
   struct x_display_info *dpyinfo;
+#ifdef HAVE_X_I18N
+  Status status_return;
+#endif
 
   if (interrupt_input_blocked)
     {
@@ -3206,7 +3274,7 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
 #ifdef FIOSNBIO
       /* If available, Xlib uses FIOSNBIO to make the socket
         non-blocking, and then looks for EWOULDBLOCK.  If O_NDELAY is set,
-        FIOSNBIO is ignored, and instead of signalling EWOULDBLOCK,
+        FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
         a read returns 0, which Xlib interprets as equivalent to EPIPE. */
       fcntl (dpyinfo->connection, F_SETFL, 0);
 #endif /* ! defined (FIOSNBIO) */
@@ -3238,7 +3306,22 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
 
       while (XPending (dpyinfo->display) != 0)
        {
+#ifdef USE_X_TOOLKIT
+          /* needed to raise Motif submenus */
+         XtAppNextEvent (Xt_app_con, &event);
+#else
          XNextEvent (dpyinfo->display, &event);
+#endif
+#ifdef HAVE_X_I18N
+         {
+           struct frame *f1 = x_any_window_to_frame (dpyinfo,
+                                                     &event.xclient.window);
+           /* The necessity of the following line took me
+              a full work-day to decipher from the docs!!  */
+           if (f1 != 0 && FRAME_XIC (f1) && XFilterEvent (&event, None))
+             break;
+         }
+#endif
          event_found = 1;
 
          switch (event.type)
@@ -3252,10 +3335,15 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
                    if (event.xclient.data.l[0]
                        == dpyinfo->Xatom_wm_take_focus)
                      {
-                       f = x_window_to_frame (event.xclient.window);
+                       f = x_window_to_frame (dpyinfo, event.xclient.window);
                        /* Since we set WM_TAKE_FOCUS, we must call
                           XSetInputFocus explicitly.  But not if f is null,
                           since that might be an event for a deleted frame.  */
+#ifdef HAVE_X_I18N
+                       /* Not quite sure this is needed -pd */
+                       if (f)
+                         XSetICFocus (FRAME_XIC (f));
+#endif
                        if (f)
                          XSetInputFocus (event.xclient.display,
                                          event.xclient.window,
@@ -3273,7 +3361,8 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
                           a keyboard or mouse event arrives. */
                        if (numchars > 0)
                          {
-                           f = x_top_window_to_frame (event.xclient.window);
+                           f = x_top_window_to_frame (dpyinfo,
+                                                      event.xclient.window);
 
                            /* This is just so we only give real data once
                               for a single Emacs process.  */
@@ -3281,7 +3370,7 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
                              XSetCommand (FRAME_X_DISPLAY (f),
                                           event.xclient.window,
                                           initial_argv, initial_argc);
-                           else
+                           else if (f)
                              XSetCommand (FRAME_X_DISPLAY (f),
                                           event.xclient.window,
                                           0, 0);
@@ -3290,7 +3379,9 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
                    else if (event.xclient.data.l[0]
                             == dpyinfo->Xatom_wm_delete_window)
                      {
-                       struct frame *f = x_any_window_to_frame (event.xclient.window);
+                       struct frame *f
+                         = x_any_window_to_frame (dpyinfo,
+                                                  event.xclient.window);
 
                        if (f)
                          {
@@ -3314,31 +3405,34 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
                         == dpyinfo->Xatom_wm_window_moved)
                  {
                    int new_x, new_y;
-                   struct frame *f = x_window_to_frame (event.xclient.window);
+                   struct frame *f
+                     = x_window_to_frame (dpyinfo, event.xclient.window);
 
                    new_x = event.xclient.data.s[0];
                    new_y = event.xclient.data.s[1];
 
                    if (f)
                      {
-                       f->display.x->left_pos = new_x;
-                       f->display.x->top_pos = new_y;
+                       f->output_data.x->left_pos = new_x;
+                       f->output_data.x->top_pos = new_y;
                      }
                  }
-#if defined (USE_X_TOOLKIT) && defined (HAVE_X11R5)
+#ifdef HACK_EDITRES
                else if (event.xclient.message_type
                         == dpyinfo->Xatom_editres)
                  {
-                   struct frame *f = x_any_window_to_frame (event.xclient.window);
-                   _XEditResCheckMessages (f->display.x->widget, NULL, &event, NULL);
+                   struct frame *f
+                     = x_any_window_to_frame (dpyinfo, event.xclient.window);
+                   _XEditResCheckMessages (f->output_data.x->widget, NULL,
+                                           &event, NULL);
                  }
-#endif /* USE_X_TOOLKIT and HAVE_X11R5 */
+#endif /* HACK_EDITRES */
              }
              break;
 
            case SelectionNotify:
 #ifdef USE_X_TOOLKIT
-             if (! x_window_to_frame (event.xselection.requestor))
+             if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
                goto OTHER;
 #endif /* not USE_X_TOOLKIT */
              x_handle_selection_notify (&event);
@@ -3346,7 +3440,7 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
 
            case SelectionClear:        /* Someone has grabbed ownership. */
 #ifdef USE_X_TOOLKIT
-             if (! x_window_to_frame (event.xselectionclear.window))
+             if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
                goto OTHER;
 #endif /* USE_X_TOOLKIT */
              {
@@ -3359,6 +3453,7 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
                SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
                SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
                SELECTION_EVENT_TIME (bufp) = eventp->time;
+               bufp->frame_or_window = Qnil;
                bufp++;
 
                count += 1;
@@ -3368,11 +3463,11 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
 
            case SelectionRequest:      /* Someone wants our selection. */
 #ifdef USE_X_TOOLKIT
-             if (!x_window_to_frame (event.xselectionrequest.owner))
+             if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
                goto OTHER;
 #endif /* USE_X_TOOLKIT */
              if (x_queue_selection_requests)
-               x_queue_event (x_window_to_frame (event.xselectionrequest.owner),
+               x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
                               &event);
              else
                {
@@ -3388,6 +3483,7 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
                  SELECTION_EVENT_TARGET (bufp) = eventp->target;
                  SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
                  SELECTION_EVENT_TIME (bufp) = eventp->time;
+                 bufp->frame_or_window = Qnil;
                  bufp++;
 
                  count += 1;
@@ -3397,26 +3493,26 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
 
            case PropertyNotify:
 #ifdef USE_X_TOOLKIT
-             if (!x_any_window_to_frame (event.xproperty.window))
+             if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
                goto OTHER;
 #endif /* not USE_X_TOOLKIT */
              x_handle_property_notify (&event);
              break;
 
            case ReparentNotify:
-             f = x_top_window_to_frame (event.xreparent.window);
+             f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
              if (f)
                {
                  int x, y;
-                 f->display.x->parent_desc = event.xreparent.parent;
+                 f->output_data.x->parent_desc = event.xreparent.parent;
                  x_real_positions (f, &x, &y);
-                 f->display.x->left_pos = x;
-                 f->display.x->top_pos = y;
+                 f->output_data.x->left_pos = x;
+                 f->output_data.x->top_pos = y;
                }
              break;
 
            case Expose:
-             f = x_window_to_frame (event.xexpose.window);
+             f = x_window_to_frame (dpyinfo, event.xexpose.window);
              if (f)
                {
                  if (f->async_visible == 0)
@@ -3426,7 +3522,8 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
                      SET_FRAME_GARBAGED (f);
                    }
                  else
-                   dumprectangle (x_window_to_frame (event.xexpose.window),
+                   dumprectangle (x_window_to_frame (dpyinfo,
+                                                     event.xexpose.window),
                                   event.xexpose.x, event.xexpose.y,
                                   event.xexpose.width, event.xexpose.height);
                }
@@ -3447,7 +3544,7 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
            case GraphicsExpose:        /* This occurs when an XCopyArea's
                                      source area was obscured or not
                                      available.*/
-             f = x_window_to_frame (event.xgraphicsexpose.drawable);
+             f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
              if (f)
                {
                  dumprectangle (f,
@@ -3467,7 +3564,7 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
              break;
 
            case UnmapNotify:
-             f = x_any_window_to_frame (event.xunmap.window);
+             f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
              if (f)            /* F may no longer exist if
                                       the frame was deleted.  */
                {
@@ -3478,7 +3575,7 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
                  /* We can't distinguish, from the event, whether the window
                     has become iconified or invisible.  So assume, if it
                     was previously visible, than now it is iconified.
-                    We depend on x_make_frame_invisible to mark it iconified.  */
+                    We depend on x_make_frame_invisible to mark it invisible.  */
                  if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
                    f->async_iconified = 1;
 
@@ -3488,15 +3585,12 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
                  count++;
                  numchars--;
                }
-#ifdef USE_X_TOOLKIT
              goto OTHER;
-#endif /* USE_X_TOOLKIT */
-             break;
 
            case MapNotify:
              /* We use x_top_window_to_frame because map events can come
                 for subwindows and they don't mean that the frame is visible.  */
-             f = x_top_window_to_frame (event.xmap.window);
+             f = x_top_window_to_frame (dpyinfo, event.xmap.window);
              if (f)
                {
                  f->async_visible = 1;
@@ -3506,23 +3600,29 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
                     the frame's display structures.  */
                  SET_FRAME_GARBAGED (f);
 
-                 bufp->kind = deiconify_event;
-                 XSETFRAME (bufp->frame_or_window, f);
-                 bufp++;
-                 count++;
-                 numchars--;
+                 if (f->iconified)
+                   {
+                     bufp->kind = deiconify_event;
+                     XSETFRAME (bufp->frame_or_window, f);
+                     bufp++;
+                     count++;
+                     numchars--;
+                   }
+                 else if (! NILP(Vframe_list)
+                          && ! NILP (XCONS (Vframe_list)->cdr))
+                   /* Force a redisplay sooner or later
+                      to update the frame titles
+                      in case this is the second frame.  */
+                   record_asynch_buffer_change ();
                }
-#ifdef USE_X_TOOLKIT
              goto OTHER;
-#endif /* USE_X_TOOLKIT */
-             break;
 
              /* Turn off processing if we become fully obscured. */
            case VisibilityNotify:
              break;
 
            case KeyPress:
-             f = x_any_window_to_frame (event.xkey.window);
+             f = x_any_window_to_frame (dpyinfo, event.xkey.window);
 
              if (f != 0)
                {
@@ -3532,6 +3632,21 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
                  unsigned char copy_buffer[81];
                  int modifiers;
 
+#if 0 /* This was how we made f10 work in Motif.
+        The drawback is, you can't type at Emacs when the
+        the mouse is in the menu bar.  So it is better to
+        turn off f10 in Motif and let Emacs handle it.  */
+#ifdef USE_MOTIF
+                  if (lw_window_is_in_menubar (event.xkey.window,
+                                               f->output_data.x->menubar_widget
+                                               ))
+                    {
+                      SET_SAVED_KEY_EVENT;
+                      break;
+                    }
+#endif /* USE_MOTIF */
+#endif /* 0 */
+
                  event.xkey.state
                    |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
                                               extra_keyboard_modifiers);
@@ -3542,8 +3657,26 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
                  /* make_lispy_event turns chars into control chars.
                     Don't do it here because XLookupString is too eager.  */
                  event.xkey.state &= ~ControlMask;
+                 event.xkey.state &= ~(dpyinfo->meta_mod_mask
+                                       | dpyinfo->super_mod_mask
+                                       | dpyinfo->hyper_mod_mask
+                                       | dpyinfo->alt_mod_mask);
+
+#ifdef HAVE_X_I18N
+                 if (FRAME_XIC (f))
+                   {
+                     nbytes = XmbLookupString (FRAME_XIC (f),
+                                               &event.xkey, copy_buffer,
+                                               80, &keysym,
+                                               &status_return);
+                   }
+                 else
+                   nbytes = XLookupString (&event.xkey, copy_buffer,
+                                           80, &keysym, &compose_status);
+#else
                  nbytes = XLookupString (&event.xkey, copy_buffer,
                                          80, &keysym, &compose_status);
+#endif
 
                  orig_keysym = keysym;
 
@@ -3657,7 +3790,7 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
                  else
                    abort ();
                }
-             break;
+             goto OTHER;
 
              /* Here's a possible interpretation of the whole
                 FocusIn-EnterNotify FocusOut-LeaveNotify mess.  If you get a
@@ -3666,7 +3799,7 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
                 then a mere LeaveNotify is enough to free you.  */
 
            case EnterNotify:
-             f = x_any_window_to_frame (event.xcrossing.window);
+             f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
 
              if (event.xcrossing.focus)                /* Entered Window */
                {
@@ -3675,35 +3808,34 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
                            || !(f->auto_lower)
                            || (event.xcrossing.time - enter_timestamp) > 500))
                    {
-                     x_new_focus_frame (f);
+                     x_new_focus_frame (dpyinfo, f);
                      enter_timestamp = event.xcrossing.time;
                    }
                }
-             else if (f == x_focus_frame)
-               x_new_focus_frame (0);
+             else if (f == dpyinfo->x_focus_frame)
+               x_new_focus_frame (dpyinfo, 0);
              /* EnterNotify counts as mouse movement,
                 so update things that depend on mouse position.  */
              if (f)
                note_mouse_movement (f, &event.xmotion);
-#ifdef USE_X_TOOLKIT
              goto OTHER;
-#endif /* USE_X_TOOLKIT */
-             break;
 
            case FocusIn:
-             f = x_any_window_to_frame (event.xfocus.window);
+             f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
              if (event.xfocus.detail != NotifyPointer)
-               x_focus_event_frame = f;
+               dpyinfo->x_focus_event_frame = f;
              if (f)
-               x_new_focus_frame (f);
-#ifdef USE_X_TOOLKIT
-             goto OTHER;
-#endif /* USE_X_TOOLKIT */
-             break;
+               x_new_focus_frame (dpyinfo, f);
 
+#ifdef HAVE_X_I18N
+             if (f && FRAME_XIC (f))
+               XSetICFocus (FRAME_XIC (f));
+#endif
+
+             goto OTHER;
 
            case LeaveNotify:
-             f = x_top_window_to_frame (event.xcrossing.window);
+             f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
              if (f)
                {
                  if (f == dpyinfo->mouse_face_mouse_frame)
@@ -3712,31 +3844,31 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
                    clear_mouse_face (dpyinfo);
 
                  if (event.xcrossing.focus)
-                   x_mouse_leave ();
+                   x_mouse_leave (dpyinfo);
                  else
                    {
-                     if (f == x_focus_event_frame)
-                       x_focus_event_frame = 0;
-                     if (f == x_focus_frame)
-                       x_new_focus_frame (0);
+                     if (f == dpyinfo->x_focus_event_frame)
+                       dpyinfo->x_focus_event_frame = 0;
+                     if (f == dpyinfo->x_focus_frame)
+                       x_new_focus_frame (dpyinfo, 0);
                    }
                }
-#ifdef USE_X_TOOLKIT
              goto OTHER;
-#endif /* USE_X_TOOLKIT */
-             break;
 
            case FocusOut:
-             f = x_any_window_to_frame (event.xfocus.window);
+             f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
              if (event.xfocus.detail != NotifyPointer
-                 && f == x_focus_event_frame)
-               x_focus_event_frame = 0;
-             if (f && f == x_focus_frame)
-               x_new_focus_frame (0);
-#ifdef USE_X_TOOLKIT
+                 && f == dpyinfo->x_focus_event_frame)
+               dpyinfo->x_focus_event_frame = 0;
+             if (f && f == dpyinfo->x_focus_frame)
+               x_new_focus_frame (dpyinfo, 0);
+
+#ifdef HAVE_X_I18N
+             if (f && FRAME_XIC (f))
+               XUnsetICFocus (FRAME_XIC (f));
+#endif
+
              goto OTHER;
-#endif /* USE_X_TOOLKIT */
-             break;
 
            case MotionNotify:
              {
@@ -3744,7 +3876,7 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
                    && FRAME_LIVE_P (last_mouse_frame))
                  f = last_mouse_frame;
                else
-                 f = x_window_to_frame (event.xmotion.window);
+                 f = x_window_to_frame (dpyinfo, event.xmotion.window);
                if (f)
                  note_mouse_movement (f, &event.xmotion);
                else
@@ -3760,67 +3892,21 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
                    clear_mouse_face (dpyinfo);
                  }
              }
-#if 0 /* This should be unnecessary, since the toolkit has no use
-            for motion events that happen outside of the menu event loop,
-            and it seems to cause the bug that mouse events stop coming
-            after a while.  */
-#ifdef USE_X_TOOLKIT
              goto OTHER;
-#endif /* USE_X_TOOLKIT */
-#endif
-             break;
 
            case ConfigureNotify:
-             f = x_any_window_to_frame (event.xconfigure.window);
-#ifdef USE_X_TOOLKIT
+             f = x_any_window_to_frame (dpyinfo, event.xconfigure.window);
              if (f
-#if 0
-                 && ! event.xconfigure.send_event
+#ifdef USE_X_TOOLKIT
+                 && (event.xconfigure.window == XtWindow (f->output_data.x->widget))
 #endif
-                 && (event.xconfigure.window == XtWindow (f->display.x->widget)))
+                 )
                {
-                 Window win, child;
-                 int win_x, win_y;
-
-                 /* Find the position of the outside upper-left corner of
-                    the window, in the root coordinate system.  Don't
-                    refer to the parent window here; we may be processing
-                    this event after the window manager has changed our
-                    parent, but before we have reached the ReparentNotify.  */
-                 XTranslateCoordinates (FRAME_X_DISPLAY (f),
-
-                                        /* From-window, to-window.  */
-                                        XtWindow (f->display.x->widget),
-                                        FRAME_X_DISPLAY_INFO (f)->root_window,
-
-                                        /* From-position, to-position.  */
-                                        -event.xconfigure.border_width,
-                                        -event.xconfigure.border_width,
-                                        &win_x, &win_y,
-
-                                        /* Child of win.  */
-                                        &child);
-                 event.xconfigure.x = win_x;
-                 event.xconfigure.y = win_y;
-
-                 f->display.x->pixel_width = event.xconfigure.width;
-                 f->display.x->pixel_height = event.xconfigure.height;
-                 f->display.x->left_pos = event.xconfigure.x;
-                 f->display.x->top_pos = event.xconfigure.y;
+#ifndef USE_X_TOOLKIT
+                 /* In the toolkit version, change_frame_size
+                    is called by the code that handles resizing
+                    of the EmacsFrame widget.  */
 
-                 /* What we have now is the position of Emacs's own window.
-                    Convert that to the position of the window manager window.  */
-                 {
-                   int x, y;
-                   x_real_positions (f, &x, &y);
-                   f->display.x->left_pos = x;
-                   f->display.x->top_pos = y;
-                 }
-               }
-             goto OTHER;
-#else /* not USE_X_TOOLKIT */
-             if (f)
-               {
                  int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
                  int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
 
@@ -3829,14 +3915,21 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
                     to check the pixel dimensions as well.  */
                  if (columns != f->width
                      || rows != f->height
-                     || event.xconfigure.width != f->display.x->pixel_width
-                     || event.xconfigure.height != f->display.x->pixel_height)
+                     || event.xconfigure.width != f->output_data.x->pixel_width
+                     || event.xconfigure.height != f->output_data.x->pixel_height)
                    {
                      change_frame_size (f, rows, columns, 0, 1);
                      SET_FRAME_GARBAGED (f);
                    }
+#endif
 
-                 if (! event.xconfigure.send_event)
+                 /* Formerly, in the USE_X_TOOLKIT version,
+                    we did not test send_event here.  */
+                 if (1
+#ifndef USE_X_TOOLKIT
+                     && ! event.xconfigure.send_event
+#endif
+                     )
                    {
                      Window win, child;
                      int win_x, win_y;
@@ -3849,7 +3942,7 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
                      XTranslateCoordinates (FRAME_X_DISPLAY (f),
 
                                             /* From-window, to-window.  */
-                                            f->display.x->window_desc,
+                                            event.xconfigure.window,
                                             FRAME_X_DISPLAY_INFO (f)->root_window,
 
                                             /* From-position, to-position.  */
@@ -3863,29 +3956,32 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
                      event.xconfigure.y = win_y;
                    }
 
-                 f->display.x->pixel_width = event.xconfigure.width;
-                 f->display.x->pixel_height = event.xconfigure.height;
-                 f->display.x->left_pos = event.xconfigure.x;
-                 f->display.x->top_pos = event.xconfigure.y;
+                 f->output_data.x->pixel_width = event.xconfigure.width;
+                 f->output_data.x->pixel_height = event.xconfigure.height;
+                 f->output_data.x->left_pos = event.xconfigure.x;
+                 f->output_data.x->top_pos = event.xconfigure.y;
 
                  /* What we have now is the position of Emacs's own window.
                     Convert that to the position of the window manager window.  */
                  {
                    int x, y;
                    x_real_positions (f, &x, &y);
-                   f->display.x->left_pos = x;
-                   f->display.x->top_pos = y;
+                   f->output_data.x->left_pos = x;
+                   f->output_data.x->top_pos = y;
+                   /* Formerly we did not do this in the USE_X_TOOLKIT
+                      version.  Let's try making them the same.  */
+/* #ifndef USE_X_TOOLKIT */
                    if (y != event.xconfigure.y)
                      {
                        /* Since the WM decorations come below top_pos now,
                           we must put them below top_pos in the future.  */
-                       f->display.x->win_gravity = NorthWestGravity;
-                       x_wm_set_size_hint (f, 0, 0);
+                       f->output_data.x->win_gravity = NorthWestGravity;
+                       x_wm_set_size_hint (f, (long) 0, 0);
                      }
+/* #endif */
                  }
                }
-#endif /* not USE_X_TOOLKIT */
-             break;
+             goto OTHER;
 
            case ButtonPress:
            case ButtonRelease:
@@ -3901,11 +3997,11 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
                    && FRAME_LIVE_P (last_mouse_frame))
                  f = last_mouse_frame;
                else
-                 f = x_window_to_frame (event.xmotion.window);
+                 f = x_window_to_frame (dpyinfo, event.xbutton.window);
 
                if (f)
                  {
-                   if (!x_focus_frame || (f == x_focus_frame))
+                   if (!dpyinfo->x_focus_frame || f == dpyinfo->x_focus_frame)
                      construct_mouse_click (&emacs_event, &event, f);
                  }
                else
@@ -3915,15 +4011,6 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
 
                    if (bar)
                      x_scroll_bar_handle_click (bar, &event, &emacs_event);
-#ifdef USE_X_TOOLKIT
-                   else
-                     {
-                       /* 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)
@@ -3945,7 +4032,47 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
                  }
 
 #ifdef USE_X_TOOLKIT
-               goto OTHER;
+               f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
+               /* For a down-event in the menu bar,
+                  don't pass it to Xt right now.
+                  Instead, save it away
+                  and we will pass it to Xt from kbd_buffer_get_event.
+                  That way, we can run some Lisp code first.  */
+               if (f && event.type == ButtonPress
+                   /* Verify the event is really within the menu bar
+                      and not just sent to it due to grabbing.  */
+                   && event.xbutton.x >= 0
+                   && event.xbutton.x < f->output_data.x->pixel_width
+                   && event.xbutton.y >= 0
+                   && event.xbutton.y < f->output_data.x->menubar_height
+                   && event.xbutton.same_screen)
+                 {
+                   SET_SAVED_BUTTON_EVENT;
+                   XSETFRAME (last_mouse_press_frame, f);
+                 }
+               else if (event.type == ButtonPress)
+                 {
+                   last_mouse_press_frame = Qnil;
+                   goto OTHER;
+                 }
+#ifdef USE_MOTIF /* This should do not harm for Lucid,
+                   but I am trying to be cautious.  */
+               else if (event.type == ButtonRelease)
+                 {
+                   if (!NILP (last_mouse_press_frame))
+                     {
+                       f = XFRAME (last_mouse_press_frame);
+                       if (f->output_data.x)
+                         {
+                           SET_SAVED_BUTTON_EVENT;
+                         }
+                     }
+                   else 
+                     goto OTHER;
+                 }
+#endif /* USE_MOTIF */
+               else
+                 goto OTHER;
 #endif /* USE_X_TOOLKIT */
              }
              break;
@@ -3966,14 +4093,11 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
                case MappingKeyboard:
                  XRefreshKeyboardMapping (&event.xmapping);
                }
-#ifdef USE_X_TOOLKIT
              goto OTHER;
-#endif /* USE_X_TOOLKIT */
-             break;
 
            default:
-#ifdef USE_X_TOOLKIT
            OTHER:
+#ifdef USE_X_TOOLKIT
              BLOCK_INPUT;
              XtDispatchEvent (&event);
              UNBLOCK_INPUT;
@@ -3994,8 +4118,14 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
       if (x_noop_count >= 100)
        {
          x_noop_count=0;
-         /* Use the first display in the list.  Why not?  */
-         XNoOp (x_display_list->display);
+
+         if (next_noop_dpyinfo == 0)
+           next_noop_dpyinfo = x_display_list;
+
+         XNoOp (next_noop_dpyinfo->display);
+
+         /* Each time we get here, cycle through the displays now open.  */
+         next_noop_dpyinfo = next_noop_dpyinfo->next;
        }
     }
 
@@ -4015,19 +4145,21 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
 /* Drawing the cursor.  */
 
 
-/* Draw a hollow box cursor.  Don't change the inside of the box.  */
+/* Draw a hollow box cursor on frame F at X, Y.
+   Don't change the inside of the box.  */
 
 static void
-x_draw_box (f)
+x_draw_box (f, x, y)
      struct frame *f;
+     int x, 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;
+  int left = CHAR_TO_PIXEL_COL (f, x);
+  int top  = CHAR_TO_PIXEL_ROW (f, y);
+  int width = FONT_WIDTH (f->output_data.x->font);
+  int height = f->output_data.x->line_height;
 
   XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
-                 f->display.x->cursor_gc,
+                 f->output_data.x->cursor_gc,
                  left, top, width - 1, height - 1);
 }
 
@@ -4046,7 +4178,7 @@ clear_cursor (f)
       || f->phys_cursor_x < 0)
     return;
 
-  x_display_cursor (f, 0);
+  x_update_cursor (f, 0);
   f->phys_cursor_x = -1;
 }
 
@@ -4068,35 +4200,28 @@ x_draw_single_glyph (f, row, column, glyph, highlight)
 }
 
 static void
-x_display_bar_cursor (f, on)
+x_display_bar_cursor (f, on, x, y)
      struct frame *f;
      int on;
+     int x, y;
 {
   struct frame_glyphs *current_glyphs = FRAME_CURRENT_GLYPHS (f);
 
   /* This is pointless on invisible frames, and dangerous on garbaged
      frames; in the latter case, the frame may be in the midst of
-     changing its size, and curs_x and curs_y may be off the frame.  */
+     changing its size, and x and y may be off the frame.  */
   if (! FRAME_VISIBLE_P (f) || FRAME_GARBAGED_P (f))
     return;
 
   if (! on && f->phys_cursor_x < 0)
     return;
 
-  /* If we're not updating, then we want to use the current frame's
-     cursor position, not our local idea of where the cursor ought to be.  */
-  if (f != updating_frame)
-    {
-      curs_x = FRAME_CURSOR_X (f);
-      curs_y = FRAME_CURSOR_Y (f);
-    }
-
   /* If there is anything wrong with the current cursor state, remove it.  */
   if (f->phys_cursor_x >= 0
       && (!on
-         || f->phys_cursor_x != curs_x
-         || f->phys_cursor_y != curs_y
-         || f->display.x->current_cursor != bar_cursor))
+         || f->phys_cursor_x != x
+         || f->phys_cursor_y != y
+         || f->output_data.x->current_cursor != bar_cursor))
     {
       /* Erase the cursor by redrawing the character underneath it.  */
       x_draw_single_glyph (f, f->phys_cursor_y, f->phys_cursor_x,
@@ -4108,24 +4233,24 @@ x_display_bar_cursor (f, on)
   /* If we now need a cursor in the new place or in the new form, do it so.  */
   if (on
       && (f->phys_cursor_x < 0
-         || (f->display.x->current_cursor != bar_cursor)))
+         || (f->output_data.x->current_cursor != bar_cursor)))
     {
       f->phys_cursor_glyph
-       = ((current_glyphs->enable[curs_y]
-           && curs_x < current_glyphs->used[curs_y])
-          ? current_glyphs->glyphs[curs_y][curs_x]
+       = ((current_glyphs->enable[y]
+           && x < current_glyphs->used[y])
+          ? current_glyphs->glyphs[y][x]
           : SPACEGLYPH);
       XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
-                     f->display.x->cursor_gc,
-                     CHAR_TO_PIXEL_COL (f, curs_x),
-                     CHAR_TO_PIXEL_ROW (f, curs_y),
-                     max (f->display.x->cursor_width, 1),
-                     f->display.x->line_height);
+                     f->output_data.x->cursor_gc,
+                     CHAR_TO_PIXEL_COL (f, x),
+                     CHAR_TO_PIXEL_ROW (f, y),
+                     max (f->output_data.x->cursor_width, 1),
+                     f->output_data.x->line_height);
 
-      f->phys_cursor_x = curs_x;
-      f->phys_cursor_y = curs_y;
+      f->phys_cursor_x = x;
+      f->phys_cursor_y = y;
 
-      f->display.x->current_cursor = bar_cursor;
+      f->output_data.x->current_cursor = bar_cursor;
     }
 
   if (updating_frame != f)
@@ -4134,19 +4259,19 @@ x_display_bar_cursor (f, on)
 
 
 /* Turn the displayed cursor of frame F on or off according to ON.
-   If ON is nonzero, where to put the cursor is specified
-   by F->cursor_x and F->cursor_y.  */
+   If ON is nonzero, where to put the cursor is specified by X and Y.  */
 
 static void
-x_display_box_cursor (f, on)
+x_display_box_cursor (f, on, x, y)
      struct frame *f;
      int on;
+     int x, y;
 {
   struct frame_glyphs *current_glyphs = FRAME_CURRENT_GLYPHS (f);
 
   /* This is pointless on invisible frames, and dangerous on garbaged
      frames; in the latter case, the frame may be in the midst of
-     changing its size, and curs_x and curs_y may be off the frame.  */
+     changing its size, and x and y may be off the frame.  */
   if (! FRAME_VISIBLE_P (f) || FRAME_GARBAGED_P (f))
     return;
 
@@ -4154,24 +4279,16 @@ x_display_box_cursor (f, on)
   if (!on && f->phys_cursor_x < 0)
     return;
 
-  /* If we're not updating, then we want to use the current frame's
-     cursor position, not our local idea of where the cursor ought to be.  */
-  if (f != updating_frame)
-    {
-      curs_x = FRAME_CURSOR_X (f);
-      curs_y = FRAME_CURSOR_Y (f);
-    }
-
   /* If cursor is currently being shown and we don't want it to be
      or it is in the wrong place,
      or we want a hollow box and it's not so, (pout!)
      erase it.  */
   if (f->phys_cursor_x >= 0
       && (!on
-         || f->phys_cursor_x != curs_x
-         || f->phys_cursor_y != curs_y
-         || (f->display.x->current_cursor != hollow_box_cursor
-             && (f != x_highlight_frame))))
+         || f->phys_cursor_x != x
+         || f->phys_cursor_y != y
+         || (f->output_data.x->current_cursor != hollow_box_cursor
+             && (f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame))))
     {
       int mouse_face_here = 0;
       struct frame_glyphs *active_glyphs = FRAME_CURRENT_GLYPHS (f);
@@ -4195,12 +4312,12 @@ x_display_box_cursor (f, on)
 
       /* If the font is not as tall as a whole line,
         we must explicitly clear the line's whole height.  */
-      if (FONT_HEIGHT (f->display.x->font) != f->display.x->line_height)
+      if (FONT_HEIGHT (f->output_data.x->font) != f->output_data.x->line_height)
        XClearArea (FRAME_X_DISPLAY (f), 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, False);
+                   FONT_WIDTH (f->output_data.x->font),
+                   f->output_data.x->line_height, False);
       /* Erase the cursor by redrawing the character underneath it.  */
       x_draw_single_glyph (f, f->phys_cursor_y, f->phys_cursor_x,
                           f->phys_cursor_glyph,
@@ -4215,44 +4332,73 @@ x_display_box_cursor (f, on)
      write it in the right place.  */
   if (on
       && (f->phys_cursor_x < 0
-         || (f->display.x->current_cursor != filled_box_cursor
-             && f == x_highlight_frame)))
+         || (f->output_data.x->current_cursor != filled_box_cursor
+             && f == FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)))
     {
       f->phys_cursor_glyph
-       = ((current_glyphs->enable[curs_y]
-           && curs_x < current_glyphs->used[curs_y])
-          ? current_glyphs->glyphs[curs_y][curs_x]
+       = ((current_glyphs->enable[y]
+           && x < current_glyphs->used[y])
+          ? current_glyphs->glyphs[y][x]
           : SPACEGLYPH);
-      if (f != x_highlight_frame)
+      if (f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
        {
-         x_draw_box (f);
-         f->display.x->current_cursor = hollow_box_cursor;
+         x_draw_box (f, x, y);
+         f->output_data.x->current_cursor = hollow_box_cursor;
        }
       else
        {
-         x_draw_single_glyph (f, curs_y, curs_x,
+         x_draw_single_glyph (f, y, x,
                               f->phys_cursor_glyph, 2);
-         f->display.x->current_cursor = filled_box_cursor;
+         f->output_data.x->current_cursor = filled_box_cursor;
        }
 
-      f->phys_cursor_x = curs_x;
-      f->phys_cursor_y = curs_y;
+      f->phys_cursor_x = x;
+      f->phys_cursor_y = y;
     }
 
   if (updating_frame != f)
     XFlush (FRAME_X_DISPLAY (f));
 }
 
-x_display_cursor (f, on)
+/* Display the cursor on frame F, or clear it, according to ON.
+   Also set the frame's cursor position to X and Y.  */
+
+x_display_cursor (f, on, x, y)
      struct frame *f;
      int on;
+     int x, y;
 {
   BLOCK_INPUT;
 
   if (FRAME_DESIRED_CURSOR (f) == filled_box_cursor)
-    x_display_box_cursor (f, on);
+    x_display_box_cursor (f, on, x, y);
   else if (FRAME_DESIRED_CURSOR (f) == bar_cursor)
-    x_display_bar_cursor (f, on);
+    x_display_bar_cursor (f, on, x, y);
+  else
+    /* Those are the only two we have implemented!  */
+    abort ();
+
+  UNBLOCK_INPUT;
+}
+
+/* Display the cursor on frame F, or clear it, according to ON.
+   Don't change the cursor's position.  */
+
+x_update_cursor (f, on)
+     struct frame *f;
+     int on;
+{
+  /* If we don't have any previous cursor position to use,
+     leave the cursor off.  */
+  if (f->phys_cursor_x < 0)
+    return;
+
+  BLOCK_INPUT;
+
+  if (FRAME_DESIRED_CURSOR (f) == filled_box_cursor)
+    x_display_box_cursor (f, on, f->phys_cursor_x, f->phys_cursor_y);
+  else if (FRAME_DESIRED_CURSOR (f) == bar_cursor)
+    x_display_bar_cursor (f, on, f->phys_cursor_x, f->phys_cursor_y);
   else
     /* Those are the only two we have implemented!  */
     abort ();
@@ -4285,9 +4431,9 @@ x_bitmap_icon (f, file)
     return 1;
 
   /* Free up our existing icon bitmap if any.  */
-  if (f->display.x->icon_bitmap > 0)
-    x_destroy_bitmap (f, f->display.x->icon_bitmap);
-  f->display.x->icon_bitmap = 0;
+  if (f->output_data.x->icon_bitmap > 0)
+    x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
+  f->output_data.x->icon_bitmap = 0;
 
   if (STRINGP (file))
     bitmap_id = x_create_bitmap_from_file (f, file);
@@ -4309,13 +4455,14 @@ x_bitmap_icon (f, file)
     }
 
   x_wm_set_icon_pixmap (f, bitmap_id);
-  f->display.x->icon_bitmap = bitmap_id;
+  f->output_data.x->icon_bitmap = bitmap_id;
 
   return 0;
 }
 
 
-/* Make the x-window of frame F use a rectangle with text.  */
+/* Make the x-window of frame F use a rectangle with text.
+   Use ICON_NAME as the text.  */
 
 int
 x_text_icon (f, icon_name)
@@ -4325,20 +4472,27 @@ x_text_icon (f, icon_name)
   if (FRAME_X_WINDOW (f) == 0)
     return 1;
 
-  if (icon_name)
-    f->display.x->icon_label = icon_name;
-  else
-    if (! f->display.x->icon_label)
-      f->display.x->icon_label = " *emacs* ";
-
-#if 0
-  XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
-               (char *) f->display.x->icon_label);
-#endif
+#ifdef HAVE_X11R4
+  {
+    XTextProperty text;
+    text.value = (unsigned char *) icon_name;
+    text.encoding = XA_STRING;
+    text.format = 8;
+    text.nitems = strlen (icon_name);
+#ifdef USE_X_TOOLKIT
+    XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
+                   &text);
+#else /* not USE_X_TOOLKIT */
+    XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
+#endif /* not USE_X_TOOLKIT */
+  }
+#else /* not HAVE_X11R4 */
+  XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
+#endif /* not HAVE_X11R4 */
 
-  if (f->display.x->icon_bitmap > 0)
-    x_destroy_bitmap (f, f->display.x->icon_bitmap);
-  f->display.x->icon_bitmap = 0;
+  if (f->output_data.x->icon_bitmap > 0)
+    x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
+  f->output_data.x->icon_bitmap = 0;
   x_wm_set_icon_pixmap (f, 0);
 
   return 0;
@@ -4356,8 +4510,13 @@ x_connection_closed (display, error_message)
   struct x_display_info *dpyinfo = x_display_info_for_display (display);
   Lisp_Object frame, tail;
 
-  if (_Xdebug)
-    abort ();
+  /* Indicate that this display is dead.  */
+
+  #ifdef USE_X_TOOLKIT
+  XtCloseDisplay (display);
+  #endif
+
+  dpyinfo->display = 0;
 
   /* First delete frames whose minibuffers are on frames
      that are on the dead display.  */
@@ -4379,13 +4538,19 @@ x_connection_closed (display, error_message)
   FOR_EACH_FRAME (tail, frame)
     if (FRAME_X_P (XFRAME (frame))
        && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
-      Fdelete_frame (frame, Qt);
+      {
+       /* 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);
+      }
 
-  x_delete_display (dpyinfo);
+  if (dpyinfo)
+    x_delete_display (dpyinfo);
 
   if (x_display_list == 0)
     {
-      fprintf (stderr, "%s", error_message);
+      fprintf (stderr, "%s\n", error_message);
       shut_down_emacs (0, 0, Qnil);
       exit (70);
     }
@@ -4397,21 +4562,10 @@ x_connection_closed (display, error_message)
   sigunblock (sigmask (SIGALRM));
   TOTALLY_UNBLOCK_INPUT;
 
+  clear_waiting_for_input ();
   error ("%s", error_message);
 }
 
-static SIGTYPE
-x_connection_signal (signalnum)        /* If we don't have an argument, */
-     int signalnum;            /* some compilers complain in signal calls. */
-{
-  /* We really ought to close the connection to the display
-     that actually failed.
-     But do we actually get this signal ever with X11?  */
-  fprintf (stderr, "X connection closed");
-  shut_down_emacs (0, 0, Qnil);
-  exit (70);
-}
-
 /* This is the usual handler for X protocol errors.
    It kills all frames on the display that we got the error for.
    If that was the only one, it prints an error message and kills Emacs.  */
@@ -4446,6 +4600,23 @@ x_io_error_quitter (display)
   x_connection_closed (display, buf);
 }
 \f
+/* Handle SIGPIPE, which can happen when the connection to a server
+   simply goes away.  SIGPIPE is handled by x_connection_signal.
+   Don't need to do anything, because the write which caused the 
+   SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
+   which will do the appropriate cleanup for us. */
+   
+static SIGTYPE
+x_connection_signal (signalnum)        /* If we don't have an argument, */
+     int signalnum;            /* some compilers complain in signal calls. */
+{
+#ifdef USG
+  /* USG systems forget handlers when they are used;
+     must reestablish each time */
+  signal (signalnum, x_connection_signal);
+#endif /* USG */
+}
+\f
 /* A buffer for storing X error messages.  */
 static char *x_caught_error_message;
 #define X_CAUGHT_ERROR_MESSAGE_SIZE 200
@@ -4599,7 +4770,7 @@ x_new_font (f, fontname)
 
   /* If we have, just return it from the table.  */
   if (already_loaded >= 0)
-    f->display.x->font = FRAME_X_DISPLAY_INFO (f)->font_table[already_loaded].font;
+    f->output_data.x->font = FRAME_X_DISPLAY_INFO (f)->font_table[already_loaded].font;
   /* Otherwise, load the font and add it to the table.  */
   else
     {
@@ -4654,9 +4825,7 @@ x_new_font (f, fontname)
       full_name = 0;
       for (i = 0; i < font->n_properties; i++)
        {
-         char *atom
-           = XGetAtomName (FRAME_X_DISPLAY (f), font->properties[i].name);
-         if (!strcmp (atom, "FONT"))
+         if (FRAME_X_DISPLAY_INFO (f)->Xatom_FONT == font->properties[i].name)
            {
              char *name = XGetAtomName (FRAME_X_DISPLAY (f),
                                         (Atom) (font->properties[i].card32));
@@ -4680,8 +4849,6 @@ x_new_font (f, fontname)
 
              break;
            }
-
-         XFree (atom);
        }
 
       n_fonts = FRAME_X_DISPLAY_INFO (f)->n_fonts;
@@ -4691,7 +4858,7 @@ x_new_font (f, fontname)
        FRAME_X_DISPLAY_INFO (f)->font_table[n_fonts].full_name = full_name;
       else
        FRAME_X_DISPLAY_INFO (f)->font_table[n_fonts].full_name = FRAME_X_DISPLAY_INFO (f)->font_table[n_fonts].name;
-      f->display.x->font = FRAME_X_DISPLAY_INFO (f)->font_table[n_fonts].font = font;
+      f->output_data.x->font = FRAME_X_DISPLAY_INFO (f)->font_table[n_fonts].font = font;
       FRAME_X_DISPLAY_INFO (f)->n_fonts++;
 
       if (full_name)
@@ -4701,7 +4868,7 @@ x_new_font (f, fontname)
   /* Compute the scroll bar width in character columns.  */
   if (f->scroll_bar_pixel_width > 0)
     {
-      int wid = FONT_WIDTH (f->display.x->font);
+      int wid = FONT_WIDTH (f->output_data.x->font);
       f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
     }
   else
@@ -4710,12 +4877,12 @@ 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->display.x->normal_gc,
-               f->display.x->font->fid);
-      XSetFont (FRAME_X_DISPLAY (f), f->display.x->reverse_gc,
-               f->display.x->font->fid);
-      XSetFont (FRAME_X_DISPLAY (f), f->display.x->cursor_gc,
-               f->display.x->font->fid);
+      XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
+               f->output_data.x->font->fid);
+      XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
+               f->output_data.x->font->fid);
+      XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
+               f->output_data.x->font->fid);
 
       frame_update_line_height (f);
       x_set_window_size (f, 0, f->width, f->height);
@@ -4723,7 +4890,7 @@ x_new_font (f, fontname)
   else
     /* If we are setting a new frame's font for the first time,
        there are no faces yet, so this font's height is the line height.  */
-    f->display.x->line_height = FONT_HEIGHT (f->display.x->font);
+    f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
 
   {
     Lisp_Object lispy_name;
@@ -4738,23 +4905,33 @@ x_new_font (f, fontname)
   }
 }
 \f
+/* Calculate the absolute position in frame F
+   from its current recorded position values and gravity.  */
+
 x_calc_absolute_position (f)
      struct frame *f;
 {
   Window win, child;
   int win_x = 0, win_y = 0;
-  int flags = f->display.x->size_hint_flags;
+  int flags = f->output_data.x->size_hint_flags;
+  int this_window;
+
+#ifdef USE_X_TOOLKIT
+  this_window = XtWindow (f->output_data.x->widget);
+#else
+  this_window = FRAME_X_WINDOW (f);
+#endif
 
   /* Find the position of the outside upper-left corner of
      the inner window, with respect to the outer window.  */
-  if (f->display.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
+  if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
     {
       BLOCK_INPUT;
       XTranslateCoordinates (FRAME_X_DISPLAY (f),
 
                             /* From-window, to-window.  */
-                            f->display.x->window_desc,
-                            f->display.x->parent_desc,
+                            this_window,
+                            f->output_data.x->parent_desc,
 
                             /* From-position, to-position.  */
                             0, 0, &win_x, &win_y,
@@ -4767,54 +4944,70 @@ x_calc_absolute_position (f)
   /* Treat negative positions as relative to the leftmost bottommost
      position that fits on the screen.  */
   if (flags & XNegative)
-    f->display.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
-                             - 2 * f->display.x->border_width - win_x
-                             - PIXEL_WIDTH (f)
-                             + f->display.x->left_pos);
+    f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
+                                 - 2 * f->output_data.x->border_width - win_x
+                                 - PIXEL_WIDTH (f)
+                                 + f->output_data.x->left_pos);
 
   if (flags & YNegative)
-    f->display.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
-                            - 2 * f->display.x->border_width - win_y
-                            - PIXEL_HEIGHT (f)
-                            + f->display.x->top_pos);
+    /* We used to subtract f->output_data.x->menubar_height here
+       in the toolkit case, but PIXEL_HEIGHT already includes that.  */
+    f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
+                                - 2 * f->output_data.x->border_width - win_y
+                                - PIXEL_HEIGHT (f)
+                                + f->output_data.x->top_pos);
+
   /* The left_pos and top_pos
      are now relative to the top and left screen edges,
      so the flags should correspond.  */
-  f->display.x->size_hint_flags &= ~ (XNegative | YNegative);
+  f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
 }
 
 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
    to really change the position, and 0 when calling from
    x_make_frame_visible (in that case, XOFF and YOFF are the current
-   position values).  */
+   position values).  It is -1 when calling from x_set_frame_parameters,
+   which means, do adjust for borders but don't change the gravity.  */
 
 x_set_offset (f, xoff, yoff, change_gravity)
      struct frame *f;
      register int xoff, yoff;
      int change_gravity;
 {
-  if (change_gravity)
+  int modified_top, modified_left;
+
+  if (change_gravity > 0)
     {
-      f->display.x->top_pos = yoff;
-      f->display.x->left_pos = xoff;
-      f->display.x->size_hint_flags &= ~ (XNegative | YNegative);
+      f->output_data.x->top_pos = yoff;
+      f->output_data.x->left_pos = xoff;
+      f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
       if (xoff < 0)
-       f->display.x->size_hint_flags |= XNegative;
+       f->output_data.x->size_hint_flags |= XNegative;
       if (yoff < 0)
-       f->display.x->size_hint_flags |= YNegative;
-      f->display.x->win_gravity = NorthWestGravity;
+       f->output_data.x->size_hint_flags |= YNegative;
+      f->output_data.x->win_gravity = NorthWestGravity;
     }
   x_calc_absolute_position (f);
 
   BLOCK_INPUT;
-  x_wm_set_size_hint (f, 0, 0);
+  x_wm_set_size_hint (f, (long) 0, 0);
+
+  /* It is a mystery why we need to add the border_width here
+     when the frame is already visible, but experiment says we do.  */
+  modified_left = f->output_data.x->left_pos;
+  modified_top = f->output_data.x->top_pos;
+  if (change_gravity != 0)
+    {
+      modified_left += f->output_data.x->border_width;
+      modified_top += f->output_data.x->border_width;
+    }
 
 #ifdef USE_X_TOOLKIT
-  XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->display.x->widget),
-              f->display.x->left_pos, f->display.x->top_pos);
+  XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
+              modified_left, modified_top);
 #else /* not USE_X_TOOLKIT */
   XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
-              f->display.x->left_pos, f->display.x->top_pos);
+              modified_left, modified_top);
 #endif /* not USE_X_TOOLKIT */
   UNBLOCK_INPUT;
 }
@@ -4831,38 +5024,38 @@ x_set_window_size (f, change_gravity, cols, rows)
 {
   int pixelwidth, pixelheight;
   int mask;
+  Lisp_Object window;
+  struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
 
-#ifdef USE_X_TOOLKIT
   BLOCK_INPUT;
+
+#ifdef USE_X_TOOLKIT
   {
     /* The x and y position of the widget is clobbered by the
        call to XtSetValues within EmacsFrameSetCharSize.
        This is a real kludge, but I don't understand Xt so I can't
        figure out a correct fix.  Can anyone else tell me? -- rms.  */
-    int xpos = f->display.x->widget->core.x;
-    int ypos = f->display.x->widget->core.y;
-    EmacsFrameSetCharSize (f->display.x->edit_widget, cols, rows);
-    f->display.x->widget->core.x = xpos;
-    f->display.x->widget->core.y = ypos;
+    int xpos = f->output_data.x->widget->core.x;
+    int ypos = f->output_data.x->widget->core.y;
+    EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
+    f->output_data.x->widget->core.x = xpos;
+    f->output_data.x->widget->core.y = ypos;
   }
-  UNBLOCK_INPUT;
 
 #else /* not USE_X_TOOLKIT */
 
-  BLOCK_INPUT;
-
   check_frame_size (f, &rows, &cols);
-  f->display.x->vertical_scroll_bar_extra
+  f->output_data.x->vertical_scroll_bar_extra
     = (!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)));
+       : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
   pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
   pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
 
-  f->display.x->win_gravity = NorthWestGravity;
-  x_wm_set_size_hint (f, 0, 0);
+  f->output_data.x->win_gravity = NorthWestGravity;
+  x_wm_set_size_hint (f, (long) 0, 0);
 
   XSync (FRAME_X_DISPLAY (f), False);
   XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
@@ -4882,6 +5075,16 @@ x_set_window_size (f, change_gravity, cols, rows)
   PIXEL_WIDTH (f) = pixelwidth;
   PIXEL_HEIGHT (f) = pixelheight;
 
+  /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
+     receive in the ConfigureNotify event; if we get what we asked
+     for, then the event won't cause the screen to become garbaged, so
+     we have to make sure to do it here.  */
+  SET_FRAME_GARBAGED (f);
+
+  XFlush (FRAME_X_DISPLAY (f));
+
+#endif /* not USE_X_TOOLKIT */
+
   /* If cursor was outside the new size, mark it as off.  */
   if (f->phys_cursor_y >= rows
       || f->phys_cursor_x >= cols)
@@ -4890,18 +5093,22 @@ x_set_window_size (f, change_gravity, cols, rows)
       f->phys_cursor_y = -1;
     }
 
-  /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
-     receive in the ConfigureNotify event; if we get what we asked
-     for, then the event won't cause the screen to become garbaged, so
-     we have to make sure to do it here.  */
-  SET_FRAME_GARBAGED (f);
+  /* Clear out any recollection of where the mouse highlighting was,
+     since it might be in a place that's outside the new frame size. 
+     Actually checking whether it is outside is a pain in the neck,
+     so don't try--just let the highlighting be done afresh with new size.  */
+  window = dpyinfo->mouse_face_window;
+  if (! NILP (window) && XFRAME (window) == f)
+    {
+      dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
+      dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
+      dpyinfo->mouse_face_window = Qnil;
+    }
 
-  XFlush (FRAME_X_DISPLAY (f));
   UNBLOCK_INPUT;
-#endif /* not USE_X_TOOLKIT */
 }
 \f
-/* Mouse warping, focus shifting, raising and lowering.  */
+/* Mouse warping.  */
 
 void
 x_set_mouse_position (f, x, y)
@@ -4910,8 +5117,8 @@ x_set_mouse_position (f, x, y)
 {
   int pix_x, pix_y;
 
-  pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH  (f->display.x->font) / 2;
-  pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->display.x->line_height / 2;
+  pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH  (f->output_data.x->font) / 2;
+  pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
 
   if (pix_x < 0) pix_x = 0;
   if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
@@ -4939,6 +5146,8 @@ x_set_mouse_pixel_position (f, pix_x, pix_y)
                0, 0, 0, 0, pix_x, pix_y);
   UNBLOCK_INPUT;
 }
+\f
+/* focus shifting, raising and lowering.  */
 
 x_focus_on_frame (f)
      struct frame *f;
@@ -4960,7 +5169,7 @@ x_unfocus_frame (f)
 {
 #if 0
   /* Look at the remarks in x_focus_on_frame.  */
-  if (x_focus_frame == f)
+  if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
     XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
                    RevertToPointerRoot, CurrentTime);
 #endif /* ! 0 */
@@ -4975,7 +5184,7 @@ x_raise_frame (f)
     {
       BLOCK_INPUT;
 #ifdef USE_X_TOOLKIT
-      XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->display.x->widget));
+      XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
 #else /* not USE_X_TOOLKIT */
       XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
 #endif /* not USE_X_TOOLKIT */
@@ -4993,7 +5202,7 @@ x_lower_frame (f)
     {
       BLOCK_INPUT;
 #ifdef USE_X_TOOLKIT
-      XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->display.x->widget));
+      XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
 #else /* not USE_X_TOOLKIT */
       XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
 #endif /* not USE_X_TOOLKIT */
@@ -5003,18 +5212,24 @@ x_lower_frame (f)
 }
 
 static void
-XTframe_raise_lower (f, raise)
+XTframe_raise_lower (f, raise_flag)
      FRAME_PTR f;
-     int raise;
+     int raise_flag;
 {
-  if (raise)
+  if (raise_flag)
     x_raise_frame (f);
   else
     x_lower_frame (f);
 }
+\f
+/* Change of visibility.  */
 
-/* Change from withdrawn state to mapped state,
-   or deiconify. */
+/* This tries to wait until the frame is really visible.
+   However, if the window manager asks the user where to position
+   the frame, this will return before the user finishes doing that.
+   The frame will not actually be visible at that time,
+   but it will become visible later when the window manager
+   finishes with it.  */
 
 x_make_frame_visible (f)
      struct frame *f;
@@ -5030,16 +5245,21 @@ x_make_frame_visible (f)
 
   if (! FRAME_VISIBLE_P (f))
     {
-#ifndef USE_X_TOOLKIT
-      if (! FRAME_ICONIFIED_P (f))
-       x_set_offset (f, f->display.x->left_pos, f->display.x->top_pos, 0);
-#endif
+      /* We test FRAME_GARBAGED_P here to make sure we don't
+        call x_set_offset a second time
+        if we get to x_make_frame_visible a second time
+        before the window gets really visible.  */
+      if (! FRAME_ICONIFIED_P (f)
+         && ! f->output_data.x->asked_for_visible)
+       x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
+
+      f->output_data.x->asked_for_visible = 1;
 
       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->display.x->widget);
+      XtMapWidget (f->output_data.x->widget);
 #else /* not USE_X_TOOLKIT */
       XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
 #endif /* not USE_X_TOOLKIT */
@@ -5101,6 +5321,8 @@ x_make_frame_visible (f)
 
 /* Change from mapped state to withdrawn state. */
 
+/* Make the frame visible (mapped and not iconified).  */
+
 x_make_frame_invisible (f)
      struct frame *f;
 {
@@ -5109,14 +5331,14 @@ x_make_frame_invisible (f)
 
 #ifdef USE_X_TOOLKIT
   /* Use the frame's outermost window, not the one we normally draw on.  */
-  window = XtWindow (f->display.x->widget);
+  window = XtWindow (f->output_data.x->widget);
 #else /* not USE_X_TOOLKIT */
   window = FRAME_X_WINDOW (f);
 #endif /* not USE_X_TOOLKIT */
 
   /* Don't keep the highlight on an invisible frame.  */
-  if (x_highlight_frame == f)
-    x_highlight_frame = 0;
+  if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
+    FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
 
 #if 0/* This might add unreliability; I don't trust it -- rms.  */
   if (! f->async_visible && ! f->async_iconified)
@@ -5130,7 +5352,7 @@ x_make_frame_invisible (f)
      program-specified, so that when the window is mapped again, it will be
      placed at the same location, without forcing the user to position it
      by hand again (they have already done that once for this window.)  */
-  x_wm_set_size_hint (f, 0, 1);
+  x_wm_set_size_hint (f, (long) 0, 1);
 
 #ifdef HAVE_X11R4
 
@@ -5191,14 +5413,16 @@ x_iconify_frame (f)
   Lisp_Object type;
 
   /* Don't keep the highlight on an invisible frame.  */
-  if (x_highlight_frame == f)
-    x_highlight_frame = 0;
+  if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
+    FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
 
   if (f->async_iconified)
     return;
 
   BLOCK_INPUT;
 
+  FRAME_SAMPLE_VISIBILITY (f);
+
   type = x_icon_type (f);
   if (!NILP (type))
     x_bitmap_icon (f, type);
@@ -5210,13 +5434,18 @@ x_iconify_frame (f)
       if (! EQ (Vx_no_window_manager, Qt))
        x_wm_set_window_state (f, IconicState);
       /* This was XtPopup, but that did nothing for an iconified frame.  */
-      XtMapWidget (f->display.x->widget);
+      XtMapWidget (f->output_data.x->widget);
+      /* The server won't give us any event to indicate
+        that an invisible frame was changed to an icon,
+        so we have to record it here.  */
+      f->iconified = 1;
+      f->async_iconified = 1;
       UNBLOCK_INPUT;
       return;
     }
 
   result = XIconifyWindow (FRAME_X_DISPLAY (f),
-                          XtWindow (f->display.x->widget),
+                          XtWindow (f->output_data.x->widget),
                           DefaultScreen (FRAME_X_DISPLAY (f)));
   UNBLOCK_INPUT;
 
@@ -5233,7 +5462,7 @@ 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))
-    x_set_offset (f, f->display.x->left_pos, f->display.x->top_pos, 0);
+    x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
 
   /* Since we don't know which revision of X we're running, we'll use both
      the X11R3 and X11R4 techniques.  I don't know if this is a good idea.  */
@@ -5276,7 +5505,7 @@ x_iconify_frame (f)
   UNBLOCK_INPUT;
 #endif /* not USE_X_TOOLKIT */
 }
-
+\f
 /* Destroy the X window of frame F.  */
 
 x_destroy_window (f)
@@ -5286,25 +5515,42 @@ x_destroy_window (f)
 
   BLOCK_INPUT;
 
-  if (f->display.x->icon_desc != 0)
-    XDestroyWindow (FRAME_X_DISPLAY (f), f->display.x->icon_desc);
-  XDestroyWindow (FRAME_X_DISPLAY (f), f->display.x->window_desc);
+  /* If a display connection is dead, don't try sending more
+     commands to the X server.  */
+  if (dpyinfo->display != 0)
+    {
+      if (f->output_data.x->icon_desc != 0)
+       XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
+#ifdef HAVE_X_I18N
+      if (FRAME_XIM (f))
+       {
+         XDestroyIC (FRAME_XIC (f));
+#if ! defined (SOLARIS2) || defined (HAVE_X11R6)
+         /* This line causes crashes on Solaris with Openwin,
+            due to an apparent bug in XCloseIM.
+            X11R6 seems not to have the bug.  */
+         XCloseIM (FRAME_XIM (f));
+#endif
+       }
+#endif
+      XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->window_desc);
 #ifdef USE_X_TOOLKIT
-  XtDestroyWidget (f->display.x->widget);
-  free_frame_menubar (f);
+      XtDestroyWidget (f->output_data.x->widget);
+      free_frame_menubar (f);
 #endif /* USE_X_TOOLKIT */
 
-  free_frame_faces (f);
-  XFlush (FRAME_X_DISPLAY (f));
+      free_frame_faces (f);
+      XFlush (FRAME_X_DISPLAY (f));
+    }
 
-  xfree (f->display.x);
-  f->display.x = 0;
-  if (f == x_focus_frame)
-    x_focus_frame = 0;
-  if (f == x_focus_event_frame)
-    x_focus_event_frame = 0;
-  if (f == x_highlight_frame)
-    x_highlight_frame = 0;
+  xfree (f->output_data.x);
+  f->output_data.x = 0;
+  if (f == dpyinfo->x_focus_frame)
+    dpyinfo->x_focus_frame = 0;
+  if (f == dpyinfo->x_focus_event_frame)
+    dpyinfo->x_focus_event_frame = 0;
+  if (f == dpyinfo->x_highlight_frame)
+    dpyinfo->x_highlight_frame = 0;
 
   dpyinfo->reference_count--;
 
@@ -5339,7 +5585,7 @@ x_wm_set_size_hint (f, flags, user_position)
   Arg al[2];
   int ac = 0;
   Dimension widget_width, widget_height;
-  Window window = XtWindow (f->display.x->widget);
+  Window window = XtWindow (f->output_data.x->widget);
 #else /* not USE_X_TOOLKIT */
   Window window = FRAME_X_WINDOW (f);
 #endif /* not USE_X_TOOLKIT */
@@ -5349,13 +5595,13 @@ x_wm_set_size_hint (f, flags, user_position)
 
   flexlines = f->height;
 
-  size_hints.x = f->display.x->left_pos;
-  size_hints.y = f->display.x->top_pos;
+  size_hints.x = f->output_data.x->left_pos;
+  size_hints.y = f->output_data.x->top_pos;
 
 #ifdef USE_X_TOOLKIT
   XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
   XtSetArg (al[ac], XtNheight, &widget_height); ac++;
-  XtGetValues (f->display.x->column_widget, al, ac);
+  XtGetValues (f->output_data.x->widget, al, ac);
   size_hints.height = widget_height;
   size_hints.width = widget_width;
 #else /* not USE_X_TOOLKIT */
@@ -5363,13 +5609,18 @@ x_wm_set_size_hint (f, flags, user_position)
   size_hints.width = PIXEL_WIDTH (f);
 #endif /* not USE_X_TOOLKIT */
 
-  size_hints.width_inc = FONT_WIDTH (f->display.x->font);
-  size_hints.height_inc = f->display.x->line_height;
+  size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
+  size_hints.height_inc = f->output_data.x->line_height;
   size_hints.max_width
     = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
   size_hints.max_height
     = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
 
+  /* Calculate the base and minimum sizes.
+
+     (When we use the X toolkit, we don't do it here.
+     Instead we copy the values that the widgets are using, below.)  */
+#ifndef USE_X_TOOLKIT
   {
     int base_width, base_height;
     int min_rows = 0, min_cols = 0;
@@ -5401,35 +5652,54 @@ x_wm_set_size_hint (f, flags, user_position)
 #endif
   }
 
+  /* If we don't need the old flags, we don't need the old hint at all.  */
   if (flags)
-    size_hints.flags |= flags;
-  else
     {
-      XSizeHints hints;                /* Sometimes I hate X Windows... */
-      long supplied_return;
-      int value;
+      size_hints.flags |= flags;
+      goto no_read;
+    }
+#endif /* not USE_X_TOOLKIT */
+
+  {
+    XSizeHints hints;          /* Sometimes I hate X Windows... */
+    long supplied_return;
+    int value;
 
 #ifdef HAVE_X11R4
-      value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
-                                &supplied_return);
+    value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
+                              &supplied_return);
 #else
-      value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
+    value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
 #endif
 
-      if (value == 0)
-       hints.flags = 0;
-      if (hints.flags & PSize)
-       size_hints.flags |= PSize;
-      if (hints.flags & PPosition)
-       size_hints.flags |= PPosition;
-      if (hints.flags & USPosition)
-       size_hints.flags |= USPosition;
-      if (hints.flags & USSize)
-       size_hints.flags |= USSize;
-    }
+#ifdef USE_X_TOOLKIT
+    size_hints.base_height = hints.base_height;
+    size_hints.base_width = hints.base_width;
+    size_hints.min_height = hints.min_height;
+    size_hints.min_width = hints.min_width;
+#endif
+
+    if (flags)
+      size_hints.flags |= flags;
+    else
+      {
+       if (value == 0)
+         hints.flags = 0;
+       if (hints.flags & PSize)
+         size_hints.flags |= PSize;
+       if (hints.flags & PPosition)
+         size_hints.flags |= PPosition;
+       if (hints.flags & USPosition)
+         size_hints.flags |= USPosition;
+       if (hints.flags & USSize)
+         size_hints.flags |= USSize;
+      }
+  }
+
+ no_read:
 
 #ifdef PWinGravity
-  size_hints.win_gravity = f->display.x->win_gravity;
+  size_hints.win_gravity = f->output_data.x->win_gravity;
   size_hints.flags |= PWinGravity;
 
   if (user_position)
@@ -5455,14 +5725,14 @@ x_wm_set_window_state (f, state)
   Arg al[1];
 
   XtSetArg (al[0], XtNinitialState, state);
-  XtSetValues (f->display.x->widget, al, 1);
+  XtSetValues (f->output_data.x->widget, al, 1);
 #else /* not USE_X_TOOLKIT */
   Window window = FRAME_X_WINDOW (f);
 
-  f->display.x->wm_hints.flags |= StateHint;
-  f->display.x->wm_hints.initial_state = state;
+  f->output_data.x->wm_hints.flags |= StateHint;
+  f->output_data.x->wm_hints.initial_state = state;
 
-  XSetWMHints (FRAME_X_DISPLAY (f), window, &f->display.x->wm_hints);
+  XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
 #endif /* not USE_X_TOOLKIT */
 }
 
@@ -5470,25 +5740,49 @@ x_wm_set_icon_pixmap (f, pixmap_id)
      struct frame *f;
      int pixmap_id;
 {
+  Pixmap icon_pixmap;
+
 #ifdef USE_X_TOOLKIT
-  Window window = XtWindow (f->display.x->widget);
+  Window window = XtWindow (f->output_data.x->widget);
 #else
   Window window = FRAME_X_WINDOW (f);
 #endif
 
   if (pixmap_id > 0)
     {
-      Pixmap icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
-      f->display.x->wm_hints.icon_pixmap = icon_pixmap;
-      f->display.x->wm_hints.flags |= IconPixmapHint;
+      icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
+      f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
     }
   else
     {
-      f->display.x->wm_hints.icon_pixmap = None;
-      f->display.x->wm_hints.flags &= ~IconPixmapHint;
+      /* It seems there is no way to turn off use of an icon pixmap.
+        The following line does it, only if no icon has yet been created,
+        for some window managers.  But with mwm it crashes.
+        Some people say it should clear the IconPixmapHint bit in this case,
+        but that doesn't work, and the X consortium said it isn't the
+        right thing at all.  Since there is no way to win,
+        best to explicitly give up.  */
+#if 0
+      f->output_data.x->wm_hints.icon_pixmap = None;
+#else
+      return;
+#endif
     }
 
-  XSetWMHints (FRAME_X_DISPLAY (f), window, &f->display.x->wm_hints);
+#ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state.  */
+
+  {
+    Arg al[1];
+    XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
+    XtSetValues (f->output_data.x->widget, al, 1);
+  }
+
+#else /* not USE_X_TOOLKIT */
+  
+  f->output_data.x->wm_hints.flags |= IconPixmapHint;
+  XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
+
+#endif /* not USE_X_TOOLKIT */
 }
 
 x_wm_set_icon_position (f, icon_x, icon_y)
@@ -5496,16 +5790,16 @@ x_wm_set_icon_position (f, icon_x, icon_y)
      int icon_x, icon_y;
 {
 #ifdef USE_X_TOOLKIT
-  Window window = XtWindow (f->display.x->widget);
+  Window window = XtWindow (f->output_data.x->widget);
 #else
   Window window = FRAME_X_WINDOW (f);
 #endif
 
-  f->display.x->wm_hints.flags |= IconPositionHint;
-  f->display.x->wm_hints.icon_x = icon_x;
-  f->display.x->wm_hints.icon_y = icon_y;
+  f->output_data.x->wm_hints.flags |= IconPositionHint;
+  f->output_data.x->wm_hints.icon_x = icon_x;
+  f->output_data.x->wm_hints.icon_y = icon_y;
 
-  XSetWMHints (FRAME_X_DISPLAY (f), window, &f->display.x->wm_hints);
+  XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
 }
 
 \f
@@ -5566,14 +5860,28 @@ x_term_init (display_name, xrm_option, resource_name)
   struct x_display_info *dpyinfo;
   XrmDatabase xrdb;
 
+  BLOCK_INPUT;
+
   if (!x_initialized)
     {
       x_initialize ();
       x_initialized = 1;
     }
 
+#ifdef HAVE_X_I18N
+  setlocale (LC_ALL, "");
+  /* In case we just overrode what init_lread did, redo it.  */
+  setlocale (LC_NUMERIC, "C");
+  setlocale (LC_TIME, "C");
+#endif
+
 #ifdef USE_X_TOOLKIT
-#ifdef HAVE_X11R5
+  /* weiner@footloose.sps.mot.com reports that this causes
+     errors with X11R5:
+          X protocol error: BadAtom (invalid Atom parameter)
+          on protocol request 18skiloaf.
+     So let's not use it until R6.  */
+#ifdef HAVE_X11XTR6
   XtSetLanguageProc (NULL, NULL, NULL);
 #endif
 
@@ -5592,6 +5900,12 @@ x_term_init (display_name, xrm_option, resource_name)
                         resource_name, EMACS_CLASS,
                         emacs_options, XtNumber (emacs_options),
                         &argc, argv);
+
+#ifdef HAVE_X11XTR6
+    /* I think this is to compensate for XtSetLanguageProc.  */
+    setlocale (LC_NUMERIC, "C");
+    setlocale (LC_TIME, "C");
+#endif
   }
 
 #else /* not USE_X_TOOLKIT */
@@ -5603,7 +5917,10 @@ x_term_init (display_name, xrm_option, resource_name)
 
   /* Detect failure.  */
   if (dpy == 0)
-    return 0;
+    {
+      UNBLOCK_INPUT;
+      return 0;
+    }
 
   /* We have definitely succeeded.  Record the new connection.  */
 
@@ -5625,8 +5942,21 @@ x_term_init (display_name, xrm_option, resource_name)
       {
        dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
        init_kboard (dpyinfo->kboard);
+       if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
+         {
+           char *vendor = ServerVendor (dpy);
+           dpyinfo->kboard->Vsystem_key_alist
+             = call1 (Qvendor_specific_keysyms,
+                      build_string (vendor ? vendor : ""));
+         }
+
        dpyinfo->kboard->next_kboard = all_kboards;
        all_kboards = dpyinfo->kboard;
+       /* Don't let the initial kboard remain current longer than necessary.
+          That would cause problems if a file loaded on startup tries to
+          prompt in the minibuffer.  */
+       if (current_kboard == initial_kboard)
+         current_kboard = dpyinfo->kboard;
       }
     dpyinfo->kboard->reference_count++;
   }
@@ -5696,6 +6026,9 @@ x_term_init (display_name, xrm_option, resource_name)
   dpyinfo->mouse_face_window = Qnil;
   dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
   dpyinfo->mouse_face_defer = 0;
+  dpyinfo->x_focus_frame = 0;
+  dpyinfo->x_focus_event_frame = 0;
+  dpyinfo->x_highlight_frame = 0;
 
   dpyinfo->Xatom_wm_protocols
     = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
@@ -5713,6 +6046,8 @@ x_term_init (display_name, xrm_option, resource_name)
     = XInternAtom (dpyinfo->display, "WM_MOVED", False);
   dpyinfo->Xatom_editres
     = XInternAtom (dpyinfo->display, "Editres", False);
+  dpyinfo->Xatom_FONT
+    = XInternAtom (dpyinfo->display, "FONT", False);
   dpyinfo->Xatom_CLIPBOARD
     = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
   dpyinfo->Xatom_TIMESTAMP
@@ -5757,9 +6092,36 @@ x_term_init (display_name, xrm_option, resource_name)
 #endif /* F_SETOWN_BUG */
 
 #ifdef SIGIO
-  init_sigio (connection);
+  if (interrupt_input)
+    init_sigio (connection);
 #endif /* ! defined (SIGIO) */
 
+#ifdef USE_LUCID
+  /* 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;
+    Font font;
+    
+    d.addr = (XPointer)&dpy;
+    d.size = sizeof (Display *);
+    fr.addr = XtDefaultFont;
+    fr.size = sizeof (XtDefaultFont);
+    to.size = sizeof (Font *);
+    to.addr = (XPointer)&font;
+    x_catch_errors (dpy);
+    if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
+      abort ();
+    if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
+      XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
+    x_uncatch_errors (dpy);
+  }
+#endif
+
+
+  UNBLOCK_INPUT;
+
   return dpyinfo;
 }
 \f
@@ -5805,22 +6167,14 @@ x_delete_display (dpyinfo)
          tail->next = tail->next->next;
     }
 
-#ifndef USE_X_TOOLKIT
-  /* I'm told Xt does this itself.  */
+#ifndef USE_X_TOOLKIT   /* I'm told Xt does this itself.  */
+#ifndef AIX            /* On AIX, XCloseDisplay calls this.  */
   XrmDestroyDatabase (dpyinfo->xrdb);
 #endif
+#endif
 #ifdef MULTI_KBOARD
   if (--dpyinfo->kboard->reference_count == 0)
-    {
-      KBOARD **kbp;
-      for (kbp = &all_kboards; *kbp != dpyinfo->kboard;
-          kbp = &(*kbp)->next_kboard)
-       if (*kbp == NULL)
-         abort ();
-      *kbp = dpyinfo->kboard->next_kboard;
-      wipe_kboard (dpyinfo->kboard);
-      xfree (dpyinfo->kboard);
-    }
+    delete_kboard (dpyinfo->kboard);
 #endif
   xfree (dpyinfo->font_table);
   xfree (dpyinfo->x_id_name);
@@ -5866,14 +6220,13 @@ x_initialize ()
 
   x_noop_count = 0;
 
-  x_focus_frame = x_highlight_frame = 0;
-
   /* Try to use interrupt input; if we can't, then start polling.  */
   Fset_input_mode (Qt, Qnil, Qt, Qnil);
 
 #ifdef USE_X_TOOLKIT
   XtToolkitInitialize ();
   Xt_app_con = XtCreateApplicationContext ();
+  XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
 #endif
 
   /* Note that there is no real way portable across R3/R4 to get the
@@ -5897,5 +6250,12 @@ syms_of_xterm ()
 
   staticpro (&last_mouse_scroll_bar);
   last_mouse_scroll_bar = Qnil;
+
+  staticpro (&Qvendor_specific_keysyms);
+  Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
+
+  staticpro (&last_mouse_press_frame);
+  last_mouse_press_frame = Qnil;
 }
-#endif /* ! defined (HAVE_X_WINDOWS) */
+
+#endif /* not HAVE_X_WINDOWS */