Support for opening X frames from a tty session.
[bpt/emacs.git] / src / dispnew.c
index 49c06c7..81a0f1f 100644 (file)
@@ -1,5 +1,5 @@
 /* Updating of data structures for redisplay.
-   Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 97, 98, 1999, 2000, 2001, 2002
+   Copyright (C) 1985,86,87,88,93,94,95,97,98,1999,2000,01,02,2003
        Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
@@ -31,7 +31,6 @@ Boston, MA 02111-1307, USA.  */
 #include "lisp.h"
 #include "termchar.h"
 #include "termopts.h"
-#include "termhooks.h"
 /* cm.h must come after dispextern.h on Windows.  */
 #include "dispextern.h"
 #include "cm.h"
@@ -39,6 +38,7 @@ Boston, MA 02111-1307, USA.  */
 #include "charset.h"
 #include "keyboard.h"
 #include "frame.h"
+#include "termhooks.h"
 #include "window.h"
 #include "commands.h"
 #include "disptab.h"
@@ -214,9 +214,9 @@ int inverse_video;
 EMACS_INT baud_rate;
 
 /* Either nil or a symbol naming the window system under which Emacs
-   is running.  */
+   creates the first frame.  */
 
-Lisp_Object Vwindow_system;
+Lisp_Object Vinitial_window_system;
 
 /* Version number of X windows: 10, 11 or nil.  */
 
@@ -258,14 +258,6 @@ Lisp_Object selected_frame;
 
 struct frame *last_nonminibuf_frame;
 
-/* Stdio stream being used for copy of all output.  */
-
-FILE *termscript;
-
-/* Structure for info on cursor positioning.  */
-
-struct cm Wcm;
-
 /* 1 means SIGWINCH happened when not safe.  */
 
 int delayed_size_change;
@@ -304,11 +296,6 @@ int glyph_pool_count;
 
 static struct frame *frame_matrix_frame;
 
-/* Current interface for window-based redisplay.  Set from init_xterm.
-   A null value means we are not using window-based redisplay.  */
-
-struct redisplay_interface *rif;
-
 /* Non-zero means that fonts have been loaded since the last glyph
    matrix adjustments.  Redisplay must stop, and glyph matrices must
    be adjusted when this flag becomes non-zero during display.  The
@@ -428,8 +415,8 @@ DEFUN ("dump-redisplay-history", Fdump_redisplay_history,
 
 #else /* GLYPH_DEBUG == 0 */
 
-#define WINDOW_TO_FRAME_VPOS(W, VPOS) ((VPOS) + XFASTINT ((W)->top))
-#define WINDOW_TO_FRAME_HPOS(W, HPOS) ((HPOS) + XFASTINT ((W)->left))
+#define WINDOW_TO_FRAME_VPOS(W, VPOS) ((VPOS) + WINDOW_TOP_EDGE_LINE (W))
+#define WINDOW_TO_FRAME_HPOS(W, HPOS) ((HPOS) + WINDOW_LEFT_EDGE_COL (W))
 
 #endif /* GLYPH_DEBUG == 0 */
 
@@ -576,7 +563,7 @@ margin_glyphs_to_reserve (w, total_glyphs, margin)
 
   if (NUMBERP (margin))
     {
-      int width = XFASTINT (w->width);
+      int width = XFASTINT (w->total_cols);
       double d = max (0, XFLOATINT (margin));
       d = min (width / 2 - 1, d);
       n = (int) ((double) total_glyphs / width * d);
@@ -623,24 +610,27 @@ adjust_glyph_matrix (w, matrix, x, y, dim)
   int header_line_changed_p = 0;
   int header_line_p = 0;
   int left = -1, right = -1;
-  int window_x, window_y, window_width = -1, window_height;
+  int window_width = -1, window_height;
 
-  /* See if W had a header line that has disappeared now, or vice versa.  */
+  /* See if W had a header line that has disappeared now, or vice versa.
+     Get W's size.  */
   if (w)
     {
+      window_box (w, -1, 0, 0, &window_width, &window_height);
+
       header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
       header_line_changed_p = header_line_p != matrix->header_line_p;
     }
   matrix->header_line_p = header_line_p;
 
-  /* Do nothing if MATRIX' size, position, vscroll, and marginal areas
+  /* If POOL is null, MATRIX is a window matrix for window-based redisplay.
+     Do nothing if MATRIX' size, position, vscroll, and marginal areas
      haven't changed.  This optimization is important because preserving
      the matrix means preventing redisplay.  */
   if (matrix->pool == NULL)
     {
-      window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
-      left = margin_glyphs_to_reserve (w, dim.width, w->left_margin_width);
-      right = margin_glyphs_to_reserve (w, dim.width, w->right_margin_width);
+      left = margin_glyphs_to_reserve (w, dim.width, w->left_margin_cols);
+      right = margin_glyphs_to_reserve (w, dim.width, w->right_margin_cols);
       xassert (left >= 0 && right >= 0);
       marginal_areas_changed_p = (left != matrix->left_margin_glyphs
                                  || right != matrix->right_margin_glyphs);
@@ -648,8 +638,8 @@ adjust_glyph_matrix (w, matrix, x, y, dim)
       if (!marginal_areas_changed_p
          && !fonts_changed_p
          && !header_line_changed_p
-         && matrix->window_left_x == XFASTINT (w->left)
-         && matrix->window_top_y == XFASTINT (w->top)
+         && matrix->window_left_col == WINDOW_LEFT_EDGE_COL (w)
+         && matrix->window_top_line == WINDOW_TOP_EDGE_LINE (w)
          && matrix->window_height == window_height
          && matrix->window_vscroll == w->vscroll
          && matrix->window_width == window_width)
@@ -679,9 +669,9 @@ adjust_glyph_matrix (w, matrix, x, y, dim)
       if (w)
        {
          left = margin_glyphs_to_reserve (w, dim.width,
-                                          w->left_margin_width);
+                                          w->left_margin_cols);
          right = margin_glyphs_to_reserve (w, dim.width,
-                                           w->right_margin_width);
+                                           w->right_margin_cols);
        }
       else
        left = right = 0;
@@ -723,7 +713,8 @@ adjust_glyph_matrix (w, matrix, x, y, dim)
   else
     {
       /* If MATRIX->pool is null, MATRIX is responsible for managing
-        its own memory.  Allocate glyph memory from the heap.  */
+        its own memory.  It is a window matrix for window-based redisplay.
+        Allocate glyph memory from the heap.  */
       if (dim.width > matrix->matrix_w
          || new_rows
          || header_line_changed_p
@@ -790,8 +781,8 @@ adjust_glyph_matrix (w, matrix, x, y, dim)
              && !header_line_changed_p
              && new_rows == 0
              && dim.width == matrix->matrix_w
-             && matrix->window_left_x == XFASTINT (w->left)
-             && matrix->window_top_y == XFASTINT (w->top)
+             && matrix->window_left_col == WINDOW_LEFT_EDGE_COL (w)
+             && matrix->window_top_line == WINDOW_TOP_EDGE_LINE (w)
              && matrix->window_width == window_width)
            {
              /* Find the last row in the window.  */
@@ -839,8 +830,8 @@ adjust_glyph_matrix (w, matrix, x, y, dim)
      was last adjusted.  This is used to optimize redisplay above.  */
   if (w)
     {
-      matrix->window_left_x = XFASTINT (w->left);
-      matrix->window_top_y = XFASTINT (w->top);
+      matrix->window_left_col = WINDOW_LEFT_EDGE_COL (w);
+      matrix->window_top_line = WINDOW_TOP_EDGE_LINE (w);
       matrix->window_height = window_height;
       matrix->window_width = window_width;
       matrix->window_vscroll = w->vscroll;
@@ -983,8 +974,8 @@ shift_glyph_matrix (w, matrix, start, end, dy)
   xassert (start >= 0 && start < matrix->nrows);
   xassert (end >= 0 && end <= matrix->nrows);
 
-  min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
-  max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w);
+  min_y = WINDOW_HEADER_LINE_HEIGHT (w);
+  max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (w);
 
   for (; start < end; ++start)
     {
@@ -1143,13 +1134,13 @@ blank_row (w, row, y)
 {
   int min_y, max_y;
 
-  min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
-  max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w);
+  min_y = WINDOW_HEADER_LINE_HEIGHT (w);
+  max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (w);
 
   clear_glyph_row (row);
   row->y = y;
   row->ascent = row->phys_ascent = 0;
-  row->height = row->phys_height = CANON_Y_UNIT (XFRAME (w->frame));
+  row->height = row->phys_height = FRAME_LINE_HEIGHT (XFRAME (w->frame));
   row->visible_height = row->height;
 
   if (row->y < min_y)
@@ -1393,7 +1384,7 @@ line_hash_code (row)
        {
          int c = glyph->u.ch;
          int face_id = glyph->face_id;
-         if (must_write_spaces)
+         if (FRAME_MUST_WRITE_SPACES (SELECTED_FRAME ())) /* XXX Is SELECTED_FRAME OK here? */
            c -= SPACEGLYPH;
          hash = (((hash << 4) + (hash >> 24)) & 0x0fffffff) + c;
          hash = (((hash << 4) + (hash >> 24)) & 0x0fffffff) + face_id;
@@ -1425,7 +1416,7 @@ line_draw_cost (matrix, vpos)
   int glyph_table_len = GLYPH_TABLE_LENGTH;
 
   /* Ignore trailing and leading spaces if we can.  */
-  if (!must_write_spaces)
+  if (!FRAME_MUST_WRITE_SPACES (SELECTED_FRAME ())) /* XXX Is SELECTED_FRAME OK here? */
     {
       /* Skip from the end over trailing spaces.  */
       while (end > beg && CHAR_GLYPH_SPACE_P (*(end - 1)))
@@ -1639,8 +1630,10 @@ realloc_glyph_pool (pool, matrix_dim)
 #if GLYPH_DEBUG
 
 
-/* Flush standard output.  This is sometimes useful to call from
-   the debugger.  */
+/* Flush standard output.  This is sometimes useful to call from the debugger.
+   XXX Maybe this should be changed to flush the current terminal instead of
+   stdout.
+*/
 
 void
 flush_stdout ()
@@ -1905,10 +1898,10 @@ allocate_matrices_for_frame_redisplay (window, x, y, dim_only_p,
              || dim.width != w->desired_matrix->matrix_w
              || dim.height != w->desired_matrix->matrix_h
              || (margin_glyphs_to_reserve (w, dim.width,
-                                           w->right_margin_width)
+                                           w->right_margin_cols)
                  != w->desired_matrix->left_margin_glyphs)
              || (margin_glyphs_to_reserve (w, dim.width,
-                                           w->left_margin_width)
+                                           w->left_margin_cols)
                  != w->desired_matrix->right_margin_glyphs))
            *window_change_flags |= CHANGED_LEAF_MATRIX;
 
@@ -1985,7 +1978,7 @@ required_matrix_height (w)
     }
 #endif /* HAVE_WINDOW_SYSTEM */
 
-  return XINT (w->height);
+  return WINDOW_TOTAL_LINES (w);
 }
 
 
@@ -2000,7 +1993,7 @@ required_matrix_width (w)
   if (FRAME_WINDOW_P (f))
     {
       int ch_width = FRAME_SMALLEST_CHAR_WIDTH (f);
-      int window_pixel_width = XFLOATINT (w->width) * CANON_X_UNIT (f);
+      int window_pixel_width = WINDOW_TOTAL_WIDTH (w);
 
       /* Compute number of glyphs needed in a glyph row.  */
       return (((window_pixel_width + ch_width - 1)
@@ -2013,7 +2006,7 @@ required_matrix_width (w)
     }
 #endif /* HAVE_WINDOW_SYSTEM */
 
-  return XINT (w->width);
+  return XINT (w->total_cols);
 }
 
 
@@ -2098,18 +2091,18 @@ adjust_frame_glyphs_initially ()
   struct frame *sf = SELECTED_FRAME ();
   struct window *root = XWINDOW (sf->root_window);
   struct window *mini = XWINDOW (root->next);
-  int frame_height = FRAME_HEIGHT (sf);
-  int frame_width = FRAME_WIDTH (sf);
+  int frame_lines = FRAME_LINES (sf);
+  int frame_cols = FRAME_COLS (sf);
   int top_margin = FRAME_TOP_MARGIN (sf);
 
   /* Do it for the root window.  */
-  XSETFASTINT (root->top, top_margin);
-  XSETFASTINT (root->width, frame_width);
-  set_window_height (sf->root_window, frame_height - 1 - top_margin, 0);
+  XSETFASTINT (root->top_line, top_margin);
+  XSETFASTINT (root->total_cols, frame_cols);
+  set_window_height (sf->root_window, frame_lines - 1 - top_margin, 0);
 
   /* Do it for the mini-buffer window.  */
-  XSETFASTINT (mini->top, frame_height - 1);
-  XSETFASTINT (mini->width, frame_width);
+  XSETFASTINT (mini->top_line, frame_lines - 1);
+  XSETFASTINT (mini->total_cols, frame_cols);
   set_window_height (root->next, 1, 0);
 
   adjust_frame_glyphs (sf);
@@ -2161,13 +2154,13 @@ fake_current_matrices (window)
          struct glyph_matrix *m = w->current_matrix;
          struct glyph_matrix *fm = f->current_matrix;
 
-         xassert (m->matrix_h == XFASTINT (w->height));
-         xassert (m->matrix_w == XFASTINT (w->width));
+         xassert (m->matrix_h == WINDOW_TOTAL_LINES (w));
+         xassert (m->matrix_w == WINDOW_TOTAL_COLS (w));
 
          for (i = 0; i < m->matrix_h; ++i)
            {
              struct glyph_row *r = m->rows + i;
-             struct glyph_row *fr = fm->rows + i + XFASTINT (w->top);
+             struct glyph_row *fr = fm->rows + i + WINDOW_TOP_EDGE_LINE (w);
 
              xassert (r->glyphs[TEXT_AREA] >= fr->glyphs[TEXT_AREA]
                       && r->glyphs[LAST_AREA] <= fr->glyphs[LAST_AREA]);
@@ -2313,8 +2306,8 @@ adjust_frame_glyphs_for_frame_redisplay (f)
       /* Size of frame matrices must equal size of frame.  Note
         that we are called for X frames with window widths NOT equal
         to the frame width (from CHANGE_FRAME_SIZE_1).  */
-      xassert (matrix_dim.width == FRAME_WIDTH (f)
-              && matrix_dim.height == FRAME_HEIGHT (f));
+      xassert (matrix_dim.width == FRAME_COLS (f)
+              && matrix_dim.height == FRAME_LINES (f));
 
       /* Pointers to glyph memory in glyph rows are exchanged during
         the update phase of redisplay, which means in general that a
@@ -2386,10 +2379,10 @@ adjust_frame_glyphs_for_window_redisplay (f)
 
     /* Set window dimensions to frame dimensions and allocate or
        adjust glyph matrices of W.  */
-    XSETFASTINT (w->top, 0);
-    XSETFASTINT (w->left, 0);
-    XSETFASTINT (w->height, FRAME_MENU_BAR_LINES (f));
-    XSETFASTINT (w->width, FRAME_WINDOW_WIDTH (f));
+    XSETFASTINT (w->top_line, 0);
+    XSETFASTINT (w->left_col, 0);
+    XSETFASTINT (w->total_lines, FRAME_MENU_BAR_LINES (f));
+    XSETFASTINT (w->total_cols, FRAME_TOTAL_COLS (f));
     allocate_matrices_for_window_redisplay (w);
   }
 #endif /* not USE_X_TOOLKIT */
@@ -2407,10 +2400,10 @@ adjust_frame_glyphs_for_window_redisplay (f)
   else
     w = XWINDOW (f->tool_bar_window);
 
-  XSETFASTINT (w->top, FRAME_MENU_BAR_LINES (f));
-  XSETFASTINT (w->left, 0);
-  XSETFASTINT (w->height, FRAME_TOOL_BAR_LINES (f));
-  XSETFASTINT (w->width, FRAME_WINDOW_WIDTH (f));
+  XSETFASTINT (w->top_line, FRAME_MENU_BAR_LINES (f));
+  XSETFASTINT (w->left_col, 0);
+  XSETFASTINT (w->total_lines, FRAME_TOOL_BAR_LINES (f));
+  XSETFASTINT (w->total_cols, FRAME_TOTAL_COLS (f));
   allocate_matrices_for_window_redisplay (w);
 #endif
 }
@@ -3022,14 +3015,14 @@ sync_window_with_frame_matrix_rows (w)
   xassert (NILP (w->hchild) && NILP (w->vchild));
   xassert (!FRAME_WINDOW_P (f));
 
-  left = margin_glyphs_to_reserve (w, 1, w->left_margin_width);
-  right = margin_glyphs_to_reserve (w, 1, w->right_margin_width);
+  left = margin_glyphs_to_reserve (w, 1, w->left_margin_cols);
+  right = margin_glyphs_to_reserve (w, 1, w->right_margin_cols);
   x = w->current_matrix->matrix_x;
   width = w->current_matrix->matrix_w;
 
   window_row = w->current_matrix->rows;
   window_row_end = window_row + w->current_matrix->nrows;
-  frame_row = f->current_matrix->rows + XFASTINT (w->top);
+  frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
 
   for (; window_row < window_row_end; ++window_row, ++frame_row)
     {
@@ -3061,8 +3054,8 @@ frame_row_to_window (w, row)
        found = frame_row_to_window (XWINDOW (w->hchild), row);
       else if (!NILP (w->vchild))
        found = frame_row_to_window (XWINDOW (w->vchild), row);
-      else if (row >= XFASTINT (w->top)
-              && row < XFASTINT (w->top) + XFASTINT (w->height))
+      else if (row >= WINDOW_TOP_EDGE_LINE (w)
+              && row < WINDOW_BOTTOM_EDGE_LINE (w))
        found = w;
 
       w = NILP (w->next) ? 0 : XWINDOW (w->next);
@@ -3267,8 +3260,8 @@ window_to_frame_vpos (w, vpos)
 
   xassert (!FRAME_WINDOW_P (f));
   xassert (vpos >= 0 && vpos <= w->desired_matrix->nrows);
-  vpos += XFASTINT (w->top);
-  xassert (vpos >= 0 && vpos <= FRAME_HEIGHT (f));
+  vpos += WINDOW_TOP_EDGE_LINE (w);
+  xassert (vpos >= 0 && vpos <= FRAME_LINES (f));
   return vpos;
 }
 
@@ -3284,7 +3277,7 @@ window_to_frame_hpos (w, hpos)
   struct frame *f = XFRAME (w->frame);
 
   xassert (!FRAME_WINDOW_P (f));
-  hpos += XFASTINT (w->left);
+  hpos += WINDOW_LEFT_EDGE_COL (w);
   return hpos;
 }
 
@@ -3313,12 +3306,15 @@ DEFUN ("redraw-frame", Fredraw_frame, Sredraw_frame, 1, 1, 0,
     return Qnil;
 
   update_begin (f);
+#ifdef MSDOS
   if (FRAME_MSDOS_P (f))
-    set_terminal_modes ();
+    set_terminal_modes (FRAME_DISPLAY (f));
+#endif
   clear_frame ();
   clear_current_matrices (f);
   update_end (f);
-  fflush (stdout);
+  if (FRAME_TERMCAP_P (f))
+    fflush (TTY_OUTPUT (FRAME_TTY (f)));
   windows_or_buffers_changed++;
   /* Mark all windows as inaccurate, so that every window will have
      its redisplay done.  */
@@ -3458,7 +3454,7 @@ direct_output_for_insert (g)
 
   /* If we can't insert glyphs, we can use this method only
      at the end of a line.  */
-  if (!char_ins_del_ok)
+  if (!FRAME_CHAR_INS_DEL_OK (f))
     if (PT != ZV && FETCH_BYTE (PT_BYTE) != '\n')
       return 0;
 
@@ -3608,17 +3604,17 @@ direct_output_for_insert (g)
   updated_row = glyph_row;
   updated_area = TEXT_AREA;
   update_begin (f);
-  if (rif)
+  if (FRAME_RIF (f))
     {
-      rif->update_window_begin_hook (w);
+      FRAME_RIF (f)->update_window_begin_hook (w);
 
       if (glyphs == end - n
          /* In front of a space added by append_space.  */
          || (glyphs == end - n - 1
              && (end - n)->charpos <= 0))
-       rif->write_glyphs (glyphs, n);
+       FRAME_RIF (f)->write_glyphs (glyphs, n);
       else
-       rif->insert_glyphs (glyphs, n);
+       FRAME_RIF (f)->insert_glyphs (glyphs, n);
     }
   else
     {
@@ -3638,24 +3634,25 @@ direct_output_for_insert (g)
      a frame matrix is used, cursor_to expects frame coordinates,
      and the X and Y parameters are not used.  */
   if (window_redisplay_p)
-    rif->cursor_to (w->cursor.vpos, w->cursor.hpos,
-                   w->cursor.y, w->cursor.x);
+    FRAME_RIF (f)->cursor_to (w->cursor.vpos, w->cursor.hpos,
+                              w->cursor.y, w->cursor.x);
   else
     {
       int x, y;
       x = (WINDOW_TO_FRAME_HPOS (w, w->cursor.hpos)
-          + (INTEGERP (w->left_margin_width)
-             ? XFASTINT (w->left_margin_width)
+          + (INTEGERP (w->left_margin_cols)
+             ? XFASTINT (w->left_margin_cols)
              : 0));
       y = WINDOW_TO_FRAME_VPOS (w, w->cursor.vpos);
       cursor_to (y, x);
     }
 
-  if (rif)
-    rif->update_window_end_hook (w, 1, 0);
+  if (FRAME_RIF (f))
+    FRAME_RIF (f)->update_window_end_hook (w, 1, 0);
   update_end (f);
   updated_row = NULL;
-  fflush (stdout);
+  if (FRAME_TERMCAP_P (f))
+    fflush (TTY_OUTPUT (FRAME_TTY (f)));
 
   TRACE ((stderr, "direct output for insert\n"));
   mark_window_display_accurate (it.window, 1);
@@ -3733,20 +3730,21 @@ direct_output_forward_char (n)
           && w->cursor.hpos < w->desired_matrix->matrix_w);
 
   if (FRAME_WINDOW_P (f))
-    rif->cursor_to (w->cursor.vpos, w->cursor.hpos,
-                   w->cursor.y, w->cursor.x);
+    FRAME_RIF (f)->cursor_to (w->cursor.vpos, w->cursor.hpos,
+                              w->cursor.y, w->cursor.x);
   else
     {
       int x, y;
       x = (WINDOW_TO_FRAME_HPOS (w, w->cursor.hpos)
-          + (INTEGERP (w->left_margin_width)
-             ? XFASTINT (w->left_margin_width)
+          + (INTEGERP (w->left_margin_cols)
+             ? XFASTINT (w->left_margin_cols)
              : 0));
       y = WINDOW_TO_FRAME_VPOS (w, w->cursor.vpos);
       cursor_to (y, x);
     }
 
-  fflush (stdout);
+  if (FRAME_TERMCAP_P (f))
+    fflush (TTY_OUTPUT (FRAME_TTY (f)));
   redisplay_performed_directly_p = 1;
   return 1;
 }
@@ -3820,7 +3818,7 @@ update_frame (f, force_p, inhibit_hairy_id_p)
 
 #if 0 /* This flush is a performance bottleneck under X,
         and it doesn't seem to be necessary anyway.  */
-      rif->flush_display (f);
+      FRAME_RIF (f)->flush_display (f);
 #endif
     }
   else
@@ -3837,9 +3835,12 @@ update_frame (f, force_p, inhibit_hairy_id_p)
       paused_p = update_frame_1 (f, force_p, inhibit_hairy_id_p);
       update_end (f);
 
-      if (termscript)
-       fflush (termscript);
-      fflush (stdout);
+      if (FRAME_TERMCAP_P (f))
+        {
+          if (TTY_TERMSCRIPT (FRAME_TTY (f)))
+            fflush (TTY_TERMSCRIPT (FRAME_TTY (f)));
+          fflush (TTY_OUTPUT (FRAME_TTY (f)));
+        }
 
       /* Check window matrices for lost pointers.  */
 #if GLYPH_DEBUG
@@ -3922,7 +3923,8 @@ redraw_overlapped_rows (w, yb)
      int yb;
 {
   int i;
-
+  struct frame *f = XFRAME (WINDOW_FRAME (w));
+  
   /* If rows overlapping others have been changed, the rows being
      overlapped have to be redrawn.  This won't draw lines that have
      already been drawn in update_window_line because overlapped_p in
@@ -3945,10 +3947,12 @@ redraw_overlapped_rows (w, yb)
            {
              updated_row = row;
              updated_area = area;
-             rif->cursor_to (i, 0, row->y, area == TEXT_AREA ? row->x : 0);
+             FRAME_RIF (f)->cursor_to (i, 0, row->y,
+                                        area == TEXT_AREA ? row->x : 0);
              if (row->used[area])
-               rif->write_glyphs (row->glyphs[area], row->used[area]);
-             rif->clear_end_of_line (-1);
+               FRAME_RIF (f)->write_glyphs (row->glyphs[area],
+                                             row->used[area]);
+             FRAME_RIF (f)->clear_end_of_line (-1);
            }
 
          row->overlapped_p = 0;
@@ -3970,7 +3974,8 @@ redraw_overlapping_rows (w, yb)
 {
   int i, bottom_y;
   struct glyph_row *row;
-
+  struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
+  
   for (i = 0; i < w->current_matrix->nrows; ++i)
     {
       row = w->current_matrix->rows + i;
@@ -4050,6 +4055,7 @@ update_window (w, force_p)
 #if GLYPH_DEBUG
   struct frame *f = XFRAME (WINDOW_FRAME (w));
 #endif
+  struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
 
   /* Check that W's frame doesn't have glyph matrices.  */
   xassert (FRAME_WINDOW_P (f));
@@ -4217,6 +4223,7 @@ update_marginal_area (w, area, vpos)
      int area, vpos;
 {
   struct glyph_row *desired_row = MATRIX_ROW (w->desired_matrix, vpos);
+  struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
 
   /* Let functions in xterm.c know what area subsequent X positions
      will be relative to.  */
@@ -4242,6 +4249,7 @@ update_text_area (w, vpos)
 {
   struct glyph_row *current_row = MATRIX_ROW (w->current_matrix, vpos);
   struct glyph_row *desired_row = MATRIX_ROW (w->desired_matrix, vpos);
+  struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
   int changed_p = 0;
 
   /* Let functions in xterm.c know what area subsequent X positions
@@ -4417,7 +4425,7 @@ update_text_area (w, vpos)
          /* If old row extends to the end of the text area, clear.  */
          if (i >= desired_row->used[TEXT_AREA])
            rif->cursor_to (vpos, i, desired_row->y,
-                           desired_row->x + desired_row->pixel_width);
+                           desired_row->pixel_width);
          rif->clear_end_of_line (-1);
          changed_p = 1;
        }
@@ -4429,7 +4437,7 @@ update_text_area (w, vpos)
 
          if (i >= desired_row->used[TEXT_AREA])
            rif->cursor_to (vpos, i, desired_row->y,
-                           desired_row->x + desired_row->pixel_width);
+                           desired_row->pixel_width);
 
          /* If cursor is displayed at the end of the line, make sure
             it's cleared.  Nowadays we don't have a phys_cursor_glyph
@@ -4443,7 +4451,7 @@ update_text_area (w, vpos)
              x = -1;
            }
          else
-           x = current_row->x + current_row->pixel_width;
+           x = current_row->pixel_width;
          rif->clear_end_of_line (x);
          changed_p = 1;
        }
@@ -4463,6 +4471,7 @@ update_window_line (w, vpos, mouse_face_overwritten_p)
 {
   struct glyph_row *current_row = MATRIX_ROW (w->current_matrix, vpos);
   struct glyph_row *desired_row = MATRIX_ROW (w->desired_matrix, vpos);
+  struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
   int changed_p = 0;
 
   /* Set the row being updated.  This is important to let xterm.c
@@ -4480,7 +4489,7 @@ update_window_line (w, vpos, mouse_face_overwritten_p)
 
       /* Update display of the left margin area, if there is one.  */
       if (!desired_row->full_width_p
-         && !NILP (w->left_margin_width))
+         && !NILP (w->left_margin_cols))
        {
          changed_p = 1;
          update_marginal_area (w, LEFT_MARGIN_AREA, vpos);
@@ -4496,7 +4505,7 @@ update_window_line (w, vpos, mouse_face_overwritten_p)
 
       /* Update display of the right margin area, if there is one.  */
       if (!desired_row->full_width_p
-         && !NILP (w->right_margin_width))
+         && !NILP (w->right_margin_cols))
        {
          changed_p = 1;
          update_marginal_area (w, RIGHT_MARGIN_AREA, vpos);
@@ -4533,6 +4542,7 @@ set_window_cursor_after_update (w)
      struct window *w;
 {
   struct frame *f = XFRAME (w->frame);
+  struct redisplay_interface *rif = FRAME_RIF (f);
   int cx, cy, vpos, hpos;
 
   /* Not intended for frame matrix updates.  */
@@ -4756,6 +4766,7 @@ scrolling_window (w, header_line_p)
   int i, j, first_old, first_new, last_old, last_new;
   int nruns, nbytes, n, run_idx;
   struct row_entry *entry;
+  struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
 
   /* Skip over rows equal at the start.  */
   for (i = header_line_p ? 1 : 0; i < current_matrix->nrows - 1; ++i)
@@ -5071,7 +5082,7 @@ update_frame_1 (f, force_p, inhibit_id_p)
     }
 
   /* If we cannot insert/delete lines, it's no use trying it.  */
-  if (!line_ins_del_ok)
+  if (!FRAME_LINE_INS_DEL_OK (f))
     inhibit_id_p = 1;
 
   /* See if any of the desired lines are enabled; don't compute for
@@ -5099,18 +5110,18 @@ update_frame_1 (f, force_p, inhibit_id_p)
                 Also flush out if likely to have more than 1k buffered
                 otherwise.   I'm told that some telnet connections get
                 really screwed by more than 1k output at once.  */
-             int outq = PENDING_OUTPUT_COUNT (stdout);
+             int outq = PENDING_OUTPUT_COUNT (TTY_OUTPUT (FRAME_TTY (f)));
              if (outq > 900
                  || (outq > 20 && ((i - 1) % preempt_count == 0)))
                {
-                 fflush (stdout);
+                 fflush (TTY_OUTPUT (FRAME_TTY (f)));
                  if (preempt_count == 1)
                    {
 #ifdef EMACS_OUTQSIZE
                      if (EMACS_OUTQSIZE (0, &outq) < 0)
                        /* Probably not a tty.  Ignore the error and reset
                           the outq count.  */
-                       outq = PENDING_OUTPUT_COUNT (stdout);
+                       outq = PENDING_OUTPUT_COUNT (TTY_OUTPUT (FRAME_TTY (f)));
 #endif
                      outq *= 10;
                      if (baud_rate <= outq && baud_rate > 0)
@@ -5126,7 +5137,7 @@ update_frame_1 (f, force_p, inhibit_id_p)
        }
     }
 
-  pause = (i < FRAME_HEIGHT (f) - 1) ? i : 0;
+  pause = (i < FRAME_LINES (f) - 1) ? i : 0;
 
   /* Now just clean up termcap drivers and set cursor, etc.  */
   if (!pause)
@@ -5143,7 +5154,7 @@ update_frame_1 (f, force_p, inhibit_id_p)
          && FRAME_HAS_MINIBUF_P (f)
          && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
        {
-         int top = XINT (XWINDOW (FRAME_MINIBUF_WINDOW (f))->top);
+         int top = WINDOW_TOP_EDGE_LINE (XWINDOW (FRAME_MINIBUF_WINDOW (f)));
          int row, col;
 
          if (cursor_in_echo_area < 0)
@@ -5159,7 +5170,7 @@ update_frame_1 (f, force_p, inhibit_id_p)
                 cursor at the end of the prompt.  If the mini-buffer
                 is several lines high, find the last line that has
                 any text on it.  */
-             row = FRAME_HEIGHT (f);
+             row = FRAME_LINES (f);
              do
                {
                  --row;
@@ -5187,9 +5198,9 @@ update_frame_1 (f, force_p, inhibit_id_p)
              if (col >= FRAME_CURSOR_X_LIMIT (f))
                {
                  /* If we have another row, advance cursor into it.  */
-                 if (row < FRAME_HEIGHT (f) - 1)
+                 if (row < FRAME_LINES (f) - 1)
                    {
-                     col = FRAME_LEFT_SCROLL_BAR_WIDTH (f);
+                     col = FRAME_LEFT_SCROLL_BAR_COLS (f);
                      row++;
                    }
                  /* Otherwise move it back in range.  */
@@ -5211,15 +5222,15 @@ update_frame_1 (f, force_p, inhibit_id_p)
                 with the cursor in the lower half of it.  The window
                 is split, and a message causes a redisplay before
                 a new cursor position has been computed.  */
-             && w->cursor.vpos < XFASTINT (w->height))
+             && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
            {
              int x = WINDOW_TO_FRAME_HPOS (w, w->cursor.hpos);
              int y = WINDOW_TO_FRAME_VPOS (w, w->cursor.vpos);
 
-             if (INTEGERP (w->left_margin_width))
-               x += XFASTINT (w->left_margin_width);
+             if (INTEGERP (w->left_margin_cols))
+               x += XFASTINT (w->left_margin_cols);
 
-             /* x = max (min (x, FRAME_WINDOW_WIDTH (f) - 1), 0); */
+             /* x = max (min (x, FRAME_TOTAL_COLS (f) - 1), 0); */
              cursor_to (y, x);
            }
        }
@@ -5241,12 +5252,12 @@ scrolling (frame)
   int unchanged_at_top, unchanged_at_bottom;
   int window_size;
   int changed_lines;
-  int *old_hash = (int *) alloca (FRAME_HEIGHT (frame) * sizeof (int));
-  int *new_hash = (int *) alloca (FRAME_HEIGHT (frame) * sizeof (int));
-  int *draw_cost = (int *) alloca (FRAME_HEIGHT (frame) * sizeof (int));
-  int *old_draw_cost = (int *) alloca (FRAME_HEIGHT (frame) * sizeof (int));
+  int *old_hash = (int *) alloca (FRAME_LINES (frame) * sizeof (int));
+  int *new_hash = (int *) alloca (FRAME_LINES (frame) * sizeof (int));
+  int *draw_cost = (int *) alloca (FRAME_LINES (frame) * sizeof (int));
+  int *old_draw_cost = (int *) alloca (FRAME_LINES (frame) * sizeof (int));
   register int i;
-  int free_at_end_vpos = FRAME_HEIGHT (frame);
+  int free_at_end_vpos = FRAME_LINES (frame);
   struct glyph_matrix *current_matrix = frame->current_matrix;
   struct glyph_matrix *desired_matrix = frame->desired_matrix;
 
@@ -5258,8 +5269,8 @@ scrolling (frame)
      number of unchanged lines at the end.  */
   changed_lines = 0;
   unchanged_at_top = 0;
-  unchanged_at_bottom = FRAME_HEIGHT (frame);
-  for (i = 0; i < FRAME_HEIGHT (frame); i++)
+  unchanged_at_bottom = FRAME_LINES (frame);
+  for (i = 0; i < FRAME_LINES (frame); i++)
     {
       /* Give up on this scrolling if some old lines are not enabled.  */
       if (!MATRIX_ROW_ENABLED_P (current_matrix, i))
@@ -5281,7 +5292,7 @@ scrolling (frame)
       if (old_hash[i] != new_hash[i])
        {
          changed_lines++;
-         unchanged_at_bottom = FRAME_HEIGHT (frame) - i - 1;
+         unchanged_at_bottom = FRAME_LINES (frame) - i - 1;
        }
       else if (i == unchanged_at_top)
        unchanged_at_top++;
@@ -5289,24 +5300,26 @@ scrolling (frame)
     }
 
   /* If changed lines are few, don't allow preemption, don't scroll.  */
-  if ((!scroll_region_ok && changed_lines < baud_rate / 2400)
-      || unchanged_at_bottom == FRAME_HEIGHT (frame))
+  if ((!FRAME_SCROLL_REGION_OK (frame)
+       && changed_lines < baud_rate / 2400)
+      || unchanged_at_bottom == FRAME_LINES (frame))
     return 1;
 
-  window_size = (FRAME_HEIGHT (frame) - unchanged_at_top
+  window_size = (FRAME_LINES (frame) - unchanged_at_top
                 - unchanged_at_bottom);
 
-  if (scroll_region_ok)
+  if (FRAME_SCROLL_REGION_OK (frame))
     free_at_end_vpos -= unchanged_at_bottom;
-  else if (memory_below_frame)
+  else if (FRAME_MEMORY_BELOW_FRAME (frame))
     free_at_end_vpos = -1;
 
   /* If large window, fast terminal and few lines in common between
      current frame and desired frame, don't bother with i/d calc.  */
-  if (!scroll_region_ok && window_size >= 18 && baud_rate > 2400
+  if (!FRAME_SCROLL_REGION_OK (frame)
+      && window_size >= 18 && baud_rate > 2400
       && (window_size >=
          10 * scrolling_max_lines_saved (unchanged_at_top,
-                                         FRAME_HEIGHT (frame) - unchanged_at_bottom,
+                                         FRAME_LINES (frame) - unchanged_at_bottom,
                                          old_hash, new_hash, draw_cost)))
     return 0;
 
@@ -5365,7 +5378,7 @@ count_match (str1, end1, str2, end2)
 /* Char insertion/deletion cost vector, from term.c */
 
 extern int *char_ins_del_vector;
-#define char_ins_del_cost(f) (&char_ins_del_vector[FRAME_WINDOW_WIDTH((f))])
+#define char_ins_del_cost(f) (&char_ins_del_vector[FRAME_TOTAL_COLS((f))])
 
 
 /* Perform a frame-based update on line VPOS in frame FRAME.  */
@@ -5383,7 +5396,7 @@ update_frame_line (f, vpos)
   struct glyph_row *current_row = MATRIX_ROW (current_matrix, vpos);
   struct glyph_row *desired_row = MATRIX_ROW (desired_matrix, vpos);
   int must_write_whole_line_p;
-  int write_spaces_p = must_write_spaces;
+  int write_spaces_p = FRAME_MUST_WRITE_SPACES (f);
   int colored_spaces_p = (FACE_FROM_ID (f, DEFAULT_FACE_ID)->background
                          != FACE_TTY_DEFAULT_BG_COLOR);
 
@@ -5441,10 +5454,10 @@ update_frame_line (f, vpos)
       /* Don't call clear_end_of_line if we already wrote the whole
         line.  The cursor will not be at the right margin in that
         case but in the line below.  */
-      if (nlen < FRAME_WINDOW_WIDTH (f))
+      if (nlen < FRAME_TOTAL_COLS (f))
        {
          cursor_to (vpos, nlen);
-          clear_end_of_line (FRAME_WINDOW_WIDTH (f));
+          clear_end_of_line (FRAME_TOTAL_COLS (f));
        }
       else
        /* Make sure we are in the right row, otherwise cursor movement
@@ -5462,7 +5475,7 @@ update_frame_line (f, vpos)
       nlen--;
 
   /* If there's no i/d char, quickly do the best we can without it.  */
-  if (!char_ins_del_ok)
+  if (!FRAME_CHAR_INS_DEL_OK (f))
     {
       int i, j;
 
@@ -5565,7 +5578,8 @@ update_frame_line (f, vpos)
 
   tem = (nlen - nsp) - (olen - osp);
   if (endmatch && tem
-      && (!char_ins_del_ok || endmatch <= char_ins_del_cost (f)[tem]))
+      && (!FRAME_CHAR_INS_DEL_OK (f)
+          || endmatch <= char_ins_del_cost (f)[tem]))
     endmatch = 0;
 
   /* nsp - osp is the distance to insert or delete.
@@ -5574,7 +5588,7 @@ update_frame_line (f, vpos)
      Is it worth it?  */
 
   if (nsp != osp
-      && (!char_ins_del_ok
+      && (!FRAME_CHAR_INS_DEL_OK (f)
          || begmatch + endmatch <= char_ins_del_cost (f)[nsp - osp]))
     {
       begmatch = 0;
@@ -5615,7 +5629,7 @@ update_frame_line (f, vpos)
             no need to do clear-to-eol at the end of this function
             (and it would not be safe, since cursor is not going to
             be "at the margin" after the text is done).  */
-         if (nlen == FRAME_WINDOW_WIDTH (f))
+         if (nlen == FRAME_TOTAL_COLS (f))
            olen = 0;
 
          /* Function write_glyphs is prepared to do nothing
@@ -5683,21 +5697,25 @@ update_frame_line (f, vpos)
  ***********************************************************************/
 
 /* Determine what's under window-relative pixel position (*X, *Y).
-   Return in *OBJECT the object (string or buffer) that's there.
-   Return in *POS the position in that object. Adjust *X and *Y
-   to character boundaries.  */
+   Return the object (string or buffer) that's there.
+   Return in *POS the position in that object.
+   Adjust *X and *Y to character positions.  */
 
-void
-buffer_posn_from_coords (w, x, y, object, pos)
+Lisp_Object
+buffer_posn_from_coords (w, x, y, pos, object, dx, dy, width, height)
      struct window *w;
      int *x, *y;
-     Lisp_Object *object;
      struct display_pos *pos;
+     Lisp_Object *object;
+     int *dx, *dy;
+     int *width, *height;
 {
   struct it it;
   struct buffer *old_current_buffer = current_buffer;
   struct text_pos startp;
-  int left_area_width;
+  Lisp_Object string;
+  struct glyph_row *row;
+  int x0, x1;
 
   current_buffer = XBUFFER (w->buffer);
   SET_TEXT_POS_FROM_MARKER (startp, w->start);
@@ -5705,63 +5723,135 @@ buffer_posn_from_coords (w, x, y, object, pos)
   BYTEPOS (startp) = min (ZV_BYTE, max (BEGV_BYTE, BYTEPOS (startp)));
   start_display (&it, w, startp);
 
-  left_area_width = WINDOW_DISPLAY_LEFT_AREA_PIXEL_WIDTH (w);
-  move_it_to (&it, -1, *x + it.first_visible_x - left_area_width, *y, -1,
+  x0 = *x - WINDOW_LEFT_MARGIN_WIDTH (w);
+  move_it_to (&it, -1, x0 + it.first_visible_x, *y, -1,
              MOVE_TO_X | MOVE_TO_Y);
 
-  *x = it.current_x - it.first_visible_x + left_area_width;
-  *y = it.current_y;
   current_buffer = old_current_buffer;
 
-  *object = STRINGP (it.string) ? it.string : w->buffer;
+  *dx = x0 + it.first_visible_x - it.current_x;
+  *dy = *y - it.current_y;
+
+  string =  w->buffer;
+  if (STRINGP (it.string))
+    string = it.string;
   *pos = it.current;
+
+#ifdef HAVE_WINDOW_SYSTEM
+  if (it.what == IT_IMAGE)
+    {
+      struct image *img;
+      if ((img = IMAGE_FROM_ID (it.f, it.image_id)) != NULL
+         && !NILP (img->spec))
+       *object = img->spec;
+    }
+#endif
+
+  row = MATRIX_ROW (w->current_matrix, it.vpos);
+  if (row->enabled_p)
+    {
+      if (it.hpos < row->used[TEXT_AREA])
+       {
+         struct glyph *glyph = row->glyphs[TEXT_AREA] + it.hpos;
+         *width = glyph->pixel_width;
+         *height = glyph->ascent + glyph->descent;
+#ifdef HAVE_WINDOW_SYSTEM
+         if (glyph->type == IMAGE_GLYPH)
+           *dy -= row->ascent - glyph->ascent;
+#endif
+       }
+      else
+       {
+         *width = 0;
+         *height = row->height;
+       }
+    }
+  else
+    {
+      *width = *height = 0;
+    }
+
+  /* Add extra (default width) columns if clicked after EOL. */
+  x1 = max(0, it.current_x + it.pixel_width - it.first_visible_x);
+  if (x0 > x1)
+    it.hpos += (x0 - x1) / WINDOW_FRAME_COLUMN_WIDTH (w);
+
+  *x = it.hpos;
+  *y = it.vpos;
+
+  return string;
 }
 
 
 /* Value is the string under window-relative coordinates X/Y in the
-   mode or header line of window W, or nil if none.  MODE_LINE_P non-zero
-   means look at the mode line.  *CHARPOS is set to the position in
-   the string returned.  */
+   mode line or header line (PART says which) of window W, or nil if none.
+   *CHARPOS is set to the position in the string returned.  */
 
 Lisp_Object
-mode_line_string (w, x, y, part, charpos)
+mode_line_string (w, part, x, y, charpos, object, dx, dy, width, height)
      struct window *w;
-     int x, y;
      enum window_part part;
+     int *x, *y;
      int *charpos;
+     Lisp_Object *object;
+     int *dx, *dy;
+     int *width, *height;
 {
   struct glyph_row *row;
   struct glyph *glyph, *end;
-  struct frame *f = XFRAME (w->frame);
-  int x0;
+  int x0, y0;
   Lisp_Object string = Qnil;
 
   if (part == ON_MODE_LINE)
     row = MATRIX_MODE_LINE_ROW (w->current_matrix);
   else
     row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
+  y0 = *y - row->y;
+  *y = row - MATRIX_FIRST_TEXT_ROW (w->current_matrix);
 
   if (row->mode_line_p && row->enabled_p)
     {
-      /* The mode lines are displayed over scroll bars and fringes,
-        and X is window-relative.  Correct X by the scroll bar
-        and fringe width.  */
-      if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (f))
-       x += FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
-      x += FRAME_LEFT_FRINGE_WIDTH (f);
-
       /* Find the glyph under X.  If we find one with a string object,
          it's the one we were looking for.  */
       glyph = row->glyphs[TEXT_AREA];
       end = glyph + row->used[TEXT_AREA];
-      for (x0 = 0; glyph < end; x0 += glyph->pixel_width, ++glyph)
-       if (x >= x0 && x < x0 + glyph->pixel_width)
-         {
-           string = glyph->object;
-           *charpos = glyph->charpos;
-           break;
-         }
+      for (x0 = *x; glyph < end && x0 > glyph->pixel_width; ++glyph)
+       x0 -= glyph->pixel_width;
+      *x = glyph - row->glyphs[TEXT_AREA];
+      if (glyph < end)
+       {
+         string = glyph->object;
+         *charpos = glyph->charpos;
+         *width = glyph->pixel_width;
+         *height = glyph->ascent + glyph->descent;
+#ifdef HAVE_WINDOW_SYSTEM
+         if (glyph->type == IMAGE_GLYPH)
+           {
+             struct image *img;
+             img = IMAGE_FROM_ID (WINDOW_XFRAME (w), glyph->u.img_id);
+             if (img != NULL)
+               *object = img->spec;
+             y0 -= row->ascent - glyph->ascent;
+           }
+#endif
+       }
+      else
+       {
+         /* Add extra (default width) columns if clicked after EOL. */
+         *x += x0 / WINDOW_FRAME_COLUMN_WIDTH (w);
+         *width = 0;
+         *height = row->height;
+       }
     }
+  else
+    {
+      *x = 0;
+      x0 = 0;
+      *width = *height = 0;
+    }
+
+  *dx = x0;
+  *dy = y0;
 
   return string;
 }
@@ -5772,15 +5862,18 @@ mode_line_string (w, x, y, part, charpos)
    the string returned.  */
 
 Lisp_Object
-marginal_area_string (w, x, y, part, charpos)
+marginal_area_string (w, part, x, y, charpos, object, dx, dy, width, height)
      struct window *w;
-     int x, y;
      enum window_part part;
+     int *x, *y;
      int *charpos;
+     Lisp_Object *object;
+     int *dx, *dy;
+     int *width, *height;
 {
   struct glyph_row *row = w->current_matrix->rows;
   struct glyph *glyph, *end;
-  int x0, i, wy = y;
+  int x0, y0, i, wy = *y;
   int area;
   Lisp_Object string = Qnil;
 
@@ -5794,26 +5887,63 @@ marginal_area_string (w, x, y, part, charpos)
   for (i = 0; row->enabled_p && i < w->current_matrix->nrows; ++i, ++row)
     if (wy >= row->y && wy < MATRIX_ROW_BOTTOM_Y (row))
       break;
+  y0 = *y - row->y;
+  *y = row - MATRIX_FIRST_TEXT_ROW (w->current_matrix);
 
   if (row->enabled_p)
     {
       /* Find the glyph under X.  If we find one with a string object,
         it's the one we were looking for.  */
+      if (area == RIGHT_MARGIN_AREA)
+       x0 = ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
+              ? WINDOW_LEFT_FRINGE_WIDTH (w) 
+              : WINDOW_TOTAL_FRINGE_WIDTH (w))
+             + window_box_width (w, LEFT_MARGIN_AREA)
+             + window_box_width (w, TEXT_AREA));
+      else
+       x0 = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
+             ? WINDOW_LEFT_FRINGE_WIDTH (w) 
+             : 0);
+
       glyph = row->glyphs[area];
       end = glyph + row->used[area];
-      if (area == RIGHT_MARGIN_AREA)
-       x0 = (window_box_width (w, TEXT_AREA)
-             + window_box_width (w, LEFT_MARGIN_AREA));
+      for (x0 = *x - x0; glyph < end && x0 > glyph->pixel_width; ++glyph)
+       x0 -= glyph->pixel_width;
+      *x = glyph - row->glyphs[area];
+      if (glyph < end)
+       {
+         string = glyph->object;
+         *charpos = glyph->charpos;
+         *width = glyph->pixel_width;
+         *height = glyph->ascent + glyph->descent;
+#ifdef HAVE_WINDOW_SYSTEM
+         if (glyph->type == IMAGE_GLYPH)
+           {
+             struct image *img;
+             img = IMAGE_FROM_ID (WINDOW_XFRAME (w), glyph->u.img_id);
+             if (img != NULL)
+               *object = img->spec;
+             y0 -= row->ascent - glyph->ascent;
+           }
+#endif
+       }
       else
-       x0 = 0;
-      for (; glyph < end; x0 += glyph->pixel_width, ++glyph)
-       if (x >= x0 && x < x0 + glyph->pixel_width)
-         {
-           string = glyph->object;
-           *charpos = glyph->charpos;
-           break;
-         }
+       {
+         /* Add extra (default width) columns if clicked after EOL. */
+         *x += x0 / WINDOW_FRAME_COLUMN_WIDTH (w);
+         *width = 0;
+         *height = row->height;
+       }
     }
+  else
+    {
+      x0 = 0;
+      *x = 0;
+      *width = *height = 0;
+    }
+
+  *dx = x0;
+  *dy = y0;
 
   return string;
 }
@@ -5835,28 +5965,31 @@ window_change_signal (signalnum) /* If we don't have an argument, */
 #endif
   int old_errno = errno;
 
-  get_frame_size (&width, &height);
+  struct tty_display_info *tty;
 
-  /* The frame size change obviously applies to a termcap-controlled
-     frame.  Find such a frame in the list, and assume it's the only
-     one (since the redisplay code always writes to stdout, not a
-     FILE * specified in the frame structure).  Record the new size,
-     but don't reallocate the data structures now.  Let that be done
-     later outside of the signal handler.  */
+  /* The frame size change obviously applies to a single
+     termcap-controlled terminal, but we can't decide which.
+     Therefore, we resize the frames corresponding to each tty.
+  */
+  for (tty = tty_list; tty; tty = tty->next) {
 
-  {
-    Lisp_Object tail, frame;
+    if (! tty->term_initted)
+      continue;
 
-    FOR_EACH_FRAME (tail, frame)
-      {
-       if (FRAME_TERMCAP_P (XFRAME (frame)))
-         {
-           change_frame_size (XFRAME (frame), height, width, 0, 1, 0);
-           break;
-         }
-      }
+    get_tty_size (fileno (TTY_INPUT (tty)), &width, &height);
+    
+    {
+      Lisp_Object tail, frame;
+      
+      FOR_EACH_FRAME (tail, frame)
+        if (FRAME_TERMCAP_P (XFRAME (frame)) && FRAME_TTY (XFRAME (frame)) == tty)
+          /* Record the new sizes, but don't reallocate the data
+             structures now.  Let that be done later outside of the
+             signal handler.  */
+          change_frame_size (XFRAME (frame), height, width, 0, 1, 0);
+    }
   }
-
+  
   signal (SIGWINCH, window_change_signal);
   errno = old_errno;
 }
@@ -5885,11 +6018,9 @@ do_pending_window_change (safe)
        {
          struct frame *f = XFRAME (frame);
 
-         int height = FRAME_NEW_HEIGHT (f);
-         int width = FRAME_NEW_WIDTH (f);
-
-         if (height != 0 || width != 0)
-           change_frame_size (f, height, width, 0, 0, safe);
+         if (f->new_text_lines != 0 || f->new_text_cols != 0)
+           change_frame_size (f, f->new_text_lines, f->new_text_cols,
+                              0, 0, safe);
        }
     }
 }
@@ -5913,10 +6044,11 @@ change_frame_size (f, newheight, newwidth, pretend, delay, safe)
 {
   Lisp_Object tail, frame;
 
-  if (! FRAME_WINDOW_P (f))
+  if (FRAME_MSDOS_P (f))
     {
-      /* When using termcap, or on MS-DOS, all frames use
-        the same screen, so a change in size affects all frames.  */
+      /* On MS-DOS, all frames use the same screen, so a change in
+         size affects all frames.  Termcap now supports multiple
+         ttys. */
       FOR_EACH_FRAME (tail, frame)
        if (! FRAME_WINDOW_P (XFRAME (frame)))
          change_frame_size_1 (XFRAME (frame), newheight, newwidth,
@@ -5931,38 +6063,38 @@ change_frame_size_1 (f, newheight, newwidth, pretend, delay, safe)
      register struct frame *f;
      int newheight, newwidth, pretend, delay, safe;
 {
-  int new_frame_window_width;
+  int new_frame_total_cols;
   int count = SPECPDL_INDEX ();
 
   /* If we can't deal with the change now, queue it for later.  */
   if (delay || (redisplaying_p && !safe))
     {
-      FRAME_NEW_HEIGHT (f) = newheight;
-      FRAME_NEW_WIDTH (f) = newwidth;
+      f->new_text_lines = newheight;
+      f->new_text_cols = newwidth;
       delayed_size_change = 1;
       return;
     }
 
   /* This size-change overrides any pending one for this frame.  */
-  FRAME_NEW_HEIGHT (f) = 0;
-  FRAME_NEW_WIDTH  (f) = 0;
+  f->new_text_lines = 0;
+  f->new_text_cols = 0;
 
   /* If an argument is zero, set it to the current value.  */
   if (newheight == 0)
-    newheight = FRAME_HEIGHT (f);
+    newheight = FRAME_LINES (f);
   if (newwidth == 0)
-    newwidth  = FRAME_WIDTH  (f);
+    newwidth  = FRAME_COLS  (f);
 
   /* Compute width of windows in F.
      This is the width of the frame without vertical scroll bars.  */
-  new_frame_window_width = FRAME_WINDOW_WIDTH_ARG (f, newwidth);
+  new_frame_total_cols = FRAME_TOTAL_COLS_ARG (f, newwidth);
 
   /* Round up to the smallest acceptable size.  */
   check_frame_size (f, &newheight, &newwidth);
 
   /* If we're not changing the frame size, quit now.  */
-  if (newheight == FRAME_HEIGHT (f)
-      && new_frame_window_width == FRAME_WINDOW_WIDTH (f))
+  if (newheight == FRAME_LINES (f)
+      && new_frame_total_cols == FRAME_TOTAL_COLS (f))
     return;
 
   BLOCK_INPUT;
@@ -5974,19 +6106,19 @@ change_frame_size_1 (f, newheight, newwidth, pretend, delay, safe)
   dos_set_window_size (&newheight, &newwidth);
 #endif
 
-  if (newheight != FRAME_HEIGHT (f))
+  if (newheight != FRAME_LINES (f))
     {
       if (FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
        {
          /* Frame has both root and mini-buffer.  */
-         XSETFASTINT (XWINDOW (FRAME_ROOT_WINDOW (f))->top,
+         XSETFASTINT (XWINDOW (FRAME_ROOT_WINDOW (f))->top_line,
                       FRAME_TOP_MARGIN (f));
          set_window_height (FRAME_ROOT_WINDOW (f),
                             (newheight
                              - 1
                              - FRAME_TOP_MARGIN (f)),
                              0);
-         XSETFASTINT (XWINDOW (FRAME_MINIBUF_WINDOW (f))->top,
+         XSETFASTINT (XWINDOW (FRAME_MINIBUF_WINDOW (f))->top_line,
                       newheight - 1);
          set_window_height (FRAME_MINIBUF_WINDOW (f), 1, 0);
        }
@@ -5996,24 +6128,24 @@ change_frame_size_1 (f, newheight, newwidth, pretend, delay, safe)
                           newheight - FRAME_TOP_MARGIN (f), 0);
 
       if (FRAME_TERMCAP_P (f) && !pretend)
-       FrameRows = newheight;
+       FrameRows (FRAME_TTY (f)) = newheight;
     }
 
-  if (new_frame_window_width  != FRAME_WINDOW_WIDTH (f))
+  if (new_frame_total_cols != FRAME_TOTAL_COLS (f))
     {
-      set_window_width (FRAME_ROOT_WINDOW (f), new_frame_window_width, 0);
+      set_window_width (FRAME_ROOT_WINDOW (f), new_frame_total_cols, 0);
       if (FRAME_HAS_MINIBUF_P (f))
-       set_window_width (FRAME_MINIBUF_WINDOW (f), new_frame_window_width, 0);
+       set_window_width (FRAME_MINIBUF_WINDOW (f), new_frame_total_cols, 0);
 
       if (FRAME_TERMCAP_P (f) && !pretend)
-       FrameCols = newwidth;
+       FrameCols (FRAME_TTY (f)) = newwidth;
 
       if (WINDOWP (f->tool_bar_window))
-       XSETFASTINT (XWINDOW (f->tool_bar_window)->width, newwidth);
+       XSETFASTINT (XWINDOW (f->tool_bar_window)->total_cols, newwidth);
     }
 
-  FRAME_HEIGHT (f) = newheight;
-  SET_FRAME_WIDTH (f, newwidth);
+  FRAME_LINES (f) = newheight;
+  SET_FRAME_COLS (f, newwidth);
 
   {
     struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
@@ -6038,7 +6170,7 @@ change_frame_size_1 (f, newheight, newwidth, pretend, delay, safe)
 
   /* This isn't quite a no-op: it runs window-configuration-change-hook.  */
   Fset_window_buffer (FRAME_SELECTED_WINDOW (f),
-                     XWINDOW (FRAME_SELECTED_WINDOW (f))->buffer);
+                     XWINDOW (FRAME_SELECTED_WINDOW (f))->buffer, Qt);
 
   unbind_to (count, Qnil);
 }
@@ -6056,14 +6188,15 @@ FILE = nil means just close any termscript file currently open.  */)
      (file)
      Lisp_Object file;
 {
-  if (termscript != 0) fclose (termscript);
-  termscript = 0;
+  if (TTY_TERMSCRIPT (CURTTY ()) != 0)
+    fclose (TTY_TERMSCRIPT (CURTTY ()));
+  TTY_TERMSCRIPT (CURTTY ()) = 0;
 
   if (! NILP (file))
     {
       file = Fexpand_file_name (file, Qnil);
-      termscript = fopen (SDATA (file), "w");
-      if (termscript == 0)
+      TTY_TERMSCRIPT (CURTTY ()) = fopen (SDATA (file), "w");
+      if (TTY_TERMSCRIPT (CURTTY ()) == 0)
        report_file_error ("Opening termscript", Fcons (file, Qnil));
     }
   return Qnil;
@@ -6079,14 +6212,18 @@ Control characters in STRING will have terminal-dependent effects.  */)
 {
   /* ??? Perhaps we should do something special for multibyte strings here.  */
   CHECK_STRING (string);
-  fwrite (SDATA (string), 1, SBYTES (string), stdout);
-  fflush (stdout);
-  if (termscript)
+  if (! FRAME_TERMCAP_P (SELECTED_FRAME ()))
+    error ("Current frame is not on a tty device");
+  
+  if (TTY_TERMSCRIPT (CURTTY ()))
     {
       fwrite (SDATA (string), 1, SBYTES (string),
-             termscript);
-      fflush (termscript);
+             TTY_TERMSCRIPT (CURTTY ()));
+      fflush (TTY_TERMSCRIPT (CURTTY ()));
     }
+  fwrite (SDATA (string), 1, SBYTES (string),
+          TTY_OUTPUT (CURTTY ()));
+  fflush (TTY_OUTPUT (CURTTY ()));
   return Qnil;
 }
 
@@ -6104,7 +6241,8 @@ terminate any keyboard macro currently executing.  */)
        putchar (07);
       else
        ring_bell ();
-      fflush (stdout);
+      if (FRAME_TERMCAP_P (XFRAME (selected_frame)))
+        fflush (TTY_OUTPUT (CURTTY ()));
     }
   else
     bitch_at_user ();
@@ -6121,7 +6259,8 @@ bitch_at_user ()
     error ("Keyboard macro terminated by a command ringing the bell");
   else
     ring_bell ();
-  fflush (stdout);
+  if (FRAME_TERMCAP_P (XFRAME (selected_frame)))
+    fflush (TTY_OUTPUT (CURTTY ()));
 }
 
 
@@ -6268,7 +6407,10 @@ An obsolete but still supported form is
 Where the optional arg MILLISECONDS specifies an additional wait period,
 in milliseconds; this was useful when Emacs was built without
 floating point support.
-usage: (sit-for SECONDS &optional NODISP) */)
+usage: (sit-for SECONDS &optional NODISP OLD-NODISP) */)
+
+/* The `old-nodisp' stuff is there so that the arglist has the correct
+   length.  Otherwise, `defdvice' will redefine it with fewer args.  */
      (seconds, milliseconds, nodisp)
      Lisp_Object seconds, milliseconds, nodisp;
 {
@@ -6403,8 +6545,6 @@ the current state.  */)
                            Initialization
 ***********************************************************************/
 
-char *terminal_type;
-
 /* Initialization done when Emacs fork is started, before doing stty.
    Determine terminal type and set terminal_driver.  Then invoke its
    decoding routine to set up variables in the terminal package.  */
@@ -6412,6 +6552,8 @@ char *terminal_type;
 void
 init_display ()
 {
+  char *terminal_type;
+
 #ifdef HAVE_X_WINDOWS
   extern int display_arg;
 #endif
@@ -6421,14 +6563,13 @@ init_display ()
   SET_CHAR_GLYPH_FROM_GLYPH (space_glyph, ' ');
   space_glyph.charpos = -1;
 
-  meta_key = 0;
   inverse_video = 0;
   cursor_in_echo_area = 0;
   terminal_type = (char *) 0;
 
   /* Now is the time to initialize this; it's used by init_sys_modes
      during startup.  */
-  Vwindow_system = Qnil;
+  Vinitial_window_system = Qnil;
 
   /* If the user wants to use a window system, we shouldn't bother
      initializing the terminal.  This is especially important when the
@@ -6457,7 +6598,7 @@ init_display ()
 #endif
      )
     {
-      Vwindow_system = intern ("x");
+      Vinitial_window_system = intern ("x");
 #ifdef HAVE_X11
       Vwindow_system_version = make_number (11);
 #else
@@ -6477,7 +6618,7 @@ init_display ()
 #ifdef HAVE_NTGUI
   if (!inhibit_window_system)
     {
-      Vwindow_system = intern ("w32");
+      Vinitial_window_system = intern ("w32");
       Vwindow_system_version = make_number (1);
       adjust_frame_glyphs_initially ();
       return;
@@ -6487,7 +6628,7 @@ init_display ()
 #ifdef MAC_OS
   if (!inhibit_window_system)
     {
-      Vwindow_system = intern ("mac");
+      Vinitial_window_system = intern ("mac");
       Vwindow_system_version = make_number (1);
       adjust_frame_glyphs_initially ();
       return;
@@ -6533,12 +6674,33 @@ For types not defined in VMS, use  define emacs_term \"TYPE\".\n\
   }
 #endif /* VMS */
 
-  term_init (terminal_type);
-
+  {
+    struct display *d;
+    struct frame *f = XFRAME (selected_frame);
+
+    /* Open a display on the controlling tty. */
+    d = term_init (0, terminal_type, 1); /* Errors are fatal. */
+
+    /* Convert the initial frame to use the new display. */
+    if (! f->output_method == output_initial)
+      abort ();
+    f->output_method = d->type;
+    f->display = d;
+
+    d->reference_count++;
+    d->display_info.tty->top_frame = selected_frame;
+    change_frame_size (XFRAME (selected_frame), FrameRows (d->display_info.tty), FrameCols (d->display_info.tty), 0, 0, 1);
+
+    /* Delete the initial display. */
+    if (--initial_display->reference_count == 0
+        && initial_display->delete_display_hook)
+      (*initial_display->delete_display_hook) (initial_display);
+  }
+  
   {
     struct frame *sf = SELECTED_FRAME ();
-    int width = FRAME_WINDOW_WIDTH (sf);
-    int height = FRAME_HEIGHT (sf);
+    int width = FRAME_TOTAL_COLS (sf);
+    int height = FRAME_LINES (sf);
 
     unsigned int total_glyphs = height * (width + 2) * sizeof (struct glyph);
 
@@ -6568,7 +6730,7 @@ For types not defined in VMS, use  define emacs_term \"TYPE\".\n\
         and internal_terminal_init.  */
       && (strcmp (terminal_type, "internal") != 0 || inhibit_window_system)
 #endif
-      && NILP (Vwindow_system))
+      && NILP (Vinitial_window_system))
     {
       /* For the initial frame, we don't have any way of knowing what
         are the foreground and background colors of the terminal.  */
@@ -6680,8 +6842,8 @@ A non-nil value is useful if the terminal can automatically preserve
 Emacs's frame display when you reenter Emacs.
 It is up to you to set this variable if your terminal can do that.  */);
 
-  DEFVAR_LISP ("window-system", &Vwindow_system,
-              doc: /* Name of window system that Emacs is displaying through.
+  DEFVAR_LISP ("initial-window-system", &Vinitial_window_system,
+              doc: /* Name of the window system that Emacs uses for the first frame.
 The value is a symbol--for instance, `x' for X windows.
 The value is nil if Emacs is using a text-only terminal.  */);
 
@@ -6717,7 +6879,10 @@ See `buffer-display-table' for more information.  */);
   if (noninteractive)
 #endif
     {
-      Vwindow_system = Qnil;
+      Vinitial_window_system = Qnil;
       Vwindow_system_version = Qnil;
     }
 }
+
+/* arch-tag: 8d812b1f-04a2-4195-a9c4-381f8457a413
+   (do not change this comment) */