No specific user configuration
[platform/upstream/bash.git] / lib / readline / display.c
index 2a482ef..4df1f73 100644 (file)
@@ -1,24 +1,24 @@
 /* display.c -- readline redisplay facility. */
 
-/* Copyright (C) 1987-2006 Free Software Foundation, Inc.
+/* Copyright (C) 1987-2013 Free Software Foundation, Inc.
 
-   This file is part of the GNU Readline Library, a library for
-   reading lines of text with interactive input and history editing.
+   This file is part of the GNU Readline Library (Readline), a library    
+   for reading lines of text with interactive input and history editing.
 
-   The GNU Readline Library is free software; you can redistribute it
-   and/or modify it under the terms of the GNU General Public License
-   as published by the Free Software Foundation; either version 2, or
+   Readline is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
 
-   The GNU Readline Library is distributed in the hope that it will be
-   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
-   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   Readline is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
-   The GNU General Public License is often shipped with GNU software, and
-   is generally kept in a file called COPYING or LICENSE.  If you do not
-   have a copy of the license, write to the Free Software Foundation,
-   59 Temple Place, Suite 330, Boston, MA 02111 USA. */
+   You should have received a copy of the GNU General Public License
+   along with Readline.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
 #define READLINE_LIBRARY
 
 #if defined (HAVE_CONFIG_H)
 
 #include <stdio.h>
 
+#ifdef __MSDOS__
+#  include <pc.h>
+#endif
+
 /* System-specific feature definitions and include files. */
 #include "rldefs.h"
 #include "rlmbutil.h"
@@ -63,17 +67,42 @@ static void update_line PARAMS((char *, char *, int, int, int, int));
 static void space_to_eol PARAMS((int));
 static void delete_chars PARAMS((int));
 static void insert_some_chars PARAMS((char *, int, int));
+static void open_some_spaces PARAMS((int));
 static void cr PARAMS((void));
 
+/* State of visible and invisible lines. */
+struct line_state
+  {
+    char *line;
+    int *lbreaks;
+    int lbsize;
 #if defined (HANDLE_MULTIBYTE)
-static int _rl_col_width PARAMS((const char *, int, int));
-static int *_rl_wrapped_line;
-#else
-#  define _rl_col_width(l, s, e)       (((e) <= (s)) ? 0 : (e) - (s))
+    int *wrapped_line;
+    int wbsize;
 #endif
+  };
 
-static int *inv_lbreaks, *vis_lbreaks;
-static int inv_lbsize, vis_lbsize;
+/* The line display buffers.  One is the line currently displayed on
+   the screen.  The other is the line about to be displayed. */
+static struct line_state line_state_array[2];
+static struct line_state *line_state_visible = &line_state_array[0];
+static struct line_state *line_state_invisible = &line_state_array[1];
+static int line_structures_initialized = 0;
+
+/* Backwards-compatible names. */
+#define inv_lbreaks    (line_state_invisible->lbreaks)
+#define inv_lbsize     (line_state_invisible->lbsize)
+#define vis_lbreaks    (line_state_visible->lbreaks)
+#define vis_lbsize     (line_state_visible->lbsize)
+
+#define visible_line   (line_state_visible->line)
+#define invisible_line (line_state_invisible->line)
+
+#if defined (HANDLE_MULTIBYTE)
+static int _rl_col_width PARAMS((const char *, int, int, int));
+#else
+#  define _rl_col_width(l, s, e, f)    (((e) <= (s)) ? 0 : (e) - (s))
+#endif
 
 /* Heuristic used to decide whether it is faster to move from CUR to NEW
    by backing up or outputting a carriage return and moving forward.  CUR
@@ -83,7 +112,7 @@ static int inv_lbsize, vis_lbsize;
 /* _rl_last_c_pos is an absolute cursor position in multibyte locales and a
    buffer index in others.  This macro is used when deciding whether the
    current cursor position is in the middle of a prompt string containing
-   invisible characters. */
+   invisible characters.  XXX - might need to take `modmark' into account. */
 #define PROMPT_ENDING_INDEX \
   ((MB_CUR_MAX > 1 && rl_byte_oriented == 0) ? prompt_physical_chars : prompt_last_invisible+1)
   
@@ -141,6 +170,8 @@ int _rl_last_v_pos = 0;
 
 static int cpos_adjusted;
 static int cpos_buffer_position;
+static int displaying_prompt_first_line;
+static int prompt_multibyte_chars;
 
 /* Number of lines currently on screen minus 1. */
 int _rl_vis_botlin = 0;
@@ -150,13 +181,9 @@ int _rl_vis_botlin = 0;
    doing horizontal scrolling.  It shifts in thirds of a screenwidth. */
 static int last_lmargin;
 
-/* The line display buffers.  One is the line currently displayed on
-   the screen.  The other is the line about to be displayed. */
-static char *visible_line = (char *)NULL;
-static char *invisible_line = (char *)NULL;
-
 /* A buffer for `modeline' messages. */
-static char msg_buf[128];
+static char *msg_buf = 0;
+static int msg_bufsiz = 0;
 
 /* Non-zero forces the redisplay even if we thought it was unnecessary. */
 static int forced_display;
@@ -195,6 +222,10 @@ static int prompt_last_screen_line;
 
 static int prompt_physical_chars;
 
+/* set to a non-zero value by rl_redisplay if we are marking modified history
+   lines and the current line is so marked. */
+static int modmark;
+
 /* Variables to save and restore prompt and display information. */
 
 /* These are getting numerous enough that it's time to create a struct. */
@@ -208,6 +239,18 @@ static int saved_local_length;
 static int saved_invis_chars_first_line;
 static int saved_physical_chars;
 
+/* Return a character indicating the editing mode, for use in the prompt. */
+static int
+prompt_modechar ()
+{
+  if (rl_editing_mode == emacs_mode)
+    return '@';
+  else if (_rl_keymap == vi_insertion_keymap)
+    return '+';                /* vi insert mode */
+  else
+    return ':';                /* vi command mode */
+}
+
 /* Expand the prompt string S and return the number of visible
    characters in *LP, if LP is not null.  This is currently more-or-less
    a placeholder for expansion.  LIP, if non-null is a place to store the
@@ -234,7 +277,16 @@ expand_prompt (pmt, lp, lip, niflp, vlp)
   /* Short-circuit if we can. */
   if ((MB_CUR_MAX <= 1 || rl_byte_oriented) && strchr (pmt, RL_PROMPT_START_IGNORE) == 0)
     {
-      r = savestring (pmt);
+      if (pmt == rl_prompt && _rl_show_mode_in_prompt)
+        {
+          l = strlen (pmt);
+          r = (char *)xmalloc (l + 2);
+          r[0] = prompt_modechar ();
+          strcpy (r + 1, pmt);
+        }
+      else
+       r = savestring (pmt);
+
       if (lp)
        *lp = strlen (r);
       if (lip)
@@ -247,13 +299,20 @@ expand_prompt (pmt, lp, lip, niflp, vlp)
     }
 
   l = strlen (pmt);
-  r = ret = (char *)xmalloc (l + 1);
+  r = ret = (char *)xmalloc (l + 2);
+
+  rl = physchars = 0;  /* move up here so mode show can set them */
+  if (pmt == rl_prompt && _rl_show_mode_in_prompt)
+    {
+      *r++ = prompt_modechar ();
+      rl = physchars = 1;
+    }
 
   invfl = 0;   /* invisible chars in first line of prompt */
   invflset = 0;        /* we only want to set invfl once */
 
   igstart = 0;
-  for (rl = ignoring = last = ninvis = physchars = 0, p = pmt; p && *p; p++)
+  for (ignoring = last = ninvis = 0, p = pmt; p && *p; p++)
     {
       /* This code strips the invisible character string markers
         RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE */
@@ -282,8 +341,13 @@ expand_prompt (pmt, lp, lip, niflp, vlp)
                *r++ = *p++;
              if (!ignoring)
                {
+                 /* rl ends up being assigned to prompt_visible_length,
+                    which is the number of characters in the buffer that
+                    contribute to characters on the screen, which might
+                    not be the same as the number of physical characters
+                    on the screen in the presence of multibyte characters */
                  rl += ind - pind;
-                 physchars += _rl_col_width (pmt, pind, ind);
+                 physchars += _rl_col_width (pmt, pind, ind, 0);
                }
              else
                ninvis += ind - pind;
@@ -337,6 +401,12 @@ _rl_strip_prompt (pmt)
   return ret;
 }
 
+void
+_rl_reset_prompt ()
+{
+  rl_visible_prompt_length = rl_expand_prompt (rl_prompt);
+}
+
 /*
  * Expand the prompt string into the various display components, if
  * necessary.
@@ -443,13 +513,21 @@ init_line_structures (minsize)
     {
       /* should be enough. */
       inv_lbsize = vis_lbsize = 256;
-      inv_lbreaks = (int *)xmalloc (inv_lbsize * sizeof (int));
-      vis_lbreaks = (int *)xmalloc (vis_lbsize * sizeof (int));
+
 #if defined (HANDLE_MULTIBYTE)
-      _rl_wrapped_line = (int *)xmalloc (vis_lbsize * sizeof (int));
+      line_state_visible->wbsize = vis_lbsize;
+      line_state_visible->wrapped_line = (int *)xmalloc (line_state_visible->wbsize * sizeof (int));
+
+      line_state_invisible->wbsize = inv_lbsize;
+      line_state_invisible->wrapped_line = (int *)xmalloc (line_state_invisible->wbsize * sizeof (int));
 #endif
+
+      inv_lbreaks = (int *)xmalloc (inv_lbsize * sizeof (int));
+      vis_lbreaks = (int *)xmalloc (vis_lbsize * sizeof (int));
       inv_lbreaks[0] = vis_lbreaks[0] = 0;
     }
+
+  line_structures_initialized = 1;
 }
   
 /* Basic redisplay algorithm. */
@@ -459,7 +537,7 @@ rl_redisplay ()
   register int in, out, c, linenum, cursor_linenum;
   register char *line;
   int inv_botlin, lb_botlin, lb_linenum, o_cpos;
-  int newlines, lpos, temp, modmark, n0, num;
+  int newlines, lpos, temp, n0, num, prompt_lines_estimate;
   char *prompt_this_line;
 #if defined (HANDLE_MULTIBYTE)
   wchar_t wc;
@@ -469,13 +547,18 @@ rl_redisplay ()
   int _rl_wrapped_multicolumn = 0;
 #endif
 
-  if (!readline_echoing_p)
+  if (_rl_echoing_p == 0)
     return;
 
+  /* Block keyboard interrupts because this function manipulates global
+     data structures. */
+  _rl_block_sigint ();  
+  RL_SETSTATE (RL_STATE_REDISPLAYING);
+
   if (!rl_display_prompt)
     rl_display_prompt = "";
 
-  if (invisible_line == 0 || vis_lbreaks == 0)
+  if (line_structures_initialized == 0)
     {
       init_line_structures (0);
       rl_on_new_line ();
@@ -484,6 +567,8 @@ rl_redisplay ()
   /* Draw the line into the buffer. */
   cpos_buffer_position = -1;
 
+  prompt_multibyte_chars = prompt_visible_length - prompt_physical_chars;
+
   line = invisible_line;
   out = inv_botlin = 0;
 
@@ -561,26 +646,14 @@ rl_redisplay ()
       wrap_offset = prompt_invis_chars_first_line = 0;
     }
 
-#if defined (HANDLE_MULTIBYTE)
 #define CHECK_INV_LBREAKS() \
       do { \
        if (newlines >= (inv_lbsize - 2)) \
          { \
            inv_lbsize *= 2; \
            inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
-           _rl_wrapped_line = (int *)xrealloc (_rl_wrapped_line, inv_lbsize * sizeof (int)); \
          } \
       } while (0)
-#else
-#define CHECK_INV_LBREAKS() \
-      do { \
-       if (newlines >= (inv_lbsize - 2)) \
-         { \
-           inv_lbsize *= 2; \
-           inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
-         } \
-      } while (0)
-#endif /* HANDLE_MULTIBYTE */
 
 #if defined (HANDLE_MULTIBYTE)   
 #define CHECK_LPOS() \
@@ -592,10 +665,14 @@ rl_redisplay ()
              { \
                inv_lbsize *= 2; \
                inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
-               _rl_wrapped_line = (int *)xrealloc (_rl_wrapped_line, inv_lbsize * sizeof (int)); \
              } \
            inv_lbreaks[++newlines] = out; \
-           _rl_wrapped_line[newlines] = _rl_wrapped_multicolumn; \
+           if (newlines >= (line_state_invisible->wbsize - 1)) \
+             { \
+               line_state_invisible->wbsize *= 2; \
+               line_state_invisible->wrapped_line = (int *)xrealloc (line_state_invisible->wrapped_line, line_state_invisible->wbsize * sizeof(int)); \
+             } \
+           line_state_invisible->wrapped_line[newlines] = _rl_wrapped_multicolumn; \
            lpos = 0; \
          } \
       } while (0)
@@ -618,21 +695,20 @@ rl_redisplay ()
 
   /* inv_lbreaks[i] is where line i starts in the buffer. */
   inv_lbreaks[newlines = 0] = 0;
-#if 0
-  lpos = out - wrap_offset;
-#else
   lpos = prompt_physical_chars + modmark;
-#endif
 
 #if defined (HANDLE_MULTIBYTE)
-  memset (_rl_wrapped_line, 0, vis_lbsize);
+  memset (line_state_invisible->wrapped_line, 0, line_state_invisible->wbsize * sizeof (int));
   num = 0;
 #endif
 
   /* prompt_invis_chars_first_line is the number of invisible characters in
      the first physical line of the prompt.
      wrap_offset - prompt_invis_chars_first_line is the number of invis
-     chars on the second line. */
+     chars on the second (or, more generally, last) line. */
+
+  /* This is zero-based, used to set the newlines */
+  prompt_lines_estimate = lpos / _rl_screenwidth;
 
   /* what if lpos is already >= _rl_screenwidth before we start drawing the
      contents of the command line? */
@@ -647,13 +723,13 @@ rl_redisplay ()
          prompts that exceed two physical lines?
          Additional logic fix from Edward Catmur <ed@catmur.co.uk> */
 #if defined (HANDLE_MULTIBYTE)
-      if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+      if (MB_CUR_MAX > 1 && rl_byte_oriented == 0 && prompt_multibyte_chars > 0)
        {
          n0 = num;
           temp = local_prompt_len;
           while (num < temp)
            {
-             z = _rl_col_width  (local_prompt, n0, num);
+             z = _rl_col_width  (local_prompt, n0, num, 1);
              if (z > _rl_screenwidth)
                {
                  num = _rl_find_prev_mbchar (local_prompt, num, MB_FIND_ANY);
@@ -670,14 +746,15 @@ rl_redisplay ()
        temp = ((newlines + 1) * _rl_screenwidth);
 
       /* Now account for invisible characters in the current line. */
-      temp += ((local_prompt_prefix == 0) ? ((newlines == 0) ? prompt_invis_chars_first_line
-                                                            : ((newlines == 1) ? wrap_offset : 0))
-                                         : ((newlines == 0) ? wrap_offset :0));
-             
+      /* XXX - this assumes that the invisible characters may be split, but only
+        between the first and the last lines. */
+      temp += (newlines == 0) ? prompt_invis_chars_first_line
+                             : ((newlines == prompt_lines_estimate) ? wrap_offset : prompt_invis_chars_first_line);
+
       inv_lbreaks[++newlines] = temp;
 #if defined (HANDLE_MULTIBYTE)
-      if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
-       lpos -= _rl_col_width (local_prompt, n0, num);
+      if (MB_CUR_MAX > 1 && rl_byte_oriented == 0 && prompt_multibyte_chars > 0)
+       lpos -= _rl_col_width (local_prompt, n0, num, 1);
       else
 #endif
        lpos -= _rl_screenwidth;
@@ -696,6 +773,7 @@ rl_redisplay ()
   if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
     {
       memset (&ps, 0, sizeof (mbstate_t));
+      /* XXX - what if wc_bytes ends up <= 0? check for MB_INVALIDCH */
       wc_bytes = mbrtowc (&wc, rl_line_buffer, rl_end, &ps);
     }
   else
@@ -723,7 +801,7 @@ rl_redisplay ()
            break;                      /* Found '\0' */
          else
            {
-             temp = wcwidth (wc);
+             temp = WCWIDTH (wc);
              wc_width = (temp >= 0) ? temp : 1;
            }
        }
@@ -856,6 +934,7 @@ rl_redisplay ()
       if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
        {
          in += wc_bytes;
+         /* XXX - what if wc_bytes ends up <= 0? check for MB_INVALIDCH */
          wc_bytes = mbrtowc (&wc, rl_line_buffer + in, rl_end - in, &ps);
        }
       else
@@ -886,7 +965,7 @@ rl_redisplay ()
   /* If we can move the cursor up and down, then use multiple lines,
      otherwise, let long lines display in a single terminal line, and
      horizontally scroll it. */
-
+  displaying_prompt_first_line = 1;
   if (_rl_horizontal_scroll_mode == 0 && _rl_term_up && *_rl_term_up)
     {
       int nleft, pos, changed_screen_line, tx;
@@ -921,6 +1000,11 @@ rl_redisplay ()
 #define VIS_LINE(line) ((line) > _rl_vis_botlin) ? "" : VIS_CHARS(line)
 #define INV_LINE(line) (invisible_line + inv_lbreaks[line])
 
+#define OLD_CPOS_IN_PROMPT() (cpos_adjusted == 0 && \
+                       _rl_last_c_pos != o_cpos && \
+                       _rl_last_c_pos > wrap_offset && \
+                       o_cpos < prompt_last_invisible)
+
          /* For each line in the buffer, do the updating display. */
          for (linenum = 0; linenum <= inv_botlin; linenum++)
            {
@@ -935,7 +1019,7 @@ rl_redisplay ()
                 take invisible characters into account, since _rl_last_c_pos
                 is an absolute cursor position in a multibyte locale.  See
                 if compensating here is the right thing, or if we have to
-                change update_line itself.  There is one case in which
+                change update_line itself.  There are several cases in which
                 update_line adjusts _rl_last_c_pos itself (so it can pass
                 _rl_move_cursor_relative accurate values); it communicates
                 this back by setting cpos_adjusted.  If we assume that
@@ -943,11 +1027,7 @@ rl_redisplay ()
                 time update_line is called, then we can assume in our
                 calculations that o_cpos does not need to be adjusted by
                 wrap_offset. */
-             if (linenum == 0 && (MB_CUR_MAX > 1 && rl_byte_oriented == 0) &&
-                 cpos_adjusted == 0 &&
-                 _rl_last_c_pos != o_cpos &&
-                 _rl_last_c_pos > wrap_offset &&
-                 o_cpos < prompt_last_invisible)
+             if (linenum == 0 && (MB_CUR_MAX > 1 && rl_byte_oriented == 0) && OLD_CPOS_IN_PROMPT())
                _rl_last_c_pos -= prompt_invis_chars_first_line;        /* XXX - was wrap_offset */
              else if (linenum == prompt_last_screen_line && prompt_physical_chars > _rl_screenwidth &&
                        (MB_CUR_MAX > 1 && rl_byte_oriented == 0) &&
@@ -974,6 +1054,19 @@ rl_redisplay ()
                  if (nleft)
                    _rl_clear_to_eol (nleft);
                }
+#if 0
+             /* This segment is intended to handle the case where the prompt
+                has invisible characters on the second line and the new line
+                to be displayed needs to clear the rest of the old characters
+                out (e.g., when printing the i-search prompt).  In general,
+                the case of the new line being shorter than the old.
+                Incomplete */
+             else if (linenum == prompt_last_screen_line &&
+                      prompt_physical_chars > _rl_screenwidth &&
+                      wrap_offset != prompt_invis_chars_first_line &&
+                      _rl_last_c_pos == out &&
+#endif
+
 
              /* Since the new first line is now visible, save its length. */
              if (linenum == 0)
@@ -1029,11 +1122,14 @@ rl_redisplay ()
              if (_rl_term_cr)
                tputs (_rl_term_cr, 1, _rl_output_character_function);
 #endif
+             if (modmark)
+               _rl_output_some_chars ("*", 1);
+
              _rl_output_some_chars (local_prompt, nleft);
              if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
-               _rl_last_c_pos = _rl_col_width (local_prompt, 0, nleft) - wrap_offset;
+               _rl_last_c_pos = _rl_col_width (local_prompt, 0, nleft, 1) - wrap_offset + modmark;
              else
-               _rl_last_c_pos = nleft;
+               _rl_last_c_pos = nleft + modmark;
            }
 
          /* Where on that line?  And where does that line start
@@ -1055,7 +1151,7 @@ rl_redisplay ()
            {
              /* TX == new physical cursor position in multibyte locale. */
              if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
-               tx = _rl_col_width (&visible_line[pos], 0, nleft) - visible_wrap_offset;
+               tx = _rl_col_width (&visible_line[pos], 0, nleft, 1) - visible_wrap_offset;
              else
                tx = nleft;
              if (tx >= 0 && _rl_last_c_pos > tx)
@@ -1123,6 +1219,8 @@ rl_redisplay ()
       else
        lmargin = last_lmargin;
 
+      displaying_prompt_first_line = lmargin < nleft;
+
       /* If the first character on the screen isn't the first character
         in the display line, indicate this with a special character. */
       if (lmargin > 0)
@@ -1136,9 +1234,11 @@ rl_redisplay ()
       if (t < out)
        line[t - 1] = '>';
 
-      if (!rl_display_fixed || forced_display || lmargin != last_lmargin)
+      if (rl_display_fixed == 0 || forced_display || lmargin != last_lmargin)
        {
          forced_display = 0;
+         o_cpos = _rl_last_c_pos;
+         cpos_adjusted = 0;
          update_line (&visible_line[last_lmargin],
                       &invisible_line[lmargin],
                       0,
@@ -1146,12 +1246,16 @@ rl_redisplay ()
                       _rl_screenwidth + (lmargin ? 0 : wrap_offset),
                       0);
 
+         if ((MB_CUR_MAX > 1 && rl_byte_oriented == 0) &&
+               displaying_prompt_first_line && OLD_CPOS_IN_PROMPT())
+           _rl_last_c_pos -= prompt_invis_chars_first_line;    /* XXX - was wrap_offset */
+
          /* If the visible new line is shorter than the old, but the number
             of invisible characters is greater, and we are at the end of
             the new line, we need to clear to eol. */
          t = _rl_last_c_pos - M_OFFSET (lmargin, wrap_offset);
          if ((M_OFFSET (lmargin, wrap_offset) > visible_wrap_offset) &&
-             (_rl_last_c_pos == out) &&
+             (_rl_last_c_pos == out) && displaying_prompt_first_line &&
              t < visible_first_line_len)
            {
              nleft = _rl_screenwidth - t;
@@ -1169,17 +1273,10 @@ rl_redisplay ()
 
   /* Swap visible and non-visible lines. */
   {
-    char *vtemp = visible_line;
-    int *itemp = vis_lbreaks, ntemp = vis_lbsize;
+    struct line_state *vtemp = line_state_visible;
 
-    visible_line = invisible_line;
-    invisible_line = vtemp;
-
-    vis_lbreaks = inv_lbreaks;
-    inv_lbreaks = itemp;
-
-    vis_lbsize = inv_lbsize;
-    inv_lbsize = ntemp;
+    line_state_visible = line_state_invisible;
+    line_state_invisible = vtemp;
 
     rl_display_fixed = 0;
     /* If we are displaying on a single line, and last_lmargin is > 0, we
@@ -1190,6 +1287,9 @@ rl_redisplay ()
     else
       visible_wrap_offset = wrap_offset;
   }
+
+  RL_UNSETSTATE (RL_STATE_REDISPLAYING);
+  _rl_release_sigint ();
 }
 
 /* PWP: update_line() is based on finding the middle difference of each
@@ -1217,6 +1317,7 @@ update_line (old, new, current_line, omax, nmax, inv_botlin)
   int temp, lendiff, wsatend, od, nd, twidth, o_cpos;
   int current_invis_chars;
   int col_lendiff, col_temp;
+  int bytes_to_insert;
 #if defined (HANDLE_MULTIBYTE)
   mbstate_t ps_new, ps_old;
   int new_offset, old_offset;
@@ -1243,10 +1344,10 @@ update_line (old, new, current_line, omax, nmax, inv_botlin)
          size_t ret;
 
          /* This fixes only double-column characters, but if the wrapped
-            character comsumes more than three columns, spaces will be
+            character consumes more than three columns, spaces will be
             inserted in the string buffer. */
-         if (_rl_wrapped_line[current_line] > 0)
-           _rl_clear_to_eol (_rl_wrapped_line[current_line]);
+         if (current_line < line_state_visible->wbsize && line_state_visible->wrapped_line[current_line] > 0)
+           _rl_clear_to_eol (line_state_visible->wrapped_line[current_line]);
 
          memset (&ps, 0, sizeof (mbstate_t));
          ret = mbrtowc (&wc, new, MB_CUR_MAX, &ps);
@@ -1258,11 +1359,11 @@ update_line (old, new, current_line, omax, nmax, inv_botlin)
          else if (MB_NULLWCH (ret))
            tempwidth = 0;
          else
-           tempwidth = wcwidth (wc);
+           tempwidth = WCWIDTH (wc);
 
          if (tempwidth > 0)
            {
-             int count;
+             int count, i;
              bytes = ret;
              for (count = 0; count < bytes; count++)
                putc (new[count], rl_outstream);
@@ -1273,10 +1374,13 @@ update_line (old, new, current_line, omax, nmax, inv_botlin)
              if (ret != 0 && bytes != 0)
                {
                  if (MB_INVALIDCH (ret))
-                   memmove (old+bytes, old+1, strlen (old+1));
-                 else
-                   memmove (old+bytes, old+ret, strlen (old+ret));
+                   ret = 1;
+                 memmove (old+bytes, old+ret, strlen (old+ret));
                  memcpy (old, new, bytes);
+                 /* Fix up indices if we copy data from one line to another */
+                 omax += bytes - ret;
+                 for (i = current_line+1; i <= inv_botlin+1; i++)
+                   vis_lbreaks[i] += bytes - ret;
                }
            }
          else
@@ -1310,8 +1414,9 @@ update_line (old, new, current_line, omax, nmax, inv_botlin)
       /* See if the old line is a subset of the new line, so that the
         only change is adding characters. */
       temp = (omax < nmax) ? omax : nmax;
-      if (memcmp (old, new, temp) == 0)
+      if (memcmp (old, new, temp) == 0)                /* adding at the end */
        {
+         new_offset = old_offset = temp;
          ofd = old + temp;
          nfd = new + temp;
        }
@@ -1322,6 +1427,8 @@ update_line (old, new, current_line, omax, nmax, inv_botlin)
 
          if (omax == nmax && STREQN (new, old, omax))
            {
+             old_offset = omax;
+             new_offset = nmax;
              ofd = old + omax;
              nfd = new + nmax;
            }
@@ -1334,6 +1441,7 @@ update_line (old, new, current_line, omax, nmax, inv_botlin)
                {
                  old_offset = _rl_find_next_mbchar (old, old_offset, 1, MB_FIND_ANY);
                  new_offset = _rl_find_next_mbchar (new, new_offset, 1, MB_FIND_ANY);
+
                  ofd = old + old_offset;
                  nfd = new + new_offset;
                }
@@ -1357,6 +1465,27 @@ update_line (old, new, current_line, omax, nmax, inv_botlin)
   if (ofd == oe && nfd == ne)
     return;
 
+#if defined (HANDLE_MULTIBYTE)
+  if (MB_CUR_MAX > 1 && rl_byte_oriented == 0 && _rl_utf8locale)
+    {
+      wchar_t wc;
+      mbstate_t ps = { 0 };
+      int t;
+
+      /* If the first character in the difference is a zero-width character,
+        assume it's a combining character and back one up so the two base
+        characters no longer compare equivalently. */
+      t = mbrtowc (&wc, ofd, MB_CUR_MAX, &ps);
+      if (t > 0 && UNICODE_COMBINING_CHAR (wc) && WCWIDTH (wc) == 0)
+       {
+         old_offset = _rl_find_prev_mbchar (old, ofd - old, MB_FIND_ANY);
+         new_offset = _rl_find_prev_mbchar (new, nfd - new, MB_FIND_ANY);
+         ofd = old + old_offset;       /* equal by definition */
+         nfd = new + new_offset;
+       }
+    }
+#endif
+
   wsatend = 1;                 /* flag for trailing whitespace */
 
 #if defined (HANDLE_MULTIBYTE)
@@ -1364,6 +1493,7 @@ update_line (old, new, current_line, omax, nmax, inv_botlin)
     {
       ols = old + _rl_find_prev_mbchar (old, oe - old, MB_FIND_ANY);
       nls = new + _rl_find_prev_mbchar (new, ne - new, MB_FIND_ANY);
+
       while ((ols > ofd) && (nls > nfd))
        {
          memset (&ps_old, 0, sizeof (mbstate_t));
@@ -1463,50 +1593,51 @@ update_line (old, new, current_line, omax, nmax, inv_botlin)
 #else
       tputs (_rl_term_cr, 1, _rl_output_character_function);
 #endif
+      if (modmark)
+       _rl_output_some_chars ("*", 1);
       _rl_output_some_chars (local_prompt, lendiff);
       if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
        {
          /* We take wrap_offset into account here so we can pass correct
             information to _rl_move_cursor_relative. */
-         _rl_last_c_pos = _rl_col_width (local_prompt, 0, lendiff) - wrap_offset;
+         _rl_last_c_pos = _rl_col_width (local_prompt, 0, lendiff, 1) - wrap_offset + modmark;
          cpos_adjusted = 1;
        }
       else
-       _rl_last_c_pos = lendiff;
+       _rl_last_c_pos = lendiff + modmark;
     }
 
   o_cpos = _rl_last_c_pos;
 
   /* When this function returns, _rl_last_c_pos is correct, and an absolute
-     cursor postion in multibyte mode, but a buffer index when not in a
+     cursor position in multibyte mode, but a buffer index when not in a
      multibyte locale. */
   _rl_move_cursor_relative (od, old);
-#if 1
+
 #if defined (HANDLE_MULTIBYTE)
   /* We need to indicate that the cursor position is correct in the presence of
      invisible characters in the prompt string.  Let's see if setting this when
      we make sure we're at the end of the drawn prompt string works. */
-  if (current_line == 0 && MB_CUR_MAX > 1 && rl_byte_oriented == 0 && 
+  if (current_line == 0 && MB_CUR_MAX > 1 && rl_byte_oriented == 0 &&
       (_rl_last_c_pos > 0 || o_cpos > 0) &&
       _rl_last_c_pos == prompt_physical_chars)
     cpos_adjusted = 1;
 #endif
-#endif
 
   /* if (len (new) > len (old))
-     lendiff == difference in buffer
-     col_lendiff == difference on screen
+     lendiff == difference in buffer (bytes)
+     col_lendiff == difference on screen (columns)
      When not using multibyte characters, these are equal */
   lendiff = (nls - nfd) - (ols - ofd);
   if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
-    col_lendiff = _rl_col_width (new, nfd - new, nls - new) - _rl_col_width (old, ofd - old, ols - old);
+    col_lendiff = _rl_col_width (new, nfd - new, nls - new, 1) - _rl_col_width (old, ofd - old, ols - old, 1);
   else
     col_lendiff = lendiff;
 
   /* If we are changing the number of invisible characters in a line, and
      the spot of first difference is before the end of the invisible chars,
      lendiff needs to be adjusted. */
-  if (current_line == 0 && !_rl_horizontal_scroll_mode &&
+  if (current_line == 0 && /* !_rl_horizontal_scroll_mode && */
       current_invis_chars != visible_wrap_offset)
     {
       if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
@@ -1521,13 +1652,21 @@ update_line (old, new, current_line, omax, nmax, inv_botlin)
        }
     }
 
+  /* We use temp as a count of the number of bytes from the first difference
+     to the end of the new line.  col_temp is the corresponding number of
+     screen columns.  A `dumb' update moves to the spot of first difference
+     and writes TEMP bytes. */
   /* Insert (diff (len (old), len (new)) ch. */
   temp = ne - nfd;
   if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
-    col_temp = _rl_col_width (new, nfd - new, ne - new);
+    col_temp = _rl_col_width (new, nfd - new, ne - new, 1);
   else
     col_temp = temp;
 
+  /* how many bytes from the new line buffer to write to the display */
+  bytes_to_insert = nls - nfd;
+
+  /* col_lendiff > 0 if we are adding characters to the line */
   if (col_lendiff > 0) /* XXX - was lendiff */
     {
       /* Non-zero if we're increasing the number of lines. */
@@ -1541,11 +1680,11 @@ update_line (old, new, current_line, omax, nmax, inv_botlin)
       if (lendiff < 0)
        {
          _rl_output_some_chars (nfd, temp);
-         _rl_last_c_pos += _rl_col_width (nfd, 0, temp);
+         _rl_last_c_pos += col_temp;   /* XXX - was _rl_col_width (nfd, 0, temp, 1); */
          /* If nfd begins before any invisible characters in the prompt,
             adjust _rl_last_c_pos to account for wrap_offset and set
             cpos_adjusted to let the caller know. */
-         if (current_line == 0 && wrap_offset && ((nfd - new) <= prompt_last_invisible))
+         if (current_line == 0 && displaying_prompt_first_line && wrap_offset && ((nfd - new) <= prompt_last_invisible))
            {
              _rl_last_c_pos -= wrap_offset;
              cpos_adjusted = 1;
@@ -1562,49 +1701,60 @@ update_line (old, new, current_line, omax, nmax, inv_botlin)
             _rl_horizontal_scroll_mode == 1, inserting the characters with
             _rl_term_IC or _rl_term_ic will screw up the screen because of the
             invisible characters.  We need to just draw them. */
-         if (*ols && (!_rl_horizontal_scroll_mode || _rl_last_c_pos > 0 ||
-                       lendiff <= prompt_visible_length || !current_invis_chars))
+         /* The same thing happens if we're trying to draw before the last
+            invisible character in the prompt string or we're increasing the
+            number of invisible characters in the line and we're not drawing
+            the entire prompt string. */
+         if (*ols && ((_rl_horizontal_scroll_mode &&
+                       _rl_last_c_pos == 0 &&
+                       lendiff > prompt_visible_length &&
+                       current_invis_chars > 0) == 0) &&
+                     (((MB_CUR_MAX > 1 && rl_byte_oriented == 0) &&
+                       current_line == 0 && wrap_offset &&
+                       ((nfd - new) <= prompt_last_invisible) &&
+                       (col_lendiff < prompt_visible_length)) == 0) &&
+                     (visible_wrap_offset >= current_invis_chars))
            {
-             insert_some_chars (nfd, lendiff, col_lendiff);
-             _rl_last_c_pos += col_lendiff;
+             open_some_spaces (col_lendiff);
+             _rl_output_some_chars (nfd, bytes_to_insert);
+             if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+               _rl_last_c_pos += _rl_col_width (nfd, 0, bytes_to_insert, 1);
+             else
+               _rl_last_c_pos += bytes_to_insert;
            }
          else if ((MB_CUR_MAX == 1 || rl_byte_oriented != 0) && *ols == 0 && lendiff > 0)
            {
              /* At the end of a line the characters do not have to
                 be "inserted".  They can just be placed on the screen. */
-             /* However, this screws up the rest of this block, which
-                assumes you've done the insert because you can. */
-             _rl_output_some_chars (nfd, lendiff);
-             _rl_last_c_pos += col_lendiff;
+             _rl_output_some_chars (nfd, temp);
+             _rl_last_c_pos += col_temp;
+             return;
            }
-         else
+         else  /* just write from first difference to end of new line */
            {
              _rl_output_some_chars (nfd, temp);
              _rl_last_c_pos += col_temp;
-             /* If nfd begins before any invisible characters in the prompt,
-                adjust _rl_last_c_pos to account for wrap_offset and set
-                cpos_adjusted to let the caller know. */
-             if (current_line == 0 && wrap_offset && ((nfd - new) <= prompt_last_invisible))
+             /* If nfd begins before the last invisible character in the
+                prompt, adjust _rl_last_c_pos to account for wrap_offset
+                and set cpos_adjusted to let the caller know. */
+             if ((MB_CUR_MAX > 1 && rl_byte_oriented == 0) && current_line == 0 && displaying_prompt_first_line && wrap_offset && ((nfd - new) <= prompt_last_invisible))
                {
                  _rl_last_c_pos -= wrap_offset;
                  cpos_adjusted = 1;
                }
              return;
            }
-         /* Copy (new) chars to screen from first diff to last match. */
-         temp = nls - nfd;
-         if ((temp - lendiff) > 0)
+
+         if (bytes_to_insert > lendiff)
            {
-             _rl_output_some_chars (nfd + lendiff, temp - lendiff);
-            /* XXX -- this bears closer inspection.  Fixes a redisplay bug
-               reported against bash-3.0-alpha by Andreas Schwab involving
-               multibyte characters and prompt strings with invisible
-               characters, but was previously disabled. */
-             if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
-               twidth = _rl_col_width (nfd+lendiff, 0, temp-col_lendiff);
-             else
-               twidth = temp - lendiff;
-             _rl_last_c_pos += twidth;
+             /* If nfd begins before the last invisible character in the
+                prompt, adjust _rl_last_c_pos to account for wrap_offset
+                and set cpos_adjusted to let the caller know. */
+             if ((MB_CUR_MAX > 1 && rl_byte_oriented == 0) && current_line == 0 && displaying_prompt_first_line && wrap_offset && ((nfd - new) <= prompt_last_invisible))
+               {
+                 _rl_last_c_pos -= wrap_offset;
+                 cpos_adjusted = 1;
+               }
            }
        }
       else
@@ -1616,6 +1766,15 @@ update_line (old, new, current_line, omax, nmax, inv_botlin)
             char in the current line (which implies we just output some invisible
             characters) we need to adjust _rl_last_c_pos, since it represents
             a physical character position. */
+         if ((MB_CUR_MAX > 1 && rl_byte_oriented == 0) &&
+               current_line == prompt_last_screen_line && wrap_offset &&
+               displaying_prompt_first_line &&
+               wrap_offset != prompt_invis_chars_first_line &&
+               ((nfd-new) < (prompt_last_invisible-(current_line*_rl_screenwidth))))
+           {
+             _rl_last_c_pos -= wrap_offset - prompt_invis_chars_first_line;
+             cpos_adjusted = 1;
+           }
        }
     }
   else                         /* Delete characters from line. */
@@ -1627,32 +1786,52 @@ update_line (old, new, current_line, omax, nmax, inv_botlin)
             prompt string, don't bother.  It screws up the assumptions
             about what's on the screen. */
          if (_rl_horizontal_scroll_mode && _rl_last_c_pos == 0 &&
+             displaying_prompt_first_line &&
              -lendiff == visible_wrap_offset)
            col_lendiff = 0;
 
+         /* If we have moved lmargin and we're shrinking the line, we've
+            already moved the cursor to the first character of the new line,
+            so deleting -col_lendiff characters will mess up the cursor
+            position calculation */
+         if (_rl_horizontal_scroll_mode && displaying_prompt_first_line == 0 &&
+               col_lendiff && _rl_last_c_pos < -col_lendiff)
+           col_lendiff = 0;
+
          if (col_lendiff)
            delete_chars (-col_lendiff); /* delete (diff) characters */
 
-         /* Copy (new) chars to screen from first diff to last match */
-         temp = nls - nfd;
-         if (temp > 0)
+         /* Copy (new) chars to screen from first diff to last match,
+            overwriting what is there. */
+         if (bytes_to_insert > 0)
            {
              /* If nfd begins at the prompt, or before the invisible
                 characters in the prompt, we need to adjust _rl_last_c_pos
                 in a multibyte locale to account for the wrap offset and
                 set cpos_adjusted accordingly. */
-             _rl_output_some_chars (nfd, temp);
+             _rl_output_some_chars (nfd, bytes_to_insert);
              if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
                {
-                  _rl_last_c_pos += _rl_col_width (nfd, 0, temp);
-                  if (current_line == 0 && wrap_offset &&  ((nfd - new) <= prompt_last_invisible))
+                 _rl_last_c_pos += _rl_col_width (nfd, 0, bytes_to_insert, 1);
+                 if (current_line == 0 && wrap_offset &&
+                       displaying_prompt_first_line &&
+                       _rl_last_c_pos > wrap_offset &&
+                       ((nfd - new) <= prompt_last_invisible))
                    {
                      _rl_last_c_pos -= wrap_offset;
                      cpos_adjusted = 1;
                    }
                }
-              else
-                _rl_last_c_pos += temp;
+             else
+               _rl_last_c_pos += bytes_to_insert;
+
+             /* XXX - we only want to do this if we are at the end of the line
+                so we move there with _rl_move_cursor_relative */
+             if (_rl_horizontal_scroll_mode && ((oe-old) > (ne-new)))
+               {
+                 _rl_move_cursor_relative (ne-new, new);
+                 goto clear_rest_of_line;
+               }
            }
        }
       /* Otherwise, print over the existing material. */
@@ -1668,20 +1847,29 @@ update_line (old, new, current_line, omax, nmax, inv_botlin)
              _rl_last_c_pos += col_temp;               /* XXX */
              if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
                {
-                 if (current_line == 0 && wrap_offset &&  ((nfd - new) <= prompt_last_invisible))
+                 if (current_line == 0 && wrap_offset &&
+                       displaying_prompt_first_line &&
+                       _rl_last_c_pos > wrap_offset &&
+                       ((nfd - new) <= prompt_last_invisible))
                    {
                      _rl_last_c_pos -= wrap_offset;
                      cpos_adjusted = 1;
                    }
                }
            }
+clear_rest_of_line:
          lendiff = (oe - old) - (ne - new);
          if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
-           col_lendiff = _rl_col_width (old, 0, oe - old) - _rl_col_width (new, 0, ne - new);
+           col_lendiff = _rl_col_width (old, 0, oe - old, 1) - _rl_col_width (new, 0, ne - new, 1);
          else
            col_lendiff = lendiff;
 
-         if (col_lendiff)
+         /* If we've already printed over the entire width of the screen,
+            including the old material, then col_lendiff doesn't matter and
+            space_to_eol will insert too many spaces.  XXX - maybe we should
+            adjust col_lendiff based on the difference between _rl_last_c_pos
+            and _rl_screenwidth */
+         if (col_lendiff && ((MB_CUR_MAX == 1 || rl_byte_oriented) || (_rl_last_c_pos < _rl_screenwidth)))
            {     
              if (_rl_term_autowrap && current_line < inv_botlin)
                space_to_eol (col_lendiff);
@@ -1735,7 +1923,7 @@ rl_on_new_line_with_prompt ()
 
   l = strlen (prompt_last_line);
   if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
-    _rl_last_c_pos = _rl_col_width (prompt_last_line, 0, l);   /* XXX */
+    _rl_last_c_pos = _rl_col_width (prompt_last_line, 0, l, 1);        /* XXX */
   else
     _rl_last_c_pos = l;
 
@@ -1797,9 +1985,14 @@ _rl_move_cursor_relative (new, data)
   register int i;
   int woff;                    /* number of invisible chars on current line */
   int cpos, dpos;              /* current and desired cursor positions */
+  int adjust;
 
   woff = WRAP_OFFSET (_rl_last_v_pos, wrap_offset);
   cpos = _rl_last_c_pos;
+
+  if (cpos == 0 && cpos == new)
+    return;
+
 #if defined (HANDLE_MULTIBYTE)
   /* If we have multibyte characters, NEW is indexed by the buffer point in
      a multibyte string, but _rl_last_c_pos is the display position.  In
@@ -1808,15 +2001,38 @@ _rl_move_cursor_relative (new, data)
      as long as we are past them and they are counted by _rl_col_width. */
   if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
     {
-      dpos = _rl_col_width (data, 0, new);
+      adjust = 1;
+      /* Try to short-circuit common cases and eliminate a bunch of multibyte
+        character function calls. */
+      /* 1.  prompt string */
+      if (new == local_prompt_len && memcmp (data, local_prompt, new) == 0)
+       {
+         dpos = prompt_physical_chars;
+         cpos_adjusted = 1;
+         adjust = 0;
+       }
+      /* 2.  prompt_string + line contents */
+      else if (new > local_prompt_len && local_prompt && memcmp (data, local_prompt, local_prompt_len) == 0)
+       {
+         dpos = prompt_physical_chars + _rl_col_width (data, local_prompt_len, new, 1);
+         cpos_adjusted = 1;
+         adjust = 0;
+       }
+      else
+        dpos = _rl_col_width (data, 0, new, 1);
+
+      if (displaying_prompt_first_line == 0)
+       adjust = 0;
+
       /* Use NEW when comparing against the last invisible character in the
         prompt string, since they're both buffer indices and DPOS is a
         desired display position. */
-      if ((new > prompt_last_invisible) ||             /* XXX - don't use woff here */
-         (prompt_physical_chars > _rl_screenwidth &&
+      if (adjust && ((new > prompt_last_invisible) ||          /* XXX - don't use woff here */
+         (prompt_physical_chars >= _rl_screenwidth &&
           _rl_last_v_pos == prompt_last_screen_line &&
-          wrap_offset != woff &&
-          new > (prompt_last_invisible-_rl_screenwidth-wrap_offset)))
+          wrap_offset >= woff && dpos >= woff &&
+          new > (prompt_last_invisible-(_rl_screenwidth*_rl_last_v_pos)-wrap_offset))))
+          /* XXX last comparison might need to be >= */
        {
          dpos -= woff;
          /* Since this will be assigned to _rl_last_c_pos at the end (more
@@ -1926,9 +2142,18 @@ _rl_move_vert (to)
     }
   else
     {                  /* delta < 0 */
+#ifdef __DJGPP__
+      int row, col;
+
+      fflush (rl_outstream);
+      ScreenGetCursor (&row, &col);
+      ScreenSetCursor (row + delta, col);
+      i = -delta;
+#else
       if (_rl_term_up && *_rl_term_up)
        for (i = 0; i < -delta; i++)
          tputs (_rl_term_up, 1, _rl_output_character_function);
+#endif /* !__DJGPP__ */
     }
 
   _rl_last_v_pos = to;         /* Now TO is here */
@@ -2006,6 +2231,9 @@ rl_message (va_alist)
 #if defined (PREFER_VARARGS)
   char *format;
 #endif
+#if defined (HAVE_VSNPRINTF)
+  int bneed;
+#endif
 
 #if defined (PREFER_STDARG)
   va_start (args, format);
@@ -2014,11 +2242,28 @@ rl_message (va_alist)
   format = va_arg (args, char *);
 #endif
 
+  if (msg_buf == 0)
+    msg_buf = xmalloc (msg_bufsiz = 128);
+
 #if defined (HAVE_VSNPRINTF)
-  vsnprintf (msg_buf, sizeof (msg_buf) - 1, format, args);
+  bneed = vsnprintf (msg_buf, msg_bufsiz - 1, format, args);
+  if (bneed >= msg_bufsiz - 1)
+    {
+      msg_bufsiz = bneed + 1;
+      msg_buf = xrealloc (msg_buf, msg_bufsiz);
+      va_end (args);
+
+#if defined (PREFER_STDARG)
+      va_start (args, format);
+#else
+      va_start (args);
+      format = va_arg (args, char *);
+#endif
+      vsnprintf (msg_buf, msg_bufsiz - 1, format, args);
+    }
 #else
   vsprintf (msg_buf, format, args);
-  msg_buf[sizeof(msg_buf) - 1] = '\0'; /* overflow? */
+  msg_buf[msg_bufsiz - 1] = '\0';      /* overflow? */
 #endif
   va_end (args);
 
@@ -2027,6 +2272,12 @@ rl_message (va_alist)
       rl_save_prompt ();
       msg_saved_prompt = 1;
     }
+  else if (local_prompt != saved_local_prompt)
+    {
+      FREE (local_prompt);
+      FREE (local_prompt_prefix);
+      local_prompt = (char *)NULL;
+    }
   rl_display_prompt = msg_buf;
   local_prompt = expand_prompt (msg_buf, &prompt_visible_length,
                                         &prompt_last_invisible,
@@ -2043,8 +2294,11 @@ int
 rl_message (format, arg1, arg2)
      char *format;
 {
+  if (msg_buf == 0)
+    msg_buf = xmalloc (msg_bufsiz = 128);
+
   sprintf (msg_buf, format, arg1, arg2);
-  msg_buf[sizeof(msg_buf) - 1] = '\0'; /* overflow? */
+  msg_buf[msg_bufsiz - 1] = '\0';      /* overflow? */
 
   rl_display_prompt = msg_buf;
   if (saved_local_prompt == 0)
@@ -2052,6 +2306,12 @@ rl_message (format, arg1, arg2)
       rl_save_prompt ();
       msg_saved_prompt = 1;
     }
+  else if (local_prompt != saved_local_prompt)
+    {
+      FREE (local_prompt);
+      FREE (local_prompt_prefix);
+      local_prompt = (char *)NULL;
+    }
   local_prompt = expand_prompt (msg_buf, &prompt_visible_length,
                                         &prompt_last_invisible,
                                         &prompt_invis_chars_first_line,
@@ -2188,10 +2448,13 @@ void
 _rl_clear_to_eol (count)
      int count;
 {
+#ifndef __MSDOS__
   if (_rl_term_clreol)
     tputs (_rl_term_clreol, 1, _rl_output_character_function);
-  else if (count)
-    space_to_eol (count);
+  else
+#endif
+    if (count)
+      space_to_eol (count);
 }
 
 /* Clear to the end of the line using spaces.  COUNT is the minimum
@@ -2211,10 +2474,15 @@ space_to_eol (count)
 void
 _rl_clear_screen ()
 {
+#ifndef __DJGPP__
   if (_rl_term_clrpag)
     tputs (_rl_term_clrpag, 1, _rl_output_character_function);
   else
     rl_crlf ();
+#else
+  ScreenClear ();
+  ScreenSetCursor (0, 0);
+#endif /* __DJGPP__ */
 }
 
 /* Insert COUNT characters from STRING to the output stream at column COL. */
@@ -2223,48 +2491,47 @@ insert_some_chars (string, count, col)
      char *string;
      int count, col;
 {
-#if defined (__MSDOS__) || defined (__MINGW32__)
+  open_some_spaces (col);
   _rl_output_some_chars (string, count);
-#else
-  /* DEBUGGING */
-  if (MB_CUR_MAX == 1 || rl_byte_oriented)
-    if (count != col)
-      fprintf(stderr, "readline: debug: insert_some_chars: count (%d) != col (%d)\n", count, col);
+}
+
+/* Insert COL spaces, keeping the cursor at the same position.  We follow the
+   ncurses documentation and use either im/ei with explicit spaces, or IC/ic
+   by itself.  We assume there will either be ei or we don't need to use it. */
+static void
+open_some_spaces (col)
+     int col;
+{
+#if !defined (__MSDOS__) && !defined (__MINGW32__)
+  char *buffer;
+  register int i;
 
   /* If IC is defined, then we do not have to "enter" insert mode. */
   if (_rl_term_IC)
     {
-      char *buffer;
-
       buffer = tgoto (_rl_term_IC, 0, col);
       tputs (buffer, 1, _rl_output_character_function);
-      _rl_output_some_chars (string, count);
     }
-  else
+  else if (_rl_term_im && *_rl_term_im)
     {
-      register int i;
-
-      /* If we have to turn on insert-mode, then do so. */
-      if (_rl_term_im && *_rl_term_im)
-       tputs (_rl_term_im, 1, _rl_output_character_function);
-
-      /* If there is a special command for inserting characters, then
-        use that first to open up the space. */
-      if (_rl_term_ic && *_rl_term_ic)
-       {
-         for (i = col; i--; )
-           tputs (_rl_term_ic, 1, _rl_output_character_function);
-       }
-
-      /* Print the text. */
-      _rl_output_some_chars (string, count);
-
-      /* If there is a string to turn off insert mode, we had best use
-        it now. */
+      tputs (_rl_term_im, 1, _rl_output_character_function);
+      /* just output the desired number of spaces */
+      for (i = col; i--; )
+       _rl_output_character_function (' ');
+      /* If there is a string to turn off insert mode, use it now. */
       if (_rl_term_ei && *_rl_term_ei)
        tputs (_rl_term_ei, 1, _rl_output_character_function);
+      /* and move back the right number of spaces */
+      _rl_backspace (col);
     }
-#endif /* __MSDOS__ || __MINGW32__ */
+  else if (_rl_term_ic && *_rl_term_ic)
+    {
+      /* If there is a special command for inserting characters, then
+        use that first to open up the space. */
+      for (i = col; i--; )
+       tputs (_rl_term_ic, 1, _rl_output_character_function);
+    }
+#endif /* !__MSDOS__ && !__MINGW32__ */
 }
 
 /* Delete COUNT characters from the display line. */
@@ -2370,10 +2637,14 @@ _rl_redisplay_after_sigwinch ()
 {
   char *t;
 
-  /* Clear the current line and put the cursor at column 0.  Make sure
-     the right thing happens if we have wrapped to a new screen line. */
+  /* Clear the last line (assuming that the screen size change will result in
+     either more or fewer characters on that line only) and put the cursor at
+     column 0.  Make sure the right thing happens if we have wrapped to a new
+     screen line. */
   if (_rl_term_cr)
     {
+      _rl_move_vert (_rl_vis_botlin);
+
 #if defined (__MSDOS__)
       putc ('\r', rl_outstream);
 #else
@@ -2409,9 +2680,10 @@ _rl_redisplay_after_sigwinch ()
 void
 _rl_clean_up_for_exit ()
 {
-  if (readline_echoing_p)
+  if (_rl_echoing_p)
     {
-      _rl_move_vert (_rl_vis_botlin);
+      if (_rl_vis_botlin > 0)  /* minor optimization plus bug fix */
+       _rl_move_vert (_rl_vis_botlin);
       _rl_vis_botlin = 0;
       fflush (rl_outstream);
       rl_restart_output (1, 0);
@@ -2454,9 +2726,9 @@ _rl_current_display_line ()
    In the case of multibyte characters with stateful encoding, we have to
    scan from the beginning of the string to take the state into account. */
 static int
-_rl_col_width (str, start, end)
+_rl_col_width (str, start, end, flags)
      const char *str;
-     int start, end;
+     int start, end, flags;
 {
   wchar_t wc;
   mbstate_t ps;
@@ -2465,6 +2737,7 @@ _rl_col_width (str, start, end)
   if (end <= start)
     return 0;
   if (MB_CUR_MAX == 1 || rl_byte_oriented)
+    /* this can happen in some cases where it's inconvenient to check */
     return (end - start);
 
   memset (&ps, 0, sizeof (mbstate_t));
@@ -2472,6 +2745,20 @@ _rl_col_width (str, start, end)
   point = 0;
   max = end;
 
+  /* Try to short-circuit common cases.  The adjustment to remove wrap_offset
+     is done by the caller. */
+  /* 1.  prompt string */
+  if (flags && start == 0 && end == local_prompt_len && memcmp (str, local_prompt, local_prompt_len) == 0)
+    return (prompt_physical_chars + wrap_offset);
+  /* 2.  prompt string + line contents */
+  else if (flags && start == 0 && local_prompt_len > 0 && end > local_prompt_len && local_prompt && memcmp (str, local_prompt, local_prompt_len) == 0)
+    {
+      tmp = prompt_physical_chars + wrap_offset;
+      /* XXX - try to call ourselves recursively with non-prompt portion */
+      tmp += _rl_col_width (str, local_prompt_len, end, flags);
+      return (tmp);
+    }
+
   while (point < start)
     {
       tmp = mbrlen (str + point, max, &ps);
@@ -2525,7 +2812,7 @@ _rl_col_width (str, start, end)
        {
          point += tmp;
          max -= tmp;
-         tmp = wcwidth(wc);
+         tmp = WCWIDTH(wc);
          width += (tmp >= 0) ? tmp : 1;
        }
     }