*** empty log message ***
[bpt/emacs.git] / src / xterm.c
index 86b66f8..c6fd73c 100644 (file)
@@ -58,24 +58,11 @@ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
 #include <strings.h>
 #else /* ! defined (BSD) */
 #ifndef VMS
-#include <sys/termio.h>
 #include <string.h>
 #endif
 #endif /* ! defined (BSD) */
 
-/* Allow m- file to inhibit use of FIONREAD.  */
-#ifdef BROKEN_FIONREAD
-#undef FIONREAD
-#endif /* ! defined (BROKEN_FIONREAD) */
-
-/* We are unable to use interrupts if FIONREAD is not available,
-   so flush SIGIO so we won't try.  */
-#ifndef FIONREAD
-#ifdef SIGIO
-#undef SIGIO
-#endif /* ! defined (SIGIO) */
-#endif /* FIONREAD */
-
+#include "systty.h"
 #include "systime.h"
 
 #include <fcntl.h>
@@ -106,6 +93,27 @@ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
 /*#include <X/Xproto.h>        */
 #endif /* ! defined (HAVE_X11) */
 
+#ifdef FD_SET
+/* We could get this from param.h, but better not to depend on finding that.
+   And better not to risk that it might define other symbols used in this
+   file.  */
+#ifdef FD_SETSIZE
+#define MAXDESC FD_SETSIZE
+#else
+#define MAXDESC 64
+#endif
+#define SELECT_TYPE fd_set
+#else /* no FD_SET */
+#define MAXDESC 32
+#define SELECT_TYPE int
+
+/* Define the macros to access a single-int bitmap of descriptors.  */
+#define FD_SET(n, p) (*(p) |= (1 << (n)))
+#define FD_CLR(n, p) (*(p) &= ~(1 << (n)))
+#define FD_ISSET(n, p) (*(p) & (1 << (n)))
+#define FD_ZERO(p) (*(p) = 0)
+#endif /* no FD_SET */
+
 /* For sending Meta-characters.  Do we need this? */
 #define METABIT 0200
 
@@ -263,6 +271,7 @@ extern Cursor XCreateCursor ();
 extern FONT_TYPE *XOpenFont ();
 
 static void flashback ();
+static void redraw_previous_char ();
 
 #ifndef HAVE_X11
 static void dumpqueue ();
@@ -280,7 +289,7 @@ static int XTclear_end_of_line ();
    of the frame being updated, so that the XT... functions do not
    need to take a frame as argument.  Most of the XT... functions
    should never be called except during an update, the only exceptions
-   being XTcursor_to, XTwrite_char and XTreassert_line_highlight.  */
+   being XTcursor_to, XTwrite_glyphs and XTreassert_line_highlight.  */
 
 extern int mouse_track_top, mouse_track_left, mouse_track_width;
 
@@ -402,8 +411,8 @@ XTcursor_to (row, col)
    Since the display generation code is responsible for calling
    compute_char_face and compute_glyph_face on everything it puts in
    the display structure, we can assume that the face code on each
-   glyph is a valid index into FRAME_FACES (f), and the one to which
-   we can actually apply intern_face.  */
+   glyph is a valid index into FRAME_COMPUTED_FACES (f), and the one
+   to which we can actually apply intern_face.  */
 
 #if 1
 /* This is the multi-face code.  */
@@ -464,13 +473,14 @@ dumpglyphs (f, left, top, gp, n, hl)
          {
            /* The face codes on the glyphs must be valid indices into the
               frame's face table.  */
-           if (cf < 0 || cf >= FRAME_N_FACES (f))
+           if (cf < 0 || cf >= FRAME_N_COMPUTED_FACES (f)
+               || FRAME_COMPUTED_FACES (f) [cf] == 0)
              abort ();
 
            if (cf == 1)
              face = FRAME_MODE_LINE_FACE (f);
            else
-             face = intern_face (f, FRAME_FACES (f) [cf]);
+             face = intern_face (f, FRAME_COMPUTED_FACES (f) [cf]);
            font = FACE_FONT (face);
            gc = FACE_GC (face);
            defaulted = 0;
@@ -671,7 +681,9 @@ XTclear_end_of_line (first_unused)
              CHAR_TO_PIXEL_ROW (f, curs_y),
              FONT_WIDTH (f->display.x->font) * (first_unused - curs_x),
              FONT_HEIGHT (f->display.x->font), False);
-             
+#if 0
+  redraw_previous_char (f, curs_x, curs_y);
+#endif
 #else /* ! defined (HAVE_X11) */
   XPixSet (FRAME_X_WINDOW (f),
           CHAR_TO_PIXEL_COL (f, curs_x),
@@ -684,6 +696,38 @@ XTclear_end_of_line (first_unused)
   UNBLOCK_INPUT;
 }
 
+/* Erase the character (if any) at the position just before X, Y in frame F,
+   then redraw it and the character before it.
+   This is necessary when we erase starting at X,
+   in case the character after X overlaps into the one before X.  */
+
+static void
+redraw_previous_char (f, x, y)
+     FRAME_PTR f;
+     int x, y;
+{
+  /* Erase the character before the new ones, in case
+     what was here before overlaps it.
+     Reoutput that character, and the previous character
+     (in case the previous character overlaps it).  */
+  if (x > 0)
+    {
+      int start_x = x - 2;
+      if (start_x < 0)
+       start_x = 0;
+      XClearArea (x_current_display, FRAME_X_WINDOW (f),
+                 CHAR_TO_PIXEL_COL (f, x - 1),
+                 CHAR_TO_PIXEL_ROW (f, y),
+                 FONT_WIDTH (f->display.x->font),
+                 FONT_HEIGHT (f->display.x->font), False);
+
+      dumpglyphs (f, CHAR_TO_PIXEL_COL (f, start_x),
+                 CHAR_TO_PIXEL_ROW (f, y),
+                 &FRAME_CURRENT_GLYPHS (f)->glyphs[y][start_x],
+                 x - start_x, highlight);
+    }
+}
+
 static
 XTclear_frame ()
 {
@@ -716,7 +760,7 @@ XTclear_frame ()
 /* Invert the middle quarter of the frame for .15 sec.  */
 
 /* We use the select system call to do the waiting, so we have to make sure
-   it's avaliable.  If it isn't, we just won't do visual bells.  */
+   it's available.  If it isn't, we just won't do visual bells.  */
 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
 
 /* Subtract the `struct timeval' values X and Y,
@@ -1236,9 +1280,14 @@ static void
 frame_highlight (frame)
      struct frame *frame;
 {
-  if (! EQ (Vx_no_window_manager, Qnil))
-    XSetWindowBorder (x_current_display, FRAME_X_WINDOW (frame),
-                     frame->display.x->border_pixel);
+  /* We used to only do this if Vx_no_window_manager was non-nil, but
+     the ICCCM (section 4.1.6) says that the window's border pixmap
+     and border pixel are window attributes which are "private to the
+     client", so we can always change it to whatever we want.  */
+  BLOCK_INPUT;
+  XSetWindowBorder (x_current_display, FRAME_X_WINDOW (frame),
+                   frame->display.x->border_pixel);
+  UNBLOCK_INPUT;
   x_display_cursor (frame, 1);
 }
 
@@ -1246,9 +1295,14 @@ static void
 frame_unhighlight (frame)
      struct frame *frame;
 {
-  if (! EQ (Vx_no_window_manager, Qnil))
-    XSetWindowBorderPixmap (x_current_display, FRAME_X_WINDOW (frame),
-                           frame->display.x->border_tile);
+  /* We used to only do this if Vx_no_window_manager was non-nil, but
+     the ICCCM (section 4.1.6) says that the window's border pixmap
+     and border pixel are window attributes which are "private to the
+     client", so we can always change it to whatever we want.  */
+  BLOCK_INPUT;
+  XSetWindowBorderPixmap (x_current_display, FRAME_X_WINDOW (frame),
+                         frame->display.x->border_tile);
+  UNBLOCK_INPUT;
   x_display_cursor (frame, 1);
 }
 #else /* ! defined (HAVE_X11) */
@@ -1301,7 +1355,7 @@ static void XTframe_rehighlight ();
 /* The focus has changed.  Update the frames as necessary to reflect
    the new situation.  Note that we can't change the selected frame
    here, because the lisp code we are interrupting might become confused.
-   Each event gets marked with the frame in which it occured, so the
+   Each event gets marked with the frame in which it occurred, so the
    lisp code can tell when the switch took place by examining the events.  */
 
 static void
@@ -1368,42 +1422,7 @@ XTframe_rehighlight ()
     }
 }
 \f
-/* Mouse clicks and mouse movement.  Rah.  */
-#ifdef HAVE_X11
-
-/* Given a pixel position (PIX_X, PIX_Y) on the frame F, return
-   glyph co-ordinates in (*X, *Y).  Set *BOUNDS to the rectangle
-   that the glyph at X, Y occupies, if BOUNDS != 0.  */
-static void
-pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds)
-     FRAME_PTR f;
-     register unsigned int pix_x, pix_y;
-     register int *x, *y;
-     XRectangle *bounds;
-{
-  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 = FONT_HEIGHT (f->display.x->font);
-      bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
-      bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
-    }
-
-  if (pix_x < 0) pix_x = 0;
-  else if (pix_x > f->width) pix_x = f->width;
-
-  if (pix_y < 0) pix_y = 0;
-  else if (pix_y > f->height) pix_y = f->height;
-
-  *x = pix_x;
-  *y = pix_y;
-}
-
-/* Any buttons grabbed. */
-unsigned int x_mouse_grabbed;
+/* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
 
 /* Which modifier keys are on which modifier bits?
 
@@ -1553,6 +1572,59 @@ x_emacs_to_x_modifiers (state)
          | ((state & meta_modifier)            ? x_meta_mod_mask  : 0));
 }
 
+/* Return true iff KEYSYM is a vendor-specific keysym that we should
+   return as a function key.  If you add a keysym to this, you should
+   make sure that the tables make_lispy_event uses contain a suitable
+   name for it.  */
+static int
+x_is_vendor_fkey (sym)
+     KeySym sym;
+{
+  return 0
+#ifdef DXK_Remove
+    || (sym == DXK_Remove)
+#endif
+      ;
+}
+
+\f
+/* Mouse clicks and mouse movement.  Rah.  */
+#ifdef HAVE_X11
+
+/* Given a pixel position (PIX_X, PIX_Y) on the frame F, return
+   glyph co-ordinates in (*X, *Y).  Set *BOUNDS to the rectangle
+   that the glyph at X, Y occupies, if BOUNDS != 0.  */
+static void
+pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds)
+     FRAME_PTR f;
+     register unsigned int pix_x, pix_y;
+     register int *x, *y;
+     XRectangle *bounds;
+{
+  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 = FONT_HEIGHT (f->display.x->font);
+      bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
+      bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
+    }
+
+  if (pix_x < 0) pix_x = 0;
+  else if (pix_x > f->width) pix_x = f->width;
+
+  if (pix_y < 0) pix_y = 0;
+  else if (pix_y > f->height) pix_y = f->height;
+
+  *x = pix_x;
+  *y = pix_y;
+}
+
+/* Any buttons grabbed. */
+unsigned int x_mouse_grabbed;
+
 /* Prepare a mouse-event in *RESULT for placement in the input queue.
 
    If the event is a button press, then note that we have grabbed
@@ -1624,7 +1696,7 @@ static XRectangle last_mouse_glyph;
 
 /* The scroll bar in which the last X motion event occurred.
 
-   If the last X motion event occured in a scroll bar, we set this
+   If the last X motion event occurred in a scroll bar, we set this
    so XTmouse_position can know whether to report a scroll bar motion or
    an ordinary motion.
 
@@ -1695,6 +1767,8 @@ static void x_scroll_bar_report_motion ();
    Set *time to the server timestamp for the time at which the mouse
    was at this position.
 
+   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
@@ -1708,6 +1782,8 @@ XTmouse_position (f, bar_window, part, x, y, time)
      Lisp_Object *x, *y;
      unsigned long *time;
 {
+  FRAME_PTR f1;
+
   BLOCK_INPUT;
 
   if (! NILP (last_mouse_scroll_bar))
@@ -1785,28 +1861,32 @@ XTmouse_position (f, bar_window, part, x, y, time)
           never use them in that case.)  */
 
        /* Is win one of our frames?  */
-       *f = x_window_to_frame (win);
+       f1 = x_window_to_frame (win);
       
        /* If not, is it one of our scroll bars?  */
-       if (! *f)
+       if (! f1)
          {
            struct scroll_bar *bar = x_window_to_scroll_bar (win);
 
            if (bar)
              {
-               *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
+               f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
                win_x = parent_x;
                win_y = parent_y;
              }
          }
 
-       if (*f)
+       if (f1)
          {
-           pixel_to_glyph_coords (*f, win_x, win_y, &win_x, &win_y,
+           /* Ok, we found a frame.  Convert from pixels to characters
+              and store all the values.  */
+
+           pixel_to_glyph_coords (f1, win_x, win_y, &win_x, &win_y,
                                   &last_mouse_glyph);
 
            *bar_window = Qnil;
            *part = 0;
+           *f = f1;
            XSET (*x, Lisp_Int, win_x);
            XSET (*y, Lisp_Int, win_y);
            *time = last_mouse_movement_time;
@@ -2022,7 +2102,7 @@ x_scroll_bar_set_handle (bar, start, end, rebuild)
   UNBLOCK_INPUT;
 }
 
-/* Move a scroll bar around on the screen, to accomodate changing
+/* Move a scroll bar around on the screen, to accommodate changing
    window configurations.  */
 static void
 x_scroll_bar_move (bar, top, left, width, height)
@@ -2122,8 +2202,8 @@ XTset_vertical_scroll_bar (window, portion, whole, position)
        x_scroll_bar_set_handle (bar, 0, top_range, 0);
       else
        {
-         int start = (position * top_range) / whole;
-         int end = ((position + portion) * top_range) / whole;
+         int start = ((double) position * top_range) / whole;
+         int end = ((double) (position + portion) * top_range) / whole;
 
          x_scroll_bar_set_handle (bar, start, end, 0);
        }
@@ -2330,7 +2410,7 @@ x_scroll_bar_handle_click (bar, event, emacs_event)
 
     /* Same deal here as the other #if 0.  */
 #if 0
-    /* Clicks on the handle are always reported as occuring at the top of 
+    /* Clicks on the handle are always reported as occurring at the top of 
        the handle.  */
     if (emacs_event->part == scroll_bar_handle)
       emacs_event->x = bar->start;
@@ -2540,7 +2620,7 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
   int items_pending;           /* How many items are in the X queue. */
   XEvent event;
   struct frame *f;
-  int event_found;
+  int event_found = 0;
   int prefix;
   Lisp_Object part;
 
@@ -2816,7 +2896,7 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
              /* The window manager never makes a window invisible
                 ("withdrawn"); all it does is switch between visible
                 and iconified.  Frames get into the invisible state
-                only through x_make_frame_invisible.
+                only through x_make_frame_invisible.  */
              if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
                f->async_iconified = 1;
            }
@@ -2855,8 +2935,8 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
 
          if (f != 0)
            {
-             KeySym keysym;
-             char copy_buffer[80];
+             KeySym keysym, orig_keysym;
+             unsigned char copy_buffer[80];
              int modifiers;
 
              event.xkey.state
@@ -2875,22 +2955,36 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
              /* Strip off the vendor-specific keysym bit, and take a shot
                 at recognizing the codes.  HP servers have extra keysyms
                 that fit into the MiscFunctionKey category.  */
+             orig_keysym = keysym;
              keysym &= ~(1<<28);
 
              if (numchars > 1)
                {
-                 if ((keysym >= XK_BackSpace && keysym <= XK_Escape)
-                     || keysym == XK_Delete
-                     || IsCursorKey (keysym)       /* 0xff50 <= x < 0xff60 */
-                     || IsMiscFunctionKey (keysym) /* 0xff60 <= x < 0xff80 */
+                 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
+                      || keysym == XK_Delete
+                      || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
+                      || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
 #ifdef HPUX
-                     /* This recognizes the "extended function keys".
-                        It seems there's no cleaner way.  */
-                     || ((unsigned) (keysym) >= XK_Select
-                         && (unsigned)(keysym) < XK_KP_Space)
+                      /* This recognizes the "extended function keys".
+                         It seems there's no cleaner way.
+                         Test IsModifierKey to avoid handling mode_switch
+                         incorrectly.  */
+                      || ((unsigned) (keysym) >= XK_Select
+                          && (unsigned)(keysym) < XK_KP_Space)
 #endif
-                     || IsKeypadKey (keysym)       /* 0xff80 <= x < 0xffbe */
-                     || IsFunctionKey (keysym))    /* 0xffbe <= x < 0xffe1 */
+                      || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
+                      || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
+                      || x_is_vendor_fkey (orig_keysym))
+                     && ! (IsModifierKey (orig_keysym)
+#ifndef HAVE_X11R5
+#ifdef XK_Mode_switch
+                           || ((unsigned)(orig_keysym) == XK_Mode_switch)
+#endif
+#ifdef XK_Num_Lock
+                           || ((unsigned)(orig_keysym) == XK_Num_Lock)
+#endif
+#endif /* not HAVE_X11R5 */
+                           ))
                    {
                      if (temp_index == sizeof temp_buffer / sizeof (short))
                        temp_index = 0;
@@ -3121,17 +3215,20 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
                  Window win, child;
                  int win_x, win_y;
 
-                 /* Coords are relative to the parent.
-                    Convert them to root-relative.  */
+                 /* 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 (x_current_display,
                               
                                         /* From-window, to-window.  */
-                                        f->display.x->parent_desc,
+                                        f->display.x->window_desc,
                                         ROOT_WINDOW,
 
                                         /* From-position, to-position.  */
-                                        event.xconfigure.x,
-                                        event.xconfigure.y,
+                                        -event.xconfigure.border_width,
+                                        -event.xconfigure.border_width,
                                         &win_x, &win_y,
 
                                         /* Child of win.  */
@@ -3249,24 +3346,23 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
        }
     }
 
-#if 0
 #ifdef HAVE_SELECT
   if (expected && ! event_found)
     {
       /* AOJ 880406: if select returns true but XPending doesn't, it means that
         there is an EOF condition; in other words, that X has died.
         Act as if there had been a hangup. */
-
       int fd = ConnectionNumber (x_current_display);
-      int mask = 1 << fd;
+      SELECT_TYPE mask;
+      EMACS_TIME timeout;
 
-      if (0 != select (fd + 1, &mask, (long *) 0, (long *) 0,
-                      (EMACS_TIME) 0)
+      FD_SET(fd, &mask);
+      EMACS_SET_SECS_USECS (timeout, 0, 0);
+      if (0 != select (fd + 1, &mask, (long *) 0, (long *) 0, &timeout)
          && !XStuffPending ())
        kill (getpid (), SIGHUP);
     }
 #endif /* ! defined (HAVE_SELECT) */
-#endif /* ! 0 */
 
 #ifndef HAVE_X11
   if (updating_frame == 0)
@@ -3763,7 +3859,7 @@ x_connection_closed ()
   if (_Xdebug)
     abort ();
 
-  shut_down_emacs (0, 1);
+  shut_down_emacs (0, 1, Qnil);
 
   exit (70);
 }
@@ -3785,9 +3881,11 @@ x_error_quitter (display, error)
   fprintf (stderr, "X protocol error: %s on protocol request %d\n",
           buf, error->request_code);
 
+#if 0
   /* While we're testing Emacs 19, we'll just dump core whenever we
      get an X error, so we can figure out why it happened.  */
   abort ();
+#endif
 
   x_connection_closed ();
 }
@@ -3802,15 +3900,18 @@ x_io_error_quitter (display)
   fprintf (stderr, "Connection to X server %s lost.\n",
           XDisplayName (DisplayString (display)));
 
+#if 0
   /* While we're testing Emacs 19, we'll just dump core whenever we
      get an X error, so we can figure out why it happened.  */
   abort ();
+#endif
 
   x_connection_closed ();
 }
 
 /* A buffer for storing X error messages.  */
-static char (*x_caught_error_message)[200];
+static char *x_caught_error_message;
+#define X_CAUGHT_ERROR_MESSAGE_SIZE 200
 
 /* An X error handler which stores the error message in
    x_caught_error_message.  This is what's installed when
@@ -3821,7 +3922,7 @@ x_error_catcher (display, error)
      XErrorEvent *error;
 {
   XGetErrorText (display, error->error_code,
-                *x_caught_error_message, sizeof (*x_caught_error_message));
+                x_caught_error_message, X_CAUGHT_ERROR_MESSAGE_SIZE);
 }
 
 
@@ -3844,9 +3945,9 @@ x_catch_errors ()
   XSync (x_current_display, False);
 
   /* Set up the error buffer.  */
-  x_caught_error_message =
-    (char (*)[]) xmalloc (sizeof (*x_caught_error_message));
-  (*x_caught_error_message)[0] = '\0';
+  x_caught_error_message
+    = (char*) xmalloc (X_CAUGHT_ERROR_MESSAGE_SIZE);
+  x_caught_error_message[0] = '\0';
 
   /* Install our little error handler.  */
   XHandleError (x_error_catcher);
@@ -3862,13 +3963,11 @@ x_check_errors (format)
   /* Make sure to catch any errors incurred so far.  */
   XSync (x_current_display, False);
 
-  if ((*x_caught_error_message)[0])
+  if (x_caught_error_message[0])
     {
-      char buf[256];
-
-      sprintf (buf, format, *x_caught_error_message);
-      xfree (x_caught_error_message);
+      char buf[X_CAUGHT_ERROR_MESSAGE_SIZE + 56];
 
+      sprintf (buf, format, x_caught_error_message);
       x_uncatch_errors ();
       error (buf);
     }
@@ -3878,6 +3977,7 @@ void
 x_uncatch_errors ()
 {
   xfree (x_caught_error_message);
+  x_caught_error_message = 0;
   XHandleError (x_error_quitter);
 }
 
@@ -3898,8 +3998,14 @@ x_trace_wire ()
 
 #ifdef HAVE_X11
 
+struct font_info
+{
+  XFontStruct *font;
+  char *name;
+};
+
 /* A table of all the fonts we have already loaded.  */
-static XFontStruct **x_font_table;
+static struct font_info *x_font_table;
 
 /* The current capacity of x_font_table.  */
 static int x_font_table_size;
@@ -3922,34 +4028,37 @@ x_new_font (f, fontname)
   /* Get a list of all the fonts that match this name.  Once we
      have a list of matching fonts, we compare them against the fonts
      we already have by comparing font ids.  */
-  font_names = (char **) XListFontsWithInfo (x_current_display, fontname,
-                                            1024, &n_matching_fonts,
-                                            &font_info);
+  font_names = (char **) XListFonts (x_current_display, fontname,
+                                    1024, &n_matching_fonts);
+  /* Apparently it doesn't set n_matching_fonts to zero when it can't
+     find any matches; font_names == 0 is the only clue.  */
+  if (! font_names)
+    n_matching_fonts = 0;
 
-  /* If the server couldn't find any fonts whose named matched fontname,
-     return an error code.  */
-  if (n_matching_fonts == 0)
-    return Qnil;
+  /* Don't just give up if n_matching_fonts is 0.
+     Apparently there's a bug on Suns: XListFontsWithInfo can
+     fail to find a font, but XLoadQueryFont may still find it.  */
 
   /* See if we've already loaded a matching font. */
-  {
-    int i, j;
+  already_loaded = -1;
+  if (n_matching_fonts != 0)
+    {
+      int i, j;
 
-    already_loaded = 0;
-    for (i = 0; i < n_fonts; i++)
-      for (j = 0; j < n_matching_fonts; j++)
-       if (x_font_table[i]->fid == font_info[j].fid)
-         {
-           already_loaded = i;
-           fontname = font_names[j];
-           goto found_font;
-         }
-  }
+      for (i = 0; i < n_fonts; i++)
+       for (j = 0; j < n_matching_fonts; j++)
+         if (!strcmp (x_font_table[i].name, font_names[j]))
+           {
+             already_loaded = i;
+             fontname = font_names[j];
+             goto found_font;
+           }
+    }
  found_font:
   
   /* If we have, just return it from the table.  */
-  if (already_loaded)
-    f->display.x->font = x_font_table[already_loaded];
+  if (already_loaded >= 0)
+    f->display.x->font = x_font_table[already_loaded].font;
   
   /* Otherwise, load the font and add it to the table.  */
   else
@@ -3967,36 +4076,42 @@ x_new_font (f, fontname)
       i = 0;
 #endif
 
-      if (i >= n_matching_fonts)
-       return Qt;
-      else
+      /* See comment above.  */
+      if (n_matching_fonts != 0)
        fontname = font_names[i];
 
       font = (XFontStruct *) XLoadQueryFont (x_current_display, fontname);
       if (! font)
-       return Qnil;
+       {
+         /* Free the information from XListFonts.  */
+         if (n_matching_fonts)
+           XFreeFontNames (font_names);
+         return Qnil;
+       }
 
       /* Do we need to create the table?  */
       if (x_font_table_size == 0)
        {
          x_font_table_size = 16;
          x_font_table
-           = (XFontStruct **) xmalloc (x_font_table_size
-                                       * sizeof (x_font_table[0]));
+           = (struct font_info *) xmalloc (x_font_table_size
+                                           * sizeof (x_font_table[0]));
        }
       /* Do we need to grow the table?  */
       else if (n_fonts >= x_font_table_size)
        {
          x_font_table_size *= 2;
          x_font_table
-           = (XFontStruct **) xrealloc (x_font_table,
-                                        (x_font_table_size
-                                         * sizeof (x_font_table[0])));
+           = (struct font_info *) xrealloc (x_font_table,
+                                            (x_font_table_size
+                                             * sizeof (x_font_table[0])));
        }
 
-      f->display.x->font = x_font_table[n_fonts++] = font;
+      x_font_table[n_fonts].name = (char *) xmalloc (strlen (fontname));
+      bcopy (fontname, x_font_table[n_fonts].name, strlen (fontname) + 1);
+      f->display.x->font = x_font_table[n_fonts++].font = font;
     }
-  
+
   /* Now make the frame display the given font.  */
   if (FRAME_X_WINDOW (f) != 0)
     {
@@ -4014,9 +4129,9 @@ x_new_font (f, fontname)
     Lisp_Object lispy_name = build_string (fontname);
 
 
-    /* Free the information from XListFontsWithInfo.  The data
+    /* Free the information from XListFonts.  The data
        we actually retain comes from XLoadQueryFont.  */
-    XFreeFontInfo (font_names, font_info, n_matching_fonts);
+    XFreeFontNames (font_names);
 
     return lispy_name;
   }
@@ -4051,13 +4166,42 @@ x_calc_absolute_position (f)
      struct frame *f;
 {
 #ifdef HAVE_X11
+  Window win, child;
+  int win_x = 0, win_y = 0;
+
+  /* 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 != ROOT_WINDOW)
+    {
+      BLOCK_INPUT;
+      XTranslateCoordinates (x_current_display,
+                              
+                            /* From-window, to-window.  */
+                            f->display.x->window_desc,
+                            f->display.x->parent_desc,
+
+                            /* From-position, to-position.  */
+                            0, 0, &win_x, &win_y,
+
+                            /* Child of win.  */
+                            &child);
+      UNBLOCK_INPUT;
+    }
+
+  /* Treat negative positions as relative to the leftmost bottommost
+     position that fits on the screen.  */
   if (f->display.x->left_pos < 0)
-    f->display.x->left_pos
-      = x_screen_width - PIXEL_WIDTH (f) + f->display.x->left_pos;
+    f->display.x->left_pos = (x_screen_width 
+                             - f->display.x->border_width - win_x
+                             - PIXEL_WIDTH (f)
+                             + f->display.x->left_pos);
 
   if (f->display.x->top_pos < 0)
-    f->display.x->top_pos
-      = x_screen_height - PIXEL_HEIGHT (f) + f->display.x->top_pos;
+    f->display.x->top_pos = (x_screen_height
+                            - f->display.x->border_width - win_y
+                            - PIXEL_HEIGHT (f)
+                            + f->display.x->top_pos);
+
 #else /* ! defined (HAVE_X11) */
   WINDOWINFO_TYPE parentinfo;
 
@@ -4085,7 +4229,7 @@ x_set_offset (f, xoff, yoff)
   XMoveWindow (XDISPLAY FRAME_X_WINDOW (f),
               f->display.x->left_pos, f->display.x->top_pos);
 #ifdef HAVE_X11
-  x_wm_set_size_hint (f, 0);
+  x_wm_set_size_hint (f, 0, xoff, yoff);
 #endif /* ! defined (HAVE_X11) */
   UNBLOCK_INPUT;
 }
@@ -4102,15 +4246,15 @@ x_set_window_size (f, cols, rows)
   BLOCK_INPUT;
 
   check_frame_size (f, &rows, &cols);
-  f->display.x->vertical_scroll_bar_extra =
-    (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
-     ? VERTICAL_SCROLL_BAR_PIXEL_WIDTH (f)
-     : 0);
+  f->display.x->vertical_scroll_bar_extra
+    (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
+       ? VERTICAL_SCROLL_BAR_PIXEL_WIDTH (f)
+       : 0);
   pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
   pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
 
 #ifdef HAVE_X11
-  x_wm_set_size_hint (f, 0);
+  x_wm_set_size_hint (f, 0, 0, 0);
 #endif /* ! defined (HAVE_X11) */
   XChangeWindowSize (FRAME_X_WINDOW (f), pixelwidth, pixelheight);
 
@@ -4301,7 +4445,7 @@ x_make_frame_invisible (f)
                         DefaultScreen (x_current_display)))
     {
       UNBLOCK_INPUT_RESIGNAL;
-      error ("can't notify window manager of window withdrawl");
+      error ("can't notify window manager of window withdrawal");
     }
 
 #else /* ! defined (HAVE_X11R4) */
@@ -4390,6 +4534,14 @@ x_iconify_frame (f)
      IconicState.  */
   x_wm_set_window_state (f, IconicState);
 
+  if (!FRAME_VISIBLE_P (f))
+    {
+      /* If the frame was withdrawn, before, we must map it.  */
+      XMapWindow (XDISPLAY FRAME_X_WINDOW (f));
+      if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
+       XMapSubwindows (x_current_display, FRAME_X_WINDOW (f));
+    }
+
   f->async_iconified = 1;
 #else /* ! defined (HAVE_X11) */
   XUnmapWindow (XDISPLAY FRAME_X_WINDOW (f));
@@ -4507,9 +4659,13 @@ mouse_event_pending_p ()
 
 #ifdef HAVE_X11
 
-x_wm_set_size_hint (f, prompting)
+/* SPEC_X and SPEC_Y are the specified positions.
+   We look only at their sign, to decide the gravity.  */
+
+x_wm_set_size_hint (f, prompting, spec_x, spec_y)
      struct frame *f;
      long prompting;
+     int spec_x, spec_y;
 {
   XSizeHints size_hints;
   Window window = FRAME_X_WINDOW (f);
@@ -4568,7 +4724,8 @@ x_wm_set_size_hint (f, prompting)
     {
       XSizeHints hints;                /* Sometimes I hate X Windows... */
       
-      XGetNormalHints (x_current_display, window, &hints);
+      if (XGetNormalHints (x_current_display, window, &hints) == 0)
+       hints.flags = 0;
       if (hints.flags & PSize)
        size_hints.flags |= PSize;
       if (hints.flags & PPosition)
@@ -4579,6 +4736,23 @@ x_wm_set_size_hint (f, prompting)
        size_hints.flags |= USSize;
     }
 
+  switch (((spec_x < 0) << 1) + (spec_y < 0))
+    {
+    case 0:
+      size_hints.win_gravity = NorthWestGravity;
+      break;
+    case 1:
+      size_hints.win_gravity = SouthWestGravity;
+      break;
+    case 2:
+      size_hints.win_gravity = NorthEastGravity;
+      break;
+    case 3:
+      size_hints.win_gravity = SouthEastGravity;
+      break;
+    }
+  size_hints.flags |= PWinGravity;
+
 #ifdef HAVE_X11R4
   XSetWMNormalHints (x_current_display, window, &size_hints);
 #else
@@ -4638,15 +4812,18 @@ x_term_init (display_name)
 {
   Lisp_Object frame;
   char *defaultvalue;
+#ifndef F_SETOWN_BUG
 #ifdef F_SETOWN
   extern int old_fcntl_owner;
 #endif /* ! defined (F_SETOWN) */
+#endif /* F_SETOWN_BUG */
   
   x_focus_frame = x_highlight_frame = 0;
 
   x_current_display = XOpenDisplay (display_name);
   if (x_current_display == 0)
-    fatal ("X server %s not responding; check the DISPLAY environment variable or use \"-d\"\n",
+    fatal ("X server %s not responding.\n\
+Check the DISPLAY environment variable or use \"-d\"\n",
           display_name);
 
 #ifdef HAVE_X11
@@ -4711,6 +4888,7 @@ x_term_init (display_name)
 
 #endif /* ! defined (HAVE_X11) */
   
+#ifndef F_SETOWN_BUG
 #ifdef F_SETOWN
   old_fcntl_owner = fcntl (0, F_GETOWN, 0);
 #ifdef F_SETOWN_SOCK_NEG
@@ -4719,16 +4897,12 @@ x_term_init (display_name)
   fcntl (0, F_SETOWN, getpid ());
 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
 #endif /* ! defined (F_SETOWN) */
+#endif /* F_SETOWN_BUG */
 
 #ifdef SIGIO
   init_sigio ();
 #endif /* ! defined (SIGIO) */
 
-  /* Must use interrupt input because we cannot otherwise
-     arrange for C-g to be noticed immediately.
-     We cannot connect it to SIGINT.  */
-  Fset_input_mode (Qt, Qnil, Qt, Qnil);
-
   expose_all_windows = 0;
 
   clear_frame_hook = XTclear_frame;
@@ -4763,6 +4937,9 @@ x_term_init (display_name)
                                   off the bottom */
   baud_rate = 19200;
 
+  /* Try to use interrupt input; if we can't, then start polling.  */
+  Fset_input_mode (Qt, Qnil, Qt, Qnil);
+
   /* Note that there is no real way portable across R3/R4 to get the 
      original error handler.  */
   XHandleError (x_error_quitter);