(handle_fontified_prop): While running fontification
[bpt/emacs.git] / src / xdisp.c
index e5ffabb..9d9815e 100644 (file)
@@ -1,5 +1,5 @@
 /* Display generation from window structure and buffer text.
-   Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 97, 98, 99
+   Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 97, 98, 99, 2000
    Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
@@ -169,10 +169,8 @@ Boston, MA 02111-1307, USA.  */
 
 #include <config.h>
 #include <stdio.h>
-#ifdef STDC_HEADERS
-#include <stdlib.h>
-#endif
 #include "lisp.h"
+#include "keyboard.h"
 #include "frame.h"
 #include "window.h"
 #include "termchar.h"
@@ -185,14 +183,17 @@ Boston, MA 02111-1307, USA.  */
 #include "disptab.h"
 #include "termhooks.h"
 #include "intervals.h"
-#include "keyboard.h"
 #include "coding.h"
 #include "process.h"
 #include "region-cache.h"
+#include "fontset.h"
 
 #ifdef HAVE_X_WINDOWS
 #include "xterm.h"
 #endif
+#ifdef WINDOWSNT
+#include "w32term.h"
+#endif
 
 #define min(a, b) ((a) < (b) ? (a) : (b))
 #define max(a, b) ((a) > (b) ? (a) : (b))
@@ -200,7 +201,7 @@ Boston, MA 02111-1307, USA.  */
 #define INFINITY 10000000
 
 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
-extern void set_frame_menubar ();
+extern void set_frame_menubar P_ ((struct frame *f, int, int));
 extern int pending_menu_activation;
 #endif
 
@@ -219,7 +220,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, Qwhen, QCfile;
+Lisp_Object QCeval, Qwhen, QCfile, QCdata;
 Lisp_Object Qfontified;
 
 /* Functions called to fontify regions of text.  */
@@ -251,14 +252,15 @@ Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
 
 /* Names of text properties relevant for redisplay.  */
 
-Lisp_Object Qdisplay, Qrelative_width, Qwidth, Qalign_to;
-extern Lisp_Object Qface, Qinvisible, Qimage;
+Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
+extern Lisp_Object Qface, Qinvisible, Qimage, Qwidth;
 
 /* Symbols used in text property values.  */
 
 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
-Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qheight, Qraise;
+Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
 Lisp_Object Qmargin;
+extern Lisp_Object Qheight;
 
 /* Non-nil means highlight trailing whitespace.  */
 
@@ -440,7 +442,7 @@ int line_number_displayed;
 
 /* Maximum buffer size for which to display line numbers.  */
 
-static int line_number_display_limit;
+Lisp_Object Vline_number_display_limit;
 
 /* line width to consider when repostioning for line number display */
 
@@ -451,6 +453,10 @@ static int line_number_display_limit_width;
 
 Lisp_Object Vmessage_log_max;
 
+/* The name of the *Messages* buffer, a string.  */
+
+static Lisp_Object Vmessages_buffer_name;
+
 /* Current, index 0, and last displayed echo area message.  Either
    buffers from echo_buffers, or nil to indicate no message.  */
 
@@ -478,7 +484,18 @@ int message_buf_print;
    specifying a fraction of the available height, or an integer
    specifying a number of lines.  */
 
-static Lisp_Object Vmax_mini_window_height;
+Lisp_Object Vmax_mini_window_height;
+
+/* Non-zero means messages should be displayed with truncated
+   lines instead of being continued.  */
+
+int message_truncate_lines;
+Lisp_Object Qmessage_truncate_lines;
+
+/* Non-zero means we want a hollow cursor in windows that are not
+   selected.  Zero means there's no cursor in such windows.  */
+
+int cursor_in_non_selected_windows;
 
 /* A scratch glyph row with contents used for generating truncation
    glyphs.  Also used in direct_output_for_insert.  */
@@ -499,12 +516,32 @@ static int last_max_ascent, last_height;
 
 #define TEXT_PROP_DISTANCE_LIMIT 100
 
+#if GLYPH_DEBUG
+
 /* Non-zero means print traces of redisplay if compiled with
    GLYPH_DEBUG != 0.  */
 
-#if GLYPH_DEBUG
 int trace_redisplay_p;
+
+#endif /* GLYPH_DEBUG */
+
+#ifdef DEBUG_TRACE_MOVE
+/* Non-zero means trace with TRACE_MOVE to stderr.  */
+int trace_move;
+
+#define TRACE_MOVE(x)  if (trace_move) fprintf x; else (void) 0
+#else
+#define TRACE_MOVE(x)  (void) 0
 #endif
+/* Non-zero means automatically scroll windows horizontally to make
+   point visible.  */
+
+int automatic_hscrolling_p;
+
+/* A list of symbols, one for each supported image type.  */
+
+Lisp_Object Vimage_types;
 
 /* Value returned from text property handlers (see below).  */
 
@@ -535,6 +572,7 @@ struct props
 static enum prop_handled handle_face_prop P_ ((struct it *));
 static enum prop_handled handle_invisible_prop P_ ((struct it *));
 static enum prop_handled handle_display_prop P_ ((struct it *));
+static enum prop_handled handle_composition_prop P_ ((struct it *));
 static enum prop_handled handle_overlay_change P_ ((struct it *));
 static enum prop_handled handle_fontified_prop P_ ((struct it *));
 
@@ -548,6 +586,7 @@ static struct props it_props[] =
   {&Qface,             FACE_PROP_IDX,          handle_face_prop},
   {&Qdisplay,          DISPLAY_PROP_IDX,       handle_display_prop},
   {&Qinvisible,                INVISIBLE_PROP_IDX,     handle_invisible_prop},
+  {&Qcomposition,      COMPOSITION_PROP_IDX,   handle_composition_prop},
   {NULL,               0,                      NULL}
 };
 
@@ -585,30 +624,50 @@ enum move_it_result
 \f
 /* Function prototypes.  */
 
+static int redisplay_mode_lines P_ ((Lisp_Object, int));
+static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
+static int invisible_text_between_p P_ ((struct it *, int, int));
+static int next_element_from_ellipsis P_ ((struct it *));
+static void pint2str P_ ((char *, int, int));
+static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
+                                                       struct text_pos));
+static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
+static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
+static void store_frame_title_char P_ ((char));
+static int store_frame_title P_ ((unsigned char *, int, int));
+static void x_consider_frame_title P_ ((Lisp_Object));
+static void handle_stop P_ ((struct it *));
+static int tool_bar_lines_needed P_ ((struct frame *));
+static int single_display_prop_intangible_p P_ ((Lisp_Object));
+static void ensure_echo_area_buffers P_ ((void));
 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 int with_echo_area_buffer P_ ((struct window *, int,
+                                     int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
+                                     EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
 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 current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
+static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
+static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
 static int display_echo_area P_ ((struct window *));
-static int display_echo_area_1 P_ ((struct window *));
+static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
+static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
 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));
 static int compute_window_start_on_continuation_line P_ ((struct window *));
 static Lisp_Object eval_handler P_ ((Lisp_Object));
-static Lisp_Object eval_form P_ ((Lisp_Object));
 static void insert_left_trunc_glyphs P_ ((struct it *));
 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
 static void extend_face_to_end_of_line P_ ((struct it *));
-static void append_space P_ ((struct it *, int));
+static int append_space P_ ((struct it *, int));
 static void make_cursor_line_fully_visible P_ ((struct window *));
 static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
+static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
 static int trailing_whitespace_p P_ ((int));
 static int message_log_check_duplicate P_ ((int, int, int, int));
 int invisible_p P_ ((Lisp_Object, Lisp_Object));
@@ -624,11 +683,11 @@ static void update_menu_bar P_ ((struct frame *, int));
 static int try_window_reusing_current_matrix P_ ((struct window *));
 static int try_window_id P_ ((struct window *));
 static int display_line P_ ((struct it *));
-static void display_mode_lines P_ ((struct window *));
+static int display_mode_lines P_ ((struct window *));
 static void display_mode_line P_ ((struct window *, enum face_id,
                                   Lisp_Object));
 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object));
-static char *decode_mode_spec P_ ((struct window *, char, int, int));
+static char *decode_mode_spec P_ ((struct window *, int, int, int));
 static void display_menu_bar P_ ((struct window *));
 static int display_count_lines P_ ((int, int, int, int, int *));
 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
@@ -647,6 +706,7 @@ static int next_element_from_display_vector P_ ((struct it *));
 static int next_element_from_string P_ ((struct it *));
 static int next_element_from_c_string P_ ((struct it *));
 static int next_element_from_buffer P_ ((struct it *));
+static int next_element_from_composition P_ ((struct it *));
 static int next_element_from_image P_ ((struct it *));
 static int next_element_from_stretch P_ ((struct it *));
 static void load_overlay_strings P_ ((struct it *));
@@ -654,7 +714,6 @@ static void init_from_display_pos P_ ((struct it *, struct window *,
                                       struct display_pos *));
 static void reseat_to_string P_ ((struct it *, unsigned char *,
                                  Lisp_Object, int, int, int, int));
-static int charset_at_position P_ ((struct text_pos));
 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
                                                           int, int, int));
 void move_it_vertically_backward P_ ((struct it *, int));
@@ -864,7 +923,7 @@ window_box_edges (w, area, top_left_x, top_left_y,
 /* Return the next character from STR which is MAXLEN bytes long.
    Return in *LEN the length of the character.  This is like
    STRING_CHAR_AND_LENGTH but never returns an invalid character.  If
-   we find one, we return a `?', but with the length of the illegal
+   we find one, we return a `?', but with the length of the invalid
    character.  */
 
 static INLINE int
@@ -877,7 +936,7 @@ string_char_and_length (str, maxlen, len)
   c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
   if (!CHAR_VALID_P (c, 1))
     /* We may not change the length here because other places in Emacs
-       don't use this function, i.e. they silently accept illegal
+       don't use this function, i.e. they silently accept invalid
        characters.  */
     c = '?';
 
@@ -1012,44 +1071,19 @@ compute_string_pos (newpos, pos, string)
   xassert (CHARPOS (*newpos) >= CHARPOS (pos));
   
   if (STRING_MULTIBYTE (string))
-    *newpos = string_pos_nchars_ahead (pos, CHARPOS (*newpos) - CHARPOS (pos),
-                                      string);
+    *newpos = string_pos_nchars_ahead (pos, string,
+                                      CHARPOS (*newpos) - CHARPOS (pos));
   else
     BYTEPOS (*newpos) = CHARPOS (*newpos);
 }
 
 
-/* Return the charset of the character at position POS in
-   current_buffer.  */
-
-static int
-charset_at_position (pos)
-     struct text_pos pos;
-{
-  int c, multibyte_p;
-  unsigned char *p = BYTE_POS_ADDR (BYTEPOS (pos));
-
-  multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
-  if (multibyte_p)
-    {
-      int maxlen = ((BYTEPOS (pos) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
-                   - BYTEPOS (pos));
-      int len;
-      c = string_char_and_length (p, maxlen, &len);
-    }
-  else
-    c = *p;
-
-  return CHAR_CHARSET (c);
-}
-
-
 \f
 /***********************************************************************
                        Lisp form evaluation
  ***********************************************************************/
 
-/* Error handler for eval_form.  */
+/* Error handler for eval_form and call_function.  */
 
 static Lisp_Object
 eval_handler (arg)
@@ -1062,14 +1096,40 @@ eval_handler (arg)
 /* Evaluate SEXPR and return the result, or nil if something went
    wrong.  */
 
-static Lisp_Object
+Lisp_Object
 eval_form (sexpr)
      Lisp_Object sexpr;
 {
   int count = specpdl_ptr - specpdl;
+  struct gcpro gcpro1;
   Lisp_Object val;
+
+  GCPRO1 (sexpr);
   specbind (Qinhibit_redisplay, Qt);
   val = internal_condition_case_1 (Feval, sexpr, Qerror, eval_handler);
+  UNGCPRO;
+  return unbind_to (count, val);
+}
+
+
+/* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
+   Return the result, or nil if something went wrong.  */
+
+Lisp_Object
+call_function (nargs, args)
+     int nargs;
+     Lisp_Object *args;
+{
+  int count = specpdl_ptr - specpdl;
+  Lisp_Object val;
+  struct gcpro gcpro1;
+
+  GCPRO1 (args[0]);
+  gcpro1.nvars = nargs;
+  specbind (Qinhibit_redisplay, Qt);
+  val = internal_condition_case_2 (Ffuncall, nargs, args, Qerror,
+                                  eval_handler);
+  UNGCPRO;
   return unbind_to (count, val);
 }
 
@@ -1204,7 +1264,6 @@ init_iterator (it, w, charpos, bytepos, row, base_face_id)
   bzero (it, sizeof *it);
   it->current.overlay_string_index = -1;
   it->current.dpvec_index = -1;
-  it->charset = CHARSET_ASCII;
   it->base_face_id = base_face_id;
 
   /* The window in which we iterate over current_buffer:  */
@@ -1212,6 +1271,16 @@ init_iterator (it, w, charpos, bytepos, row, base_face_id)
   it->w = w;
   it->f = XFRAME (w->frame);
 
+  /* Extra space between lines (on window systems only).  */
+  if (base_face_id == DEFAULT_FACE_ID
+      && FRAME_WINDOW_P (it->f))
+    {
+      if (NATNUMP (current_buffer->extra_line_spacing))
+       it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
+      else if (it->f->extra_line_spacing > 0)
+       it->extra_line_spacing = it->f->extra_line_spacing;
+    }
+
   /* If realized faces have been removed, e.g. because of face
      attribute changes of named faces, recompute them.  */
   if (FRAME_FACE_CACHE (it->f)->used == 0)
@@ -1434,15 +1503,25 @@ start_display (it, w, pos)
         \003, or in the middle of an overlay string).  In this case
         move_it_to above will not have taken us to the start of
         the continuation line but to the end of the continued line.  */
-      if (!it->truncate_lines_p && it->current_x > 0)
+      if (!it->truncate_lines_p)
        {
-         if (it->current.dpvec_index >= 0
-             || it->current.overlay_string_index >= 0)
+         if (it->current_x > 0)
            {
-             set_iterator_to_next (it);
-             move_it_in_display_line_to (it, -1, -1, 0);
+             if (it->current.dpvec_index >= 0
+                 || it->current.overlay_string_index >= 0)
+               {
+                 set_iterator_to_next (it);
+                 move_it_in_display_line_to (it, -1, -1, 0);
+               }
+         
+             it->continuation_lines_width += it->current_x;
            }
-         it->continuation_lines_width += it->current_x;
+
+         /* We're starting a new display line, not affected by the
+            height of the continued line, so clear the appropriate
+            fields in the iterator structure.  */
+         it->max_ascent = it->max_descent = 0;
+         it->max_phys_ascent = it->max_phys_descent = 0;
        }
       
       it->current_y = first_y;
@@ -1587,6 +1666,7 @@ handle_stop (it)
 
   it->dpvec = NULL;
   it->current.dpvec_index = -1;
+  it->add_overlay_start = 0;
 
   do
     {
@@ -1596,7 +1676,7 @@ handle_stop (it)
       for (p = it_props; p->handler; ++p)
        {
          handled = p->handler (it);
-         
+
          if (handled == HANDLED_RECOMPUTE_PROPS)
            break;
          else if (handled == HANDLED_RETURN)
@@ -1745,12 +1825,12 @@ next_overlay_change (pos)
   /* Get all overlays at the given position.  */
   len = 10;
   overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
-  noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL);
+  noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
   if (noverlays > len)
     {
       len = noverlays;
       overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
-      noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL);
+      noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
     }
 
   /* If any of these overlays ends before endpos,
@@ -1792,16 +1872,56 @@ handle_fontified_prop (it)
   if (!STRINGP (it->string)
       && it->s == NULL
       && !NILP (Vfontification_functions)
+      && !NILP (Vrun_hooks)
       && (pos = make_number (IT_CHARPOS (*it)),
          prop = Fget_char_property (pos, Qfontified, Qnil),
          NILP (prop)))
     {
-      Lisp_Object args[2];
+      int count = specpdl_ptr - specpdl;
+      Lisp_Object val;
+
+      val = Vfontification_functions;
+      specbind (Qfontification_functions, Qnil);
+      specbind (Qafter_change_functions, Qnil);
+  
+      if (!CONSP (val) || EQ (XCAR (val), Qlambda))
+       call1 (val, pos);
+      else
+       {
+         Lisp_Object globals, fn;
+         struct gcpro gcpro1, gcpro2;
+
+         globals = Qnil;
+         GCPRO2 (val, globals);
+         
+         for (; CONSP (val); val = XCDR (val))
+           {
+             fn = XCAR (val);
+             
+             if (EQ (fn, Qt))
+               {
+                 /* A value of t indicates this hook has a local
+                    binding; it means to run the global binding too.
+                    In a global value, t should not occur.  If it
+                    does, we must ignore it to avoid an endless
+                    loop.  */
+                 for (globals = Fdefault_value (Qfontification_functions);
+                      CONSP (globals);
+                      globals = XCDR (globals))
+                   {
+                     fn = XCAR (globals);
+                     if (!EQ (fn, Qt))
+                       call1 (fn, pos);
+                   }
+               }
+             else
+               call1 (fn, pos);
+           }
 
-      /* Run the hook functions.  */
-      args[0] = Qfontification_functions;
-      args[1] = pos;
-      Frun_hook_with_args (make_number (2), args);
+         UNGCPRO;
+       }
+
+      unbind_to (count, Qnil);
 
       /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
         something.  This avoids an endless loop if they failed to
@@ -1908,7 +2028,6 @@ handle_face_prop (it)
     }
   
   it->face_id = new_face_id;
-  it->charset = CHARSET_ASCII;
   return HANDLED_NORMALLY;
 }
 
@@ -1941,7 +2060,11 @@ face_before_or_after_it_pos (it, before_p)
       if (before_p)
        pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
       else
-       pos = string_pos (IT_STRING_CHARPOS (*it) + 1, it->string);
+       /* For composition, we must check the character after the
+           composition.  */
+       pos = (it->what == IT_COMPOSITION
+              ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
+              : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
 
       /* Get the face for ASCII, or unibyte.  */
       face_id
@@ -1963,12 +2086,11 @@ face_before_or_after_it_pos (it, before_p)
        {
          unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
          int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
-         int c, len, charset;
+         int c, len;
+         struct face *face = FACE_FROM_ID (it->f, face_id);
       
          c = string_char_and_length (p, rest, &len);
-         charset = CHAR_CHARSET (c);
-         if (charset != CHARSET_ASCII)
-           face_id = FACE_FOR_CHARSET (it->f, face_id, charset);
+         face_id = FACE_FOR_CHAR (it->f, face, c);
        }
     }
   else
@@ -1981,10 +2103,16 @@ face_before_or_after_it_pos (it, before_p)
       pos = it->current.pos;
       
       if (before_p)
-       DEC_TEXT_POS (pos);
+       DEC_TEXT_POS (pos, it->multibyte_p);
       else
-       INC_TEXT_POS (pos);
-
+       {
+         if (it->what == IT_COMPOSITION)
+           /* For composition, we must check the position after the
+              composition.  */
+           pos.charpos += it->cmp_len, pos.bytepos += it->len;
+         else
+           INC_TEXT_POS (pos, it->multibyte_p);
+       }
       /* Determine face for CHARSET_ASCII, or unibyte.  */
       face_id = face_at_buffer_position (it->w,
                                         CHARPOS (pos),
@@ -1998,9 +2126,9 @@ face_before_or_after_it_pos (it, before_p)
         suitable for unibyte text if current_buffer is unibyte.  */
       if (it->multibyte_p)
        {
-         int charset = charset_at_position (pos);
-         if (charset != CHARSET_ASCII)
-           face_id = FACE_FOR_CHARSET (it->f, face_id, charset);
+         int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
+         struct face *face = FACE_FROM_ID (it->f, face_id);
+         face_id = FACE_FOR_CHAR (it->f, face, c);
        }
     }
   
@@ -2033,7 +2161,8 @@ handle_invisible_prop (it)
       XSETFASTINT (charpos, IT_STRING_CHARPOS (*it));
       prop = Fget_text_property (charpos, Qinvisible, it->string);
 
-      if (!NILP (prop))
+      if (!NILP (prop)
+         && IT_STRING_CHARPOS (*it) < it->end_charpos)
        {
          handled = HANDLED_RECOMPUTE_PROPS;
          
@@ -2087,7 +2216,8 @@ handle_invisible_prop (it)
       prop = Fget_char_property (pos, Qinvisible, it->window);
       
       /* If we are on invisible text, skip over it.  */
-      if (TEXT_PROP_MEANS_INVISIBLE (prop))
+      if (TEXT_PROP_MEANS_INVISIBLE (prop)
+         && IT_CHARPOS (*it) < it->end_charpos)
        {
          /* Record whether we have to display an ellipsis for the
             invisible text.  */
@@ -2095,6 +2225,7 @@ handle_invisible_prop (it)
            = TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop);
 
          handled = HANDLED_RECOMPUTE_PROPS;
+         it->add_overlay_start = IT_CHARPOS (*it);
          
          /* Loop skipping over invisible text.  The loop is left at
             ZV or with IT on the first char being visible again.  */
@@ -2255,8 +2386,8 @@ display_prop_end (it, object, start_pos)
   Lisp_Object end;
   struct text_pos end_pos;
 
-  end = next_single_char_property_change (make_number (CHARPOS (start_pos)),
-                                         Qdisplay, object, Qnil);
+  end = Fnext_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);
@@ -2285,7 +2416,6 @@ handle_single_display_prop (it, prop, object, position)
 {
   Lisp_Object value;
   int space_or_image_found_p = 0;
-
   Lisp_Object form;
 
   /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
@@ -2305,16 +2435,22 @@ handle_single_display_prop (it, prop, object, position)
       struct gcpro gcpro1;
       struct text_pos end_pos, pt;
       
-      end_pos = display_prop_end (it, object, *position);
       GCPRO1 (form);
+      end_pos = display_prop_end (it, object, *position);
 
       /* Temporarily set point to the end position, and then evaluate
         the form.  This makes `(eolp)' work as FORM.  */
-      CHARPOS (pt) = PT;
-      BYTEPOS (pt) = PT_BYTE;
-      TEMP_SET_PT_BOTH (CHARPOS (end_pos), BYTEPOS (end_pos));
+      if (BUFFERP (object))
+       {
+         CHARPOS (pt) = PT;
+         BYTEPOS (pt) = PT_BYTE;
+         TEMP_SET_PT_BOTH (CHARPOS (end_pos), BYTEPOS (end_pos));
+       }
+      
       form = eval_form (form);
-      TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
+      
+      if (BUFFERP (object))
+       TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
       UNGCPRO;  
     }
   
@@ -2347,20 +2483,18 @@ handle_single_display_prop (it, prop, object, position)
                steps = - steps;
              it->face_id = smaller_face (it->f, it->face_id, steps);
            }
-         else if (SYMBOLP (it->font_height))
+         else if (FUNCTIONP (it->font_height))
            {
              /* Call function with current height as argument.
                 Value is the new height.  */
-             Lisp_Object form, height;
-             struct gcpro gcpro1;
+             Lisp_Object args[2], height;
+             
+             args[0] = it->font_height;
+             args[1] = face->lface[LFACE_HEIGHT_INDEX];
+             height = call_function (2, args);
              
-             height = face->lface[LFACE_HEIGHT_INDEX];
-             form = Fcons (it->font_height, Fcons (height, Qnil));
-             GCPRO1 (form);
-             height = eval_form (form);
              if (NUMBERP (height))
                new_height = XFLOATINT (height);
-             UNGCPRO;
            }
          else if (NUMBERP (it->font_height))
            {
@@ -2406,17 +2540,17 @@ handle_single_display_prop (it, prop, object, position)
           && EQ (XCAR (prop), Qraise)
           && CONSP (XCDR (prop)))
     {
-#ifdef HAVE_WINDOW_SYSTEM
       /* `(raise FACTOR)'.  */
       if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
        return 0;
       
+#ifdef HAVE_WINDOW_SYSTEM
       value = XCAR (XCDR (prop));
       if (NUMBERP (value))
        {
          struct face *face = FACE_FROM_ID (it->f, it->face_id);
          it->voffset = - (XFLOATINT (value)
-                          * (face->font->ascent + face->font->descent));
+                          * (FONT_HEIGHT (face->font)));
        }
 #endif /* HAVE_WINDOW_SYSTEM */
     }
@@ -2430,9 +2564,9 @@ handle_single_display_prop (it, prop, object, position)
 
       /* Characters having this form of property are not displayed, so
          we have to find the end of the property.  */
-      space_or_image_found_p = 1;
       start_pos = *position;
       *position = display_prop_end (it, object, start_pos);
+      value = Qnil;
 
       /* Let's stop at the new position and assume that all
         text properties change there.  */
@@ -2464,7 +2598,7 @@ handle_single_display_prop (it, prop, object, position)
        }
 
 #ifdef HAVE_WINDOW_SYSTEM
-      if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
+      if (FRAME_TERMCAP_P (it->f))
        valid_p = STRINGP (value);
       else
        valid_p = (STRINGP (value)
@@ -2479,6 +2613,8 @@ handle_single_display_prop (it, prop, object, position)
           || NILP (location))
          && valid_p)
        {
+         space_or_image_found_p = 1;
+         
          /* Save current settings of IT so that we can restore them
             when we are finished with the glyph property value.  */
          push_it (it);
@@ -2517,19 +2653,152 @@ handle_single_display_prop (it, prop, object, position)
              it->object = NILP (object) ? it->w->buffer : object;
              it->method = next_element_from_image;
              
-             /* Say that we don't have consumed the characters with
+             /* Say that we haven't consumed the characters with
                 `display' property yet.  The call to pop_it in
                 set_iterator_to_next will clean this up.  */
              *position = start_pos;
            }
 #endif /* HAVE_WINDOW_SYSTEM */
        }
+      else
+       /* Invalid property or property not supported.  Restore
+          the position to what it was before.  */
+       *position = start_pos;
     }
 
   return space_or_image_found_p;
 }
 
 
+/* Check if PROP is a display sub-property value whose text should be
+   treated as intangible.  */
+
+static int
+single_display_prop_intangible_p (prop)
+     Lisp_Object prop;
+{
+  /* Skip over `when FORM'.  */
+  if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
+    {
+      prop = XCDR (prop);
+      if (!CONSP (prop))
+       return 0;
+      prop = XCDR (prop);
+    }
+
+  if (!CONSP (prop))
+    return 0;
+
+  /* Skip over `margin LOCATION'.  If LOCATION is in the margins,
+     we don't need to treat text as intangible.  */
+  if (EQ (XCAR (prop), Qmargin))
+    {
+      prop = XCDR (prop);
+      if (!CONSP (prop))
+       return 0;
+
+      prop = XCDR (prop);
+      if (!CONSP (prop)
+         || EQ (XCAR (prop), Qleft_margin)
+         || EQ (XCAR (prop), Qright_margin))
+       return 0;
+    }
+  
+  return CONSP (prop) && EQ (XCAR (prop), Qimage);
+}
+
+
+/* Check if PROP is a display property value whose text should be
+   treated as intangible.  */
+
+int
+display_prop_intangible_p (prop)
+     Lisp_Object prop;
+{
+  if (CONSP (prop)
+      && CONSP (XCAR (prop))
+      && !EQ (Qmargin, XCAR (XCAR (prop))))
+    {
+      /* A list of sub-properties.  */
+      while (CONSP (prop))
+       {
+         if (single_display_prop_intangible_p (XCAR (prop)))
+           return 1;
+         prop = XCDR (prop);
+       }
+    }
+  else if (VECTORP (prop))
+    {
+      /* A vector of sub-properties.  */
+      int i;
+      for (i = 0; i < XVECTOR (prop)->size; ++i)
+       if (single_display_prop_intangible_p (XVECTOR (prop)->contents[i]))
+         return 1;
+    }
+  else
+    return single_display_prop_intangible_p (prop);
+
+  return 0;
+}
+
+\f
+/***********************************************************************
+                       `composition' property
+ ***********************************************************************/
+
+/* Set up iterator IT from `composition' property at its current
+   position.  Called from handle_stop.  */
+
+static enum prop_handled
+handle_composition_prop (it)
+     struct it *it;
+{
+  Lisp_Object prop, string;
+  int pos, pos_byte, end;
+  enum prop_handled handled = HANDLED_NORMALLY;
+
+  if (STRINGP (it->string))
+    {
+      pos = IT_STRING_CHARPOS (*it);
+      pos_byte = IT_STRING_BYTEPOS (*it);
+      string = it->string;
+    }
+  else
+    {
+      pos = IT_CHARPOS (*it);
+      pos_byte = IT_BYTEPOS (*it);
+      string = Qnil;
+    }
+
+  /* If there's a valid composition and point is not inside of the
+     composition (in the case that the composition is from the current
+     buffer), draw a glyph composed from the composition components.  */
+  if (find_composition (pos, -1, &pos, &end, &prop, string)
+      && COMPOSITION_VALID_P (pos, end, prop)
+      && (STRINGP (it->string) || (PT <= pos || PT >= end)))
+    {
+      int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
+
+      if (id >= 0)
+       {
+         it->method = next_element_from_composition;
+         it->cmp_id = id;
+         it->cmp_len = COMPOSITION_LENGTH (prop);
+         /* For a terminal, draw only the first character of the
+             components.  */
+         it->c = COMPOSITION_GLYPH (composition_table[id], 0);
+         it->len = (STRINGP (it->string)
+                    ? string_char_to_byte (it->string, end)
+                    : CHAR_TO_BYTE (end)) - pos_byte;
+         it->stop_charpos = end;
+         handled = HANDLED_RETURN;
+       }
+    }
+
+  return handled;
+}
+
+
 \f
 /***********************************************************************
                           Overlay strings
@@ -2540,6 +2809,7 @@ handle_single_display_prop (it, prop, object, position)
 
 struct overlay_entry
 {
+  Lisp_Object overlay;
   Lisp_Object string;
   int priority;
   int after_string_p;
@@ -2553,13 +2823,10 @@ static enum prop_handled
 handle_overlay_change (it)
      struct it *it;
 {
-  /* Overlays are handled in current_buffer only.  */
-  if (STRINGP (it->string))
-    return HANDLED_NORMALLY;
+  if (!STRINGP (it->string) && get_overlay_strings (it))
+    return HANDLED_RECOMPUTE_PROPS;
   else
-    return (get_overlay_strings (it)
-           ? HANDLED_RECOMPUTE_PROPS
-           : HANDLED_NORMALLY);
+    return HANDLED_NORMALLY;
 }
 
 
@@ -2588,6 +2855,12 @@ next_overlay_string (it)
       SET_TEXT_POS (it->current.string_pos, -1, -1);
       it->n_overlay_strings = 0;
       it->method = next_element_from_buffer;
+
+      /* If we're at the end of the buffer, record that we have
+        processed the overlay strings there already, so that
+        next_element_from_buffer doesn't try it again.  */
+      if (IT_CHARPOS (*it) >= it->end_charpos)
+       it->overlay_strings_at_end_processed_p = 1;
     }
   else
     {
@@ -2617,7 +2890,8 @@ next_overlay_string (it)
    comparison function for qsort in load_overlay_strings.  Overlay
    strings for the same position are sorted so that
 
-   1. All after-strings come in front of before-strings.
+   1. All after-strings come in front of before-strings, except
+   when they come from the same overlay.
    
    2. Within after-strings, strings are sorted so that overlay strings
    from overlays with higher priorities come first.
@@ -2637,8 +2911,14 @@ compare_overlay_entries (e1, e2)
   int result;
 
   if (entry1->after_string_p != entry2->after_string_p)
-    /* Let after-strings appear in front of before-strings.  */
-    result = entry1->after_string_p ? -1 : 1;
+    {
+      /* Let after-strings appear in front of before-strings if
+        they come from different overlays.  */
+      if (EQ (entry1->overlay, entry2->overlay))
+       result = entry1->after_string_p ? 1 : -1;
+      else
+       result = entry1->after_string_p ? -1 : 1;
+    }
   else if (entry1->after_string_p)
     /* After-strings sorted in order of decreasing priority.  */
     result = entry2->priority - entry1->priority;
@@ -2660,6 +2940,15 @@ compare_overlay_entries (e1, e2)
    strings that have already been loaded by previous calls to this
    function.
 
+   IT->add_overlay_start contains an additional overlay start
+   position to consider for taking overlay strings from, if non-zero.
+   This position comes into play when the overlay has an `invisible'
+   property, and both before and after-strings.  When we've skipped to
+   the end of the overlay, because of its `invisible' property, we
+   nevertheless want its before-string to appear.
+   IT->add_overlay_start will contain the overlay start position
+   in this case.
+
    Overlay strings are sorted so that after-string strings come in
    front of before-string strings.  Within before and after-strings,
    strings are sorted by overlay priority.  See also function
@@ -2698,18 +2987,16 @@ load_overlay_strings (it)
        }                                                               \
                                                                        \
       entries[n].string = (STRING);                                    \
+      entries[n].overlay = (OVERLAY);                                  \
       priority = Foverlay_get ((OVERLAY), Qpriority);                  \
-      entries[n].priority                                              \
-       = INTEGERP (priority) ? XFASTINT (priority) : 0;                \
+      entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0;  \
       entries[n].after_string_p = (AFTER_P);                           \
       ++n;                                                             \
     }                                                                  \
   while (0)
 
   /* Process overlay before the overlay center.  */
-  for (ov = current_buffer->overlays_before;
-       CONSP (ov);
-       ov = XCDR (ov))
+  for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
     {
       overlay = XCAR (ov);
       xassert (OVERLAYP (overlay));
@@ -2721,7 +3008,9 @@ load_overlay_strings (it)
 
       /* Skip this overlay if it doesn't start or end at IT's current
         position.  */
-      if (end != IT_CHARPOS (*it) && start != IT_CHARPOS (*it))
+      if (end != IT_CHARPOS (*it)
+         && start != IT_CHARPOS (*it)
+         && it->add_overlay_start != IT_CHARPOS (*it))
        continue;
       
       /* Skip this overlay if it doesn't apply to IT->w.  */
@@ -2730,7 +3019,8 @@ load_overlay_strings (it)
        continue;
 
       /* If overlay has a non-empty before-string, record it.  */
-      if (start == IT_CHARPOS (*it)
+      if ((start == IT_CHARPOS (*it)
+          || start == it->add_overlay_start)
          && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
          && XSTRING (str)->size)
        RECORD_OVERLAY_STRING (overlay, str, 0);
@@ -2743,9 +3033,7 @@ load_overlay_strings (it)
     }
       
   /* Process overlays after the overlay center.  */
-  for (ov = current_buffer->overlays_after;
-       CONSP (ov);
-       ov = XCDR (ov))
+  for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
     {
       overlay = XCAR (ov);
       xassert (OVERLAYP (overlay));
@@ -2757,7 +3045,9 @@ load_overlay_strings (it)
       
       /* Skip this overlay if it doesn't start or end at IT's current
         position.  */
-      if (end != IT_CHARPOS (*it) && start != IT_CHARPOS (*it))
+      if (end != IT_CHARPOS (*it)
+         && start != IT_CHARPOS (*it)
+         && it->add_overlay_start != IT_CHARPOS (*it))
        continue;
 
       /* Skip this overlay if it doesn't apply to IT->w.  */
@@ -2766,7 +3056,8 @@ load_overlay_strings (it)
        continue;
       
       /* If overlay has a non-empty before-string, record it.  */
-      if (start == IT_CHARPOS (*it)
+      if ((start == IT_CHARPOS (*it)
+          || start == it->add_overlay_start)
          && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
          && XSTRING (str)->size)
        RECORD_OVERLAY_STRING (overlay, str, 0);
@@ -2781,7 +3072,8 @@ load_overlay_strings (it)
 #undef RECORD_OVERLAY_STRING
    
   /* Sort entries.  */
-  qsort (entries, n, sizeof *entries, compare_overlay_entries);
+  if (n)
+    qsort (entries, n, sizeof *entries, compare_overlay_entries);
 
   /* Record the total number of strings to process.  */
   it->n_overlay_strings = n;
@@ -2793,7 +3085,7 @@ load_overlay_strings (it)
   j = it->current.overlay_string_index;
   while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
     it->overlay_strings[i++] = entries[j++].string;
-  
+
   CHECK_IT (it);
 }
 
@@ -2976,16 +3268,15 @@ back_to_previous_visible_line_start (it)
          && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
                                it->selective))
        visible_p = 0;
-#ifdef USE_TEXT_PROPERTIES
       else 
        {
          Lisp_Object prop;
 
-         prop = Fget_char_property (IT_CHARPOS (*it), Qinvisible, it->window);
+         prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
+                                    Qinvisible, it->window);
          if (TEXT_PROP_MEANS_INVISIBLE (prop))
            visible_p = 0;
        }
-#endif /* USE_TEXT_PROPERTIES  */
 
       /* Back one more newline if the current one is invisible.  */
       if (!visible_p)
@@ -3061,7 +3352,9 @@ reseat_at_next_visible_line_start (it, on_newline_p)
          forward_to_next_line_start (it);
 
       /* Position on the newline if we should.  */
-      if (on_newline_p && IT_CHARPOS (*it) > BEGV)
+      if (on_newline_p
+         && IT_CHARPOS (*it) > BEGV
+         && FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n')
        {
          --IT_CHARPOS (*it);
          IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
@@ -3177,7 +3470,6 @@ reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
   bzero (&it->current, sizeof it->current);
   it->current.overlay_string_index = -1;
   it->current.dpvec_index = -1;
-  it->charset = CHARSET_ASCII;
   xassert (charpos >= 0);
   
   /* Use the setting of MULTIBYTE if specified.  */
@@ -3256,7 +3548,6 @@ get_next_display_element (it)
      function pointer `method' used here turns out to be faster than
      using a sequence of if-statements.  */
   int success_p = (*it->method) (it);
-  int charset;
 
   if (it->what == IT_CHARACTER)
     {
@@ -3340,8 +3631,8 @@ get_next_display_element (it)
                }
              else
                {
-                 unsigned char work[4], *str;
-                 int len = CHAR_STRING (it->c, work, str);
+                 unsigned char str[MAX_MULTIBYTE_LENGTH];
+                 int len;
                  int i;
                  GLYPH escape_glyph;
 
@@ -3353,6 +3644,11 @@ get_next_display_element (it)
                  else
                    escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
 
+                 if (SINGLE_BYTE_CHAR_P (it->c))
+                   str[0] = it->c, len = 1;
+                 else
+                   len = CHAR_STRING (it->c, str);
+
                  for (i = 0; i < len; i++)
                    {
                      XSETINT (it->ctl_chars[i * 4], escape_glyph);
@@ -3378,16 +3674,14 @@ get_next_display_element (it)
            }
        }
 
-      /* Adjust face id if charset changes.  There are no charset
-         changes in unibyte text because Emacs' charsets are not
-        applicable there.  */
+      /* Adjust face id for a multibyte character.  There are no
+         multibyte character in unibyte text.  */
       if (it->multibyte_p
          && success_p
-         && (charset = CHAR_CHARSET (it->c),
-             charset != it->charset))
+         && FRAME_WINDOW_P (it->f))
        {
-         it->charset = charset;
-         it->face_id = FACE_FOR_CHARSET (it->f, it->face_id, charset);
+         struct face *face = FACE_FROM_ID (it->f, it->face_id);
+         it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
        }
     }
 
@@ -3442,6 +3736,23 @@ set_iterator_to_next (it)
          xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
        }
     }
+  else if (it->method == next_element_from_composition)
+    {
+      xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
+      if (STRINGP (it->string))
+       {
+         IT_STRING_BYTEPOS (*it) += it->len;
+         IT_STRING_CHARPOS (*it) += it->cmp_len;
+         it->method = next_element_from_string;
+         goto consider_string_end;
+       }
+      else
+       {
+         IT_BYTEPOS (*it) += it->len;
+         IT_CHARPOS (*it) += it->cmp_len;
+         it->method = next_element_from_buffer;
+       }
+    }
   else if (it->method == next_element_from_c_string)
     {
       /* Current display element of IT is from a C string.  */
@@ -3456,11 +3767,9 @@ set_iterator_to_next (it)
         strings.  */
       ++it->current.dpvec_index;
 
-      /* Restore face and charset of the iterator to what they were
-        before the display vector entry (these entries may contain
-        faces, and of course characters of different charsets).  */
+      /* Restore face of the iterator to what they were before the
+         display vector entry (these entries may contain faces).  */
       it->face_id = it->saved_face_id;
-      it->charset = FACE_FROM_ID (it->f, it->face_id)->charset;
       
       if (it->dpvec + it->current.dpvec_index == it->dpend)
        {
@@ -3569,20 +3878,18 @@ next_element_from_display_vector (it)
 
       g = XFASTINT (it->dpvec[it->current.dpvec_index]);
       it->c = FAST_GLYPH_CHAR (g);
-      it->len = CHAR_LEN (it->c);
+      it->len = CHAR_BYTES (it->c);
 
       /* The entry may contain a face id to use.  Such a face id is
         the id of a Lisp face, not a realized face.  A face id of
-        zero means no face.  */
+        zero means no face is specified.  */
       lface_id = FAST_GLYPH_FACE (g);
       if (lface_id)
        {
+         /* The function returns -1 if lface_id is invalid.  */
          int face_id = ascii_face_of_lisp_face (it->f, lface_id);
          if (face_id >= 0)
-           {
-             it->face_id = face_id;
-             it->charset = CHARSET_ASCII;
-           }
+           it->face_id = face_id;
        }
     }
   else
@@ -3743,32 +4050,37 @@ next_element_from_c_string (it)
    entry.  This function Fills IT with the first glyph from the
    ellipsis if an ellipsis is to be displayed.  */
 
-static void
+static int
 next_element_from_ellipsis (it)
      struct it *it;
 {
-  if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
-    {
-      /* Use the display table definition for `...'.  Invalid glyphs
-        will be handled by the method returning elements from dpvec.  */
-      struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
-      it->dpvec_char_len = it->len;
-      it->dpvec = v->contents;
-      it->dpend = v->contents + v->size;
-      it->current.dpvec_index = 0;
-      it->method = next_element_from_display_vector;
-      get_next_display_element (it);
-    }
-  else if (it->selective_display_ellipsis_p)
+  if (it->selective_display_ellipsis_p)
     {
-      /* Use default `...' which is stored in default_invis_vector.  */
-      it->dpvec_char_len = it->len;
-      it->dpvec = default_invis_vector;
-      it->dpend = default_invis_vector + 3;
-      it->current.dpvec_index = 0;
-      it->method = next_element_from_display_vector;
-      get_next_display_element (it);
+      if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
+       {
+         /* Use the display table definition for `...'.  Invalid glyphs
+            will be handled by the method returning elements from dpvec.  */
+         struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
+         it->dpvec_char_len = it->len;
+         it->dpvec = v->contents;
+         it->dpend = v->contents + v->size;
+         it->current.dpvec_index = 0;
+         it->method = next_element_from_display_vector;
+       }
+      else
+       {
+         /* Use default `...' which is stored in default_invis_vector.  */
+         it->dpvec_char_len = it->len;
+         it->dpvec = default_invis_vector;
+         it->dpend = default_invis_vector + 3;
+         it->current.dpvec_index = 0;
+         it->method = next_element_from_display_vector;
+       }
     }
+  else
+    reseat_at_next_visible_line_start (it, 1);
+  
+  return get_next_display_element (it);
 }
 
 
@@ -3828,8 +4140,7 @@ next_element_from_buffer (it)
          else
            {
              it->overlay_strings_at_end_processed_p = 1;
-             overlay_strings_follow_p
-               = get_overlay_strings (it);
+             overlay_strings_follow_p = get_overlay_strings (it);
            }
 
          if (overlay_strings_follow_p)
@@ -3862,7 +4173,7 @@ next_element_from_buffer (it)
 
       /* Get the next character, maybe multibyte.  */
       p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
-      if (it->multibyte_p)
+      if (it->multibyte_p && !ASCII_BYTE_P (*p))
        {
          int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
                        - IT_BYTEPOS (*it));
@@ -3890,7 +4201,7 @@ next_element_from_buffer (it)
                                        IT_BYTEPOS (*it) + 1,
                                        it->selective))
                {
-                 next_element_from_ellipsis (it);
+                 success_p = next_element_from_ellipsis (it);
                  it->dpvec_char_len = -1;
                }
            }
@@ -3899,14 +4210,14 @@ next_element_from_buffer (it)
              /* A value of selective == -1 means that everything from the
                 CR to the end of the line is invisible, with maybe an
                 ellipsis displayed for it.  */
-             next_element_from_ellipsis (it);
+             success_p = next_element_from_ellipsis (it);
              it->dpvec_char_len = -1;
            }
        }
     }
 
   /* Value is zero if end of buffer reached.  */
-  xassert (!success_p || it->len > 0);
+  xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
   return success_p;
 }
 
@@ -3939,6 +4250,22 @@ run_redisplay_end_trigger_hook (it)
 }
 
 
+/* Deliver a composition display element.  The iterator IT is already
+   filled with composition information (done in
+   handle_composition_prop).  Value is always 1.  */
+
+static int
+next_element_from_composition (it)
+     struct it *it;
+{
+  it->what = IT_COMPOSITION;
+  it->position = (STRINGP (it->string)
+                 ? it->current.string_pos
+                 : it->current.pos);
+  return 1;
+}
+
+
 \f
 /***********************************************************************
             Moving an iterator without producing glyphs
@@ -3991,7 +4318,7 @@ move_it_in_display_line_to (it, to_charpos, to_x, op)
 
   while (1)
     {
-      int x, i;
+      int x, i, ascent = 0, descent = 0;
       
       /* Stop when ZV or TO_CHARPOS reached.  */
       if (!get_next_display_element (it)
@@ -4008,6 +4335,15 @@ move_it_in_display_line_to (it, to_charpos, to_x, op)
         x-position before this display element in case it does not
         fit on the line.  */
       x = it->current_x;
+      
+      /* Remember the line height so far in case the next element doesn't
+        fit on the line.  */
+      if (!it->truncate_lines_p)
+       {
+         ascent = it->max_ascent;
+         descent = it->max_descent;
+       }
+      
       PRODUCE_GLYPHS (it);
 
       if (it->area != TEXT_AREA)
@@ -4073,8 +4409,14 @@ move_it_in_display_line_to (it, to_charpos, to_x, op)
                        set_iterator_to_next (it);
                    }
                  else
-                   it->current_x = x;
-
+                   {
+                     it->current_x = x;
+                     it->max_ascent = ascent;
+                     it->max_descent = descent;
+                   }
+                 
+                 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
+                              IT_CHARPOS (*it)));
                  result = MOVE_LINE_CONTINUED;
                  break;
                }
@@ -4149,8 +4491,9 @@ 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;
+  int reached = 0;
 
-  while (1)
+  for (;;)
     {
       if (op & MOVE_TO_VPOS)
        {
@@ -4159,31 +4502,46 @@ move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
          if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
            {
              if (it->vpos == to_vpos)
-               break;
-             skip = move_it_in_display_line_to (it, -1, -1, 0);
+               {
+                 reached = 1;
+                 break;
+               }
+             else
+               skip = move_it_in_display_line_to (it, -1, -1, 0);
            }
          else
            {
              /* TO_VPOS >= 0 means stop at TO_X in the line at
                 TO_VPOS, or at TO_POS, whichever comes first.  */
+             if (it->vpos == to_vpos)
+               {
+                 reached = 2;
+                 break;
+               }
+             
              skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
 
              if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
-               break;
+               {
+                 reached = 3;
+                 break;
+               }
              else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
                {
                  /* We have reached TO_X but not in the line we want.  */
                  skip = move_it_in_display_line_to (it, to_charpos,
                                                     -1, MOVE_TO_POS);
                  if (skip == MOVE_POS_MATCH_OR_ZV)
-                   break;
+                   {
+                     reached = 4;
+                     break;
+                   }
                }
            }
        }
       else if (op & MOVE_TO_Y)
        {
          struct it it_backup;
-         int done_p;
          
          /* TO_Y specified means stop at TO_X in the line containing
             TO_Y---or at TO_CHARPOS if this is reached first.  The
@@ -4205,22 +4563,27 @@ move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
 
          /* If TO_CHARPOS is reached or ZV, we don't have to do more.  */
          if (skip == MOVE_POS_MATCH_OR_ZV)
-           break;
+           {
+             reached = 5;
+             break;
+           }
          
          /* If TO_X was reached, we would like to know whether TO_Y
             is in the line.  This can only be said if we know the
             total line height which requires us to scan the rest of
             the line.  */
-         done_p = 0;
          if (skip == MOVE_X_REACHED)
            {
              it_backup = *it;
+             TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
              skip2 = move_it_in_display_line_to (it, to_charpos, -1,
                                                  op & MOVE_TO_POS);
+             TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
            }
 
          /* Now, decide whether TO_Y is in this line.  */
          line_height = it->max_ascent + it->max_descent;
+         TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
          
          if (to_y >= it->current_y
              && to_y < it->current_y + line_height)
@@ -4230,16 +4593,16 @@ move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
                   we scanned too far.  We have to restore IT's settings
                   to the ones before skipping.  */
                *it = it_backup;
-             done_p = 1;
+             reached = 6;
            }
          else if (skip == MOVE_X_REACHED)
            {
              skip = skip2;
              if (skip == MOVE_POS_MATCH_OR_ZV)
-               done_p = 1;
+               reached = 7;
            }
 
-         if (done_p)
+         if (reached)
            break;
        }
       else
@@ -4248,7 +4611,8 @@ move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
       switch (skip)
        {
        case MOVE_POS_MATCH_OR_ZV:
-         return;
+         reached = 8;
+         goto out;
 
        case MOVE_NEWLINE_OR_CR:
          set_iterator_to_next (it);
@@ -4260,7 +4624,10 @@ move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
          reseat_at_next_visible_line_start (it, 0);
          if ((op & MOVE_TO_POS) != 0
              && IT_CHARPOS (*it) > to_charpos)
-           goto out;
+           {
+             reached = 9;
+             goto out;
+           }
          break;
 
        case MOVE_LINE_CONTINUED:
@@ -4280,7 +4647,10 @@ move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
       last_max_ascent = it->max_ascent;
       it->max_ascent = it->max_descent = 0;
     }
- out:;
+  
+ out:
+
+  TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
 }
 
 
@@ -4329,6 +4699,7 @@ move_it_vertically_backward (it, dy)
              MOVE_TO_POS | MOVE_TO_VPOS);
   xassert (IT_CHARPOS (*it) >= BEGV);
   line_height = it2.max_ascent + it2.max_descent;
+  
   move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
   xassert (IT_CHARPOS (*it) >= BEGV);
   h = it2.current_y - it->current_y;
@@ -4383,8 +4754,10 @@ move_it_vertically (it, dy)
     move_it_vertically_backward (it, -dy);
   else if (dy > 0)
     {
+      TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
       move_it_to (it, ZV, -1, it->current_y + dy, -1,
                  MOVE_TO_POS | MOVE_TO_Y);
+      TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
 
       /* If buffer ends in ZV without a newline, move to the start of
         the line to satisfy the post-condition.  */
@@ -4404,7 +4777,6 @@ invisible_text_between_p (it, start_charpos, end_charpos)
      struct it *it;
      int start_charpos, end_charpos;
 {
-#ifdef USE_TEXT_PROPERTIES
   Lisp_Object prop, limit;
   int invisible_found_p;
   
@@ -4417,17 +4789,13 @@ invisible_text_between_p (it, start_charpos, end_charpos)
     invisible_found_p = 1;
   else
     {
-      limit = next_single_char_property_change (make_number (start_charpos),
-                                               Qinvisible, Qnil,
-                                               make_number (end_charpos));
+      limit = Fnext_single_char_property_change (make_number (start_charpos),
+                                                Qinvisible, Qnil,
+                                                make_number (end_charpos));
       invisible_found_p = XFASTINT (limit) < end_charpos;
     }
 
   return invisible_found_p;
-  
-#else /* not USE_TEXT_PROPERTIES */
-  return 0;
-#endif /* not USE_TEXT_PROPERTIES */
 }
 
 
@@ -4591,13 +4959,13 @@ add_to_log (format, arg1, arg2)
   args[0] = fmt = build_string (format);
   args[1] = arg1;
   args[2] = arg2;
-  msg = Fformat (make_number (3), args);
+  msg = Fformat (3, args);
 
   len = STRING_BYTES (XSTRING (msg)) + 1;
   buffer = (char *) alloca (len);
   strcpy (buffer, XSTRING (msg)->data);
   
-  message_dolog (buffer, len, 1, 0);
+  message_dolog (buffer, len - 1, 1, 0);
   UNGCPRO;
 }
 
@@ -4635,7 +5003,7 @@ message_dolog (m, len, nlflag, multibyte)
 
       old_deactivate_mark = Vdeactivate_mark;
       oldbuf = current_buffer;
-      Fset_buffer (Fget_buffer_create (build_string ("*Messages*")));
+      Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
       current_buffer->undo_list = Qt;
 
       oldpoint = Fpoint_marker ();
@@ -4678,14 +5046,14 @@ message_dolog (m, len, nlflag, multibyte)
        {
          int i, c, nbytes;
          unsigned char *msg = (unsigned char *) m;
-         unsigned char *str, work[4];
+         unsigned char str[MAX_MULTIBYTE_LENGTH];
          /* Convert a single-byte string to multibyte
             for the *Message* buffer.  */
          for (i = 0; i < len; i++)
            {
              c = unibyte_char_to_multibyte (msg[i]);
-             nbytes = CHAR_STRING (c, work, str);
-             insert_1_both (work, 1, nbytes, 1, 0, 0);
+             nbytes = CHAR_STRING (c, str);
+             insert_1_both (str, 1, nbytes, 1, 0, 0);
            }
        }
       else if (len)
@@ -5169,7 +5537,35 @@ update_echo_area ()
 }
 
 
-/* Call FN with args A1..A5 with either the current or last displayed
+/* Make sure echo area buffers in echo_buffers[] are life.  If they
+   aren't, make new ones.  */
+
+static void
+ensure_echo_area_buffers ()
+{
+  int i;
+
+  for (i = 0; i < 2; ++i)
+    if (!BUFFERP (echo_buffer[i])
+       || NILP (XBUFFER (echo_buffer[i])->name))
+      {
+       char name[30];
+       Lisp_Object old_buffer;
+       int j;
+
+       old_buffer = echo_buffer[i];
+       sprintf (name, " *Echo Area %d*", i);
+       echo_buffer[i] = Fget_buffer_create (build_string (name));
+       XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
+
+       for (j = 0; j < 2; ++j)
+         if (EQ (old_buffer, echo_area_buffer[j]))
+           echo_area_buffer[j] = echo_buffer[i];
+      }
+}
+
+
+/* Call FN with args A1..A4 with either the current or last displayed
    echo_area_buffer as current buffer.
 
    WHICH zero means use the current message buffer
@@ -5186,25 +5582,20 @@ update_echo_area ()
    Value is what FN returns. */
 
 static int
-with_echo_area_buffer (w, which, fn, a1, a2, a3, a4, a5)
+with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
      struct window *w;
      int which;
-     int (*fn) ();
-     int a1, a2, a3, a4, a5;
+     int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
+     EMACS_INT a1;
+     Lisp_Object a2;
+     EMACS_INT a3, a4;
 {
   Lisp_Object buffer;
-  int i, this_one, the_other, clear_buffer_p, rc;
+  int 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));
-      }
+  ensure_echo_area_buffers ();
 
   clear_buffer_p = 0;
   
@@ -5253,7 +5644,7 @@ with_echo_area_buffer (w, which, fn, a1, a2, a3, a4, a5)
       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;
 
@@ -5263,7 +5654,7 @@ with_echo_area_buffer (w, which, fn, a1, a2, a3, a4, a5)
   xassert (BEGV >= BEG);
   xassert (ZV <= Z && ZV >= BEGV);
 
-  rc = fn (a1, a2, a3, a4, a5);
+  rc = fn (a1, a2, a3, a4);
 
   xassert (BEGV >= BEG);
   xassert (ZV <= Z && ZV >= BEGV);
@@ -5356,6 +5747,8 @@ void
 setup_echo_area_for_printing (multibyte_p)
      int multibyte_p;
 {
+  ensure_echo_area_buffers ();
+
   if (!message_buf_print)
     {
       /* A message has been output since the last time we printed.
@@ -5385,11 +5778,23 @@ setup_echo_area_for_printing (multibyte_p)
          Fraise_frame  (WINDOW_FRAME (XWINDOW (mini_window)));
        }
 
+      message_log_maybe_newline ();
       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]));
+  else
+    {
+      if (NILP (echo_area_buffer[0]))
+       {
+         if (EQ (echo_area_buffer[1], echo_buffer[0]))
+           echo_area_buffer[0] = echo_buffer[1]; 
+         else
+           echo_area_buffer[0] = echo_buffer[0];
+       }
+      
+      if (current_buffer != XBUFFER (echo_area_buffer[0]))
+       /* Someone switched buffers between print requests.  */
+       set_buffer_internal (XBUFFER (echo_area_buffer[0]));
+    }
 }
 
 
@@ -5402,7 +5807,14 @@ static int
 display_echo_area (w)
      struct window *w;
 {
-  int i, no_message_p, window_height_changed_p;
+  int i, no_message_p, window_height_changed_p, count;
+
+  /* Temporarily disable garbage collections while displaying the echo
+     area.  This is done because a GC can print a message itself.
+     That message would modify the echo area buffer's contents while a
+     redisplay of the buffer is going on, and seriously confuse
+     redisplay.  */
+  count = inhibit_garbage_collection ();
 
   /* If there is no message, we must call display_echo_area_1
      nevertheless because it resizes the window.  But we will have to
@@ -5413,24 +5825,30 @@ display_echo_area (w)
   
   window_height_changed_p
     = with_echo_area_buffer (w, display_last_displayed_message_p,
-                            (int (*) ()) display_echo_area_1, w);
+                            display_echo_area_1,
+                            (EMACS_INT) w, Qnil, 0, 0);
 
   if (no_message_p)
     echo_area_buffer[i] = Qnil;
-  
+
+  unbind_to (count, 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.
+   contains the current echo area message in window W, a mini-window,
+   a pointer to which is passed in A1.  A2..A4 are currently not used.
    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;
+display_echo_area_1 (a1, a2, a3, a4)
+     EMACS_INT a1;
+     Lisp_Object a2;
+     EMACS_INT a3, a4;
 {
+  struct window *w = (struct window *) a1;
   Lisp_Object window;
   struct text_pos start;
   int window_height_changed_p = 0;
@@ -5461,9 +5879,8 @@ resize_echo_area_axactly ()
       struct window *w = XWINDOW (echo_area_window);
       int resized_p;
       
-      resized_p = with_echo_area_buffer (w, 0,
-                                        (int (*) ()) resize_mini_window,
-                                        w, 1);
+      resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
+                                        (EMACS_INT) w, Qnil, 0, 0);
       if (resized_p)
        {
          ++windows_or_buffers_changed;
@@ -5474,6 +5891,21 @@ resize_echo_area_axactly ()
 }
 
 
+/* Callback function for with_echo_area_buffer, when used from
+   resize_echo_area_axactly.  A1 contains a pointer to the window to
+   resize, A2 to A4 are not used.  Value is what resize_mini_window
+   returns.  */
+
+static int
+resize_mini_window_1 (a1, a2, a3, a4)
+     EMACS_INT a1;
+     Lisp_Object a2;
+     EMACS_INT a3, a4;
+{
+  return resize_mini_window ((struct window *) a1, 1);
+}
+
+
 /* 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
@@ -5490,7 +5922,8 @@ resize_mini_window (w, exact_p)
   xassert (MINI_WINDOW_P (w));
 
   /* Nil means don't try to resize.  */
-  if (NILP (Vmax_mini_window_height))
+  if (NILP (Vmax_mini_window_height)
+      || (FRAME_X_P (f) && f->output_data.x == NULL))
     return 0;
   
   if (!FRAME_MINIBUF_ONLY_P (f))
@@ -5517,9 +5950,19 @@ resize_mini_window (w, exact_p)
       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);
+      if (it.truncate_lines_p)
+       height = 1;
+      else
+       {
+         last_height = 0;
+         move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
+         if (it.max_ascent == 0 && it.max_descent == 0)
+           height = it.current_y + last_height;
+         else
+           height = it.current_y + it.max_ascent + it.max_descent;
+         height -= it.extra_line_spacing;
+         height = (height + unit - 1) / unit;
+       }
       
       /* Compute a suitable window start.  */
       if (height > max_height)
@@ -5568,7 +6011,8 @@ current_message ()
     msg = Qnil;
   else
     {
-      with_echo_area_buffer (0, 0, (int (*) ()) current_message_1, &msg);
+      with_echo_area_buffer (0, 0, current_message_1,
+                            (EMACS_INT) &msg, Qnil, 0, 0);
       if (NILP (msg))
        echo_area_buffer[0] = Qnil;
     }
@@ -5578,9 +6022,13 @@ current_message ()
 
 
 static int
-current_message_1 (msg)
-     Lisp_Object *msg;
+current_message_1 (a1, a2, a3, a4)
+     EMACS_INT a1;
+     Lisp_Object a2;
+     EMACS_INT a3, a4;
 {
+  Lisp_Object *msg = (Lisp_Object *) a1;
+  
   if (Z > BEG)
     *msg = make_buffer_string (BEG, Z, 1);
   else
@@ -5660,7 +6108,7 @@ truncate_echo_area (nchars)
     {
       struct frame *sf = SELECTED_FRAME ();
       if (FRAME_MESSAGE_BUF (sf))
-       with_echo_area_buffer (0, 0, (int (*) ()) truncate_message_1, nchars);
+       with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
     }
 }
 
@@ -5669,8 +6117,10 @@ truncate_echo_area (nchars)
    message to at most NCHARS characters.  */
 
 static int
-truncate_message_1 (nchars)
-     int nchars;
+truncate_message_1 (nchars, a2, a3, a4)
+     EMACS_INT nchars;
+     Lisp_Object a2;
+     EMACS_INT a3, a4;
 {
   if (BEG + nchars < Z)
     del_range (BEG + nchars, Z);
@@ -5700,22 +6150,26 @@ set_message (s, string, nbytes, multibyte_p)
     = ((s && multibyte_p)
        || (STRINGP (string) && STRING_MULTIBYTE (string)));
   
-  with_echo_area_buffer (0, -1, (int (*) ()) set_message_1,
-                        s, string, nbytes, multibyte_p);
+  with_echo_area_buffer (0, -1, set_message_1,
+                        (EMACS_INT) 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
+   as there, with A1 corresponding to S and A2 corresponding to STRING
+   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;
+set_message_1 (a1, a2, nbytes, multibyte_p)
+     EMACS_INT a1;
+     Lisp_Object a2;
+     EMACS_INT nbytes, multibyte_p;
 {
+  char *s = (char *) a1;
+  Lisp_Object string = a2;
+  
   xassert (BEG == Z);
   
   /* Change multibyteness of the echo buffer appropriately.  */
@@ -5723,6 +6177,8 @@ set_message_1 (s, string, nbytes, multibyte_p)
       != !NILP (current_buffer->enable_multibyte_characters))
     Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
 
+  current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
+  
   /* Insert new message at BEG.  */
   TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
 
@@ -5766,14 +6222,14 @@ set_message_1 (s, string, nbytes, multibyte_p)
          /* Convert from single-byte to multi-byte.  */
          int i, c, n;
          unsigned char *msg = (unsigned char *) s;
-         unsigned char *str, work[4];
+         unsigned char str[MAX_MULTIBYTE_LENGTH];
       
          /* 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);
+             n = CHAR_STRING (c, str);
+             insert_1_both (str, 1, n, 1, 0, 0);
            }
        }
       else
@@ -5856,18 +6312,14 @@ echo_area_display (update_frame_p)
   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.  */
+#ifdef HAVE_WINDOW_SYSTEM
   /* 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 (sf))
+  if (EQ (selected_frame, Vterminal_frame) 
+      && !NILP (Vwindow_system))
     return 0;
 #endif /* HAVE_WINDOW_SYSTEM */
-#endif
 
   /* Redraw garbaged frames.  */
   if (frame_garbaged)
@@ -5879,20 +6331,30 @@ echo_area_display (update_frame_p)
       window_height_changed_p = display_echo_area (w);
       w->must_be_updated_p = 1;
 
+      /* Update the display, unless called from redisplay_internal. */
       if (update_frame_p)
        {
-         /* Not called from redisplay_internal.  If we changed
-            window configuration, we must redisplay thoroughly.
-            Otherwise, we can do with updating what we displayed
-            above.  */
+         int n = 0;
+
+         /* If the display update has been interrupted by pending
+            input, update mode lines in the frame.  Due to the
+            pending input, it might have been that redisplay hasn't
+            been called, so that mode lines above the echo area are
+            garbaged.  This looks odd, so we prevent it here.  */
+         if (!display_completed)
+           n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
+           
          if (window_height_changed_p)
            {
-             ++windows_or_buffers_changed;
-             ++update_mode_lines;
+             /* Must update other windows.  */
+             windows_or_buffers_changed = 1;
              redisplay_internal (0);
            }
-         else if (FRAME_WINDOW_P (f))
+         else if (FRAME_WINDOW_P (f) && n == 0)
            {
+             /* Window configuration is the same as before.
+                Can do with a display update of the echo area,
+                unless we displayed some mode lines.  */
              update_single_window (w, 1);
              rif->flush_display (f);
            }
@@ -6362,7 +6824,8 @@ build_desired_tool_bar_string (f)
 
   /* Reuse f->desired_tool_bar_string, if possible.  */
   if (size < size_needed)
-    f->desired_tool_bar_string = Fmake_string (make_number (size_needed), ' ');
+    f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
+                                              make_number (' '));
   else
     {
       props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
@@ -6799,9 +7262,16 @@ static int
 hscroll_windows (window)
      Lisp_Object window;
 {
-  int hscrolled_p = hscroll_window_tree (window);
-  if (hscrolled_p)
-    clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
+  int hscrolled_p;
+  
+  if (automatic_hscrolling_p)
+    {
+      hscrolled_p = hscroll_window_tree (window);
+      if (hscrolled_p)
+       clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
+    }
+  else
+    hscrolled_p = 0;
   return hscrolled_p;
 }
 
@@ -6943,6 +7413,43 @@ redisplay ()
   redisplay_internal (0);
 }
 
+/* Return 1 if point moved out of or into a composition.  Otherwise
+   return 0.  PREV_BUF and PREV_PT are the last point buffer and
+   position.  BUF and PT are the current point buffer and position.  */
+
+int
+check_point_in_composition (prev_buf, prev_pt, buf, pt)
+     struct buffer *prev_buf, *buf;
+     int prev_pt, pt;
+{
+  int start, end;
+  Lisp_Object prop;
+  Lisp_Object buffer;
+
+  XSETBUFFER (buffer, buf);
+  /* Check a composition at the last point if point moved within the
+     same buffer.  */
+  if (prev_buf == buf)
+    {
+      if (prev_pt == pt)
+       /* Point didn't move.  */
+       return 0;
+    
+      if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
+         && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
+         && COMPOSITION_VALID_P (start, end, prop)
+         && start < prev_pt && end > prev_pt)
+       /* The last point was within the composition.  Return 1 iff
+            point moved out of the composition.  */
+       return (pt <= start || pt >= end);
+    }
+
+  /* Check a composition at the current point.  */
+  return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
+         && find_composition (pt, -1, &start, &end, &prop, buffer)
+         && COMPOSITION_VALID_P (start, end, prop)
+         && start < pt && end > pt);
+}
 
 /* Reconsider the setting of B->clip_changed which is displayed
    in window W.  */
@@ -6960,6 +7467,29 @@ reconsider_clip_changes (w, b)
           && w->current_matrix->zv == BUF_ZV (b)
           && w->current_matrix->begv == BUF_BEGV (b))
     b->clip_changed = 0;
+
+  /* If display wasn't paused, and W is not a tool bar window, see if
+     point has been moved into or out of a composition.  In that case,
+     we set b->clip_changed to 1 to force updating the screen.  If
+     b->clip_changed has already been set to 1, we can skip this
+     check.  */
+  if (!b->clip_changed
+      && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
+    {
+      int pt;
+
+      if (w == XWINDOW (selected_window))
+       pt = BUF_PT (current_buffer);
+      else
+       pt = marker_position (w->pointm);
+
+      if ((w->current_matrix->buffer != XBUFFER (w->buffer)
+          || pt != XINT (w->last_point))
+         && check_point_in_composition (w->current_matrix->buffer,
+                                        XINT (w->last_point),
+                                        XBUFFER (w->buffer), pt))
+       b->clip_changed = 1;
+    }
 }
 
 
@@ -7022,7 +7552,7 @@ redisplay_internal (preserve_echo_area)
   ++redisplaying_p;
   
  retry:
-
+  pause = 0;
   reconsider_clip_changes (w, current_buffer);
 
   /* If new fonts have been loaded that make a glyph matrix adjustment
@@ -7135,7 +7665,7 @@ redisplay_internal (preserve_echo_area)
            clear_garbaged_frames ();
        }
     }
-  else if (w == XWINDOW (minibuf_window)
+  else if (EQ (selected_window, minibuf_window)
           && (current_buffer->clip_changed
               || XFASTINT (w->last_modified) < MODIFF
               || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
@@ -7278,10 +7808,10 @@ redisplay_internal (preserve_echo_area)
                                     - MATRIX_ROW_START_BYTEPOS (row));
                    }
   
-                 increment_glyph_matrix_buffer_positions (w->current_matrix,
-                                                          this_line_vpos + 1,
-                                                          w->current_matrix->nrows,
-                                                          delta, delta_bytes);
+                 increment_matrix_positions (w->current_matrix,
+                                             this_line_vpos + 1,
+                                             w->current_matrix->nrows,
+                                             delta, delta_bytes);
                }
 
              /* If this row displays text now but previously didn't,
@@ -7331,7 +7861,7 @@ redisplay_internal (preserve_echo_area)
         then we can't just move the cursor.  */
       else if (! (!NILP (Vtransient_mark_mode)
                  && !NILP (current_buffer->mark_active))
-              && (w == XWINDOW (current_buffer->last_selected_window)
+              && (EQ (selected_window, current_buffer->last_selected_window)
                   || highlight_nonselected_windows)
               && NILP (w->region_showing)
               && NILP (Vshow_trailing_whitespace)
@@ -7376,9 +7906,9 @@ redisplay_internal (preserve_echo_area)
   ++clear_face_cache_count;
 
   
-  /* Build desired matrices.  If consider_all_windows_p is non-zero,
-     do it for all windows on all frames.  Otherwise do it for
-     selected_window, only.  */
+  /* Build desired matrices, and update the display.  If
+     consider_all_windows_p is non-zero, do it for all windows on all
+     frames.  Otherwise do it for selected_window, only.  */
 
   if (consider_all_windows_p)
     {
@@ -7398,6 +7928,7 @@ redisplay_internal (preserve_echo_area)
       FOR_EACH_FRAME (tail, frame)
        {
          struct frame *f = XFRAME (frame);
+         
          if (FRAME_WINDOW_P (f) || f == sf)
            {
              /* Mark all the scroll bars to be removed; we'll redeem
@@ -7412,81 +7943,60 @@ redisplay_internal (preserve_echo_area)
                 nuked should now go away.  */
              if (judge_scroll_bars_hook)
                (*judge_scroll_bars_hook) (f);
+
+             /* If fonts changed, display again.  */
+             if (fonts_changed_p)
+               goto retry;
+             
+             if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
+               {
+                 /* See if we have to hscroll.  */
+                 if (hscroll_windows (f->root_window))
+                   goto retry;
+
+                 /* Prevent various kinds of signals during display
+                    update.  stdio is not robust about handling
+                    signals, which can cause an apparent I/O
+                    error.  */
+                 if (interrupt_input)
+                   unrequest_sigio ();
+                 stop_polling ();
+
+                 /* Update the display.  */
+                 set_window_update_flags (XWINDOW (f->root_window), 1);
+                 pause |= update_frame (f, 0, 0);
+                 if (pause)
+                   break;
+
+                 mark_window_display_accurate (f->root_window, 1);
+                 if (frame_up_to_date_hook)
+                   frame_up_to_date_hook (f);
+               }
            }
        }
     }
-  else if (FRAME_VISIBLE_P (sf)
-          && !FRAME_OBSCURED_P (sf))
-    redisplay_window (selected_window, 1);
+  else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
+    {
+      Lisp_Object mini_window;
+      struct frame *mini_frame;
 
+      redisplay_window (selected_window, 1);
   
-  /* Compare desired and current matrices, perform output.  */
-  
-update:
+      /* Compare desired and current matrices, perform output.  */
+    update:
   
-  /* If fonts changed, display again.  */
-  if (fonts_changed_p)
-    goto retry;
-
-  /* Prevent various kinds of signals during display update.
-     stdio is not robust about handling signals,
-     which can cause an apparent I/O error.  */
-  if (interrupt_input)
-    unrequest_sigio ();
-  stop_polling ();
-
-  if (consider_all_windows_p)
-    {
-      Lisp_Object tail;
-      struct frame *f;
-      int hscrolled_p;
-
-      pause = 0;
-      hscrolled_p = 0;
-
-      /* 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 (hscrolled_p)
+      /* If fonts changed, display again.  */
+      if (fonts_changed_p)
        goto retry;
 
-      for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
-       {
-         if (!FRAMEP (XCAR (tail)))
-           continue;
+      /* Prevent various kinds of signals during display update.
+        stdio is not robust about handling signals,
+        which can cause an apparent I/O error.  */
+      if (interrupt_input)
+       unrequest_sigio ();
+      stop_polling ();
 
-         f = XFRAME (XCAR (tail));
-
-         if ((FRAME_WINDOW_P (f) || f == sf)
-             && FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
-           {
-             /* Mark all windows as to be updated.  */
-             set_window_update_flags (XWINDOW (f->root_window), 1);
-             pause |= update_frame (f, 0, 0);
-             if (!pause)
-               {
-                 mark_window_display_accurate (f->root_window, 1);
-                 if (frame_up_to_date_hook != 0)
-                   (*frame_up_to_date_hook) (f);
-               }
-           }
-       }
-    }
-  else
-    {
-      if (FRAME_VISIBLE_P (sf)
-         && !FRAME_OBSCURED_P (sf))
+      if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
        {
          if (hscroll_windows (selected_window))
            goto retry;
@@ -7494,29 +8004,22 @@ update:
          XWINDOW (selected_window)->must_be_updated_p = 1;
          pause = update_frame (sf, 0, 0);
        }
-      else
-       pause = 0;
 
       /* We may have called echo_area_display at the top of this
         function.  If the echo area is on another frame, that may
         have put text on a frame other than the selected one, so the
         above call to update_frame would not have caught it.  Catch
         it here.  */
-      {
-       Lisp_Object mini_window;
-       struct frame *mini_frame;
-
-       mini_window = FRAME_MINIBUF_WINDOW (sf);
-       mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
+      mini_window = FRAME_MINIBUF_WINDOW (sf);
+      mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
        
-       if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
-         {
-           XWINDOW (mini_window)->must_be_updated_p = 1;
-           pause |= update_frame (mini_frame, 0, 0);
-           if (!pause && hscroll_windows (mini_window))
-             goto retry;
-         }
-      }
+      if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
+       {
+         XWINDOW (mini_window)->must_be_updated_p = 1;
+         pause |= update_frame (mini_frame, 0, 0);
+         if (!pause && hscroll_windows (mini_window))
+           goto retry;
+       }
     }
 
   /* If display was paused because of pending input, make sure we do a
@@ -7573,7 +8076,8 @@ update:
          /* Record if we are showing a region, so can make sure to
             update it fully at next redisplay.  */
          w->region_showing = (!NILP (Vtransient_mark_mode)
-                              && (w == XWINDOW (current_buffer->last_selected_window)
+                              && (EQ (selected_window,
+                                      current_buffer->last_selected_window)
                                   || highlight_nonselected_windows)
                               && !NILP (XBUFFER (w->buffer)->mark_active)
                               ? Fmarker_position (XBUFFER (w->buffer)->mark)
@@ -7589,7 +8093,7 @@ update:
          w->current_matrix->begv = BUF_BEGV (b);
          w->current_matrix->zv = BUF_ZV (b);
        }
-      
+
       update_mode_lines = 0;
       windows_or_buffers_changed = 0;
     }
@@ -7732,9 +8236,9 @@ mark_window_display_accurate (window, accurate_p)
              w->last_cursor = w->cursor;
              w->last_cursor_off_p = w->cursor_off_p;
              if (w == XWINDOW (selected_window))
-               w->last_point = BUF_PT (b);
+               w->last_point = make_number (BUF_PT (b));
              else
-               w->last_point = XMARKER (w->pointm)->charpos;
+               w->last_point = make_number (XMARKER (w->pointm)->charpos);
            }
        }
 
@@ -7779,14 +8283,11 @@ disp_char_vector (dp, c)
   if (SINGLE_BYTE_CHAR_P (c))
     return (dp->contents[c]);
   
-  SPLIT_NON_ASCII_CHAR (c, code[0], code[1], code[2]);
-  if (code[0] != CHARSET_COMPOSITION)
-    {
-      if (code[1] < 32)
-       code[1] = -1;
-      else if (code[2] < 32)
-       code[2] = -1;
-    }
+  SPLIT_CHAR (c, code[0], code[1], code[2]);
+  if (code[1] < 32)
+    code[1] = -1;
+  else if (code[2] < 32)
+    code[2] = -1;
   
   /* Here, the possible range of code[0] (== charset ID) is
      128..max_charset.  Since the top level char table contains data
@@ -7856,7 +8357,7 @@ set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
      frames.  */
   if (row->displays_text_p)
     while (glyph < end
-          && !glyph->object
+          && INTEGERP (glyph->object)
           && glyph->charpos < 0)
       {
        x += glyph->pixel_width;
@@ -7864,7 +8365,7 @@ set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
       }
 
   while (glyph < end
-        && glyph->object
+        && !INTEGERP (glyph->object)
         && (!BUFFERP (glyph->object)
             || glyph->charpos < pt_old))
     {
@@ -7945,7 +8446,7 @@ make_cursor_line_fully_visible (w)
 {
   struct glyph_matrix *matrix;
   struct glyph_row *row;
-  int header_line_height;
+  int window_height, 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.  */
@@ -7955,11 +8456,15 @@ make_cursor_line_fully_visible (w)
   matrix = w->desired_matrix;
   row = MATRIX_ROW (matrix, w->cursor.vpos);
 
-  /* 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.  */
-  header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
-  if (row->y == header_line_height)
+  /* If the cursor row is not partially visible, there's nothing
+     to do.  */
+  if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
+    return;
+
+  /* If the row the cursor is in is taller than the window's height,
+     it's not clear what to do, so do nothing.  */
+  window_height = window_box_height (w);
+  if (row->height >= window_height)
     return;
 
   if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
@@ -7969,14 +8474,14 @@ make_cursor_line_fully_visible (w)
       w->cursor.y += dy;
       shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
     }
-  else if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row))
+  else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
     {
       int dy = - (row->height - row->visible_height);
       w->vscroll = dy;
       w->cursor.y += dy;
       shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
     }
-
+  
   /* When we change the cursor y-position of the selected window,
      change this_line_y as well so that the display optimization for
      the cursor line of the selected window in redisplay_internal uses
@@ -8077,7 +8582,7 @@ try_scrolling (window, just_this_one_p, scroll_conservatively,
       
       /* Point is in the scroll margin at the bottom of the window, or
         below.  Compute a new window start that makes point visible.  */
-      
+
       /* Compute the distance from the scroll margin to PT.
         Give up if the distance is greater than scroll_max.  */
       start_display (&it, w, scroll_margin_pos);
@@ -8088,6 +8593,7 @@ try_scrolling (window, just_this_one_p, scroll_conservatively,
                     ? it.max_ascent + it.max_descent
                     : last_height);
       dy = it.current_y + line_height - y0;
+      
       if (dy > scroll_max)
        return 0;
       
@@ -8257,18 +8763,231 @@ compute_window_start_on_continuation_line (w)
          while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
                 distance < min_distance)
            {
-             min_distance = distance;
-             pos = it.current.pos;
-             move_it_by_lines (&it, 1, 0);
+             min_distance = distance;
+             pos = it.current.pos;
+             move_it_by_lines (&it, 1, 0);
+           }
+         
+         /* Set the window start there.  */
+         SET_MARKER_FROM_TEXT_POS (w->start, pos);
+         window_start_changed_p = 1;
+       }
+    }
+  
+  return window_start_changed_p;
+}
+
+
+/* Try cursor movement in case text has not changes in window WINDOW,
+   with window start STARTP.  Value is
+
+   1   if successful
+   
+   0   if this method cannot be used
+   
+   -1  if we know we have to scroll the display.  *SCROLL_STEP is
+   set to 1, under certain circumstances, if we want to scroll as
+   if scroll-step were set to 1.  See the code.  */
+
+static int
+try_cursor_movement (window, startp, scroll_step)
+     Lisp_Object window;
+     struct text_pos startp;
+     int *scroll_step;
+{
+  struct window *w = XWINDOW (window);
+  struct frame *f = XFRAME (w->frame);
+  int rc = 0;
+  
+  /* Handle case where text has not changed, only point, and it has
+     not moved off the frame.  */
+  if (/* Point may be in this window.  */
+      PT >= CHARPOS (startp)
+      /* If we don't check this, we are called to move the cursor in a
+        horizontally split window with a current matrix that doesn't
+        fit the display.  */
+      && !windows_or_buffers_changed
+      /* Selective display hasn't changed.  */
+      && !current_buffer->clip_changed
+      /* If force-mode-line-update was called, really redisplay;
+        that's how redisplay is forced after e.g. changing
+        buffer-invisibility-spec.  */
+      && NILP (w->update_mode_line)
+      /* Can't use this case if highlighting a region.  When a 
+         region exists, cursor movement has to do more than just
+         set the cursor.  */
+      && !(!NILP (Vtransient_mark_mode)
+          && !NILP (current_buffer->mark_active))
+      && NILP (w->region_showing)
+      && NILP (Vshow_trailing_whitespace)
+      /* Right after splitting windows, last_point may be nil.  */
+      && INTEGERP (w->last_point)
+      /* This code is not used for mini-buffer for the sake of the case
+        of redisplaying to replace an echo area message; since in
+        that case the mini-buffer contents per se are usually
+        unchanged.  This code is of no real use in the mini-buffer
+        since the handling of this_line_start_pos, etc., in redisplay
+        handles the same cases.  */
+      && !EQ (window, minibuf_window)
+      /* When splitting windows or for new windows, it happens that
+        redisplay is called with a nil window_end_vpos or one being
+        larger than the window.  This should really be fixed in
+        window.c.  I don't have this on my list, now, so we do
+        approximately the same as the old redisplay code.  --gerd.  */
+      && INTEGERP (w->window_end_vpos)
+      && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
+      && (FRAME_WINDOW_P (f)
+         || !MARKERP (Voverlay_arrow_position)
+         || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
+    {
+      int this_scroll_margin;
+      struct glyph_row *row;
+
+#if GLYPH_DEBUG
+      debug_method_add (w, "cursor movement");
+#endif
+
+      /* Scroll if point within this distance from the top or bottom
+        of the window.  This is a pixel value.  */
+      this_scroll_margin = max (0, scroll_margin);
+      this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
+      this_scroll_margin *= CANON_Y_UNIT (f);
+
+      /* Start with the row the cursor was displayed during the last
+        not paused redisplay.  Give up if that row is not valid.  */
+      if (w->last_cursor.vpos < 0
+         || w->last_cursor.vpos >= w->current_matrix->nrows)
+       rc = -1;
+      else
+       {
+         row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
+         if (row->mode_line_p)
+           ++row;
+         if (!row->enabled_p)
+           rc = -1;
+       }
+
+      if (rc == 0)
+       {
+         int scroll_p = 0;
+         
+         if (PT > XFASTINT (w->last_point))
+           {
+             /* Point has moved forward.  */
+             int last_y = window_text_bottom_y (w) - this_scroll_margin;
+         
+             while (MATRIX_ROW_END_CHARPOS (row) < PT
+                    && MATRIX_ROW_BOTTOM_Y (row) < last_y)
+               {
+                 xassert (row->enabled_p);
+                 ++row;
+               }
+
+             /* The end position of a row equals the start position
+                of the next row.  If PT is there, we would rather
+                display it in the next line.  Exceptions are when the
+                row ends in the middle of a character, or ends in
+                ZV.  */
+             if (MATRIX_ROW_BOTTOM_Y (row) < last_y
+                 && MATRIX_ROW_END_CHARPOS (row) == PT
+                 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
+                 && !row->ends_at_zv_p)
+               {
+                 xassert (row->enabled_p);
+                 ++row;
+               }
+
+             /* If within the scroll margin, scroll.  Note that
+                MATRIX_ROW_BOTTOM_Y gives the pixel position at which
+                the next line would be drawn, and that
+                this_scroll_margin can be zero.  */
+             if (MATRIX_ROW_BOTTOM_Y (row) > last_y
+                 || PT > MATRIX_ROW_END_CHARPOS (row)
+                 /* Line is completely visible last line in window
+                    and PT is to be set in the next line.  */
+                 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
+                     && PT == MATRIX_ROW_END_CHARPOS (row)
+                     && !row->ends_at_zv_p
+                     && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
+               scroll_p = 1;
+           }
+         else if (PT < XFASTINT (w->last_point))
+           {
+             /* Cursor has to be moved backward.  Note that PT >=
+                CHARPOS (startp) because of the outer
+                if-statement.  */
+             while (!row->mode_line_p
+                    && (MATRIX_ROW_START_CHARPOS (row) > PT
+                        || (MATRIX_ROW_START_CHARPOS (row) == PT
+                            && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
+                    && (row->y > this_scroll_margin
+                        || CHARPOS (startp) == BEGV))
+               {
+                 xassert (row->enabled_p);
+                 --row;
+               }
+
+             /* Consider the following case: Window starts at BEGV,
+                there is invisible, intangible text at BEGV, so that
+                display starts at some point START > BEGV.  It can
+                happen that we are called with PT somewhere between
+                BEGV and START.  Try to handle that case.  */
+             if (row < w->current_matrix->rows
+                 || row->mode_line_p)
+               {
+                 row = w->current_matrix->rows;
+                 if (row->mode_line_p)
+                   ++row;
+               }
+
+             /* Due to newlines in overlay strings, we may have to
+                skip forward over overlay strings.  */
+             while (MATRIX_ROW_END_CHARPOS (row) == PT
+                    && MATRIX_ROW_ENDS_IN_OVERLAY_STRING_P (row)
+                    && !row->ends_at_zv_p)
+               ++row;
+         
+             /* If within the scroll margin, scroll.  */
+             if (row->y < this_scroll_margin
+                 && CHARPOS (startp) != BEGV)
+               scroll_p = 1;
+           }
+
+         if (PT < MATRIX_ROW_START_CHARPOS (row)
+             || PT > MATRIX_ROW_END_CHARPOS (row))
+           {
+             /* if PT is not in the glyph row, give up.  */
+             rc = -1;
+           }
+         else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
+           {
+             /* If we end up in a partially visible line, let's make it
+                fully visible, except when it's taller than the window,
+                in which case we can't do much about it.  */
+             if (row->height > window_box_height (w))
+               {
+                 *scroll_step = 1;
+                 rc = -1;
+               }
+             else
+               {
+                 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
+                 try_window (window, startp);
+                 make_cursor_line_fully_visible (w);
+                 rc = 1;
+               }
+           }
+         else if (scroll_p)
+           rc = -1;
+         else
+           {
+             set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
+             rc = 1;
            }
-         
-         /* Set the window start there.  */
-         SET_MARKER_FROM_TEXT_POS (w->start, pos);
-         window_start_changed_p = 1;
        }
     }
-  
-  return window_start_changed_p;
+
+  return rc;
 }
 
 
@@ -8290,9 +9009,9 @@ redisplay_window (window, just_this_one_p)
   struct it it;
   /* Record it now because it's overwritten.  */
   int current_matrix_up_to_date_p = 0;
-  int really_switched_buffer = 0;
   int temp_scroll_step = 0;
   int count = specpdl_ptr - specpdl;
+  int rc;
 
   SET_TEXT_POS (lpoint, PT, PT_BYTE);
   opoint = lpoint;
@@ -8343,15 +9062,9 @@ redisplay_window (window, just_this_one_p)
 
   /* Otherwise set up data on this window; select its buffer and point
      value.  */
-  if (update_mode_line)
-    {
-      /* Really select the buffer, for the sake of buffer-local
-         variables.  */
-      set_buffer_internal_1 (XBUFFER (w->buffer));
-      really_switched_buffer = 1;
-    }
-  else
-    set_buffer_temp (XBUFFER (w->buffer));
+  /* Really select the buffer, for the sake of buffer-local
+     variables.  */
+  set_buffer_internal_1 (XBUFFER (w->buffer));
   SET_TEXT_POS (opoint, PT, PT_BYTE);
 
   current_matrix_up_to_date_p
@@ -8454,10 +9167,11 @@ redisplay_window (window, just_this_one_p)
 
   /* If someone specified a new starting point but did not insist,
      check whether it can be used.  */
-  if (!NILP (w->optional_new_start))
+  if (!NILP (w->optional_new_start)
+      && CHARPOS (startp) >= BEGV
+      && CHARPOS (startp) <= ZV)
     {
       w->optional_new_start = Qnil;
-      /* This takes a mini-buffer prompt into account.  */
       start_display (&it, w, startp);
       move_it_to (&it, PT, 0, it.last_visible_y, -1,
                  MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
@@ -8489,13 +9203,6 @@ redisplay_window (window, just_this_one_p)
       if (!update_mode_line
          || ! NILP (Vwindow_scroll_functions))
        {
-         if (!really_switched_buffer)
-           {
-             set_buffer_temp (old);
-             set_buffer_internal_1 (XBUFFER (w->buffer));
-             really_switched_buffer = 1;
-           }
-         
          update_mode_line = 1;
          w->update_mode_line = Qt;
          startp = run_window_scroll_functions (window, startp);
@@ -8519,13 +9226,15 @@ redisplay_window (window, just_this_one_p)
 
       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
-            matrix has been built above, so we can use it.  */
-         int height = window_box_height (w) / 2;
-         struct glyph_row *row = MATRIX_ROW (w->desired_matrix, 0);
-         
-         while (row->y < height)
+         /* If point does not appear, try to move point so it does
+            appear. The desired matrix has been built above, so we
+            can use it here.  */
+         int window_height;
+         struct glyph_row *row;
+
+         window_height = window_box_height (w) / 2;
+         row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
+         while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
            ++row;
 
          TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
@@ -8559,164 +9268,14 @@ redisplay_window (window, just_this_one_p)
   /* Handle case where text has not changed, only point, and it has
      not moved off the frame.  */
   if (current_matrix_up_to_date_p
-      /* Point may be in this window.  */
-      && PT >= CHARPOS (startp)
-      /* If we don't check this, we are called to move the cursor in a
-        horizontally split window with a current matrix that doesn't
-        fit the display.  */
-      && !windows_or_buffers_changed
-      /* Selective display hasn't changed.  */
-      && !current_buffer->clip_changed
-      /* If force-mode-line-update was called, really redisplay;
-        that's how redisplay is forced after e.g. changing
-        buffer-invisibility-spec.  */
-      && NILP (w->update_mode_line)
-      /* Can't use this case if highlighting a region.  When a 
-         region exists, cursor movement has to do more than just
-         set the cursor.  */
-      && !(!NILP (Vtransient_mark_mode)
-          && !NILP (current_buffer->mark_active))
-      && NILP (w->region_showing)
-      && NILP (Vshow_trailing_whitespace)
-      /* Right after splitting windows, last_point may be nil.  */
-      && INTEGERP (w->last_point)
-      /* This code is not used for mini-buffer for the sake of the case
-        of redisplaying to replace an echo area message; since in
-        that case the mini-buffer contents per se are usually
-        unchanged.  This code is of no real use in the mini-buffer
-        since the handling of this_line_start_pos, etc., in redisplay
-        handles the same cases.  */
-      && !EQ (window, minibuf_window)
-      /* When splitting windows or for new windows, it happens that
-        redisplay is called with a nil window_end_vpos or one being
-        larger than the window.  This should really be fixed in
-        window.c.  I don't have this on my list, now, so we do
-        approximately the same as the old redisplay code.  --gerd.  */
-      && INTEGERP (w->window_end_vpos)
-      && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
-      && (FRAME_WINDOW_P (f)
-         || !MARKERP (Voverlay_arrow_position)
-         || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
+      && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
+         rc != 0))
     {
-      int this_scroll_margin;
-      struct glyph_row *row;
-      int scroll_p;
-
-#if GLYPH_DEBUG
-      debug_method_add (w, "cursor movement");
-#endif
-
-      /* Scroll if point within this distance from the top or bottom
-        of the window.  This is a pixel value.  */
-      this_scroll_margin = max (0, scroll_margin);
-      this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
-      this_scroll_margin *= CANON_Y_UNIT (f);
-
-      /* Start with the row the cursor was displayed during the last
-        not paused redisplay.  Give up if that row is not valid.  */
-      if (w->last_cursor.vpos >= w->current_matrix->nrows)
-       goto try_to_scroll;
-      row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
-      if (row->mode_line_p)
-       ++row;
-      if (!row->enabled_p)
-       goto try_to_scroll;
-
-      scroll_p = 0;
-      if (PT > XFASTINT (w->last_point))
-       {
-         /* Point has moved forward.  */
-         int last_y = window_text_bottom_y (w) - this_scroll_margin;
-         
-         while ((MATRIX_ROW_END_CHARPOS (row) < PT
-                 /* The end position of a row equals the start
-                    position of the next row.  If PT is there, we
-                    would rather display it in the next line, except
-                    when this line ends in ZV.  */
-                 || (MATRIX_ROW_END_CHARPOS (row) == PT
-                     && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
-                         || !row->ends_at_zv_p)))
-                && MATRIX_ROW_BOTTOM_Y (row) < last_y)
-           {
-             xassert (row->enabled_p);
-             ++row;
-           }
-
-         /* If within the scroll margin, scroll.  Note that
-            MATRIX_ROW_BOTTOM_Y gives the pixel position at which the
-            next line would be drawn, and that this_scroll_margin can
-            be zero.  */
-         if (MATRIX_ROW_BOTTOM_Y (row) > last_y
-             || PT > MATRIX_ROW_END_CHARPOS (row)
-             /* Line is completely visible last line in window and PT
-                is to be set in the next line.  */
-             || (MATRIX_ROW_BOTTOM_Y (row) == last_y
-                 && PT == MATRIX_ROW_END_CHARPOS (row)
-                 && !row->ends_at_zv_p
-                 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
-           scroll_p = 1;
-       }
-      else if (PT < XFASTINT (w->last_point))
-       {
-         /* Cursor has to be moved backward.  Note that PT >=
-            CHARPOS (startp) because of the outer if-statement.  */
-         while (!row->mode_line_p
-                && (MATRIX_ROW_START_CHARPOS (row) > PT
-                    || (MATRIX_ROW_START_CHARPOS (row) == PT
-                        && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
-                && (row->y > this_scroll_margin
-                    || CHARPOS (startp) == BEGV))
-           {
-             xassert (row->enabled_p);
-             --row;
-           }
-
-         /* Consider the following case: Window starts at BEGV, there
-            is invisible, intangible text at BEGV, so that display
-            starts at some point START > BEGV.  It can happen that
-            we are called with PT somewhere between BEGV and START.
-            Try to handle that case.  */
-         if (row < w->current_matrix->rows
-             || row->mode_line_p)
-           {
-             row = w->current_matrix->rows;
-             if (row->mode_line_p)
-               ++row;
-           }
-
-         /* Due to newlines in overlay strings, we may have to skip
-            forward over overlay strings.  */
-         while (MATRIX_ROW_END_CHARPOS (row) == PT
-                && MATRIX_ROW_ENDS_IN_OVERLAY_STRING_P (row)
-                && !row->ends_at_zv_p)
-           ++row;
-         
-         /* If within the scroll margin, scroll.  */
-         if (row->y < this_scroll_margin
-             && CHARPOS (startp) != BEGV)
-           scroll_p = 1;
-       }
-
-      /* if PT is not in the glyph row, give up.  */
-      if (PT < MATRIX_ROW_START_CHARPOS (row)
-         || PT > MATRIX_ROW_END_CHARPOS (row))
+      if (rc == -1)
        goto try_to_scroll;
-
-      /* If we end up in a partially visible line, let's make it fully
-        visible.  This can be done most easily by using the existing
-        scrolling code.  */
-      if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
-       {
-         temp_scroll_step = 1;
-         goto try_to_scroll;
-       }
-      else if (scroll_p)
-       goto try_to_scroll;
-      
-      set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
-      goto done;
+      else
+       goto done;
     }
-  
   /* If current starting point was originally the beginning of a line
      but no longer is, find a new starting point.  */
   else if (!NILP (w->start_at_line_beg)
@@ -8761,13 +9320,14 @@ redisplay_window (window, just_this_one_p)
           && (tem = try_window_id (w)) != 0)
     {
 #if GLYPH_DEBUG
-      debug_method_add (w, "try_window_id");
+      debug_method_add (w, "try_window_id %d", tem);
 #endif
 
       if (fonts_changed_p)
        goto restore_buffers;
       if (tem > 0)
        goto done;
+
       /* Otherwise try_window_id has returned -1 which means that we
         don't want the alternative below this comment to execute.  */
     }
@@ -8788,7 +9348,8 @@ redisplay_window (window, just_this_one_p)
          If point has not moved off frame, accept the results.  */
       if (!current_matrix_up_to_date_p
          /* Don't use try_window_reusing_current_matrix in this case
-            because it can have changed the buffer.  */
+            because a window scroll function can have changed the
+            buffer.  */
          || !NILP (Vwindow_scroll_functions)
          || MINI_WINDOW_P (w)
          || !try_window_reusing_current_matrix (w))
@@ -8823,12 +9384,6 @@ redisplay_window (window, just_this_one_p)
   /* Redisplay the mode line.  Select the buffer properly for that.  */
   if (!update_mode_line)
     {
-      if (!really_switched_buffer)
-       {
-         set_buffer_temp (old);
-         set_buffer_internal_1 (XBUFFER (w->buffer));
-         really_switched_buffer = 1;
-       }
       update_mode_line = 1;
       w->update_mode_line = Qt;
     }
@@ -8980,7 +9535,13 @@ redisplay_window (window, just_this_one_p)
        && (WINDOW_WANTS_MODELINE_P (w)
           || WINDOW_WANTS_HEADER_LINE_P (w)))
     {
+      Lisp_Object old_selected_frame;
+      
+      old_selected_frame = selected_frame;
+      
+      XSETFRAME (selected_frame, f);
       display_mode_lines (w);
+      selected_frame = old_selected_frame;
 
       /* If mode line height has changed, arrange for a thorough
         immediate redisplay using the correct mode line height.  */
@@ -9086,10 +9647,7 @@ redisplay_window (window, just_this_one_p)
 
   /* Restore current_buffer and value of point in it.  */
   TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
-  if (really_switched_buffer)
-    set_buffer_internal_1 (old);
-  else
-    set_buffer_temp (old);
+  set_buffer_internal_1 (old);
   TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
 
   unbind_to (count, Qnil);
@@ -9187,8 +9745,12 @@ try_window_reusing_current_matrix (w)
   struct glyph_row *start_row;
   int start_vpos, min_y, max_y;
 
-  /* Right now this function doesn't handle terminal frames.  */
-  if (!FRAME_WINDOW_P (f))
+  
+  if (/* This function doesn't handle terminal frames.  */
+      !FRAME_WINDOW_P (f)
+      /* Don't try to reuse the display if windows have been split
+        or such.  */
+      || windows_or_buffers_changed)
     return 0;
 
   /* Can't do this if region may have changed.  */
@@ -9285,12 +9847,14 @@ try_window_reusing_current_matrix (w)
          run.current_y = first_row_y;
          run.desired_y = it.current_y;
          run.height = it.last_visible_y - it.current_y;
-         if (run.height > 0)
+         if (run.height > 0
+             && run.current_y != run.desired_y)
            {
              update_begin (f);
              rif->update_window_begin_hook (w);
+             rif->clear_mouse_face (w);
              rif->scroll_run_hook (w, &run);
-             rif->update_window_end_hook (w, 0);
+             rif->update_window_end_hook (w, 0, 0);
              update_end (f);
            }
 
@@ -9457,8 +10021,9 @@ try_window_reusing_current_matrix (w)
          struct frame *f = XFRAME (WINDOW_FRAME (w));
          update_begin (f);
          rif->update_window_begin_hook (w);
+         rif->clear_mouse_face (w);
          rif->scroll_run_hook (w, &run);
-         rif->update_window_end_hook (w, 0);
+         rif->update_window_end_hook (w, 0, 0);
          update_end (f);
        }
 
@@ -9847,29 +10412,50 @@ try_window_id (w)
      only if buffer has really changed.  The reason is that the gap is
      initially at Z for freshly visited files.  The code below would
      set end_unchanged to 0 in that case.  */
-  if (MODIFF > SAVE_MODIFF)
+  if (MODIFF > SAVE_MODIFF
+      /* This seems to happen sometimes after saving a buffer.  */
+      || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
     {
       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
      front of that newline, then changes don't affect the display.
-     This case happens with stealth-fontification.  */
+     This case happens with stealth-fontification.  Note that although
+     the display is unchanged, glyph positions in the matrix have to
+     be adjusted, of course.  */
   row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
   if (CHARPOS (start) > BEGV
       && Z - END_UNCHANGED < CHARPOS (start) - 1
       && FETCH_BYTE (BYTEPOS (start) - 1) == '\n'
       && PT < MATRIX_ROW_END_CHARPOS (row))
     {
-      /* We have to update window end positions because the buffer's
-        size has changed.  */
+      struct glyph_row *r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
+      int delta = CHARPOS (start) - MATRIX_ROW_START_CHARPOS (r0);
+
+      if (delta)
+       {
+         struct glyph_row *r1 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
+         int delta_bytes = BYTEPOS (start) - MATRIX_ROW_START_BYTEPOS (r0);
+
+         increment_matrix_positions (w->current_matrix,
+                                     MATRIX_ROW_VPOS (r0, current_matrix),
+                                     MATRIX_ROW_VPOS (r1, current_matrix),
+                                     delta, delta_bytes);
+       }
+      
+#if 0  /* If changes are all in front of the window start, the
+         distance of the last displayed glyph from Z hasn't
+         changed.  */
       w->window_end_pos
        = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
       w->window_end_bytepos
-       = make_number (Z_BYTE - MATRIX_ROW_END_BYTEPOS (row));
+       = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
+#endif
+
       return 1;
     }
 
@@ -9883,8 +10469,12 @@ try_window_id (w)
       w->window_end_pos
        = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
       w->window_end_bytepos
-       = make_number (Z_BYTE - MATRIX_ROW_END_BYTEPOS (row));
-      return 1;
+       = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
+
+      row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
+      row = row_containing_pos (w, PT, row, NULL);
+      set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
+      return 2;
     }
 
   /* Check that window start agrees with the start of the first glyph
@@ -10051,8 +10641,8 @@ try_window_id (w)
          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);
+         if (row)
+           set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
        }
 
       /* Start from first_unchanged_at_end_row looking for PT.  */
@@ -10098,15 +10688,16 @@ try_window_id (w)
   /* Scroll the display.  Do it before changing the current matrix so
      that xterm.c doesn't get confused about where the cursor glyph is
      found.  */
-  if (dy)
+  if (dy && run.height)
     {
       update_begin (f);
          
       if (FRAME_WINDOW_P (f))
        {
          rif->update_window_begin_hook (w);
+         rif->clear_mouse_face (w);
          rif->scroll_run_hook (w, &run);
-         rif->update_window_end_hook (w, 0);
+         rif->update_window_end_hook (w, 0, 0);
        }
       else
        {
@@ -10184,9 +10775,9 @@ try_window_id (w)
 
   /* Adjust buffer positions in reused rows.  */
   if (delta)
-    increment_glyph_matrix_buffer_positions (current_matrix,
-                                            first_unchanged_at_end_vpos + dvpos,
-                                            bottom_vpos, delta, delta_bytes);
+    increment_matrix_positions (current_matrix,
+                               first_unchanged_at_end_vpos + dvpos,
+                               bottom_vpos, delta, delta_bytes);
 
   /* Adjust Y positions.  */
   if (dy)
@@ -10315,7 +10906,7 @@ try_window_id (w)
   /* Record that display has not been completed.  */
   w->window_end_valid = Qnil;
   w->desired_matrix->no_scrolling_p = 1;
-  return 1;
+  return 3;
 }
 
 
@@ -10333,7 +10924,7 @@ static void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
 /* Dump the contents of glyph matrix MATRIX on stderr.  If
    WITH_GLYPHS_P is non-zero, dump glyph contents as well.  */
 
-void
+static void
 dump_glyph_matrix (matrix, with_glyphs_p)
      struct glyph_matrix *matrix;
      int with_glyphs_p;
@@ -10359,10 +10950,11 @@ dump_glyph_row (matrix, vpos, with_glyphs_p)
 
   row = MATRIX_ROW (matrix, vpos);
   
-  fprintf (stderr, "Row Start   End Used oEI><O\\CTZF    X   Y   W\n");
-  fprintf (stderr, "=============================================\n");
+  fprintf (stderr, "Row Start   End Used oEI><O\\CTZFes     X    Y    W    H    V    A    P\n");
+  fprintf (stderr, "=======================================================================\n");
   
-  fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d\n",
+  fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d\
+%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
           row - matrix->rows,
           MATRIX_ROW_START_CHARPOS (row),
           MATRIX_ROW_END_CHARPOS (row),
@@ -10378,9 +10970,15 @@ dump_glyph_row (matrix, vpos, with_glyphs_p)
           row->displays_text_p,
           row->ends_at_zv_p,
           row->fill_line_p,
+          row->ends_in_middle_of_char_p,
+          row->starts_in_middle_of_char_p,
           row->x,
           row->y,
-          row->pixel_width);
+          row->pixel_width,
+          row->height,
+          row->visible_height,
+          row->ascent,
+          row->phys_ascent);
   fprintf (stderr, "%9d %5d\n", row->start.overlay_string_index,
           row->end.overlay_string_index);
   fprintf (stderr, "%9d %5d\n",
@@ -10403,7 +11001,7 @@ dump_glyph_row (matrix, vpos, with_glyphs_p)
       
       if (glyph < glyph_end)
        {
-         fprintf (stderr, "  Glyph    Type Pos   W    Code C Face LR\n");
+         fprintf (stderr, "  Glyph    Type Pos   W    Code C Face LR\n");
          prev_had_glyphs_p = 1;
        }
       else
@@ -10414,44 +11012,59 @@ dump_glyph_row (matrix, vpos, with_glyphs_p)
          if (glyph->type == CHAR_GLYPH)
            {
              fprintf (stderr,
-                      "  %5d %4c %6d %3d 0x%05x %c %4d %1.1d%1.1d\n",
+                      "  %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
                       glyph - row->glyphs[TEXT_AREA],
                       'C',
                       glyph->charpos,
+                      (BUFFERP (glyph->object)
+                       ? 'B'
+                       : (STRINGP (glyph->object)
+                          ? 'S'
+                          : '-')),
                       glyph->pixel_width,
-                      glyph->u.ch.code,
-                      (glyph->u.ch.code < 0x80 && glyph->u.ch.code >= ' '
-                       ? glyph->u.ch.code
+                      glyph->u.ch,
+                      (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
+                       ? glyph->u.ch
                        : '.'),
-                      glyph->u.ch.face_id,
+                      glyph->face_id,
                       glyph->left_box_line_p,
                       glyph->right_box_line_p);
            }
          else if (glyph->type == STRETCH_GLYPH)
            {
              fprintf (stderr,
-                      "  %5d %4c %6d %3d 0x%05x %c %4d %1.1d%1.1d\n",
+                      "  %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
                       glyph - row->glyphs[TEXT_AREA],
                       'S',
                       glyph->charpos,
+                      (BUFFERP (glyph->object)
+                       ? 'B'
+                       : (STRINGP (glyph->object)
+                          ? 'S'
+                          : '-')),
                       glyph->pixel_width,
                       0,
                       '.',
-                      glyph->u.stretch.face_id,
+                      glyph->face_id,
                       glyph->left_box_line_p,
                       glyph->right_box_line_p);
            }
          else if (glyph->type == IMAGE_GLYPH)
            {
              fprintf (stderr,
-                      "  %5d %4c %6d %3d 0x%05x %c %4d %1.1d%1.1d\n",
+                      "  %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
                       glyph - row->glyphs[TEXT_AREA],
                       'I',
                       glyph->charpos,
+                      (BUFFERP (glyph->object)
+                       ? 'B'
+                       : (STRINGP (glyph->object)
+                          ? 'S'
+                          : '-')),
                       glyph->pixel_width,
-                      glyph->u.img.id,
+                      glyph->u.img_id,
                       '.',
-                      glyph->u.img.face_id,
+                      glyph->face_id,
                       glyph->left_box_line_p,
                       glyph->right_box_line_p);
            }
@@ -10467,6 +11080,7 @@ DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
 Shows contents of glyph row structures.  With non-nil optional\n\
 parameter WITH-GLYPHS-P, dump glyphs as well.")
   (with_glyphs_p)
+     Lisp_Object with_glyphs_p;
 {
   struct window *w = XWINDOW (selected_window);
   struct buffer *buffer = XBUFFER (w->buffer);
@@ -10512,7 +11126,17 @@ DEFUN ("trace-redisplay-toggle", Ftrace_redisplay_toggle,
   trace_redisplay_p = !trace_redisplay_p;
   return Qnil;
 }
-       
+
+
+DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, 1, "",
+   "Print STRING to stderr.")
+   (string)
+     Lisp_Object string;
+{
+  CHECK_STRING (string, 0);
+  fprintf (stderr, "%s", XSTRING (string)->data);
+  return Qnil;
+}
        
 #endif /* GLYPH_DEBUG */
 
@@ -10599,13 +11223,12 @@ insert_left_trunc_glyphs (it)
 
   /* Get the truncation glyphs.  */
   truncate_it = *it;
-  truncate_it.charset = -1;
   truncate_it.current_x = 0;
   truncate_it.face_id = DEFAULT_FACE_ID;
   truncate_it.glyph_row = &scratch_glyph_row;
   truncate_it.glyph_row->used[TEXT_AREA] = 0;
   CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
-  truncate_it.object = 0;
+  truncate_it.object = make_number (0);
   produce_special_glyphs (&truncate_it, IT_TRUNCATION);
   
   /* Overwrite glyphs from IT with truncation glyphs.  */
@@ -10683,7 +11306,7 @@ compute_line_metrics (it)
       /* If first line's physical ascent is larger than its logical
          ascent, use the physical ascent, and make the row taller.
          This makes accented characters fully visible.  */
-      if (row == it->w->desired_matrix->rows
+      if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
          && row->phys_ascent > row->ascent)
        {
          row->height += row->phys_ascent - row->ascent;
@@ -10716,6 +11339,8 @@ compute_line_metrics (it)
     for (i = 0; i < row->used[area]; ++i)
       row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
                   + row->glyphs[area][i].u.val
+                  + row->glyphs[area][i].face_id
+                  + row->glyphs[area][i].padding_p
                   + (row->glyphs[area][i].type << 2));
 
   it->max_ascent = it->max_descent = 0;
@@ -10726,7 +11351,7 @@ 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.
+   IT->face_id.  Value is non-zero if a space was added.
 
    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
@@ -10736,7 +11361,7 @@ compute_line_metrics (it)
    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
+static int
 append_space (it, default_face_p)
      struct it *it;
      int default_face_p;
@@ -10753,25 +11378,22 @@ append_space (it, default_face_p)
          struct text_pos saved_pos;
          int saved_what = it->what;
          int saved_face_id = it->face_id;
-         int saved_charset = it->charset;
          Lisp_Object saved_object;
+         struct face *face;
 
          saved_object = it->object;
          saved_pos = it->position;
          
          it->what = IT_CHARACTER;
          bzero (&it->position, sizeof it->position);
-         it->object = 0;
+         it->object = make_number (0);
          it->c = ' ';
          it->len = 1;
-         it->charset = CHARSET_ASCII;
 
          if (default_face_p)
            it->face_id = DEFAULT_FACE_ID;
-         if (it->multibyte_p)
-           it->face_id = FACE_FOR_CHARSET (it->f, it->face_id, CHARSET_ASCII);
-         else
-           it->face_id = FACE_FOR_CHARSET (it->f, it->face_id, -1);
+         face = FACE_FROM_ID (it->f, it->face_id);
+         it->face_id = FACE_FOR_CHAR (it->f, face, 0);
 
          PRODUCE_GLYPHS (it);
          
@@ -10780,9 +11402,11 @@ append_space (it, default_face_p)
          it->position = saved_pos;
          it->what = saved_what;
          it->face_id = saved_face_id;
-         it->charset = saved_charset;
+         return 1;
        }
     }
+
+  return 0;
 }
 
 
@@ -10816,15 +11440,13 @@ extend_face_to_end_of_line (it)
      in the text area has to be drawn to the end of the text area.  */
   it->glyph_row->fill_line_p = 1;
 
-  /* If current charset of IT is not ASCII, make sure we have the
-     ASCII face.  This will be automatically undone the next time
-     get_next_display_element returns a character from a different
-     charset.  Note that the charset will always be ASCII in unibyte
-     text.  */
-  if (it->charset != CHARSET_ASCII)
+  /* If current character of IT is not ASCII, make sure we have the
+         ASCII face.  This will be automatically undone the next time
+         get_next_display_element returns a multibyte character.  Note
+         that the character will always be single byte in unibyte text.  */
+  if (!SINGLE_BYTE_CHAR_P (it->c))
     {
-      it->charset = CHARSET_ASCII;
-      it->face_id = FACE_FOR_CHARSET (f, it->face_id, CHARSET_ASCII);
+      it->face_id = FACE_FOR_CHAR (f, face, 0);
     }
 
   if (FRAME_WINDOW_P (f))
@@ -10834,7 +11456,7 @@ extend_face_to_end_of_line (it)
       if (it->glyph_row->used[TEXT_AREA] == 0)
        {
          it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
-         it->glyph_row->glyphs[TEXT_AREA][0].u.ch.face_id = it->face_id;
+         it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
          it->glyph_row->used[TEXT_AREA] = 1;
        }
     }
@@ -10851,7 +11473,7 @@ extend_face_to_end_of_line (it)
   
       it->what = IT_CHARACTER;
       bzero (&it->position, sizeof it->position);
-      it->object = 0;
+      it->object = make_number (0);
       it->c = ' ';
       it->len = 1;
       
@@ -10911,8 +11533,8 @@ highlight_trailing_whitespace (f, row)
       /* Skip over the space glyph inserted to display the
         cursor at the end of a line.  */
       if (glyph->type == CHAR_GLYPH
-         && glyph->u.ch.code == ' '
-         && glyph->object == 0)
+         && glyph->u.ch == ' '
+         && INTEGERP (glyph->object))
        --glyph;
 
       /* If last glyph is a space or stretch, and it's trailing
@@ -10922,24 +11544,17 @@ highlight_trailing_whitespace (f, row)
          && BUFFERP (glyph->object)
          && (glyph->type == STRETCH_GLYPH
              || (glyph->type == CHAR_GLYPH
-                 && glyph->u.ch.code == ' '))
+                 && glyph->u.ch == ' '))
          && trailing_whitespace_p (glyph->charpos))
        {
-         int face_id = lookup_named_face (f, Qtrailing_whitespace,
-                                          CHARSET_ASCII);
+         int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
          
          while (glyph >= start
                 && BUFFERP (glyph->object)
                 && (glyph->type == STRETCH_GLYPH
                     || (glyph->type == CHAR_GLYPH
-                        && glyph->u.ch.code == ' ')))
-           {
-             if (glyph->type == STRETCH_GLYPH)
-               glyph->u.stretch.face_id = face_id;
-             else
-               glyph->u.ch.face_id = face_id;
-             --glyph;
-           }
+                        && glyph->u.ch == ' ')))
+           (glyph--)->face_id = face_id;
        }
     }
 }
@@ -10974,6 +11589,8 @@ display_line (it)
   row->start = it->current;
   row->continuation_lines_width = it->continuation_lines_width;
   row->displays_text_p = 1;
+  row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
+  it->starts_in_middle_of_char_p = 0;
 
   /* Arrange the overlays nicely for our purposes.  Usually, we call
      display_line on only one line at a time, in which case this
@@ -11002,19 +11619,18 @@ display_line (it)
     {
       int n_glyphs_before, hpos_before, x_before;
       int x, i, nglyphs;
-      
+      int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
+
       /* Retrieve the next thing to display.  Value is zero if end of
         buffer reached.  */
       if (!get_next_display_element (it))
        {
          /* Maybe add a space at the end of this line that is used to
-            display the cursor there under X.  */
-         append_space (it, 1);
-
-         /* The position -1 below indicates a blank line not
-            corresponding to any text, as opposed to an empty line
-            corresponding to a line end.  */
-         if (row->used[TEXT_AREA] <= 1)
+            display the cursor there under X.  Set the charpos of the
+            first glyph of blank lines not corresponding to any text
+            to -1.  */
+         if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
+             || row->used[TEXT_AREA] == 0)
            {
              row->glyphs[TEXT_AREA]->charpos = -1;
              row->displays_text_p = 0;
@@ -11032,6 +11648,17 @@ display_line (it)
         generates glyphs in `row' (which is IT->glyph_row).  */
       n_glyphs_before = row->used[TEXT_AREA];
       x = it->current_x;
+
+      /* Remember the line height so far in case the next element doesn't
+        fit on the line.  */
+      if (!it->truncate_lines_p)
+       {
+         ascent = it->max_ascent;
+         descent = it->max_descent;
+         phys_ascent = it->max_phys_ascent;
+         phys_descent = it->max_phys_descent;
+       }
+      
       PRODUCE_GLYPHS (it);
 
       /* If this display element was in marginal areas, continue with
@@ -11096,9 +11723,10 @@ display_line (it)
                      || (new_x == it->last_visible_x
                          && FRAME_WINDOW_P (it->f)))
                    {
-                     /* Current glyph fits exactly on the line.  We
-                        must continue the line because we can't draw
-                        the cursor after the glyph.  */
+                     /* Current glyph is the only one on the line or
+                        fits exactly on the line.  We must continue
+                        the line because we can't draw the cursor
+                        after the glyph.  */
                      row->continued_p = 1;
                      it->current_x = new_x;
                      it->continuation_lines_width += new_x;
@@ -11106,6 +11734,31 @@ display_line (it)
                      if (i == nglyphs - 1)
                        set_iterator_to_next (it);
                    }
+                 else if (CHAR_GLYPH_PADDING_P (*glyph)
+                          && !FRAME_WINDOW_P (it->f))
+                   {
+                     /* A padding glyph that doesn't fit on this line.
+                        This means the whole character doesn't fit
+                        on the line.  */
+                     row->used[TEXT_AREA] = n_glyphs_before;
+                 
+                     /* Fill the rest of the row with continuation
+                        glyphs like in 20.x.  */
+                     while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
+                            < row->glyphs[1 + TEXT_AREA])
+                       produce_special_glyphs (it, IT_CONTINUATION);
+                     
+                     row->continued_p = 1;
+                     it->current_x = x_before;
+                     it->continuation_lines_width += x_before;
+                     
+                     /* Restore the height to what it was before the
+                        element not fitting on the line.  */
+                     it->max_ascent = ascent;
+                     it->max_descent = descent;
+                     it->max_phys_ascent = phys_ascent;
+                     it->max_phys_descent = phys_descent;
+                   }
                  else
                    {
                      /* Display element draws past the right edge of
@@ -11122,7 +11775,20 @@ display_line (it)
                      
                      it->current_x = x;
                      it->continuation_lines_width += x;
+                     if (nglyphs > 1 && i > 0)
+                       {
+                         row->ends_in_middle_of_char_p = 1;
+                         it->starts_in_middle_of_char_p = 1;
+                       }
+                     
+                     /* Restore the height to what it was before the
+                        element not fitting on the line.  */
+                     it->max_ascent = ascent;
+                     it->max_descent = descent;
+                     it->max_phys_ascent = phys_ascent;
+                     it->max_phys_descent = phys_descent;
                    }
+
                  break;
                }
              else if (new_x > it->first_visible_x)
@@ -11265,16 +11931,20 @@ display_line (it)
   /* Remember the position at which this line ends.  */
   row->end = it->current;
 
-  /* Maybe set the cursor.  If you change this, it's probably a good
-     idea to also change the code in redisplay_window for cursor
-     movement in an unchanged window.  */
+  /* Maybe set the cursor.  */
   if (it->w->cursor.vpos < 0
       && PT >= MATRIX_ROW_START_CHARPOS (row)
-      && MATRIX_ROW_END_CHARPOS (row) >= PT
-      && !(MATRIX_ROW_END_CHARPOS (row) == PT
-          && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
-              || !row->ends_at_zv_p)))
-    set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
+      && PT <= MATRIX_ROW_END_CHARPOS (row))
+    {
+      /* Also see redisplay_window, case cursor movement in unchanged
+        window.  */
+      if (MATRIX_ROW_END_CHARPOS (row) == PT
+         && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
+         && !row->ends_at_zv_p)
+       ;
+      else
+       set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
+    }
 
   /* Highlight trailing whitespace.  */
   if (!NILP (Vshow_trailing_whitespace))
@@ -11328,7 +11998,7 @@ display_menu_bar (w)
 
 #ifdef USE_X_TOOLKIT
   xassert (!FRAME_WINDOW_P (f));
-  init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MODE_LINE_FACE_ID);
+  init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
   it.first_visible_x = 0;
   it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
 #else /* not USE_X_TOOLKIT */
@@ -11340,7 +12010,7 @@ display_menu_bar (w)
       xassert (WINDOWP (f->menu_bar_window));
       menu_w = XWINDOW (f->menu_bar_window);
       init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
-                    MODE_LINE_FACE_ID);
+                    MENU_FACE_ID);
       it.first_visible_x = 0;
       it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
     }
@@ -11349,7 +12019,7 @@ display_menu_bar (w)
       /* This is a TTY frame, i.e. character hpos/vpos are used as
         pixel x/y.  */
       init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
-                    MODE_LINE_FACE_ID);
+                    MENU_FACE_ID);
       it.first_visible_x = 0;
       it.last_visible_x = FRAME_WIDTH (f);
     }
@@ -11401,23 +12071,106 @@ display_menu_bar (w)
                              Mode Line
  ***********************************************************************/
 
-/* Display the mode and/or top line of window W.  */
+/* Redisplay mode lines in the window tree whose root is WINDOW.  If
+   FORCE is non-zero, redisplay mode lines unconditionally.
+   Otherwise, redisplay only mode lines that are garbaged.  Value is
+   the number of windows whose mode lines were redisplayed.  */
 
-static void
+static int
+redisplay_mode_lines (window, force)
+     Lisp_Object window;
+     int force;
+{
+  int nwindows = 0;
+  
+  while (!NILP (window))
+    {
+      struct window *w = XWINDOW (window);
+      
+      if (WINDOWP (w->hchild))
+       nwindows += redisplay_mode_lines (w->hchild, force);
+      else if (WINDOWP (w->vchild))
+       nwindows += redisplay_mode_lines (w->vchild, force);
+      else if (force
+              || FRAME_GARBAGED_P (XFRAME (w->frame))
+              || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
+       {
+         Lisp_Object old_selected_frame;
+         struct text_pos lpoint;
+         struct buffer *old = current_buffer;
+
+         /* Set the window's buffer for the mode line display.  */
+         SET_TEXT_POS (lpoint, PT, PT_BYTE);
+         set_buffer_internal_1 (XBUFFER (w->buffer));
+         
+         /* Point refers normally to the selected window.  For any
+            other window, set up appropriate value.  */
+         if (!EQ (window, selected_window))
+           {
+             struct text_pos pt;
+             
+             SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
+             if (CHARPOS (pt) < BEGV)
+               TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
+             else if (CHARPOS (pt) > (ZV - 1))
+               TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
+             else
+               TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
+           }
+
+         /* Temporarily set up the selected frame.  */
+         old_selected_frame = selected_frame;
+         selected_frame = w->frame;
+
+         /* Display mode lines.  */
+         clear_glyph_matrix (w->desired_matrix);
+         if (display_mode_lines (w))
+           {
+             ++nwindows;
+             w->must_be_updated_p = 1;
+           }
+
+         /* Restore old settings.  */
+         selected_frame = old_selected_frame;
+         set_buffer_internal_1 (old);
+         TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
+       }
+
+      window = w->next;
+    }
+
+  return nwindows;
+}
+
+
+/* Display the mode and/or top line of window W.  Value is the number
+   of mode lines displayed.  */
+
+static int
 display_mode_lines (w)
      struct window *w;
 {
+  int n = 0;
+  
   /* These will be set while the mode line specs are processed.  */
   line_number_displayed = 0;
   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);
+      ++n;
+    }
   
   if (WINDOW_WANTS_HEADER_LINE_P (w))
-    display_mode_line (w, HEADER_LINE_FACE_ID,
-                      current_buffer->header_line_format);
+    {
+      display_mode_line (w, HEADER_LINE_FACE_ID,
+                        current_buffer->header_line_format);
+      ++n;
+    }
+
+  return n;
 }
 
 
@@ -11800,9 +12553,10 @@ decode_mode_spec_coding (coding_system, buf, eol_flag)
   /* The EOL conversion we are using.  */
   Lisp_Object eoltype;
 
-  val = coding_system;
+  val = Fget (coding_system, Qcoding_system);
+  eoltype = Qnil;
 
-  if (NILP (val))              /* Not yet decided.  */
+  if (!VECTORP (val))          /* Not yet decided.  */
     {
       if (multibyte)
        *buf++ = '-';
@@ -11816,13 +12570,6 @@ decode_mode_spec_coding (coding_system, buf, eol_flag)
 
       eolvalue = Fget (coding_system, Qeol_type);
 
-      while (!NILP (val) && SYMBOLP (val))
-       {
-         val = Fget (val, Qcoding_system);
-         if (NILP (eolvalue))
-           eolvalue = Fget (val, Qeol_type);
-       }
-
       if (multibyte)
        *buf++ = XFASTINT (XVECTOR (val)->contents[1]);
 
@@ -11853,9 +12600,8 @@ decode_mode_spec_coding (coding_system, buf, eol_flag)
       else if (INTEGERP (eoltype)
               && CHAR_VALID_P (XINT (eoltype), 0))
        {
-         unsigned char work[4];
-
-         eol_str_len = CHAR_STRING (XINT (eoltype), work, eol_str);
+         eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
+         eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
        }
       else
        {
@@ -11879,7 +12625,7 @@ static char lots_of_dashes[] = "------------------------------------------------
 static char *
 decode_mode_spec (w, c, field_width, precision)
      struct window *w;
-     register char c;
+     register int c;
      int field_width, precision;
 {
   Lisp_Object obj;
@@ -12001,7 +12747,8 @@ decode_mode_spec (w, c, field_width, precision)
          w->base_line_pos = Qnil;
 
        /* If the buffer is very big, don't waste time.  */
-       if (BUF_ZV (b) - BUF_BEGV (b) > line_number_display_limit)
+       if (INTEGERP (Vline_number_display_limit)
+           && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
          {
            w->base_line_pos = Qnil;
            w->base_line_number = Qnil;
@@ -12088,7 +12835,8 @@ decode_mode_spec (w, c, field_width, precision)
          while (pad-- > 0)
            *p++ = ' ';
          *p++ = '?';
-         *p = '?';
+         *p++ = '?';
+         *p = '\0';
          return decode_mode_spec_buf;
        }
       }
@@ -12375,6 +13123,8 @@ display_string (string, lisp_string, face_string, face_string_pos,
                start, it, field_width, precision, max_x, multibyte)
      unsigned char *string;
      Lisp_Object lisp_string;
+     Lisp_Object face_string;
+     int face_string_pos;
      int start;
      struct it *it;
      int field_width, precision, max_x;
@@ -12536,6 +13286,7 @@ invisible_p (propval, list)
      Lisp_Object list;
 {
   register Lisp_Object tail, proptail;
+  
   for (tail = list; CONSP (tail); tail = XCDR (tail))
     {
       register Lisp_Object tem;
@@ -12545,22 +13296,25 @@ invisible_p (propval, list)
       if (CONSP (tem) && EQ (propval, XCAR (tem)))
        return 1;
     }
+  
   if (CONSP (propval))
-    for (proptail = propval; CONSP (proptail);
-        proptail = XCDR (proptail))
-      {
-       Lisp_Object propelt;
-       propelt = XCAR (proptail);
-       for (tail = list; CONSP (tail); tail = XCDR (tail))
-         {
-           register Lisp_Object tem;
-           tem = XCAR (tail);
-           if (EQ (propelt, tem))
-             return 1;
-           if (CONSP (tem) && EQ (propelt, XCAR (tem)))
-             return 1;
-         }
-      }
+    {
+      for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
+       {
+         Lisp_Object propelt;
+         propelt = XCAR (proptail);
+         for (tail = list; CONSP (tail); tail = XCDR (tail))
+           {
+             register Lisp_Object tem;
+             tem = XCAR (tail);
+             if (EQ (propelt, tem))
+               return 1;
+             if (CONSP (tem) && EQ (propelt, XCAR (tem)))
+               return 1;
+           }
+       }
+    }
+  
   return 0;
 }
 
@@ -12626,6 +13380,7 @@ syms_of_xdisp ()
   defsubr (&Sdump_glyph_row);
   defsubr (&Sdump_tool_bar_row);
   defsubr (&Strace_redisplay_toggle);
+  defsubr (&Strace_to_stderr);
 #endif
 
   staticpro (&Qmenu_bar_update_hook);
@@ -12646,12 +13401,12 @@ syms_of_xdisp ()
   staticpro (&Qinhibit_point_motion_hooks);
   Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
 
+  QCdata = intern (":data");
+  staticpro (&QCdata);
   Qdisplay = intern ("display");
   staticpro (&Qdisplay);
   Qspace_width = intern ("space-width");
   staticpro (&Qspace_width);
-  Qheight = intern ("height");
-  staticpro (&Qheight);
   Qraise = intern ("raise");
   staticpro (&Qraise);
   Qspace = intern ("space");
@@ -12666,8 +13421,6 @@ syms_of_xdisp ()
   staticpro (&Qalign_to);
   QCalign_to = intern (":align-to");
   staticpro (&QCalign_to);
-  Qwidth = intern ("width");
-  staticpro (&Qwidth);
   Qrelative_width = intern ("relative-width");
   staticpro (&Qrelative_width);
   QCrelative_width = intern (":relative-width");
@@ -12688,6 +13441,8 @@ syms_of_xdisp ()
   staticpro (&Qtrailing_whitespace);
   Qimage = intern ("image");
   staticpro (&Qimage);
+  Qmessage_truncate_lines = intern ("message-truncate-lines");
+  staticpro (&Qmessage_truncate_lines);
 
   last_arrow_position = Qnil;
   last_arrow_string = Qnil;
@@ -12702,6 +13457,9 @@ syms_of_xdisp ()
   staticpro (&echo_area_buffer[0]);
   staticpro (&echo_area_buffer[1]);
 
+  Vmessages_buffer_name = build_string ("*Messages*");
+  staticpro (&Vmessages_buffer_name);
+  
   DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
     "Non-nil means highlight trailing whitespace.\n\
 The face used for trailing whitespace is `trailing-whitespace'.");
@@ -12732,7 +13490,9 @@ If that fails to bring point back on frame, point is centered instead.\n\
 If this is zero, point is always centered after it moves off frame.");
 
   DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
-    "*Scroll up to this many lines, to bring point back on screen.");
+    "*Scroll up to this many lines, to bring point back on screen.\n\
+A value of zero means to scroll the text to center point vertically\n\
+in the window.");
   scroll_conservatively = 0;
 
   DEFVAR_INT ("scroll-margin", &scroll_margin,
@@ -12754,13 +13514,14 @@ of the top or bottom of the window.");
     "*Non-nil means use inverse video for the mode line.");
   mode_line_inverse_video = 1;
 
-  DEFVAR_INT ("line-number-display-limit", &line_number_display_limit,
+  DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
     "*Maximum buffer size for which line number should be displayed.\n\
 If the buffer is bigger than this, the line number does not appear\n\
-in the mode line.");
-  line_number_display_limit = 1000000;
+in the mode line.  A value of nil means no limit.");
+  Vline_number_display_limit = Qnil;
 
-  DEFVAR_INT ("line-number-display-limit-width", &line_number_display_limit_width,
+  DEFVAR_INT ("line-number-display-limit-width",
+             &line_number_display_limit_width,
     "*Maximum line width (in characters) for line number display.\n\
 If the average length of the lines near point is bigger than this, then the\n\
 line number may be omitted from the mode line.");
@@ -12863,6 +13624,26 @@ 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);
+  
+  DEFVAR_BOOL ("cursor-in-non-selected-windows",
+              &cursor_in_non_selected_windows,
+    "*Non-nil means display a hollow cursor in non-selected windows.\n\
+Nil means don't display a cursor there.");
+  cursor_in_non_selected_windows = 1;
+  
+  DEFVAR_BOOL ("automatic-hscrolling", &automatic_hscrolling_p,
+    "*Non-nil means scroll the display automatically to make point visible.");
+  automatic_hscrolling_p = 1;
+  
+  DEFVAR_LISP ("image-types", &Vimage_types,
+    "List of supported image types.\n\
+Each element of the list is a symbol for a supported image type.");
+  Vimage_types = Qnil;
+  
+  DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
+    "If non-nil, messages are truncated instead of resizing the echo area.\n\
+Bind this around calls to `message' to let it take effect.");
+  message_truncate_lines = 0;
 }