Remove gdb workaround from readline/xfree.c
[deliverable/binutils-gdb.git] / readline / display.c
index 90443057974d4e5f6bff7343fd3016a96fd9c554..cfe816402a44ad832f05f351bfc904912feca74a 100644 (file)
@@ -1,6 +1,6 @@
 /* display.c -- readline redisplay facility. */
 
-/* Copyright (C) 1987-2009 Free Software Foundation, Inc.
+/* Copyright (C) 1987-2015 Free Software Foundation, Inc.
 
    This file is part of the GNU Readline Library (Readline), a library    
    for reading lines of text with interactive input and history editing.
@@ -42,7 +42,7 @@
 #include <stdio.h>
 
 #ifdef __MSDOS__
-# include <pc.h>
+#  include <pc.h>
 #endif
 
 /* System-specific feature definitions and include files. */
@@ -67,7 +67,14 @@ 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));
+static void redraw_prompt PARAMS((char *));
+
+/* Values for FLAGS */
+#define PMT_MULTILINE  0x01
+
+static char *expand_prompt PARAMS((char *, int, int *, int *, int *, int *));
 
 /* State of visible and invisible lines. */
 struct line_state
@@ -76,8 +83,8 @@ struct line_state
     int *lbreaks;
     int lbsize;
 #if defined (HANDLE_MULTIBYTE)
-    int *wrapped_line;
     int wbsize;
+    int *wrapped_line;
 #endif
   };
 
@@ -112,6 +119,8 @@ static int _rl_col_width PARAMS((const char *, int, int, int));
    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.  XXX - might need to take `modmark' into account. */
+/* XXX - only valid when tested against _rl_last_c_pos; buffer indices need
+   to use prompt_last_invisible directly. */
 #define PROMPT_ENDING_INDEX \
   ((MB_CUR_MAX > 1 && rl_byte_oriented == 0) ? prompt_physical_chars : prompt_last_invisible+1)
   
@@ -157,6 +166,16 @@ int _rl_want_redisplay = 0;
    This is usually pointing to rl_prompt. */
 char *rl_display_prompt = (char *)NULL;
 
+/* Variables used to include the editing mode in the prompt. */
+char *_rl_emacs_mode_str;
+int _rl_emacs_modestr_len;
+
+char *_rl_vi_ins_mode_str;
+int _rl_vi_ins_modestr_len;
+
+char *_rl_vi_cmd_mode_str;
+int _rl_vi_cmd_modestr_len;
+
 /* Pseudo-global variables declared here. */
 
 /* The visible cursor position.  If you print some text, adjust this. */
@@ -169,18 +188,22 @@ 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;
 
+static int _rl_inv_botlin = 0;
+
 /* Variables used only in this file. */
 /* The last left edge of text that was displayed.  This is used when
    doing horizontal scrolling.  It shifts in thirds of a screenwidth. */
 static int last_lmargin;
 
 /* 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;
@@ -236,6 +259,32 @@ static int saved_local_length;
 static int saved_invis_chars_first_line;
 static int saved_physical_chars;
 
+/* Return a string indicating the editing mode, for use in the prompt. */
+
+static char *
+prompt_modestr (lenp)
+     int *lenp;
+{
+  if (rl_editing_mode == emacs_mode)
+    {
+      if (lenp)
+       *lenp = _rl_emacs_mode_str ? _rl_emacs_modestr_len : RL_EMACS_MODESTR_DEFLEN;
+      return _rl_emacs_mode_str ? _rl_emacs_mode_str : RL_EMACS_MODESTR_DEFAULT;
+    }
+  else if (_rl_keymap == vi_insertion_keymap)
+    {
+      if (lenp)
+       *lenp = _rl_vi_ins_mode_str ? _rl_vi_ins_modestr_len : RL_VI_INS_MODESTR_DEFLEN;
+      return _rl_vi_ins_mode_str ? _rl_vi_ins_mode_str : RL_VI_INS_MODESTR_DEFAULT;            /* vi insert mode */
+    }
+  else
+    {
+      if (lenp)
+       *lenp = _rl_vi_cmd_mode_str ? _rl_vi_cmd_modestr_len : RL_VI_CMD_MODESTR_DEFLEN;
+      return _rl_vi_cmd_mode_str ? _rl_vi_cmd_mode_str : RL_VI_CMD_MODESTR_DEFAULT;            /* 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
@@ -251,18 +300,38 @@ static int saved_physical_chars;
    the returned string; all characters except those between \001 and
    \002 are assumed to be `visible'. */        
 
+/* Possible values for FLAGS:
+       PMT_MULTILINE   caller indicates that this is part of a multiline prompt
+*/
+
 static char *
-expand_prompt (pmt, lp, lip, niflp, vlp)
+expand_prompt (pmt, flags, lp, lip, niflp, vlp)
      char *pmt;
+     int flags;
      int *lp, *lip, *niflp, *vlp;
 {
-  char *r, *ret, *p, *igstart;
+  char *r, *ret, *p, *igstart, *nprompt, *ms;
   int l, rl, last, ignoring, ninvis, invfl, invflset, ind, pind, physchars;
+  int mlen;
+
+  /* We only expand the mode string for the last line of a multiline prompt
+     (a prompt with embedded newlines). */
+  ms = (((pmt == rl_prompt) ^ (flags & PMT_MULTILINE)) && _rl_show_mode_in_prompt) ? prompt_modestr (&mlen) : 0;
+  if (ms)
+    {
+      l = strlen (pmt);
+      nprompt = (char *)xmalloc (l + mlen + 1);
+      memcpy (nprompt, ms, mlen);
+      strcpy (nprompt + mlen, pmt);
+    }
+  else
+    nprompt = pmt;
 
   /* Short-circuit if we can. */
-  if ((MB_CUR_MAX <= 1 || rl_byte_oriented) && strchr (pmt, RL_PROMPT_START_IGNORE) == 0)
+  if ((MB_CUR_MAX <= 1 || rl_byte_oriented) && strchr (nprompt, RL_PROMPT_START_IGNORE) == 0)
     {
-      r = savestring (pmt);
+      r = (nprompt == pmt) ? savestring (pmt) : nprompt;
+
       if (lp)
        *lp = strlen (r);
       if (lip)
@@ -274,14 +343,15 @@ expand_prompt (pmt, lp, lip, niflp, vlp)
       return r;
     }
 
-  l = strlen (pmt);
+  l = strlen (nprompt);                        /* XXX */
   r = ret = (char *)xmalloc (l + 1);
 
+  rl = physchars = 0;  /* mode string now part of nprompt */
   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 = nprompt; p && *p; p++)
     {
       /* This code strips the invisible character string markers
         RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE */
@@ -303,8 +373,8 @@ expand_prompt (pmt, lp, lip, niflp, vlp)
 #if defined (HANDLE_MULTIBYTE)
          if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
            {
-             pind = p - pmt;
-             ind = _rl_find_next_mbchar (pmt, pind, 1, MB_FIND_NONZERO);
+             pind = p - nprompt;
+             ind = _rl_find_next_mbchar (nprompt, pind, 1, MB_FIND_NONZERO);
              l = ind - pind;
              while (l--)
                *r++ = *p++;
@@ -316,7 +386,7 @@ expand_prompt (pmt, lp, lip, niflp, vlp)
                     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, 0);
+                 physchars += _rl_col_width (nprompt, pind, ind, 0);
                }
              else
                ninvis += ind - pind;
@@ -355,6 +425,10 @@ expand_prompt (pmt, lp, lip, niflp, vlp)
     *niflp = invfl;
   if  (vlp)
     *vlp = physchars;
+
+  if (nprompt != pmt)
+    free (nprompt);
+
   return ret;
 }
 
@@ -366,10 +440,16 @@ _rl_strip_prompt (pmt)
 {
   char *ret;
 
-  ret = expand_prompt (pmt, (int *)NULL, (int *)NULL, (int *)NULL, (int *)NULL);
+  ret = expand_prompt (pmt, 0, (int *)NULL, (int *)NULL, (int *)NULL, (int *)NULL);
   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.
@@ -407,13 +487,13 @@ rl_expand_prompt (prompt)
     return (0);
 
   p = strrchr (prompt, '\n');
-  if (!p)
+  if (p == 0)
     {
       /* The prompt is only one logical line, though it might wrap. */
-      local_prompt = expand_prompt (prompt, &prompt_visible_length,
-                                           &prompt_last_invisible,
-                                           &prompt_invis_chars_first_line,
-                                           &prompt_physical_chars);
+      local_prompt = expand_prompt (prompt, 0, &prompt_visible_length,
+                                              &prompt_last_invisible,
+                                              &prompt_invis_chars_first_line,
+                                              &prompt_physical_chars);
       local_prompt_prefix = (char *)0;
       local_prompt_len = local_prompt ? strlen (local_prompt) : 0;
       return (prompt_visible_length);
@@ -422,14 +502,16 @@ rl_expand_prompt (prompt)
     {
       /* The prompt spans multiple lines. */
       t = ++p;
-      local_prompt = expand_prompt (p, &prompt_visible_length,
+      local_prompt = expand_prompt (p, PMT_MULTILINE,
+                                      &prompt_visible_length,
                                       &prompt_last_invisible,
                                       &prompt_invis_chars_first_line,
                                       &prompt_physical_chars);
       c = *t; *t = '\0';
       /* The portion of the prompt string up to and including the
         final newline is now null-terminated. */
-      local_prompt_prefix = expand_prompt (prompt, &prompt_prefix_length,
+      local_prompt_prefix = expand_prompt (prompt, PMT_MULTILINE,
+                                                  &prompt_prefix_length,
                                                   (int *)NULL,
                                                   (int *)NULL,
                                                   (int *)NULL);
@@ -450,6 +532,9 @@ init_line_structures (minsize)
 {
   register int n;
 
+  if (minsize <= _rl_screenwidth)      /* XXX - for gdb */
+    minsize = _rl_screenwidth + 1;
+
   if (invisible_line == 0)     /* initialize it */
     {
       if (line_size < minsize)
@@ -502,6 +587,7 @@ rl_redisplay ()
   int inv_botlin, lb_botlin, lb_linenum, o_cpos;
   int newlines, lpos, temp, n0, num, prompt_lines_estimate;
   char *prompt_this_line;
+  int mb_cur_max = MB_CUR_MAX;
 #if defined (HANDLE_MULTIBYTE)
   wchar_t wc;
   size_t wc_bytes;
@@ -526,6 +612,8 @@ rl_redisplay ()
       init_line_structures (0);
       rl_on_new_line ();
     }
+  else if (line_size <= _rl_screenwidth)
+    init_line_structures (_rl_screenwidth + 1);
 
   /* Draw the line into the buffer. */
   cpos_buffer_position = -1;
@@ -658,6 +746,8 @@ rl_redisplay ()
 
   /* inv_lbreaks[i] is where line i starts in the buffer. */
   inv_lbreaks[newlines = 0] = 0;
+  /* lpos is a physical cursor position, so it needs to be adjusted by the
+     number of invisible characters in the prompt, per line */
   lpos = prompt_physical_chars + modmark;
 
 #if defined (HANDLE_MULTIBYTE)
@@ -677,7 +767,19 @@ rl_redisplay ()
      contents of the command line? */
   while (lpos >= _rl_screenwidth)
     {
-      int z;
+      int z, p;
+      int nocorrect, wadjust;
+
+      nocorrect = 0;
+      /* Adjust depending on the invisible characters in the line.  We use a
+        heuristic based on experience: invisible characters nearly always
+        appear in the first and last lines of the prompt */
+      wadjust = (newlines == 0)
+                 ? prompt_invis_chars_first_line
+                 : ((newlines == prompt_lines_estimate)
+                       ? (wrap_offset - prompt_invis_chars_first_line)
+                       : 0);
+
       /* fix from Darin Johnson <darin@acuson.com> for prompt string with
          invisible characters that is longer than the screen width.  The
          prompt_invis_chars_first_line variable could be made into an array
@@ -686,20 +788,31 @@ 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 && prompt_multibyte_chars > 0)
+      if (mb_cur_max > 1 && rl_byte_oriented == 0 && prompt_multibyte_chars > 0)
        {
+         nocorrect = 1;
          n0 = num;
           temp = local_prompt_len;
           while (num < temp)
            {
-             z = _rl_col_width  (local_prompt, n0, num, 1);
+             /* This has to take invisible characters in the prompt into
+                account. */
+             z = _rl_col_width  (local_prompt, n0, num, 1) - wadjust;
              if (z > _rl_screenwidth)
                {
                  num = _rl_find_prev_mbchar (local_prompt, num, MB_FIND_ANY);
                  break;
                }
              else if (z == _rl_screenwidth)
-               break;
+               {
+                 /* If we are in the middle or at the end of a multibyte
+                    character, we want to move to the start, then find out
+                    where it ends so we know where to insert the newline.
+                    If this isn't a multibyte character, its the same as num++ */
+                 p = _rl_find_prev_mbchar (local_prompt, num, MB_FIND_ANY);
+                 num = _rl_find_next_mbchar (local_prompt, p, 1, MB_FIND_ANY);
+                 break;
+               }
              num++;
            }
           temp = num;
@@ -711,17 +824,18 @@ rl_redisplay ()
       /* Now account for invisible characters in the current line. */
       /* XXX - this assumes that the invisible characters may be split, but only
         between the first and the last lines. */
-      temp += ((local_prompt_prefix == 0) ? ((newlines == 0) ? prompt_invis_chars_first_line
-                                                            : ((newlines == prompt_lines_estimate) ? wrap_offset : prompt_invis_chars_first_line))
-                                         : ((newlines == 0) ? wrap_offset : 0));
-             
+      if (nocorrect == 0)
+       temp += wadjust;
+
       inv_lbreaks[++newlines] = temp;
 #if defined (HANDLE_MULTIBYTE)
-      if (MB_CUR_MAX > 1 && rl_byte_oriented == 0 && prompt_multibyte_chars > 0)
-       lpos -= _rl_col_width (local_prompt, n0, num, 1);
+      /* lpos is a physical cursor position, so it needs to take the invisible
+        characters into account. */
+      if (mb_cur_max > 1 && rl_byte_oriented == 0 && prompt_multibyte_chars > 0)
+       lpos -= _rl_col_width (local_prompt, n0, num, 1) - wadjust;
       else
 #endif
-       lpos -= _rl_screenwidth;
+       lpos -= _rl_screenwidth;        /* all physical cursor positions */
     }
 
   prompt_last_screen_line = newlines;
@@ -734,7 +848,7 @@ rl_redisplay ()
   lb_linenum = 0;
 #if defined (HANDLE_MULTIBYTE)
   in = 0;
-  if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+  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 */
@@ -750,7 +864,7 @@ rl_redisplay ()
       c = (unsigned char)rl_line_buffer[in];
 
 #if defined (HANDLE_MULTIBYTE)
-      if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+      if (mb_cur_max > 1 && rl_byte_oriented == 0)
        {
          if (MB_INVALIDCH (wc_bytes))
            {
@@ -765,7 +879,7 @@ rl_redisplay ()
            break;                      /* Found '\0' */
          else
            {
-             temp = wcwidth (wc);
+             temp = WCWIDTH (wc);
              wc_width = (temp >= 0) ? temp : 1;
            }
        }
@@ -859,7 +973,7 @@ rl_redisplay ()
       else
        {
 #if defined (HANDLE_MULTIBYTE)
-         if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+         if (mb_cur_max > 1 && rl_byte_oriented == 0)
            {
              register int i;
 
@@ -895,7 +1009,7 @@ rl_redisplay ()
        }
 
 #if defined (HANDLE_MULTIBYTE)
-      if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+      if (mb_cur_max > 1 && rl_byte_oriented == 0)
        {
          in += wc_bytes;
          /* XXX - what if wc_bytes ends up <= 0? check for MB_INVALIDCH */
@@ -913,7 +1027,7 @@ rl_redisplay ()
       lb_linenum = newlines;
     }
 
-  inv_botlin = lb_botlin = newlines;
+  inv_botlin = lb_botlin = _rl_inv_botlin = newlines;
   CHECK_INV_LBREAKS ();
   inv_lbreaks[newlines+1] = out;
   cursor_linenum = lb_linenum;
@@ -929,7 +1043,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;
@@ -943,9 +1057,11 @@ rl_redisplay ()
             not the first.  */
          if (out >= _rl_screenchars)
            {
-             if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+#if defined (HANDLE_MULTIBYTE)
+             if (mb_cur_max > 1 && rl_byte_oriented == 0)
                out = _rl_find_prev_mbchar (line, _rl_screenchars, MB_FIND_ANY);
              else
+#endif
                out = _rl_screenchars - 1;
            }
 
@@ -991,15 +1107,15 @@ 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) && OLD_CPOS_IN_PROMPT())
+             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) &&
+                       (mb_cur_max > 1 && rl_byte_oriented == 0) &&
                        cpos_adjusted == 0 &&
                        _rl_last_c_pos != o_cpos &&
                        _rl_last_c_pos > (prompt_last_invisible - _rl_screenwidth - prompt_invis_chars_first_line))
                _rl_last_c_pos -= (wrap_offset-prompt_invis_chars_first_line);
-                 
+
              /* If this is the line with the prompt, we might need to
                 compensate for invisible characters in the new line. Do
                 this only if there is not more than one new line (which
@@ -1011,7 +1127,7 @@ rl_redisplay ()
                  (wrap_offset > visible_wrap_offset) &&
                  (_rl_last_c_pos < visible_first_line_len))
                {
-                 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+                 if (mb_cur_max > 1 && rl_byte_oriented == 0)
                    nleft = _rl_screenwidth - _rl_last_c_pos;
                  else
                    nleft = _rl_screenwidth + wrap_offset - _rl_last_c_pos;
@@ -1063,7 +1179,7 @@ rl_redisplay ()
                 the physical cursor position on the screen stays the same,
                 but the buffer position needs to be adjusted to account
                 for invisible characters. */
-             if ((MB_CUR_MAX == 1 || rl_byte_oriented) && cursor_linenum == 0 && wrap_offset)
+             if ((mb_cur_max == 1 || rl_byte_oriented) && cursor_linenum == 0 && wrap_offset)
                _rl_last_c_pos += wrap_offset;
            }
 
@@ -1090,7 +1206,7 @@ rl_redisplay ()
                _rl_output_some_chars ("*", 1);
 
              _rl_output_some_chars (local_prompt, nleft);
-             if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+             if (mb_cur_max > 1 && rl_byte_oriented == 0)
                _rl_last_c_pos = _rl_col_width (local_prompt, 0, nleft, 1) - wrap_offset + modmark;
              else
                _rl_last_c_pos = nleft + modmark;
@@ -1114,7 +1230,7 @@ rl_redisplay ()
          if (wrap_offset && cursor_linenum == 0 && nleft < _rl_last_c_pos)
            {
              /* TX == new physical cursor position in multibyte locale. */
-             if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+             if (mb_cur_max > 1 && rl_byte_oriented == 0)
                tx = _rl_col_width (&visible_line[pos], 0, nleft, 1) - visible_wrap_offset;
              else
                tx = nleft;
@@ -1129,7 +1245,7 @@ rl_redisplay ()
             _rl_last_c_pos as an absolute cursor position, but moving to a
             point specified by a buffer position (NLEFT) that doesn't take
             invisible characters into account. */
-         if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+         if (mb_cur_max > 1 && rl_byte_oriented == 0)
            _rl_move_cursor_relative (nleft, &invisible_line[pos]);
          else if (nleft != _rl_last_c_pos)
            _rl_move_cursor_relative (nleft, &invisible_line[pos]);
@@ -1183,6 +1299,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)
@@ -1208,7 +1326,8 @@ rl_redisplay ()
                       _rl_screenwidth + (lmargin ? 0 : wrap_offset),
                       0);
 
-         if ((MB_CUR_MAX > 1 && rl_byte_oriented == 0) && OLD_CPOS_IN_PROMPT())
+         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
@@ -1216,7 +1335,7 @@ rl_redisplay ()
             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;
@@ -1278,6 +1397,8 @@ 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;
+  int mb_cur_max = MB_CUR_MAX;
 #if defined (HANDLE_MULTIBYTE)
   mbstate_t ps_new, ps_old;
   int new_offset, old_offset;
@@ -1288,7 +1409,7 @@ update_line (old, new, current_line, omax, nmax, inv_botlin)
      the exact cursor position and cut-and-paste with certain terminal
      emulators.  In this calculation, TEMP is the physical screen
      position of the cursor. */
-  if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+  if (mb_cur_max > 1 && rl_byte_oriented == 0)
     temp = _rl_last_c_pos;
   else
     temp = _rl_last_c_pos - WRAP_OFFSET (_rl_last_v_pos, visible_wrap_offset);
@@ -1296,7 +1417,7 @@ update_line (old, new, current_line, omax, nmax, inv_botlin)
        && _rl_last_v_pos == current_line - 1)
     {
 #if defined (HANDLE_MULTIBYTE)
-      if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+      if (mb_cur_max > 1 && rl_byte_oriented == 0)
        {
          wchar_t wc;
          mbstate_t ps;
@@ -1304,13 +1425,13 @@ 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 (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);
+         ret = mbrtowc (&wc, new, mb_cur_max, &ps);
          if (MB_INVALIDCH (ret))
            {
              tempwidth = 1;
@@ -1319,7 +1440,7 @@ 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)
            {
@@ -1330,7 +1451,7 @@ update_line (old, new, current_line, omax, nmax, inv_botlin)
              _rl_last_c_pos = tempwidth;
              _rl_last_v_pos++;
              memset (&ps, 0, sizeof (mbstate_t));
-             ret = mbrtowc (&wc, old, MB_CUR_MAX, &ps);
+             ret = mbrtowc (&wc, old, mb_cur_max, &ps);
              if (ret != 0 && bytes != 0)
                {
                  if (MB_INVALIDCH (ret))
@@ -1339,7 +1460,7 @@ update_line (old, new, current_line, omax, nmax, inv_botlin)
                  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++)
+                 for (i = current_line+1; i <= inv_botlin+1; i++)
                    vis_lbreaks[i] += bytes - ret;
                }
            }
@@ -1369,13 +1490,14 @@ update_line (old, new, current_line, omax, nmax, inv_botlin)
       
   /* Find first difference. */
 #if defined (HANDLE_MULTIBYTE)
-  if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+  if (mb_cur_max > 1 && rl_byte_oriented == 0)
     {
       /* 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)                /* adding at the end */
        {
+         new_offset = old_offset = temp;
          ofd = old + temp;
          nfd = new + temp;
        }
@@ -1386,6 +1508,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;
            }
@@ -1398,6 +1522,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;
                }
@@ -1421,13 +1546,35 @@ 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)
-  if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+  if (mb_cur_max > 1 && rl_byte_oriented == 0)
     {
       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));
@@ -1481,14 +1628,14 @@ update_line (old, new, current_line, omax, nmax, inv_botlin)
     {
       if (*ols)                        /* don't step past the NUL */
        {
-         if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+         if (mb_cur_max > 1 && rl_byte_oriented == 0)
            ols = old + _rl_find_next_mbchar (old, ols - old, 1, MB_FIND_ANY);
          else
            ols++;
        }
       if (*nls)
        {
-         if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+         if (mb_cur_max > 1 && rl_byte_oriented == 0)
            nls = new + _rl_find_next_mbchar (new, nls - new, 1, MB_FIND_ANY);
          else
            nls++;
@@ -1500,7 +1647,12 @@ update_line (old, new, current_line, omax, nmax, inv_botlin)
   if (_rl_last_v_pos != current_line)
     {
       _rl_move_vert (current_line);
-      if ((MB_CUR_MAX == 1 || rl_byte_oriented) && current_line == 0 && visible_wrap_offset)
+      /* We have moved up to a new screen line.  This line may or may not have
+         invisible characters on it, but we do our best to recalculate
+         visible_wrap_offset based on what we know. */
+      if (current_line == 0)
+       visible_wrap_offset = prompt_invis_chars_first_line;    /* XXX */
+      if ((mb_cur_max == 1 || rl_byte_oriented) && current_line == 0 && visible_wrap_offset)
        _rl_last_c_pos += visible_wrap_offset;
     }
 
@@ -1511,16 +1663,31 @@ update_line (old, new, current_line, omax, nmax, inv_botlin)
      string, then redraw the entire prompt string.  We can only do this
      reliably if the terminal supports a `cr' capability.
 
+     This can also happen if the prompt string has changed, and the first
+     difference in the line is in the middle of the prompt string, after a
+     sequence of invisible characters (worst case) and before the end of
+     the prompt.  In this case, we have to redraw the entire prompt string
+     so that the entire sequence of invisible characters is drawn.  We need
+     to handle the worst case, when the difference is after (or in the middle
+     of) a sequence of invisible characters that changes the text color and
+     before the sequence that restores the text color to normal.  Then we have
+     to make sure that the lines still differ -- if they don't, we can
+     return immediately.
+
      This is not an efficiency hack -- there is a problem with redrawing
      portions of the prompt string if they contain terminal escape
      sequences (like drawing the `unbold' sequence without a corresponding
      `bold') that manifests itself on certain terminals. */
 
   lendiff = local_prompt_len;
+  if (lendiff > nmax)
+    lendiff = nmax;
   od = ofd - old;      /* index of first difference in visible line */
+  nd = nfd - new;      /* nd, od are buffer indexes */
   if (current_line == 0 && !_rl_horizontal_scroll_mode &&
       _rl_term_cr && lendiff > prompt_visible_length && _rl_last_c_pos > 0 &&
-      od >= lendiff && _rl_last_c_pos < PROMPT_ENDING_INDEX)
+      (((od > 0 || nd > 0) && (od <= prompt_last_invisible || nd <= prompt_last_invisible)) ||
+               ((od >= lendiff) && _rl_last_c_pos < PROMPT_ENDING_INDEX)))
     {
 #if defined (__MSDOS__)
       putc ('\r', rl_outstream);
@@ -1530,7 +1697,7 @@ update_line (old, new, current_line, omax, nmax, inv_botlin)
       if (modmark)
        _rl_output_some_chars ("*", 1);
       _rl_output_some_chars (local_prompt, lendiff);
-      if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+      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. */
@@ -1539,32 +1706,68 @@ update_line (old, new, current_line, omax, nmax, inv_botlin)
        }
       else
        _rl_last_c_pos = lendiff + modmark;
+
+      /* Now if we have printed the prompt string because the first difference
+        was within the prompt, see if we need to recompute where the lines
+        differ.  Check whether where we are now is past the last place where
+        the old and new lines are the same and short-circuit now if we are. */
+      if ((od <= prompt_last_invisible || nd <= prompt_last_invisible) &&
+          omax == nmax &&
+         lendiff > (ols-old) && lendiff > (nls-new))
+       return;
+
+      /* XXX - we need to fix up our calculations if we are now past the
+        old ofd/nfd and the prompt length (or line length) has changed.
+        We punt on the problem and do a dumb update.  We'd like to be able
+        to just output the prompt from the beginning of the line up to the
+        first difference, but you don't know the number of invisible
+        characters in that case.
+        This needs a lot of work to be efficient. */
+      if ((od <= prompt_last_invisible || nd <= prompt_last_invisible))
+       {
+         nfd = new + lendiff;  /* number of characters we output above */
+         nd = lendiff;
+
+         /* Do a dumb update and return */
+         temp = ne - nfd;
+         if (temp > 0)
+           {
+             _rl_output_some_chars (nfd, temp);
+             if (mb_cur_max > 1 && rl_byte_oriented == 0)
+               _rl_last_c_pos += _rl_col_width (new, nd, ne - new, 1);
+             else
+               _rl_last_c_pos += temp;
+           }
+         if (nmax < omax)
+           goto clear_rest_of_line;    /* XXX */
+         else
+           return;
+       }
     }
 
   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)
+  if (mb_cur_max > 1 && rl_byte_oriented == 0)
     col_lendiff = _rl_col_width (new, nfd - new, nls - new, 1) - _rl_col_width (old, ofd - old, ols - old, 1);
   else
     col_lendiff = lendiff;
@@ -1572,10 +1775,10 @@ update_line (old, new, current_line, omax, nmax, inv_botlin)
   /* 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)
+      if (mb_cur_max > 1 && rl_byte_oriented == 0)
        {
          lendiff += visible_wrap_offset - current_invis_chars;
          col_lendiff += visible_wrap_offset - current_invis_chars;
@@ -1587,13 +1790,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)
+  if (mb_cur_max > 1 && rl_byte_oriented == 0)
     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. */
@@ -1607,11 +1818,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, 1);
+         _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;
@@ -1636,63 +1847,48 @@ update_line (old, new, current_line, omax, nmax, inv_botlin)
                        _rl_last_c_pos == 0 &&
                        lendiff > prompt_visible_length &&
                        current_invis_chars > 0) == 0) &&
-                     (((MB_CUR_MAX > 1 && rl_byte_oriented == 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;
-           }
-#if 0          /* XXX - for now */
-         else if ((MB_CUR_MAX > 1 && rl_byte_oriented == 0) && _rl_last_c_pos == 0 && wrap_offset && (nfd-new) <= prompt_last_invisible && col_lendiff < prompt_visible_length && visible_wrap_offset >= current_invis_chars)
-           {
-             _rl_output_some_chars (nfd, 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;
            }
-#endif
-         else if ((MB_CUR_MAX == 1 || rl_byte_oriented != 0) && *ols == 0 && lendiff > 0)
+         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 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 && wrap_offset && ((nfd - new) <= prompt_last_invisible))
+             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, 1);
-             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 && wrap_offset && ((nfd - new) <= prompt_last_invisible))
+             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;
@@ -1708,10 +1904,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) &&
+         /* The current_line*rl_screenwidth+prompt_invis_chars_first_line is a
+            crude attempt to compute how far into the new line buffer we are.
+            It doesn't work well in the face of multibyte characters and needs
+            to be rethought. XXX */
+         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))))
+               ((nfd-new) < (prompt_last_invisible-(current_line*_rl_screenwidth+prompt_invis_chars_first_line))))
            {
              _rl_last_c_pos -= wrap_offset - prompt_invis_chars_first_line;
              cpos_adjusted = 1;
@@ -1727,32 +1928,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);
-             if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+             _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, 1);
-                 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;
+               _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. */
@@ -1766,31 +1987,31 @@ update_line (old, new, current_line, omax, nmax, inv_botlin)
                 set cpos_adjusted accordingly. */
              _rl_output_some_chars (nfd, temp);
              _rl_last_c_pos += col_temp;               /* XXX */
-             if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+             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)
+         if (mb_cur_max > 1 && rl_byte_oriented == 0)
            col_lendiff = _rl_col_width (old, 0, oe - old, 1) - _rl_col_width (new, 0, ne - new, 1);
          else
            col_lendiff = lendiff;
 
-#if 0
-         if (col_lendiff)
-#else
          /* 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)))
-#endif
+         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);
@@ -1816,6 +2037,34 @@ rl_on_new_line ()
   return 0;
 }
 
+/* Clear all screen lines occupied by the current readline line buffer
+   (visible line) */
+int
+rl_clear_visible_line ()
+{
+  int curr_line;
+
+  /* Make sure we move to column 0 so we clear the entire line */
+#if defined (__MSDOS__)
+  putc ('\r', rl_outstream);
+#else
+  tputs (_rl_term_cr, 1, _rl_output_character_function);
+#endif
+  _rl_last_c_pos = 0;
+
+  /* Move to the last screen line of the current visible line */
+  _rl_move_vert (_rl_vis_botlin);
+
+  /* And erase screen lines going up to line 0 (first visible line) */
+  for (curr_line = _rl_last_v_pos; curr_line >= 0; curr_line--)
+    {
+      _rl_move_vert (curr_line);
+      _rl_clear_to_eol (0);
+    }
+
+  return 0;
+}
+
 /* Tell the update routines that we have moved onto a new line with the
    prompt already displayed.  Code originally from the version of readline
    distributed with CLISP.  rl_expand_prompt must have already been called
@@ -1893,11 +2142,25 @@ rl_forced_update_display ()
   return 0;
 }
 
+/* Redraw only the last line of a multi-line prompt. */
+void
+rl_redraw_prompt_last_line ()
+{
+  char *t;
+
+  t = strrchr (rl_display_prompt, '\n');
+  if (t)
+    redraw_prompt (++t);
+  else
+    rl_forced_update_display ();
+}
+
 /* Move the cursor from _rl_last_c_pos to NEW, which are buffer indices.
    (Well, when we don't have multibyte characters, _rl_last_c_pos is a
    buffer index.)
    DATA is the contents of the screen line of interest; i.e., where
-   the movement is being done. */
+   the movement is being done.
+   DATA is always the visible line or the invisible line */
 void
 _rl_move_cursor_relative (new, data)
      int new;
@@ -1907,6 +2170,8 @@ _rl_move_cursor_relative (new, data)
   int woff;                    /* number of invisible chars on current line */
   int cpos, dpos;              /* current and desired cursor positions */
   int adjust;
+  int in_invisline;
+  int mb_cur_max = MB_CUR_MAX;
 
   woff = WRAP_OFFSET (_rl_last_v_pos, wrap_offset);
   cpos = _rl_last_c_pos;
@@ -1920,7 +2185,7 @@ _rl_move_cursor_relative (new, data)
      this case, NEW's display position is not obvious and must be
      calculated.  We need to account for invisible characters in this line,
      as long as we are past them and they are counted by _rl_col_width. */
-  if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+  if (mb_cur_max > 1 && rl_byte_oriented == 0)
     {
       adjust = 1;
       /* Try to short-circuit common cases and eliminate a bunch of multibyte
@@ -1942,14 +2207,31 @@ _rl_move_cursor_relative (new, data)
       else
         dpos = _rl_col_width (data, 0, new, 1);
 
+      if (displaying_prompt_first_line == 0)
+       adjust = 0;
+
+      /* yet another special case: printing the last line of a prompt with
+        multibyte characters and invisible characters whose printable length
+        exceeds the screen width with the last invisible character
+        (prompt_last_invisible) in the last line.  IN_INVISLINE is the
+        offset of DATA in invisible_line */
+      in_invisline = 0;
+      if (data > invisible_line && data < invisible_line+inv_lbreaks[_rl_inv_botlin+1])
+       in_invisline = data - invisible_line;
+
       /* 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. */
+      /* NEW is relative to the current displayed line, while
+        PROMPT_LAST_INVISIBLE is relative to the entire (wrapped) line.
+        Need a way to reconcile these two variables by turning NEW into a
+        buffer position relative to the start of the line */
       if (adjust && ((new > prompt_last_invisible) ||          /* XXX - don't use woff here */
-         (prompt_physical_chars >= _rl_screenwidth &&
+                    (new+in_invisline > prompt_last_invisible) ||      /* invisible line */
+         (prompt_physical_chars >= _rl_screenwidth &&          /* visible line */
           _rl_last_v_pos == prompt_last_screen_line &&
           wrap_offset >= woff && dpos >= woff &&
-          new > (prompt_last_invisible-(_rl_screenwidth*_rl_last_v_pos)-wrap_offset))))
+          new > (prompt_last_invisible-(vis_lbreaks[_rl_last_v_pos])-wrap_offset))))
           /* XXX last comparison might need to be >= */
        {
          dpos -= woff;
@@ -1971,7 +2253,7 @@ _rl_move_cursor_relative (new, data)
      of moving backwards. */
   /* i == current physical cursor position. */
 #if defined (HANDLE_MULTIBYTE)
-  if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+  if (mb_cur_max > 1 && rl_byte_oriented == 0)
     i = _rl_last_c_pos;
   else
 #endif
@@ -2006,7 +2288,7 @@ _rl_move_cursor_relative (new, data)
         in the buffer and we have to go back to the beginning of the screen
         line.  In this case, we can use the terminal sequence to move forward
         if it's available. */
-      if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+      if (mb_cur_max > 1 && rl_byte_oriented == 0)
        {
          if (_rl_term_forward_char)
            {
@@ -2060,18 +2342,18 @@ _rl_move_vert (to)
     }
   else
     {                  /* delta < 0 */
-#ifdef __MSDOS__
+#ifdef __DJGPP__
       int row, col;
 
-      fflush (rl_outstream); /* make sure the cursor pos is current! */
+      fflush (rl_outstream);
       ScreenGetCursor (&row, &col);
       ScreenSetCursor (row + delta, col);
-      i = -delta;    /* in case someone wants to use it after the loop */
-#else /* !__MSDOS__ */
+      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 /* !__MSDOS__ */
+#endif /* !__DJGPP__ */
     }
 
   _rl_last_v_pos = to;         /* Now TO is here */
@@ -2149,6 +2431,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);
@@ -2157,11 +2442,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, 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);
 
@@ -2170,11 +2472,17 @@ 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,
-                                        &prompt_invis_chars_first_line,
-                                        &prompt_physical_chars);
+  local_prompt = expand_prompt (msg_buf, 0, &prompt_visible_length,
+                                           &prompt_last_invisible,
+                                           &prompt_invis_chars_first_line,
+                                           &prompt_physical_chars);
   local_prompt_prefix = (char *)NULL;
   local_prompt_len = local_prompt ? strlen (local_prompt) : 0;
   (*rl_redisplay_function) ();
@@ -2186,8 +2494,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)
@@ -2195,10 +2506,16 @@ rl_message (format, arg1, arg2)
       rl_save_prompt ();
       msg_saved_prompt = 1;
     }
-  local_prompt = expand_prompt (msg_buf, &prompt_visible_length,
-                                        &prompt_last_invisible,
-                                        &prompt_invis_chars_first_line,
-                                        &prompt_physical_chars);
+  else if (local_prompt != saved_local_prompt)
+    {
+      FREE (local_prompt);
+      FREE (local_prompt_prefix);
+      local_prompt = (char *)NULL;
+    }
+  local_prompt = expand_prompt (msg_buf, 0, &prompt_visible_length,
+                                           &prompt_last_invisible,
+                                           &prompt_invis_chars_first_line,
+                                           &prompt_physical_chars);
   local_prompt_prefix = (char *)NULL;
   local_prompt_len = local_prompt ? strlen (local_prompt) : 0;
   (*rl_redisplay_function) ();
@@ -2336,8 +2653,8 @@ _rl_clear_to_eol (count)
     tputs (_rl_term_clreol, 1, _rl_output_character_function);
   else
 #endif
-  if (count)
-    space_to_eol (count);
+    if (count)
+      space_to_eol (count);
 }
 
 /* Clear to the end of the line using spaces.  COUNT is the minimum
@@ -2357,15 +2674,15 @@ space_to_eol (count)
 void
 _rl_clear_screen ()
 {
-#if defined (__GO32__)
-  ScreenClear ();      /* FIXME: only works in text modes */
-  ScreenSetCursor (0, 0);  /* term_clrpag is "cl" which homes the cursor */
-#else
+#ifndef __DJGPP__
   if (_rl_term_clrpag)
     tputs (_rl_term_clrpag, 1, _rl_output_character_function);
   else
     rl_crlf ();
-#endif
+#else
+  ScreenClear ();
+  ScreenSetCursor (0, 0);
+#endif /* __DJGPP__ */
 }
 
 /* Insert COUNT characters from STRING to the output stream at column COL. */
@@ -2374,48 +2691,47 @@ insert_some_chars (string, count, col)
      char *string;
      int count, col;
 {
-#if defined (__MSDOS__) || (defined (__MINGW32__) && !defined (NCURSES_VERSION))
+  open_some_spaces (col);
   _rl_output_some_chars (string, count);
-#else
-  /* DEBUGGING */
-  if (MB_CUR_MAX == 1 || rl_byte_oriented)
-    if (count != col)
-      _rl_ttymsg ("debug: insert_some_chars: count (%d) != col (%d)", 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__) || defined (NCURSES_VERSION))
+  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__ || NCURSES_VERSION)*/
 }
 
 /* Delete COUNT characters from the display line. */
@@ -2426,7 +2742,7 @@ delete_chars (count)
   if (count > _rl_screenwidth) /* XXX */
     return;
 
-#if !defined (__MSDOS__) && !(defined (__MINGW32__) && !defined (NCURSES_VERSION))
+#if !defined (__MSDOS__) && (!defined (__MINGW32__) || defined (NCURSES_VERSION))
   if (_rl_term_DC && *_rl_term_DC)
     {
       char *buffer;
@@ -2439,7 +2755,7 @@ delete_chars (count)
        while (count--)
          tputs (_rl_term_dc, 1, _rl_output_character_function);
     }
-#endif /* !__MSDOS__ && !__MINGW32__ */
+#endif /* !__MSDOS__ && (!__MINGW32__ || NCURSES_VERSION)*/
 }
 
 void
@@ -2502,7 +2818,8 @@ redraw_prompt (t)
   rl_save_prompt ();
 
   rl_display_prompt = t;
-  local_prompt = expand_prompt (t, &prompt_visible_length,
+  local_prompt = expand_prompt (t, PMT_MULTILINE,
+                                  &prompt_visible_length,
                                   &prompt_last_invisible,
                                   &prompt_invis_chars_first_line,
                                   &prompt_physical_chars);
@@ -2566,7 +2883,8 @@ _rl_clean_up_for_exit ()
 {
   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);
@@ -2582,6 +2900,12 @@ _rl_erase_entire_line ()
   fflush (rl_outstream);
 }
 
+void
+_rl_ttyflush ()
+{
+  fflush (rl_outstream);
+}
+
 /* return the `current display line' of the cursor -- the number of lines to
    move up to get to the first screen line of the current readline line. */
 int
@@ -2620,10 +2944,8 @@ _rl_col_width (str, start, end, flags)
   if (end <= start)
     return 0;
   if (MB_CUR_MAX == 1 || rl_byte_oriented)
-{
-_rl_ttymsg ("_rl_col_width: called with MB_CUR_MAX == 1");
+    /* this can happen in some cases where it's inconvenient to check */
     return (end - start);
-}
 
   memset (&ps, 0, sizeof (mbstate_t));
 
@@ -2697,7 +3019,7 @@ _rl_ttymsg ("_rl_col_width: called with MB_CUR_MAX == 1");
        {
          point += tmp;
          max -= tmp;
-         tmp = wcwidth(wc);
+         tmp = WCWIDTH(wc);
          width += (tmp >= 0) ? tmp : 1;
        }
     }
This page took 0.072048 seconds and 4 git commands to generate.