Deleted Added
sdiff udiff text old ( 75409 ) new ( 119614 )
full compact
1/* $FreeBSD: head/contrib/libreadline/display.c 119614 2003-08-31 18:29:38Z ache $ */
2/* display.c -- readline redisplay facility. */
3
4/* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
5
6 This file is part of the GNU Readline Library, a library for
7 reading lines of text with interactive input and history editing.
8
9 The GNU Readline Library is free software; you can redistribute it

--- 29 unchanged lines hidden (view full) ---

39#else
40# include "ansi_stdlib.h"
41#endif /* HAVE_STDLIB_H */
42
43#include <stdio.h>
44
45/* System-specific feature definitions and include files. */
46#include "rldefs.h"
47#include "rlmbutil.h"
48
49/* Termcap library stuff. */
50#include "tcap.h"
51
52/* Some standard library routines. */
53#include "readline.h"
54#include "history.h"
55
56#include "rlprivate.h"
57#include "xmalloc.h"
58
59#if !defined (strchr) && !defined (__STDC__)
60extern char *strchr (), *strrchr ();
61#endif /* !strchr && !__STDC__ */
62
63#if defined (HACK_TERMCAP_MOTION)
64extern char *_rl_term_forward_char;
65#endif
66
67static void update_line PARAMS((char *, char *, int, int, int, int));
68static void space_to_eol PARAMS((int));
69static void delete_chars PARAMS((int));
70static void insert_some_chars PARAMS((char *, int, int));
71static void cr PARAMS((void));
72
73#if defined (HANDLE_MULTIBYTE)
74static int _rl_col_width PARAMS((char *, int, int));
75static int *_rl_wrapped_line;
76#else
77# define _rl_col_width(l, s, e) (((e) <= (s)) ? 0 : (e) - (s))
78#endif
79
80static int *inv_lbreaks, *vis_lbreaks;
81static int inv_lbsize, vis_lbsize;
82
83/* Heuristic used to decide whether it is faster to move from CUR to NEW
84 by backing up or outputting a carriage return and moving forward. */
85#define CR_FASTER(new, cur) (((new) + 1) < ((cur) - (new)))
86
87/* **************************************************************** */

--- 118 unchanged lines hidden (view full) ---

206 {
207 r = savestring (pmt);
208 if (lp)
209 *lp = strlen (r);
210 return r;
211 }
212
213 l = strlen (pmt);
214 r = ret = (char *)xmalloc (l + 1);
215
216 invfl = 0; /* invisible chars in first line of prompt */
217
218 for (rl = ignoring = last = ninvis = 0, p = pmt; p && *p; p++)
219 {
220 /* This code strips the invisible character string markers
221 RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE */
222 if (*p == RL_PROMPT_START_IGNORE)

--- 116 unchanged lines hidden (view full) ---

339 int minsize;
340{
341 register int n;
342
343 if (invisible_line == 0) /* initialize it */
344 {
345 if (line_size < minsize)
346 line_size = minsize;
347 visible_line = (char *)xmalloc (line_size);
348 invisible_line = (char *)xmalloc (line_size);
349 }
350 else if (line_size < minsize) /* ensure it can hold MINSIZE chars */
351 {
352 line_size *= 2;
353 if (line_size < minsize)
354 line_size = minsize;
355 visible_line = (char *)xrealloc (visible_line, line_size);
356 invisible_line = (char *)xrealloc (invisible_line, line_size);
357 }
358
359 for (n = minsize; n < line_size; n++)
360 {
361 visible_line[n] = 0;
362 invisible_line[n] = 1;
363 }
364
365 if (vis_lbreaks == 0)
366 {
367 /* should be enough. */
368 inv_lbsize = vis_lbsize = 256;
369 inv_lbreaks = (int *)xmalloc (inv_lbsize * sizeof (int));
370 vis_lbreaks = (int *)xmalloc (vis_lbsize * sizeof (int));
371#if defined (HANDLE_MULTIBYTE)
372 _rl_wrapped_line = (int *)xmalloc (vis_lbsize * sizeof (int));
373#endif
374 inv_lbreaks[0] = vis_lbreaks[0] = 0;
375 }
376}
377
378/* Basic redisplay algorithm. */
379void
380rl_redisplay ()
381{
382 register int in, out, c, linenum, cursor_linenum;
383 register char *line;
384 int c_pos, inv_botlin, lb_botlin, lb_linenum;
385 int newlines, lpos, temp;
386 char *prompt_this_line;
387#if defined (HANDLE_MULTIBYTE)
388 wchar_t wc;
389 size_t wc_bytes;
390 int wc_width;
391 mbstate_t ps;
392 int _rl_wrapped_multicolumn = 0;
393#endif
394
395 if (!readline_echoing_p)
396 return;
397
398 if (!rl_display_prompt)
399 rl_display_prompt = "";
400
401 if (invisible_line == 0)

--- 33 unchanged lines hidden (view full) ---

435 _rl_output_some_chars (local_prompt_prefix, strlen (local_prompt_prefix));
436
437 if (local_len > 0)
438 {
439 temp = local_len + out + 2;
440 if (temp >= line_size)
441 {
442 line_size = (temp + 1024) - (temp % 1024);
443 visible_line = (char *)xrealloc (visible_line, line_size);
444 line = invisible_line = (char *)xrealloc (invisible_line, line_size);
445 }
446 strncpy (line + out, local_prompt, local_len);
447 out += local_len;
448 }
449 line[out] = '\0';
450 wrap_offset = local_len - prompt_visible_length;
451 }
452 else

--- 16 unchanged lines hidden (view full) ---

469 }
470 }
471
472 pmtlen = strlen (prompt_this_line);
473 temp = pmtlen + out + 2;
474 if (temp >= line_size)
475 {
476 line_size = (temp + 1024) - (temp % 1024);
477 visible_line = (char *)xrealloc (visible_line, line_size);
478 line = invisible_line = (char *)xrealloc (invisible_line, line_size);
479 }
480 strncpy (line + out, prompt_this_line, pmtlen);
481 out += pmtlen;
482 line[out] = '\0';
483 wrap_offset = prompt_invis_chars_first_line = 0;
484 }
485
486#define CHECK_INV_LBREAKS() \
487 do { \
488 if (newlines >= (inv_lbsize - 2)) \
489 { \
490 inv_lbsize *= 2; \
491 inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
492 } \
493 } while (0)
494
495#if defined (HANDLE_MULTIBYTE)
496#define CHECK_LPOS() \
497 do { \
498 lpos++; \
499 if (lpos >= _rl_screenwidth) \
500 { \
501 if (newlines >= (inv_lbsize - 2)) \
502 { \
503 inv_lbsize *= 2; \
504 inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
505 _rl_wrapped_line = (int *)xrealloc (_rl_wrapped_line, inv_lbsize * sizeof (int)); \
506 } \
507 inv_lbreaks[++newlines] = out; \
508 _rl_wrapped_line[newlines] = _rl_wrapped_multicolumn; \
509 lpos = 0; \
510 } \
511 } while (0)
512#else
513#define CHECK_LPOS() \
514 do { \
515 lpos++; \
516 if (lpos >= _rl_screenwidth) \
517 { \
518 if (newlines >= (inv_lbsize - 2)) \
519 { \
520 inv_lbsize *= 2; \
521 inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
522 } \
523 inv_lbreaks[++newlines] = out; \
524 lpos = 0; \
525 } \
526 } while (0)
527#endif
528
529 /* inv_lbreaks[i] is where line i starts in the buffer. */
530 inv_lbreaks[newlines = 0] = 0;
531 lpos = out - wrap_offset;
532#if defined (HANDLE_MULTIBYTE)
533 memset (_rl_wrapped_line, 0, vis_lbsize);
534#endif
535
536 /* prompt_invis_chars_first_line is the number of invisible characters in
537 the first physical line of the prompt.
538 wrap_offset - prompt_invis_chars_first_line is the number of invis
539 chars on the second line. */
540
541 /* what if lpos is already >= _rl_screenwidth before we start drawing the
542 contents of the command line? */
543 while (lpos >= _rl_screenwidth)
544 {
545 /* fix from Darin Johnson <darin@acuson.com> for prompt string with
546 invisible characters that is longer than the screen width. The
547 prompt_invis_chars_first_line variable could be made into an array
548 saying how many invisible characters there are per line, but that's
549 probably too much work for the benefit gained. How many people have
550 prompts that exceed two physical lines? */
551 temp = ((newlines + 1) * _rl_screenwidth) +
552#if 0
553 ((newlines == 0) ? prompt_invis_chars_first_line : 0) +
554#else
555 ((newlines == 0 && local_prompt_prefix == 0) ? prompt_invis_chars_first_line : 0) +
556#endif
557 ((newlines == 1) ? wrap_offset : 0);
558
559 inv_lbreaks[++newlines] = temp;
560 lpos -= _rl_screenwidth;
561 }
562
563 prompt_last_screen_line = newlines;
564
565 /* Draw the rest of the line (after the prompt) into invisible_line, keeping
566 track of where the cursor is (c_pos), the number of the line containing
567 the cursor (lb_linenum), the last line number (lb_botlin and inv_botlin).
568 It maintains an array of line breaks for display (inv_lbreaks).
569 This handles expanding tabs for display and displaying meta characters. */
570 lb_linenum = 0;
571#if defined (HANDLE_MULTIBYTE)
572 in = 0;
573 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
574 {
575 memset (&ps, 0, sizeof (mbstate_t));
576 wc_bytes = mbrtowc (&wc, rl_line_buffer, rl_end, &ps);
577 }
578 else
579 wc_bytes = 1;
580 while (in < rl_end)
581#else
582 for (in = 0; in < rl_end; in++)
583#endif
584 {
585 c = (unsigned char)rl_line_buffer[in];
586
587#if defined (HANDLE_MULTIBYTE)
588 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
589 {
590 if (wc_bytes == (size_t)-1 || wc_bytes == (size_t)-2)
591 {
592 /* Byte sequence is invalid or shortened. Assume that the
593 first byte represents a character. */
594 wc_bytes = 1;
595 /* Assume that a character occupies a single column. */
596 wc_width = 1;
597 memset (&ps, 0, sizeof (mbstate_t));
598 }
599 else if (wc_bytes == (size_t)0)
600 break; /* Found '\0' */
601 else
602 {
603 temp = wcwidth (wc);
604 wc_width = (temp < 0) ? 1 : temp;
605 }
606 }
607#endif
608
609 if (out + 8 >= line_size) /* XXX - 8 for \t */
610 {
611 line_size *= 2;
612 visible_line = (char *)xrealloc (visible_line, line_size);
613 invisible_line = (char *)xrealloc (invisible_line, line_size);
614 line = invisible_line;
615 }
616
617 if (in == rl_point)
618 {
619 c_pos = out;
620 lb_linenum = newlines;
621 }
622
623#if defined (HANDLE_MULTIBYTE)
624 if (META_CHAR (c) && _rl_output_meta_chars == 0) /* XXX - clean up */
625#else
626 if (META_CHAR (c))
627#endif
628 {
629 if (_rl_output_meta_chars == 0)
630 {
631 sprintf (line + out, "\\%o", c);
632
633 if (lpos + 4 >= _rl_screenwidth)
634 {
635 temp = _rl_screenwidth - lpos;

--- 52 unchanged lines hidden (view full) ---

688 {
689 line[out++] = '^';
690 CHECK_LPOS();
691 line[out++] = CTRL_CHAR (c) ? UNCTRL (c) : '?';
692 CHECK_LPOS();
693 }
694 else
695 {
696#if defined (HANDLE_MULTIBYTE)
697 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
698 {
699 register int i;
700
701 _rl_wrapped_multicolumn = 0;
702
703 if (_rl_screenwidth < lpos + wc_width)
704 for (i = lpos; i < _rl_screenwidth; i++)
705 {
706 /* The space will be removed in update_line() */
707 line[out++] = ' ';
708 _rl_wrapped_multicolumn++;
709 CHECK_LPOS();
710 }
711 if (in == rl_point)
712 {
713 c_pos = out;
714 lb_linenum = newlines;
715 }
716 for (i = in; i < in+wc_bytes; i++)
717 line[out++] = rl_line_buffer[i];
718 for (i = 0; i < wc_width; i++)
719 CHECK_LPOS();
720 }
721 else
722 {
723 line[out++] = c;
724 CHECK_LPOS();
725 }
726#else
727 line[out++] = c;
728 CHECK_LPOS();
729#endif
730 }
731
732#if defined (HANDLE_MULTIBYTE)
733 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
734 {
735 in += wc_bytes;
736 wc_bytes = mbrtowc (&wc, rl_line_buffer + in, rl_end - in, &ps);
737 }
738 else
739 in++;
740#endif
741
742 }
743 line[out] = '\0';
744 if (c_pos < 0)
745 {
746 c_pos = out;
747 lb_linenum = newlines;
748 }
749

--- 21 unchanged lines hidden (view full) ---

771 if (!rl_display_fixed || forced_display)
772 {
773 forced_display = 0;
774
775 /* If we have more than a screenful of material to display, then
776 only display a screenful. We should display the last screen,
777 not the first. */
778 if (out >= _rl_screenchars)
779 {
780 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
781 out = _rl_find_prev_mbchar (line, _rl_screenchars, MB_FIND_ANY);
782 else
783 out = _rl_screenchars - 1;
784 }
785
786 /* The first line is at character position 0 in the buffer. The
787 second and subsequent lines start at inv_lbreaks[N], offset by
788 OFFSET (which has already been calculated above). */
789
790#define W_OFFSET(line, offset) ((line) == 0 ? offset : 0)
791#define VIS_LLEN(l) ((l) > _rl_vis_botlin ? 0 : (vis_lbreaks[l+1] - vis_lbreaks[l]))
792#define INV_LLEN(l) (inv_lbreaks[l+1] - inv_lbreaks[l])

--- 69 unchanged lines hidden (view full) ---

862 {
863#if defined (__MSDOS__)
864 putc ('\r', rl_outstream);
865#else
866 if (_rl_term_cr)
867 tputs (_rl_term_cr, 1, _rl_output_character_function);
868#endif
869 _rl_output_some_chars (local_prompt, nleft);
870 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
871 _rl_last_c_pos = _rl_col_width(local_prompt, 0, nleft);
872 else
873 _rl_last_c_pos = nleft;
874 }
875
876 /* Where on that line? And where does that line start
877 in the buffer? */
878 pos = inv_lbreaks[cursor_linenum];
879 /* nleft == number of characters in the line buffer between the
880 start of the line and the cursor position. */
881 nleft = c_pos - pos;
882
883 /* Since _rl_backspace() doesn't know about invisible characters in the
884 prompt, and there's no good way to tell it, we compensate for
885 those characters here and call _rl_backspace() directly. */
886 if (wrap_offset && cursor_linenum == 0 && nleft < _rl_last_c_pos)
887 {
888 _rl_backspace (_rl_last_c_pos - nleft);
889 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
890 _rl_last_c_pos = _rl_col_width (&visible_line[pos], 0, nleft);
891 else
892 _rl_last_c_pos = nleft;
893 }
894
895 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
896 _rl_move_cursor_relative (nleft, &invisible_line[pos]);
897 else if (nleft != _rl_last_c_pos)
898 _rl_move_cursor_relative (nleft, &invisible_line[pos]);
899 }
900 }
901 else /* Do horizontal scrolling. */
902 {
903#define M_OFFSET(margin, offset) ((margin) == 0 ? offset : 0)
904 int lmargin, ndisp, nleft, phys_c_pos, t;
905
906 /* Always at top line. */

--- 127 unchanged lines hidden (view full) ---

1034static void
1035update_line (old, new, current_line, omax, nmax, inv_botlin)
1036 register char *old, *new;
1037 int current_line, omax, nmax, inv_botlin;
1038{
1039 register char *ofd, *ols, *oe, *nfd, *nls, *ne;
1040 int temp, lendiff, wsatend, od, nd;
1041 int current_invis_chars;
1042 int col_lendiff, col_temp;
1043#if defined (HANDLE_MULTIBYTE)
1044 mbstate_t ps_new, ps_old;
1045 int new_offset, old_offset, tmp;
1046#endif
1047
1048 /* If we're at the right edge of a terminal that supports xn, we're
1049 ready to wrap around, so do so. This fixes problems with knowing
1050 the exact cursor position and cut-and-paste with certain terminal
1051 emulators. In this calculation, TEMP is the physical screen
1052 position of the cursor. */
1053 temp = _rl_last_c_pos - W_OFFSET(_rl_last_v_pos, visible_wrap_offset);
1054 if (temp == _rl_screenwidth && _rl_term_autowrap && !_rl_horizontal_scroll_mode
1055 && _rl_last_v_pos == current_line - 1)
1056 {
1057#if defined (HANDLE_MULTIBYTE)
1058 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1059 {
1060 wchar_t wc;
1061 mbstate_t ps;
1062 int tempwidth, bytes;
1063 size_t ret;
1064
1065 /* This fixes only double-column characters, but if the wrapped
1066 character comsumes more than three columns, spaces will be
1067 inserted in the string buffer. */
1068 if (_rl_wrapped_line[current_line] > 0)
1069 _rl_clear_to_eol (_rl_wrapped_line[current_line]);
1070
1071 memset (&ps, 0, sizeof (mbstate_t));
1072 ret = mbrtowc (&wc, new, MB_CUR_MAX, &ps);
1073 if (ret == (size_t)-1 || ret == (size_t)-2)
1074 {
1075 tempwidth = 1;
1076 ret = 1;
1077 }
1078 else if (ret == 0)
1079 tempwidth = 0;
1080 else
1081 tempwidth = wcwidth (wc);
1082
1083 if (tempwidth > 0)
1084 {
1085 int count;
1086 bytes = ret;
1087 for (count = 0; count < bytes; count++)
1088 putc (new[count], rl_outstream);
1089 _rl_last_c_pos = tempwidth;
1090 _rl_last_v_pos++;
1091 memset (&ps, 0, sizeof (mbstate_t));
1092 ret = mbrtowc (&wc, old, MB_CUR_MAX, &ps);
1093 if (ret != 0 && bytes != 0)
1094 {
1095 if (ret == (size_t)-1 || ret == (size_t)-2)
1096 memmove (old+bytes, old+1, strlen (old+1));
1097 else
1098 memmove (old+bytes, old+ret, strlen (old+ret));
1099 memcpy (old, new, bytes);
1100 }
1101 }
1102 else
1103 {
1104 putc (' ', rl_outstream);
1105 _rl_last_c_pos = 1;
1106 _rl_last_v_pos++;
1107 if (old[0] && new[0])
1108 old[0] = new[0];
1109 }
1110 }
1111 else
1112#endif
1113 {
1114 if (new[0])
1115 putc (new[0], rl_outstream);
1116 else
1117 putc (' ', rl_outstream);
1118 _rl_last_c_pos = 1; /* XXX */
1119 _rl_last_v_pos++;
1120 if (old[0] && new[0])
1121 old[0] = new[0];
1122 }
1123 }
1124
1125
1126 /* Find first difference. */
1127#if defined (HANDLE_MULTIBYTE)
1128 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1129 {
1130 memset (&ps_new, 0, sizeof(mbstate_t));
1131 memset (&ps_old, 0, sizeof(mbstate_t));
1132
1133 new_offset = old_offset = 0;
1134 for (ofd = old, nfd = new;
1135 (ofd - old < omax) && *ofd &&
1136 _rl_compare_chars(old, old_offset, &ps_old, new, new_offset, &ps_new); )
1137 {
1138 old_offset = _rl_find_next_mbchar (old, old_offset, 1, MB_FIND_ANY);
1139 new_offset = _rl_find_next_mbchar (new, new_offset, 1, MB_FIND_ANY);
1140 ofd = old + old_offset;
1141 nfd = new + new_offset;
1142 }
1143 }
1144 else
1145#endif
1146 for (ofd = old, nfd = new;
1147 (ofd - old < omax) && *ofd && (*ofd == *nfd);
1148 ofd++, nfd++)
1149 ;
1150
1151 /* Move to the end of the screen line. ND and OD are used to keep track
1152 of the distance between ne and new and oe and old, respectively, to
1153 move a subtraction out of each loop. */
1154 for (od = ofd - old, oe = ofd; od < omax && *oe; oe++, od++);
1155 for (nd = nfd - new, ne = nfd; nd < nmax && *ne; ne++, nd++);
1156
1157 /* If no difference, continue to next line. */
1158 if (ofd == oe && nfd == ne)
1159 return;
1160
1161 wsatend = 1; /* flag for trailing whitespace */
1162
1163#if defined (HANDLE_MULTIBYTE)
1164 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1165 {
1166 ols = old + _rl_find_prev_mbchar (old, oe - old, MB_FIND_ANY);
1167 nls = new + _rl_find_prev_mbchar (new, ne - new, MB_FIND_ANY);
1168 while ((ols > ofd) && (nls > nfd))
1169 {
1170 memset (&ps_old, 0, sizeof (mbstate_t));
1171 memset (&ps_new, 0, sizeof (mbstate_t));
1172
1173 _rl_adjust_point (old, ols - old, &ps_old);
1174 _rl_adjust_point (new, nls - new, &ps_new);
1175
1176 if (_rl_compare_chars (old, ols - old, &ps_old, new, nls - new, &ps_new) == 0)
1177 break;
1178
1179 if (*ols == ' ')
1180 wsatend = 0;
1181
1182 ols = old + _rl_find_prev_mbchar (old, ols - old, MB_FIND_ANY);
1183 nls = new + _rl_find_prev_mbchar (new, nls - new, MB_FIND_ANY);
1184 }
1185 }
1186 else
1187 {
1188#endif /* HANDLE_MULTIBYTE */
1189 ols = oe - 1; /* find last same */
1190 nls = ne - 1;
1191 while ((ols > ofd) && (nls > nfd) && (*ols == *nls))
1192 {
1193 if (*ols != ' ')
1194 wsatend = 0;
1195 ols--;
1196 nls--;
1197 }
1198#if defined (HANDLE_MULTIBYTE)
1199 }
1200#endif
1201
1202 if (wsatend)
1203 {
1204 ols = oe;
1205 nls = ne;
1206 }
1207#if defined (HANDLE_MULTIBYTE)
1208 /* This may not work for stateful encoding, but who cares? To handle
1209 stateful encoding properly, we have to scan each string from the
1210 beginning and compare. */
1211 else if (_rl_compare_chars (ols, 0, NULL, nls, 0, NULL) == 0)
1212#else
1213 else if (*ols != *nls)
1214#endif
1215 {
1216 if (*ols) /* don't step past the NUL */
1217 {
1218 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1219 ols = old + _rl_find_next_mbchar (old, ols - old, 1, MB_FIND_ANY);
1220 else
1221 ols++;
1222 }
1223 if (*nls)
1224 {
1225 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1226 nls = new + _rl_find_next_mbchar (new, nls - new, 1, MB_FIND_ANY);
1227 else
1228 nls++;
1229 }
1230 }
1231
1232 /* count of invisible characters in the current invisible line. */
1233 current_invis_chars = W_OFFSET (current_line, wrap_offset);
1234 if (_rl_last_v_pos != current_line)
1235 {
1236 _rl_move_vert (current_line);
1237 if (current_line == 0 && visible_wrap_offset)

--- 19 unchanged lines hidden (view full) ---

1257 od >= lendiff && _rl_last_c_pos <= prompt_last_invisible)
1258 {
1259#if defined (__MSDOS__)
1260 putc ('\r', rl_outstream);
1261#else
1262 tputs (_rl_term_cr, 1, _rl_output_character_function);
1263#endif
1264 _rl_output_some_chars (local_prompt, lendiff);
1265 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1266 _rl_last_c_pos = _rl_col_width (local_prompt, 0, lendiff);
1267 else
1268 _rl_last_c_pos = lendiff;
1269 }
1270
1271 _rl_move_cursor_relative (od, old);
1272
1273 /* if (len (new) > len (old))
1274 lendiff == difference in buffer
1275 col_lendiff == difference on screen
1276 When not using multibyte characters, these are equal */
1277 lendiff = (nls - nfd) - (ols - ofd);
1278 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1279 col_lendiff = _rl_col_width (new, nfd - new, nls - new) - _rl_col_width (old, ofd - old, ols - old);
1280 else
1281 col_lendiff = lendiff;
1282
1283 /* If we are changing the number of invisible characters in a line, and
1284 the spot of first difference is before the end of the invisible chars,
1285 lendiff needs to be adjusted. */
1286 if (current_line == 0 && !_rl_horizontal_scroll_mode &&
1287 current_invis_chars != visible_wrap_offset)
1288 {
1289 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1290 {
1291 lendiff += visible_wrap_offset - current_invis_chars;
1292 col_lendiff += visible_wrap_offset - current_invis_chars;
1293 }
1294 else
1295 {
1296 lendiff += visible_wrap_offset - current_invis_chars;
1297 col_lendiff = lendiff;
1298 }
1299 }
1300
1301 /* Insert (diff (len (old), len (new)) ch. */
1302 temp = ne - nfd;
1303 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1304 col_temp = _rl_col_width (new, nfd - new, ne - new);
1305 else
1306 col_temp = temp;
1307
1308 if (col_lendiff > 0) /* XXX - was lendiff */
1309 {
1310 /* Non-zero if we're increasing the number of lines. */
1311 int gl = current_line >= _rl_vis_botlin && inv_botlin > _rl_vis_botlin;
1312 /* Sometimes it is cheaper to print the characters rather than
1313 use the terminal's capabilities. If we're growing the number
1314 of lines, make sure we actually cause the new line to wrap
1315 around on auto-wrapping terminals. */
1316 if (_rl_terminal_can_insert && ((2 * col_temp) >= col_lendiff || _rl_term_IC) && (!_rl_term_autowrap || !gl))
1317 {
1318 /* If lendiff > prompt_visible_length and _rl_last_c_pos == 0 and
1319 _rl_horizontal_scroll_mode == 1, inserting the characters with
1320 _rl_term_IC or _rl_term_ic will screw up the screen because of the
1321 invisible characters. We need to just draw them. */
1322 if (*ols && (!_rl_horizontal_scroll_mode || _rl_last_c_pos > 0 ||
1323 lendiff <= prompt_visible_length || !current_invis_chars))
1324 {
1325 insert_some_chars (nfd, lendiff, col_lendiff);
1326 _rl_last_c_pos += col_lendiff;
1327 }
1328 else if (*ols == 0)
1329 {
1330 /* At the end of a line the characters do not have to
1331 be "inserted". They can just be placed on the screen. */
1332 /* However, this screws up the rest of this block, which
1333 assumes you've done the insert because you can. */
1334 _rl_output_some_chars (nfd, lendiff);
1335 _rl_last_c_pos += col_lendiff;
1336 }
1337 else
1338 {
1339 /* We have horizontal scrolling and we are not inserting at
1340 the end. We have invisible characters in this line. This
1341 is a dumb update. */
1342 _rl_output_some_chars (nfd, temp);
1343 _rl_last_c_pos += col_temp;
1344 return;
1345 }
1346 /* Copy (new) chars to screen from first diff to last match. */
1347 temp = nls - nfd;
1348 if ((temp - lendiff) > 0)
1349 {
1350 _rl_output_some_chars (nfd + lendiff, temp - lendiff);
1351#if 0
1352 _rl_last_c_pos += _rl_col_width (nfd+lendiff, 0, temp-lendiff) - col_lendiff;
1353#else
1354 _rl_last_c_pos += _rl_col_width (nfd+lendiff, 0, temp-col_lendiff);
1355#endif
1356 }
1357 }
1358 else
1359 {
1360 /* cannot insert chars, write to EOL */
1361 _rl_output_some_chars (nfd, temp);
1362 _rl_last_c_pos += col_temp;
1363 }
1364 }
1365 else /* Delete characters from line. */
1366 {
1367 /* If possible and inexpensive to use terminal deletion, then do so. */
1368 if (_rl_term_dc && (2 * col_temp) >= -col_lendiff)
1369 {
1370 /* If all we're doing is erasing the invisible characters in the
1371 prompt string, don't bother. It screws up the assumptions
1372 about what's on the screen. */
1373 if (_rl_horizontal_scroll_mode && _rl_last_c_pos == 0 &&
1374 -lendiff == visible_wrap_offset)
1375 col_lendiff = 0;
1376
1377 if (col_lendiff)
1378 delete_chars (-col_lendiff); /* delete (diff) characters */
1379
1380 /* Copy (new) chars to screen from first diff to last match */
1381 temp = nls - nfd;
1382 if (temp > 0)
1383 {
1384 _rl_output_some_chars (nfd, temp);
1385 _rl_last_c_pos += _rl_col_width (nfd, 0, temp);;
1386 }
1387 }
1388 /* Otherwise, print over the existing material. */
1389 else
1390 {
1391 if (temp > 0)
1392 {
1393 _rl_output_some_chars (nfd, temp);
1394 _rl_last_c_pos += col_temp;
1395 }
1396 lendiff = (oe - old) - (ne - new);
1397 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1398 col_lendiff = _rl_col_width (old, 0, oe - old) - _rl_col_width (new, 0, ne - new);
1399 else
1400 col_lendiff = lendiff;
1401
1402 if (col_lendiff)
1403 {
1404 if (_rl_term_autowrap && current_line < inv_botlin)
1405 space_to_eol (col_lendiff);
1406 else
1407 _rl_clear_to_eol (col_lendiff);
1408 }
1409 }
1410 }
1411}
1412
1413/* Tell the update routines that we have moved onto a new (empty) line. */
1414int
1415rl_on_new_line ()

--- 29 unchanged lines hidden (view full) ---

1445 strcpy (invisible_line, rl_prompt);
1446
1447 /* If the prompt contains newlines, take the last tail. */
1448 prompt_last_line = strrchr (rl_prompt, '\n');
1449 if (!prompt_last_line)
1450 prompt_last_line = rl_prompt;
1451
1452 l = strlen (prompt_last_line);
1453 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1454 _rl_last_c_pos = _rl_col_width (prompt_last_line, 0, l);
1455 else
1456 _rl_last_c_pos = l;
1457
1458 /* Dissect prompt_last_line into screen lines. Note that here we have
1459 to use the real screenwidth. Readline's notion of screenwidth might be
1460 one less, see terminal.c. */
1461 real_screenwidth = _rl_screenwidth + (_rl_term_autowrap ? 0 : 1);
1462 _rl_last_v_pos = l / real_screenwidth;
1463 /* If the prompt length is a multiple of real_screenwidth, we don't know
1464 whether the cursor is at the end of the last line, or already at the

--- 38 unchanged lines hidden (view full) ---

1503void
1504_rl_move_cursor_relative (new, data)
1505 int new;
1506 const char *data;
1507{
1508 register int i;
1509
1510 /* If we don't have to do anything, then return. */
1511#if defined (HANDLE_MULTIBYTE)
1512 /* If we have multibyte characters, NEW is indexed by the buffer point in
1513 a multibyte string, but _rl_last_c_pos is the display position. In
1514 this case, NEW's display position is not obvious. */
1515 if ((MB_CUR_MAX == 1 || rl_byte_oriented ) && _rl_last_c_pos == new) return;
1516#else
1517 if (_rl_last_c_pos == new) return;
1518#endif
1519
1520 /* It may be faster to output a CR, and then move forwards instead
1521 of moving backwards. */
1522 /* i == current physical cursor position. */
1523 i = _rl_last_c_pos - W_OFFSET(_rl_last_v_pos, visible_wrap_offset);
1524 if (new == 0 || CR_FASTER (new, _rl_last_c_pos) ||
1525 (_rl_term_autowrap && i == _rl_screenwidth))
1526 {

--- 13 unchanged lines hidden (view full) ---

1540
1541 /* The above comment is left here for posterity. It is faster
1542 to print one character (non-control) than to print a control
1543 sequence telling the terminal to move forward one character.
1544 That kind of control is for people who don't know what the
1545 data is underneath the cursor. */
1546#if defined (HACK_TERMCAP_MOTION)
1547 if (_rl_term_forward_char)
1548 {
1549 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1550 {
1551 int width;
1552 width = _rl_col_width (data, _rl_last_c_pos, new);
1553 for (i = 0; i < width; i++)
1554 tputs (_rl_term_forward_char, 1, _rl_output_character_function);
1555 }
1556 else
1557 {
1558 for (i = _rl_last_c_pos; i < new; i++)
1559 tputs (_rl_term_forward_char, 1, _rl_output_character_function);
1560 }
1561 }
1562 else if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1563 {
1564 tputs (_rl_term_cr, 1, _rl_output_character_function);
1565 for (i = 0; i < new; i++)
1566 putc (data[i], rl_outstream);
1567 }
1568 else
1569 for (i = _rl_last_c_pos; i < new; i++)
1570 putc (data[i], rl_outstream);
1571
1572#else /* !HACK_TERMCAP_MOTION */
1573
1574 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1575 {
1576 tputs (_rl_term_cr, 1, _rl_output_character_function);
1577 for (i = 0; i < new; i++)
1578 putc (data[i], rl_outstream);
1579 }
1580 else
1581 for (i = _rl_last_c_pos; i < new; i++)
1582 putc (data[i], rl_outstream);
1583
1584#endif /* !HACK_TERMCAP_MOTION */
1585
1586 }
1587#if defined (HANDLE_MULTIBYTE)
1588 /* NEW points to the buffer point, but _rl_last_c_pos is the display point.
1589 The byte length of the string is probably bigger than the column width
1590 of the string, which means that if NEW == _rl_last_c_pos, then NEW's
1591 display point is less than _rl_last_c_pos. */
1592 else if (_rl_last_c_pos >= new)
1593#else
1594 else if (_rl_last_c_pos > new)
1595#endif
1596 {
1597 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1598 {
1599 tputs (_rl_term_cr, 1, _rl_output_character_function);
1600 for (i = 0; i < new; i++)
1601 putc (data[i], rl_outstream);
1602 }
1603 else
1604 _rl_backspace (_rl_last_c_pos - new);
1605 }
1606
1607 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1608 _rl_last_c_pos = _rl_col_width (data, 0, new);
1609 else
1610 _rl_last_c_pos = new;
1611}
1612
1613/* PWP: move the cursor up or down. */
1614void
1615_rl_move_vert (to)
1616 int to;
1617{
1618 register int delta, i;

--- 70 unchanged lines hidden (view full) ---

1689#else
1690 return (2);
1691#endif /* !DISPLAY_TABS */
1692 }
1693
1694 if (CTRL_CHAR (c) || c == RUBOUT)
1695 return (2);
1696
1697 return ((ISPRINT (uc)) ? 1 : 2);
1698}
1699
1700/* How to print things in the "echo-area". The prompt is treated as a
1701 mini-modeline. */
1702
1703#if defined (USE_VARARGS)
1704int
1705#if defined (PREFER_STDARG)

--- 10 unchanged lines hidden (view full) ---

1716
1717#if defined (PREFER_STDARG)
1718 va_start (args, format);
1719#else
1720 va_start (args);
1721 format = va_arg (args, char *);
1722#endif
1723
1724#if defined (HAVE_VSNPRINTF)
1725 vsnprintf (msg_buf, sizeof (msg_buf) - 1, format, args);
1726#else
1727 vsprintf (msg_buf, format, args);
1728 msg_buf[sizeof(msg_buf) - 1] = '\0'; /* overflow? */
1729#endif
1730 va_end (args);
1731
1732 rl_display_prompt = msg_buf;
1733 (*rl_redisplay_function) ();
1734 return 0;
1735}
1736#else /* !USE_VARARGS */
1737int
1738rl_message (format, arg1, arg2)
1739 char *format;
1740{
1741 sprintf (msg_buf, format, arg1, arg2);
1742 msg_buf[sizeof(msg_buf) - 1] = '\0'; /* overflow? */
1743 rl_display_prompt = msg_buf;
1744 (*rl_redisplay_function) ();
1745 return 0;
1746}
1747#endif /* !USE_VARARGS */
1748
1749/* How to clear things from the "echo-area". */
1750int

--- 50 unchanged lines hidden (view full) ---

1801 int len;
1802 char *pmt;
1803
1804 rl_save_prompt ();
1805
1806 if (saved_local_prompt == 0)
1807 {
1808 len = (rl_prompt && *rl_prompt) ? strlen (rl_prompt) : 0;
1809 pmt = (char *)xmalloc (len + 2);
1810 if (len)
1811 strcpy (pmt, rl_prompt);
1812 pmt[len] = pchar;
1813 pmt[len+1] = '\0';
1814 }
1815 else
1816 {
1817 len = *saved_local_prompt ? strlen (saved_local_prompt) : 0;
1818 pmt = (char *)xmalloc (len + 2);
1819 if (len)
1820 strcpy (pmt, saved_local_prompt);
1821 pmt[len] = pchar;
1822 pmt[len+1] = '\0';
1823 local_prompt = savestring (pmt);
1824 prompt_last_invisible = saved_last_invisible;
1825 prompt_visible_length = saved_visible_length + 1;
1826 }

--- 46 unchanged lines hidden (view full) ---

1873_rl_clear_screen ()
1874{
1875 if (_rl_term_clrpag)
1876 tputs (_rl_term_clrpag, 1, _rl_output_character_function);
1877 else
1878 rl_crlf ();
1879}
1880
1881/* Insert COUNT characters from STRING to the output stream at column COL. */
1882static void
1883insert_some_chars (string, count, col)
1884 char *string;
1885 int count, col;
1886{
1887 /* DEBUGGING */
1888 if (MB_CUR_MAX == 1 || rl_byte_oriented)
1889 if (count != col)
1890 fprintf(stderr, "readline: debug: insert_some_chars: count (%d) != col (%d)\n", count, col);
1891
1892 /* If IC is defined, then we do not have to "enter" insert mode. */
1893 if (_rl_term_IC)
1894 {
1895 char *buffer;
1896
1897 buffer = tgoto (_rl_term_IC, 0, col);
1898 tputs (buffer, 1, _rl_output_character_function);
1899 _rl_output_some_chars (string, count);
1900 }
1901 else
1902 {
1903 register int i;
1904
1905 /* If we have to turn on insert-mode, then do so. */
1906 if (_rl_term_im && *_rl_term_im)
1907 tputs (_rl_term_im, 1, _rl_output_character_function);
1908
1909 /* If there is a special command for inserting characters, then
1910 use that first to open up the space. */
1911 if (_rl_term_ic && *_rl_term_ic)
1912 {
1913 for (i = col; i--; )
1914 tputs (_rl_term_ic, 1, _rl_output_character_function);
1915 }
1916
1917 /* Print the text. */
1918 _rl_output_some_chars (string, count);
1919
1920 /* If there is a string to turn off insert mode, we had best use
1921 it now. */

--- 38 unchanged lines hidden (view full) ---

1960 _rl_vis_botlin--;
1961 full_lines = 1;
1962 }
1963 _rl_move_vert (_rl_vis_botlin);
1964 /* If we've wrapped lines, remove the final xterm line-wrap flag. */
1965 if (full_lines && _rl_term_autowrap && (VIS_LLEN(_rl_vis_botlin) == _rl_screenwidth))
1966 {
1967 char *last_line;
1968
1969 last_line = &visible_line[vis_lbreaks[_rl_vis_botlin]];
1970 _rl_move_cursor_relative (_rl_screenwidth - 1, last_line);
1971 _rl_clear_to_eol (0);
1972 putc (last_line[_rl_screenwidth - 1], rl_outstream);
1973 }
1974 _rl_vis_botlin = 0;
1975 rl_crlf ();
1976 fflush (rl_outstream);
1977 rl_display_fixed++;

--- 128 unchanged lines hidden (view full) ---

2106
2107 if (nleft > 0)
2108 ret = 1 + nleft / _rl_screenwidth;
2109 else
2110 ret = 0;
2111
2112 return ret;
2113}
2114
2115#if defined (HANDLE_MULTIBYTE)
2116/* Calculate the number of screen columns occupied by STR from START to END.
2117 In the case of multibyte characters with stateful encoding, we have to
2118 scan from the beginning of the string to take the state into account. */
2119static int
2120_rl_col_width (str, start, end)
2121 char *str;
2122 int start, end;
2123{
2124 wchar_t wc;
2125 mbstate_t ps = {0};
2126 int tmp, point, width, max;
2127
2128 if (end <= start)
2129 return 0;
2130
2131 point = 0;
2132 max = end;
2133
2134 while (point < start)
2135 {
2136 tmp = mbrlen (str + point, max, &ps);
2137 if ((size_t)tmp == (size_t)-1 || (size_t)tmp == (size_t)-2)
2138 {
2139 /* In this case, the bytes are invalid or too short to compose a
2140 multibyte character, so we assume that the first byte represents
2141 a single character. */
2142 point++;
2143 max--;
2144
2145 /* Clear the state of the byte sequence, because in this case the
2146 effect of mbstate is undefined. */
2147 memset (&ps, 0, sizeof (mbstate_t));
2148 }
2149 else if (tmp == 0)
2150 break; /* Found '\0' */
2151 else
2152 {
2153 point += tmp;
2154 max -= tmp;
2155 }
2156 }
2157
2158 /* If START is not a byte that starts a character, then POINT will be
2159 greater than START. In this case, assume that (POINT - START) gives
2160 a byte count that is the number of columns of difference. */
2161 width = point - start;
2162
2163 while (point < end)
2164 {
2165 tmp = mbrtowc (&wc, str + point, max, &ps);
2166 if ((size_t)tmp == (size_t)-1 || (size_t)tmp == (size_t)-2)
2167 {
2168 /* In this case, the bytes are invalid or too short to compose a
2169 multibyte character, so we assume that the first byte represents
2170 a single character. */
2171 point++;
2172 max--;
2173
2174 /* and assume that the byte occupies a single column. */
2175 width++;
2176
2177 /* Clear the state of the byte sequence, because in this case the
2178 effect of mbstate is undefined. */
2179 memset (&ps, 0, sizeof (mbstate_t));
2180 }
2181 else if (tmp == 0)
2182 break; /* Found '\0' */
2183 else
2184 {
2185 point += tmp;
2186 max -= tmp;
2187 tmp = wcwidth(wc);
2188 width += (tmp >= 0) ? tmp : 1;
2189 }
2190 }
2191
2192 width += point - end;
2193
2194 return width;
2195}
2196#endif /* HANDLE_MULTIBYTE */
2197