XEmacs 21.2.32 "Kastor & Polydeukes".
[chise/xemacs-chise.git.1] / src / redisplay.c
index fa3e3b3..6fe354b 100644 (file)
@@ -156,7 +156,7 @@ typedef struct position_redisplay_data_type
   Bytind bi_start_col_enabled;
   int start_col_xoffset;       /* Number of pixels that still need to
                           be skipped.  This is used for
-                          horizontal scrolling of glyphs, where we want 
+                          horizontal scrolling of glyphs, where we want
                           to be able to scroll over part of the glyph. */
 
   int hscroll_glyph_width_adjust;  /* how much the width of the hscroll
@@ -246,18 +246,13 @@ typedef struct
 } prop_block_dynarr;
 
 
-static void generate_formatted_string_db (Lisp_Object format_str,
-                                         Lisp_Object result_str,
-                                         struct window *w,
-                                         struct display_line *dl,
-                                         struct display_block *db,
-                                         face_index findex, int min_pixpos,
-                                         int max_pixpos, int type);
 static Charcount generate_fstring_runes (struct window *w, pos_data *data,
                                         Charcount pos, Charcount min_pos,
                                         Charcount max_pos, Lisp_Object elt,
                                         int depth, int max_pixsize,
-                                        face_index findex, int type);
+                                        face_index findex, int type,
+                                        Charcount *offset,
+                                        Lisp_Object cur_ext);
 static prop_block_dynarr *add_glyph_rune (pos_data *data,
                                          struct glyph_block *gb,
                                          int pos_type, int allow_cursor,
@@ -300,10 +295,6 @@ static int max_preempts;
    isn't any reason we need more than a single set. */
 display_line_dynarr *cmotion_display_lines;
 
-/* Used by generate_formatted_string.  Global because they get used so
-   much that the dynamic allocation time adds up. */
-static Emchar_dynarr *formatted_string_emchar_dynarr;
-static struct display_line formatted_string_display_line;
 /* We store the extents that we need to generate in a Dynarr and then
    frob them all on at the end of generating the string.  We do it
    this way rather than adding them as we generate the string because
@@ -373,8 +364,7 @@ int frame_changed;
 int glyphs_changed;
 int glyphs_changed_set;
 
-/* non-zero if any displayed subwindow is in need of updating
-   somewhere. */
+/* non-zero if any subwindow has been deleted. */
 int subwindows_changed;
 int subwindows_changed_set;
 
@@ -428,10 +418,9 @@ int windows_structure_changed;
 Lisp_Object Vbar_cursor;
 Lisp_Object Qbar_cursor;
 
-
-int visible_bell;      /* If true and the terminal will support it
-                          then the frame will flash instead of
-                          beeping when an error occurs */
+Lisp_Object Vvisible_bell;     /* If true and the terminal will support it
+                                  then the frame will flash instead of
+                                  beeping when an error occurs */
 
 /* Nonzero means no need to redraw the entire frame on resuming
    a suspended Emacs.  This is useful on terminals with multiple pages,
@@ -481,6 +470,8 @@ Lisp_Object Vtext_cursor_visible_p;
 
 int column_number_start_at_one;
 
+Lisp_Object Qtop_bottom;
+
 #define WINDOW_SCROLLED(w) \
 (w->hscroll > 0 || w->left_xoffset)
 
@@ -869,7 +860,7 @@ add_emchar_rune (pos_data *data)
          Lisp_Object font_instance =
            ensure_face_cachel_contains_charset (cachel, data->window,
                                                 charset);
-         struct Lisp_Font_Instance *fi;
+         Lisp_Font_Instance *fi;
 
          if (EQ (font_instance, Vthe_null_font_instance))
            {
@@ -1351,7 +1342,7 @@ add_disp_table_entry_runes (pos_data *data, Lisp_Object entry)
   prop_block_dynarr *prop = NULL;
   if (VECTORP (entry))
     {
-      struct Lisp_Vector *de = XVECTOR (entry);
+      Lisp_Vector *de = XVECTOR (entry);
       EMACS_INT len = vector_length (de);
       int elt;
 
@@ -1534,6 +1525,18 @@ add_glyph_rune (pos_data *data, struct glyph_block *gb, int pos_type,
 {
   struct window *w = XWINDOW (data->window);
 
+  /* If window faces changed, and glyph instance is text, then
+     glyph sizes might have changed too */
+  invalidate_glyph_geometry_maybe (gb->glyph, w);
+
+  /* This makes sure the glyph is in the cachels.
+
+     #### We do this to make sure the glyph is in the glyph cachels,
+     so that the dirty flag can be reset after redisplay has
+     finished. We should do this some other way, maybe by iterating
+     over the window cache of subwindows. */
+  get_glyph_cachel_index (w, gb->glyph);
+
   /* A nil extent indicates a special glyph (ex. truncator). */
   if (NILP (gb->extent)
       || (pos_type == BEGIN_GLYPHS &&
@@ -1551,7 +1554,7 @@ add_glyph_rune (pos_data *data, struct glyph_block *gb, int pos_type,
       if (cachel)
        width = cachel->width;
       else
-       width = glyph_width (gb->glyph, Qnil, data->findex, data->window);
+       width = glyph_width (gb->glyph, data->window);
 
       if (!width)
        return NULL;
@@ -1614,9 +1617,8 @@ add_glyph_rune (pos_data *data, struct glyph_block *gb, int pos_type,
        }
       else
        {
-         ascent = glyph_ascent (gb->glyph, Qnil, data->findex, data->window);
-         descent = glyph_descent (gb->glyph, Qnil, data->findex,
-                                  data->window);
+         ascent = glyph_ascent (gb->glyph, data->window);
+         descent = glyph_descent (gb->glyph, data->window);
        }
 
       baseline = glyph_baseline (gb->glyph, data->window);
@@ -1815,7 +1817,7 @@ create_text_block (struct window *w, struct display_line *dl,
      after a ^M is invisible. */
   int selective = (INTP (b->selective_display)
                   ? XINT (b->selective_display)
-                  : ((!NILP (b->selective_display) ? -1 : 0)));
+                  : (!NILP (b->selective_display) ? -1 : 0));
 
   /* The variable ctl-arrow allows the user to specify what characters
      can actually be displayed and which octal should be used for.
@@ -2288,7 +2290,7 @@ create_text_block (struct window *w, struct display_line *dl,
              int prop_width = 0;
 
              if (data.start_col > 1)
-               tab_start_pixpos -= (space_width (w) * (data.start_col - 1)) 
+               tab_start_pixpos -= (space_width (w) * (data.start_col - 1))
                  + data.start_col_xoffset;
 
              next_tab_start =
@@ -2777,8 +2779,8 @@ add_margin_runes (struct display_line *dl, struct display_block *db, int start,
              unsigned short ascent, descent;
              Lisp_Object baseline = glyph_baseline (gb->glyph, window);
 
-             ascent = glyph_ascent (gb->glyph, Qnil, gb->findex, window);
-             descent = glyph_descent (gb->glyph, Qnil, gb->findex, window);
+             ascent = glyph_ascent (gb->glyph, window);
+             descent = glyph_descent (gb->glyph, window);
 
              /* A pixmap that has not had a baseline explicitly set.
                  We use the existing ascent / descent ratio of the
@@ -2892,7 +2894,7 @@ create_left_glyph_block (struct window *w, struct display_line *dl,
        {
          int width;
 
-         width = glyph_width (gb->glyph, Qnil, gb->findex, window);
+         width = glyph_width (gb->glyph, window);
 
          if (white_in_start - width >= left_in_end)
            {
@@ -2943,7 +2945,7 @@ create_left_glyph_block (struct window *w, struct display_line *dl,
        if (extent_begin_glyph_layout (XEXTENT (gb->extent)) ==
            GL_INSIDE_MARGIN)
          {
-           gb->width = glyph_width (gb->glyph, Qnil, gb->findex, window);
+           gb->width = glyph_width (gb->glyph, window);
            used_in += gb->width;
            Dynarr_add (ib, *gb);
          }
@@ -3012,7 +3014,7 @@ create_left_glyph_block (struct window *w, struct display_line *dl,
        if (extent_begin_glyph_layout (XEXTENT (gb->extent)) ==
            GL_INSIDE_MARGIN)
          {
-           int width = glyph_width (gb->glyph, Qnil, gb->findex, window);
+           int width = glyph_width (gb->glyph, window);
 
            if (used_out)
              {
@@ -3054,7 +3056,7 @@ create_left_glyph_block (struct window *w, struct display_line *dl,
       if (extent_begin_glyph_layout (XEXTENT (gb->extent)) ==
          GL_OUTSIDE_MARGIN)
        {
-         int width = glyph_width (gb->glyph, Qnil, gb->findex, window);
+         int width = glyph_width (gb->glyph, window);
 
          if (out_end + width <= in_out_start)
            {
@@ -3211,7 +3213,7 @@ create_right_glyph_block (struct window *w, struct display_line *dl)
 
       if (extent_end_glyph_layout (XEXTENT (gb->extent)) == GL_WHITESPACE)
        {
-         int width = glyph_width (gb->glyph, Qnil, gb->findex, window);
+         int width = glyph_width (gb->glyph, window);
 
          if (white_in_end + width <= dl->bounds.right_in)
            {
@@ -3261,7 +3263,7 @@ create_right_glyph_block (struct window *w, struct display_line *dl)
 
        if (extent_end_glyph_layout (XEXTENT (gb->extent)) == GL_INSIDE_MARGIN)
          {
-           gb->width = glyph_width (gb->glyph, Qnil, gb->findex, window);
+           gb->width = glyph_width (gb->glyph, window);
            used_in += gb->width;
            Dynarr_add (ib, *gb);
          }
@@ -3325,7 +3327,7 @@ create_right_glyph_block (struct window *w, struct display_line *dl)
 
        if (extent_end_glyph_layout (XEXTENT (gb->extent)) == GL_INSIDE_MARGIN)
          {
-           int width = glyph_width (gb->glyph, Qnil, gb->findex, window);
+           int width = glyph_width (gb->glyph, window);
 
            if (used_out)
              {
@@ -3366,7 +3368,7 @@ create_right_glyph_block (struct window *w, struct display_line *dl)
 
       if (extent_end_glyph_layout (XEXTENT (gb->extent)) == GL_OUTSIDE_MARGIN)
        {
-         int width = glyph_width (gb->glyph, Qnil, gb->findex, window);
+         int width = glyph_width (gb->glyph, window);
 
          if (out_start - width >= in_out_end)
            {
@@ -3482,110 +3484,8 @@ create_right_glyph_block (struct window *w, struct display_line *dl)
 /*                                                                        */
 /***************************************************************************/
 
-/* Ensure that the given display line DL accurately represents the
-   modeline for the given window. */
-
-static void
-generate_modeline (struct window *w, struct display_line *dl, int type)
-{
-  struct buffer *b = XBUFFER (w->buffer);
-  struct frame *f = XFRAME (w->frame);
-  struct device *d = XDEVICE (f->device);
-
-  /* Unlike display line and rune pointers, this one can't change underneath
-     our feet. */
-  struct display_block *db = get_display_block_from_line (dl, TEXT);
-  int max_pixpos, min_pixpos, ypos_adj;
-  Lisp_Object font_inst;
-
-  /* This will actually determine incorrect inside boundaries for the
-     modeline since it ignores the margins.  However being aware of this fact
-     we never use those values anywhere so it doesn't matter. */
-  dl->bounds = calculate_display_line_boundaries (w, 1);
-
-  /* We are generating a modeline. */
-  dl->modeline = 1;
-  dl->cursor_elt = -1;
-
-  /* Reset the runes on the modeline. */
-  Dynarr_reset (db->runes);
-
-  if (!WINDOW_HAS_MODELINE_P (w))
-    {
-      struct rune rb;
-
-      /* If there is a horizontal scrollbar, don't add anything. */
-      if (window_scrollbar_height (w))
-       return;
-
-      dl->ascent = DEVMETH (d, divider_height, ());
-      dl->descent = 0;
-      /* The modeline is at the bottom of the gutters. */
-      dl->ypos = WINDOW_BOTTOM (w);
-
-      /* adjust for the bottom gutter */
-      if (window_is_lowest (w))
-       dl->ypos -= FRAME_BOTTOM_GUTTER_BOUNDS (f);
-
-      rb.findex = MODELINE_INDEX;
-      rb.xpos = dl->bounds.left_out;
-      rb.width = dl->bounds.right_out - dl->bounds.left_out;
-      rb.bufpos = 0;
-      rb.endpos = 0;
-      rb.type = RUNE_HLINE;
-      rb.object.hline.thickness = 1;
-      rb.object.hline.yoffset = 0;
-      rb.cursor_type = NO_CURSOR;
-
-      if (!EQ (Qzero, w->modeline_shadow_thickness)
-         && FRAME_WIN_P (f))
-       {
-         int shadow_thickness = MODELINE_SHADOW_THICKNESS (w);
-
-         dl->ypos -= shadow_thickness;
-         rb.xpos += shadow_thickness;
-         rb.width -= 2 * shadow_thickness;
-       }
-
-      Dynarr_add (db->runes, rb);
-      return;
-    }
-
-  /* !!#### not right; needs to compute the max height of
-     all the charsets */
-  font_inst = WINDOW_FACE_CACHEL_FONT (w, MODELINE_INDEX, Vcharset_ascii);
-
-  dl->ascent = XFONT_INSTANCE (font_inst)->ascent;
-  dl->descent = XFONT_INSTANCE (font_inst)->descent;
-
-  min_pixpos = dl->bounds.left_out;
-  max_pixpos = dl->bounds.right_out;
-
-  if (!EQ (Qzero, w->modeline_shadow_thickness) && FRAME_WIN_P (f))
-    {
-      int shadow_thickness = MODELINE_SHADOW_THICKNESS (w);
-
-      ypos_adj = shadow_thickness;
-      min_pixpos += shadow_thickness;
-      max_pixpos -= shadow_thickness;
-    }
-  else
-    ypos_adj = 0;
-
-  generate_formatted_string_db (b->modeline_format,
-                               b->generated_modeline_string, w, dl, db,
-                               MODELINE_INDEX, min_pixpos, max_pixpos, type);
-
-  /* The modeline is at the bottom of the gutters.  We have to wait to
-     set this until we've generated the modeline in order to account
-     for any embedded faces. */
-  dl->ypos = WINDOW_BOTTOM (w) - dl->descent - ypos_adj;
-  /* adjust for the bottom gutter */
-  if (window_is_lowest (w))
-    dl->ypos -= FRAME_BOTTOM_GUTTER_BOUNDS (f);
-}
-
-static void
+/* This function is also used in frame.c by `generate_title_string' */
+void
 generate_formatted_string_db (Lisp_Object format_str, Lisp_Object result_str,
                               struct window *w, struct display_line *dl,
                               struct display_block *db, face_index findex,
@@ -3596,6 +3496,7 @@ generate_formatted_string_db (Lisp_Object format_str, Lisp_Object result_str,
 
   pos_data data;
   int c_pixpos;
+  Charcount offset = 0;
 
   xzero (data);
   data.d = d;
@@ -3616,9 +3517,14 @@ generate_formatted_string_db (Lisp_Object format_str, Lisp_Object result_str,
   Dynarr_reset (formatted_string_extent_start_dynarr);
   Dynarr_reset (formatted_string_extent_end_dynarr);
 
-  /* This recursively builds up the modeline. */
+  /* result_str is nil when we're building a frame or icon title. Otherwise,
+     we're building a modeline, so the offset starts at the modeline
+     horizontal scrolling ammount */
+  if (! NILP (result_str))
+    offset = w->modeline_hscroll;
   generate_fstring_runes (w, &data, 0, 0, -1, format_str, 0,
-                          max_pixpos - min_pixpos, findex, type);
+                          max_pixpos - min_pixpos, findex, type, &offset,
+                         Qnil);
 
   if (Dynarr_length (db->runes))
     {
@@ -3689,13 +3595,108 @@ generate_formatted_string_db (Lisp_Object format_str, Lisp_Object result_str,
     }
 }
 
+/* Ensure that the given display line DL accurately represents the
+   modeline for the given window. */
+static void
+generate_modeline (struct window *w, struct display_line *dl, int type)
+{
+  struct buffer *b = XBUFFER (w->buffer);
+  struct frame *f = XFRAME (w->frame);
+  struct device *d = XDEVICE (f->device);
+
+  /* Unlike display line and rune pointers, this one can't change underneath
+     our feet. */
+  struct display_block *db = get_display_block_from_line (dl, TEXT);
+  int max_pixpos, min_pixpos, ypos_adj;
+  Lisp_Object font_inst;
+
+  /* This will actually determine incorrect inside boundaries for the
+     modeline since it ignores the margins.  However being aware of this fact
+     we never use those values anywhere so it doesn't matter. */
+  dl->bounds = calculate_display_line_boundaries (w, 1);
+
+  /* We are generating a modeline. */
+  dl->modeline = 1;
+  dl->cursor_elt = -1;
+
+  /* Reset the runes on the modeline. */
+  Dynarr_reset (db->runes);
+
+  if (!WINDOW_HAS_MODELINE_P (w))
+    {
+      struct rune rb;
+
+      /* If there is a horizontal scrollbar, don't add anything. */
+      if (window_scrollbar_height (w))
+       return;
+
+      dl->ascent = DEVMETH (d, divider_height, ());
+      dl->descent = 0;
+      /* The modeline is at the bottom of the gutters. */
+      dl->ypos = WINDOW_BOTTOM (w);
+
+      rb.findex = MODELINE_INDEX;
+      rb.xpos = dl->bounds.left_out;
+      rb.width = dl->bounds.right_out - dl->bounds.left_out;
+      rb.bufpos = 0;
+      rb.endpos = 0;
+      rb.type = RUNE_HLINE;
+      rb.object.hline.thickness = 1;
+      rb.object.hline.yoffset = 0;
+      rb.cursor_type = NO_CURSOR;
+
+      if (!EQ (Qzero, w->modeline_shadow_thickness)
+         && FRAME_WIN_P (f))
+       {
+         int shadow_thickness = MODELINE_SHADOW_THICKNESS (w);
+
+         dl->ypos -= shadow_thickness;
+         rb.xpos += shadow_thickness;
+         rb.width -= 2 * shadow_thickness;
+       }
+
+      Dynarr_add (db->runes, rb);
+      return;
+    }
+
+  /* !!#### not right; needs to compute the max height of
+     all the charsets */
+  font_inst = WINDOW_FACE_CACHEL_FONT (w, MODELINE_INDEX, Vcharset_ascii);
+
+  dl->ascent = XFONT_INSTANCE (font_inst)->ascent;
+  dl->descent = XFONT_INSTANCE (font_inst)->descent;
+
+  min_pixpos = dl->bounds.left_out;
+  max_pixpos = dl->bounds.right_out;
+
+  if (!EQ (Qzero, w->modeline_shadow_thickness) && FRAME_WIN_P (f))
+    {
+      int shadow_thickness = MODELINE_SHADOW_THICKNESS (w);
+
+      ypos_adj = shadow_thickness;
+      min_pixpos += shadow_thickness;
+      max_pixpos -= shadow_thickness;
+    }
+  else
+    ypos_adj = 0;
+
+  generate_formatted_string_db (b->modeline_format,
+                               b->generated_modeline_string, w, dl, db,
+                               MODELINE_INDEX, min_pixpos, max_pixpos, type);
+
+  /* The modeline is at the bottom of the gutters.  We have to wait to
+     set this until we've generated the modeline in order to account
+     for any embedded faces. */
+  dl->ypos = WINDOW_BOTTOM (w) - dl->descent - ypos_adj;
+}
+
 static Charcount
-add_string_to_fstring_db_runes (pos_data *data, CONST Bufbyte *str,
+add_string_to_fstring_db_runes (pos_data *data, const Bufbyte *str,
                                 Charcount pos, Charcount min_pos, Charcount max_pos)
 {
   /* This function has been Mule-ized. */
   Charcount end;
-  CONST Bufbyte *cur_pos = str;
+  const Bufbyte *cur_pos = str;
   struct display_block *db = data->db;
 
   data->blank_width = space_width (XWINDOW (data->window));
@@ -3703,13 +3704,13 @@ add_string_to_fstring_db_runes (pos_data *data, CONST Bufbyte *str,
     add_blank_rune (data, NULL, 0);
 
   end = (Dynarr_length (db->runes) +
-         bytecount_to_charcount (str, strlen ((CONST char *) str)));
+         bytecount_to_charcount (str, strlen ((const char *) str)));
   if (max_pos != -1)
     end = min (max_pos, end);
 
   while (pos < end && *cur_pos)
     {
-      CONST Bufbyte *old_cur_pos = cur_pos;
+      const Bufbyte *old_cur_pos = cur_pos;
       int succeeded;
 
       data->ch = charptr_emchar (cur_pos);
@@ -3734,7 +3735,8 @@ add_string_to_fstring_db_runes (pos_data *data, CONST Bufbyte *str,
    modeline extents. */
 static Charcount
 add_glyph_to_fstring_db_runes (pos_data *data, Lisp_Object glyph,
-                               Charcount pos, Charcount min_pos, Charcount max_pos)
+                               Charcount pos, Charcount min_pos,
+                              Charcount max_pos, Lisp_Object extent)
 {
   /* This function has been Mule-ized. */
   Charcount end;
@@ -3750,7 +3752,7 @@ add_glyph_to_fstring_db_runes (pos_data *data, Lisp_Object glyph,
     end = min (max_pos, end);
 
   gb.glyph = glyph;
-  gb.extent = Qnil;
+  gb.extent = extent;
   add_glyph_rune (data, &gb, BEGIN_GLYPHS, 0, 0);
   pos++;
 
@@ -3777,7 +3779,8 @@ static Charcount
 generate_fstring_runes (struct window *w, pos_data *data, Charcount pos,
                         Charcount min_pos, Charcount max_pos,
                         Lisp_Object elt, int depth, int max_pixsize,
-                        face_index findex, int type)
+                        face_index findex, int type, Charcount *offset,
+                       Lisp_Object cur_ext)
 {
   /* This function has been Mule-ized. */
   /* #### The other losing things in this function are:
@@ -3809,13 +3812,22 @@ tail_recurse:
 
           if (this != last)
             {
-              /* The string is just a string. */
+              /* No %-construct */
               Charcount size =
-                bytecount_to_charcount (last, this - last) + pos;
-              Charcount tmp_max = (max_pos == -1 ? size : min (size, max_pos));
+               bytecount_to_charcount (last, this - last);
 
-              pos = add_string_to_fstring_db_runes (data, last, pos, pos,
-                                                    tmp_max);
+             if (size <= *offset)
+               *offset -= size;
+             else
+               {
+                 Charcount tmp_max = (max_pos == -1 ? pos + size - *offset :
+                                      min (pos + size - *offset, max_pos));
+                 const Bufbyte *tmp_last = charptr_n_addr (last, *offset);
+
+                 pos = add_string_to_fstring_db_runes (data, tmp_last,
+                                                       pos, pos, tmp_max);
+                 *offset = 0;
+               }
             }
           else /* *this == '%' */
             {
@@ -3840,7 +3852,7 @@ tail_recurse:
                   pos = generate_fstring_runes (w, data, pos, spec_width,
                                                 max_pos, Vglobal_mode_string,
                                                 depth, max_pixsize, findex,
-                                                type);
+                                                type, offset, cur_ext);
                 }
               else if (*this == '-')
                 {
@@ -3867,17 +3879,35 @@ tail_recurse:
 
                   while (num_to_add--)
                     pos = add_string_to_fstring_db_runes
-                      (data, (CONST Bufbyte *) "-", pos, pos, max_pos);
+                      (data, (const Bufbyte *) "-", pos, pos, max_pos);
                 }
               else if (*this != 0)
                 {
-                  Bufbyte *str;
                   Emchar ch = charptr_emchar (this);
+                  Bufbyte *str;
+                 Charcount size;
+
                   decode_mode_spec (w, ch, type);
 
                   str = Dynarr_atp (mode_spec_bufbyte_string, 0);
-                  pos = add_string_to_fstring_db_runes (data,str, pos, pos,
-                                                        max_pos);
+                 size = bytecount_to_charcount
+                   /* Skip the null character added by `decode_mode_spec' */
+                   (str, Dynarr_length (mode_spec_bufbyte_string)) - 1;
+
+                 if (size <= *offset)
+                   *offset -= size;
+                 else
+                   {
+                     const Bufbyte *tmp_str = charptr_n_addr (str, *offset);
+
+                     /* #### NOTE: I don't understand why a tmp_max is not
+                        computed and used here as in the plain string case
+                        above. -- dv */
+                     pos = add_string_to_fstring_db_runes (data, tmp_str,
+                                                           pos, pos,
+                                                           max_pos);
+                     *offset = 0;
+                   }
                 }
 
               /* NOT this++.  There could be any sort of character at
@@ -3903,13 +3933,26 @@ tail_recurse:
 
       if (!UNBOUNDP (tem))
         {
-          /* If value is a string, output that string literally:
+         /* If value is a string, output that string literally:
              don't check for % within it.  */
           if (STRINGP (tem))
             {
-              pos =
-                add_string_to_fstring_db_runes
-                (data, XSTRING_DATA (tem), pos, min_pos, max_pos);
+             Bufbyte *str = XSTRING_DATA (tem);
+             Charcount size = XSTRING_CHAR_LENGTH (tem);
+
+             if (size <= *offset)
+               *offset -= size;
+             else
+               {
+                 const Bufbyte *tmp_str = charptr_n_addr (str, *offset);
+
+                 /* #### NOTE: I don't understand why a tmp_max is not
+                    computed and used here as in the plain string case
+                    above. -- dv */
+                 pos = add_string_to_fstring_db_runes (data, tmp_str, pos,
+                                                       min_pos, max_pos);
+                 *offset = 0;
+               }
             }
           /* Give up right away for nil or t.  */
           else if (!EQ (tem, elt))
@@ -3934,50 +3977,53 @@ tail_recurse:
   else if (CONSP (elt))
     {
       /* A cons cell: four distinct cases.
-       * If first element is a string or a cons, process all the elements
-       * and effectively concatenate them.
-       * If first element is a negative number, truncate displaying cdr to
-       * at most that many characters.  If positive, pad (with spaces)
-       * to at least that many characters.
-       * If first element is a symbol, process the cadr or caddr recursively
-       * according to whether the symbol's value is non-nil or nil.
-       * If first element is a face, process the cdr recursively
-       * without altering the depth.
+       * - If first element is a string or a cons, process all the elements
+       *   and effectively concatenate them.
+       * - If first element is a negative number, truncate displaying cdr to
+       *   at most that many characters.  If positive, pad (with spaces)
+       *   to at least that many characters.
+       * - If first element is another symbol, process the cadr or caddr
+       *   recursively according to whether the symbol's value is non-nil or
+       *   nil.
+       * - If first element is a face, process the cdr recursively
+       *   without altering the depth.
        */
+
       Lisp_Object car, tem;
 
       car = XCAR (elt);
       if (SYMBOLP (car))
-        {
-          elt = XCDR (elt);
-          if (!CONSP (elt))
-            goto invalid;
-          tem = symbol_value_in_buffer (car, w->buffer);
-          /* elt is now the cdr, and we know it is a cons cell.
-             Use its car if CAR has a non-nil value.  */
-          if (!UNBOUNDP (tem))
-            {
-              if (!NILP (tem))
-                {
-                  elt = XCAR (elt);
-                  goto tail_recurse;
-                }
-            }
-          /* Symbol's value is nil (or symbol is unbound)
-           * Get the cddr of the original list
-           * and if possible find the caddr and use that.
-           */
-          elt = XCDR (elt);
-          if (NILP (elt))
-            ;
-          else if (!CONSP (elt))
-            goto invalid;
-          else
-            {
-              elt = XCAR (elt);
-              goto tail_recurse;
-            }
-        }
+       {
+         elt = XCDR (elt);
+         if (!CONSP (elt))
+           goto invalid;
+
+         tem = symbol_value_in_buffer (car, w->buffer);
+         /* elt is now the cdr, and we know it is a cons cell.
+            Use its car if CAR has a non-nil value.  */
+         if (!UNBOUNDP (tem))
+           {
+             if (!NILP (tem))
+               {
+                 elt = XCAR (elt);
+                 goto tail_recurse;
+               }
+           }
+         /* Symbol's value is nil (or symbol is unbound)
+          * Get the cddr of the original list
+          * and if possible find the caddr and use that.
+          */
+         elt = XCDR (elt);
+         if (NILP (elt))
+           ;
+         else if (!CONSP (elt))
+           goto invalid;
+         else
+           {
+             elt = XCAR (elt);
+             goto tail_recurse;
+           }
+       }
       else if (INTP (car))
         {
           Charcount lim = XINT (car);
@@ -4016,13 +4062,14 @@ tail_recurse:
       else if (STRINGP (car) || CONSP (car))
         {
           int limit = 50;
+
           /* LIMIT is to protect against circular lists.  */
           while (CONSP (elt) && --limit > 0
                  && (pos < max_pos || max_pos == -1))
             {
               pos = generate_fstring_runes (w, data, pos, pos, max_pos,
-                                            XCAR (elt), depth,
-                                            max_pixsize, findex, type);
+                                            XCAR (elt), depth, max_pixsize,
+                                           findex, type, offset, cur_ext);
               elt = XCDR (elt);
             }
         }
@@ -4061,7 +4108,8 @@ tail_recurse:
               data->findex = new_findex;
               pos = generate_fstring_runes (w, data, pos, pos, max_pos,
                                             XCDR (elt), depth - 1,
-                                            max_pixsize, new_findex, type);
+                                           max_pixsize, new_findex, type,
+                                           offset, car);
               data->findex = old_findex;
               Dynarr_add (formatted_string_extent_dynarr, ext);
               Dynarr_add (formatted_string_extent_start_dynarr, start);
@@ -4071,57 +4119,46 @@ tail_recurse:
     }
   else if (GLYPHP (elt))
     {
-      pos = add_glyph_to_fstring_db_runes (data, elt, pos, pos, max_pos);
+      /* Glyphs are considered as one character with respect to the modeline
+        horizontal scrolling facility. -- dv */
+      if (*offset > 0)
+       *offset -= 1;
+      else
+       pos = add_glyph_to_fstring_db_runes (data, elt, pos, pos, max_pos,
+                                            cur_ext);
     }
   else
     {
     invalid:
-      pos =
-        add_string_to_fstring_db_runes
-          (data, (CONST Bufbyte *) GETTEXT ("*invalid*"), pos, min_pos,
-           max_pos);
+      {
+       char *str = GETTEXT ("*invalid*");
+       Charcount size = (Charcount) strlen (str); /* is this ok ?? -- dv */
+
+       if (size <= *offset)
+         *offset -= size;
+       else
+         {
+           const Bufbyte *tmp_str =
+             charptr_n_addr ((const Bufbyte *) str, *offset);
+
+           /* #### NOTE: I don't understand why a tmp_max is not computed and
+              used here as in the plain string case above. -- dv */
+           pos = add_string_to_fstring_db_runes (data, tmp_str, pos,
+                                                 min_pos, max_pos);
+           *offset = 0;
+         }
+      }
     }
 
   if (min_pos > pos)
     {
-      add_string_to_fstring_db_runes (data, (CONST Bufbyte *) "", pos, min_pos,
-                                      -1);
+      add_string_to_fstring_db_runes (data, (const Bufbyte *) "", pos,
+                                     min_pos, -1);
     }
 
   return pos;
 }
 
-/* The caller is responsible for freeing the returned string. */
-Bufbyte *
-generate_formatted_string (struct window *w, Lisp_Object format_str,
-                          Lisp_Object result_str, face_index findex, int type)
-{
-  struct display_line *dl;
-  struct display_block *db;
-  int elt = 0;
-
-  dl = &formatted_string_display_line;
-  db = get_display_block_from_line (dl, TEXT);
-  Dynarr_reset (db->runes);
-
-  generate_formatted_string_db (format_str, result_str, w, dl, db, findex, 0,
-                                -1, type);
-
-  Dynarr_reset (formatted_string_emchar_dynarr);
-  while (elt < Dynarr_length (db->runes))
-    {
-      if (Dynarr_atp (db->runes, elt)->type == RUNE_CHAR)
-       Dynarr_add (formatted_string_emchar_dynarr,
-                   Dynarr_atp (db->runes, elt)->object.chr.ch);
-      elt++;
-    }
-
-  return
-    convert_emchar_string_into_malloced_string
-    ( Dynarr_atp (formatted_string_emchar_dynarr, 0),
-      Dynarr_length (formatted_string_emchar_dynarr), 0);
-}
-
 /* Update just the modeline.  Assumes the desired display structs.  If
    they do not have a modeline block, it does nothing. */
 static void
@@ -4250,7 +4287,7 @@ create_string_text_block (struct window *w, Lisp_Object disp_string,
      against this case. */
   struct buffer *b = BUFFERP (w->buffer) ? XBUFFER (w->buffer) : 0;
   struct device *d = XDEVICE (f->device);
-  struct Lisp_String* s = XSTRING (disp_string);
+  Lisp_String* s = XSTRING (disp_string);
 
   /* we're working with these a lot so precalculate them */
   Bytecount slen = XSTRING_LENGTH (disp_string);
@@ -4341,9 +4378,9 @@ create_string_text_block (struct window *w, Lisp_Object disp_string,
     }
   else
     {
-      dl->left_margin_findex = 
+      dl->left_margin_findex =
        get_builtin_face_cache_index (w, Vleft_margin_face);
-      dl->right_margin_findex = 
+      dl->right_margin_findex =
        get_builtin_face_cache_index (w, Vright_margin_face);
     }
 
@@ -4380,7 +4417,7 @@ create_string_text_block (struct window *w, Lisp_Object disp_string,
 
   data.start_col = 0;
   /* I don't think we want this, string areas should not scroll with
-     the window 
+     the window
   data.start_col = w->hscroll;
   data.bi_start_col_enabled = (w->hscroll ? bi_start_pos : 0);
   */
@@ -4731,7 +4768,7 @@ done:
 
          if (truncate_win && data.bi_bufpos == bi_string_zv)
            {
-             CONST Bufbyte* endb = charptr_n_addr (string_data (s), bi_string_zv);
+             const Bufbyte* endb = charptr_n_addr (string_data (s), bi_string_zv);
              DEC_CHARPTR (endb);
              if (charptr_emchar (endb) != '\n')
                {
@@ -4849,7 +4886,7 @@ done:
   else
     dl->end_bufpos = buffer_or_string_bytind_to_bufpos (disp_string, data.bi_bufpos) - 1;
   if (truncate_win)
-    data.dl->num_chars = 
+    data.dl->num_chars =
       string_column_at_point (s, dl->end_bufpos, b ? XINT (b->tab_width) : 8);
   else
     /* This doesn't correctly take into account tabs and control
@@ -4889,8 +4926,8 @@ done:
    representation of the buffer contents starting from the given
    position when displayed in the given window.  The display line ends
    when the contents of the line reach the right boundary of the given
-   window. 
-   
+   window.
+
    This is very similar to generate_display_line but with the same
    limitations as create_string_text_block. I have taken the liberty
    of fixing the bytind stuff though.*/
@@ -4905,7 +4942,7 @@ generate_string_display_line (struct window *w, Lisp_Object disp_string,
   Bufpos ret_bufpos;
 
   /* you must set bounds before calling this. */
-  
+
   /* Reset what this line is using. */
   if (dl->display_blocks)
     Dynarr_reset (dl->display_blocks);
@@ -5116,6 +5153,9 @@ regenerate_window (struct window *w, Bufpos start_pos, Bufpos point, int type)
   else
     prop = 0;
 
+  /* Make sure this is set always */
+  /* Note the conversion at end */
+  w->window_end_pos[type] = start_pos;
   while (ypos < yend)
     {
       struct display_line dl;
@@ -5151,7 +5191,7 @@ regenerate_window (struct window *w, Bufpos start_pos, Bufpos point, int type)
 
       /* See if we've been asked to start midway through a line, for
          partial display line scrolling. */
-      if (yclip)               
+      if (yclip)
        {
          dlp->top_clip = yclip;
          yclip = 0;
@@ -5219,10 +5259,14 @@ regenerate_window (struct window *w, Bufpos start_pos, Bufpos point, int type)
     Dynarr_free (prop);
 
   /* #### More not quite right, but close enough. */
-  /* #### Ben sez: apparently window_end_pos[] is measured
+  /* Ben sez: apparently window_end_pos[] is measured
      as the number of characters between the window end and the
      end of the buffer?  This seems rather weirdo.  What's
-     the justification for this? */
+     the justification for this?
+
+     JV sez: Because BUF_Z (b) would be a good initial value, however
+     that can change. This representation allows initalizing with 0.
+  */
   w->window_end_pos[type] = BUF_Z (b) - w->window_end_pos[type];
 
   if (need_modeline)
@@ -5900,7 +5944,7 @@ redisplay_window (Lisp_Object window, int skip_selected)
      the cache purely because glyphs have changed - this is now
      handled by the dirty flag.*/
   if ((!echo_active && b != window_display_buffer (w))
-      || !Dynarr_length (w->glyph_cachels))
+      || !Dynarr_length (w->glyph_cachels) || f->faces_changed)
     reset_glyph_cachels (w);
   else
     mark_glyph_cachels_as_not_updated (w);
@@ -5986,7 +6030,7 @@ redisplay_window (Lisp_Object window, int skip_selected)
          && !f->faces_changed
          && !f->glyphs_changed
          && !f->subwindows_changed
-         && !f->subwindows_state_changed
+         /*      && !f->subwindows_state_changed*/
          && !f->point_changed
          && !f->windows_structure_changed)
        {
@@ -6008,7 +6052,7 @@ redisplay_window (Lisp_Object window, int skip_selected)
              && !f->faces_changed
              && !f->glyphs_changed
              && !f->subwindows_changed
-             && !f->subwindows_state_changed
+             /*              && !f->subwindows_state_changed*/
              && !f->windows_structure_changed)
            {
              if (point_visible (w, pointm, CURRENT_DISP)
@@ -6067,7 +6111,7 @@ redisplay_window (Lisp_Object window, int skip_selected)
           && !f->faces_changed
           && !f->glyphs_changed
           && !f->subwindows_changed
-          && !f->subwindows_state_changed
+          /*      && !f->subwindows_state_changed*/
           && !f->windows_structure_changed
           && !f->frame_changed
           && !truncation_changed
@@ -6164,7 +6208,9 @@ regeneration_done:
      somewhere else once tty updates occur on a per-frame basis. */
   mark_face_cachels_as_clean (w);
 
-  /* The glyph cachels only get dirty if someone changed something. */
+  /* The glyph cachels only get dirty if someone changed something.
+   Since redisplay has now effectively ended we can reset the dirty
+   flag since everything must be up-to-date. */
   if (glyphs_changed)
     mark_glyph_cachels_as_clean (w);
 
@@ -6247,7 +6293,7 @@ call_redisplay_end_triggers (struct window *w, void *closure)
 
 /* Ensure that all windows on the given frame are correctly displayed. */
 
-static int
+int
 redisplay_frame (struct frame *f, int preemption_check)
 {
   struct device *d = XDEVICE (f->device);
@@ -6289,29 +6335,26 @@ redisplay_frame (struct frame *f, int preemption_check)
      being handled. */
   update_frame_menubars (f);
 #endif /* HAVE_MENUBARS */
-  /* widgets are similar to menus in that they can call lisp to
-     determine activation etc. Therefore update them before we get
-     into redisplay. This is primarily for connected widgets such as
-     radio buttons. */
-  update_frame_subwindows (f);
 #ifdef HAVE_TOOLBARS
   /* Update the toolbars. */
   update_frame_toolbars (f);
 #endif /* HAVE_TOOLBARS */
+  /* Gutter update proper has to be done inside display when no frame
+     size changes can occur, thus we separately update the gutter
+     geometry here if it needs it. */
+  update_frame_gutter_geometry (f);
 
   /* If we clear the frame we have to force its contents to be redrawn. */
   if (f->clear)
     f->frame_changed = 1;
 
-  /* invalidate the subwindow cache. We use subwindows_changed here to
+  /* Invalidate the subwindow cache. We use subwindows_changed here to
      cause subwindows to get instantiated. This is because
      subwindows_state_changed is less strict - dealing with things
      like the clicked state of button. We have to do this before
      redisplaying the gutters as subwindows get unmapped in the
      process.*/
-  if (!Dynarr_length (f->subwindow_cachels)
-      || f->subwindows_changed
-      || f->frame_changed)
+  if (f->frame_changed || f->subwindows_changed)
     {
       reset_subwindow_cachels (f);
       /* we have to do this so the gutter gets regenerated. */
@@ -6319,9 +6362,6 @@ redisplay_frame (struct frame *f, int preemption_check)
     }
   else
     mark_subwindow_cachels_as_not_updated (f);
-  /* We can now update the gutters, safe in the knowledge that our
-     efforts won't get undone. */
-  update_frame_gutters (f);
 
   hold_frame_size_changes ();
 
@@ -6349,6 +6389,16 @@ redisplay_frame (struct frame *f, int preemption_check)
      #### If a frame-size change does occur we should probably
      actually be preempting redisplay. */
 
+  /* We can now update the gutters, safe in the knowledge that our
+     efforts won't get undone. */
+
+  /* This can call lisp, but redisplay is protected by binding
+     inhibit_quit.  More importantly the code involving display lines
+     *assumes* that GC will not happen and so does not GCPRO
+     anything. Since we use this code the whole time with the gutters
+     we cannot allow GC to happen when manipulating the gutters. */
+  update_frame_gutters (f);
+
   /* Erase the frame before outputting its contents. */
   if (f->clear)
     {
@@ -6388,16 +6438,27 @@ redisplay_frame (struct frame *f, int preemption_check)
   return 0;
 }
 
-/* Ensure that all frames on the given device are correctly displayed. */
+/* Ensure that all frames on the given device are correctly displayed.
+   If AUTOMATIC is non-zero, and the device implementation indicates
+   no automatic redisplay, as printers do, then the device is not
+   redisplayed. AUTOMATIC is set to zero when called from lisp
+   functions (redraw-device) and (redisplay-device), and to non-zero
+   when called from "lazy" redisplay();
+*/
 
 static int
-redisplay_device (struct device *d)
+redisplay_device (struct device *d, int automatic)
 {
   Lisp_Object frame, frmcons;
   int preempted = 0;
   int size_change_failed = 0;
   struct frame *f;
 
+  if (automatic
+      && (MAYBE_INT_DEVMETH (d, device_implementation_flags, ())
+         & XDEVIMPF_NO_AUTO_REDISPLAY))
+    return 0;
+
   if (DEVICE_STREAM_P (d)) /* nothing to do */
     return 0;
 
@@ -6512,7 +6573,7 @@ redisplay_without_hooks (void)
 
       if (CLASS_REDISPLAY_FLAGS_CHANGEDP (d))
        {
-         preempted = redisplay_device (d);
+         preempted = redisplay_device (d, 1);
 
          if (preempted)
            {
@@ -6626,7 +6687,7 @@ static void
 decode_mode_spec (struct window *w, Emchar spec, int type)
 {
   Lisp_Object obj = Qnil;
-  CONST char *str = NULL;
+  const char *str = NULL;
   struct buffer *b = XBUFFER (w->buffer);
 
   Dynarr_reset (mode_spec_bufbyte_string);
@@ -6655,7 +6716,7 @@ decode_mode_spec (struct window *w, Emchar spec, int type)
        long_to_string (buf, col);
 
        Dynarr_add_many (mode_spec_bufbyte_string,
-                        (CONST Bufbyte *) buf, strlen (buf));
+                        (const Bufbyte *) buf, strlen (buf));
 
        goto decode_mode_spec_done;
       }
@@ -6992,7 +7053,9 @@ mark_glyph_block_dynarr (glyph_block_dynarr *gba)
     }
 }
 
-static void
+/* See the comment in image_instantiate_cache_result as to why marking
+   the glyph will also mark the image_instance. */
+void
 mark_redisplay_structs (display_line_dynarr *dla)
 {
   display_line *dl = Dynarr_atp (dla, 0);
@@ -7052,6 +7115,7 @@ mark_redisplay (void)
       struct frame *f = XFRAME (XCAR (frmcons));
       update_frame_window_mirror (f);
       mark_window_mirror (f->root_mirror);
+      mark_gutters (f);
     }
 }
 \f
@@ -7136,7 +7200,7 @@ update_internal_cache_list (struct window *w, int type)
       else
        {
          struct line_start_cache lsc;
-         
+
          lsc.start = dl->bufpos;
          lsc.end = dl->end_bufpos;
          lsc.height = dl->ascent + dl->descent;
@@ -7488,11 +7552,18 @@ point_would_be_visible (struct window *w, Bufpos startp, Bufpos point)
    displayed.  The end of the last line is also know as the window end
    position.
 
+   WARNING: It is possible that rediplay failed to layout any lines for the
+   windows. Under normal circumstances this is rare. However it seems that it
+   does occur in the following situation: A mouse event has come in and we
+   need to compute its location in a window. That code (in
+   pixel_to_glyph_translation) already can handle 0 as an error return value.
+
    #### With a little work this could probably be reworked as just a
    call to start_with_line_at_pixpos. */
 
 static Bufpos
-start_end_of_last_line (struct window *w, Bufpos startp, int end)
+start_end_of_last_line (struct window *w, Bufpos startp, int end,
+                        int may_error)
 {
   struct buffer *b = XBUFFER (w->buffer);
   line_start_cache_dynarr *cache = w->line_start_cache;
@@ -7512,7 +7583,7 @@ start_end_of_last_line (struct window *w, Bufpos startp, int end)
 
   start_elt = point_in_line_start_cache (w, cur_start, 0);
   if (start_elt == -1)
-    abort ();  /* this had better never happen */
+      return may_error ? 0 : startp;
 
   while (1)
     {
@@ -7576,7 +7647,7 @@ start_end_of_last_line (struct window *w, Bufpos startp, int end)
 Bufpos
 start_of_last_line (struct window *w, Bufpos startp)
 {
-  return start_end_of_last_line (w, startp, 0);
+  return start_end_of_last_line (w, startp, 0 , 0);
 }
 
 /* For the given window W, if display starts at STARTP, what will be
@@ -7586,9 +7657,16 @@ start_of_last_line (struct window *w, Bufpos startp)
 Bufpos
 end_of_last_line (struct window *w, Bufpos startp)
 {
-  return start_end_of_last_line (w, startp, 1);
+  return start_end_of_last_line (w, startp, 1, 0);
 }
 
+static Bufpos
+end_of_last_line_may_error (struct window *w, Bufpos startp)
+{
+  return start_end_of_last_line (w, startp, 1, 1);
+}
+
+
 /* For window W, what does the starting position have to be so that
    the line containing POINT will cover pixel position PIXPOS. */
 
@@ -7657,7 +7735,7 @@ start_with_line_at_pixpos (struct window *w, Bufpos point, int pixpos)
          assert (cur_elt >= -1);
          /* This used to be cur_elt>=0 under the assumption that if
             point is in the top line and not at BUF_BEGV, then
-            setting the window_start to a newline before the start of 
+            setting the window_start to a newline before the start of
             the first line will always cause scrolling.
 
             However in my (jv) opinion this is wrong.  That new line
@@ -7667,7 +7745,7 @@ start_with_line_at_pixpos (struct window *w, Bufpos point, int pixpos)
             on that assert.  So we have no option but to continue the
             search if we found point at the top of the line_start_cache
             again. */
-         cur_pos = Dynarr_atp (w->line_start_cache,0)->start;       
+         cur_pos = Dynarr_atp (w->line_start_cache,0)->start;
        }
       prev_pos = cur_pos;
     }
@@ -8742,7 +8820,7 @@ pixel_to_glyph_translation (struct frame *f, int x_coord, int y_coord,
   if (!MARKERP ((*w)->start[CURRENT_DISP]))
     *closest = 0;
   else
-    *closest = end_of_last_line (*w,
+    *closest = end_of_last_line_may_error (*w,
                                 marker_position ((*w)->start[CURRENT_DISP]));
   *col = 0;
   UPDATE_CACHE_RETURN;
@@ -8830,6 +8908,9 @@ input and is guaranteed to proceed to completion.
   f->clear = 1;
   redisplay_frame (f, 1);
 
+  /* See the comment in Fredisplay_frame. */
+  RESET_CHANGED_SET_FLAGS;
+
   return unbind_to (count, Qnil);
 }
 
@@ -8857,6 +8938,15 @@ input and is guaranteed to proceed to completion.
 
   redisplay_frame (f, 1);
 
+  /* If we don't reset the global redisplay flafs here, subsequent
+     changes to the display will not get registered by redisplay
+     because it thinks it already has registered changes. If you
+     really knew what you were doing you could confuse redisplay by
+     calling Fredisplay_frame while updating another frame. We assume
+     that if you know what you are doing you will not be that
+     stupid. */
+  RESET_CHANGED_SET_FLAGS;
+
   return unbind_to (count, Qnil);
 }
 
@@ -8884,7 +8974,10 @@ input and is guaranteed to proceed to completion.
     {
       XFRAME (XCAR (frmcons))->clear = 1;
     }
-  redisplay_device (d);
+  redisplay_device (d, 0);
+
+  /* See the comment in Fredisplay_frame. */
+  RESET_CHANGED_SET_FLAGS;
 
   return unbind_to (count, Qnil);
 }
@@ -8911,7 +9004,10 @@ input and is guaranteed to proceed to completion.
       disable_preemption++;
     }
 
-  redisplay_device (d);
+  redisplay_device (d, 0);
+
+  /* See the comment in Fredisplay_frame. */
+  RESET_CHANGED_SET_FLAGS;
 
   return unbind_to (count, Qnil);
 }
@@ -9091,14 +9187,18 @@ init_redisplay (void)
   if (!initialized)
 #endif
     {
-      cmotion_display_lines = Dynarr_new (display_line);
-      mode_spec_bufbyte_string = Dynarr_new (Bufbyte);
-      formatted_string_emchar_dynarr = Dynarr_new (Emchar);
-      formatted_string_extent_dynarr = Dynarr_new (EXTENT);
-      formatted_string_extent_start_dynarr = Dynarr_new (Bytecount);
-      formatted_string_extent_end_dynarr = Dynarr_new (Bytecount);
-      internal_cache = Dynarr_new (line_start_cache);
-      xzero (formatted_string_display_line);
+      if (!cmotion_display_lines)
+       cmotion_display_lines = Dynarr_new (display_line);
+      if (!mode_spec_bufbyte_string)
+       mode_spec_bufbyte_string = Dynarr_new (Bufbyte);
+      if (!formatted_string_extent_dynarr)
+       formatted_string_extent_dynarr = Dynarr_new (EXTENT);
+      if (!formatted_string_extent_start_dynarr)
+       formatted_string_extent_start_dynarr = Dynarr_new (Bytecount);
+      if (!formatted_string_extent_end_dynarr)
+       formatted_string_extent_end_dynarr = Dynarr_new (Bytecount);
+      if (!internal_cache)
+       internal_cache = Dynarr_new (line_start_cache);
     }
 
   /* window system is nil when in -batch mode */
@@ -9171,6 +9271,7 @@ syms_of_redisplay (void)
   defsymbol (&Qbar_cursor, "bar-cursor");
   defsymbol (&Qredisplay_end_trigger_functions,
             "redisplay-end-trigger-functions");
+  defsymbol (&Qtop_bottom, "top-bottom");
 
   DEFSUBR (Fredisplay_echo_area);
   DEFSUBR (Fredraw_frame);
@@ -9259,10 +9360,19 @@ If this is zero, point is always centered after it moves off screen.
                     redisplay_variable_changed);
   truncate_partial_width_windows = 1;
 
-  DEFVAR_BOOL ("visible-bell", &visible_bell /*
-*Non-nil means try to flash the frame to represent a bell.
+  DEFVAR_LISP ("visible-bell", &Vvisible_bell /*
+*Non-nil substitutes a visual signal for the audible bell.
+
+Default behavior is to flash the whole screen.  On some platforms,
+special effects are available using the following values:
+
+'display       Flash the whole screen (ie, the default behavior).
+'top-bottom    Flash only the top and bottom lines of the selected frame.
+
+When effects are unavailable on a platform, the visual bell is the
+default, whole screen.  (Currently only X supports any special effects.)
 */ );
-  visible_bell = 0;
+  Vvisible_bell = Qnil;
 
   DEFVAR_BOOL ("no-redraw-on-reenter", &no_redraw_on_reenter /*
 *Non-nil means no need to redraw entire frame after suspending.
@@ -9382,9 +9492,9 @@ This is a specifier; use `set-specifier' to change it.
   Vleft_margin_width = Fmake_specifier (Qnatnum);
   set_specifier_fallback (Vleft_margin_width, list1 (Fcons (Qnil, Qzero)));
   set_specifier_caching (Vleft_margin_width,
-                        slot_offset (struct window, left_margin_width),
+                        offsetof (struct window, left_margin_width),
                         some_window_value_changed,
-                        slot_offset (struct frame, left_margin_width),
+                        offsetof (struct frame, left_margin_width),
                         margin_width_changed_in_frame);
 
   DEFVAR_SPECIFIER ("right-margin-width", &Vright_margin_width /*
@@ -9394,9 +9504,9 @@ This is a specifier; use `set-specifier' to change it.
   Vright_margin_width = Fmake_specifier (Qnatnum);
   set_specifier_fallback (Vright_margin_width, list1 (Fcons (Qnil, Qzero)));
   set_specifier_caching (Vright_margin_width,
-                        slot_offset (struct window, right_margin_width),
+                        offsetof (struct window, right_margin_width),
                         some_window_value_changed,
-                        slot_offset (struct frame, right_margin_width),
+                        offsetof (struct frame, right_margin_width),
                         margin_width_changed_in_frame);
 
   DEFVAR_SPECIFIER ("minimum-line-ascent", &Vminimum_line_ascent /*
@@ -9406,7 +9516,7 @@ This is a specifier; use `set-specifier' to change it.
   Vminimum_line_ascent = Fmake_specifier (Qnatnum);
   set_specifier_fallback (Vminimum_line_ascent, list1 (Fcons (Qnil, Qzero)));
   set_specifier_caching (Vminimum_line_ascent,
-                        slot_offset (struct window, minimum_line_ascent),
+                        offsetof (struct window, minimum_line_ascent),
                         some_window_value_changed,
                         0, 0);
 
@@ -9417,7 +9527,7 @@ This is a specifier; use `set-specifier' to change it.
   Vminimum_line_descent = Fmake_specifier (Qnatnum);
   set_specifier_fallback (Vminimum_line_descent, list1 (Fcons (Qnil, Qzero)));
   set_specifier_caching (Vminimum_line_descent,
-                        slot_offset (struct window, minimum_line_descent),
+                        offsetof (struct window, minimum_line_descent),
                         some_window_value_changed,
                         0, 0);
 
@@ -9429,7 +9539,7 @@ This is a specifier; use `set-specifier' to change it.
   Vuse_left_overflow = Fmake_specifier (Qboolean);
   set_specifier_fallback (Vuse_left_overflow, list1 (Fcons (Qnil, Qnil)));
   set_specifier_caching (Vuse_left_overflow,
-                        slot_offset (struct window, use_left_overflow),
+                        offsetof (struct window, use_left_overflow),
                         some_window_value_changed,
                         0, 0);
 
@@ -9441,7 +9551,7 @@ This is a specifier; use `set-specifier' to change it.
   Vuse_right_overflow = Fmake_specifier (Qboolean);
   set_specifier_fallback (Vuse_right_overflow, list1 (Fcons (Qnil, Qnil)));
   set_specifier_caching (Vuse_right_overflow,
-                        slot_offset (struct window, use_right_overflow),
+                        offsetof (struct window, use_right_overflow),
                         some_window_value_changed,
                         0, 0);
 
@@ -9452,7 +9562,7 @@ This is a specifier; use `set-specifier' to change it.
   Vtext_cursor_visible_p = Fmake_specifier (Qboolean);
   set_specifier_fallback (Vtext_cursor_visible_p, list1 (Fcons (Qnil, Qt)));
   set_specifier_caching (Vtext_cursor_visible_p,
-                        slot_offset (struct window, text_cursor_visible_p),
+                        offsetof (struct window, text_cursor_visible_p),
                         text_cursor_visible_p_changed,
                         0, 0);