1 /* display.c -- readline redisplay facility. */
3 /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
5 This file is part of the GNU Readline Library, a library for
6 reading lines of text with interactive input and history editing.
8 The GNU Readline Library is free software; you can redistribute it
9 and/or modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2, or
11 (at your option) any later version.
13 The GNU Readline Library is distributed in the hope that it will be
14 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 The GNU General Public License is often shipped with GNU software, and
19 is generally kept in a file called COPYING or LICENSE. If you do not
20 have a copy of the license, write to the Free Software Foundation,
21 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
22 #define READLINE_LIBRARY
24 #if defined (HAVE_CONFIG_H)
28 #include <sys/types.h>
30 #if defined (HAVE_UNISTD_H)
32 #endif /* HAVE_UNISTD_H */
34 #include "posixstat.h"
36 #if defined (HAVE_STDLIB_H)
39 # include "ansi_stdlib.h"
40 #endif /* HAVE_STDLIB_H */
48 /* System-specific feature definitions and include files. */
52 /* Termcap library stuff. */
55 /* Some standard library routines. */
59 #include "rlprivate.h"
62 #if !defined (strchr) && !defined (__STDC__)
63 extern char *strchr (), *strrchr ();
64 #endif /* !strchr && !__STDC__ */
66 #if defined (HACK_TERMCAP_MOTION)
67 extern char *_rl_term_forward_char;
70 static void update_line PARAMS((char *, char *, int, int, int, int));
71 static void space_to_eol PARAMS((int));
72 static void delete_chars PARAMS((int));
73 static void insert_some_chars PARAMS((char *, int, int));
74 static void cr PARAMS((void));
76 #if defined (HANDLE_MULTIBYTE)
77 static int _rl_col_width PARAMS((const char *, int, int));
78 static int *_rl_wrapped_line;
80 # define _rl_col_width(l, s, e) (((e) <= (s)) ? 0 : (e) - (s))
83 static int *inv_lbreaks, *vis_lbreaks;
84 static int inv_lbsize, vis_lbsize;
86 /* Heuristic used to decide whether it is faster to move from CUR to NEW
87 by backing up or outputting a carriage return and moving forward. */
88 #define CR_FASTER(new, cur) (((new) + 1) < ((cur) - (new)))
90 /* **************************************************************** */
94 /* **************************************************************** */
96 /* This is the stuff that is hard for me. I never seem to write good
97 display routines in C. Let's see how I do this time. */
99 /* (PWP) Well... Good for a simple line updater, but totally ignores
100 the problems of input lines longer than the screen width.
102 update_line and the code that calls it makes a multiple line,
103 automatically wrapping line update. Careful attention needs
104 to be paid to the vertical position variables. */
106 /* Keep two buffers; one which reflects the current contents of the
107 screen, and the other to draw what we think the new contents should
108 be. Then compare the buffers, and make whatever changes to the
109 screen itself that we should. Finally, make the buffer that we
110 just drew into be the one which reflects the current contents of the
111 screen, and place the cursor where it belongs.
113 Commands that want to can fix the display themselves, and then let
114 this function know that the display has been fixed by setting the
115 RL_DISPLAY_FIXED variable. This is good for efficiency. */
117 /* Application-specific redisplay function. */
118 rl_voidfunc_t *rl_redisplay_function = rl_redisplay;
120 /* Global variables declared here. */
121 /* What YOU turn on when you have handled all redisplay yourself. */
122 int rl_display_fixed = 0;
124 int _rl_suppress_redisplay = 0;
126 /* The stuff that gets printed out before the actual text of the line.
127 This is usually pointing to rl_prompt. */
128 char *rl_display_prompt = (char *)NULL;
130 /* Pseudo-global variables declared here. */
131 /* The visible cursor position. If you print some text, adjust this. */
132 int _rl_last_c_pos = 0;
133 int _rl_last_v_pos = 0;
135 /* Number of lines currently on screen minus 1. */
136 int _rl_vis_botlin = 0;
138 /* Variables used only in this file. */
139 /* The last left edge of text that was displayed. This is used when
140 doing horizontal scrolling. It shifts in thirds of a screenwidth. */
141 static int last_lmargin;
143 /* The line display buffers. One is the line currently displayed on
144 the screen. The other is the line about to be displayed. */
145 static char *visible_line = (char *)NULL;
146 static char *invisible_line = (char *)NULL;
148 /* A buffer for `modeline' messages. */
149 static char msg_buf[128];
151 /* Non-zero forces the redisplay even if we thought it was unnecessary. */
152 static int forced_display;
154 /* Default and initial buffer size. Can grow. */
155 static int line_size = 1024;
157 /* Variables to keep track of the expanded prompt string, which may
158 include invisible characters. */
160 static char *local_prompt, *local_prompt_prefix;
161 static int prompt_visible_length, prompt_prefix_length;
163 /* The number of invisible characters in the line currently being
164 displayed on the screen. */
165 static int visible_wrap_offset;
167 /* The number of invisible characters in the prompt string. Static so it
168 can be shared between rl_redisplay and update_line */
169 static int wrap_offset;
171 /* The index of the last invisible character in the prompt string. */
172 static int prompt_last_invisible;
174 /* The length (buffer offset) of the first line of the last (possibly
175 multi-line) buffer displayed on the screen. */
176 static int visible_first_line_len;
178 /* Number of invisible characters on the first physical line of the prompt.
179 Only valid when the number of physical characters in the prompt exceeds
180 (or is equal to) _rl_screenwidth. */
181 static int prompt_invis_chars_first_line;
183 static int prompt_last_screen_line;
185 /* Expand the prompt string S and return the number of visible
186 characters in *LP, if LP is not null. This is currently more-or-less
187 a placeholder for expansion. LIP, if non-null is a place to store the
188 index of the last invisible character in the returned string. NIFLP,
189 if non-zero, is a place to store the number of invisible characters in
190 the first prompt line. */
192 /* Current implementation:
193 \001 (^A) start non-visible characters
194 \002 (^B) end non-visible characters
195 all characters except \001 and \002 (following a \001) are copied to
196 the returned string; all characters except those between \001 and
197 \002 are assumed to be `visible'. */
200 expand_prompt (pmt, lp, lip, niflp)
202 int *lp, *lip, *niflp;
205 int l, rl, last, ignoring, ninvis, invfl;
207 /* Short-circuit if we can. */
208 if (strchr (pmt, RL_PROMPT_START_IGNORE) == 0)
210 r = savestring (pmt);
217 r = ret = (char *)xmalloc (l + 1);
219 invfl = 0; /* invisible chars in first line of prompt */
221 for (rl = ignoring = last = ninvis = 0, p = pmt; p && *p; p++)
223 /* This code strips the invisible character string markers
224 RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE */
225 if (*p == RL_PROMPT_START_IGNORE)
230 else if (ignoring && *p == RL_PROMPT_END_IGNORE)
243 if (rl == _rl_screenwidth)
248 if (rl < _rl_screenwidth)
261 /* Just strip out RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE from
262 PMT and return the rest of PMT. */
264 _rl_strip_prompt (pmt)
269 ret = expand_prompt (pmt, (int *)NULL, (int *)NULL, (int *)NULL);
274 * Expand the prompt string into the various display components, if
277 * local_prompt = expanded last line of string in rl_display_prompt
278 * (portion after the final newline)
279 * local_prompt_prefix = portion before last newline of rl_display_prompt,
280 * expanded via expand_prompt
281 * prompt_visible_length = number of visible characters in local_prompt
282 * prompt_prefix_length = number of visible characters in local_prompt_prefix
284 * This function is called once per call to readline(). It may also be
285 * called arbitrarily to expand the primary prompt.
287 * The return value is the number of visible characters on the last line
288 * of the (possibly multi-line) prompt.
291 rl_expand_prompt (prompt)
297 /* Clear out any saved values. */
299 FREE (local_prompt_prefix);
301 local_prompt = local_prompt_prefix = (char *)0;
302 prompt_last_invisible = prompt_visible_length = 0;
304 if (prompt == 0 || *prompt == 0)
307 p = strrchr (prompt, '\n');
310 /* The prompt is only one logical line, though it might wrap. */
311 local_prompt = expand_prompt (prompt, &prompt_visible_length,
312 &prompt_last_invisible,
313 &prompt_invis_chars_first_line);
314 local_prompt_prefix = (char *)0;
315 return (prompt_visible_length);
319 /* The prompt spans multiple lines. */
321 local_prompt = expand_prompt (p, &prompt_visible_length,
322 &prompt_last_invisible,
323 &prompt_invis_chars_first_line);
325 /* The portion of the prompt string up to and including the
326 final newline is now null-terminated. */
327 local_prompt_prefix = expand_prompt (prompt, &prompt_prefix_length,
329 &prompt_invis_chars_first_line);
331 return (prompt_prefix_length);
335 /* Initialize the VISIBLE_LINE and INVISIBLE_LINE arrays, and their associated
336 arrays of line break markers. MINSIZE is the minimum size of VISIBLE_LINE
337 and INVISIBLE_LINE; if it is greater than LINE_SIZE, LINE_SIZE is
338 increased. If the lines have already been allocated, this ensures that
339 they can hold at least MINSIZE characters. */
341 init_line_structures (minsize)
346 if (invisible_line == 0) /* initialize it */
348 if (line_size < minsize)
350 visible_line = (char *)xmalloc (line_size);
351 invisible_line = (char *)xmalloc (line_size);
353 else if (line_size < minsize) /* ensure it can hold MINSIZE chars */
356 if (line_size < minsize)
358 visible_line = (char *)xrealloc (visible_line, line_size);
359 invisible_line = (char *)xrealloc (invisible_line, line_size);
362 for (n = minsize; n < line_size; n++)
365 invisible_line[n] = 1;
368 if (vis_lbreaks == 0)
370 /* should be enough. */
371 inv_lbsize = vis_lbsize = 256;
372 inv_lbreaks = (int *)xmalloc (inv_lbsize * sizeof (int));
373 vis_lbreaks = (int *)xmalloc (vis_lbsize * sizeof (int));
374 #if defined (HANDLE_MULTIBYTE)
375 _rl_wrapped_line = (int *)xmalloc (vis_lbsize * sizeof (int));
377 inv_lbreaks[0] = vis_lbreaks[0] = 0;
381 /* Basic redisplay algorithm. */
385 register int in, out, c, linenum, cursor_linenum;
387 int c_pos, inv_botlin, lb_botlin, lb_linenum;
388 int newlines, lpos, temp;
389 char *prompt_this_line;
390 #if defined (HANDLE_MULTIBYTE)
395 int _rl_wrapped_multicolumn = 0;
398 if (!readline_echoing_p)
401 if (!rl_display_prompt)
402 rl_display_prompt = "";
404 if (invisible_line == 0)
406 init_line_structures (0);
410 /* Draw the line into the buffer. */
413 line = invisible_line;
414 out = inv_botlin = 0;
416 /* Mark the line as modified or not. We only do this for history
418 if (_rl_mark_modified_lines && current_history () && rl_undo_list)
424 /* If someone thought that the redisplay was handled, but the currently
425 visible line has a different modification state than the one about
426 to become visible, then correct the caller's misconception. */
427 if (visible_line[0] != invisible_line[0])
428 rl_display_fixed = 0;
430 /* If the prompt to be displayed is the `primary' readline prompt (the
431 one passed to readline()), use the values we have already expanded.
432 If not, use what's already in rl_display_prompt. WRAP_OFFSET is the
433 number of non-visible characters in the prompt string. */
434 if (rl_display_prompt == rl_prompt || local_prompt)
436 int local_len = local_prompt ? strlen (local_prompt) : 0;
437 if (local_prompt_prefix && forced_display)
438 _rl_output_some_chars (local_prompt_prefix, strlen (local_prompt_prefix));
442 temp = local_len + out + 2;
443 if (temp >= line_size)
445 line_size = (temp + 1024) - (temp % 1024);
446 visible_line = (char *)xrealloc (visible_line, line_size);
447 line = invisible_line = (char *)xrealloc (invisible_line, line_size);
449 strncpy (line + out, local_prompt, local_len);
453 wrap_offset = local_len - prompt_visible_length;
458 prompt_this_line = strrchr (rl_display_prompt, '\n');
459 if (!prompt_this_line)
460 prompt_this_line = rl_display_prompt;
464 pmtlen = prompt_this_line - rl_display_prompt; /* temp var */
467 _rl_output_some_chars (rl_display_prompt, pmtlen);
468 /* Make sure we are at column zero even after a newline,
469 regardless of the state of terminal output processing. */
470 if (pmtlen < 2 || prompt_this_line[-2] != '\r')
475 pmtlen = strlen (prompt_this_line);
476 temp = pmtlen + out + 2;
477 if (temp >= line_size)
479 line_size = (temp + 1024) - (temp % 1024);
480 visible_line = (char *)xrealloc (visible_line, line_size);
481 line = invisible_line = (char *)xrealloc (invisible_line, line_size);
483 strncpy (line + out, prompt_this_line, pmtlen);
486 wrap_offset = prompt_invis_chars_first_line = 0;
489 #define CHECK_INV_LBREAKS() \
491 if (newlines >= (inv_lbsize - 2)) \
494 inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
498 #if defined (HANDLE_MULTIBYTE)
499 #define CHECK_LPOS() \
502 if (lpos >= _rl_screenwidth) \
504 if (newlines >= (inv_lbsize - 2)) \
507 inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
508 _rl_wrapped_line = (int *)xrealloc (_rl_wrapped_line, inv_lbsize * sizeof (int)); \
510 inv_lbreaks[++newlines] = out; \
511 _rl_wrapped_line[newlines] = _rl_wrapped_multicolumn; \
516 #define CHECK_LPOS() \
519 if (lpos >= _rl_screenwidth) \
521 if (newlines >= (inv_lbsize - 2)) \
524 inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
526 inv_lbreaks[++newlines] = out; \
532 /* inv_lbreaks[i] is where line i starts in the buffer. */
533 inv_lbreaks[newlines = 0] = 0;
534 lpos = out - wrap_offset;
535 #if defined (HANDLE_MULTIBYTE)
536 memset (_rl_wrapped_line, 0, vis_lbsize);
539 /* prompt_invis_chars_first_line is the number of invisible characters in
540 the first physical line of the prompt.
541 wrap_offset - prompt_invis_chars_first_line is the number of invis
542 chars on the second line. */
544 /* what if lpos is already >= _rl_screenwidth before we start drawing the
545 contents of the command line? */
546 while (lpos >= _rl_screenwidth)
548 /* fix from Darin Johnson <darin@acuson.com> for prompt string with
549 invisible characters that is longer than the screen width. The
550 prompt_invis_chars_first_line variable could be made into an array
551 saying how many invisible characters there are per line, but that's
552 probably too much work for the benefit gained. How many people have
553 prompts that exceed two physical lines? */
554 temp = ((newlines + 1) * _rl_screenwidth) +
556 ((newlines == 0) ? prompt_invis_chars_first_line : 0) +
558 ((newlines == 0 && local_prompt_prefix == 0) ? prompt_invis_chars_first_line : 0) +
560 ((newlines == 1) ? wrap_offset : 0);
562 inv_lbreaks[++newlines] = temp;
563 lpos -= _rl_screenwidth;
566 prompt_last_screen_line = newlines;
568 /* Draw the rest of the line (after the prompt) into invisible_line, keeping
569 track of where the cursor is (c_pos), the number of the line containing
570 the cursor (lb_linenum), the last line number (lb_botlin and inv_botlin).
571 It maintains an array of line breaks for display (inv_lbreaks).
572 This handles expanding tabs for display and displaying meta characters. */
574 #if defined (HANDLE_MULTIBYTE)
576 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
578 memset (&ps, 0, sizeof (mbstate_t));
579 wc_bytes = mbrtowc (&wc, rl_line_buffer, rl_end, &ps);
585 for (in = 0; in < rl_end; in++)
588 c = (unsigned char)rl_line_buffer[in];
590 #if defined (HANDLE_MULTIBYTE)
591 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
593 if (wc_bytes == (size_t)-1 || wc_bytes == (size_t)-2)
595 /* Byte sequence is invalid or shortened. Assume that the
596 first byte represents a character. */
598 /* Assume that a character occupies a single column. */
600 memset (&ps, 0, sizeof (mbstate_t));
602 else if (wc_bytes == (size_t)0)
603 break; /* Found '\0' */
607 wc_width = (temp < 0) ? 1 : temp;
612 if (out + 8 >= line_size) /* XXX - 8 for \t */
615 visible_line = (char *)xrealloc (visible_line, line_size);
616 invisible_line = (char *)xrealloc (invisible_line, line_size);
617 line = invisible_line;
623 lb_linenum = newlines;
626 #if defined (HANDLE_MULTIBYTE)
627 if (META_CHAR (c) && _rl_output_meta_chars == 0) /* XXX - clean up */
632 if (_rl_output_meta_chars == 0)
634 sprintf (line + out, "\\%o", c);
636 if (lpos + 4 >= _rl_screenwidth)
638 temp = _rl_screenwidth - lpos;
639 CHECK_INV_LBREAKS ();
640 inv_lbreaks[++newlines] = out + temp;
654 #if defined (DISPLAY_TABS)
660 newout = (out | (int)7) + 1;
662 newout = out + 8 - lpos % 8;
665 if (lpos + temp >= _rl_screenwidth)
668 temp2 = _rl_screenwidth - lpos;
669 CHECK_INV_LBREAKS ();
670 inv_lbreaks[++newlines] = out + temp2;
683 else if (c == '\n' && _rl_horizontal_scroll_mode == 0 && _rl_term_up && *_rl_term_up)
685 line[out++] = '\0'; /* XXX - sentinel */
686 CHECK_INV_LBREAKS ();
687 inv_lbreaks[++newlines] = out;
690 else if (CTRL_CHAR (c) || c == RUBOUT)
694 line[out++] = CTRL_CHAR (c) ? UNCTRL (c) : '?';
699 #if defined (HANDLE_MULTIBYTE)
700 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
704 _rl_wrapped_multicolumn = 0;
706 if (_rl_screenwidth < lpos + wc_width)
707 for (i = lpos; i < _rl_screenwidth; i++)
709 /* The space will be removed in update_line() */
711 _rl_wrapped_multicolumn++;
717 lb_linenum = newlines;
719 for (i = in; i < in+wc_bytes; i++)
720 line[out++] = rl_line_buffer[i];
721 for (i = 0; i < wc_width; i++)
735 #if defined (HANDLE_MULTIBYTE)
736 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
739 wc_bytes = mbrtowc (&wc, rl_line_buffer + in, rl_end - in, &ps);
750 lb_linenum = newlines;
753 inv_botlin = lb_botlin = newlines;
754 CHECK_INV_LBREAKS ();
755 inv_lbreaks[newlines+1] = out;
756 cursor_linenum = lb_linenum;
758 /* C_POS == position in buffer where cursor should be placed.
759 CURSOR_LINENUM == line number where the cursor should be placed. */
761 /* PWP: now is when things get a bit hairy. The visible and invisible
762 line buffers are really multiple lines, which would wrap every
763 (screenwidth - 1) characters. Go through each in turn, finding
764 the changed region and updating it. The line order is top to bottom. */
766 /* If we can move the cursor up and down, then use multiple lines,
767 otherwise, let long lines display in a single terminal line, and
768 horizontally scroll it. */
770 if (_rl_horizontal_scroll_mode == 0 && _rl_term_up && *_rl_term_up)
772 int nleft, pos, changed_screen_line;
774 if (!rl_display_fixed || forced_display)
778 /* If we have more than a screenful of material to display, then
779 only display a screenful. We should display the last screen,
781 if (out >= _rl_screenchars)
783 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
784 out = _rl_find_prev_mbchar (line, _rl_screenchars, MB_FIND_ANY);
786 out = _rl_screenchars - 1;
789 /* The first line is at character position 0 in the buffer. The
790 second and subsequent lines start at inv_lbreaks[N], offset by
791 OFFSET (which has already been calculated above). */
793 #define W_OFFSET(line, offset) ((line) == 0 ? offset : 0)
794 #define VIS_LLEN(l) ((l) > _rl_vis_botlin ? 0 : (vis_lbreaks[l+1] - vis_lbreaks[l]))
795 #define INV_LLEN(l) (inv_lbreaks[l+1] - inv_lbreaks[l])
796 #define VIS_CHARS(line) (visible_line + vis_lbreaks[line])
797 #define VIS_LINE(line) ((line) > _rl_vis_botlin) ? "" : VIS_CHARS(line)
798 #define INV_LINE(line) (invisible_line + inv_lbreaks[line])
800 /* For each line in the buffer, do the updating display. */
801 for (linenum = 0; linenum <= inv_botlin; linenum++)
803 update_line (VIS_LINE(linenum), INV_LINE(linenum), linenum,
804 VIS_LLEN(linenum), INV_LLEN(linenum), inv_botlin);
806 /* If this is the line with the prompt, we might need to
807 compensate for invisible characters in the new line. Do
808 this only if there is not more than one new line (which
809 implies that we completely overwrite the old visible line)
810 and the new line is shorter than the old. Make sure we are
811 at the end of the new line before clearing. */
813 inv_botlin == 0 && _rl_last_c_pos == out &&
814 (wrap_offset > visible_wrap_offset) &&
815 (_rl_last_c_pos < visible_first_line_len))
817 nleft = _rl_screenwidth + wrap_offset - _rl_last_c_pos;
819 _rl_clear_to_eol (nleft);
822 /* Since the new first line is now visible, save its length. */
824 visible_first_line_len = (inv_botlin > 0) ? inv_lbreaks[1] : out - wrap_offset;
827 /* We may have deleted some lines. If so, clear the left over
828 blank ones at the bottom out. */
829 if (_rl_vis_botlin > inv_botlin)
832 for (; linenum <= _rl_vis_botlin; linenum++)
834 tt = VIS_CHARS (linenum);
835 _rl_move_vert (linenum);
836 _rl_move_cursor_relative (0, tt);
838 ((linenum == _rl_vis_botlin) ? strlen (tt) : _rl_screenwidth);
841 _rl_vis_botlin = inv_botlin;
843 /* CHANGED_SCREEN_LINE is set to 1 if we have moved to a
844 different screen line during this redisplay. */
845 changed_screen_line = _rl_last_v_pos != cursor_linenum;
846 if (changed_screen_line)
848 _rl_move_vert (cursor_linenum);
849 /* If we moved up to the line with the prompt using _rl_term_up,
850 the physical cursor position on the screen stays the same,
851 but the buffer position needs to be adjusted to account
852 for invisible characters. */
853 if (cursor_linenum == 0 && wrap_offset)
854 _rl_last_c_pos += wrap_offset;
857 /* We have to reprint the prompt if it contains invisible
858 characters, since it's not generally OK to just reprint
859 the characters from the current cursor position. But we
860 only need to reprint it if the cursor is before the last
861 invisible character in the prompt string. */
862 nleft = prompt_visible_length + wrap_offset;
863 if (cursor_linenum == 0 && wrap_offset > 0 && _rl_last_c_pos > 0 &&
864 _rl_last_c_pos <= prompt_last_invisible && local_prompt)
866 #if defined (__MSDOS__)
867 putc ('\r', rl_outstream);
870 tputs (_rl_term_cr, 1, _rl_output_character_function);
872 _rl_output_some_chars (local_prompt, nleft);
873 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
874 _rl_last_c_pos = _rl_col_width(local_prompt, 0, nleft);
876 _rl_last_c_pos = nleft;
879 /* Where on that line? And where does that line start
881 pos = inv_lbreaks[cursor_linenum];
882 /* nleft == number of characters in the line buffer between the
883 start of the line and the cursor position. */
886 /* Since _rl_backspace() doesn't know about invisible characters in the
887 prompt, and there's no good way to tell it, we compensate for
888 those characters here and call _rl_backspace() directly. */
889 if (wrap_offset && cursor_linenum == 0 && nleft < _rl_last_c_pos)
891 _rl_backspace (_rl_last_c_pos - nleft);
892 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
893 _rl_last_c_pos = _rl_col_width (&visible_line[pos], 0, nleft);
895 _rl_last_c_pos = nleft;
898 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
899 _rl_move_cursor_relative (nleft, &invisible_line[pos]);
900 else if (nleft != _rl_last_c_pos)
901 _rl_move_cursor_relative (nleft, &invisible_line[pos]);
904 else /* Do horizontal scrolling. */
906 #define M_OFFSET(margin, offset) ((margin) == 0 ? offset : 0)
907 int lmargin, ndisp, nleft, phys_c_pos, t;
909 /* Always at top line. */
912 /* Compute where in the buffer the displayed line should start. This
915 /* The number of characters that will be displayed before the cursor. */
916 ndisp = c_pos - wrap_offset;
917 nleft = prompt_visible_length + wrap_offset;
918 /* Where the new cursor position will be on the screen. This can be
919 longer than SCREENWIDTH; if it is, lmargin will be adjusted. */
920 phys_c_pos = c_pos - (last_lmargin ? last_lmargin : wrap_offset);
921 t = _rl_screenwidth / 3;
923 /* If the number of characters had already exceeded the screenwidth,
924 last_lmargin will be > 0. */
926 /* If the number of characters to be displayed is more than the screen
927 width, compute the starting offset so that the cursor is about
928 two-thirds of the way across the screen. */
929 if (phys_c_pos > _rl_screenwidth - 2)
931 lmargin = c_pos - (2 * t);
934 /* If the left margin would be in the middle of a prompt with
935 invisible characters, don't display the prompt at all. */
936 if (wrap_offset && lmargin > 0 && lmargin < nleft)
939 else if (ndisp < _rl_screenwidth - 2) /* XXX - was -1 */
941 else if (phys_c_pos < 1)
943 /* If we are moving back towards the beginning of the line and
944 the last margin is no longer correct, compute a new one. */
945 lmargin = ((c_pos - 1) / t) * t; /* XXX */
946 if (wrap_offset && lmargin > 0 && lmargin < nleft)
950 lmargin = last_lmargin;
952 /* If the first character on the screen isn't the first character
953 in the display line, indicate this with a special character. */
957 /* If SCREENWIDTH characters starting at LMARGIN do not encompass
958 the whole line, indicate that with a special character at the
959 right edge of the screen. If LMARGIN is 0, we need to take the
960 wrap offset into account. */
961 t = lmargin + M_OFFSET (lmargin, wrap_offset) + _rl_screenwidth;
965 if (!rl_display_fixed || forced_display || lmargin != last_lmargin)
968 update_line (&visible_line[last_lmargin],
969 &invisible_line[lmargin],
971 _rl_screenwidth + visible_wrap_offset,
972 _rl_screenwidth + (lmargin ? 0 : wrap_offset),
975 /* If the visible new line is shorter than the old, but the number
976 of invisible characters is greater, and we are at the end of
977 the new line, we need to clear to eol. */
978 t = _rl_last_c_pos - M_OFFSET (lmargin, wrap_offset);
979 if ((M_OFFSET (lmargin, wrap_offset) > visible_wrap_offset) &&
980 (_rl_last_c_pos == out) &&
981 t < visible_first_line_len)
983 nleft = _rl_screenwidth - t;
984 _rl_clear_to_eol (nleft);
986 visible_first_line_len = out - lmargin - M_OFFSET (lmargin, wrap_offset);
987 if (visible_first_line_len > _rl_screenwidth)
988 visible_first_line_len = _rl_screenwidth;
990 _rl_move_cursor_relative (c_pos - lmargin, &invisible_line[lmargin]);
991 last_lmargin = lmargin;
994 fflush (rl_outstream);
996 /* Swap visible and non-visible lines. */
998 char *vtemp = visible_line;
999 int *itemp = vis_lbreaks, ntemp = vis_lbsize;
1001 visible_line = invisible_line;
1002 invisible_line = vtemp;
1004 vis_lbreaks = inv_lbreaks;
1005 inv_lbreaks = itemp;
1007 vis_lbsize = inv_lbsize;
1010 rl_display_fixed = 0;
1011 /* If we are displaying on a single line, and last_lmargin is > 0, we
1012 are not displaying any invisible characters, so set visible_wrap_offset
1014 if (_rl_horizontal_scroll_mode && last_lmargin)
1015 visible_wrap_offset = 0;
1017 visible_wrap_offset = wrap_offset;
1021 /* PWP: update_line() is based on finding the middle difference of each
1022 line on the screen; vis:
1024 /old first difference
1025 /beginning of line | /old last same /old EOL
1027 old: eddie> Oh, my little gruntle-buggy is to me, as lurgid as
1028 new: eddie> Oh, my little buggy says to me, as lurgid as
1030 \beginning of line | \new last same \new end of line
1031 \new first difference
1033 All are character pointers for the sake of speed. Special cases for
1034 no differences, as well as for end of line additions must be handled.
1036 Could be made even smarter, but this works well enough */
1038 update_line (old, new, current_line, omax, nmax, inv_botlin)
1039 register char *old, *new;
1040 int current_line, omax, nmax, inv_botlin;
1042 register char *ofd, *ols, *oe, *nfd, *nls, *ne;
1043 int temp, lendiff, wsatend, od, nd;
1044 int current_invis_chars;
1045 int col_lendiff, col_temp;
1046 #if defined (HANDLE_MULTIBYTE)
1047 mbstate_t ps_new, ps_old;
1048 int new_offset, old_offset, tmp;
1051 /* If we're at the right edge of a terminal that supports xn, we're
1052 ready to wrap around, so do so. This fixes problems with knowing
1053 the exact cursor position and cut-and-paste with certain terminal
1054 emulators. In this calculation, TEMP is the physical screen
1055 position of the cursor. */
1056 temp = _rl_last_c_pos - W_OFFSET(_rl_last_v_pos, visible_wrap_offset);
1057 if (temp == _rl_screenwidth && _rl_term_autowrap && !_rl_horizontal_scroll_mode
1058 && _rl_last_v_pos == current_line - 1)
1060 #if defined (HANDLE_MULTIBYTE)
1061 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1065 int tempwidth, bytes;
1068 /* This fixes only double-column characters, but if the wrapped
1069 character comsumes more than three columns, spaces will be
1070 inserted in the string buffer. */
1071 if (_rl_wrapped_line[current_line] > 0)
1072 _rl_clear_to_eol (_rl_wrapped_line[current_line]);
1074 memset (&ps, 0, sizeof (mbstate_t));
1075 ret = mbrtowc (&wc, new, MB_CUR_MAX, &ps);
1076 if (ret == (size_t)-1 || ret == (size_t)-2)
1084 tempwidth = wcwidth (wc);
1090 for (count = 0; count < bytes; count++)
1091 putc (new[count], rl_outstream);
1092 _rl_last_c_pos = tempwidth;
1094 memset (&ps, 0, sizeof (mbstate_t));
1095 ret = mbrtowc (&wc, old, MB_CUR_MAX, &ps);
1096 if (ret != 0 && bytes != 0)
1098 if (ret == (size_t)-1 || ret == (size_t)-2)
1099 memmove (old+bytes, old+1, strlen (old+1));
1101 memmove (old+bytes, old+ret, strlen (old+ret));
1102 memcpy (old, new, bytes);
1107 putc (' ', rl_outstream);
1110 if (old[0] && new[0])
1118 putc (new[0], rl_outstream);
1120 putc (' ', rl_outstream);
1121 _rl_last_c_pos = 1; /* XXX */
1123 if (old[0] && new[0])
1129 /* Find first difference. */
1130 #if defined (HANDLE_MULTIBYTE)
1131 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1133 memset (&ps_new, 0, sizeof(mbstate_t));
1134 memset (&ps_old, 0, sizeof(mbstate_t));
1136 new_offset = old_offset = 0;
1137 for (ofd = old, nfd = new;
1138 (ofd - old < omax) && *ofd &&
1139 _rl_compare_chars(old, old_offset, &ps_old, new, new_offset, &ps_new); )
1141 old_offset = _rl_find_next_mbchar (old, old_offset, 1, MB_FIND_ANY);
1142 new_offset = _rl_find_next_mbchar (new, new_offset, 1, MB_FIND_ANY);
1143 ofd = old + old_offset;
1144 nfd = new + new_offset;
1149 for (ofd = old, nfd = new;
1150 (ofd - old < omax) && *ofd && (*ofd == *nfd);
1154 /* Move to the end of the screen line. ND and OD are used to keep track
1155 of the distance between ne and new and oe and old, respectively, to
1156 move a subtraction out of each loop. */
1157 for (od = ofd - old, oe = ofd; od < omax && *oe; oe++, od++);
1158 for (nd = nfd - new, ne = nfd; nd < nmax && *ne; ne++, nd++);
1160 /* If no difference, continue to next line. */
1161 if (ofd == oe && nfd == ne)
1164 wsatend = 1; /* flag for trailing whitespace */
1166 #if defined (HANDLE_MULTIBYTE)
1167 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1169 ols = old + _rl_find_prev_mbchar (old, oe - old, MB_FIND_ANY);
1170 nls = new + _rl_find_prev_mbchar (new, ne - new, MB_FIND_ANY);
1171 while ((ols > ofd) && (nls > nfd))
1173 memset (&ps_old, 0, sizeof (mbstate_t));
1174 memset (&ps_new, 0, sizeof (mbstate_t));
1176 _rl_adjust_point (old, ols - old, &ps_old);
1177 _rl_adjust_point (new, nls - new, &ps_new);
1179 if (_rl_compare_chars (old, ols - old, &ps_old, new, nls - new, &ps_new) == 0)
1185 ols = old + _rl_find_prev_mbchar (old, ols - old, MB_FIND_ANY);
1186 nls = new + _rl_find_prev_mbchar (new, nls - new, MB_FIND_ANY);
1191 #endif /* HANDLE_MULTIBYTE */
1192 ols = oe - 1; /* find last same */
1194 while ((ols > ofd) && (nls > nfd) && (*ols == *nls))
1201 #if defined (HANDLE_MULTIBYTE)
1210 #if defined (HANDLE_MULTIBYTE)
1211 /* This may not work for stateful encoding, but who cares? To handle
1212 stateful encoding properly, we have to scan each string from the
1213 beginning and compare. */
1214 else if (_rl_compare_chars (ols, 0, NULL, nls, 0, NULL) == 0)
1216 else if (*ols != *nls)
1219 if (*ols) /* don't step past the NUL */
1221 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1222 ols = old + _rl_find_next_mbchar (old, ols - old, 1, MB_FIND_ANY);
1228 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1229 nls = new + _rl_find_next_mbchar (new, nls - new, 1, MB_FIND_ANY);
1235 /* count of invisible characters in the current invisible line. */
1236 current_invis_chars = W_OFFSET (current_line, wrap_offset);
1237 if (_rl_last_v_pos != current_line)
1239 _rl_move_vert (current_line);
1240 if (current_line == 0 && visible_wrap_offset)
1241 _rl_last_c_pos += visible_wrap_offset;
1244 /* If this is the first line and there are invisible characters in the
1245 prompt string, and the prompt string has not changed, and the current
1246 cursor position is before the last invisible character in the prompt,
1247 and the index of the character to move to is past the end of the prompt
1248 string, then redraw the entire prompt string. We can only do this
1249 reliably if the terminal supports a `cr' capability.
1251 This is not an efficiency hack -- there is a problem with redrawing
1252 portions of the prompt string if they contain terminal escape
1253 sequences (like drawing the `unbold' sequence without a corresponding
1254 `bold') that manifests itself on certain terminals. */
1256 lendiff = local_prompt ? strlen (local_prompt) : 0;
1257 od = ofd - old; /* index of first difference in visible line */
1258 if (current_line == 0 && !_rl_horizontal_scroll_mode &&
1259 _rl_term_cr && lendiff > prompt_visible_length && _rl_last_c_pos > 0 &&
1260 od >= lendiff && _rl_last_c_pos <= prompt_last_invisible)
1262 #if defined (__MSDOS__)
1263 putc ('\r', rl_outstream);
1265 tputs (_rl_term_cr, 1, _rl_output_character_function);
1267 _rl_output_some_chars (local_prompt, lendiff);
1268 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1269 _rl_last_c_pos = _rl_col_width (local_prompt, 0, lendiff);
1271 _rl_last_c_pos = lendiff;
1274 _rl_move_cursor_relative (od, old);
1276 /* if (len (new) > len (old))
1277 lendiff == difference in buffer
1278 col_lendiff == difference on screen
1279 When not using multibyte characters, these are equal */
1280 lendiff = (nls - nfd) - (ols - ofd);
1281 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1282 col_lendiff = _rl_col_width (new, nfd - new, nls - new) - _rl_col_width (old, ofd - old, ols - old);
1284 col_lendiff = lendiff;
1286 /* If we are changing the number of invisible characters in a line, and
1287 the spot of first difference is before the end of the invisible chars,
1288 lendiff needs to be adjusted. */
1289 if (current_line == 0 && !_rl_horizontal_scroll_mode &&
1290 current_invis_chars != visible_wrap_offset)
1292 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1294 lendiff += visible_wrap_offset - current_invis_chars;
1295 col_lendiff += visible_wrap_offset - current_invis_chars;
1299 lendiff += visible_wrap_offset - current_invis_chars;
1300 col_lendiff = lendiff;
1304 /* Insert (diff (len (old), len (new)) ch. */
1306 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1307 col_temp = _rl_col_width (new, nfd - new, ne - new);
1311 if (col_lendiff > 0) /* XXX - was lendiff */
1313 /* Non-zero if we're increasing the number of lines. */
1314 int gl = current_line >= _rl_vis_botlin && inv_botlin > _rl_vis_botlin;
1315 /* Sometimes it is cheaper to print the characters rather than
1316 use the terminal's capabilities. If we're growing the number
1317 of lines, make sure we actually cause the new line to wrap
1318 around on auto-wrapping terminals. */
1319 if (_rl_terminal_can_insert && ((2 * col_temp) >= col_lendiff || _rl_term_IC) && (!_rl_term_autowrap || !gl))
1321 /* If lendiff > prompt_visible_length and _rl_last_c_pos == 0 and
1322 _rl_horizontal_scroll_mode == 1, inserting the characters with
1323 _rl_term_IC or _rl_term_ic will screw up the screen because of the
1324 invisible characters. We need to just draw them. */
1325 if (*ols && (!_rl_horizontal_scroll_mode || _rl_last_c_pos > 0 ||
1326 lendiff <= prompt_visible_length || !current_invis_chars))
1328 insert_some_chars (nfd, lendiff, col_lendiff);
1329 _rl_last_c_pos += col_lendiff;
1333 /* At the end of a line the characters do not have to
1334 be "inserted". They can just be placed on the screen. */
1335 /* However, this screws up the rest of this block, which
1336 assumes you've done the insert because you can. */
1337 _rl_output_some_chars (nfd, lendiff);
1338 _rl_last_c_pos += col_lendiff;
1342 /* We have horizontal scrolling and we are not inserting at
1343 the end. We have invisible characters in this line. This
1344 is a dumb update. */
1345 _rl_output_some_chars (nfd, temp);
1346 _rl_last_c_pos += col_temp;
1349 /* Copy (new) chars to screen from first diff to last match. */
1351 if ((temp - lendiff) > 0)
1353 _rl_output_some_chars (nfd + lendiff, temp - lendiff);
1355 _rl_last_c_pos += _rl_col_width (nfd+lendiff, 0, temp-col_lendiff);
1357 _rl_last_c_pos += _rl_col_width (nfd+lendiff, 0, temp-lendiff);
1363 /* cannot insert chars, write to EOL */
1364 _rl_output_some_chars (nfd, temp);
1365 _rl_last_c_pos += col_temp;
1368 else /* Delete characters from line. */
1370 /* If possible and inexpensive to use terminal deletion, then do so. */
1371 if (_rl_term_dc && (2 * col_temp) >= -col_lendiff)
1373 /* If all we're doing is erasing the invisible characters in the
1374 prompt string, don't bother. It screws up the assumptions
1375 about what's on the screen. */
1376 if (_rl_horizontal_scroll_mode && _rl_last_c_pos == 0 &&
1377 -lendiff == visible_wrap_offset)
1381 delete_chars (-col_lendiff); /* delete (diff) characters */
1383 /* Copy (new) chars to screen from first diff to last match */
1387 _rl_output_some_chars (nfd, temp);
1388 _rl_last_c_pos += _rl_col_width (nfd, 0, temp);;
1391 /* Otherwise, print over the existing material. */
1396 _rl_output_some_chars (nfd, temp);
1397 _rl_last_c_pos += col_temp;
1399 lendiff = (oe - old) - (ne - new);
1400 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1401 col_lendiff = _rl_col_width (old, 0, oe - old) - _rl_col_width (new, 0, ne - new);
1403 col_lendiff = lendiff;
1407 if (_rl_term_autowrap && current_line < inv_botlin)
1408 space_to_eol (col_lendiff);
1410 _rl_clear_to_eol (col_lendiff);
1416 /* Tell the update routines that we have moved onto a new (empty) line. */
1421 visible_line[0] = '\0';
1423 _rl_last_c_pos = _rl_last_v_pos = 0;
1424 _rl_vis_botlin = last_lmargin = 0;
1426 vis_lbreaks[0] = vis_lbreaks[1] = 0;
1427 visible_wrap_offset = 0;
1431 /* Tell the update routines that we have moved onto a new line with the
1432 prompt already displayed. Code originally from the version of readline
1433 distributed with CLISP. */
1435 rl_on_new_line_with_prompt ()
1437 int prompt_size, i, l, real_screenwidth, newlines;
1438 char *prompt_last_line;
1440 /* Initialize visible_line and invisible_line to ensure that they can hold
1441 the already-displayed prompt. */
1442 prompt_size = strlen (rl_prompt) + 1;
1443 init_line_structures (prompt_size);
1445 /* Make sure the line structures hold the already-displayed prompt for
1447 strcpy (visible_line, rl_prompt);
1448 strcpy (invisible_line, rl_prompt);
1450 /* If the prompt contains newlines, take the last tail. */
1451 prompt_last_line = strrchr (rl_prompt, '\n');
1452 if (!prompt_last_line)
1453 prompt_last_line = rl_prompt;
1455 l = strlen (prompt_last_line);
1456 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1457 _rl_last_c_pos = _rl_col_width (prompt_last_line, 0, l);
1461 /* Dissect prompt_last_line into screen lines. Note that here we have
1462 to use the real screenwidth. Readline's notion of screenwidth might be
1463 one less, see terminal.c. */
1464 real_screenwidth = _rl_screenwidth + (_rl_term_autowrap ? 0 : 1);
1465 _rl_last_v_pos = l / real_screenwidth;
1466 /* If the prompt length is a multiple of real_screenwidth, we don't know
1467 whether the cursor is at the end of the last line, or already at the
1468 beginning of the next line. Output a newline just to be safe. */
1469 if (l > 0 && (l % real_screenwidth) == 0)
1470 _rl_output_some_chars ("\n", 1);
1473 newlines = 0; i = 0;
1476 _rl_vis_botlin = newlines;
1477 vis_lbreaks[newlines++] = i;
1478 i += real_screenwidth;
1480 vis_lbreaks[newlines] = l;
1481 visible_wrap_offset = 0;
1486 /* Actually update the display, period. */
1488 rl_forced_update_display ()
1492 register char *temp = visible_line;
1499 (*rl_redisplay_function) ();
1503 /* Move the cursor from _rl_last_c_pos to NEW, which are buffer indices.
1504 DATA is the contents of the screen line of interest; i.e., where
1505 the movement is being done. */
1507 _rl_move_cursor_relative (new, data)
1513 /* If we don't have to do anything, then return. */
1514 #if defined (HANDLE_MULTIBYTE)
1515 /* If we have multibyte characters, NEW is indexed by the buffer point in
1516 a multibyte string, but _rl_last_c_pos is the display position. In
1517 this case, NEW's display position is not obvious and must be
1519 if (MB_CUR_MAX == 1 || rl_byte_oriented)
1521 if (_rl_last_c_pos == new)
1524 else if (_rl_last_c_pos == _rl_col_width (data, 0, new))
1527 if (_rl_last_c_pos == new) return;
1530 /* It may be faster to output a CR, and then move forwards instead
1531 of moving backwards. */
1532 /* i == current physical cursor position. */
1533 i = _rl_last_c_pos - W_OFFSET(_rl_last_v_pos, visible_wrap_offset);
1534 if (new == 0 || CR_FASTER (new, _rl_last_c_pos) ||
1535 (_rl_term_autowrap && i == _rl_screenwidth))
1537 #if defined (__MSDOS__)
1538 putc ('\r', rl_outstream);
1540 tputs (_rl_term_cr, 1, _rl_output_character_function);
1541 #endif /* !__MSDOS__ */
1545 if (_rl_last_c_pos < new)
1547 /* Move the cursor forward. We do it by printing the command
1548 to move the cursor forward if there is one, else print that
1549 portion of the output buffer again. Which is cheaper? */
1551 /* The above comment is left here for posterity. It is faster
1552 to print one character (non-control) than to print a control
1553 sequence telling the terminal to move forward one character.
1554 That kind of control is for people who don't know what the
1555 data is underneath the cursor. */
1556 #if defined (HACK_TERMCAP_MOTION)
1557 if (_rl_term_forward_char)
1559 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1562 width = _rl_col_width (data, _rl_last_c_pos, new);
1563 for (i = 0; i < width; i++)
1564 tputs (_rl_term_forward_char, 1, _rl_output_character_function);
1568 for (i = _rl_last_c_pos; i < new; i++)
1569 tputs (_rl_term_forward_char, 1, _rl_output_character_function);
1572 else if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1574 tputs (_rl_term_cr, 1, _rl_output_character_function);
1575 for (i = 0; i < new; i++)
1576 putc (data[i], rl_outstream);
1579 for (i = _rl_last_c_pos; i < new; i++)
1580 putc (data[i], rl_outstream);
1582 #else /* !HACK_TERMCAP_MOTION */
1584 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1586 tputs (_rl_term_cr, 1, _rl_output_character_function);
1587 for (i = 0; i < new; i++)
1588 putc (data[i], rl_outstream);
1591 for (i = _rl_last_c_pos; i < new; i++)
1592 putc (data[i], rl_outstream);
1594 #endif /* !HACK_TERMCAP_MOTION */
1597 #if defined (HANDLE_MULTIBYTE)
1598 /* NEW points to the buffer point, but _rl_last_c_pos is the display point.
1599 The byte length of the string is probably bigger than the column width
1600 of the string, which means that if NEW == _rl_last_c_pos, then NEW's
1601 display point is less than _rl_last_c_pos. */
1602 else if (_rl_last_c_pos >= new)
1604 else if (_rl_last_c_pos > new)
1607 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1608 _rl_backspace (_rl_last_c_pos - _rl_col_width (data, 0, new));
1610 _rl_backspace (_rl_last_c_pos - new);
1613 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1614 _rl_last_c_pos = _rl_col_width (data, 0, new);
1616 _rl_last_c_pos = new;
1619 /* PWP: move the cursor up or down. */
1624 register int delta, i;
1626 if (_rl_last_v_pos == to || to > _rl_screenheight)
1629 if ((delta = to - _rl_last_v_pos) > 0)
1631 for (i = 0; i < delta; i++)
1632 putc ('\n', rl_outstream);
1633 #if defined (__MSDOS__)
1634 putc ('\r', rl_outstream);
1636 tputs (_rl_term_cr, 1, _rl_output_character_function);
1645 fflush (rl_outstream); /* make sure the cursor pos is current! */
1646 ScreenGetCursor (&row, &col);
1647 ScreenSetCursor (row + delta, col);
1648 i = -delta; /* in case someone wants to use it after the loop */
1649 #else /* !__MSDOS__ */
1650 if (_rl_term_up && *_rl_term_up)
1651 for (i = 0; i < -delta; i++)
1652 tputs (_rl_term_up, 1, _rl_output_character_function);
1653 #endif /* !__MSDOS__ */
1656 _rl_last_v_pos = to; /* Now TO is here */
1659 /* Physically print C on rl_outstream. This is for functions which know
1660 how to optimize the display. Return the number of characters output. */
1666 if (META_CHAR (c) && (_rl_output_meta_chars == 0))
1668 fprintf (rl_outstream, "M-");
1673 #if defined (DISPLAY_TABS)
1674 if ((CTRL_CHAR (c) && c != '\t') || c == RUBOUT)
1676 if (CTRL_CHAR (c) || c == RUBOUT)
1677 #endif /* !DISPLAY_TABS */
1679 fprintf (rl_outstream, "C-");
1681 c = CTRL_CHAR (c) ? UNCTRL (c) : '?';
1684 putc (c, rl_outstream);
1685 fflush (rl_outstream);
1690 rl_character_len (c, pos)
1691 register int c, pos;
1695 uc = (unsigned char)c;
1698 return ((_rl_output_meta_chars == 0) ? 4 : 1);
1702 #if defined (DISPLAY_TABS)
1703 return (((pos | 7) + 1) - pos);
1706 #endif /* !DISPLAY_TABS */
1709 if (CTRL_CHAR (c) || c == RUBOUT)
1712 return ((ISPRINT (uc)) ? 1 : 2);
1715 /* How to print things in the "echo-area". The prompt is treated as a
1718 #if defined (USE_VARARGS)
1720 #if defined (PREFER_STDARG)
1721 rl_message (const char *format, ...)
1723 rl_message (va_alist)
1728 #if defined (PREFER_VARARGS)
1732 #if defined (PREFER_STDARG)
1733 va_start (args, format);
1736 format = va_arg (args, char *);
1739 #if defined (HAVE_VSNPRINTF)
1740 vsnprintf (msg_buf, sizeof (msg_buf) - 1, format, args);
1742 vsprintf (msg_buf, format, args);
1743 msg_buf[sizeof(msg_buf) - 1] = '\0'; /* overflow? */
1747 rl_display_prompt = msg_buf;
1748 (*rl_redisplay_function) ();
1751 #else /* !USE_VARARGS */
1753 rl_message (format, arg1, arg2)
1756 sprintf (msg_buf, format, arg1, arg2);
1757 msg_buf[sizeof(msg_buf) - 1] = '\0'; /* overflow? */
1758 rl_display_prompt = msg_buf;
1759 (*rl_redisplay_function) ();
1762 #endif /* !USE_VARARGS */
1764 /* How to clear things from the "echo-area". */
1768 rl_display_prompt = rl_prompt;
1769 (*rl_redisplay_function) ();
1774 rl_reset_line_state ()
1778 rl_display_prompt = rl_prompt ? rl_prompt : "";
1783 static char *saved_local_prompt;
1784 static char *saved_local_prefix;
1785 static int saved_last_invisible;
1786 static int saved_visible_length;
1791 saved_local_prompt = local_prompt;
1792 saved_local_prefix = local_prompt_prefix;
1793 saved_last_invisible = prompt_last_invisible;
1794 saved_visible_length = prompt_visible_length;
1796 local_prompt = local_prompt_prefix = (char *)0;
1797 prompt_last_invisible = prompt_visible_length = 0;
1801 rl_restore_prompt ()
1803 FREE (local_prompt);
1804 FREE (local_prompt_prefix);
1806 local_prompt = saved_local_prompt;
1807 local_prompt_prefix = saved_local_prefix;
1808 prompt_last_invisible = saved_last_invisible;
1809 prompt_visible_length = saved_visible_length;
1813 _rl_make_prompt_for_search (pchar)
1821 if (saved_local_prompt == 0)
1823 len = (rl_prompt && *rl_prompt) ? strlen (rl_prompt) : 0;
1824 pmt = (char *)xmalloc (len + 2);
1826 strcpy (pmt, rl_prompt);
1832 len = *saved_local_prompt ? strlen (saved_local_prompt) : 0;
1833 pmt = (char *)xmalloc (len + 2);
1835 strcpy (pmt, saved_local_prompt);
1838 local_prompt = savestring (pmt);
1839 prompt_last_invisible = saved_last_invisible;
1840 prompt_visible_length = saved_visible_length + 1;
1845 /* Quick redisplay hack when erasing characters at the end of the line. */
1847 _rl_erase_at_end_of_line (l)
1853 for (i = 0; i < l; i++)
1854 putc (' ', rl_outstream);
1856 for (i = 0; i < l; i++)
1857 visible_line[--_rl_last_c_pos] = '\0';
1861 /* Clear to the end of the line. COUNT is the minimum
1862 number of character spaces to clear, */
1864 _rl_clear_to_eol (count)
1868 if (_rl_term_clreol)
1869 tputs (_rl_term_clreol, 1, _rl_output_character_function);
1873 space_to_eol (count);
1876 /* Clear to the end of the line using spaces. COUNT is the minimum
1877 number of character spaces to clear, */
1879 space_to_eol (count)
1884 for (i = 0; i < count; i++)
1885 putc (' ', rl_outstream);
1887 _rl_last_c_pos += count;
1893 #if defined (__GO32__)
1894 ScreenClear (); /* FIXME: only works in text modes */
1895 ScreenSetCursor (0, 0); /* term_clrpag is "cl" which homes the cursor */
1897 if (_rl_term_clrpag)
1898 tputs (_rl_term_clrpag, 1, _rl_output_character_function);
1904 /* Insert COUNT characters from STRING to the output stream at column COL. */
1906 insert_some_chars (string, count, col)
1910 #if defined(__MSDOS__) || defined(__MINGW32__)
1911 _rl_output_some_chars (string, count);
1912 #else /* !__MSDOS__ && !__MINGW32__ */
1914 if (MB_CUR_MAX == 1 || rl_byte_oriented)
1916 fprintf(stderr, "readline: debug: insert_some_chars: count (%d) != col (%d)\n", count, col);
1918 /* If IC is defined, then we do not have to "enter" insert mode. */
1923 buffer = tgoto (_rl_term_IC, 0, col);
1924 tputs (buffer, 1, _rl_output_character_function);
1925 _rl_output_some_chars (string, count);
1931 /* If we have to turn on insert-mode, then do so. */
1932 if (_rl_term_im && *_rl_term_im)
1933 tputs (_rl_term_im, 1, _rl_output_character_function);
1935 /* If there is a special command for inserting characters, then
1936 use that first to open up the space. */
1937 if (_rl_term_ic && *_rl_term_ic)
1939 for (i = col; i--; )
1940 tputs (_rl_term_ic, 1, _rl_output_character_function);
1943 /* Print the text. */
1944 _rl_output_some_chars (string, count);
1946 /* If there is a string to turn off insert mode, we had best use
1948 if (_rl_term_ei && *_rl_term_ei)
1949 tputs (_rl_term_ei, 1, _rl_output_character_function);
1951 #endif /* !__MSDOS__ */
1954 /* Delete COUNT characters from the display line. */
1956 delete_chars (count)
1959 if (count > _rl_screenwidth) /* XXX */
1962 #if !defined(__MSDOS__) && !defined(__MINGW32__)
1963 if (_rl_term_DC && *_rl_term_DC)
1966 buffer = tgoto (_rl_term_DC, count, count);
1967 tputs (buffer, count, _rl_output_character_function);
1971 if (_rl_term_dc && *_rl_term_dc)
1973 tputs (_rl_term_dc, 1, _rl_output_character_function);
1975 #endif /* !__MSDOS__ && !__MINGW32__ */
1984 /* If the cursor is the only thing on an otherwise-blank last line,
1985 compensate so we don't print an extra CRLF. */
1986 if (_rl_vis_botlin && _rl_last_c_pos == 0 &&
1987 visible_line[vis_lbreaks[_rl_vis_botlin]] == 0)
1992 _rl_move_vert (_rl_vis_botlin);
1993 /* If we've wrapped lines, remove the final xterm line-wrap flag. */
1994 if (full_lines && _rl_term_autowrap && (VIS_LLEN(_rl_vis_botlin) == _rl_screenwidth))
1998 last_line = &visible_line[vis_lbreaks[_rl_vis_botlin]];
1999 _rl_move_cursor_relative (_rl_screenwidth - 1, last_line);
2000 _rl_clear_to_eol (0);
2001 putc (last_line[_rl_screenwidth - 1], rl_outstream);
2005 fflush (rl_outstream);
2009 /* Move to the start of the current line. */
2015 #if defined (__MSDOS__)
2016 putc ('\r', rl_outstream);
2018 tputs (_rl_term_cr, 1, _rl_output_character_function);
2024 /* Redraw the last line of a multi-line prompt that may possibly contain
2025 terminal escape sequences. Called with the cursor at column 0 of the
2026 line to draw the prompt on. */
2031 char *oldp, *oldl, *oldlprefix;
2032 int oldlen, oldlast, oldplen, oldninvis;
2034 /* Geez, I should make this a struct. */
2035 oldp = rl_display_prompt;
2036 oldl = local_prompt;
2037 oldlprefix = local_prompt_prefix;
2038 oldlen = prompt_visible_length;
2039 oldplen = prompt_prefix_length;
2040 oldlast = prompt_last_invisible;
2041 oldninvis = prompt_invis_chars_first_line;
2043 rl_display_prompt = t;
2044 local_prompt = expand_prompt (t, &prompt_visible_length,
2045 &prompt_last_invisible,
2046 &prompt_invis_chars_first_line);
2047 local_prompt_prefix = (char *)NULL;
2048 rl_forced_update_display ();
2050 rl_display_prompt = oldp;
2051 local_prompt = oldl;
2052 local_prompt_prefix = oldlprefix;
2053 prompt_visible_length = oldlen;
2054 prompt_prefix_length = oldplen;
2055 prompt_last_invisible = oldlast;
2056 prompt_invis_chars_first_line = oldninvis;
2059 /* Redisplay the current line after a SIGWINCH is received. */
2061 _rl_redisplay_after_sigwinch ()
2065 /* Clear the current line and put the cursor at column 0. Make sure
2066 the right thing happens if we have wrapped to a new screen line. */
2069 #if defined (__MSDOS__)
2070 putc ('\r', rl_outstream);
2072 tputs (_rl_term_cr, 1, _rl_output_character_function);
2075 #if defined (__MSDOS__)
2076 space_to_eol (_rl_screenwidth);
2077 putc ('\r', rl_outstream);
2079 if (_rl_term_clreol)
2080 tputs (_rl_term_clreol, 1, _rl_output_character_function);
2083 space_to_eol (_rl_screenwidth);
2084 tputs (_rl_term_cr, 1, _rl_output_character_function);
2087 if (_rl_last_v_pos > 0)
2093 /* Redraw only the last line of a multi-line prompt. */
2094 t = strrchr (rl_display_prompt, '\n');
2096 redraw_prompt (++t);
2098 rl_forced_update_display ();
2102 _rl_clean_up_for_exit ()
2104 if (readline_echoing_p)
2106 _rl_move_vert (_rl_vis_botlin);
2108 fflush (rl_outstream);
2109 rl_restart_output (1, 0);
2114 _rl_erase_entire_line ()
2117 _rl_clear_to_eol (0);
2119 fflush (rl_outstream);
2122 /* return the `current display line' of the cursor -- the number of lines to
2123 move up to get to the first screen line of the current readline line. */
2125 _rl_current_display_line ()
2129 /* Find out whether or not there might be invisible characters in the
2131 if (rl_display_prompt == rl_prompt)
2132 nleft = _rl_last_c_pos - _rl_screenwidth - rl_visible_prompt_length;
2134 nleft = _rl_last_c_pos - _rl_screenwidth;
2137 ret = 1 + nleft / _rl_screenwidth;
2144 #if defined (HANDLE_MULTIBYTE)
2145 /* Calculate the number of screen columns occupied by STR from START to END.
2146 In the case of multibyte characters with stateful encoding, we have to
2147 scan from the beginning of the string to take the state into account. */
2149 _rl_col_width (str, start, end)
2155 int tmp, point, width, max;
2163 while (point < start)
2165 tmp = mbrlen (str + point, max, &ps);
2166 if ((size_t)tmp == (size_t)-1 || (size_t)tmp == (size_t)-2)
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. */
2174 /* Clear the state of the byte sequence, because in this case the
2175 effect of mbstate is undefined. */
2176 memset (&ps, 0, sizeof (mbstate_t));
2179 break; /* Found '\0' */
2187 /* If START is not a byte that starts a character, then POINT will be
2188 greater than START. In this case, assume that (POINT - START) gives
2189 a byte count that is the number of columns of difference. */
2190 width = point - start;
2194 tmp = mbrtowc (&wc, str + point, max, &ps);
2195 if ((size_t)tmp == (size_t)-1 || (size_t)tmp == (size_t)-2)
2197 /* In this case, the bytes are invalid or too short to compose a
2198 multibyte character, so we assume that the first byte represents
2199 a single character. */
2203 /* and assume that the byte occupies a single column. */
2206 /* Clear the state of the byte sequence, because in this case the
2207 effect of mbstate is undefined. */
2208 memset (&ps, 0, sizeof (mbstate_t));
2211 break; /* Found '\0' */
2217 width += (tmp >= 0) ? tmp : 1;
2221 width += point - end;
2225 #endif /* HANDLE_MULTIBYTE */