Remove gdb workaround from readline/xfree.c
[deliverable/binutils-gdb.git] / readline / display.c
index c6b14fb56051301787044ab4cf51245281285d67..cfe816402a44ad832f05f351bfc904912feca74a 100644 (file)
@@ -1,6 +1,6 @@
 /* display.c -- readline redisplay facility. */
 
-/* Copyright (C) 1987-2013 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.
@@ -83,8 +83,8 @@ struct line_state
     int *lbreaks;
     int lbsize;
 #if defined (HANDLE_MULTIBYTE)
-    int *wrapped_line;
     int wbsize;
+    int *wrapped_line;
 #endif
   };
 
@@ -119,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)
   
@@ -192,6 +194,8 @@ 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. */
@@ -528,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)
@@ -580,13 +587,13 @@ 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;
   int wc_width;
   mbstate_t ps;
   int _rl_wrapped_multicolumn = 0;
-  int mb_cur_max = MB_CUR_MAX;
 #endif
 
   if (_rl_echoing_p == 0)
@@ -605,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;
@@ -737,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)
@@ -757,6 +768,18 @@ rl_redisplay ()
   while (lpos >= _rl_screenwidth)
     {
       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
@@ -767,11 +790,14 @@ rl_redisplay ()
 #if defined (HANDLE_MULTIBYTE)
       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);
@@ -798,16 +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 += (newlines == 0) ? prompt_invis_chars_first_line
-                             : ((newlines == prompt_lines_estimate) ? wrap_offset : prompt_invis_chars_first_line);
+      if (nocorrect == 0)
+       temp += wadjust;
 
       inv_lbreaks[++newlines] = temp;
 #if defined (HANDLE_MULTIBYTE)
+      /* 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);
+       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;
@@ -999,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;
@@ -1029,9 +1057,11 @@ rl_redisplay ()
             not the first.  */
          if (out >= _rl_screenchars)
            {
+#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;
            }
 
@@ -1368,6 +1398,7 @@ update_line (old, new, current_line, omax, nmax, inv_botlin)
   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;
@@ -1378,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);
@@ -1386,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;
@@ -1400,7 +1431,7 @@ update_line (old, new, current_line, omax, nmax, inv_botlin)
            _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;
@@ -1420,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))
@@ -1459,7 +1490,7 @@ 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. */
@@ -1516,7 +1547,7 @@ update_line (old, new, current_line, omax, nmax, inv_botlin)
     return;
 
 #if defined (HANDLE_MULTIBYTE)
-  if (MB_CUR_MAX > 1 && rl_byte_oriented == 0 && _rl_utf8locale)
+  if (mb_cur_max > 1 && rl_byte_oriented == 0 && _rl_utf8locale)
     {
       wchar_t wc;
       mbstate_t ps = { 0 };
@@ -1525,7 +1556,7 @@ update_line (old, new, current_line, omax, nmax, inv_botlin)
       /* 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);
+      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);
@@ -1539,7 +1570,7 @@ update_line (old, new, current_line, omax, nmax, inv_botlin)
   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);
@@ -1597,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++;
@@ -1616,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;
     }
 
@@ -1627,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);
@@ -1646,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. */
@@ -1655,6 +1706,43 @@ 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;
@@ -1668,7 +1756,7 @@ update_line (old, new, current_line, omax, nmax, inv_botlin)
   /* 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;
@@ -1679,7 +1767,7 @@ update_line (old, new, current_line, omax, nmax, inv_botlin)
      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;
@@ -1690,7 +1778,7 @@ update_line (old, new, current_line, omax, nmax, inv_botlin)
   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;
@@ -1708,7 +1796,7 @@ update_line (old, new, current_line, omax, nmax, inv_botlin)
      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;
@@ -1759,7 +1847,7 @@ 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) &&
@@ -1767,12 +1855,12 @@ update_line (old, new, current_line, omax, nmax, inv_botlin)
            {
              open_some_spaces (col_lendiff);
              _rl_output_some_chars (nfd, bytes_to_insert);
-             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 (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)
+         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. */
@@ -1787,7 +1875,7 @@ update_line (old, new, current_line, omax, nmax, inv_botlin)
              /* 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))
+             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;
@@ -1800,7 +1888,7 @@ update_line (old, new, current_line, omax, nmax, inv_botlin)
              /* 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))
+             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;
@@ -1816,11 +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;
@@ -1860,7 +1952,7 @@ update_line (old, new, current_line, omax, nmax, inv_botlin)
                 in a multibyte locale to account for the wrap offset and
                 set cpos_adjusted accordingly. */
              _rl_output_some_chars (nfd, bytes_to_insert);
-             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 (nfd, 0, bytes_to_insert, 1);
                  if (current_line == 0 && wrap_offset &&
@@ -1895,7 +1987,7 @@ 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 &&
                        displaying_prompt_first_line &&
@@ -1909,7 +2001,7 @@ update_line (old, new, current_line, omax, nmax, inv_botlin)
            }
 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;
@@ -1919,7 +2011,7 @@ clear_rest_of_line:
             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 (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);
@@ -1945,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
@@ -2039,7 +2159,8 @@ rl_redraw_prompt_last_line ()
    (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;
@@ -2049,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;
@@ -2062,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
@@ -2087,14 +2210,28 @@ _rl_move_cursor_relative (new, data)
       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;
@@ -2116,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
@@ -2151,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)
            {
This page took 0.033246 seconds and 4 git commands to generate.