(display_prop_end, invisible_text_between_p): Use
[bpt/emacs.git] / src / xdisp.c
index 764f7f3..e5ffabb 100644 (file)
@@ -219,7 +219,7 @@ Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
 Lisp_Object Qredisplay_end_trigger_functions;
 Lisp_Object Qinhibit_point_motion_hooks;
-Lisp_Object QCeval, QCwhen;
+Lisp_Object QCeval, Qwhen, QCfile;
 Lisp_Object Qfontified;
 
 /* Functions called to fontify regions of text.  */
@@ -227,23 +227,23 @@ Lisp_Object Qfontified;
 Lisp_Object Vfontification_functions;
 Lisp_Object Qfontification_functions;
 
-/* Non-zero means draw toolbar buttons raised when the mouse moves
+/* Non-zero means draw tool bar buttons raised when the mouse moves
    over them.  */
 
-int auto_raise_toolbar_buttons_p;
+int auto_raise_tool_bar_buttons_p;
 
-/* Margin around toolbar buttons in pixels.  */
+/* Margin around tool bar buttons in pixels.  */
 
-int toolbar_button_margin;
+int tool_bar_button_margin;
 
-/* Thickness of shadow to draw around toolbar buttons.  */
+/* Thickness of shadow to draw around tool bar buttons.  */
 
-int toolbar_button_relief;
+int tool_bar_button_relief;
 
-/* Non-zero means automatically resize toolbars so that all toolbar
+/* Non-zero means automatically resize tool-bars so that all tool-bar
    items are visible, and no blank lines remain.  */
 
-int auto_resize_toolbars_p;
+int auto_resize_tool_bars_p;
 
 /* Non-nil means don't actually do any redisplay.  */
 
@@ -258,6 +258,7 @@ extern Lisp_Object Qface, Qinvisible, Qimage;
 
 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qheight, Qraise;
+Lisp_Object Qmargin;
 
 /* Non-nil means highlight trailing whitespace.  */
 
@@ -381,11 +382,6 @@ static int scroll_conservatively;
 
 int scroll_margin;
 
-/* Number of characters of overlap to show, when scrolling a one-line
-   window such as a minibuffer.  */
-
-static int minibuffer_scroll_overlap;
-
 /* Number of windows showing the buffer of the selected window (or
    another buffer with the same base buffer).  keyboard.c refers to
    this.  */
@@ -410,37 +406,19 @@ Lisp_Object minibuf_prompt;
 int minibuf_prompt_width;
 int minibuf_prompt_pixel_width;
 
-/* Message to display instead of mini-buffer contents.  This is what
-   the functions error and message make, and command echoing uses it
-   as well.  It overrides the minibuf_prompt as well as the buffer.  */
-
-char *echo_area_glyphs;
-
-/* A Lisp string to display instead of mini-buffer contents, analogous
-   to echo_area_glyphs.  If this is a string, display that string.
-   Otherwise, if echo_area_glyphs is non-null, display that.  */
-
-Lisp_Object echo_area_message;
-
-/* This is the length of the message in echo_area_glyphs or
-   echo_area_message.  */
-
-int echo_area_glyphs_length;
-
-/* Value of echo_area_glyphs when it was last acted on.  If this is
-   nonzero, there is a message on the frame in the mini-buffer and it
-   should be erased as soon as it is no longer requested to appear.  */
-
-char *previous_echo_glyphs;
-Lisp_Object previous_echo_area_message;
-static int previous_echo_glyphs_length;
-
 /* This is the window where the echo area message was displayed.  It
    is always a mini-buffer window, but it may not be the same window
    currently active as a mini-buffer.  */
 
 Lisp_Object echo_area_window;
 
+/* List of pairs (MESSAGE . MULTIBYTE).  The function save_message
+   pushes the current message and the value of
+   message_enable_multibyte on the stack, the function restore_message
+   pops the stack and displays MESSAGE again.  */
+
+Lisp_Object Vmessage_stack;
+
 /* Nonzero means multibyte characters were enabled when the echo area
    message was specified.  */
 
@@ -450,28 +428,6 @@ int message_enable_multibyte;
 
 int update_mode_lines;
 
-/* Smallest number of characters before the gap at any time since last
-   redisplay that finished.  Valid for current buffer when
-   try_window_id can be called.  */
-
-int beg_unchanged;
-
-/* Smallest number of characters after the gap at any time since last
-   redisplay that finished.  Valid for current buffer when
-   try_window_id can be called.  */
-
-int end_unchanged;
-
-/* MODIFF as of last redisplay that finished; if it matches MODIFF,
-   and overlay_unchanged_modified matches OVERLAY_MODIFF, that means
-   beg_unchanged and end_unchanged contain no useful information.  */
-
-int unchanged_modified;
-
-/* OVERLAY_MODIFF as of last redisplay that finished.  */
-
-int overlay_unchanged_modified;
-
 /* Nonzero if window sizes or contents have changed since last
    redisplay that finished */
 
@@ -495,6 +451,35 @@ static int line_number_display_limit_width;
 
 Lisp_Object Vmessage_log_max;
 
+/* Current, index 0, and last displayed echo area message.  Either
+   buffers from echo_buffers, or nil to indicate no message.  */
+
+Lisp_Object echo_area_buffer[2];
+
+/* The buffers referenced from echo_area_buffer.  */
+
+static Lisp_Object echo_buffer[2];
+
+/* A vector saved used in with_area_buffer to reduce consing.  */
+
+static Lisp_Object Vwith_echo_area_save_vector;
+
+/* Non-zero means display_echo_area should display the last echo area
+   message again.  Set by redisplay_preserve_echo_area.  */
+
+static int display_last_displayed_message_p;
+
+/* Nonzero if echo area is being used by print; zero if being used by
+   message.  */
+
+int message_buf_print;
+
+/* Maximum height for resizing mini-windows.  Either a float
+   specifying a fraction of the available height, or an integer
+   specifying a number of lines.  */
+
+static Lisp_Object Vmax_mini_window_height;
+
 /* A scratch glyph row with contents used for generating truncation
    glyphs.  Also used in direct_output_for_insert.  */
 
@@ -600,6 +585,18 @@ enum move_it_result
 \f
 /* Function prototypes.  */
 
+static struct glyph_row *row_containing_pos P_ ((struct window *, int,
+                                                struct glyph_row *,
+                                                struct glyph_row *));
+static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
+static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
+static void clear_garbaged_frames P_ ((void));
+static int current_message_1 P_ ((Lisp_Object *));
+static int truncate_message_1 P_ ((int));
+static int set_message_1 P_ ((char *s, Lisp_Object, int, int));
+static int display_echo_area P_ ((struct window *));
+static int display_echo_area_1 P_ ((struct window *));
+static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
 static int string_char_and_length P_ ((unsigned char *, int, int *));
 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
                                             struct text_pos));
@@ -620,7 +617,7 @@ static void push_it P_ ((struct it *));
 static void pop_it P_ ((struct it *));
 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
 static void redisplay_internal P_ ((int));
-static void echo_area_display P_ ((int));
+static int echo_area_display P_ ((int));
 static void redisplay_windows P_ ((Lisp_Object));
 static void redisplay_window P_ ((Lisp_Object, int));
 static void update_menu_bar P_ ((struct frame *, int));
@@ -685,10 +682,10 @@ static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
 
 #ifdef HAVE_WINDOW_SYSTEM
 
-static void update_toolbar P_ ((struct frame *, int));
-static void build_desired_toolbar_string P_ ((struct frame *f));
-static int redisplay_toolbar P_ ((struct frame *));
-static void display_toolbar_line P_ ((struct it *));
+static void update_tool_bar P_ ((struct frame *, int));
+static void build_desired_tool_bar_string P_ ((struct frame *f));
+static int redisplay_tool_bar P_ ((struct frame *));
+static void display_tool_bar_line P_ ((struct it *));
 
 #endif /* HAVE_WINDOW_SYSTEM */
 
@@ -728,7 +725,7 @@ window_box_width (w, area)
   
   if (!w->pseudo_window_p)
     {
-      width -= FRAME_SCROLL_BAR_WIDTH (f) + 2 * FRAME_FLAGS_AREA_COLS (f);
+      width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FLAGS_AREA_COLS (f);
       
       if (area == TEXT_AREA)
        {
@@ -762,8 +759,8 @@ window_box_height (w)
   if (WINDOW_WANTS_MODELINE_P (w))
     height -= CURRENT_MODE_LINE_HEIGHT (w);
 
-  if (WINDOW_WANTS_TOP_LINE_P (w))
-    height -= CURRENT_TOP_LINE_HEIGHT (w);
+  if (WINDOW_WANTS_HEADER_LINE_P (w))
+    height -= CURRENT_HEADER_LINE_HEIGHT (w);
 
   return height;
 }
@@ -785,7 +782,7 @@ window_box_left (w, area)
   if (!w->pseudo_window_p)
     {
       x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
-           + FRAME_FLAGS_AREA_WIDTH (f));
+           + FRAME_LEFT_FLAGS_AREA_WIDTH (f));
       
       if (area == TEXT_AREA)
        x += window_box_width (w, LEFT_MARGIN_AREA);
@@ -832,8 +829,8 @@ window_box (w, area, box_x, box_y, box_width, box_height)
   *box_x = window_box_left (w, area);
   *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
            + XFASTINT (w->top) * CANON_Y_UNIT (f));
-  if (WINDOW_WANTS_TOP_LINE_P (w))
-    *box_y += CURRENT_TOP_LINE_HEIGHT (w);
+  if (WINDOW_WANTS_HEADER_LINE_P (w))
+    *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
 }
 
 
@@ -1163,11 +1160,11 @@ check_window_end (w)
 
    BASE_FACE_ID is the id of a base face to use.  It must be one of
    DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID or
-   TOP_LINE_FACE_ID for displaying mode lines, or TOOLBAR_FACE_ID for
-   displaying the toolbar.
+   HEADER_LINE_FACE_ID for displaying mode lines, or TOOL_BAR_FACE_ID for
+   displaying the tool-bar.
    
    If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID or
-   TOP_LINE_FACE_ID, the iterator will be initialized to use the
+   HEADER_LINE_FACE_ID, the iterator will be initialized to use the
    corresponding mode line glyph row of the desired matrix of W.  */
 
 void
@@ -1179,11 +1176,9 @@ init_iterator (it, w, charpos, bytepos, row, base_face_id)
      enum face_id base_face_id;
 {
   int highlight_region_p;
-  Lisp_Object value;
 
   /* Some precondition checks.  */
   xassert (w != NULL && it != NULL);
-  xassert (charpos < 0 || current_buffer == XBUFFER (w->buffer));
   xassert (charpos < 0 || (charpos > 0 && charpos <= ZV));
 
   /* If face attributes have been changed since the last redisplay,
@@ -1201,8 +1196,8 @@ init_iterator (it, w, charpos, bytepos, row, base_face_id)
     {
       if (base_face_id == MODE_LINE_FACE_ID)
        row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
-      else if (base_face_id == TOP_LINE_FACE_ID)
-       row = MATRIX_TOP_LINE_ROW (w->desired_matrix);
+      else if (base_face_id == HEADER_LINE_FACE_ID)
+       row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
     }
   
   /* Clear IT.  */
@@ -1355,8 +1350,8 @@ init_iterator (it, w, charpos, bytepos, row, base_face_id)
            it->last_visible_x -= it->continuation_pixel_width;
        }
 
-      it->top_line_p = WINDOW_WANTS_TOP_LINE_P (w);
-      it->current_y = WINDOW_DISPLAY_TOP_LINE_HEIGHT (w) + w->vscroll;
+      it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
+      it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
     }
 
   /* Leave room for a border glyph.  */
@@ -1413,7 +1408,7 @@ start_display (it, w, pos)
 {
   int start_at_line_beg_p;
   struct glyph_row *row;
-  int first_vpos = WINDOW_WANTS_TOP_LINE_P (w) ? 1 : 0;
+  int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
   int first_y;
 
   row = w->desired_matrix->rows + first_vpos;
@@ -1428,16 +1423,6 @@ start_display (it, w, pos)
   if (!start_at_line_beg_p)
     reseat_at_previous_visible_line_start (it);
 
-#if NO_PROMPT_IN_BUFFER
-  /* Take the mini-buffer prompt width into account for tab
-     calculations.  */
-  if (MINI_WINDOW_P (w) && IT_CHARPOS (*it) == BEGV)
-    {
-      /* Why is mini-buffer_prompt_width guaranteed to be set here?  */
-      it->prompt_width = minibuf_prompt_pixel_width;
-    }
-#endif /* NO_PROMPT_IN_BUFFER */
-
   /* If window start is not at a line start, skip forward to POS to
      get the correct continuation_lines_width and current_x.  */
   if (!start_at_line_beg_p)
@@ -2228,8 +2213,11 @@ handle_display_prop (it)
     return HANDLED_NORMALLY;
 
   space_or_image_found_p = 0;
-  if (CONSP (prop) && CONSP (XCAR (prop)))
+  if (CONSP (prop)
+      && CONSP (XCAR (prop))
+      && !EQ (Qmargin, XCAR (XCAR (prop))))
     {
+      /* A list of sub-properties.  */
       while (CONSP (prop))
        {
          if (handle_single_display_prop (it, XCAR (prop), object, position))
@@ -2255,7 +2243,7 @@ handle_display_prop (it)
 }
 
 
-/* Value is the position of the end of the `display' property stating
+/* Value is the position of the end of the `display' property starting
    at START_POS in OBJECT.  */
 
 static struct text_pos
@@ -2266,27 +2254,14 @@ display_prop_end (it, object, start_pos)
 {
   Lisp_Object end;
   struct text_pos end_pos;
-  
-  /* Characters having this form of property are not displayed, so
-     we have to find the end of the property.  */
-  end = Fnext_single_property_change (make_number (start_pos.charpos),
-                                     Qdisplay, object, Qnil);
-  if (NILP (end))
-    {
-      /* A nil value of `end' means there are no changes of the
-        property to the end of the buffer or string.  */
-      if (it->current.overlay_string_index >= 0)
-       end_pos.charpos = XSTRING (it->string)->size;
-      else
-       end_pos.charpos = it->end_charpos;
-    }
-  else
-    end_pos.charpos = XFASTINT (end);
 
-  if (STRINGP (it->string))
+  end = next_single_char_property_change (make_number (CHARPOS (start_pos)),
+                                         Qdisplay, object, Qnil);
+  CHARPOS (end_pos) = XFASTINT (end);
+  if (STRINGP (object))
     compute_string_pos (&end_pos, start_pos, it->string);
   else
-    end_pos.bytepos = CHAR_TO_BYTE (end_pos.charpos);
+    BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
 
   return end_pos;
 }
@@ -2313,19 +2288,16 @@ handle_single_display_prop (it, prop, object, position)
 
   Lisp_Object form;
 
-  /* If PROP is a list of the form `(:when FORM VALUE)', FORM is
-     evaluated.  If the result is nil, VALUE is ignored.  */
+  /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
+     evaluated.  If the result is nil, VALUE is ignored. */
   form = Qt;
-  if (CONSP (prop) && EQ (XCAR (prop), QCwhen))
+  if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
     {
       prop = XCDR (prop);
       if (!CONSP (prop))
        return 0;
       form = XCAR (prop);
       prop = XCDR (prop);
-      if (!CONSP (prop))
-       return 0;
-      prop = XCAR (prop);
     }
 
   if (!NILP (form) && !EQ (form, Qt))
@@ -2450,8 +2422,8 @@ handle_single_display_prop (it, prop, object, position)
     }
   else if (!it->string_from_display_prop_p)
     {
-      /* `(left-margin VALUE)' or `(right-margin VALUE)
-        or `(nil VALUE)' or VALUE.  */
+      /* `((margin left-margin) VALUE)' or `((margin right-margin)
+        VALUE) or `((margin nil) VALUE)' or VALUE. */
       Lisp_Object location, value;
       struct text_pos start_pos;
       int valid_p;
@@ -2466,14 +2438,26 @@ handle_single_display_prop (it, prop, object, position)
         text properties change there.  */
       it->stop_charpos = position->charpos;
 
-      if (CONSP (prop)
-         && !EQ (XCAR (prop), Qspace)
-         && !EQ (XCAR (prop), Qimage))
+      location = Qunbound;
+      if (CONSP (prop) && CONSP (XCAR (prop)))
        {
-         location = XCAR (prop);
+         Lisp_Object tem;
+         
          value = XCDR (prop);
+         if (CONSP (value))
+           value = XCAR (value);
+
+         tem = XCAR (prop);
+         if (EQ (XCAR (tem), Qmargin)
+             && (tem = XCDR (tem),
+                 tem = CONSP (tem) ? XCAR (tem) : Qnil,
+                 (NILP (tem)
+                  || EQ (tem, Qleft_margin)
+                  || EQ (tem, Qright_margin))))
+           location = tem;
        }
-      else
+
+      if (EQ (location, Qunbound))
        {
          location = Qnil;
          value = prop;
@@ -2725,9 +2709,9 @@ load_overlay_strings (it)
   /* Process overlay before the overlay center.  */
   for (ov = current_buffer->overlays_before;
        CONSP (ov);
-       ov = XCONS (ov)->cdr)
+       ov = XCDR (ov))
     {
-      overlay = XCONS (ov)->car;
+      overlay = XCAR (ov);
       xassert (OVERLAYP (overlay));
       start = OVERLAY_POSITION (OVERLAY_START (overlay));
       end = OVERLAY_POSITION (OVERLAY_END (overlay));
@@ -2761,9 +2745,9 @@ load_overlay_strings (it)
   /* Process overlays after the overlay center.  */
   for (ov = current_buffer->overlays_after;
        CONSP (ov);
-       ov = XCONS (ov)->cdr)
+       ov = XCDR (ov))
     {
-      overlay = XCONS (ov)->car;
+      overlay = XCAR (ov);
       xassert (OVERLAYP (overlay));
       start = OVERLAY_POSITION (OVERLAY_START (overlay));
       end = OVERLAY_POSITION (OVERLAY_END (overlay));
@@ -3314,14 +3298,16 @@ get_next_display_element (it)
             Control characters coming from a display table entry are
             currently not translated because we use IT->dpvec to hold
             the translation.  This could easily be changed but I
-            don't believe that it is worth doing.  */
+            don't believe that it is worth doing.
+
+            Non-printable multibyte characters are also translated
+            octal form.  */
          else if ((it->c < ' '
                    && (it->area != TEXT_AREA
-                       || (it->c != '\n'
-                           && it->c != '\t'
-                           && it->c != '\r')))
+                       || (it->c != '\n' && it->c != '\t')))
                   || (it->c >= 127
-                      && it->len == 1))
+                      && it->len == 1)
+                  || !CHAR_PRINTABLE_P (it->c))
            {
              /* IT->c is a control character which must be displayed
                 either as '\003' or as `^C' where the '\\' and '^'
@@ -3354,29 +3340,37 @@ get_next_display_element (it)
                }
              else
                {
+                 unsigned char work[4], *str;
+                 int len = CHAR_STRING (it->c, work, str);
+                 int i;
+                 GLYPH escape_glyph;
+
                  /* Set IT->ctl_chars[0] to the glyph for `\\'.  */
                  if (it->dp
                      && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
                      && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
-                   g = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
+                   escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
                  else
-                   g = FAST_MAKE_GLYPH ('\\', 0);
-                 XSETINT (it->ctl_chars[0], g);
+                   escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
 
-                 /* Insert three more glyphs into IT->ctl_chars for
-                    the octal display of the character.  */
-                 g = FAST_MAKE_GLYPH (((it->c >> 6) & 7) + '0', 0); 
-                 XSETINT (it->ctl_chars[1], g);
-                 g = FAST_MAKE_GLYPH (((it->c >> 3) & 7) + '0', 0); 
-                 XSETINT (it->ctl_chars[2], g);
-                 g = FAST_MAKE_GLYPH ((it->c & 7) + '0', 0); 
-                 XSETINT (it->ctl_chars[3], g);
+                 for (i = 0; i < len; i++)
+                   {
+                     XSETINT (it->ctl_chars[i * 4], escape_glyph);
+                     /* Insert three more glyphs into IT->ctl_chars for
+                        the octal display of the character.  */
+                     g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0); 
+                     XSETINT (it->ctl_chars[i * 4 + 1], g);
+                     g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0); 
+                     XSETINT (it->ctl_chars[i * 4 + 2], g);
+                     g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0); 
+                     XSETINT (it->ctl_chars[i * 4 + 3], g);
+                   }
 
                  /* Set up IT->dpvec and return the first character
                      from it.  */
                  it->dpvec_char_len = it->len;
                  it->dpvec = it->ctl_chars;
-                 it->dpend = it->dpvec + 4;
+                 it->dpend = it->dpvec + len * 4;
                  it->current.dpvec_index = 0;
                  it->method = next_element_from_display_vector;
                  get_next_display_element (it);
@@ -3460,8 +3454,6 @@ set_iterator_to_next (it)
         Advance in the display table definition.  Reset it to null if
         end reached, and continue with characters from buffers/
         strings.  */
-      struct face *face;
-      
       ++it->current.dpvec_index;
 
       /* Restore face and charset of the iterator to what they were
@@ -3985,7 +3977,7 @@ run_redisplay_end_trigger_hook (it)
      - when we stopped at a line end, i.e. a newline or a CR and selective
      display is on.  */
 
-enum move_it_result
+static enum move_it_result
 move_it_in_display_line_to (it, to_charpos, to_x, op)
      struct it *it;
      int to_charpos, to_x, op;
@@ -3997,16 +3989,6 @@ move_it_in_display_line_to (it, to_charpos, to_x, op)
   saved_glyph_row = it->glyph_row;
   it->glyph_row = NULL;
 
-#if NO_PROMPT_IN_BUFFER
-  /* Take a mini-buffer prompt into account.  */
-  if (MINI_WINDOW_P (it->w)
-      && IT_CHARPOS (*it) == BEGV)
-    {
-      it->current_x = minibuf_prompt_pixel_width;
-      it->hpos = minibuf_prompt_width;
-    }
-#endif
-
   while (1)
     {
       int x, i;
@@ -4168,8 +4150,6 @@ move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
   enum move_it_result skip, skip2 = MOVE_X_REACHED;
   int line_height;
 
-  xassert (XBUFFER (it->w->buffer) == current_buffer);
-
   while (1)
     {
       if (op & MOVE_TO_VPOS)
@@ -4437,10 +4417,9 @@ invisible_text_between_p (it, start_charpos, end_charpos)
     invisible_found_p = 1;
   else
     {
-      limit = Fnext_single_property_change (make_number (start_charpos),
-                                           Qinvisible,
-                                           Fcurrent_buffer (),
-                                           make_number (end_charpos));
+      limit = next_single_char_property_change (make_number (start_charpos),
+                                               Qinvisible, Qnil,
+                                               make_number (end_charpos));
       invisible_found_p = XFASTINT (limit) < end_charpos;
     }
 
@@ -4592,6 +4571,37 @@ move_it_by_lines (it, dvpos, need_y_p)
  ***********************************************************************/
 
 
+/* Add a message with format string FORMAT and arguments ARG1 and ARG2
+   to *Messages*.  */
+
+void
+add_to_log (format, arg1, arg2)
+     char *format;
+     Lisp_Object arg1, arg2;
+{
+  Lisp_Object args[3];
+  Lisp_Object msg, fmt;
+  char *buffer;
+  int len;
+  struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
+
+  fmt = msg = Qnil;
+  GCPRO4 (fmt, msg, arg1, arg2);
+  
+  args[0] = fmt = build_string (format);
+  args[1] = arg1;
+  args[2] = arg2;
+  msg = Fformat (make_number (3), args);
+
+  len = STRING_BYTES (XSTRING (msg)) + 1;
+  buffer = (char *) alloca (len);
+  strcpy (buffer, XSTRING (msg)->data);
+  
+  message_dolog (buffer, len, 1, 0);
+  UNGCPRO;
+}
+
+
 /* Output a newline in the *Messages* buffer if "needs" one.  */
 
 void
@@ -4831,6 +4841,7 @@ message2_nolog (m, len, multibyte)
      char *m;
      int len;
 {
+  struct frame *sf = SELECTED_FRAME ();
   message_enable_multibyte = multibyte;
 
   if (noninteractive)
@@ -4848,55 +4859,49 @@ message2_nolog (m, len, multibyte)
      initialized yet.  Error messages get reported properly by
      cmd_error, so this must be just an informative message; toss it.  */
   else if (INTERACTIVE 
-          && selected_frame->glyphs_initialized_p
-          && FRAME_MESSAGE_BUF (selected_frame))
+          && sf->glyphs_initialized_p
+          && FRAME_MESSAGE_BUF (sf))
     {
       Lisp_Object mini_window;
       struct frame *f;
 
       /* Get the frame containing the mini-buffer
         that the selected frame is using.  */
-      mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
+      mini_window = FRAME_MINIBUF_WINDOW (sf);
       f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
 
       FRAME_SAMPLE_VISIBILITY (f);
-      if (FRAME_VISIBLE_P (selected_frame)
+      if (FRAME_VISIBLE_P (sf)
          && ! FRAME_VISIBLE_P (f))
        Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
 
       if (m)
        {
-         echo_area_glyphs = m;
-         echo_area_glyphs_length = len;
-         echo_area_message = Qnil;
-
+         set_message (m, Qnil, len, multibyte);
          if (minibuffer_auto_raise)
            Fraise_frame  (WINDOW_FRAME (XWINDOW (mini_window)));
        }
       else
-       {
-         echo_area_glyphs = previous_echo_glyphs = NULL;
-         echo_area_message = previous_echo_area_message = Qnil;
-       }
+       clear_message (1, 1);
 
-      do_pending_window_change ();
+      do_pending_window_change (0);
       echo_area_display (1);
-      do_pending_window_change ();
+      do_pending_window_change (0);
       if (frame_up_to_date_hook != 0 && ! gc_in_progress)
        (*frame_up_to_date_hook) (f);
     }
 }
 
 
-/* Display an echo area message M with a specified length of LEN
-   chars.  The string may include null characters.  If M is not a
+/* Display an echo area message M with a specified length of NBYTES
+   bytes.  The string may include null characters.  If M is not a
    string, clear out any existing message, and let the mini-buffer
    text show through.  */
 
 void
-message3 (m, len, multibyte)
+message3 (m, nbytes, multibyte)
      Lisp_Object m;
-     int len;
+     int nbytes;
      int multibyte;
 {
   struct gcpro gcpro1;
@@ -4906,8 +4911,8 @@ message3 (m, len, multibyte)
   /* First flush out any partial line written with print.  */
   message_log_maybe_newline ();
   if (STRINGP (m))
-    message_dolog (XSTRING (m)->data, len, 1, multibyte);
-  message3_nolog (m, len, multibyte);
+    message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
+  message3_nolog (m, nbytes, multibyte);
 
   UNGCPRO;
 }
@@ -4916,10 +4921,11 @@ message3 (m, len, multibyte)
 /* The non-logging version of message3.  */
 
 void
-message3_nolog (m, len, multibyte)
+message3_nolog (m, nbytes, multibyte)
      Lisp_Object m;
-     int len, multibyte;
+     int nbytes, multibyte;
 {
+  struct frame *sf = SELECTED_FRAME ();
   message_enable_multibyte = multibyte;
 
   if (noninteractive)
@@ -4928,7 +4934,7 @@ message3_nolog (m, len, multibyte)
        putc ('\n', stderr);
       noninteractive_need_newline = 0;
       if (STRINGP (m))
-       fwrite (XSTRING (m)->data, len, 1, stderr);
+       fwrite (XSTRING (m)->data, nbytes, 1, stderr);
       if (cursor_in_echo_area == 0)
        fprintf (stderr, "\n");
       fflush (stderr);
@@ -4937,40 +4943,36 @@ message3_nolog (m, len, multibyte)
      initialized yet.  Error messages get reported properly by
      cmd_error, so this must be just an informative message; toss it.  */
   else if (INTERACTIVE 
-          && selected_frame->glyphs_initialized_p
-          && FRAME_MESSAGE_BUF (selected_frame))
+          && sf->glyphs_initialized_p
+          && FRAME_MESSAGE_BUF (sf))
     {
       Lisp_Object mini_window;
+      Lisp_Object frame;
       struct frame *f;
 
       /* Get the frame containing the mini-buffer
         that the selected frame is using.  */
-      mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
-      f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
+      mini_window = FRAME_MINIBUF_WINDOW (sf);
+      frame = XWINDOW (mini_window)->frame;
+      f = XFRAME (frame);
 
       FRAME_SAMPLE_VISIBILITY (f);
-      if (FRAME_VISIBLE_P (selected_frame)
-         && ! FRAME_VISIBLE_P (f))
-       Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
+      if (FRAME_VISIBLE_P (sf)
+         && !FRAME_VISIBLE_P (f))
+       Fmake_frame_visible (frame);
 
-      if (STRINGP (m))
+      if (STRINGP (m) && XSTRING (m)->size)
        {
-         echo_area_glyphs = NULL;
-         echo_area_message = m;
-         echo_area_glyphs_length = len;
-
+         set_message (NULL, m, nbytes, multibyte);
          if (minibuffer_auto_raise)
-           Fraise_frame  (WINDOW_FRAME (XWINDOW (mini_window)));
+           Fraise_frame (frame);
        }
       else
-       {
-         echo_area_glyphs = previous_echo_glyphs = NULL;
-         echo_area_message = previous_echo_area_message = Qnil;
-       }
+       clear_message (1, 1);
 
-      do_pending_window_change ();
+      do_pending_window_change (0);
       echo_area_display (1);
-      do_pending_window_change ();
+      do_pending_window_change (0);
       if (frame_up_to_date_hook != 0 && ! gc_in_progress)
        (*frame_up_to_date_hook) (f);
     }
@@ -5030,11 +5032,11 @@ message_with_string (m, string, log)
         It may be larger than the selected frame, so we need
         to use its buffer, not the selected frame's buffer.  */
       Lisp_Object mini_window;
-      FRAME_PTR f;
+      struct frame *f, *sf = SELECTED_FRAME ();
 
       /* Get the frame containing the minibuffer
         that the selected frame is using.  */
-      mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
+      mini_window = FRAME_MINIBUF_WINDOW (sf);
       f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
 
       /* A null message buffer means that the frame hasn't really been
@@ -5064,27 +5066,6 @@ message_with_string (m, string, log)
 }
 
 
-/* Truncate what will be displayed in the echo area
-   the next time we display it--but don't redisplay it now.  */
-
-void
-truncate_echo_area (len)
-     int len;
-{
-  /* A null message buffer means that the frame hasn't really been
-     initialized yet.  Error messages get reported properly by
-     cmd_error, so this must be just an informative message; toss it.  */
-  if (!noninteractive && INTERACTIVE && FRAME_MESSAGE_BUF (selected_frame))
-    echo_area_glyphs_length = len;
-}
-
-
-/* Nonzero if FRAME_MESSAGE_BUF (selected_frame) is being used by
-   print; zero if being used by message.  */
-
-int message_buf_print;
-
-
 /* Dump an informative message to the minibuf.  If M is 0, clear out
    any existing message, and let the mini-buffer text show through.  */
 
@@ -5113,11 +5094,11 @@ message (m, a1, a2, a3)
         on.  It may be larger than the selected frame, so we need to
         use its buffer, not the selected frame's buffer.  */
       Lisp_Object mini_window;
-      struct frame *f;
+      struct frame *f, *sf = SELECTED_FRAME ();
 
       /* Get the frame containing the mini-buffer
         that the selected frame is using.  */
-      mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
+      mini_window = FRAME_MINIBUF_WINDOW (sf);
       f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
 
       /* A null message buffer means that the frame hasn't really been
@@ -5145,152 +5126,772 @@ message (m, a1, a2, a3)
 
              message2 (FRAME_MESSAGE_BUF (f), len, 0);
            }
-         else
-           message1 (0);
-
-         /* Print should start at the beginning of the message
-            buffer next time.  */
-         message_buf_print = 0;
+         else
+           message1 (0);
+
+         /* Print should start at the beginning of the message
+            buffer next time.  */
+         message_buf_print = 0;
+       }
+    }
+}
+
+
+/* The non-logging version of message.  */
+
+void
+message_nolog (m, a1, a2, a3)
+     char *m;
+     EMACS_INT a1, a2, a3;
+{
+  Lisp_Object old_log_max;
+  old_log_max = Vmessage_log_max;
+  Vmessage_log_max = Qnil;
+  message (m, a1, a2, a3);
+  Vmessage_log_max = old_log_max;
+}
+
+
+/* Display the current message in the current mini-buffer.  This is
+   only called from error handlers in process.c, and is not time
+   critical.  */
+
+void
+update_echo_area ()
+{
+  if (!NILP (echo_area_buffer[0]))
+    {
+      Lisp_Object string;
+      string = Fcurrent_message ();
+      message3 (string, XSTRING (string)->size, 
+               !NILP (current_buffer->enable_multibyte_characters));
+    }
+}
+
+
+/* Call FN with args A1..A5 with either the current or last displayed
+   echo_area_buffer as current buffer.
+
+   WHICH zero means use the current message buffer
+   echo_area_buffer[0].  If that is nil, choose a suitable buffer
+   from echo_buffer[] and clear it.
+
+   WHICH > 0 means use echo_area_buffer[1].  If that is nil, choose a
+   suitable buffer from echo_buffer[] and clear it.
+
+   If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
+   that the current message becomes the last displayed one, make
+   choose a suitable buffer for echo_area_buffer[0], and clear it.
+
+   Value is what FN returns. */
+
+static int
+with_echo_area_buffer (w, which, fn, a1, a2, a3, a4, a5)
+     struct window *w;
+     int which;
+     int (*fn) ();
+     int a1, a2, a3, a4, a5;
+{
+  Lisp_Object buffer;
+  int i, this_one, the_other, clear_buffer_p, rc;
+  int count = specpdl_ptr - specpdl;
+
+  /* If buffers aren't life, make new ones.  */
+  for (i = 0; i < 2; ++i)
+    if (!BUFFERP (echo_buffer[i])
+       || NILP (XBUFFER (echo_buffer[i])->name))
+      {
+       char name[30];
+       sprintf (name, " *Echo Area %d*", i);
+       echo_buffer[i] = Fget_buffer_create (build_string (name));
+      }
+
+  clear_buffer_p = 0;
+  
+  if (which == 0)
+    this_one = 0, the_other = 1;
+  else if (which > 0)
+    this_one = 1, the_other = 0;
+  else
+    {
+      this_one = 0, the_other = 1;
+      clear_buffer_p = 1;
+      
+      /* We need a fresh one in case the current echo buffer equals
+        the one containing the last displayed echo area message.  */
+      if (!NILP (echo_area_buffer[this_one])
+         && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
+       echo_area_buffer[this_one] = Qnil;
+    }
+
+  /* Choose a suitable buffer from echo_buffer[] is we don't
+     have one.  */
+  if (NILP (echo_area_buffer[this_one]))
+    {
+      echo_area_buffer[this_one]
+       = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
+          ? echo_buffer[the_other]
+          : echo_buffer[this_one]);
+      clear_buffer_p = 1;
+    }
+
+  buffer = echo_area_buffer[this_one];
+
+  record_unwind_protect (unwind_with_echo_area_buffer,
+                        with_echo_area_buffer_unwind_data (w));
+
+  /* Make the echo area buffer current.  Note that for display
+     purposes, it is not necessary that the displayed window's buffer
+     == current_buffer, except for text property lookup.  So, let's
+     only set that buffer temporarily here without doing a full
+     Fset_window_buffer.  We must also change w->pointm, though,
+     because otherwise an assertions in unshow_buffer fails, and Emacs
+     aborts.  */
+  set_buffer_internal_1 (XBUFFER (buffer));
+  if (w)
+    {
+      w->buffer = buffer;
+      set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
+    }
+  current_buffer->truncate_lines = Qnil;
+  current_buffer->undo_list = Qt;
+  current_buffer->read_only = Qnil;
+
+  if (clear_buffer_p && Z > BEG)
+    del_range (BEG, Z);
+
+  xassert (BEGV >= BEG);
+  xassert (ZV <= Z && ZV >= BEGV);
+
+  rc = fn (a1, a2, a3, a4, a5);
+
+  xassert (BEGV >= BEG);
+  xassert (ZV <= Z && ZV >= BEGV);
+
+  unbind_to (count, Qnil);
+  return rc;
+}
+
+
+/* Save state that should be preserved around the call to the function
+   FN called in with_echo_area_buffer.  */
+
+static Lisp_Object
+with_echo_area_buffer_unwind_data (w)
+     struct window *w;
+{
+  int i = 0;
+  Lisp_Object vector;
+
+  /* Reduce consing by keeping one vector in
+     Vwith_echo_area_save_vector.  */
+  vector = Vwith_echo_area_save_vector;
+  Vwith_echo_area_save_vector = Qnil;
+  
+  if (NILP (vector))
+    vector = Fmake_vector (make_number (7), Qnil);
+  
+  XSETBUFFER (XVECTOR (vector)->contents[i], current_buffer); ++i;
+  XVECTOR (vector)->contents[i++] = Vdeactivate_mark;
+  XVECTOR (vector)->contents[i++] = make_number (windows_or_buffers_changed);
+  
+  if (w)
+    {
+      XSETWINDOW (XVECTOR (vector)->contents[i], w); ++i;
+      XVECTOR (vector)->contents[i++] = w->buffer;
+      XVECTOR (vector)->contents[i++]
+       = make_number (XMARKER (w->pointm)->charpos);
+      XVECTOR (vector)->contents[i++]
+       = make_number (XMARKER (w->pointm)->bytepos);
+    }
+  else
+    {
+      int end = i + 4;
+      while (i < end)
+       XVECTOR (vector)->contents[i++] = Qnil;
+    }
+
+  xassert (i == XVECTOR (vector)->size);
+  return vector;
+}
+
+
+/* Restore global state from VECTOR which was created by
+   with_echo_area_buffer_unwind_data.  */
+
+static Lisp_Object
+unwind_with_echo_area_buffer (vector)
+     Lisp_Object vector;
+{
+  int i = 0;
+  
+  set_buffer_internal_1 (XBUFFER (XVECTOR (vector)->contents[i])); ++i;
+  Vdeactivate_mark = XVECTOR (vector)->contents[i]; ++i;
+  windows_or_buffers_changed = XFASTINT (XVECTOR (vector)->contents[i]); ++i;
+
+  if (WINDOWP (XVECTOR (vector)->contents[i]))
+    {
+      struct window *w;
+      Lisp_Object buffer, charpos, bytepos;
+      
+      w = XWINDOW (XVECTOR (vector)->contents[i]); ++i;
+      buffer = XVECTOR (vector)->contents[i]; ++i;
+      charpos = XVECTOR (vector)->contents[i]; ++i;
+      bytepos = XVECTOR (vector)->contents[i]; ++i;
+      
+      w->buffer = buffer;
+      set_marker_both (w->pointm, buffer,
+                      XFASTINT (charpos), XFASTINT (bytepos));
+    }
+
+  Vwith_echo_area_save_vector = vector;
+  return Qnil;
+}
+
+
+/* Set up the echo area for use by print functions.  MULTIBYTE_P
+   non-zero means we will print multibyte.  */
+
+void
+setup_echo_area_for_printing (multibyte_p)
+     int multibyte_p;
+{
+  if (!message_buf_print)
+    {
+      /* A message has been output since the last time we printed.
+        Choose a fresh echo area buffer.  */
+      if (EQ (echo_area_buffer[1], echo_buffer[0]))
+       echo_area_buffer[0] = echo_buffer[1]; 
+      else
+       echo_area_buffer[0] = echo_buffer[0];
+
+      /* Switch to that buffer and clear it.  */
+      set_buffer_internal (XBUFFER (echo_area_buffer[0]));
+      if (Z > BEG)
+       del_range (BEG, Z);
+      TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
+
+      /* Set up the buffer for the multibyteness we need.  */
+      if (multibyte_p
+         != !NILP (current_buffer->enable_multibyte_characters))
+       Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
+
+      /* Raise the frame containing the echo area.  */
+      if (minibuffer_auto_raise)
+       {
+         struct frame *sf = SELECTED_FRAME ();
+         Lisp_Object mini_window;
+         mini_window = FRAME_MINIBUF_WINDOW (sf);
+         Fraise_frame  (WINDOW_FRAME (XWINDOW (mini_window)));
+       }
+
+      message_buf_print = 1;
+    }
+  else if (current_buffer != XBUFFER (echo_area_buffer[0]))
+    /* Someone switched buffers between print requests.  */
+    set_buffer_internal (XBUFFER (echo_area_buffer[0]));
+}
+
+
+/* Display an echo area message in window W.  Value is non-zero if W's
+   height is changed.  If display_last_displayed_message_p is
+   non-zero, display the message that was last displayed, otherwise
+   display the current message.  */
+
+static int
+display_echo_area (w)
+     struct window *w;
+{
+  int i, no_message_p, window_height_changed_p;
+
+  /* If there is no message, we must call display_echo_area_1
+     nevertheless because it resizes the window.  But we will have to
+     reset the echo_area_buffer in question to nil at the end because
+     with_echo_area_buffer will sets it to an empty buffer.  */
+  i = display_last_displayed_message_p ? 1 : 0;
+  no_message_p = NILP (echo_area_buffer[i]);
+  
+  window_height_changed_p
+    = with_echo_area_buffer (w, display_last_displayed_message_p,
+                            (int (*) ()) display_echo_area_1, w);
+
+  if (no_message_p)
+    echo_area_buffer[i] = Qnil;
+  
+  return window_height_changed_p;
+}
+
+
+/* Helper for display_echo_area.  Display the current buffer which
+   contains the current echo area message in window W, a mini-window.
+   Change the height of W so that all of the message is displayed.
+   Value is non-zero if height of W was changed.  */
+
+static int
+display_echo_area_1 (w)
+     struct window *w;
+{
+  Lisp_Object window;
+  struct text_pos start;
+  int window_height_changed_p = 0;
+
+  /* Do this before displaying, so that we have a large enough glyph
+     matrix for the display.  */
+  window_height_changed_p = resize_mini_window (w, 0);
+
+  /* Display.  */
+  clear_glyph_matrix (w->desired_matrix);
+  XSETWINDOW (window, w);
+  SET_TEXT_POS (start, BEG, BEG_BYTE);
+  try_window (window, start);
+
+  return window_height_changed_p;
+}
+
+
+/* Resize the echo area window to exactly the size needed for the
+   currently displayed message, if there is one.  */
+
+void
+resize_echo_area_axactly ()
+{
+  if (BUFFERP (echo_area_buffer[0])
+      && WINDOWP (echo_area_window))
+    {
+      struct window *w = XWINDOW (echo_area_window);
+      int resized_p;
+      
+      resized_p = with_echo_area_buffer (w, 0,
+                                        (int (*) ()) resize_mini_window,
+                                        w, 1);
+      if (resized_p)
+       {
+         ++windows_or_buffers_changed;
+         ++update_mode_lines;
+         redisplay_internal (0);
+       }
+    }
+}
+
+
+/* Resize mini-window W to fit the size of its contents.  EXACT:P
+   means size the window exactly to the size needed.  Otherwise, it's
+   only enlarged until W's buffer is empty.  Value is non-zero if
+   the window height has been changed. */
+
+int
+resize_mini_window (w, exact_p)
+     struct window *w;
+     int exact_p;
+{
+  struct frame *f = XFRAME (w->frame);
+  int window_height_changed_p = 0;
+
+  xassert (MINI_WINDOW_P (w));
+
+  /* Nil means don't try to resize.  */
+  if (NILP (Vmax_mini_window_height))
+    return 0;
+  
+  if (!FRAME_MINIBUF_ONLY_P (f))
+    {
+      struct it it;
+      struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
+      int total_height = XFASTINT (root->height) + XFASTINT (w->height);
+      int height, max_height;
+      int unit = CANON_Y_UNIT (f);
+      struct text_pos start;
+
+      init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
+
+      /* Compute the max. number of lines specified by the user.  */
+      if (FLOATP (Vmax_mini_window_height))
+       max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
+      else if (INTEGERP (Vmax_mini_window_height))
+       max_height = XINT (Vmax_mini_window_height);
+      else
+       max_height = total_height / 4;
+      
+      /* Correct that max. height if it's bogus. */
+      max_height = max (1, max_height);
+      max_height = min (total_height, max_height);
+      
+      /* Find out the height of the text in the window.  */
+      move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
+      height = (unit - 1 + it.current_y + last_height) / unit;
+      height = max (1, height);
+      
+      /* Compute a suitable window start.  */
+      if (height > max_height)
+       {
+         height = max_height;
+         init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
+         move_it_vertically_backward (&it, (height - 1) * unit);
+         start = it.current.pos;
+       }
+      else
+       SET_TEXT_POS (start, BEGV, BEGV_BYTE);
+      SET_MARKER_FROM_TEXT_POS (w->start, start);
+
+      /* Let it grow only, until we display an empty message, in which
+        case the window shrinks again.  */
+      if (height > XFASTINT (w->height))
+       {
+         int old_height = XFASTINT (w->height);
+         freeze_window_starts (f, 1);
+         grow_mini_window (w, height - XFASTINT (w->height));
+         window_height_changed_p = XFASTINT (w->height) != old_height;
+       }
+      else if (height < XFASTINT (w->height)
+              && (exact_p || BEGV == ZV))
+       {
+         int old_height = XFASTINT (w->height);
+         freeze_window_starts (f, 0);
+         shrink_mini_window (w);
+         window_height_changed_p = XFASTINT (w->height) != old_height;
+       }
+    }
+
+  return window_height_changed_p;
+}
+
+
+/* Value is the current message, a string, or nil if there is no
+   current message.  */
+
+Lisp_Object
+current_message ()
+{
+  Lisp_Object msg;
+
+  if (NILP (echo_area_buffer[0]))
+    msg = Qnil;
+  else
+    {
+      with_echo_area_buffer (0, 0, (int (*) ()) current_message_1, &msg);
+      if (NILP (msg))
+       echo_area_buffer[0] = Qnil;
+    }
+  
+  return msg;
+}
+
+
+static int
+current_message_1 (msg)
+     Lisp_Object *msg;
+{
+  if (Z > BEG)
+    *msg = make_buffer_string (BEG, Z, 1);
+  else
+    *msg = Qnil;
+  return 0;
+}
+
+
+/* Push the current message on Vmessage_stack for later restauration
+   by restore_message.  Value is non-zero if the current message isn't
+   empty.  This is a relatively infrequent operation, so it's not
+   worth optimizing.  */
+
+int
+push_message ()
+{
+  Lisp_Object msg;
+  msg = current_message ();
+  Vmessage_stack = Fcons (msg, Vmessage_stack);
+  return STRINGP (msg);
+}
+
+
+/* Restore message display from the top of Vmessage_stack.  */
+
+void
+restore_message ()
+{
+  Lisp_Object msg;
+  
+  xassert (CONSP (Vmessage_stack));
+  msg = XCAR (Vmessage_stack);
+  if (STRINGP (msg))
+    message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
+  else
+    message3_nolog (msg, 0, 0);
+}
+
+
+/* Pop the top-most entry off Vmessage_stack.  */
+
+void
+pop_message ()
+{
+  xassert (CONSP (Vmessage_stack));
+  Vmessage_stack = XCDR (Vmessage_stack);
+}
+
+
+/* Check that Vmessage_stack is nil.  Called from emacs.c when Emacs
+   exits.  If the stack is not empty, we have a missing pop_message
+   somewhere.  */
+
+void
+check_message_stack ()
+{
+  if (!NILP (Vmessage_stack))
+    abort ();
+}
+
+
+/* Truncate to NCHARS what will be displayed in the echo area the next
+   time we display it---but don't redisplay it now.  */
+
+void
+truncate_echo_area (nchars)
+     int nchars;
+{
+  if (nchars == 0)
+    echo_area_buffer[0] = Qnil;
+  /* A null message buffer means that the frame hasn't really been
+     initialized yet.  Error messages get reported properly by
+     cmd_error, so this must be just an informative message; toss it.  */
+  else if (!noninteractive
+          && INTERACTIVE
+          && !NILP (echo_area_buffer[0]))
+    {
+      struct frame *sf = SELECTED_FRAME ();
+      if (FRAME_MESSAGE_BUF (sf))
+       with_echo_area_buffer (0, 0, (int (*) ()) truncate_message_1, nchars);
+    }
+}
+
+
+/* Helper function for truncate_echo_area.  Truncate the current
+   message to at most NCHARS characters.  */
+
+static int
+truncate_message_1 (nchars)
+     int nchars;
+{
+  if (BEG + nchars < Z)
+    del_range (BEG + nchars, Z);
+  if (Z == BEG)
+    echo_area_buffer[0] = Qnil;
+  return 0;
+}
+
+
+/* Set the current message to a substring of S or STRING.
+
+   If STRING is a Lisp string, set the message to the first NBYTES
+   bytes from STRING.  NBYTES zero means use the whole string.  If
+   STRING is multibyte, the message will be displayed multibyte.
+
+   If S is not null, set the message to the first LEN bytes of S.  LEN
+   zero means use the whole string.  MULTIBYTE_P non-zero means S is
+   multibyte.  Display the message multibyte in that case.  */
+
+void
+set_message (s, string, nbytes, multibyte_p)
+     char *s;
+     Lisp_Object string;
+     int nbytes;
+{
+  message_enable_multibyte
+    = ((s && multibyte_p)
+       || (STRINGP (string) && STRING_MULTIBYTE (string)));
+  
+  with_echo_area_buffer (0, -1, (int (*) ()) set_message_1,
+                        s, string, nbytes, multibyte_p);
+  message_buf_print = 0;
+}
+
+
+/* Helper function for set_message.  Arguments have the same meaning
+   as there.  This function is called with the echo area buffer being
+   current.  */
+
+static int
+set_message_1 (s, string, nbytes, multibyte_p)
+     char *s;
+     Lisp_Object string;
+     int nbytes, multibyte_p;
+{
+  xassert (BEG == Z);
+  
+  /* Change multibyteness of the echo buffer appropriately.  */
+  if (message_enable_multibyte
+      != !NILP (current_buffer->enable_multibyte_characters))
+    Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
+
+  /* Insert new message at BEG.  */
+  TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
+
+  if (STRINGP (string))
+    {
+      int nchars;
+      
+      if (nbytes == 0)
+       nbytes = XSTRING (string)->size_byte;
+      nchars = string_byte_to_char (string, nbytes);
+      
+      /* This function takes care of single/multibyte conversion.  We
+         just have to ensure that the echo area buffer has the right
+         setting of enable_multibyte_characters.  */
+      insert_from_string (string, 0, 0, nchars, nbytes, 1);
+    }
+  else if (s)
+    {
+      if (nbytes == 0)
+       nbytes = strlen (s);
+      
+      if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
+       {
+         /* Convert from multi-byte to single-byte.  */
+         int i, c, n;
+         unsigned char work[1];
+         
+         /* Convert a multibyte string to single-byte.  */
+         for (i = 0; i < nbytes; i += n)
+           {
+             c = string_char_and_length (s + i, nbytes - i, &n);
+             work[0] = (SINGLE_BYTE_CHAR_P (c)
+                        ? c
+                        : multibyte_char_to_unibyte (c, Qnil));
+             insert_1_both (work, 1, 1, 1, 0, 0);
+           }
+       }
+      else if (!multibyte_p
+              && !NILP (current_buffer->enable_multibyte_characters))
+       {
+         /* Convert from single-byte to multi-byte.  */
+         int i, c, n;
+         unsigned char *msg = (unsigned char *) s;
+         unsigned char *str, work[4];
+      
+         /* Convert a single-byte string to multibyte.  */
+         for (i = 0; i < nbytes; i++)
+           {
+             c = unibyte_char_to_multibyte (msg[i]);
+             n = CHAR_STRING (c, work, str);
+             insert_1_both (work, 1, n, 1, 0, 0);
+           }
        }
+      else
+       insert_1 (s, nbytes, 1, 0, 0);
     }
+
+  return 0;
 }
 
 
-/* The non-logging version of message.  */
+/* Clear messages.  CURRENT_P non-zero means clear the current
+   message.  LAST_DISPLAYED_P non-zero means clear the message
+   last displayed.  */
 
 void
-message_nolog (m, a1, a2, a3)
-     char *m;
-     EMACS_INT a1, a2, a3;
+clear_message (current_p, last_displayed_p)
+     int current_p, last_displayed_p;
 {
-  Lisp_Object old_log_max;
-  old_log_max = Vmessage_log_max;
-  Vmessage_log_max = Qnil;
-  message (m, a1, a2, a3);
-  Vmessage_log_max = old_log_max;
+  if (current_p)
+    echo_area_buffer[0] = Qnil;
+  
+  if (last_displayed_p)
+    echo_area_buffer[1] = Qnil;
+  
+  message_buf_print = 0;
 }
 
+/* Clear garbaged frames.
 
-/* Display echo_area_message or echo_area_glyphs in the current
-   mini-buffer.  */
+   This function is used where the old redisplay called
+   redraw_garbaged_frames which in turn called redraw_frame which in
+   turn called clear_frame.  The call to clear_frame was a source of
+   flickering.  I believe a clear_frame is not necessary.  It should
+   suffice in the new redisplay to invalidate all current matrices,
+   and ensure a complete redisplay of all windows.  */
 
-void
-update_echo_area ()
+static void
+clear_garbaged_frames ()
 {
-  if (STRINGP (echo_area_message))
-    message3 (echo_area_message, echo_area_glyphs_length,
-             !NILP (current_buffer->enable_multibyte_characters));
-  else
-    message2 (echo_area_glyphs, echo_area_glyphs_length,
-             !NILP (current_buffer->enable_multibyte_characters));
+  if (frame_garbaged)
+    {
+      Lisp_Object tail, frame;
+      
+      FOR_EACH_FRAME (tail, frame)
+       {
+         struct frame *f = XFRAME (frame);
+         
+         if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
+           {
+             clear_current_matrices (f);
+             f->garbaged = 0;
+           }
+       }
+
+      frame_garbaged = 0;
+      ++windows_or_buffers_changed;
+    }
 }
 
 
-/* Redisplay the echo area of selected_frame.  If UPDATE_FRAME_P is
-   non-zero update selected_frame.  */
+/* Redisplay the echo area of the selected frame.  If UPDATE_FRAME_P
+   is non-zero update selected_frame.  Value is non-zero if the
+   mini-windows height has been changed.  */
 
-static void
+static int
 echo_area_display (update_frame_p)
      int update_frame_p;
 {
   Lisp_Object mini_window;
   struct window *w;
   struct frame *f;
+  int window_height_changed_p = 0;
+  struct frame *sf = SELECTED_FRAME ();
 
-  mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
+  mini_window = FRAME_MINIBUF_WINDOW (sf);
   w = XWINDOW (mini_window);
   f = XFRAME (WINDOW_FRAME (w));
 
   /* Don't display if frame is invisible or not yet initialized.  */
-  if (!FRAME_VISIBLE_P (f) 
-      || !f->glyphs_initialized_p)
-    return;
+  if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
+    return 0;
 
+#if 0 /* inhibit_window_system is not a valid way of testing
+        whether a window system is in use.
+        This code prevents all echo area display
+        when you run plain `emacs' on a tty.  */
   /* When Emacs starts, selected_frame may be a visible terminal
      frame, even if we run under a window system.  If we let this
      through, a message would be displayed on the terminal.  */
 #ifdef HAVE_WINDOW_SYSTEM
-  if (!inhibit_window_system && !FRAME_WINDOW_P (selected_frame))
-    return;
+  if (!inhibit_window_system && !FRAME_WINDOW_P (sf))
+    return 0;
 #endif /* HAVE_WINDOW_SYSTEM */
+#endif
 
   /* Redraw garbaged frames.  */
   if (frame_garbaged)
-    {
-      /* Old redisplay called redraw_garbaged_frames here which in
-        turn called redraw_frame which in turn called clear_frame.
-        The call to clear_frame is a source of flickering.  After
-        checking the places where SET_FRAME_GARBAGED is called, I
-        believe a clear_frame is not necessary.  It should suffice in
-        the new redisplay to invalidate all current matrices, and
-        ensure a complete redisplay of all windows.  */
-      Lisp_Object tail, frame;
-      
-      FOR_EACH_FRAME (tail, frame)
-       {
-         struct frame *f = XFRAME (frame);
-         
-         if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
-           {
-             clear_current_matrices (f);
-             f->garbaged = 0;
-           }
-       }
-
-      frame_garbaged = 0;
-      ++windows_or_buffers_changed;
-    }
+    clear_garbaged_frames ();
 
-  if (echo_area_glyphs
-      || STRINGP (echo_area_message)
-      || minibuf_level == 0)
+  if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
     {
-      struct it it;
-
       echo_area_window = mini_window;
-      clear_glyph_matrix (w->desired_matrix);
-      init_iterator (&it, w, -1, -1, w->desired_matrix->rows, DEFAULT_FACE_ID);
-      
-      if (STRINGP (echo_area_message)
-         && echo_area_glyphs_length)
-       {
-         prepare_desired_row (it.glyph_row);
-         display_string (NULL, echo_area_message, Qnil, 0, 0,
-                         &it, -1, echo_area_glyphs_length, 0,
-                         message_enable_multibyte);
-         it.glyph_row->truncated_on_right_p = 0;
-         compute_line_metrics (&it);
-       }
-      else if (echo_area_glyphs
-              && echo_area_glyphs_length)
-       {
-         prepare_desired_row (it.glyph_row);
-         display_string (echo_area_glyphs, Qnil, Qnil, 0, 0, &it,
-                         -1, echo_area_glyphs_length, 0,
-                         message_enable_multibyte);
-         it.glyph_row->truncated_on_right_p = 0;
-         compute_line_metrics (&it);
-       }
-      else
-       blank_row (w, it.glyph_row, 0);
-      
-      it.glyph_row->y = it.current_y;
-      it.current_y += it.glyph_row->height;
-
-      /* Clear the rest of the lines.  */
-      while (it.current_y < it.last_visible_y)
-       {
-         ++it.glyph_row;
-         blank_row (w, it.glyph_row, it.current_y);
-         it.current_y += it.glyph_row->height;
-       }
-
+      window_height_changed_p = display_echo_area (w);
       w->must_be_updated_p = 1;
+
       if (update_frame_p)
        {
-         /* Calling update_single_window is faster when we can use
-            window-based redisplay.  */
-         if (FRAME_WINDOW_P (f))
+         /* Not called from redisplay_internal.  If we changed
+            window configuration, we must redisplay thoroughly.
+            Otherwise, we can do with updating what we displayed
+            above.  */
+         if (window_height_changed_p)
+           {
+             ++windows_or_buffers_changed;
+             ++update_mode_lines;
+             redisplay_internal (0);
+           }
+         else if (FRAME_WINDOW_P (f))
            {
              update_single_window (w, 1);
              rif->flush_display (f);
@@ -5302,15 +5903,16 @@ echo_area_display (update_frame_p)
   else if (!EQ (mini_window, selected_window))
     windows_or_buffers_changed++;
 
+  /* Last displayed message is now the current message.  */
+  echo_area_buffer[1] = echo_area_buffer[0];
+      
   /* Prevent redisplay optimization in redisplay_internal by resetting
      this_line_start_pos.  This is done because the mini-buffer now
      displays the message instead of its buffer text.  */
   if (EQ (mini_window, selected_window))
     CHARPOS (this_line_start_pos) = 0;
-  
-  previous_echo_glyphs = echo_area_glyphs;
-  previous_echo_area_message = echo_area_message;
-  previous_echo_glyphs_length = echo_area_glyphs_length;
+
+  return window_height_changed_p;
 }
 
 
@@ -5411,9 +6013,9 @@ x_consider_frame_title (frame)
       int len;
       struct it it;
 
-      for (tail = Vframe_list; CONSP (tail); tail = XCONS (tail)->cdr)
+      for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
        {
-         struct frame *tf = XFRAME (XCONS (tail)->car);
+         struct frame *tf = XFRAME (XCAR (tail));
 
          if (tf != f 
              && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
@@ -5542,7 +6144,7 @@ prepare_menu_bars ()
          GCPRO1 (tail);
          update_menu_bar (f, 0);
 #ifdef HAVE_WINDOW_SYSTEM
-         update_toolbar (f, 0);
+         update_tool_bar (f, 0);
 #endif
          UNGCPRO;
        }
@@ -5551,9 +6153,10 @@ prepare_menu_bars ()
     }
   else
     {
-      update_menu_bar (selected_frame, 1);
+      struct frame *sf = SELECTED_FRAME ();
+      update_menu_bar (sf, 1);
 #ifdef HAVE_WINDOW_SYSTEM
-      update_toolbar (selected_frame, 1);
+      update_tool_bar (sf, 1);
 #endif
     }
 
@@ -5656,23 +6259,23 @@ update_menu_bar (f, save_match_data)
 
 \f
 /***********************************************************************
-                              Toolbars
+                              Tool-bars
  ***********************************************************************/
 
 #ifdef HAVE_WINDOW_SYSTEM
 
-/* Update the toolbar item list for frame F.  This has to be done
+/* Update the tool-bar item list for frame F.  This has to be done
    before we start to fill in any display lines.  Called from
    prepare_menu_bars.  If SAVE_MATCH_DATA is non-zero, we must save
    and restore it here.  */
 
 static void
-update_toolbar (f, save_match_data)
+update_tool_bar (f, save_match_data)
      struct frame *f;
      int save_match_data;
 {
-  if (WINDOWP (f->toolbar_window)
-      && XFASTINT (XWINDOW (f->toolbar_window)->height) > 0)
+  if (WINDOWP (f->tool_bar_window)
+      && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
     {
       Lisp_Object window;
       struct window *w;
@@ -5715,12 +6318,12 @@ update_toolbar (f, save_match_data)
              specbind (Qoverriding_local_map, Qnil);
            }
 
-         /* Build desired toolbar items from keymaps.  */
-         f->desired_toolbar_items
-           = toolbar_items (f->desired_toolbar_items,
-                            &f->n_desired_toolbar_items);
+         /* Build desired tool-bar items from keymaps.  */
+         f->desired_tool_bar_items
+           = tool_bar_items (f->desired_tool_bar_items,
+                             &f->n_desired_tool_bar_items);
          
-         /* Redisplay the toolbar in case we changed it.  */
+         /* Redisplay the tool-bar in case we changed it.  */
          w->update_mode_line = Qt;
 
          unbind_to (count, Qnil);
@@ -5730,12 +6333,12 @@ update_toolbar (f, save_match_data)
 }
 
 
-/* Set F->desired_toolbar_string to a Lisp string representing frame
-   F's desired toolbar contents.  F->desired_toolbar_items must have
+/* Set F->desired_tool_bar_string to a Lisp string representing frame
+   F's desired tool-bar contents.  F->desired_tool_bar_items must have
    been set up previously by calling prepare_menu_bars.  */
 
 static void
-build_desired_toolbar_string (f)
+build_desired_tool_bar_string (f)
      struct frame *f;
 {
   int i, size, size_needed, string_idx;
@@ -5745,60 +6348,60 @@ build_desired_toolbar_string (f)
   image = plist = props = Qnil;
   GCPRO3 (image, plist, props);
 
-  /* Prepare F->desired_toolbar_string.  If we can reuse it, do so.
+  /* Prepare F->desired_tool_bar_string.  If we can reuse it, do so.
      Otherwise, make a new string.  */
   
   /* The size of the string we might be able to reuse.  */
-  size = (STRINGP (f->desired_toolbar_string)
-         ? XSTRING (f->desired_toolbar_string)->size
+  size = (STRINGP (f->desired_tool_bar_string)
+         ? XSTRING (f->desired_tool_bar_string)->size
          : 0);
 
   /* Each image in the string we build is preceded by a space,
      and there is a space at the end.  */
-  size_needed = f->n_desired_toolbar_items + 1;
+  size_needed = f->n_desired_tool_bar_items + 1;
 
-  /* Reuse f->desired_toolbar_string, if possible.  */
+  /* Reuse f->desired_tool_bar_string, if possible.  */
   if (size < size_needed)
-    f->desired_toolbar_string = Fmake_string (make_number (size_needed), ' ');
+    f->desired_tool_bar_string = Fmake_string (make_number (size_needed), ' ');
   else
     {
       props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
       Fremove_text_properties (make_number (0), make_number (size),
-                              props, f->desired_toolbar_string);
+                              props, f->desired_tool_bar_string);
     }
 
   /* Put a `display' property on the string for the images to display,
-     put a `menu_item' property on toolbar items with a value that
-     is the index of the item in F's toolbar item vector.  */
+     put a `menu_item' property on tool-bar items with a value that
+     is the index of the item in F's tool-bar item vector.  */
   for (i = 0, string_idx = 0;
-       i < f->n_desired_toolbar_items;
+       i < f->n_desired_tool_bar_items;
        ++i, string_idx += 1)
     {
 #define PROP(IDX)                                      \
-      (XVECTOR (f->desired_toolbar_items)              \
-       ->contents[i * TOOLBAR_ITEM_NSLOTS + (IDX)])
+      (XVECTOR (f->desired_tool_bar_items)             \
+       ->contents[i * TOOL_BAR_ITEM_NSLOTS + (IDX)])
 
-      int enabled_p = !NILP (PROP (TOOLBAR_ITEM_ENABLED_P));
-      int selected_p = !NILP (PROP (TOOLBAR_ITEM_SELECTED_P));
+      int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
+      int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
       int margin, relief;
       extern Lisp_Object QCrelief, QCmargin, QCalgorithm, Qimage;
       extern Lisp_Object Qlaplace;
 
       /* If image is a vector, choose the image according to the
         button state.  */
-      image = PROP (TOOLBAR_ITEM_IMAGES);
+      image = PROP (TOOL_BAR_ITEM_IMAGES);
       if (VECTORP (image))
        {
-         enum toolbar_item_image idx;
+         enum tool_bar_item_image idx;
          
          if (enabled_p)
            idx = (selected_p
-                  ? TOOLBAR_IMAGE_ENABLED_SELECTED
-                  : TOOLBAR_IMAGE_ENABLED_DESELECTED);
+                  ? TOOL_BAR_IMAGE_ENABLED_SELECTED
+                  : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
          else
            idx = (selected_p
-                  ? TOOLBAR_IMAGE_DISABLED_SELECTED
-                  : TOOLBAR_IMAGE_DISABLED_DESELECTED);
+                  ? TOOL_BAR_IMAGE_DISABLED_SELECTED
+                  : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
          
          xassert (XVECTOR (image)->size >= idx);
          image = XVECTOR (image)->contents[idx];
@@ -5808,14 +6411,14 @@ build_desired_toolbar_string (f)
       if (!valid_image_p (image))
        continue;
 
-      /* Display the toolbar button pressed, or depressed.  */
+      /* Display the tool-bar button pressed, or depressed.  */
       plist = Fcopy_sequence (XCDR (image));
 
       /* Compute margin and relief to draw.  */
-      relief = toolbar_button_relief > 0 ? toolbar_button_relief : 3;
-      margin = relief + max (0, toolbar_button_margin);
+      relief = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
+      margin = relief + max (0, tool_bar_button_margin);
       
-      if (auto_raise_toolbar_buttons_p)
+      if (auto_raise_tool_bar_buttons_p)
        {
          /* Add a `:relief' property to the image spec if the item is
             selected.  */
@@ -5848,14 +6451,14 @@ build_desired_toolbar_string (f)
       
       /* Put a `display' text property on the string for the image to
         display.  Put a `menu-item' property on the string that gives
-        the start of this item's properties in the toolbar items
+        the start of this item's properties in the tool-bar items
         vector.  */
       image = Fcons (Qimage, plist);
       props = list4 (Qdisplay, image,
-                    Qmenu_item, make_number (i * TOOLBAR_ITEM_NSLOTS)),
+                    Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS)),
       Fadd_text_properties (make_number (string_idx),
                            make_number (string_idx + 1),
-                           props, f->desired_toolbar_string);
+                           props, f->desired_tool_bar_string);
 #undef PROP
     }
 
@@ -5863,10 +6466,10 @@ build_desired_toolbar_string (f)
 }
 
 
-/* Display one line of the toolbar of frame IT->f.  */
+/* Display one line of the tool-bar of frame IT->f.  */
 
 static void
-display_toolbar_line (it)
+display_tool_bar_line (it)
      struct it *it;
 {
   struct glyph_row *row = it->glyph_row;
@@ -5924,7 +6527,7 @@ display_toolbar_line (it)
   last->right_box_line_p = 1;
   compute_line_metrics (it);
   
-  /* If line is empty, make it occupy the rest of the toolbar.  */
+  /* If line is empty, make it occupy the rest of the tool-bar.  */
   if (!row->displays_text_p)
     {
       row->height = row->phys_height = it->last_visible_y - row->y;
@@ -5943,39 +6546,39 @@ display_toolbar_line (it)
 }
 
 
-/* Value is the number of screen lines needed to make all toolbar
+/* Value is the number of screen lines needed to make all tool-bar
    items of frame F visible.  */
 
 static int
-toolbar_lines_needed (f)
+tool_bar_lines_needed (f)
      struct frame *f;
 {
-  struct window *w = XWINDOW (f->toolbar_window);
+  struct window *w = XWINDOW (f->tool_bar_window);
   struct it it;
   
-  /* Initialize an iterator for iteration over F->desired_toolbar_string
-     in the toolbar window of frame F.  */
-  init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOLBAR_FACE_ID);
+  /* Initialize an iterator for iteration over
+     F->desired_tool_bar_string in the tool-bar window of frame F.  */
+  init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
   it.first_visible_x = 0;
   it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
-  reseat_to_string (&it, NULL, f->desired_toolbar_string, 0, 0, 0, -1);
+  reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
 
   while (!ITERATOR_AT_END_P (&it))
     {
       it.glyph_row = w->desired_matrix->rows;
       clear_glyph_row (it.glyph_row);
-      display_toolbar_line (&it);
+      display_tool_bar_line (&it);
     }
 
   return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
 }
 
 
-/* Display the toolbar of frame F.  Value is non-zero if toolbar's
+/* Display the tool-bar of frame F.  Value is non-zero if tool-bar's
    height should be changed.  */
 
 static int
-redisplay_toolbar (f)
+redisplay_tool_bar (f)
      struct frame *f;
 {
   struct window *w;
@@ -5983,65 +6586,65 @@ redisplay_toolbar (f)
   struct glyph_row *row;
   int change_height_p = 0;
   
-  /* If frame hasn't a toolbar window or if it is zero-height, don't
-     do anything.  This means you must start with toolbar-lines
+  /* If frame hasn't a tool-bar window or if it is zero-height, don't
+     do anything.  This means you must start with tool-bar-lines
      non-zero to get the auto-sizing effect.  Or in other words, you
-     can turn off toolbars by specifying toolbar-lines zero.  */
-  if (!WINDOWP (f->toolbar_window)
-      || (w = XWINDOW (f->toolbar_window),
+     can turn off tool-bars by specifying tool-bar-lines zero.  */
+  if (!WINDOWP (f->tool_bar_window)
+      || (w = XWINDOW (f->tool_bar_window),
          XFASTINT (w->height) == 0))
     return 0;
 
-  /* Set up an iterator for the toolbar window.  */
-  init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOLBAR_FACE_ID);
+  /* Set up an iterator for the tool-bar window.  */
+  init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
   it.first_visible_x = 0;
   it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
   row = it.glyph_row;
 
-  /* Build a string that represents the contents of the toolbar.  */
-  build_desired_toolbar_string (f);
-  reseat_to_string (&it, NULL, f->desired_toolbar_string, 0, 0, 0, -1);
+  /* Build a string that represents the contents of the tool-bar.  */
+  build_desired_tool_bar_string (f);
+  reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
 
-  /* Display as many lines as needed to display all toolbar items.  */
+  /* Display as many lines as needed to display all tool-bar items.  */
   while (it.current_y < it.last_visible_y)
-    display_toolbar_line (&it);
+    display_tool_bar_line (&it);
 
-  /* It doesn't make much sense to try scrolling in the toolbar
+  /* It doesn't make much sense to try scrolling in the tool-bar
      window, so don't do it.  */
   w->desired_matrix->no_scrolling_p = 1;
   w->must_be_updated_p = 1;
 
-  if (auto_resize_toolbars_p)
+  if (auto_resize_tool_bars_p)
     {
       int nlines;
       
       /* If there are blank lines at the end, except for a partially
         visible blank line at the end that is smaller than
-        CANON_Y_UNIT, change the toolbar's height.  */
+        CANON_Y_UNIT, change the tool-bar's height.  */
       row = it.glyph_row - 1;
       if (!row->displays_text_p
          && row->height >= CANON_Y_UNIT (f))
        change_height_p = 1;
 
-      /* If row displays toolbar items, but is partially visible,
-        change the toolbar's height.  */
+      /* If row displays tool-bar items, but is partially visible,
+        change the tool-bar's height.  */
       if (row->displays_text_p
          && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
        change_height_p = 1;
 
-      /* Resize windows as needed by changing the `toolbar-lines'
+      /* Resize windows as needed by changing the `tool-bar-lines'
         frame parameter.  */
       if (change_height_p
-         && (nlines = toolbar_lines_needed (f),
+         && (nlines = tool_bar_lines_needed (f),
              nlines != XFASTINT (w->height)))
        {
-         extern Lisp_Object Qtoolbar_lines;
+         extern Lisp_Object Qtool_bar_lines;
          Lisp_Object frame;
          
          XSETFRAME (frame, f);
          clear_glyph_matrix (w->desired_matrix);
          Fmodify_frame_parameters (frame,
-                                   Fcons (Fcons (Qtoolbar_lines,
+                                   Fcons (Fcons (Qtool_bar_lines,
                                                  make_number (nlines)),
                                           Qnil));
          fonts_changed_p = 1;
@@ -6052,13 +6655,13 @@ redisplay_toolbar (f)
 }
 
 
-/* Get information about the toolbar item which is displayed in GLYPH
-   on frame F.  Return in *PROP_IDX the index where toolbar item
-   properties start in F->current_toolbar_items.  Value is zero if
-   GLYPH doesn't display a toolbar item.  */
+/* Get information about the tool-bar item which is displayed in GLYPH
+   on frame F.  Return in *PROP_IDX the index where tool-bar item
+   properties start in F->current_tool_bar_items.  Value is zero if
+   GLYPH doesn't display a tool-bar item.  */
 
 int
-toolbar_item_info (f, glyph, prop_idx)
+tool_bar_item_info (f, glyph, prop_idx)
      struct frame *f;
      struct glyph *glyph;
      int *prop_idx;
@@ -6068,9 +6671,9 @@ toolbar_item_info (f, glyph, prop_idx)
   
   /* Get the text property `menu-item' at pos. The value of that
      property is the start index of this item's properties in
-     F->current_toolbar_items.  */
+     F->current_tool_bar_items.  */
   prop = Fget_text_property (make_number (glyph->charpos),
-                            Qmenu_item, f->current_toolbar_string);
+                            Qmenu_item, f->current_tool_bar_string);
   if (INTEGERP (prop))
     {
       *prop_idx = XINT (prop);
@@ -6117,8 +6720,14 @@ hscroll_window_tree (window)
        {
          int hscroll_margin, text_area_x, text_area_y;
          int text_area_width, text_area_height;
-         struct glyph_row *cursor_row = MATRIX_ROW (w->current_matrix,
-                                                    w->cursor.vpos);
+         struct glyph_row *current_cursor_row
+           = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
+         struct glyph_row *desired_cursor_row
+           = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
+         struct glyph_row *cursor_row
+           = (desired_cursor_row->enabled_p
+              ? desired_cursor_row
+              : current_cursor_row);
 
          window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
                      &text_area_width, &text_area_height);
@@ -6128,7 +6737,8 @@ hscroll_window_tree (window)
          
          if ((XFASTINT (w->hscroll)
               && w->cursor.x < hscroll_margin)
-             || (cursor_row->truncated_on_right_p
+             || (cursor_row->enabled_p
+                 && cursor_row->truncated_on_right_p
                  && (w->cursor.x > text_area_width - hscroll_margin)))
            {
              struct it it;
@@ -6207,11 +6817,6 @@ hscroll_windows (window)
 
 #if GLYPH_DEBUG
 
-/* Values of beg_unchanged and end_unchanged as of last call to
-   try_window_id.  */
-
-int debug_beg_unchanged, debug_end_unchanged;
-
 /* First and last unchanged row for try_window_id.  */
 
 int debug_first_unchanged_at_end_vpos;
@@ -6308,8 +6913,8 @@ text_outside_line_unchanged_p (w, start, end)
 
       /* Changes start in front of the line, or end after it?  */
       if (unchanged_p
-         && (beg_unchanged < start - 1
-             || end_unchanged < end))
+         && (BEG_UNCHANGED < start - 1
+             || END_UNCHANGED < end))
        unchanged_p = 0;
       
       /* If selective display, can't optimize if changes start at the
@@ -6317,7 +6922,7 @@ text_outside_line_unchanged_p (w, start, end)
       if (unchanged_p
          && INTEGERP (current_buffer->selective_display)
          && XINT (current_buffer->selective_display) > 0
-         && (beg_unchanged < start || GPT <= start))
+         && (BEG_UNCHANGED < start || GPT <= start))
        unchanged_p = 0;
     }
 
@@ -6339,6 +6944,25 @@ redisplay ()
 }
 
 
+/* Reconsider the setting of B->clip_changed which is displayed
+   in window W.  */
+
+static INLINE void
+reconsider_clip_changes (w, b)
+     struct window *w;
+     struct buffer *b;
+{
+  if (b->prevent_redisplay_optimizations_p)
+    b->clip_changed = 1;
+  else if (b->clip_changed
+          && !NILP (w->window_end_valid)
+          && w->current_matrix->buffer == b
+          && w->current_matrix->zv == BUF_ZV (b)
+          && w->current_matrix->begv == BUF_BEGV (b))
+    b->clip_changed = 0;
+}
+
+
 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
    response to any user action; therefore, we should preserve the echo
    area.  (Actually, our caller does that job.)  Perhaps in the future
@@ -6355,6 +6979,8 @@ redisplay_internal (preserve_echo_area)
   int must_finish = 0;
   struct text_pos tlbufpos, tlendpos;
   int number_of_visible_frames;
+  int count;
+  struct frame *sf = SELECTED_FRAME ();
 
   /* Non-zero means redisplay has to consider all windows on all
      frames.  Zero means, only selected_window is considered.  */
@@ -6385,12 +7011,20 @@ redisplay_internal (preserve_echo_area)
     return;
 #endif
 
+  /* I don't think this happens but let's be paranoid.  */
   if (redisplaying_p)
     return;
-  ++redisplaying_p;
 
+  /* Record a function that resets redisplaying_p to its old value
+     when we leave this function.  */
+  count = specpdl_ptr - specpdl;
+  record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
+  ++redisplaying_p;
+  
  retry:
 
+  reconsider_clip_changes (w, current_buffer);
+
   /* If new fonts have been loaded that make a glyph matrix adjustment
      necessary, do it.  */
   if (fonts_changed_p)
@@ -6400,17 +7034,17 @@ redisplay_internal (preserve_echo_area)
       fonts_changed_p = 0;
     }
 
-  if (! FRAME_WINDOW_P (selected_frame)
-      && previous_terminal_frame != selected_frame)
+  if (! FRAME_WINDOW_P (sf)
+      && previous_terminal_frame != sf)
     {
       /* Since frames on an ASCII terminal share the same display
         area, displaying a different frame means redisplay the whole
         thing.  */
       windows_or_buffers_changed++;
-      SET_FRAME_GARBAGED (selected_frame);
-      XSETFRAME (Vterminal_frame, selected_frame);
+      SET_FRAME_GARBAGED (sf);
+      XSETFRAME (Vterminal_frame, sf);
     }
-  previous_terminal_frame = selected_frame;
+  previous_terminal_frame = sf;
 
   /* Set the visible flags for all frames.  Do this before checking
      for resized or garbaged frames; they want to know if their frames
@@ -6433,36 +7067,13 @@ redisplay_internal (preserve_echo_area)
   }
 
   /* Notice any pending interrupt request to change frame size.  */
-  do_pending_window_change ();
+  do_pending_window_change (1);
 
   /* Clear frames marked as garbaged.  */
   if (frame_garbaged)
-    {
-      /* Old redisplay called redraw_garbaged_frames here which in
-        turn called redraw_frame which in turn called clear_frame.
-        The call to clear_frame is a source of flickering.  After
-        checking the places where SET_FRAME_GARBAGED is called, I
-        believe a clear_frame is not necessary.  It should suffice in
-        the new redisplay to invalidate all current matrices, and
-        ensure a complete redisplay of all windows.  */
-      Lisp_Object tail, frame;
-      
-      FOR_EACH_FRAME (tail, frame)
-       {
-         struct frame *f = XFRAME (frame);
-         
-         if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
-           {
-             clear_current_matrices (f);
-             f->garbaged = 0;
-           }
-       }
+    clear_garbaged_frames ();
 
-      frame_garbaged = 0;
-      ++windows_or_buffers_changed;
-    }
-
-  /* Build menubar and toolbar items.  */
+  /* Build menubar and tool-bar items.  */
   prepare_menu_bars ();
 
   if (windows_or_buffers_changed)
@@ -6502,15 +7113,48 @@ redisplay_internal (preserve_echo_area)
   /* Normally the message* functions will have already displayed and
      updated the echo area, but the frame may have been trashed, or
      the update may have been preempted, so display the echo area
-     again here.  */
-  if (echo_area_glyphs
-      || STRINGP (echo_area_message)
-      || previous_echo_glyphs
-      || STRINGP (previous_echo_area_message))
+     again here.  Checking both message buffers captures the case that
+     the echo area should be cleared.  */
+  if (!NILP (echo_area_buffer[0]) || !NILP (echo_area_buffer[1]))
+    {
+      int window_height_changed_p = echo_area_display (0);
+      must_finish = 1;
+      
+      if (fonts_changed_p)
+       goto retry;
+      else if (window_height_changed_p)
+       {
+         consider_all_windows_p = 1;
+         ++update_mode_lines;
+         ++windows_or_buffers_changed;
+         
+         /* If window configuration was changed, frames may have been
+            marked garbaged.  Clear them or we will experience
+            surprises wrt scrolling.  */
+         if (frame_garbaged)
+           clear_garbaged_frames ();
+       }
+    }
+  else if (w == XWINDOW (minibuf_window)
+          && (current_buffer->clip_changed
+              || XFASTINT (w->last_modified) < MODIFF
+              || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
+          && resize_mini_window (w, 0))
     {
-      echo_area_display (0);
+      /* Resized active mini-window to fit the size of what it is
+         showing if its contents might have changed.  */
       must_finish = 1;
+      consider_all_windows_p = 1;
+      ++windows_or_buffers_changed;
+      ++update_mode_lines;
+      
+      /* If window configuration was changed, frames may have been
+        marked garbaged.  Clear them or we will experience
+        surprises wrt scrolling.  */
+      if (frame_garbaged)
+       clear_garbaged_frames ();
     }
+  
 
   /* If showing the region, and mark has changed, we must redisplay
      the whole window.  The assignment to this_line_start_pos prevents
@@ -6674,7 +7318,7 @@ redisplay_internal (preserve_echo_area)
        {
          if (!must_finish)
            {
-             do_pending_window_change ();
+             do_pending_window_change (1);
 
              /* We used to always goto end_of_redisplay here, but this 
                 isn't enough if we have a blinking cursor.  */
@@ -6754,7 +7398,7 @@ redisplay_internal (preserve_echo_area)
       FOR_EACH_FRAME (tail, frame)
        {
          struct frame *f = XFRAME (frame);
-         if (FRAME_WINDOW_P (f) || f == selected_frame)
+         if (FRAME_WINDOW_P (f) || f == sf)
            {
              /* Mark all the scroll bars to be removed; we'll redeem
                 the ones we want when we redisplay their windows.  */
@@ -6771,8 +7415,8 @@ redisplay_internal (preserve_echo_area)
            }
        }
     }
-  else if (FRAME_VISIBLE_P (selected_frame)
-          && !FRAME_OBSCURED_P (selected_frame))
+  else if (FRAME_VISIBLE_P (sf)
+          && !FRAME_OBSCURED_P (sf))
     redisplay_window (selected_window, 1);
 
   
@@ -6794,19 +7438,37 @@ update:
   if (consider_all_windows_p)
     {
       Lisp_Object tail;
+      struct frame *f;
+      int hscrolled_p;
 
       pause = 0;
+      hscrolled_p = 0;
 
-      for (tail = Vframe_list; CONSP (tail); tail = XCONS (tail)->cdr)
-       {
-         struct frame *f;
+      /* See if we have to hscroll.  */
+      for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
+       if (FRAMEP (XCAR (tail)))
+         {
+           f = XFRAME (XCAR (tail));
+           
+           if ((FRAME_WINDOW_P (f)
+                || f == sf)
+               && FRAME_VISIBLE_P (f)
+               && !FRAME_OBSCURED_P (f)
+               && hscroll_windows (f->root_window))
+             hscrolled_p = 1;
+         }
 
-         if (!FRAMEP (XCONS (tail)->car))
+      if (hscrolled_p)
+       goto retry;
+
+      for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
+       {
+         if (!FRAMEP (XCAR (tail)))
            continue;
 
-         f = XFRAME (XCONS (tail)->car);
+         f = XFRAME (XCAR (tail));
 
-         if ((FRAME_WINDOW_P (f) || f == selected_frame)
+         if ((FRAME_WINDOW_P (f) || f == sf)
              && FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
            {
              /* Mark all windows as to be updated.  */
@@ -6814,9 +7476,6 @@ update:
              pause |= update_frame (f, 0, 0);
              if (!pause)
                {
-                 if (hscroll_windows (f->root_window))
-                   goto retry;
-
                  mark_window_display_accurate (f->root_window, 1);
                  if (frame_up_to_date_hook != 0)
                    (*frame_up_to_date_hook) (f);
@@ -6826,13 +7485,14 @@ update:
     }
   else
     {
-      if (FRAME_VISIBLE_P (selected_frame)
-         && !FRAME_OBSCURED_P (selected_frame))
+      if (FRAME_VISIBLE_P (sf)
+         && !FRAME_OBSCURED_P (sf))
        {
-         XWINDOW (selected_window)->must_be_updated_p = 1;
-         pause = update_frame (selected_frame, 0, 0);
-         if (!pause && hscroll_windows (selected_window))
+         if (hscroll_windows (selected_window))
            goto retry;
+         
+         XWINDOW (selected_window)->must_be_updated_p = 1;
+         pause = update_frame (sf, 0, 0);
        }
       else
        pause = 0;
@@ -6846,10 +7506,10 @@ update:
        Lisp_Object mini_window;
        struct frame *mini_frame;
 
-       mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
+       mini_window = FRAME_MINIBUF_WINDOW (sf);
        mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
        
-       if (mini_frame != selected_frame && FRAME_WINDOW_P (mini_frame))
+       if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
          {
            XWINDOW (mini_window)->must_be_updated_p = 1;
            pause |= update_frame (mini_frame, 0, 0);
@@ -6888,13 +7548,13 @@ update:
     {
       register struct buffer *b = XBUFFER (w->buffer);
 
-      unchanged_modified = BUF_MODIFF (b);
-      overlay_unchanged_modified = BUF_OVERLAY_MODIFF (b);
-      beg_unchanged = BUF_GPT (b) - BUF_BEG (b);
-      end_unchanged = BUF_Z (b) - BUF_GPT (b);
+      BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
+      BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
+      BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
+      BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
 
       if (consider_all_windows_p)
-       mark_window_display_accurate (FRAME_ROOT_WINDOW (selected_frame), 1);
+       mark_window_display_accurate (FRAME_ROOT_WINDOW (sf), 1);
       else
        {
          XSETFASTINT (w->last_point, BUF_PT (b));
@@ -6902,6 +7562,7 @@ update:
          w->last_cursor_off_p = w->cursor_off_p;
 
          b->clip_changed = 0;
+         b->prevent_redisplay_optimizations_p = 0;
          w->update_mode_line = Qnil;
          XSETFASTINT (w->last_modified, BUF_MODIFF (b));
          XSETFASTINT (w->last_overlay_modified, BUF_OVERLAY_MODIFF (b));
@@ -6922,7 +7583,11 @@ update:
          last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
          last_arrow_string = Voverlay_arrow_string;
          if (frame_up_to_date_hook != 0)
-           (*frame_up_to_date_hook) (selected_frame);
+           (*frame_up_to_date_hook) (sf);
+
+         w->current_matrix->buffer = b;
+         w->current_matrix->begv = BUF_BEGV (b);
+         w->current_matrix->zv = BUF_ZV (b);
        }
       
       update_mode_lines = 0;
@@ -6967,7 +7632,7 @@ update:
     }
 
   /* Change frame size now if a change is pending.  */
-  do_pending_window_change ();
+  do_pending_window_change (1);
 
   /* If we just did a pending size change, or have additional
      visible frames, redisplay again.  */
@@ -6975,9 +7640,8 @@ update:
     goto retry;
 
  end_of_redisplay:;
-  
-  if (--redisplaying_p < 0)
-    redisplaying_p = 0;
+
+  unbind_to (count, Qnil);
 }
 
 
@@ -6992,23 +7656,32 @@ update:
 void
 redisplay_preserve_echo_area ()
 {
-  if (!echo_area_glyphs
-      && !STRINGP (echo_area_message)
-      && (previous_echo_glyphs
-         || STRINGP (previous_echo_area_message)))
+  if (!NILP (echo_area_buffer[1]))
     {
-      echo_area_glyphs = previous_echo_glyphs;
-      echo_area_message = previous_echo_area_message;
-      echo_area_glyphs_length = previous_echo_glyphs_length;
+      /* We have a previously displayed message, but no current
+        message.  Redisplay the previous message.  */
+      display_last_displayed_message_p = 1;
       redisplay_internal (1);
-      echo_area_glyphs = NULL;
-      echo_area_message = Qnil;
+      display_last_displayed_message_p = 0;
     }
   else
     redisplay_internal (1);
 }
 
 
+/* Function registered with record_unwind_protect in
+   redisplay_internal.  Clears the flag indicating that a redisplay is
+   in progress.  */
+
+static Lisp_Object
+unwind_redisplay (old_redisplaying_p)
+     Lisp_Object old_redisplaying_p;
+{
+  redisplaying_p = XFASTINT (old_redisplaying_p);
+  return Qnil;
+}
+
+
 /* Mark the display of windows in the window tree rooted at WINDOW as
    accurate or inaccurate.  If FLAG is non-zero mark display of WINDOW
    as accurate.  If FLAG is zero arrange for WINDOW to be redisplayed
@@ -7052,6 +7725,10 @@ mark_window_display_accurate (window, accurate_p)
          if (accurate_p)
            {
              b->clip_changed = 0;
+             b->prevent_redisplay_optimizations_p = 0;
+             w->current_matrix->buffer = b;
+             w->current_matrix->begv = BUF_BEGV (b);
+             w->current_matrix->zv = BUF_ZV (b);
              w->last_cursor = w->cursor;
              w->last_cursor_off_p = w->cursor_off_p;
              if (w == XWINDOW (selected_window))
@@ -7230,7 +7907,9 @@ set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
 
 
 /* Run window scroll functions, if any, for WINDOW with new window
-   start STARTP.  Sets the window start of WINDOW to that position.  */
+   start STARTP.  Sets the window start of WINDOW to that position.
+
+   We assume that the window's buffer is really current.  */
 
 static INLINE struct text_pos
 run_window_scroll_functions (window, startp)
@@ -7239,12 +7918,18 @@ run_window_scroll_functions (window, startp)
 {
   struct window *w = XWINDOW (window);
   SET_MARKER_FROM_TEXT_POS (w->start, startp);
-  
+
+  if (current_buffer != XBUFFER (w->buffer))
+    abort ();
+
   if (!NILP (Vwindow_scroll_functions))
     {
       run_hook_with_args_2 (Qwindow_scroll_functions, window, 
                            make_number (CHARPOS (startp)));
       SET_TEXT_POS_FROM_MARKER (startp, w->start);
+      /* In case the hook functions switch buffers.  */
+      if (current_buffer != XBUFFER (w->buffer))
+       set_buffer_internal_1 (XBUFFER (w->buffer));
     }
 
   return startp;
@@ -7260,7 +7945,7 @@ make_cursor_line_fully_visible (w)
 {
   struct glyph_matrix *matrix;
   struct glyph_row *row;
-  int top_line_height;
+  int header_line_height;
   
   /* It's not always possible to find the cursor, e.g, when a window
      is full of overlay strings.  Don't do anything in that case.  */
@@ -7273,8 +7958,8 @@ make_cursor_line_fully_visible (w)
   /* If row->y == top y of window display area, the window isn't tall
      enough to display a single line.  There is nothing we can do
      about it.  */
-  top_line_height = WINDOW_DISPLAY_TOP_LINE_HEIGHT (w);
-  if (row->y == top_line_height)
+  header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
+  if (row->y == header_line_height)
     return;
 
   if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
@@ -7419,7 +8104,7 @@ try_scrolling (window, just_this_one_p, scroll_conservatively,
        {
          aggressive = current_buffer->scroll_down_aggressively;
          height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
-                   - WINDOW_DISPLAY_TOP_LINE_HEIGHT (w));
+                   - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
          if (NUMBERP (aggressive))
            amount_to_scroll = XFLOATINT (aggressive) * height;
        }
@@ -7472,7 +8157,7 @@ try_scrolling (window, just_this_one_p, scroll_conservatively,
            {
              aggressive = current_buffer->scroll_up_aggressively;
              height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
-                       - WINDOW_DISPLAY_TOP_LINE_HEIGHT (w));
+                       - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
              if (NUMBERP (aggressive))
                amount_to_scroll = XFLOATINT (aggressive) * height;
            }
@@ -7502,7 +8187,7 @@ try_scrolling (window, just_this_one_p, scroll_conservatively,
       /* Maybe forget recorded base line for line number display.  */
       if (!just_this_one_p 
          || current_buffer->clip_changed
-         || beg_unchanged < CHARPOS (startp))
+         || BEG_UNCHANGED < CHARPOS (startp))
        w->base_line_number = Qnil;
       
       /* If cursor ends up on a partially visible line, shift display
@@ -7540,10 +8225,16 @@ compute_window_start_on_continuation_line (w)
     {
       struct it it;
       struct glyph_row *row;
+
+      /* Handle the case that the window start is out of range.  */
+      if (CHARPOS (start_pos) < BEGV)
+       SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
+      else if (CHARPOS (start_pos) > ZV)
+       SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
       
       /* Find the start of the continued line.  This should be fast
         because scan_buffer is fast (newline cache).  */
-      row = w->desired_matrix->rows + (WINDOW_WANTS_TOP_LINE_P (w) ? 1 : 0);
+      row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
       init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
                     row, DEFAULT_FACE_ID);
       reseat_at_previous_visible_line_start (&it);
@@ -7613,7 +8304,9 @@ redisplay_window (window, just_this_one_p)
 #endif
 
   specbind (Qinhibit_point_motion_hooks, Qt);
-  
+
+  reconsider_clip_changes (w, buffer);
+    
   /* Has the mode line to be updated?  */ 
   update_mode_line = (!NILP (w->update_mode_line)
                      || update_mode_lines
@@ -7622,12 +8315,11 @@ redisplay_window (window, just_this_one_p)
   if (MINI_WINDOW_P (w))
     {
       if (w == XWINDOW (echo_area_window)
-         && (echo_area_glyphs
-             || STRINGP (echo_area_message)))
+         && !NILP (echo_area_buffer[0]))
        {
          if (update_mode_line)
            /* We may have to update a tty frame's menu bar or a
-              toolbar.  Example `M-x C-h C-h C-g'.  */
+              tool-bar.  Example `M-x C-h C-h C-g'.  */
            goto finish_menu_bars;
          else
            /* We've already displayed the echo area glyphs in this window.  */
@@ -7775,7 +8467,8 @@ redisplay_window (window, just_this_one_p)
 
   /* Handle case where place to start displaying has been specified,
      unless the specified location is outside the accessible range.  */
-  if (!NILP (w->force_start))
+  if (!NILP (w->force_start)
+      || w->frozen_window_start_p)
     {
       w->force_start = Qnil;
       w->vscroll = 0;
@@ -7824,7 +8517,7 @@ redisplay_window (window, just_this_one_p)
          goto restore_buffers;
        }
 
-      if (w->cursor.vpos < 0)
+      if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
        {
          /* If point does not appear, or on a line that is not fully
             visible, move point so it does appear.  The desired
@@ -8037,12 +8730,12 @@ redisplay_window (window, just_this_one_p)
     }
   
   /* Try scrolling with try_window_id.  */
-  else if (!windows_or_buffers_changed
+  else if (/* Windows and buffers haven't changed.  */
+          !windows_or_buffers_changed
           /* Window must be either use window-based redisplay or
              be full width.  */
           && (FRAME_WINDOW_P (f)
-              || ((line_ins_del_ok && WINDOW_FULL_WIDTH_P (w))
-                  && just_this_one_p))
+              || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)))
           && !MINI_WINDOW_P (w)
           /* Point is not known NOT to appear in window.  */
           && PT >= CHARPOS (startp)
@@ -8111,7 +8804,7 @@ redisplay_window (window, just_this_one_p)
        {
          if (!just_this_one_p 
              || current_buffer->clip_changed
-             || beg_unchanged < CHARPOS (startp))
+             || BEG_UNCHANGED < CHARPOS (startp))
            /* Forget any recorded base line for line number display.  */
            w->base_line_number = Qnil;
          
@@ -8262,13 +8955,13 @@ redisplay_window (window, just_this_one_p)
   
   make_cursor_line_fully_visible (w);
 
+ done:
+
   SET_TEXT_POS_FROM_MARKER (startp, w->start);
   w->start_at_line_beg = ((CHARPOS (startp) == BEGV
                           || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
                          ? Qt : Qnil);
 
- done:
-
   /* Display the mode line, if we must.  */
   if ((update_mode_line
        /* If window not full width, must redo its mode line
@@ -8285,7 +8978,7 @@ redisplay_window (window, just_this_one_p)
           && XFASTINT (w->column_number_displayed) != current_column ()))
        /* This means that the window has a mode line.  */
        && (WINDOW_WANTS_MODELINE_P (w)
-          || WINDOW_WANTS_TOP_LINE_P (w)))
+          || WINDOW_WANTS_HEADER_LINE_P (w)))
     {
       display_mode_lines (w);
 
@@ -8301,12 +8994,12 @@ redisplay_window (window, just_this_one_p)
       
       /* If top line height has changed, arrange for a thorough
         immediate redisplay using the correct mode line height.  */
-      if (WINDOW_WANTS_TOP_LINE_P (w)
-         && CURRENT_TOP_LINE_HEIGHT (w) != DESIRED_TOP_LINE_HEIGHT (w))
+      if (WINDOW_WANTS_HEADER_LINE_P (w)
+         && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
        {
          fonts_changed_p = 1;
-         MATRIX_TOP_LINE_ROW (w->current_matrix)->height
-           = DESIRED_TOP_LINE_HEIGHT (w);
+         MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
+           = DESIRED_HEADER_LINE_HEIGHT (w);
        }
 
       if (fonts_changed_p)
@@ -8343,10 +9036,10 @@ redisplay_window (window, just_this_one_p)
         display_menu_bar (w);
 
 #ifdef HAVE_WINDOW_SYSTEM
-      if (WINDOWP (f->toolbar_window)
-         && (FRAME_TOOLBAR_LINES (f) > 0
-             || auto_resize_toolbars_p))
-       redisplay_toolbar (f);
+      if (WINDOWP (f->tool_bar_window)
+         && (FRAME_TOOL_BAR_LINES (f) > 0
+             || auto_resize_tool_bars_p))
+       redisplay_tool_bar (f);
 #endif
     }
 
@@ -8363,10 +9056,9 @@ redisplay_window (window, just_this_one_p)
         visible region.
 
         Note that mini-buffers sometimes aren't displaying any text.  */
-      if (! MINI_WINDOW_P (w)
+      if (!MINI_WINDOW_P (w)
          || (w == XWINDOW (minibuf_window)
-             && !echo_area_glyphs
-             && !STRINGP (echo_area_message)))
+             && NILP (echo_area_buffer[0])))
        {
          whole = ZV - BEGV;
          start = marker_position (w->start) - BEGV;
@@ -8507,8 +9199,8 @@ try_window_reusing_current_matrix (w)
     return 0;
 
   /* If top-line visibility has changed, give up.  */
-  if (WINDOW_WANTS_TOP_LINE_P (w)
-      != MATRIX_TOP_LINE_ROW (w->current_matrix)->mode_line_p)
+  if (WINDOW_WANTS_HEADER_LINE_P (w)
+      != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
     return 0;
 
   /* Give up if old or new display is scrolled vertically.  We could
@@ -8615,7 +9307,7 @@ try_window_reusing_current_matrix (w)
          
          /* Re-compute Y positions.  */
          row = MATRIX_FIRST_TEXT_ROW (w->current_matrix) + nrows_scrolled;
-         min_y = WINDOW_DISPLAY_TOP_LINE_HEIGHT (w);
+         min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
          max_y = it.last_visible_y;
          while (row < bottom_row)
            {
@@ -8701,14 +9393,11 @@ try_window_reusing_current_matrix (w)
 
       /* Give up if there is no row to reuse.  */
       if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
-         || !first_reusable_row->enabled_p)
+         || !first_reusable_row->enabled_p
+         || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
+             != CHARPOS (new_start)))
        return 0;
 
-      /* The row we found must start at new_start, or else something
-        is broken.  */
-      xassert (MATRIX_ROW_START_CHARPOS (first_reusable_row)
-              == CHARPOS (new_start));
-      
       /* We can reuse fully visible rows beginning with
          first_reusable_row to the end of the window.  Set
          first_row_to_display to the first row that cannot be reused.
@@ -8761,7 +9450,7 @@ try_window_reusing_current_matrix (w)
 
       /* Scroll the display.  */
       run.current_y = first_reusable_row->y;
-      run.desired_y = WINDOW_DISPLAY_TOP_LINE_HEIGHT (w);
+      run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
       run.height = it.last_visible_y - run.current_y;
       if (run.height)
        {
@@ -8777,7 +9466,7 @@ try_window_reusing_current_matrix (w)
       bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
       row = first_reusable_row;
       dy = first_reusable_row->y;
-      min_y = WINDOW_DISPLAY_TOP_LINE_HEIGHT (w);
+      min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
       max_y = it.last_visible_y;
       while (row < first_row_to_display)
        {
@@ -8896,7 +9585,7 @@ static struct glyph_row *
 get_last_unchanged_at_beg_row (w)
      struct window *w;
 {
-  int first_changed_pos = BEG + beg_unchanged;
+  int first_changed_pos = BEG + BEG_UNCHANGED;
   struct glyph_row *row;
   struct glyph_row *row_found = NULL;
   int yb = window_text_bottom_y (w);
@@ -8933,14 +9622,10 @@ get_last_unchanged_at_beg_row (w)
 /* Find the first glyph row in the current matrix of W that is not
    affected by changes at the end of current_buffer since the last
    time the window was redisplayed.  Return in *DELTA the number of
-   bytes by which buffer positions in unchanged text at the end of
-   current_buffer must be adjusted.  Value is null if no such row
-   exists, i.e. all rows are affected by changes.
-
-   The global variable end_unchanged is assumed to contain the number
-   of unchanged bytes at the end of current_buffer.  The buffer
-   position of the last changed byte in current_buffer is then Z -
-   end_unchanged.  */
+   chars by which buffer positions in unchanged text at the end of
+   current_buffer must be adjusted.  Return in *DELTA_BYTES the
+   corresponding number of bytes.  Value is null if no such row
+   exists, i.e. all rows are affected by changes.  */
    
 static struct glyph_row *
 get_first_unchanged_at_end_row (w, delta, delta_bytes)
@@ -8956,17 +9641,12 @@ get_first_unchanged_at_end_row (w, delta, delta_bytes)
      end is in the range of changed text.  If so, there is no
      unchanged row at the end of W's current matrix.  */
   xassert (!NILP (w->window_end_valid));
-  if (XFASTINT (w->window_end_pos) >= end_unchanged)
+  if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
     return NULL;
 
   /* Set row to the last row in W's current matrix displaying text.  */
   row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
   
-  /* End vpos should always be on text, except in an entirely empty
-     matrix.  */
-  xassert (MATRIX_ROW_DISPLAYS_TEXT_P (row)
-          || MATRIX_ROW_VPOS (row, w->current_matrix) == 0);
-
   /* If matrix is entirely empty, no unchanged row exists.  */ 
   if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
     {
@@ -8990,7 +9670,7 @@ get_first_unchanged_at_end_row (w, delta, delta_bytes)
         subtracting end_unchanged we get the index of the last
         unchanged character, and we have to add BEG to get its buffer
         position.  */
-      last_unchanged_pos = Z - end_unchanged + BEG;
+      last_unchanged_pos = Z - END_UNCHANGED + BEG;
       last_unchanged_pos_old = last_unchanged_pos - *delta;
       
       /* Search backward from ROW for a row displaying a line that
@@ -9039,13 +9719,65 @@ sync_frame_with_window_matrix_rows (w)
   while (window_row < window_row_end)
     {
       int area;
+      
       for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
        frame_row->glyphs[area] = window_row->glyphs[area];
+
+      /* Disable frame rows whose corresponding window rows have
+        been disabled in try_window_id.  */
+      if (!window_row->enabled_p)
+       frame_row->enabled_p = 0;
+      
       ++window_row, ++frame_row;
     }
 }
 
 
+/* Find the glyph row in window W containing CHARPOS.  Consider all
+   rows between START and END (not inclusive).  END null means search
+   all rows to the end of the display area of W.  Value is the row
+   containing CHARPOS or null.  */
+
+static struct glyph_row *
+row_containing_pos (w, charpos, start, end)
+     struct window *w;
+     int charpos;
+     struct glyph_row *start, *end;
+{
+  struct glyph_row *row = start;
+  int last_y;
+
+  /* If we happen to start on a header-line, skip that.  */
+  if (row->mode_line_p)
+    ++row;
+  
+  if ((end && row >= end) || !row->enabled_p)
+    return NULL;
+  
+  last_y = window_text_bottom_y (w);
+      
+  while ((end == NULL || row < end)
+        && (MATRIX_ROW_END_CHARPOS (row) < charpos
+            /* The end position of a row equals the start
+               position of the next row.  If CHARPOS is there, we
+               would rather display it in the next line, except
+               when this line ends in ZV.  */
+            || (MATRIX_ROW_END_CHARPOS (row) == charpos
+                && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
+                    || !row->ends_at_zv_p)))
+        && MATRIX_ROW_BOTTOM_Y (row) < last_y)
+    ++row;
+      
+  /* Give up if CHARPOS not found.  */
+  if ((end && row >= end)
+      || charpos < MATRIX_ROW_START_CHARPOS (row)
+      || charpos > MATRIX_ROW_END_CHARPOS (row))
+    row = NULL;
+
+  return row;
+}
+
+
 /* Try to redisplay window W by reusing its existing display.  W's
    current matrix must be up to date when this function is called,
    i.e. window_end_valid must not be nil.
@@ -9117,10 +9849,10 @@ try_window_id (w)
      set end_unchanged to 0 in that case.  */
   if (MODIFF > SAVE_MODIFF)
     {
-      if (GPT - BEG < beg_unchanged)
-       beg_unchanged = GPT - BEG;
-      if (Z - GPT < end_unchanged)
-       end_unchanged = Z - GPT;
+      if (GPT - BEG < BEG_UNCHANGED)
+       BEG_UNCHANGED = GPT - BEG;
+      if (Z - GPT < END_UNCHANGED)
+       END_UNCHANGED = Z - GPT;
     }
   
   /* If window starts after a line end, and the last change is in
@@ -9128,7 +9860,7 @@ try_window_id (w)
      This case happens with stealth-fontification.  */
   row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
   if (CHARPOS (start) > BEGV
-      && Z - end_unchanged < CHARPOS (start) - 1
+      && Z - END_UNCHANGED < CHARPOS (start) - 1
       && FETCH_BYTE (BYTEPOS (start) - 1) == '\n'
       && PT < MATRIX_ROW_END_CHARPOS (row))
     {
@@ -9143,7 +9875,7 @@ try_window_id (w)
 
   /* Return quickly if changes are all below what is displayed in the
      window, and if PT is in the window.  */
-  if (beg_unchanged > MATRIX_ROW_END_CHARPOS (row)
+  if (BEG_UNCHANGED > MATRIX_ROW_END_CHARPOS (row)
       && PT < MATRIX_ROW_END_CHARPOS (row))
     {
       /* We have to update window end positions because the buffer's
@@ -9163,10 +9895,6 @@ try_window_id (w)
   if (!TEXT_POS_EQUAL_P (start, row->start.pos))
     return 0;
 
-  /* Remember beg_unchanged and end_unchanged for debugging purposes.  */
-  IF_DEBUG (debug_beg_unchanged = beg_unchanged;
-           debug_end_unchanged = end_unchanged);
-
   /* Compute the position at which we have to start displaying new
      lines.  Some of the lines at the top of the window might be
      reusable because they are not displaying changed text.  Find the
@@ -9197,9 +9925,6 @@ try_window_id (w)
       start_pos = it.current.pos;
     }
 
-  bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
-  bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
-  
   /* Find the first row that is not affected by changes at the end of
      the buffer.  Value will be null if there is no unchanged row, in
      which case we must redisplay to the end of the window.  delta
@@ -9242,7 +9967,7 @@ try_window_id (w)
                      + delta);
          first_unchanged_at_end_vpos
            = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
-         xassert (stop_pos >= Z - end_unchanged);
+         xassert (stop_pos >= Z - END_UNCHANGED);
        }
     }
   else if (last_unchanged_at_beg_row == NULL)
@@ -9288,7 +10013,10 @@ try_window_id (w)
   /* Compute differences in buffer positions, y-positions etc.  for
      lines reused at the bottom of the window.  Compute what we can
      scroll.  */
-  if (first_unchanged_at_end_row)
+  if (first_unchanged_at_end_row
+      /* No lines reused because we displayed everything up to the
+         bottom of the window.  */
+      && it.current_y < it.last_visible_y)
     {
       dvpos = (it.vpos
               - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
@@ -9299,10 +10027,13 @@ try_window_id (w)
       run.height = it.last_visible_y - max (run.current_y, run.desired_y);
     }
   else
-    delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
+    {
+      delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
+      first_unchanged_at_end_row = NULL;
+    }
   IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
 
-  
+
   /* Find the cursor if not already found.  We have to decide whether
      PT will appear on this window (it sometimes doesn't, but this is
      not a very frequent case.)  This decision has to be made before
@@ -9313,37 +10044,25 @@ try_window_id (w)
      mentioned, this is not a frequent case.  */
   if (w->cursor.vpos < 0)
     {
-      int last_y = min (it.last_visible_y, it.last_visible_y + dy);
-
       /* Cursor in unchanged rows at the top?  */
       if (PT < CHARPOS (start_pos)
          && last_unchanged_at_beg_row)
        {
-         row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
-         while (row <= last_unchanged_at_beg_row
-                && MATRIX_ROW_END_CHARPOS (row) <= PT)
-           ++row;
-         xassert (row <= last_unchanged_at_beg_row);
+         row = row_containing_pos (w, PT,
+                                   MATRIX_FIRST_TEXT_ROW (w->current_matrix),
+                                   last_unchanged_at_beg_row + 1);
+         xassert (row && row <= last_unchanged_at_beg_row);
          set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
        }
 
       /* Start from first_unchanged_at_end_row looking for PT.  */
       else if (first_unchanged_at_end_row)
        {
-         row = first_unchanged_at_end_row;
-         while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
-           {
-             if (PT - delta >= MATRIX_ROW_START_CHARPOS (row)
-                 && PT - delta < MATRIX_ROW_END_CHARPOS (row))
-               {
-                 set_cursor_from_row (w, row, w->current_matrix, delta,
-                                      delta_bytes, dy, dvpos);
-                 break;
-               }
-             else if (MATRIX_ROW_BOTTOM_Y (row) >= last_y)
-               break;
-             ++row;
-           }
+         row = row_containing_pos (w, PT - delta,
+                                   first_unchanged_at_end_row, NULL);
+         if (row)
+           set_cursor_from_row (w, row, w->current_matrix, delta,
+                                delta_bytes, dy, dvpos);
        }
 
       /* Give up if cursor was not found.  */
@@ -9438,7 +10157,11 @@ try_window_id (w)
       update_end (f);
     }
 
-  /* Shift reused rows of the current matrix to the right position.  */
+  /* Shift reused rows of the current matrix to the right position.
+     BOTTOM_ROW is the last + 1 row in the current matrix reserved for
+     text.  */
+  bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
+  bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
   if (dvpos < 0)
     {
       rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
@@ -9572,13 +10295,13 @@ try_window_id (w)
       /* Displayed to end of window, but no line containing text was
         displayed.  Lines were deleted at the end of the window.  */
       int vpos;
-      int top_line_p = WINDOW_WANTS_TOP_LINE_P (w) ? 1 : 0;
+      int header_line_p = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
 
       for (vpos = XFASTINT (w->window_end_vpos); vpos > 0; --vpos)
-       if ((w->desired_matrix->rows[vpos + top_line_p].enabled_p
-            && w->desired_matrix->rows[vpos + top_line_p].displays_text_p)
-           || (!w->desired_matrix->rows[vpos + top_line_p].enabled_p
-               && w->current_matrix->rows[vpos + top_line_p].displays_text_p))
+       if ((w->desired_matrix->rows[vpos + header_line_p].enabled_p
+            && w->desired_matrix->rows[vpos + header_line_p].displays_text_p)
+           || (!w->desired_matrix->rows[vpos + header_line_p].enabled_p
+               && w->current_matrix->rows[vpos + header_line_p].displays_text_p))
          break;
 
       w->window_end_vpos = make_number (vpos);
@@ -9769,11 +10492,12 @@ DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 1, "",
 }
 
 
-DEFUN ("dump-toolbar-row", Fdump_toolbar_row, Sdump_toolbar_row,
+DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row,
        0, 0, "", "")
   ()
 {
-  struct glyph_matrix *m = (XWINDOW (selected_frame->toolbar_window)
+  struct frame *sf = SELECTED_FRAME ();
+  struct glyph_matrix *m = (XWINDOW (sf->tool_bar_window)
                            ->current_matrix);
   dump_glyph_row (m, 0, 1);
   return Qnil;
@@ -9930,7 +10654,7 @@ compute_line_metrics (it)
 
   if (FRAME_WINDOW_P (it->f))
     {
-      int i, top_line_height;
+      int i, header_line_height;
 
       /* The line may consist of one space only, that was added to
         place the cursor on it.  If so, the row's height hasn't been
@@ -9969,9 +10693,9 @@ compute_line_metrics (it)
       /* Compute how much of the line is visible.  */
       row->visible_height = row->height;
       
-      top_line_height = WINDOW_DISPLAY_TOP_LINE_HEIGHT (it->w);
-      if (row->y < top_line_height)
-       row->visible_height -= top_line_height - row->y;
+      header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
+      if (row->y < header_line_height)
+       row->visible_height -= header_line_height - row->y;
       else
        {
          int max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
@@ -10002,10 +10726,15 @@ compute_line_metrics (it)
 /* Append one space to the glyph row of iterator IT if doing a
    window-based redisplay.  DEFAULT_FACE_P non-zero means let the
    space have the default face, otherwise let it have the same face as
-   IT->face_id.  This function is called to make sure that there is
-   always one glyph at the end of a glyph row that the cursor can be
-   set on under window-systems.  (If there weren't such a glyph we
-   would not know how wide and tall the cursor should be displayed).  */
+   IT->face_id.
+
+   This function is called to make sure that there is always one glyph
+   at the end of a glyph row that the cursor can be set on under
+   window-systems.  (If there weren't such a glyph we would not know
+   how wide and tall a box cursor should be displayed).
+
+   At the same time this space let's a nicely handle clearing to the
+   end of the line if the row ends in italic text.  */
 
 static void
 append_space (it, default_face_p)
@@ -10253,45 +10982,6 @@ display_line (it)
      recenter_overlay_lists but the first will be pretty cheap.  */
   recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
 
-#if NO_PROMPT_IN_BUFFER
-  /* Show mini-buffer prompt, if at the beginning of a mini-buffer
-     window.  */
-  if (MINI_WINDOW_P (it->w) 
-      && MATRIX_ROW_START_CHARPOS (row) == BEG 
-      && it->vpos == 0)
-    {
-      if (NILP (minibuf_prompt))
-       minibuf_prompt_width = minibuf_prompt_pixel_width = 0;
-      else
-       {
-         /* We would like to truncate the prompt a little bit before
-            the right margin of the window, so that user input can
-            start on the first line.  Set max_x to this position.  */
-         int max_x = (it->last_visible_x - 4 * CANON_X_UNIT (it->f));
-
-         /* We use a temporary iterator different from IT so that
-            IT's settings are not overwritten when displaying
-            the prompt.  */
-         struct it ti;
-
-         ti = *it;
-
-         /* Display the prompt.  Set minibuf_prompt_width to the 
-            number of glyphs generated for the prompt, set
-            minibuf_prompt_pixel_width to its width in pixels.  */
-         xassert (it->current_x == 0);
-         display_string (NULL, minibuf_prompt, Qnil, 0, 0, &ti,
-                         0, 0, max_x, -1);
-         minibuf_prompt_width = ti.hpos;
-         minibuf_prompt_pixel_width = ti.current_x;
-
-         /* Transfer pixel and hpos information to IT.  */
-         it->hpos = ti.hpos;
-         it->current_x = ti.current_x;
-       }
-    }
-#endif /* NO_PROMPT_IN_BUFFER */
-
   /* Move over display elements that are not visible because we are
      hscrolled.  This may stop at an x-position < IT->first_visible_x
      if the first glyph is partially visible or if we hit a line end.  */
@@ -10722,15 +11412,17 @@ display_mode_lines (w)
   w->column_number_displayed = Qnil;
 
   if (WINDOW_WANTS_MODELINE_P (w))
-    display_mode_line (w, MODE_LINE_FACE_ID, current_buffer->mode_line_format);
+    display_mode_line (w, MODE_LINE_FACE_ID,
+                      current_buffer->mode_line_format);
   
-  if (WINDOW_WANTS_TOP_LINE_P (w))
-    display_mode_line (w, TOP_LINE_FACE_ID, current_buffer->top_line_format);
+  if (WINDOW_WANTS_HEADER_LINE_P (w))
+    display_mode_line (w, HEADER_LINE_FACE_ID,
+                      current_buffer->header_line_format);
 }
 
 
 /* Display mode or top line of window W.  FACE_ID specifies which line
-   to display; it is either MODE_LINE_FACE_ID or TOP_LINE_FACE_ID.
+   to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
    FORMAT is the mode line format to display.  */
 
 static void
@@ -10948,7 +11640,7 @@ display_mode_element (it, depth, field_width, precision, elt)
           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.  */
-       car = XCONS (elt)->car;
+       car = XCAR (elt);
        if (EQ (car, QCeval) && CONSP (XCDR (elt)))
          {
            /* An element of the form (:eval FORM) means evaluate FORM
@@ -10965,7 +11657,7 @@ display_mode_element (it, depth, field_width, precision, elt)
        else if (SYMBOLP (car))
          {
            tem = Fboundp (car);
-           elt = XCONS (elt)->cdr;
+           elt = XCDR (elt);
            if (!CONSP (elt))
              goto invalid;
            /* elt is now the cdr, and we know it is a cons cell.
@@ -10974,23 +11666,26 @@ display_mode_element (it, depth, field_width, precision, elt)
              {
                tem = Fsymbol_value (car);
                if (!NILP (tem))
-                 { elt = XCONS (elt)->car; goto tail_recurse; }
+                 {
+                   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 = XCONS (elt)->cdr;
+           elt = XCDR (elt);
            if (NILP (elt))
              break;
            else if (!CONSP (elt))
              goto invalid;
-           elt = XCONS (elt)->car;
+           elt = XCAR (elt);
            goto tail_recurse;
          }
        else if (INTEGERP (car))
          {
            register int lim = XINT (car);
-           elt = XCONS (elt)->cdr;
+           elt = XCDR (elt);
            if (lim < 0)
              {
                /* Negative int means reduce maximum width.  */
@@ -11022,8 +11717,8 @@ display_mode_element (it, depth, field_width, precision, elt)
                   && (precision <= 0 || n < precision))
              {
                n += display_mode_element (it, depth, field_width - n,
-                                          precision - n, XCONS (elt)->car);
-               elt = XCONS (elt)->cdr;
+                                          precision - n, XCAR (elt));
+               elt = XCDR (elt);
              }
          }
       }
@@ -11158,7 +11853,6 @@ decode_mode_spec_coding (coding_system, buf, eol_flag)
       else if (INTEGERP (eoltype)
               && CHAR_VALID_P (XINT (eoltype), 0))
        {
-         int c = XINT (eoltype);
          unsigned char work[4];
 
          eol_str_len = CHAR_STRING (XINT (eoltype), work, eol_str);
@@ -11842,28 +12536,28 @@ invisible_p (propval, list)
      Lisp_Object list;
 {
   register Lisp_Object tail, proptail;
-  for (tail = list; CONSP (tail); tail = XCONS (tail)->cdr)
+  for (tail = list; CONSP (tail); tail = XCDR (tail))
     {
       register Lisp_Object tem;
-      tem = XCONS (tail)->car;
+      tem = XCAR (tail);
       if (EQ (propval, tem))
        return 1;
-      if (CONSP (tem) && EQ (propval, XCONS (tem)->car))
+      if (CONSP (tem) && EQ (propval, XCAR (tem)))
        return 1;
     }
   if (CONSP (propval))
     for (proptail = propval; CONSP (proptail);
-        proptail = XCONS (proptail)->cdr)
+        proptail = XCDR (proptail))
       {
        Lisp_Object propelt;
-       propelt = XCONS (proptail)->car;
-       for (tail = list; CONSP (tail); tail = XCONS (tail)->cdr)
+       propelt = XCAR (proptail);
+       for (tail = list; CONSP (tail); tail = XCDR (tail))
          {
            register Lisp_Object tem;
-           tem = XCONS (tail)->car;
+           tem = XCAR (tail);
            if (EQ (propelt, tem))
              return 1;
-           if (CONSP (tem) && EQ (propelt, XCONS (tem)->car))
+           if (CONSP (tem) && EQ (propelt, XCAR (tem)))
              return 1;
          }
       }
@@ -11883,27 +12577,29 @@ invisible_ellipsis_p (propval, list)
      Lisp_Object list;
 {
   register Lisp_Object tail, proptail;
-  for (tail = list; CONSP (tail); tail = XCONS (tail)->cdr)
+  
+  for (tail = list; CONSP (tail); tail = XCDR (tail))
     {
       register Lisp_Object tem;
-      tem = XCONS (tail)->car;
-      if (CONSP (tem) && EQ (propval, XCONS (tem)->car))
-       return ! NILP (XCONS (tem)->cdr);
+      tem = XCAR (tail);
+      if (CONSP (tem) && EQ (propval, XCAR (tem)))
+       return ! NILP (XCDR (tem));
     }
+  
   if (CONSP (propval))
-    for (proptail = propval; CONSP (proptail);
-        proptail = XCONS (proptail)->cdr)
+    for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
       {
        Lisp_Object propelt;
-       propelt = XCONS (proptail)->car;
-       for (tail = list; CONSP (tail); tail = XCONS (tail)->cdr)
+       propelt = XCAR (proptail);
+       for (tail = list; CONSP (tail); tail = XCDR (tail))
          {
            register Lisp_Object tem;
-           tem = XCONS (tail)->car;
-           if (CONSP (tem) && EQ (propelt, XCONS (tem)->car))
-             return ! NILP (XCONS (tem)->cdr);
+           tem = XCAR (tail);
+           if (CONSP (tem) && EQ (propelt, XCAR (tem)))
+             return ! NILP (XCDR (tem));
          }
       }
+  
   return 0;
 }
 
@@ -11916,17 +12612,19 @@ invisible_ellipsis_p (propval, list)
 void
 syms_of_xdisp ()
 {
-  echo_area_message = previous_echo_area_message = Qnil;
-  staticpro (&echo_area_message);
-  staticpro (&previous_echo_area_message);
+  Vwith_echo_area_save_vector = Qnil;
+  staticpro (&Vwith_echo_area_save_vector);
 
-  staticpro (&Qinhibit_redisplay);
+  Vmessage_stack = Qnil;
+  staticpro (&Vmessage_stack);
+  
   Qinhibit_redisplay = intern ("inhibit-redisplay");
+  staticpro (&Qinhibit_redisplay);
 
 #if GLYPH_DEBUG
   defsubr (&Sdump_glyph_matrix);
   defsubr (&Sdump_glyph_row);
-  defsubr (&Sdump_toolbar_row);
+  defsubr (&Sdump_tool_bar_row);
   defsubr (&Strace_redisplay_toggle);
 #endif
 
@@ -11948,9 +12646,8 @@ syms_of_xdisp ()
   staticpro (&Qinhibit_point_motion_hooks);
   Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
 
-  staticpro (&Qdisplay);
   Qdisplay = intern ("display");
-  staticpro (&Qleft_margin);
+  staticpro (&Qdisplay);
   Qspace_width = intern ("space-width");
   staticpro (&Qspace_width);
   Qheight = intern ("height");
@@ -11959,9 +12656,12 @@ syms_of_xdisp ()
   staticpro (&Qraise);
   Qspace = intern ("space");
   staticpro (&Qspace);
+  Qmargin = intern ("margin");
+  staticpro (&Qmargin);
   Qleft_margin = intern ("left-margin");
-  staticpro (&Qright_margin);
+  staticpro (&Qleft_margin);
   Qright_margin = intern ("right-margin");
+  staticpro (&Qright_margin);
   Qalign_to = intern ("align-to");
   staticpro (&Qalign_to);
   QCalign_to = intern (":align-to");
@@ -11976,8 +12676,10 @@ syms_of_xdisp ()
   staticpro (&QCrelative_height);
   QCeval = intern (":eval");
   staticpro (&QCeval);
-  QCwhen = intern (":when");
-  staticpro (&QCwhen);
+  Qwhen = intern ("when");
+  staticpro (&Qwhen);
+  QCfile = intern (":file");
+  staticpro (&QCfile);
   Qfontified = intern ("fontified");
   staticpro (&Qfontified);
   Qfontification_functions = intern ("fontification-functions");
@@ -11987,10 +12689,18 @@ syms_of_xdisp ()
   Qimage = intern ("image");
   staticpro (&Qimage);
 
-  staticpro (&last_arrow_position);
-  staticpro (&last_arrow_string);
   last_arrow_position = Qnil;
   last_arrow_string = Qnil;
+  staticpro (&last_arrow_position);
+  staticpro (&last_arrow_string);
+  
+  echo_buffer[0] = echo_buffer[1] = Qnil;
+  staticpro (&echo_buffer[0]);
+  staticpro (&echo_buffer[1]);
+
+  echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
+  staticpro (&echo_area_buffer[0]);
+  staticpro (&echo_area_buffer[1]);
 
   DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
     "Non-nil means highlight trailing whitespace.\n\
@@ -12110,24 +12820,24 @@ and its new display-start position.  Note that the value of `window-end'\n\
 is not valid when these functions are called.");
   Vwindow_scroll_functions = Qnil;
   
-  DEFVAR_BOOL ("auto-resize-toolbars", &auto_resize_toolbars_p,
-    "*Non-nil means automatically resize toolbars.\n\
-This increases a toolbar's height if not all toolbar items are visible.\n\
-It decreases a toolbar's height when it would display blank lines\n\
+  DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
+    "*Non-nil means automatically resize tool-bars.\n\
+This increases a tool-bar's height if not all tool-bar items are visible.\n\
+It decreases a tool-bar's height when it would display blank lines\n\
 otherwise.");
-  auto_resize_toolbars_p = 1;
+  auto_resize_tool_bars_p = 1;
   
-  DEFVAR_BOOL ("auto-raise-toolbar-buttons", &auto_raise_toolbar_buttons_p,
-    "*Non-nil means raise toolbar buttons when the mouse moves over them.");
-  auto_raise_toolbar_buttons_p = 1;
+  DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
+    "*Non-nil means raise tool-bar buttons when the mouse moves over them.");
+  auto_raise_tool_bar_buttons_p = 1;
 
-  DEFVAR_INT ("toolbar-button-margin", &toolbar_button_margin,
-    "*Margin around toolbar buttons in pixels.");
-  toolbar_button_margin = 1;
+  DEFVAR_INT ("tool-bar-button-margin", &tool_bar_button_margin,
+    "*Margin around tool-bar buttons in pixels.");
+  tool_bar_button_margin = 1;
 
-  DEFVAR_INT ("toolbar-button-relief", &toolbar_button_relief,
-    "Relief thickness of toolbar buttons.");
-  toolbar_button_relief = 3;
+  DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
+    "Relief thickness of tool-bar buttons.");
+  tool_bar_button_relief = 3;
 
   DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
     "List of functions to call to fontify regions of text.\n\
@@ -12146,6 +12856,13 @@ are displayed by converting them to the equivalent multibyte characters\n\
 according to the current language environment.  As a result, they are\n\
 displayed according to the current fontset.");
   unibyte_display_via_language_environment = 0;
+
+  DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
+    "*Maximum height for resizing mini-windows.\n\
+If a float, it specifies a fraction of the mini-window frame's height.\n\
+If an integer, it specifies a number of lines.\n\
+If nil, don't resize.");
+  Vmax_mini_window_height = make_float (0.25);
 }
 
 
@@ -12162,10 +12879,6 @@ init_xdisp ()
   mini_w = XWINDOW (minibuf_window);
   root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
 
-  echo_area_glyphs = 0;
-  previous_echo_glyphs = 0;
-  echo_area_message = previous_echo_area_message = Qnil;
-
   if (!noninteractive)
     {
       struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));