Merge from trunk after a lot of time.
[bpt/emacs.git] / src / dispnew.c
index bc65050..824c62b 100644 (file)
@@ -22,7 +22,7 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #define DISPEXTERN_INLINE EXTERN_INLINE
 
-#include <stdio.h>
+#include "sysstdio.h"
 #include <unistd.h>
 
 #include "lisp.h"
@@ -49,12 +49,10 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include TERM_HEADER
 #endif /* HAVE_WINDOW_SYSTEM */
 
-/* Include systime.h after xterm.h to avoid double inclusion of time.h.  */
-
-#include "systime.h"
 #include <errno.h>
 
 #include <fpending.h>
+#include <timespec.h>
 
 #if defined (HAVE_TERM_H) && defined (GNU_LINUX)
 #include <term.h>              /* for tgetent */
@@ -135,25 +133,20 @@ struct frame *last_nonminibuf_frame;
 
 static bool delayed_size_change;
 
-/* Updated window if != 0.  Set by update_window.  */
-
-struct window *updated_window;
-
-/* Glyph row updated in update_window_line, and area that is updated.  */
-
-struct glyph_row *updated_row;
-int updated_area;
-
 /* A glyph for a space.  */
 
 struct glyph space_glyph;
 
+#if defined GLYPH_DEBUG && defined ENABLE_CHECKING
+
 /* Counts of allocated structures.  These counts serve to diagnose
    memory leaks and double frees.  */
 
 static int glyph_matrix_count;
 static int glyph_pool_count;
 
+#endif /* GLYPH_DEBUG and ENABLE_CHECKING */
+
 /* If non-null, the frame whose frame matrices are manipulated.  If
    null, window matrices are worked on.  */
 
@@ -213,6 +206,7 @@ static void
 add_window_display_history (struct window *w, const char *msg, bool paused_p)
 {
   char *buf;
+  void *ptr = w;
 
   if (history_idx >= REDISPLAY_HISTORY_SIZE)
     history_idx = 0;
@@ -222,10 +216,10 @@ add_window_display_history (struct window *w, const char *msg, bool paused_p)
   snprintf (buf, sizeof redisplay_history[0].trace,
            "%"pMu": window %p (`%s')%s\n%s",
            history_tick++,
-           w,
-           ((BUFFERP (w->buffer)
-             && STRINGP (BVAR (XBUFFER (w->buffer), name)))
-            ? SSDATA (BVAR (XBUFFER (w->buffer), name))
+           ptr,
+           ((BUFFERP (w->contents)
+             && STRINGP (BVAR (XBUFFER (w->contents), name)))
+            ? SSDATA (BVAR (XBUFFER (w->contents), name))
             : "???"),
            paused_p ? " ***paused***" : "",
            msg);
@@ -240,6 +234,7 @@ static void
 add_frame_display_history (struct frame *f, bool paused_p)
 {
   char *buf;
+  void *ptr = f;
 
   if (history_idx >= REDISPLAY_HISTORY_SIZE)
     history_idx = 0;
@@ -248,7 +243,7 @@ add_frame_display_history (struct frame *f, bool paused_p)
 
   sprintf (buf, "%"pMu": update frame %p%s",
           history_tick++,
-          f, paused_p ? " ***paused***" : "");
+          ptr, paused_p ? " ***paused***" : "");
 }
 
 
@@ -305,9 +300,11 @@ new_glyph_matrix (struct glyph_pool *pool)
 {
   struct glyph_matrix *result = xzalloc (sizeof *result);
 
+#if defined GLYPH_DEBUG && defined ENABLE_CHECKING
   /* Increment number of allocated matrices.  This count is used
      to detect memory leaks.  */
   ++glyph_matrix_count;
+#endif
 
   /* Set pool and return.  */
   result->pool = pool;
@@ -317,10 +314,10 @@ new_glyph_matrix (struct glyph_pool *pool)
 
 /* Free glyph matrix MATRIX.  Passing in a null MATRIX is allowed.
 
-   The global counter glyph_matrix_count is decremented when a matrix
-   is freed.  If the count gets negative, more structures were freed
-   than allocated, i.e. one matrix was freed more than once or a bogus
-   pointer was passed to this function.
+   If GLYPH_DEBUG and ENABLE_CHECKING are in effect, the global counter
+   glyph_matrix_count is decremented when a matrix is freed.  If the count
+   gets negative, more structures were freed than allocated, i.e. one matrix
+   was freed more than once or a bogus pointer was passed to this function.
 
    If MATRIX->pool is null, this means that the matrix manages its own
    glyph memory---this is done for matrices on X frames.  Freeing the
@@ -333,10 +330,12 @@ free_glyph_matrix (struct glyph_matrix *matrix)
     {
       int i;
 
+#if defined GLYPH_DEBUG && defined ENABLE_CHECKING
       /* Detect the case that more matrices are freed than were
         allocated.  */
-      if (--glyph_matrix_count < 0)
-       emacs_abort ();
+      --glyph_matrix_count;
+      eassert (glyph_matrix_count >= 0);
+#endif
 
       /* Free glyph memory if MATRIX owns it.  */
       if (matrix->pool == NULL)
@@ -353,25 +352,19 @@ free_glyph_matrix (struct glyph_matrix *matrix)
 /* Return the number of glyphs to reserve for a marginal area of
    window W.  TOTAL_GLYPHS is the number of glyphs in a complete
    display line of window W.  MARGIN gives the width of the marginal
-   area in canonical character units.  MARGIN should be an integer
-   or a float.  */
+   area in canonical character units.  */
 
 static int
-margin_glyphs_to_reserve (struct window *w, int total_glyphs, Lisp_Object margin)
+margin_glyphs_to_reserve (struct window *w, int total_glyphs, int margin)
 {
-  int n;
-
-  if (NUMBERP (margin))
+  if (margin > 0)
     {
       int width = w->total_cols;
-      double d = max (0, XFLOATINT (margin));
+      double d = max (0, margin);
       d = min (width / 2 - 1, d);
-      n = (int) ((double) total_glyphs / width * d);
+      return (int) ((double) total_glyphs / width * d);
     }
-  else
-    n = 0;
-
-  return n;
+  return 0;
 }
 
 /* Return true if ROW's hash value is correct.
@@ -420,7 +413,7 @@ adjust_glyph_matrix (struct window *w, struct glyph_matrix *matrix, int x, int y
      Get W's size.  */
   if (w)
     {
-      window_box (w, -1, 0, 0, &window_width, &window_height);
+      window_box (w, ANY_AREA, 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;
@@ -598,8 +591,7 @@ adjust_glyph_matrix (struct window *w, struct glyph_matrix *matrix, int x, int y
 
              /* Window end is invalid, if inside of the rows that
                 are invalidated below.  */
-             if (INTEGERP (w->window_end_vpos)
-                 && XFASTINT (w->window_end_vpos) >= i)
+             if (w->window_end_vpos >= i)
                w->window_end_valid = 0;
 
              while (i < matrix->nrows)
@@ -794,11 +786,13 @@ clear_current_matrices (register struct frame *f)
   if (f->current_matrix)
     clear_glyph_matrix (f->current_matrix);
 
+#if defined (HAVE_X_WINDOWS) && ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
   /* Clear the matrix of the menu bar window, if such a window exists.
      The menu bar window is currently used to display menus on X when
      no toolkit support is compiled in.  */
   if (WINDOWP (f->menu_bar_window))
     clear_glyph_matrix (XWINDOW (f->menu_bar_window)->current_matrix);
+#endif
 
   /* Clear the matrix of the tool-bar window, if any.  */
   if (WINDOWP (f->tool_bar_window))
@@ -818,8 +812,10 @@ clear_desired_matrices (register struct frame *f)
   if (f->desired_matrix)
     clear_glyph_matrix (f->desired_matrix);
 
+#if defined (HAVE_X_WINDOWS) && ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
   if (WINDOWP (f->menu_bar_window))
     clear_glyph_matrix (XWINDOW (f->menu_bar_window)->desired_matrix);
+#endif
 
   if (WINDOWP (f->tool_bar_window))
     clear_glyph_matrix (XWINDOW (f->tool_bar_window)->desired_matrix);
@@ -838,16 +834,8 @@ clear_window_matrices (struct window *w, bool desired_p)
 {
   while (w)
     {
-      if (!NILP (w->hchild))
-       {
-         eassert (WINDOWP (w->hchild));
-         clear_window_matrices (XWINDOW (w->hchild), desired_p);
-       }
-      else if (!NILP (w->vchild))
-       {
-         eassert (WINDOWP (w->vchild));
-         clear_window_matrices (XWINDOW (w->vchild), desired_p);
-       }
+      if (WINDOWP (w->contents))
+       clear_window_matrices (XWINDOW (w->contents), desired_p);
       else
        {
          if (desired_p)
@@ -1312,38 +1300,41 @@ row_equal_p (struct glyph_row *a, struct glyph_row *b, bool mouse_face_p)
      See dispextern.h for an overall explanation of glyph pools.
  ***********************************************************************/
 
-/* Allocate a glyph_pool structure.  The structure returned is
-   initialized with zeros.  The global variable glyph_pool_count is
-   incremented for each pool allocated.  */
+/* Allocate a glyph_pool structure.  The structure returned is initialized
+   with zeros.  If GLYPH_DEBUG and ENABLE_CHECKING are in effect, the global
+   variable glyph_pool_count is incremented for each pool allocated.  */
 
 static struct glyph_pool *
 new_glyph_pool (void)
 {
   struct glyph_pool *result = xzalloc (sizeof *result);
 
+#if defined GLYPH_DEBUG && defined ENABLE_CHECKING
   /* For memory leak and double deletion checking.  */
   ++glyph_pool_count;
+#endif
 
   return result;
 }
 
 
 /* Free a glyph_pool structure POOL.  The function may be called with
-   a null POOL pointer.  The global variable glyph_pool_count is
-   decremented with every pool structure freed.  If this count gets
-   negative, more structures were freed than allocated, i.e. one
-   structure must have been freed more than once or a bogus pointer
-   was passed to free_glyph_pool.  */
+   a null POOL pointer.  If GLYPH_DEBUG and ENABLE_CHECKING are in effect,
+   global variable glyph_pool_count is decremented with every pool structure
+   freed.  If this count gets negative, more structures were freed than
+   allocated, i.e. one structure must have been freed more than once or
+   a bogus pointer was passed to free_glyph_pool.  */
 
 static void
 free_glyph_pool (struct glyph_pool *pool)
 {
   if (pool)
     {
+#if defined GLYPH_DEBUG && defined ENABLE_CHECKING
       /* More freed than allocated?  */
       --glyph_pool_count;
       eassert (glyph_pool_count >= 0);
-
+#endif
       xfree (pool->glyphs);
       xfree (pool);
     }
@@ -1468,7 +1459,7 @@ check_matrix_invariants (struct window *w)
   struct glyph_row *row = matrix->rows;
   struct glyph_row *last_text_row = NULL;
   struct buffer *saved = current_buffer;
-  struct buffer *buffer = XBUFFER (w->buffer);
+  struct buffer *buffer = XBUFFER (w->contents);
   int c;
 
   /* This can sometimes happen for a fresh window.  */
@@ -1632,7 +1623,7 @@ allocate_matrices_for_frame_redisplay (Lisp_Object window, int x, int y,
      vertically below other windows.  */
   in_horz_combination_p
     = (!NILP (XWINDOW (window)->parent)
-       && !NILP (XWINDOW (XWINDOW (window)->parent)->hchild));
+       && WINDOW_HORIZONTAL_COMBINATION_P (XWINDOW (XWINDOW (window)->parent)));
 
   /* For WINDOW and all windows on the same level.  */
   do
@@ -1641,12 +1632,8 @@ allocate_matrices_for_frame_redisplay (Lisp_Object window, int x, int y,
 
       /* Get the dimension of the window sub-matrix for W, depending
         on whether this is a combination or a leaf window.  */
-      if (!NILP (w->hchild))
-       dim = allocate_matrices_for_frame_redisplay (w->hchild, x, y,
-                                                    dim_only_p,
-                                                    window_change_flags);
-      else if (!NILP (w->vchild))
-       dim = allocate_matrices_for_frame_redisplay (w->vchild, x, y,
+      if (WINDOWP (w->contents))
+       dim = allocate_matrices_for_frame_redisplay (w->contents, x, y,
                                                     dim_only_p,
                                                     window_change_flags);
       else
@@ -1788,10 +1775,8 @@ allocate_matrices_for_window_redisplay (struct window *w)
 {
   while (w)
     {
-      if (!NILP (w->vchild))
-       allocate_matrices_for_window_redisplay (XWINDOW (w->vchild));
-      else if (!NILP (w->hchild))
-       allocate_matrices_for_window_redisplay (XWINDOW (w->hchild));
+      if (WINDOWP (w->contents))
+       allocate_matrices_for_window_redisplay (XWINDOW (w->contents));
       else
        {
          /* W is a leaf window.  */
@@ -1863,18 +1848,12 @@ showing_window_margins_p (struct window *w)
 {
   while (w)
     {
-      if (!NILP (w->hchild))
+      if (WINDOWP (w->contents))
        {
-         if (showing_window_margins_p (XWINDOW (w->hchild)))
+         if (showing_window_margins_p (XWINDOW (w->contents)))
            return 1;
        }
-      else if (!NILP (w->vchild))
-       {
-         if (showing_window_margins_p (XWINDOW (w->vchild)))
-           return 1;
-       }
-      else if (!NILP (w->left_margin_cols)
-              || !NILP (w->right_margin_cols))
+      else if (w->left_margin_cols > 0 || w->right_margin_cols > 0)
        return 1;
 
       w = NILP (w->next) ? 0 : XWINDOW (w->next);
@@ -1895,10 +1874,8 @@ fake_current_matrices (Lisp_Object window)
     {
       w = XWINDOW (window);
 
-      if (!NILP (w->hchild))
-       fake_current_matrices (w->hchild);
-      else if (!NILP (w->vchild))
-       fake_current_matrices (w->vchild);
+      if (WINDOWP (w->contents))
+       fake_current_matrices (w->contents);
       else
        {
          int i;
@@ -1936,7 +1913,7 @@ fake_current_matrices (Lisp_Object window)
 /* Save away the contents of frame F's current frame matrix.  Value is
    a glyph matrix holding the contents of F's current frame matrix.  */
 
-static struct glyph_matrix *
+struct glyph_matrix *
 save_current_matrix (struct frame *f)
 {
   int i;
@@ -1949,9 +1926,26 @@ save_current_matrix (struct frame *f)
       struct glyph_row *from = f->current_matrix->rows + i;
       struct glyph_row *to = saved->rows + i;
       ptrdiff_t nbytes = from->used[TEXT_AREA] * sizeof (struct glyph);
+
       to->glyphs[TEXT_AREA] = xmalloc (nbytes);
       memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA], nbytes);
       to->used[TEXT_AREA] = from->used[TEXT_AREA];
+      if (from->used[LEFT_MARGIN_AREA])
+       {
+         nbytes = from->used[LEFT_MARGIN_AREA] * sizeof (struct glyph);
+         to->glyphs[LEFT_MARGIN_AREA] = (struct glyph *) xmalloc (nbytes);
+         memcpy (to->glyphs[LEFT_MARGIN_AREA],
+                 from->glyphs[LEFT_MARGIN_AREA], nbytes);
+         to->used[LEFT_MARGIN_AREA] = from->used[LEFT_MARGIN_AREA];
+       }
+      if (from->used[RIGHT_MARGIN_AREA])
+       {
+         nbytes = from->used[RIGHT_MARGIN_AREA] * sizeof (struct glyph);
+         to->glyphs[RIGHT_MARGIN_AREA] = (struct glyph *) xmalloc (nbytes);
+         memcpy (to->glyphs[RIGHT_MARGIN_AREA],
+                 from->glyphs[RIGHT_MARGIN_AREA], nbytes);
+         to->used[RIGHT_MARGIN_AREA] = from->used[RIGHT_MARGIN_AREA];
+       }
     }
 
   return saved;
@@ -1971,9 +1965,30 @@ restore_current_matrix (struct frame *f, struct glyph_matrix *saved)
       struct glyph_row *from = saved->rows + i;
       struct glyph_row *to = f->current_matrix->rows + i;
       ptrdiff_t nbytes = from->used[TEXT_AREA] * sizeof (struct glyph);
+
       memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA], nbytes);
       to->used[TEXT_AREA] = from->used[TEXT_AREA];
       xfree (from->glyphs[TEXT_AREA]);
+      nbytes = from->used[LEFT_MARGIN_AREA];
+      if (nbytes)
+       {
+         memcpy (to->glyphs[LEFT_MARGIN_AREA],
+                 from->glyphs[LEFT_MARGIN_AREA], nbytes);
+         to->used[LEFT_MARGIN_AREA] = from->used[LEFT_MARGIN_AREA];
+         xfree (from->glyphs[LEFT_MARGIN_AREA]);
+       }
+      else
+       to->used[LEFT_MARGIN_AREA] = 0;
+      nbytes = from->used[RIGHT_MARGIN_AREA];
+      if (nbytes)
+       {
+         memcpy (to->glyphs[RIGHT_MARGIN_AREA],
+                 from->glyphs[RIGHT_MARGIN_AREA], nbytes);
+         to->used[RIGHT_MARGIN_AREA] = from->used[RIGHT_MARGIN_AREA];
+         xfree (from->glyphs[RIGHT_MARGIN_AREA]);
+       }
+      else
+       to->used[RIGHT_MARGIN_AREA] = 0;
     }
 
   xfree (saved->rows);
@@ -2184,6 +2199,7 @@ free_glyphs (struct frame *f)
       if (!NILP (f->root_window))
         free_window_matrices (XWINDOW (f->root_window));
 
+#if defined (HAVE_X_WINDOWS) && ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
       /* Free the dummy window for menu bars without X toolkit and its
         glyph matrices.  */
       if (!NILP (f->menu_bar_window))
@@ -2194,6 +2210,7 @@ free_glyphs (struct frame *f)
          w->desired_matrix = w->current_matrix = NULL;
          fset_menu_bar_window (f, Qnil);
        }
+#endif
 
       /* Free the tool bar window and its glyph matrices.  */
       if (!NILP (f->tool_bar_window))
@@ -2236,10 +2253,8 @@ free_window_matrices (struct window *w)
 {
   while (w)
     {
-      if (!NILP (w->hchild))
-       free_window_matrices (XWINDOW (w->hchild));
-      else if (!NILP (w->vchild))
-       free_window_matrices (XWINDOW (w->vchild));
+      if (WINDOWP (w->contents))
+       free_window_matrices (XWINDOW (w->contents));
       else
        {
          /* This is a leaf window.  Free its memory and reset fields
@@ -2270,11 +2285,11 @@ check_glyph_memory (void)
   FOR_EACH_FRAME (tail, frame)
     free_glyphs (XFRAME (frame));
 
+#if defined GLYPH_DEBUG && defined ENABLE_CHECKING
   /* Check that nothing is left allocated.  */
-  if (glyph_matrix_count)
-    emacs_abort ();
-  if (glyph_pool_count)
-    emacs_abort ();
+  eassert (glyph_matrix_count == 0);
+  eassert (glyph_pool_count == 0);
+#endif
 }
 
 
@@ -2291,7 +2306,7 @@ check_glyph_memory (void)
    screen.  We build such a view by constructing a frame matrix from
    window matrices in this section.
 
-   Windows that must be updated have their must_be_update_p flag set.
+   Windows that must be updated have their must_be_updated_p flag set.
    For all such windows, their desired matrix is made part of the
    desired frame matrix.  For other windows, their current matrix is
    made part of the desired frame matrix.
@@ -2372,10 +2387,8 @@ build_frame_matrix_from_window_tree (struct glyph_matrix *matrix, struct window
 {
   while (w)
     {
-      if (!NILP (w->hchild))
-       build_frame_matrix_from_window_tree (matrix, XWINDOW (w->hchild));
-      else if (!NILP (w->vchild))
-       build_frame_matrix_from_window_tree (matrix, XWINDOW (w->vchild));
+      if (WINDOWP (w->contents))
+       build_frame_matrix_from_window_tree (matrix, XWINDOW (w->contents));
       else
        build_frame_matrix_from_leaf_window (matrix, w);
 
@@ -2639,10 +2652,8 @@ mirror_make_current (struct window *w, int frame_row)
 {
   while (w)
     {
-      if (!NILP (w->hchild))
-       mirror_make_current (XWINDOW (w->hchild), frame_row);
-      else if (!NILP (w->vchild))
-       mirror_make_current (XWINDOW (w->vchild), frame_row);
+      if (WINDOWP (w->contents))
+       mirror_make_current (XWINDOW (w->contents), frame_row);
       else
        {
          /* Row relative to window W.  Don't use FRAME_TO_WINDOW_VPOS
@@ -2738,8 +2749,8 @@ sync_window_with_frame_matrix_rows (struct window *w)
   struct glyph_row *window_row, *window_row_end, *frame_row;
   int left, right, x, width;
 
-  /* Preconditions: W must be a leaf window on a tty frame.  */
-  eassert (NILP (w->hchild) && NILP (w->vchild));
+  /* Preconditions: W must be a live window on a tty frame.  */
+  eassert (BUFFERP (w->contents));
   eassert (!FRAME_WINDOW_P (f));
 
   left = margin_glyphs_to_reserve (w, 1, w->left_margin_cols);
@@ -2775,10 +2786,8 @@ frame_row_to_window (struct window *w, int row)
 
   while (w && !found)
     {
-      if (!NILP (w->hchild))
-       found = frame_row_to_window (XWINDOW (w->hchild), row);
-      else if (!NILP (w->vchild))
-       found = frame_row_to_window (XWINDOW (w->vchild), row);
+      if (WINDOWP (w->contents))
+       found = frame_row_to_window (XWINDOW (w->contents), row);
       else if (row >= WINDOW_TOP_EDGE_LINE (w)
               && row < WINDOW_BOTTOM_EDGE_LINE (w))
        found = w;
@@ -2806,11 +2815,8 @@ mirror_line_dance (struct window *w, int unchanged_at_top, int nlines, int *copy
 {
   while (w)
     {
-      if (!NILP (w->hchild))
-       mirror_line_dance (XWINDOW (w->hchild), unchanged_at_top,
-                          nlines, copy_from, retained_p);
-      else if (!NILP (w->vchild))
-       mirror_line_dance (XWINDOW (w->vchild), unchanged_at_top,
+      if (WINDOWP (w->contents))
+       mirror_line_dance (XWINDOW (w->contents), unchanged_at_top,
                           nlines, copy_from, retained_p);
       else
        {
@@ -2919,10 +2925,8 @@ check_window_matrix_pointers (struct window *w)
 {
   while (w)
     {
-      if (!NILP (w->hchild))
-       check_window_matrix_pointers (XWINDOW (w->hchild));
-      else if (!NILP (w->vchild))
-       check_window_matrix_pointers (XWINDOW (w->vchild));
+      if (WINDOWP (w->contents))
+       check_window_matrix_pointers (XWINDOW (w->contents));
       else
        {
          struct frame *f = XFRAME (w->frame);
@@ -3092,10 +3096,12 @@ update_frame (struct frame *f, bool force_p, bool inhibit_hairy_id_p)
         when pending input is detected.  */
       update_begin (f);
 
+#if defined (HAVE_X_WINDOWS) && ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
       /* Update the menu bar on X frames that don't have toolkit
         support.  */
       if (WINDOWP (f->menu_bar_window))
        update_window (XWINDOW (f->menu_bar_window), 1);
+#endif
 
       /* Update the tool-bar window, if present.  */
       if (WINDOWP (f->tool_bar_window))
@@ -3170,6 +3176,41 @@ update_frame (struct frame *f, bool force_p, bool inhibit_hairy_id_p)
   return paused_p;
 }
 
+/* Update a TTY frame F that has a menu dropped down over some of its
+   glyphs.  This is like the second part of update_frame, but it
+   doesn't call build_frame_matrix, because we already have the
+   desired matrix prepared, and don't want it to be overwritten by the
+   text of the normal display.  */
+void
+update_frame_with_menu (struct frame *f)
+{
+  struct window *root_window = XWINDOW (f->root_window);
+
+  xassert (FRAME_TERMCAP_P (f));
+
+  /* We are working on frame matrix basis.  Set the frame on whose
+     frame matrix we operate.  */
+  set_frame_matrix_frame (f);
+
+  /* Update the display  */
+  update_begin (f);
+  /* Force update_frame_1 not to stop due to pending input, and not
+     try scrolling.  */
+  paused_p = update_frame_1 (f, 1, 1);
+  update_end (f);
+
+  if (FRAME_TTY (f)->termscript)
+    fflush (FRAME_TTY (f)->termscript);
+  fflush (FRAME_TTY (f)->output);
+  /* Check window matrices for lost pointers.  */
+#if GLYPH_DEBUG
+  check_window_matrix_pointers (root_window);
+  add_frame_display_history (f, paused_p);
+#endif
+
+  /* Reset flags indicating that a window should be updated.  */
+  set_window_update_flags (root_window, 0);
+}
 
 \f
 /************************************************************************
@@ -3186,10 +3227,8 @@ update_window_tree (struct window *w, bool force_p)
 
   while (w && !paused_p)
     {
-      if (!NILP (w->hchild))
-       paused_p |= update_window_tree (XWINDOW (w->hchild), force_p);
-      else if (!NILP (w->vchild))
-       paused_p |= update_window_tree (XWINDOW (w->vchild), force_p);
+      if (WINDOWP (w->contents))
+       paused_p |= update_window_tree (XWINDOW (w->contents), force_p);
       else if (w->must_be_updated_p)
        paused_p |= update_window (w, force_p);
 
@@ -3257,14 +3296,12 @@ redraw_overlapped_rows (struct window *w, int yb)
 
          for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
            {
-             updated_row = row;
-             updated_area = area;
-             FRAME_RIF (f)->cursor_to (i, 0, row->y,
-                                        area == TEXT_AREA ? row->x : 0);
+             output_cursor_to (w, i, 0, row->y,
+                               area == TEXT_AREA ? row->x : 0);
              if (row->used[area])
-               FRAME_RIF (f)->write_glyphs (row->glyphs[area],
-                                             row->used[area]);
-             FRAME_RIF (f)->clear_end_of_line (-1);
+               FRAME_RIF (f)->write_glyphs (w, row, row->glyphs[area],
+                                             area, row->used[area]);
+             FRAME_RIF (f)->clear_end_of_line (w, row, area, -1);
            }
 
          row->overlapped_p = 0;
@@ -3517,7 +3554,7 @@ update_window (struct window *w, bool force_p)
       /* End the update of window W.  Don't set the cursor if we
          paused updating the display because in this case,
          set_window_cursor_after_update hasn't been called, and
-         output_cursor doesn't contain the cursor location.  */
+         W->output_cursor doesn't contain the cursor location.  */
       rif->update_window_end_hook (w, !paused_p, mouse_face_overwritten_p);
     }
   else
@@ -3538,22 +3575,20 @@ update_window (struct window *w, bool force_p)
    AREA can be either LEFT_MARGIN_AREA or RIGHT_MARGIN_AREA.  */
 
 static void
-update_marginal_area (struct window *w, int area, int vpos)
+update_marginal_area (struct window *w, struct glyph_row *updated_row,
+                     enum glyph_row_area area, int 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.  */
-  updated_area = area;
-
   /* Set cursor to start of glyphs, write them, and clear to the end
      of the area.  I don't think that something more sophisticated is
      necessary here, since marginal areas will not be the default.  */
-  rif->cursor_to (vpos, 0, desired_row->y, 0);
+  output_cursor_to (w, vpos, 0, desired_row->y, 0);
   if (desired_row->used[area])
-    rif->write_glyphs (desired_row->glyphs[area], desired_row->used[area]);
-  rif->clear_end_of_line (-1);
+    rif->write_glyphs (w, updated_row, desired_row->glyphs[area],
+                      area, desired_row->used[area]);
+  rif->clear_end_of_line (w, updated_row, area, -1);
 }
 
 
@@ -3561,17 +3596,13 @@ update_marginal_area (struct window *w, int area, int vpos)
    Value is true if display has changed.  */
 
 static bool
-update_text_area (struct window *w, int vpos)
+update_text_area (struct window *w, struct glyph_row *updated_row, int 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)));
   bool changed_p = 0;
 
-  /* Let functions in xterm.c know what area subsequent X positions
-     will be relative to.  */
-  updated_area = TEXT_AREA;
-
   /* If rows are at different X or Y, or rows have different height,
      or the current row is marked invalid, write the entire line.  */
   if (!current_row->enabled_p
@@ -3591,14 +3622,14 @@ update_text_area (struct window *w, int vpos)
          && !(current_row->mode_line_p && vpos > 0))
       || current_row->x != desired_row->x)
     {
-      rif->cursor_to (vpos, 0, desired_row->y, desired_row->x);
+      output_cursor_to (w, vpos, 0, desired_row->y, desired_row->x);
 
       if (desired_row->used[TEXT_AREA])
-       rif->write_glyphs (desired_row->glyphs[TEXT_AREA],
-                          desired_row->used[TEXT_AREA]);
+       rif->write_glyphs (w, updated_row, desired_row->glyphs[TEXT_AREA],
+                          TEXT_AREA, desired_row->used[TEXT_AREA]);
 
       /* Clear to end of window.  */
-      rif->clear_end_of_line (-1);
+      rif->clear_end_of_line (w, updated_row, TEXT_AREA, -1);
       changed_p = 1;
 
       /* This erases the cursor.  We do this here because
@@ -3734,8 +3765,9 @@ update_text_area (struct window *w, int vpos)
                  break;
                }
 
-             rif->cursor_to (vpos, start_hpos, desired_row->y, start_x);
-             rif->write_glyphs (start, i - start_hpos);
+             output_cursor_to (w, vpos, start_hpos, desired_row->y, start_x);
+             rif->write_glyphs (w, updated_row, start,
+                                TEXT_AREA, i - start_hpos);
              changed_p = 1;
            }
        }
@@ -3743,8 +3775,9 @@ update_text_area (struct window *w, int vpos)
       /* Write the rest.  */
       if (i < desired_row->used[TEXT_AREA])
        {
-         rif->cursor_to (vpos, i, desired_row->y, x);
-         rif->write_glyphs (desired_glyph, desired_row->used[TEXT_AREA] - i);
+         output_cursor_to (w, vpos, i, desired_row->y, x);
+         rif->write_glyphs (w, updated_row, desired_glyph,
+                            TEXT_AREA, desired_row->used[TEXT_AREA] - i);
          changed_p = 1;
        }
 
@@ -3764,9 +3797,9 @@ update_text_area (struct window *w, int 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->pixel_width);
-         rif->clear_end_of_line (-1);
+           output_cursor_to (w, vpos, i, desired_row->y,
+                             desired_row->pixel_width);
+         rif->clear_end_of_line (w, updated_row, TEXT_AREA, -1);
          changed_p = 1;
        }
       else if (desired_row->pixel_width < current_row->pixel_width)
@@ -3776,8 +3809,8 @@ update_text_area (struct window *w, int vpos)
          int xlim;
 
          if (i >= desired_row->used[TEXT_AREA])
-           rif->cursor_to (vpos, i, desired_row->y,
-                           desired_row->pixel_width);
+           output_cursor_to (w, vpos, i, desired_row->y,
+                             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
@@ -3794,7 +3827,7 @@ update_text_area (struct window *w, int vpos)
            }
          else
            xlim = current_row->pixel_width;
-         rif->clear_end_of_line (xlim);
+         rif->clear_end_of_line (w, updated_row, TEXT_AREA, xlim);
          changed_p = 1;
        }
     }
@@ -3813,10 +3846,6 @@ update_window_line (struct window *w, int vpos, bool *mouse_face_overwritten_p)
   struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
   bool changed_p = 0;
 
-  /* Set the row being updated.  This is important to let xterm.c
-     know what line height values are in effect.  */
-  updated_row = desired_row;
-
   /* A row can be completely invisible in case a desired matrix was
      built with a vscroll and then make_cursor_line_fully_visible shifts
      the matrix.  Make sure to make such rows current anyway, since
@@ -3827,11 +3856,10 @@ update_window_line (struct window *w, int vpos, bool *mouse_face_overwritten_p)
       eassert (desired_row->enabled_p);
 
       /* Update display of the left margin area, if there is one.  */
-      if (!desired_row->full_width_p
-         && !NILP (w->left_margin_cols))
+      if (!desired_row->full_width_p && w->left_margin_cols > 0)
        {
          changed_p = 1;
-         update_marginal_area (w, LEFT_MARGIN_AREA, vpos);
+         update_marginal_area (w, desired_row, LEFT_MARGIN_AREA, vpos);
          /* Setting this flag will ensure the vertical border, if
             any, between this window and the one on its left will be
             redrawn.  This is necessary because updating the left
@@ -3840,7 +3868,7 @@ update_window_line (struct window *w, int vpos, bool *mouse_face_overwritten_p)
        }
 
       /* Update the display of the text area.  */
-      if (update_text_area (w, vpos))
+      if (update_text_area (w, desired_row, vpos))
        {
          changed_p = 1;
          if (current_row->mouse_face_p)
@@ -3848,11 +3876,10 @@ update_window_line (struct window *w, int vpos, bool *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_cols))
+      if (!desired_row->full_width_p && w->right_margin_cols > 0)
        {
          changed_p = 1;
-         update_marginal_area (w, RIGHT_MARGIN_AREA, vpos);
+         update_marginal_area (w, desired_row, RIGHT_MARGIN_AREA, vpos);
        }
 
       /* Draw truncation marks etc.  */
@@ -3866,12 +3893,11 @@ update_window_line (struct window *w, int vpos, bool *mouse_face_overwritten_p)
          || desired_row->exact_window_width_line_p != current_row->exact_window_width_line_p
          || (MATRIX_ROW_CONTINUATION_LINE_P (desired_row)
              != MATRIX_ROW_CONTINUATION_LINE_P (current_row)))
-       rif->after_update_window_line_hook (desired_row);
+       rif->after_update_window_line_hook (w, desired_row);
     }
 
   /* Update current_row from desired_row.  */
   make_current (w->desired_matrix, w->current_matrix, vpos);
-  updated_row = NULL;
   return changed_p;
 }
 
@@ -3883,7 +3909,6 @@ static void
 set_window_cursor_after_update (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.  */
@@ -3955,7 +3980,7 @@ set_window_cursor_after_update (struct window *w)
      Horizontal position is -1 when cursor is on the left fringe.   */
   hpos = clip_to_bounds (-1, hpos, w->current_matrix->matrix_w - 1);
   vpos = clip_to_bounds (0, vpos, w->current_matrix->nrows - 1);
-  rif->cursor_to (vpos, hpos, cy, cx);
+  output_cursor_to (w, vpos, hpos, cy, cx);
 }
 
 
@@ -3967,10 +3992,8 @@ set_window_update_flags (struct window *w, bool on_p)
 {
   while (w)
     {
-      if (!NILP (w->hchild))
-       set_window_update_flags (XWINDOW (w->hchild), on_p);
-      else if (!NILP (w->vchild))
-       set_window_update_flags (XWINDOW (w->vchild), on_p);
+      if (WINDOWP (w->contents))
+       set_window_update_flags (XWINDOW (w->contents), on_p);
       else
        w->must_be_updated_p = on_p;
 
@@ -4451,7 +4474,7 @@ scrolling_window (struct window *w, bool header_line_p)
     row_table[row_entry_pool[i].bucket] = NULL;
 
   /* Value is 1 to indicate that we scrolled the display.  */
-  return 0 < nruns;
+  return nruns > 0;
 }
 
 
@@ -4628,10 +4651,7 @@ update_frame_1 (struct frame *f, bool force_p, bool inhibit_id_p)
              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_cols))
-               x += XFASTINT (w->left_margin_cols);
-
-             /* x = max (min (x, FRAME_TOTAL_COLS (f) - 1), 0); */
+             x += max (0, w->left_margin_cols);
              cursor_to (f, y, x);
            }
        }
@@ -5117,11 +5137,9 @@ buffer_posn_from_coords (struct window *w, int *x, int *y, struct display_pos *p
 
   /* We used to set current_buffer directly here, but that does the
      wrong thing with `face-remapping-alist' (bug#2044).  */
-  Fset_buffer (w->buffer);
+  Fset_buffer (w->contents);
   itdata = bidi_shelve_cache ();
-  SET_TEXT_POS_FROM_MARKER (startp, w->start);
-  CHARPOS (startp) = min (ZV, max (BEGV, CHARPOS (startp)));
-  BYTEPOS (startp) = min (ZV_BYTE, max (BEGV_BYTE, BYTEPOS (startp)));
+  CLIP_TEXT_POS_FROM_MARKER (startp, w->start);
   start_display (&it, w, startp);
   /* start_display takes into account the header-line row, but IT's
      vpos still counts from the glyph row that includes the window's
@@ -5163,7 +5181,7 @@ buffer_posn_from_coords (struct window *w, int *x, int *y, struct display_pos *p
   *dx = x0 + it.first_visible_x - it.current_x;
   *dy = *y - it.current_y;
 
-  string =  w->buffer;
+  string = w->contents;
   if (STRINGP (it.string))
     string = it.string;
   *pos = it.current;
@@ -5181,7 +5199,7 @@ buffer_posn_from_coords (struct window *w, int *x, int *y, struct display_pos *p
       if (STRINGP (it.string))
        BYTEPOS (pos->pos) = string_char_to_byte (string, CHARPOS (pos->pos));
       else
-       BYTEPOS (pos->pos) = buf_charpos_to_bytepos (XBUFFER (w->buffer),
+       BYTEPOS (pos->pos) = buf_charpos_to_bytepos (XBUFFER (w->contents),
                                                     CHARPOS (pos->pos));
     }
 
@@ -5636,19 +5654,19 @@ FILE = nil means just close any termscript file currently open.  */)
   tty = CURTTY ();
 
   if (tty->termscript != 0)
-  {
-    block_input ();
-    fclose (tty->termscript);
-    unblock_input ();
-  }
-  tty->termscript = 0;
+    {
+      block_input ();
+      fclose (tty->termscript);
+      tty->termscript = 0;
+      unblock_input ();
+    }
 
   if (! NILP (file))
     {
       file = Fexpand_file_name (file, Qnil);
-      tty->termscript = fopen (SSDATA (file), "w");
+      tty->termscript = emacs_fopen (SSDATA (file), "w");
       if (tty->termscript == 0)
-       report_file_error ("Opening termscript", Fcons (file, Qnil));
+       report_file_error ("Opening termscript", file);
     }
   return Qnil;
 }
@@ -5728,7 +5746,7 @@ bitch_at_user (void)
     {
       const char *msg
        = "Keyboard macro terminated by a command ringing the bell";
-      Fsignal (Quser_error, Fcons (build_string (msg), Qnil));
+      Fsignal (Quser_error, list1 (build_string (msg)));
     }
   else
     ring_bell (XFRAME (selected_frame));
@@ -5756,11 +5774,11 @@ additional wait period, in milliseconds; this is for backwards compatibility.
       duration += XINT (milliseconds) / 1000.0;
     }
 
-  if (0 < duration)
+  if (duration > 0)
     {
-      EMACS_TIME t = EMACS_TIME_FROM_DOUBLE (duration);
-      wait_reading_process_output (min (EMACS_SECS (t), WAIT_READING_MAX),
-                                  EMACS_NSECS (t), 0, 0, Qnil, NULL, 0);
+      struct timespec t = dtotimespec (duration);
+      wait_reading_process_output (min (t.tv_sec, WAIT_READING_MAX),
+                                  t.tv_nsec, 0, 0, Qnil, NULL, 0);
     }
 
   return Qnil;
@@ -5796,7 +5814,7 @@ sit_for (Lisp_Object timeout, bool reading, int display_option)
   if (INTEGERP (timeout))
     {
       sec = XINT (timeout);
-      if (! (0 < sec))
+      if (sec <= 0)
        return Qt;
       nsec = 0;
     }
@@ -5807,9 +5825,9 @@ sit_for (Lisp_Object timeout, bool reading, int display_option)
        return Qt;
       else
        {
-         EMACS_TIME t = EMACS_TIME_FROM_DOUBLE (seconds);
-         sec = min (EMACS_SECS (t), WAIT_READING_MAX);
-         nsec = EMACS_NSECS (t);
+         struct timespec t = dtotimespec (seconds);
+         sec = min (t.tv_sec, WAIT_READING_MAX);
+         nsec = t.tv_nsec;
        }
     }
   else if (EQ (timeout, Qt))
@@ -5913,9 +5931,8 @@ pass nil for VARIABLE.  */)
        goto changed;
     }
   /* Check that the buffer info matches.  */
-  for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
+  FOR_EACH_LIVE_BUFFER (tail, buf)
     {
-      buf = XCDR (XCAR (tail));
       /* Ignore buffers that aren't included in buffer lists.  */
       if (SREF (BVAR (XBUFFER (buf), name), 0) == ' ')
        continue;
@@ -5945,7 +5962,7 @@ pass nil for VARIABLE.  */)
   n = 1;
   FOR_EACH_FRAME (tail, frame)
     n += 2;
-  for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
+  FOR_EACH_LIVE_BUFFER (tail, buf)
     n += 3;
   /* Reallocate the vector if data has grown to need it,
      or if it has shrunk a lot.  */
@@ -5970,9 +5987,8 @@ pass nil for VARIABLE.  */)
       ASET (state, idx, XFRAME (frame)->name);
       idx++;
     }
-  for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
+  FOR_EACH_LIVE_BUFFER (tail, buf)
     {
-      buf = XCDR (XCAR (tail));
       /* Ignore buffers that aren't included in buffer lists.  */
       if (SREF (BVAR (XBUFFER (buf), name), 0) == ' ')
        continue;
@@ -6070,7 +6086,7 @@ init_display (void)
 #ifdef HAVE_X11
       Vwindow_system_version = make_number (11);
 #endif
-#ifdef GNU_LINUX
+#ifdef USE_NCURSES
       /* In some versions of ncurses,
         tputs crashes if we have not called tgetent.
         So call tgetent.  */
@@ -6156,15 +6172,14 @@ init_display (void)
 
     /* Update frame parameters to reflect the new type. */
     Fmodify_frame_parameters
-      (selected_frame, Fcons (Fcons (Qtty_type,
-                                     Ftty_type (selected_frame)), Qnil));
+      (selected_frame, list1 (Fcons (Qtty_type,
+                                     Ftty_type (selected_frame))));
     if (t->display_info.tty->name)
-      Fmodify_frame_parameters (selected_frame,
-                               Fcons (Fcons (Qtty, build_string (t->display_info.tty->name)),
-                                      Qnil));
+      Fmodify_frame_parameters
+       (selected_frame,
+        list1 (Fcons (Qtty, build_string (t->display_info.tty->name))));
     else
-      Fmodify_frame_parameters (selected_frame, Fcons (Fcons (Qtty, Qnil),
-                                                      Qnil));
+      Fmodify_frame_parameters (selected_frame, list1 (Fcons (Qtty, Qnil)));
   }
 
   {
@@ -6230,19 +6245,6 @@ WINDOW nil or omitted means report on the selected window.  */)
 {
   return decode_any_window (window)->cursor_off_p ? Qnil : Qt;
 }
-
-DEFUN ("last-nonminibuffer-frame", Flast_nonminibuf_frame,
-       Slast_nonminibuf_frame, 0, 0, 0,
-       doc: /* Value is last nonminibuffer frame. */)
-  (void)
-{
-  Lisp_Object frame = Qnil;
-
-  if (last_nonminibuf_frame)
-    XSETFRAME (frame, last_nonminibuf_frame);
-
-  return frame;
-}
 \f
 /***********************************************************************
                            Initialization
@@ -6261,7 +6263,6 @@ syms_of_display (void)
   defsubr (&Ssend_string_to_terminal);
   defsubr (&Sinternal_show_cursor);
   defsubr (&Sinternal_show_cursor_p);
-  defsubr (&Slast_nonminibuf_frame);
 
 #ifdef GLYPH_DEBUG
   defsubr (&Sdump_redisplay_history);