Deleted Added
full compact
1c1
< /* $FreeBSD: head/contrib/libreadline/display.c 75409 2001-04-11 03:15:56Z ache $ */
---
> /* $FreeBSD: head/contrib/libreadline/display.c 119614 2003-08-31 18:29:38Z ache $ */
46a47
> #include "rlmbutil.h"
66,70c67,71
< static void update_line __P((char *, char *, int, int, int, int));
< static void space_to_eol __P((int));
< static void delete_chars __P((int));
< static void insert_some_chars __P((char *, int));
< static void cr __P((void));
---
> 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 cr PARAMS((void));
71a73,79
> #if defined (HANDLE_MULTIBYTE)
> static int _rl_col_width PARAMS((char *, int, int));
> static int *_rl_wrapped_line;
> #else
> # define _rl_col_width(l, s, e) (((e) <= (s)) ? 0 : (e) - (s))
> #endif
>
206c214
< r = ret = xmalloc (l + 1);
---
> r = ret = (char *)xmalloc (l + 1);
339,340c347,348
< visible_line = xmalloc (line_size);
< invisible_line = xmalloc (line_size);
---
> visible_line = (char *)xmalloc (line_size);
> invisible_line = (char *)xmalloc (line_size);
347,348c355,356
< visible_line = xrealloc (visible_line, line_size);
< invisible_line = xrealloc (invisible_line, line_size);
---
> visible_line = (char *)xrealloc (visible_line, line_size);
> invisible_line = (char *)xrealloc (invisible_line, line_size);
362a371,373
> #if defined (HANDLE_MULTIBYTE)
> _rl_wrapped_line = (int *)xmalloc (vis_lbsize * sizeof (int));
> #endif
375a387,393
> #if defined (HANDLE_MULTIBYTE)
> wchar_t wc;
> size_t wc_bytes;
> int wc_width;
> mbstate_t ps;
> int _rl_wrapped_multicolumn = 0;
> #endif
425,426c443,444
< visible_line = xrealloc (visible_line, line_size);
< line = invisible_line = xrealloc (invisible_line, line_size);
---
> visible_line = (char *)xrealloc (visible_line, line_size);
> line = invisible_line = (char *)xrealloc (invisible_line, line_size);
459,460c477,478
< visible_line = xrealloc (visible_line, line_size);
< line = invisible_line = xrealloc (invisible_line, line_size);
---
> visible_line = (char *)xrealloc (visible_line, line_size);
> line = invisible_line = (char *)xrealloc (invisible_line, line_size);
476c494,495
<
---
>
> #if defined (HANDLE_MULTIBYTE)
485a505
> _rl_wrapped_line = (int *)xrealloc (_rl_wrapped_line, inv_lbsize * sizeof (int)); \
487a508
> _rl_wrapped_line[newlines] = _rl_wrapped_multicolumn; \
490a512,527
> #else
> #define CHECK_LPOS() \
> do { \
> lpos++; \
> if (lpos >= _rl_screenwidth) \
> { \
> if (newlines >= (inv_lbsize - 2)) \
> { \
> inv_lbsize *= 2; \
> inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
> } \
> inv_lbreaks[++newlines] = out; \
> lpos = 0; \
> } \
> } while (0)
> #endif
494a532,534
> #if defined (HANDLE_MULTIBYTE)
> memset (_rl_wrapped_line, 0, vis_lbsize);
> #endif
511a552
> #if 0
512a554,556
> #else
> ((newlines == 0 && local_prompt_prefix == 0) ? prompt_invis_chars_first_line : 0) +
> #endif
526a571,581
> #if defined (HANDLE_MULTIBYTE)
> in = 0;
> if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
> {
> memset (&ps, 0, sizeof (mbstate_t));
> wc_bytes = mbrtowc (&wc, rl_line_buffer, rl_end, &ps);
> }
> else
> wc_bytes = 1;
> while (in < rl_end)
> #else
527a583
> #endif
530a587,608
> #if defined (HANDLE_MULTIBYTE)
> if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
> {
> if (wc_bytes == (size_t)-1 || wc_bytes == (size_t)-2)
> {
> /* Byte sequence is invalid or shortened. Assume that the
> first byte represents a character. */
> wc_bytes = 1;
> /* Assume that a character occupies a single column. */
> wc_width = 1;
> memset (&ps, 0, sizeof (mbstate_t));
> }
> else if (wc_bytes == (size_t)0)
> break; /* Found '\0' */
> else
> {
> temp = wcwidth (wc);
> wc_width = (temp < 0) ? 1 : temp;
> }
> }
> #endif
>
534,535c612,613
< visible_line = xrealloc (visible_line, line_size);
< invisible_line = xrealloc (invisible_line, line_size);
---
> visible_line = (char *)xrealloc (visible_line, line_size);
> invisible_line = (char *)xrealloc (invisible_line, line_size);
544a623,625
> #if defined (HANDLE_MULTIBYTE)
> if (META_CHAR (c) && _rl_output_meta_chars == 0) /* XXX - clean up */
> #else
545a627
> #endif
613a696,726
> #if defined (HANDLE_MULTIBYTE)
> if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
> {
> register int i;
>
> _rl_wrapped_multicolumn = 0;
>
> if (_rl_screenwidth < lpos + wc_width)
> for (i = lpos; i < _rl_screenwidth; i++)
> {
> /* The space will be removed in update_line() */
> line[out++] = ' ';
> _rl_wrapped_multicolumn++;
> CHECK_LPOS();
> }
> if (in == rl_point)
> {
> c_pos = out;
> lb_linenum = newlines;
> }
> for (i = in; i < in+wc_bytes; i++)
> line[out++] = rl_line_buffer[i];
> for (i = 0; i < wc_width; i++)
> CHECK_LPOS();
> }
> else
> {
> line[out++] = c;
> CHECK_LPOS();
> }
> #else
615a729
> #endif
616a731,741
>
> #if defined (HANDLE_MULTIBYTE)
> if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
> {
> in += wc_bytes;
> wc_bytes = mbrtowc (&wc, rl_line_buffer + in, rl_end - in, &ps);
> }
> else
> in++;
> #endif
>
654c779,784
< out = _rl_screenchars - 1;
---
> {
> if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
> out = _rl_find_prev_mbchar (line, _rl_screenchars, MB_FIND_ANY);
> else
> out = _rl_screenchars - 1;
> }
740c870,873
< _rl_last_c_pos = nleft;
---
> if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
> _rl_last_c_pos = _rl_col_width(local_prompt, 0, nleft);
> else
> _rl_last_c_pos = nleft;
756c889,892
< _rl_last_c_pos = nleft;
---
> if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
> _rl_last_c_pos = _rl_col_width (&visible_line[pos], 0, nleft);
> else
> _rl_last_c_pos = nleft;
759c895
< if (nleft != _rl_last_c_pos)
---
> if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
760a897,898
> else if (nleft != _rl_last_c_pos)
> _rl_move_cursor_relative (nleft, &invisible_line[pos]);
903a1042,1046
> int col_lendiff, col_temp;
> #if defined (HANDLE_MULTIBYTE)
> mbstate_t ps_new, ps_old;
> int new_offset, old_offset, tmp;
> #endif
912c1055
< && _rl_last_v_pos == current_line - 1)
---
> && _rl_last_v_pos == current_line - 1)
914,915c1057,1110
< if (new[0])
< putc (new[0], rl_outstream);
---
> #if defined (HANDLE_MULTIBYTE)
> if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
> {
> wchar_t wc;
> mbstate_t ps;
> int tempwidth, bytes;
> size_t ret;
>
> /* This fixes only double-column characters, but if the wrapped
> character comsumes 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]);
>
> memset (&ps, 0, sizeof (mbstate_t));
> ret = mbrtowc (&wc, new, MB_CUR_MAX, &ps);
> if (ret == (size_t)-1 || ret == (size_t)-2)
> {
> tempwidth = 1;
> ret = 1;
> }
> else if (ret == 0)
> tempwidth = 0;
> else
> tempwidth = wcwidth (wc);
>
> if (tempwidth > 0)
> {
> int count;
> bytes = ret;
> for (count = 0; count < bytes; count++)
> putc (new[count], rl_outstream);
> _rl_last_c_pos = tempwidth;
> _rl_last_v_pos++;
> memset (&ps, 0, sizeof (mbstate_t));
> ret = mbrtowc (&wc, old, MB_CUR_MAX, &ps);
> if (ret != 0 && bytes != 0)
> {
> if (ret == (size_t)-1 || ret == (size_t)-2)
> memmove (old+bytes, old+1, strlen (old+1));
> else
> memmove (old+bytes, old+ret, strlen (old+ret));
> memcpy (old, new, bytes);
> }
> }
> else
> {
> putc (' ', rl_outstream);
> _rl_last_c_pos = 1;
> _rl_last_v_pos++;
> if (old[0] && new[0])
> old[0] = new[0];
> }
> }
917,921c1112,1122
< putc (' ', rl_outstream);
< _rl_last_c_pos = 1; /* XXX */
< _rl_last_v_pos++;
< if (old[0] && new[0])
< old[0] = new[0];
---
> #endif
> {
> if (new[0])
> putc (new[0], rl_outstream);
> else
> putc (' ', rl_outstream);
> _rl_last_c_pos = 1; /* XXX */
> _rl_last_v_pos++;
> if (old[0] && new[0])
> old[0] = new[0];
> }
922a1124
>
924a1127,1145
> #if defined (HANDLE_MULTIBYTE)
> if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
> {
> memset (&ps_new, 0, sizeof(mbstate_t));
> memset (&ps_old, 0, sizeof(mbstate_t));
>
> new_offset = old_offset = 0;
> for (ofd = old, nfd = new;
> (ofd - old < omax) && *ofd &&
> _rl_compare_chars(old, old_offset, &ps_old, new, new_offset, &ps_new); )
> {
> 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;
> }
> }
> else
> #endif
940a1162,1188
>
> #if defined (HANDLE_MULTIBYTE)
> 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));
> memset (&ps_new, 0, sizeof (mbstate_t));
>
> _rl_adjust_point (old, ols - old, &ps_old);
> _rl_adjust_point (new, nls - new, &ps_new);
>
> if (_rl_compare_chars (old, ols - old, &ps_old, new, nls - new, &ps_new) == 0)
> break;
>
> if (*ols == ' ')
> wsatend = 0;
>
> ols = old + _rl_find_prev_mbchar (old, ols - old, MB_FIND_ANY);
> nls = new + _rl_find_prev_mbchar (new, nls - new, MB_FIND_ANY);
> }
> }
> else
> {
> #endif /* HANDLE_MULTIBYTE */
949a1198,1200
> #if defined (HANDLE_MULTIBYTE)
> }
> #endif
955a1207,1212
> #if defined (HANDLE_MULTIBYTE)
> /* This may not work for stateful encoding, but who cares? To handle
> stateful encoding properly, we have to scan each string from the
> beginning and compare. */
> else if (_rl_compare_chars (ols, 0, NULL, nls, 0, NULL) == 0)
> #else
956a1214
> #endif
959c1217,1222
< ols++;
---
> {
> if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
> ols = old + _rl_find_next_mbchar (old, ols - old, 1, MB_FIND_ANY);
> else
> ols++;
> }
961c1224,1229
< nls++;
---
> {
> if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
> nls = new + _rl_find_next_mbchar (new, nls - new, 1, MB_FIND_ANY);
> else
> nls++;
> }
997c1265,1268
< _rl_last_c_pos = lendiff;
---
> if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
> _rl_last_c_pos = _rl_col_width (local_prompt, 0, lendiff);
> else
> _rl_last_c_pos = lendiff;
1002c1273,1276
< /* if (len (new) > len (old)) */
---
> /* if (len (new) > len (old))
> lendiff == difference in buffer
> col_lendiff == difference on screen
> When not using multibyte characters, these are equal */
1003a1278,1281
> 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);
> else
> col_lendiff = lendiff;
1010c1288,1299
< lendiff += visible_wrap_offset - current_invis_chars;
---
> {
> if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
> {
> lendiff += visible_wrap_offset - current_invis_chars;
> col_lendiff += visible_wrap_offset - current_invis_chars;
> }
> else
> {
> lendiff += visible_wrap_offset - current_invis_chars;
> col_lendiff = lendiff;
> }
> }
1014c1303,1308
< if (lendiff > 0)
---
> if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
> col_temp = _rl_col_width (new, nfd - new, ne - new);
> else
> col_temp = temp;
>
> if (col_lendiff > 0) /* XXX - was lendiff */
1022c1316
< if (_rl_terminal_can_insert && ((2 * temp) >= lendiff || _rl_term_IC) && (!_rl_term_autowrap || !gl))
---
> if (_rl_terminal_can_insert && ((2 * col_temp) >= col_lendiff || _rl_term_IC) && (!_rl_term_autowrap || !gl))
1031,1032c1325,1326
< insert_some_chars (nfd, lendiff);
< _rl_last_c_pos += lendiff;
---
> insert_some_chars (nfd, lendiff, col_lendiff);
> _rl_last_c_pos += col_lendiff;
1041c1335
< _rl_last_c_pos += lendiff;
---
> _rl_last_c_pos += col_lendiff;
1049c1343
< _rl_last_c_pos += temp;
---
> _rl_last_c_pos += col_temp;
1057c1351,1355
< _rl_last_c_pos += temp - lendiff;
---
> #if 0
> _rl_last_c_pos += _rl_col_width (nfd+lendiff, 0, temp-lendiff) - col_lendiff;
> #else
> _rl_last_c_pos += _rl_col_width (nfd+lendiff, 0, temp-col_lendiff);
> #endif
1064c1362
< _rl_last_c_pos += temp;
---
> _rl_last_c_pos += col_temp;
1070c1368
< if (_rl_term_dc && (2 * temp) >= -lendiff)
---
> if (_rl_term_dc && (2 * col_temp) >= -col_lendiff)
1077c1375
< lendiff = 0;
---
> col_lendiff = 0;
1079,1080c1377,1378
< if (lendiff)
< delete_chars (-lendiff); /* delete (diff) characters */
---
> if (col_lendiff)
> delete_chars (-col_lendiff); /* delete (diff) characters */
1087c1385
< _rl_last_c_pos += temp;
---
> _rl_last_c_pos += _rl_col_width (nfd, 0, temp);;
1096c1394
< _rl_last_c_pos += temp;
---
> _rl_last_c_pos += col_temp;
1099c1397,1402
< if (lendiff)
---
> 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);
> else
> col_lendiff = lendiff;
>
> if (col_lendiff)
1102c1405
< space_to_eol (lendiff);
---
> space_to_eol (col_lendiff);
1104c1407
< _rl_clear_to_eol (lendiff);
---
> _rl_clear_to_eol (col_lendiff);
1150c1453,1456
< _rl_last_c_pos = l;
---
> if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
> _rl_last_c_pos = _rl_col_width (prompt_last_line, 0, l);
> else
> _rl_last_c_pos = l;
1204a1511,1516
> #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
> this case, NEW's display position is not obvious. */
> if ((MB_CUR_MAX == 1 || rl_byte_oriented ) && _rl_last_c_pos == new) return;
> #else
1205a1518
> #endif
1234a1548,1568
> {
> if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
> {
> int width;
> width = _rl_col_width (data, _rl_last_c_pos, new);
> for (i = 0; i < width; i++)
> tputs (_rl_term_forward_char, 1, _rl_output_character_function);
> }
> else
> {
> for (i = _rl_last_c_pos; i < new; i++)
> tputs (_rl_term_forward_char, 1, _rl_output_character_function);
> }
> }
> else if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
> {
> tputs (_rl_term_cr, 1, _rl_output_character_function);
> for (i = 0; i < new; i++)
> putc (data[i], rl_outstream);
> }
> else
1236c1570,1579
< tputs (_rl_term_forward_char, 1, _rl_output_character_function);
---
> putc (data[i], rl_outstream);
>
> #else /* !HACK_TERMCAP_MOTION */
>
> if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
> {
> tputs (_rl_term_cr, 1, _rl_output_character_function);
> for (i = 0; i < new; i++)
> putc (data[i], rl_outstream);
> }
1240,1243c1583,1585
< #else
< for (i = _rl_last_c_pos; i < new; i++)
< putc (data[i], rl_outstream);
< #endif /* HACK_TERMCAP_MOTION */
---
>
> #endif /* !HACK_TERMCAP_MOTION */
>
1244a1587,1593
> #if defined (HANDLE_MULTIBYTE)
> /* NEW points to the buffer point, but _rl_last_c_pos is the display point.
> The byte length of the string is probably bigger than the column width
> of the string, which means that if NEW == _rl_last_c_pos, then NEW's
> display point is less than _rl_last_c_pos. */
> else if (_rl_last_c_pos >= new)
> #else
1246,1247c1595,1610
< _rl_backspace (_rl_last_c_pos - new);
< _rl_last_c_pos = new;
---
> #endif
> {
> if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
> {
> tputs (_rl_term_cr, 1, _rl_output_character_function);
> for (i = 0; i < new; i++)
> putc (data[i], rl_outstream);
> }
> else
> _rl_backspace (_rl_last_c_pos - new);
> }
>
> if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
> _rl_last_c_pos = _rl_col_width (data, 0, new);
> else
> _rl_last_c_pos = new;
1334c1697
< return ((isprint (uc)) ? 1 : 2);
---
> return ((ISPRINT (uc)) ? 1 : 2);
1360a1724,1726
> #if defined (HAVE_VSNPRINTF)
> vsnprintf (msg_buf, sizeof (msg_buf) - 1, format, args);
> #else
1361a1728,1729
> msg_buf[sizeof(msg_buf) - 1] = '\0'; /* overflow? */
> #endif
1373a1742
> msg_buf[sizeof(msg_buf) - 1] = '\0'; /* overflow? */
1440c1809
< pmt = xmalloc (len + 2);
---
> pmt = (char *)xmalloc (len + 2);
1449c1818
< pmt = xmalloc (len + 2);
---
> pmt = (char *)xmalloc (len + 2);
1512c1881
< /* Insert COUNT characters from STRING to the output stream. */
---
> /* Insert COUNT characters from STRING to the output stream at column COL. */
1514c1883
< insert_some_chars (string, count)
---
> insert_some_chars (string, count, col)
1516c1885
< int count;
---
> int count, col;
1517a1887,1891
> /* 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);
>
1522c1896,1897
< buffer = tgoto (_rl_term_IC, 0, count);
---
>
> buffer = tgoto (_rl_term_IC, 0, col);
1538c1913
< for (i = count; i--; )
---
> for (i = col; i--; )
1593,1595c1968
< #if 0
< last_line = &visible_line[inv_lbreaks[_rl_vis_botlin]];
< #else
---
>
1597d1969
< #endif
1741a2114,2197
>
> #if defined (HANDLE_MULTIBYTE)
> /* Calculate the number of screen columns occupied by STR from START to END.
> 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)
> char *str;
> int start, end;
> {
> wchar_t wc;
> mbstate_t ps = {0};
> int tmp, point, width, max;
>
> if (end <= start)
> return 0;
>
> point = 0;
> max = end;
>
> while (point < start)
> {
> tmp = mbrlen (str + point, max, &ps);
> if ((size_t)tmp == (size_t)-1 || (size_t)tmp == (size_t)-2)
> {
> /* In this case, the bytes are invalid or too short to compose a
> multibyte character, so we assume that the first byte represents
> a single character. */
> point++;
> max--;
>
> /* Clear the state of the byte sequence, because in this case the
> effect of mbstate is undefined. */
> memset (&ps, 0, sizeof (mbstate_t));
> }
> else if (tmp == 0)
> break; /* Found '\0' */
> else
> {
> point += tmp;
> max -= tmp;
> }
> }
>
> /* If START is not a byte that starts a character, then POINT will be
> greater than START. In this case, assume that (POINT - START) gives
> a byte count that is the number of columns of difference. */
> width = point - start;
>
> while (point < end)
> {
> tmp = mbrtowc (&wc, str + point, max, &ps);
> if ((size_t)tmp == (size_t)-1 || (size_t)tmp == (size_t)-2)
> {
> /* In this case, the bytes are invalid or too short to compose a
> multibyte character, so we assume that the first byte represents
> a single character. */
> point++;
> max--;
>
> /* and assume that the byte occupies a single column. */
> width++;
>
> /* Clear the state of the byte sequence, because in this case the
> effect of mbstate is undefined. */
> memset (&ps, 0, sizeof (mbstate_t));
> }
> else if (tmp == 0)
> break; /* Found '\0' */
> else
> {
> point += tmp;
> max -= tmp;
> tmp = wcwidth(wc);
> width += (tmp >= 0) ? tmp : 1;
> }
> }
>
> width += point - end;
>
> return width;
> }
> #endif /* HANDLE_MULTIBYTE */
>