2 /* display.c -- readline redisplay facility. */
4 /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
6 This file is part of the GNU Readline Library, a library for
7 reading lines of text with interactive input and history editing.
9 The GNU Readline Library is free software; you can redistribute it
10 and/or modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2, or
12 (at your option) any later version.
14 The GNU Readline Library is distributed in the hope that it will be
15 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
16 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 The GNU General Public License is often shipped with GNU software, and
20 is generally kept in a file called COPYING or LICENSE. If you do not
21 have a copy of the license, write to the Free Software Foundation,
22 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
23 #define READLINE_LIBRARY
25 #if defined (HAVE_CONFIG_H)
29 #include <sys/types.h>
31 #if defined (HAVE_UNISTD_H)
33 #endif /* HAVE_UNISTD_H */
35 #include "posixstat.h"
37 #if defined (HAVE_STDLIB_H)
40 # include "ansi_stdlib.h"
41 #endif /* HAVE_STDLIB_H */
45 /* System-specific feature definitions and include files. */
49 /* Termcap library stuff. */
52 /* Some standard library routines. */
56 #include "rlprivate.h"
59 #if !defined (strchr) && !defined (__STDC__)
60 extern char *strchr (), *strrchr ();
61 #endif /* !strchr && !__STDC__ */
63 #if defined (HACK_TERMCAP_MOTION)
64 extern char *_rl_term_forward_char;
67 static void update_line PARAMS((char *, char *, int, int, int, int));
68 static void space_to_eol PARAMS((int));
69 static void delete_chars PARAMS((int));
70 static void insert_some_chars PARAMS((char *, int, int));
71 static void cr PARAMS((void));
73 #if defined (HANDLE_MULTIBYTE)
74 static int _rl_col_width PARAMS((char *, int, int));
75 static int *_rl_wrapped_line;
77 # define _rl_col_width(l, s, e) (((e) <= (s)) ? 0 : (e) - (s))
80 static int *inv_lbreaks, *vis_lbreaks;
81 static int inv_lbsize, vis_lbsize;
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)))
87 /* **************************************************************** */
91 /* **************************************************************** */
93 /* This is the stuff that is hard for me. I never seem to write good
94 display routines in C. Let's see how I do this time. */
96 /* (PWP) Well... Good for a simple line updater, but totally ignores
97 the problems of input lines longer than the screen width.
99 update_line and the code that calls it makes a multiple line,
100 automatically wrapping line update. Careful attention needs
101 to be paid to the vertical position variables. */
103 /* Keep two buffers; one which reflects the current contents of the
104 screen, and the other to draw what we think the new contents should
105 be. Then compare the buffers, and make whatever changes to the
106 screen itself that we should. Finally, make the buffer that we
107 just drew into be the one which reflects the current contents of the
108 screen, and place the cursor where it belongs.
110 Commands that want to can fix the display themselves, and then let
111 this function know that the display has been fixed by setting the
112 RL_DISPLAY_FIXED variable. This is good for efficiency. */
114 /* Application-specific redisplay function. */
115 rl_voidfunc_t *rl_redisplay_function = rl_redisplay;
117 /* Global variables declared here. */
118 /* What YOU turn on when you have handled all redisplay yourself. */
119 int rl_display_fixed = 0;
121 int _rl_suppress_redisplay = 0;
123 /* The stuff that gets printed out before the actual text of the line.
124 This is usually pointing to rl_prompt. */
125 char *rl_display_prompt = (char *)NULL;
127 /* Pseudo-global variables declared here. */
128 /* The visible cursor position. If you print some text, adjust this. */
129 int _rl_last_c_pos = 0;
130 int _rl_last_v_pos = 0;
132 /* Number of lines currently on screen minus 1. */
133 int _rl_vis_botlin = 0;
135 /* Variables used only in this file. */
136 /* The last left edge of text that was displayed. This is used when
137 doing horizontal scrolling. It shifts in thirds of a screenwidth. */
138 static int last_lmargin;
140 /* The line display buffers. One is the line currently displayed on
141 the screen. The other is the line about to be displayed. */
142 static char *visible_line = (char *)NULL;
143 static char *invisible_line = (char *)NULL;
145 /* A buffer for `modeline' messages. */
146 static char msg_buf[128];
148 /* Non-zero forces the redisplay even if we thought it was unnecessary. */
149 static int forced_display;
151 /* Default and initial buffer size. Can grow. */
152 static int line_size = 1024;
154 /* Variables to keep track of the expanded prompt string, which may
155 include invisible characters. */
157 static char *local_prompt, *local_prompt_prefix;
158 static int prompt_visible_length, prompt_prefix_length;
160 /* The number of invisible characters in the line currently being
161 displayed on the screen. */
162 static int visible_wrap_offset;
164 /* The number of invisible characters in the prompt string. Static so it
165 can be shared between rl_redisplay and update_line */
166 static int wrap_offset;
168 /* The index of the last invisible character in the prompt string. */
169 static int prompt_last_invisible;
171 /* The length (buffer offset) of the first line of the last (possibly
172 multi-line) buffer displayed on the screen. */
173 static int visible_first_line_len;
175 /* Number of invisible characters on the first physical line of the prompt.
176 Only valid when the number of physical characters in the prompt exceeds
177 (or is equal to) _rl_screenwidth. */
178 static int prompt_invis_chars_first_line;
180 static int prompt_last_screen_line;
182 /* Expand the prompt string S and return the number of visible
183 characters in *LP, if LP is not null. This is currently more-or-less
184 a placeholder for expansion. LIP, if non-null is a place to store the
185 index of the last invisible character in the returned string. NIFLP,
186 if non-zero, is a place to store the number of invisible characters in
187 the first prompt line. */
189 /* Current implementation:
190 \001 (^A) start non-visible characters
191 \002 (^B) end non-visible characters
192 all characters except \001 and \002 (following a \001) are copied to
193 the returned string; all characters except those between \001 and
194 \002 are assumed to be `visible'. */
197 expand_prompt (pmt, lp, lip, niflp)
199 int *lp, *lip, *niflp;
202 int l, rl, last, ignoring, ninvis, invfl;
204 /* Short-circuit if we can. */
205 if (strchr (pmt, RL_PROMPT_START_IGNORE) == 0)
207 r = savestring (pmt);
214 r = ret = (char *)xmalloc (l + 1);
216 invfl = 0; /* invisible chars in first line of prompt */
218 for (rl = ignoring = last = ninvis = 0, p = pmt; p && *p; p++)
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)
227 else if (ignoring && *p == RL_PROMPT_END_IGNORE)
240 if (rl == _rl_screenwidth)
245 if (rl < _rl_screenwidth)
258 /* Just strip out RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE from
259 PMT and return the rest of PMT. */
261 _rl_strip_prompt (pmt)
266 ret = expand_prompt (pmt, (int *)NULL, (int *)NULL, (int *)NULL);
271 * Expand the prompt string into the various display components, if
274 * local_prompt = expanded last line of string in rl_display_prompt
275 * (portion after the final newline)
276 * local_prompt_prefix = portion before last newline of rl_display_prompt,
277 * expanded via expand_prompt
278 * prompt_visible_length = number of visible characters in local_prompt
279 * prompt_prefix_length = number of visible characters in local_prompt_prefix
281 * This function is called once per call to readline(). It may also be
282 * called arbitrarily to expand the primary prompt.
284 * The return value is the number of visible characters on the last line
285 * of the (possibly multi-line) prompt.
288 rl_expand_prompt (prompt)
294 /* Clear out any saved values. */
296 FREE (local_prompt_prefix);
298 local_prompt = local_prompt_prefix = (char *)0;
299 prompt_last_invisible = prompt_visible_length = 0;
301 if (prompt == 0 || *prompt == 0)
304 p = strrchr (prompt, '\n');
307 /* The prompt is only one logical line, though it might wrap. */
308 local_prompt = expand_prompt (prompt, &prompt_visible_length,
309 &prompt_last_invisible,
310 &prompt_invis_chars_first_line);
311 local_prompt_prefix = (char *)0;
312 return (prompt_visible_length);
316 /* The prompt spans multiple lines. */
318 local_prompt = expand_prompt (p, &prompt_visible_length,
319 &prompt_last_invisible,
320 &prompt_invis_chars_first_line);
322 /* The portion of the prompt string up to and including the
323 final newline is now null-terminated. */
324 local_prompt_prefix = expand_prompt (prompt, &prompt_prefix_length,
326 &prompt_invis_chars_first_line);
328 return (prompt_prefix_length);
332 /* Initialize the VISIBLE_LINE and INVISIBLE_LINE arrays, and their associated
333 arrays of line break markers. MINSIZE is the minimum size of VISIBLE_LINE
334 and INVISIBLE_LINE; if it is greater than LINE_SIZE, LINE_SIZE is
335 increased. If the lines have already been allocated, this ensures that
336 they can hold at least MINSIZE characters. */
338 init_line_structures (minsize)
343 if (invisible_line == 0) /* initialize it */
345 if (line_size < minsize)
347 visible_line = (char *)xmalloc (line_size);
348 invisible_line = (char *)xmalloc (line_size);
350 else if (line_size < minsize) /* ensure it can hold MINSIZE chars */
353 if (line_size < minsize)
355 visible_line = (char *)xrealloc (visible_line, line_size);
356 invisible_line = (char *)xrealloc (invisible_line, line_size);
359 for (n = minsize; n < line_size; n++)
362 invisible_line[n] = 1;
365 if (vis_lbreaks == 0)
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));
374 inv_lbreaks[0] = vis_lbreaks[0] = 0;
378 /* Basic redisplay algorithm. */
382 register int in, out, c, linenum, cursor_linenum;
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)
392 int _rl_wrapped_multicolumn = 0;
395 if (!readline_echoing_p)
398 if (!rl_display_prompt)
399 rl_display_prompt = "";
401 if (invisible_line == 0)
403 init_line_structures (0);
407 /* Draw the line into the buffer. */
410 line = invisible_line;
411 out = inv_botlin = 0;
413 /* Mark the line as modified or not. We only do this for history
415 if (_rl_mark_modified_lines && current_history () && rl_undo_list)
421 /* If someone thought that the redisplay was handled, but the currently
422 visible line has a different modification state than the one about
423 to become visible, then correct the caller's misconception. */
424 if (visible_line[0] != invisible_line[0])
425 rl_display_fixed = 0;
427 /* If the prompt to be displayed is the `primary' readline prompt (the
428 one passed to readline()), use the values we have already expanded.
429 If not, use what's already in rl_display_prompt. WRAP_OFFSET is the
430 number of non-visible characters in the prompt string. */
431 if (rl_display_prompt == rl_prompt || local_prompt)
433 int local_len = local_prompt ? strlen (local_prompt) : 0;
434 if (local_prompt_prefix && forced_display)
435 _rl_output_some_chars (local_prompt_prefix, strlen (local_prompt_prefix));
439 temp = local_len + out + 2;
440 if (temp >= line_size)
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);
446 strncpy (line + out, local_prompt, local_len);
450 wrap_offset = local_len - prompt_visible_length;
455 prompt_this_line = strrchr (rl_display_prompt, '\n');
456 if (!prompt_this_line)
457 prompt_this_line = rl_display_prompt;
461 pmtlen = prompt_this_line - rl_display_prompt; /* temp var */
464 _rl_output_some_chars (rl_display_prompt, pmtlen);
465 /* Make sure we are at column zero even after a newline,
466 regardless of the state of terminal output processing. */
467 if (pmtlen < 2 || prompt_this_line[-2] != '\r')
472 pmtlen = strlen (prompt_this_line);
473 temp = pmtlen + out + 2;
474 if (temp >= line_size)
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);
480 strncpy (line + out, prompt_this_line, pmtlen);
483 wrap_offset = prompt_invis_chars_first_line = 0;
486 #define CHECK_INV_LBREAKS() \
488 if (newlines >= (inv_lbsize - 2)) \
491 inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
495 #if defined (HANDLE_MULTIBYTE)
496 #define CHECK_LPOS() \
499 if (lpos >= _rl_screenwidth) \
501 if (newlines >= (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)); \
507 inv_lbreaks[++newlines] = out; \
508 _rl_wrapped_line[newlines] = _rl_wrapped_multicolumn; \
513 #define CHECK_LPOS() \
516 if (lpos >= _rl_screenwidth) \
518 if (newlines >= (inv_lbsize - 2)) \
521 inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
523 inv_lbreaks[++newlines] = out; \
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);
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. */
541 /* what if lpos is already >= _rl_screenwidth before we start drawing the
542 contents of the command line? */
543 while (lpos >= _rl_screenwidth)
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) +
553 ((newlines == 0) ? prompt_invis_chars_first_line : 0) +
555 ((newlines == 0 && local_prompt_prefix == 0) ? prompt_invis_chars_first_line : 0) +
557 ((newlines == 1) ? wrap_offset : 0);
559 inv_lbreaks[++newlines] = temp;
560 lpos -= _rl_screenwidth;
563 prompt_last_screen_line = newlines;
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. */
571 #if defined (HANDLE_MULTIBYTE)
573 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
575 memset (&ps, 0, sizeof (mbstate_t));
576 wc_bytes = mbrtowc (&wc, rl_line_buffer, rl_end, &ps);
582 for (in = 0; in < rl_end; in++)
585 c = (unsigned char)rl_line_buffer[in];
587 #if defined (HANDLE_MULTIBYTE)
588 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
590 if (wc_bytes == (size_t)-1 || wc_bytes == (size_t)-2)
592 /* Byte sequence is invalid or shortened. Assume that the
593 first byte represents a character. */
595 /* Assume that a character occupies a single column. */
597 memset (&ps, 0, sizeof (mbstate_t));
599 else if (wc_bytes == (size_t)0)
600 break; /* Found '\0' */
604 wc_width = (temp < 0) ? 1 : temp;
609 if (out + 8 >= line_size) /* XXX - 8 for \t */
612 visible_line = (char *)xrealloc (visible_line, line_size);
613 invisible_line = (char *)xrealloc (invisible_line, line_size);
614 line = invisible_line;
620 lb_linenum = newlines;
623 #if defined (HANDLE_MULTIBYTE)
624 if (META_CHAR (c) && _rl_output_meta_chars == 0) /* XXX - clean up */
629 if (_rl_output_meta_chars == 0)
631 sprintf (line + out, "\\%o", c);
633 if (lpos + 4 >= _rl_screenwidth)
635 temp = _rl_screenwidth - lpos;
636 CHECK_INV_LBREAKS ();
637 inv_lbreaks[++newlines] = out + temp;
651 #if defined (DISPLAY_TABS)
657 newout = (out | (int)7) + 1;
659 newout = out + 8 - lpos % 8;
662 if (lpos + temp >= _rl_screenwidth)
665 temp2 = _rl_screenwidth - lpos;
666 CHECK_INV_LBREAKS ();
667 inv_lbreaks[++newlines] = out + temp2;
680 else if (c == '\n' && _rl_horizontal_scroll_mode == 0 && _rl_term_up && *_rl_term_up)
682 line[out++] = '\0'; /* XXX - sentinel */
683 CHECK_INV_LBREAKS ();
684 inv_lbreaks[++newlines] = out;
687 else if (CTRL_CHAR (c) || c == RUBOUT)
691 line[out++] = CTRL_CHAR (c) ? UNCTRL (c) : '?';
696 #if defined (HANDLE_MULTIBYTE)
697 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
701 _rl_wrapped_multicolumn = 0;
703 if (_rl_screenwidth < lpos + wc_width)
704 for (i = lpos; i < _rl_screenwidth; i++)
706 /* The space will be removed in update_line() */
708 _rl_wrapped_multicolumn++;
714 lb_linenum = newlines;
716 for (i = in; i < in+wc_bytes; i++)
717 line[out++] = rl_line_buffer[i];
718 for (i = 0; i < wc_width; i++)
732 #if defined (HANDLE_MULTIBYTE)
733 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
736 wc_bytes = mbrtowc (&wc, rl_line_buffer + in, rl_end - in, &ps);
747 lb_linenum = newlines;
750 inv_botlin = lb_botlin = newlines;
751 CHECK_INV_LBREAKS ();
752 inv_lbreaks[newlines+1] = out;
753 cursor_linenum = lb_linenum;
755 /* C_POS == position in buffer where cursor should be placed.
756 CURSOR_LINENUM == line number where the cursor should be placed. */
758 /* PWP: now is when things get a bit hairy. The visible and invisible
759 line buffers are really multiple lines, which would wrap every
760 (screenwidth - 1) characters. Go through each in turn, finding
761 the changed region and updating it. The line order is top to bottom. */
763 /* If we can move the cursor up and down, then use multiple lines,
764 otherwise, let long lines display in a single terminal line, and
765 horizontally scroll it. */
767 if (_rl_horizontal_scroll_mode == 0 && _rl_term_up && *_rl_term_up)
769 int nleft, pos, changed_screen_line;
771 if (!rl_display_fixed || forced_display)
775 /* If we have more than a screenful of material to display, then
776 only display a screenful. We should display the last screen,
778 if (out >= _rl_screenchars)
780 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
781 out = _rl_find_prev_mbchar (line, _rl_screenchars, MB_FIND_ANY);
783 out = _rl_screenchars - 1;
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). */
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])
793 #define VIS_CHARS(line) (visible_line + vis_lbreaks[line])
794 #define VIS_LINE(line) ((line) > _rl_vis_botlin) ? "" : VIS_CHARS(line)
795 #define INV_LINE(line) (invisible_line + inv_lbreaks[line])
797 /* For each line in the buffer, do the updating display. */
798 for (linenum = 0; linenum <= inv_botlin; linenum++)
800 update_line (VIS_LINE(linenum), INV_LINE(linenum), linenum,
801 VIS_LLEN(linenum), INV_LLEN(linenum), inv_botlin);
803 /* If this is the line with the prompt, we might need to
804 compensate for invisible characters in the new line. Do
805 this only if there is not more than one new line (which
806 implies that we completely overwrite the old visible line)
807 and the new line is shorter than the old. Make sure we are
808 at the end of the new line before clearing. */
810 inv_botlin == 0 && _rl_last_c_pos == out &&
811 (wrap_offset > visible_wrap_offset) &&
812 (_rl_last_c_pos < visible_first_line_len))
814 nleft = _rl_screenwidth + wrap_offset - _rl_last_c_pos;
816 _rl_clear_to_eol (nleft);
819 /* Since the new first line is now visible, save its length. */
821 visible_first_line_len = (inv_botlin > 0) ? inv_lbreaks[1] : out - wrap_offset;
824 /* We may have deleted some lines. If so, clear the left over
825 blank ones at the bottom out. */
826 if (_rl_vis_botlin > inv_botlin)
829 for (; linenum <= _rl_vis_botlin; linenum++)
831 tt = VIS_CHARS (linenum);
832 _rl_move_vert (linenum);
833 _rl_move_cursor_relative (0, tt);
835 ((linenum == _rl_vis_botlin) ? strlen (tt) : _rl_screenwidth);
838 _rl_vis_botlin = inv_botlin;
840 /* CHANGED_SCREEN_LINE is set to 1 if we have moved to a
841 different screen line during this redisplay. */
842 changed_screen_line = _rl_last_v_pos != cursor_linenum;
843 if (changed_screen_line)
845 _rl_move_vert (cursor_linenum);
846 /* If we moved up to the line with the prompt using _rl_term_up,
847 the physical cursor position on the screen stays the same,
848 but the buffer position needs to be adjusted to account
849 for invisible characters. */
850 if (cursor_linenum == 0 && wrap_offset)
851 _rl_last_c_pos += wrap_offset;
854 /* We have to reprint the prompt if it contains invisible
855 characters, since it's not generally OK to just reprint
856 the characters from the current cursor position. But we
857 only need to reprint it if the cursor is before the last
858 invisible character in the prompt string. */
859 nleft = prompt_visible_length + wrap_offset;
860 if (cursor_linenum == 0 && wrap_offset > 0 && _rl_last_c_pos > 0 &&
861 _rl_last_c_pos <= prompt_last_invisible && local_prompt)
863 #if defined (__MSDOS__)
864 putc ('\r', rl_outstream);
867 tputs (_rl_term_cr, 1, _rl_output_character_function);
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);
873 _rl_last_c_pos = nleft;
876 /* Where on that line? And where does that line start
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. */
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)
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);
892 _rl_last_c_pos = nleft;
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]);
901 else /* Do horizontal scrolling. */
903 #define M_OFFSET(margin, offset) ((margin) == 0 ? offset : 0)
904 int lmargin, ndisp, nleft, phys_c_pos, t;
906 /* Always at top line. */
909 /* Compute where in the buffer the displayed line should start. This
912 /* The number of characters that will be displayed before the cursor. */
913 ndisp = c_pos - wrap_offset;
914 nleft = prompt_visible_length + wrap_offset;
915 /* Where the new cursor position will be on the screen. This can be
916 longer than SCREENWIDTH; if it is, lmargin will be adjusted. */
917 phys_c_pos = c_pos - (last_lmargin ? last_lmargin : wrap_offset);
918 t = _rl_screenwidth / 3;
920 /* If the number of characters had already exceeded the screenwidth,
921 last_lmargin will be > 0. */
923 /* If the number of characters to be displayed is more than the screen
924 width, compute the starting offset so that the cursor is about
925 two-thirds of the way across the screen. */
926 if (phys_c_pos > _rl_screenwidth - 2)
928 lmargin = c_pos - (2 * t);
931 /* If the left margin would be in the middle of a prompt with
932 invisible characters, don't display the prompt at all. */
933 if (wrap_offset && lmargin > 0 && lmargin < nleft)
936 else if (ndisp < _rl_screenwidth - 2) /* XXX - was -1 */
938 else if (phys_c_pos < 1)
940 /* If we are moving back towards the beginning of the line and
941 the last margin is no longer correct, compute a new one. */
942 lmargin = ((c_pos - 1) / t) * t; /* XXX */
943 if (wrap_offset && lmargin > 0 && lmargin < nleft)
947 lmargin = last_lmargin;
949 /* If the first character on the screen isn't the first character
950 in the display line, indicate this with a special character. */
954 /* If SCREENWIDTH characters starting at LMARGIN do not encompass
955 the whole line, indicate that with a special character at the
956 right edge of the screen. If LMARGIN is 0, we need to take the
957 wrap offset into account. */
958 t = lmargin + M_OFFSET (lmargin, wrap_offset) + _rl_screenwidth;
962 if (!rl_display_fixed || forced_display || lmargin != last_lmargin)
965 update_line (&visible_line[last_lmargin],
966 &invisible_line[lmargin],
968 _rl_screenwidth + visible_wrap_offset,
969 _rl_screenwidth + (lmargin ? 0 : wrap_offset),
972 /* If the visible new line is shorter than the old, but the number
973 of invisible characters is greater, and we are at the end of
974 the new line, we need to clear to eol. */
975 t = _rl_last_c_pos - M_OFFSET (lmargin, wrap_offset);
976 if ((M_OFFSET (lmargin, wrap_offset) > visible_wrap_offset) &&
977 (_rl_last_c_pos == out) &&
978 t < visible_first_line_len)
980 nleft = _rl_screenwidth - t;
981 _rl_clear_to_eol (nleft);
983 visible_first_line_len = out - lmargin - M_OFFSET (lmargin, wrap_offset);
984 if (visible_first_line_len > _rl_screenwidth)
985 visible_first_line_len = _rl_screenwidth;
987 _rl_move_cursor_relative (c_pos - lmargin, &invisible_line[lmargin]);
988 last_lmargin = lmargin;
991 fflush (rl_outstream);
993 /* Swap visible and non-visible lines. */
995 char *vtemp = visible_line;
996 int *itemp = vis_lbreaks, ntemp = vis_lbsize;
998 visible_line = invisible_line;
999 invisible_line = vtemp;
1001 vis_lbreaks = inv_lbreaks;
1002 inv_lbreaks = itemp;
1004 vis_lbsize = inv_lbsize;
1007 rl_display_fixed = 0;
1008 /* If we are displaying on a single line, and last_lmargin is > 0, we
1009 are not displaying any invisible characters, so set visible_wrap_offset
1011 if (_rl_horizontal_scroll_mode && last_lmargin)
1012 visible_wrap_offset = 0;
1014 visible_wrap_offset = wrap_offset;
1018 /* PWP: update_line() is based on finding the middle difference of each
1019 line on the screen; vis:
1021 /old first difference
1022 /beginning of line | /old last same /old EOL
1024 old: eddie> Oh, my little gruntle-buggy is to me, as lurgid as
1025 new: eddie> Oh, my little buggy says to me, as lurgid as
1027 \beginning of line | \new last same \new end of line
1028 \new first difference
1030 All are character pointers for the sake of speed. Special cases for
1031 no differences, as well as for end of line additions must be handled.
1033 Could be made even smarter, but this works well enough */
1035 update_line (old, new, current_line, omax, nmax, inv_botlin)
1036 register char *old, *new;
1037 int current_line, omax, nmax, inv_botlin;
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;
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)
1057 #if defined (HANDLE_MULTIBYTE)
1058 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1062 int tempwidth, bytes;
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]);
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)
1081 tempwidth = wcwidth (wc);
1087 for (count = 0; count < bytes; count++)
1088 putc (new[count], rl_outstream);
1089 _rl_last_c_pos = tempwidth;
1091 memset (&ps, 0, sizeof (mbstate_t));
1092 ret = mbrtowc (&wc, old, MB_CUR_MAX, &ps);
1093 if (ret != 0 && bytes != 0)
1095 if (ret == (size_t)-1 || ret == (size_t)-2)
1096 memmove (old+bytes, old+1, strlen (old+1));
1098 memmove (old+bytes, old+ret, strlen (old+ret));
1099 memcpy (old, new, bytes);
1104 putc (' ', rl_outstream);
1107 if (old[0] && new[0])
1115 putc (new[0], rl_outstream);
1117 putc (' ', rl_outstream);
1118 _rl_last_c_pos = 1; /* XXX */
1120 if (old[0] && new[0])
1126 /* Find first difference. */
1127 #if defined (HANDLE_MULTIBYTE)
1128 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1130 memset (&ps_new, 0, sizeof(mbstate_t));
1131 memset (&ps_old, 0, sizeof(mbstate_t));
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); )
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;
1146 for (ofd = old, nfd = new;
1147 (ofd - old < omax) && *ofd && (*ofd == *nfd);
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++);
1157 /* If no difference, continue to next line. */
1158 if (ofd == oe && nfd == ne)
1161 wsatend = 1; /* flag for trailing whitespace */
1163 #if defined (HANDLE_MULTIBYTE)
1164 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
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))
1170 memset (&ps_old, 0, sizeof (mbstate_t));
1171 memset (&ps_new, 0, sizeof (mbstate_t));
1173 _rl_adjust_point (old, ols - old, &ps_old);
1174 _rl_adjust_point (new, nls - new, &ps_new);
1176 if (_rl_compare_chars (old, ols - old, &ps_old, new, nls - new, &ps_new) == 0)
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);
1188 #endif /* HANDLE_MULTIBYTE */
1189 ols = oe - 1; /* find last same */
1191 while ((ols > ofd) && (nls > nfd) && (*ols == *nls))
1198 #if defined (HANDLE_MULTIBYTE)
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)
1213 else if (*ols != *nls)
1216 if (*ols) /* don't step past the NUL */
1218 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1219 ols = old + _rl_find_next_mbchar (old, ols - old, 1, MB_FIND_ANY);
1225 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1226 nls = new + _rl_find_next_mbchar (new, nls - new, 1, MB_FIND_ANY);
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)
1236 _rl_move_vert (current_line);
1237 if (current_line == 0 && visible_wrap_offset)
1238 _rl_last_c_pos += visible_wrap_offset;
1241 /* If this is the first line and there are invisible characters in the
1242 prompt string, and the prompt string has not changed, and the current
1243 cursor position is before the last invisible character in the prompt,
1244 and the index of the character to move to is past the end of the prompt
1245 string, then redraw the entire prompt string. We can only do this
1246 reliably if the terminal supports a `cr' capability.
1248 This is not an efficiency hack -- there is a problem with redrawing
1249 portions of the prompt string if they contain terminal escape
1250 sequences (like drawing the `unbold' sequence without a corresponding
1251 `bold') that manifests itself on certain terminals. */
1253 lendiff = local_prompt ? strlen (local_prompt) : 0;
1254 od = ofd - old; /* index of first difference in visible line */
1255 if (current_line == 0 && !_rl_horizontal_scroll_mode &&
1256 _rl_term_cr && lendiff > prompt_visible_length && _rl_last_c_pos > 0 &&
1257 od >= lendiff && _rl_last_c_pos <= prompt_last_invisible)
1259 #if defined (__MSDOS__)
1260 putc ('\r', rl_outstream);
1262 tputs (_rl_term_cr, 1, _rl_output_character_function);
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);
1268 _rl_last_c_pos = lendiff;
1271 _rl_move_cursor_relative (od, old);
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);
1281 col_lendiff = lendiff;
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)
1289 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1291 lendiff += visible_wrap_offset - current_invis_chars;
1292 col_lendiff += visible_wrap_offset - current_invis_chars;
1296 lendiff += visible_wrap_offset - current_invis_chars;
1297 col_lendiff = lendiff;
1301 /* Insert (diff (len (old), len (new)) ch. */
1303 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1304 col_temp = _rl_col_width (new, nfd - new, ne - new);
1308 if (col_lendiff > 0) /* XXX - was lendiff */
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))
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))
1325 insert_some_chars (nfd, lendiff, col_lendiff);
1326 _rl_last_c_pos += col_lendiff;
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;
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;
1346 /* Copy (new) chars to screen from first diff to last match. */
1348 if ((temp - lendiff) > 0)
1350 _rl_output_some_chars (nfd + lendiff, temp - lendiff);
1352 _rl_last_c_pos += _rl_col_width (nfd+lendiff, 0, temp-lendiff) - col_lendiff;
1354 _rl_last_c_pos += _rl_col_width (nfd+lendiff, 0, temp-col_lendiff);
1360 /* cannot insert chars, write to EOL */
1361 _rl_output_some_chars (nfd, temp);
1362 _rl_last_c_pos += col_temp;
1365 else /* Delete characters from line. */
1367 /* If possible and inexpensive to use terminal deletion, then do so. */
1368 if (_rl_term_dc && (2 * col_temp) >= -col_lendiff)
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)
1378 delete_chars (-col_lendiff); /* delete (diff) characters */
1380 /* Copy (new) chars to screen from first diff to last match */
1384 _rl_output_some_chars (nfd, temp);
1385 _rl_last_c_pos += _rl_col_width (nfd, 0, temp);;
1388 /* Otherwise, print over the existing material. */
1393 _rl_output_some_chars (nfd, temp);
1394 _rl_last_c_pos += col_temp;
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);
1400 col_lendiff = lendiff;
1404 if (_rl_term_autowrap && current_line < inv_botlin)
1405 space_to_eol (col_lendiff);
1407 _rl_clear_to_eol (col_lendiff);
1413 /* Tell the update routines that we have moved onto a new (empty) line. */
1418 visible_line[0] = '\0';
1420 _rl_last_c_pos = _rl_last_v_pos = 0;
1421 _rl_vis_botlin = last_lmargin = 0;
1423 vis_lbreaks[0] = vis_lbreaks[1] = 0;
1424 visible_wrap_offset = 0;
1428 /* Tell the update routines that we have moved onto a new line with the
1429 prompt already displayed. Code originally from the version of readline
1430 distributed with CLISP. */
1432 rl_on_new_line_with_prompt ()
1434 int prompt_size, i, l, real_screenwidth, newlines;
1435 char *prompt_last_line;
1437 /* Initialize visible_line and invisible_line to ensure that they can hold
1438 the already-displayed prompt. */
1439 prompt_size = strlen (rl_prompt) + 1;
1440 init_line_structures (prompt_size);
1442 /* Make sure the line structures hold the already-displayed prompt for
1444 strcpy (visible_line, rl_prompt);
1445 strcpy (invisible_line, rl_prompt);
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;
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);
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
1465 beginning of the next line. Output a newline just to be safe. */
1466 if (l > 0 && (l % real_screenwidth) == 0)
1467 _rl_output_some_chars ("\n", 1);
1470 newlines = 0; i = 0;
1473 _rl_vis_botlin = newlines;
1474 vis_lbreaks[newlines++] = i;
1475 i += real_screenwidth;
1477 vis_lbreaks[newlines] = l;
1478 visible_wrap_offset = 0;
1483 /* Actually update the display, period. */
1485 rl_forced_update_display ()
1489 register char *temp = visible_line;
1496 (*rl_redisplay_function) ();
1500 /* Move the cursor from _rl_last_c_pos to NEW, which are buffer indices.
1501 DATA is the contents of the screen line of interest; i.e., where
1502 the movement is being done. */
1504 _rl_move_cursor_relative (new, data)
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;
1517 if (_rl_last_c_pos == new) return;
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))
1527 #if defined (__MSDOS__)
1528 putc ('\r', rl_outstream);
1530 tputs (_rl_term_cr, 1, _rl_output_character_function);
1531 #endif /* !__MSDOS__ */
1535 if (_rl_last_c_pos < new)
1537 /* Move the cursor forward. We do it by printing the command
1538 to move the cursor forward if there is one, else print that
1539 portion of the output buffer again. Which is cheaper? */
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)
1549 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
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);
1558 for (i = _rl_last_c_pos; i < new; i++)
1559 tputs (_rl_term_forward_char, 1, _rl_output_character_function);
1562 else if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1564 tputs (_rl_term_cr, 1, _rl_output_character_function);
1565 for (i = 0; i < new; i++)
1566 putc (data[i], rl_outstream);
1569 for (i = _rl_last_c_pos; i < new; i++)
1570 putc (data[i], rl_outstream);
1572 #else /* !HACK_TERMCAP_MOTION */
1574 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1576 tputs (_rl_term_cr, 1, _rl_output_character_function);
1577 for (i = 0; i < new; i++)
1578 putc (data[i], rl_outstream);
1581 for (i = _rl_last_c_pos; i < new; i++)
1582 putc (data[i], rl_outstream);
1584 #endif /* !HACK_TERMCAP_MOTION */
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)
1594 else if (_rl_last_c_pos > new)
1597 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1599 tputs (_rl_term_cr, 1, _rl_output_character_function);
1600 for (i = 0; i < new; i++)
1601 putc (data[i], rl_outstream);
1604 _rl_backspace (_rl_last_c_pos - new);
1607 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1608 _rl_last_c_pos = _rl_col_width (data, 0, new);
1610 _rl_last_c_pos = new;
1613 /* PWP: move the cursor up or down. */
1618 register int delta, i;
1620 if (_rl_last_v_pos == to || to > _rl_screenheight)
1623 if ((delta = to - _rl_last_v_pos) > 0)
1625 for (i = 0; i < delta; i++)
1626 putc ('\n', rl_outstream);
1627 #if defined (__MSDOS__)
1628 putc ('\r', rl_outstream);
1630 tputs (_rl_term_cr, 1, _rl_output_character_function);
1636 if (_rl_term_up && *_rl_term_up)
1637 for (i = 0; i < -delta; i++)
1638 tputs (_rl_term_up, 1, _rl_output_character_function);
1641 _rl_last_v_pos = to; /* Now TO is here */
1644 /* Physically print C on rl_outstream. This is for functions which know
1645 how to optimize the display. Return the number of characters output. */
1651 if (META_CHAR (c) && (_rl_output_meta_chars == 0))
1653 fprintf (rl_outstream, "M-");
1658 #if defined (DISPLAY_TABS)
1659 if ((CTRL_CHAR (c) && c != '\t') || c == RUBOUT)
1661 if (CTRL_CHAR (c) || c == RUBOUT)
1662 #endif /* !DISPLAY_TABS */
1664 fprintf (rl_outstream, "C-");
1666 c = CTRL_CHAR (c) ? UNCTRL (c) : '?';
1669 putc (c, rl_outstream);
1670 fflush (rl_outstream);
1675 rl_character_len (c, pos)
1676 register int c, pos;
1680 uc = (unsigned char)c;
1683 return ((_rl_output_meta_chars == 0) ? 4 : 1);
1687 #if defined (DISPLAY_TABS)
1688 return (((pos | 7) + 1) - pos);
1691 #endif /* !DISPLAY_TABS */
1694 if (CTRL_CHAR (c) || c == RUBOUT)
1697 return ((ISPRINT (uc)) ? 1 : 2);
1700 /* How to print things in the "echo-area". The prompt is treated as a
1703 #if defined (USE_VARARGS)
1705 #if defined (PREFER_STDARG)
1706 rl_message (const char *format, ...)
1708 rl_message (va_alist)
1713 #if defined (PREFER_VARARGS)
1717 #if defined (PREFER_STDARG)
1718 va_start (args, format);
1721 format = va_arg (args, char *);
1724 #if defined (HAVE_VSNPRINTF)
1725 vsnprintf (msg_buf, sizeof (msg_buf) - 1, format, args);
1727 vsprintf (msg_buf, format, args);
1728 msg_buf[sizeof(msg_buf) - 1] = '\0'; /* overflow? */
1732 rl_display_prompt = msg_buf;
1733 (*rl_redisplay_function) ();
1736 #else /* !USE_VARARGS */
1738 rl_message (format, arg1, arg2)
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) ();
1747 #endif /* !USE_VARARGS */
1749 /* How to clear things from the "echo-area". */
1753 rl_display_prompt = rl_prompt;
1754 (*rl_redisplay_function) ();
1759 rl_reset_line_state ()
1763 rl_display_prompt = rl_prompt ? rl_prompt : "";
1768 static char *saved_local_prompt;
1769 static char *saved_local_prefix;
1770 static int saved_last_invisible;
1771 static int saved_visible_length;
1776 saved_local_prompt = local_prompt;
1777 saved_local_prefix = local_prompt_prefix;
1778 saved_last_invisible = prompt_last_invisible;
1779 saved_visible_length = prompt_visible_length;
1781 local_prompt = local_prompt_prefix = (char *)0;
1782 prompt_last_invisible = prompt_visible_length = 0;
1786 rl_restore_prompt ()
1788 FREE (local_prompt);
1789 FREE (local_prompt_prefix);
1791 local_prompt = saved_local_prompt;
1792 local_prompt_prefix = saved_local_prefix;
1793 prompt_last_invisible = saved_last_invisible;
1794 prompt_visible_length = saved_visible_length;
1798 _rl_make_prompt_for_search (pchar)
1806 if (saved_local_prompt == 0)
1808 len = (rl_prompt && *rl_prompt) ? strlen (rl_prompt) : 0;
1809 pmt = (char *)xmalloc (len + 2);
1811 strcpy (pmt, rl_prompt);
1817 len = *saved_local_prompt ? strlen (saved_local_prompt) : 0;
1818 pmt = (char *)xmalloc (len + 2);
1820 strcpy (pmt, saved_local_prompt);
1823 local_prompt = savestring (pmt);
1824 prompt_last_invisible = saved_last_invisible;
1825 prompt_visible_length = saved_visible_length + 1;
1830 /* Quick redisplay hack when erasing characters at the end of the line. */
1832 _rl_erase_at_end_of_line (l)
1838 for (i = 0; i < l; i++)
1839 putc (' ', rl_outstream);
1841 for (i = 0; i < l; i++)
1842 visible_line[--_rl_last_c_pos] = '\0';
1846 /* Clear to the end of the line. COUNT is the minimum
1847 number of character spaces to clear, */
1849 _rl_clear_to_eol (count)
1852 if (_rl_term_clreol)
1853 tputs (_rl_term_clreol, 1, _rl_output_character_function);
1855 space_to_eol (count);
1858 /* Clear to the end of the line using spaces. COUNT is the minimum
1859 number of character spaces to clear, */
1861 space_to_eol (count)
1866 for (i = 0; i < count; i++)
1867 putc (' ', rl_outstream);
1869 _rl_last_c_pos += count;
1875 if (_rl_term_clrpag)
1876 tputs (_rl_term_clrpag, 1, _rl_output_character_function);
1881 /* Insert COUNT characters from STRING to the output stream at column COL. */
1883 insert_some_chars (string, count, col)
1888 if (MB_CUR_MAX == 1 || rl_byte_oriented)
1890 fprintf(stderr, "readline: debug: insert_some_chars: count (%d) != col (%d)\n", count, col);
1892 /* If IC is defined, then we do not have to "enter" insert mode. */
1897 buffer = tgoto (_rl_term_IC, 0, col);
1898 tputs (buffer, 1, _rl_output_character_function);
1899 _rl_output_some_chars (string, count);
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);
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)
1913 for (i = col; i--; )
1914 tputs (_rl_term_ic, 1, _rl_output_character_function);
1917 /* Print the text. */
1918 _rl_output_some_chars (string, count);
1920 /* If there is a string to turn off insert mode, we had best use
1922 if (_rl_term_ei && *_rl_term_ei)
1923 tputs (_rl_term_ei, 1, _rl_output_character_function);
1927 /* Delete COUNT characters from the display line. */
1929 delete_chars (count)
1932 if (count > _rl_screenwidth) /* XXX */
1935 if (_rl_term_DC && *_rl_term_DC)
1938 buffer = tgoto (_rl_term_DC, count, count);
1939 tputs (buffer, count, _rl_output_character_function);
1943 if (_rl_term_dc && *_rl_term_dc)
1945 tputs (_rl_term_dc, 1, _rl_output_character_function);
1955 /* If the cursor is the only thing on an otherwise-blank last line,
1956 compensate so we don't print an extra CRLF. */
1957 if (_rl_vis_botlin && _rl_last_c_pos == 0 &&
1958 visible_line[vis_lbreaks[_rl_vis_botlin]] == 0)
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))
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);
1976 fflush (rl_outstream);
1980 /* Move to the start of the current line. */
1986 #if defined (__MSDOS__)
1987 putc ('\r', rl_outstream);
1989 tputs (_rl_term_cr, 1, _rl_output_character_function);
1995 /* Redraw the last line of a multi-line prompt that may possibly contain
1996 terminal escape sequences. Called with the cursor at column 0 of the
1997 line to draw the prompt on. */
2002 char *oldp, *oldl, *oldlprefix;
2003 int oldlen, oldlast, oldplen, oldninvis;
2005 /* Geez, I should make this a struct. */
2006 oldp = rl_display_prompt;
2007 oldl = local_prompt;
2008 oldlprefix = local_prompt_prefix;
2009 oldlen = prompt_visible_length;
2010 oldplen = prompt_prefix_length;
2011 oldlast = prompt_last_invisible;
2012 oldninvis = prompt_invis_chars_first_line;
2014 rl_display_prompt = t;
2015 local_prompt = expand_prompt (t, &prompt_visible_length,
2016 &prompt_last_invisible,
2017 &prompt_invis_chars_first_line);
2018 local_prompt_prefix = (char *)NULL;
2019 rl_forced_update_display ();
2021 rl_display_prompt = oldp;
2022 local_prompt = oldl;
2023 local_prompt_prefix = oldlprefix;
2024 prompt_visible_length = oldlen;
2025 prompt_prefix_length = oldplen;
2026 prompt_last_invisible = oldlast;
2027 prompt_invis_chars_first_line = oldninvis;
2030 /* Redisplay the current line after a SIGWINCH is received. */
2032 _rl_redisplay_after_sigwinch ()
2036 /* Clear the current line and put the cursor at column 0. Make sure
2037 the right thing happens if we have wrapped to a new screen line. */
2040 #if defined (__MSDOS__)
2041 putc ('\r', rl_outstream);
2043 tputs (_rl_term_cr, 1, _rl_output_character_function);
2046 #if defined (__MSDOS__)
2047 space_to_eol (_rl_screenwidth);
2048 putc ('\r', rl_outstream);
2050 if (_rl_term_clreol)
2051 tputs (_rl_term_clreol, 1, _rl_output_character_function);
2054 space_to_eol (_rl_screenwidth);
2055 tputs (_rl_term_cr, 1, _rl_output_character_function);
2058 if (_rl_last_v_pos > 0)
2064 /* Redraw only the last line of a multi-line prompt. */
2065 t = strrchr (rl_display_prompt, '\n');
2067 redraw_prompt (++t);
2069 rl_forced_update_display ();
2073 _rl_clean_up_for_exit ()
2075 if (readline_echoing_p)
2077 _rl_move_vert (_rl_vis_botlin);
2079 fflush (rl_outstream);
2080 rl_restart_output (1, 0);
2085 _rl_erase_entire_line ()
2088 _rl_clear_to_eol (0);
2090 fflush (rl_outstream);
2093 /* return the `current display line' of the cursor -- the number of lines to
2094 move up to get to the first screen line of the current readline line. */
2096 _rl_current_display_line ()
2100 /* Find out whether or not there might be invisible characters in the
2102 if (rl_display_prompt == rl_prompt)
2103 nleft = _rl_last_c_pos - _rl_screenwidth - rl_visible_prompt_length;
2105 nleft = _rl_last_c_pos - _rl_screenwidth;
2108 ret = 1 + nleft / _rl_screenwidth;
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. */
2120 _rl_col_width (str, start, end)
2126 int tmp, point, width, max;
2134 while (point < start)
2136 tmp = mbrlen (str + point, max, &ps);
2137 if ((size_t)tmp == (size_t)-1 || (size_t)tmp == (size_t)-2)
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. */
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));
2150 break; /* Found '\0' */
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;
2165 tmp = mbrtowc (&wc, 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 /* and assume that the byte occupies a single column. */
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));
2182 break; /* Found '\0' */
2188 width += (tmp >= 0) ? tmp : 1;
2192 width += point - end;
2196 #endif /* HANDLE_MULTIBYTE */