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 1, 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 675 Mass Ave, Cambridge, MA 02139, USA. */
22 #define READLINE_LIBRARY
24 #if defined (HAVE_CONFIG_H)
29 #include <sys/types.h>
31 #if defined (HAVE_UNISTD_H)
33 #endif /* HAVE_UNISTD_H */
35 #if defined (HAVE_STDLIB_H)
38 # include "ansi_stdlib.h"
39 #endif /* HAVE_STDLIB_H */
41 #include "posixstat.h"
43 /* System-specific feature definitions and include files. */
46 /* Termcap library stuff. */
49 /* Some standard library routines. */
53 #if !defined (strchr) && !defined (__STDC__)
54 extern char *strchr (), *strrchr ();
55 #endif /* !strchr && !__STDC__ */
57 /* Global and pseudo-global variables and functions
58 imported from readline.c. */
59 extern char *rl_prompt;
60 extern int readline_echoing_p;
62 extern int _rl_output_meta_chars;
63 extern int _rl_horizontal_scroll_mode;
64 extern int _rl_mark_modified_lines;
65 extern int _rl_prefer_visible_bell;
67 /* Variables and functions imported from terminal.c */
68 extern void _rl_output_some_chars ();
69 extern int _rl_output_character_function ();
70 extern int _rl_backspace ();
72 extern char *term_clreol, *term_im, *term_ic, *term_ei, *term_DC;
73 extern char *term_up, *term_dc, *term_cr, *term_IC;
74 extern int screenheight, screenwidth, screenchars;
75 extern int terminal_can_insert, _rl_term_autowrap;
77 /* Pseudo-global functions (local to the readline library) exported
79 void _rl_move_cursor_relative (), _rl_output_some_chars ();
80 void _rl_move_vert ();
82 static void update_line (), clear_to_eol (), space_to_eol ();
83 static void delete_chars (), insert_some_chars ();
86 static int *inv_lbreaks, *vis_lbreaks;
88 extern char *xmalloc (), *xrealloc ();
90 /* Heuristic used to decide whether it is faster to move from CUR to NEW
91 by backing up or outputting a carriage return and moving forward. */
92 #define CR_FASTER(new, cur) (((new) + 1) < ((cur) - (new)))
94 /* **************************************************************** */
98 /* **************************************************************** */
100 /* This is the stuff that is hard for me. I never seem to write good
101 display routines in C. Let's see how I do this time. */
103 /* (PWP) Well... Good for a simple line updater, but totally ignores
104 the problems of input lines longer than the screen width.
106 update_line and the code that calls it makes a multiple line,
107 automatically wrapping line update. Careful attention needs
108 to be paid to the vertical position variables. */
110 /* Keep two buffers; one which reflects the current contents of the
111 screen, and the other to draw what we think the new contents should
112 be. Then compare the buffers, and make whatever changes to the
113 screen itself that we should. Finally, make the buffer that we
114 just drew into be the one which reflects the current contents of the
115 screen, and place the cursor where it belongs.
117 Commands that want to can fix the display themselves, and then let
118 this function know that the display has been fixed by setting the
119 RL_DISPLAY_FIXED variable. This is good for efficiency. */
121 /* Application-specific redisplay function. */
122 VFunction *rl_redisplay_function = rl_redisplay;
124 /* Global variables declared here. */
125 /* What YOU turn on when you have handled all redisplay yourself. */
126 int rl_display_fixed = 0;
128 int _rl_suppress_redisplay = 0;
130 /* The stuff that gets printed out before the actual text of the line.
131 This is usually pointing to rl_prompt. */
132 char *rl_display_prompt = (char *)NULL;
134 /* Pseudo-global variables declared here. */
135 /* The visible cursor position. If you print some text, adjust this. */
136 int _rl_last_c_pos = 0;
137 int _rl_last_v_pos = 0;
139 /* Number of lines currently on screen minus 1. */
140 int _rl_vis_botlin = 0;
142 /* Variables used only in this file. */
143 /* The last left edge of text that was displayed. This is used when
144 doing horizontal scrolling. It shifts in thirds of a screenwidth. */
145 static int last_lmargin;
147 /* The line display buffers. One is the line currently displayed on
148 the screen. The other is the line about to be displayed. */
149 static char *visible_line = (char *)NULL;
150 static char *invisible_line = (char *)NULL;
152 /* A buffer for `modeline' messages. */
153 static char msg_buf[128];
155 /* Non-zero forces the redisplay even if we thought it was unnecessary. */
156 static int forced_display;
158 /* Default and initial buffer size. Can grow. */
159 static int line_size = 1024;
161 static char *local_prompt, *local_prompt_prefix;
162 static int visible_length, prefix_length;
164 /* The number of invisible characters in the line currently being
165 displayed on the screen. */
166 static int visible_wrap_offset;
168 /* static so it 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 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 /* Expand the prompt string S and return the number of visible
179 characters in *LP, if LP is not null. This is currently more-or-less
180 a placeholder for expansion. LIP, if non-null is a place to store the
181 index of the last invisible character in ther eturned string. */
183 /* Current implementation:
184 \001 (^A) start non-visible characters
185 \002 (^B) end non-visible characters
186 all characters except \001 and \002 (following a \001) are copied to
187 the returned string; all characters except those between \001 and
188 \002 are assumed to be `visible'. */
191 expand_prompt (pmt, lp, lip)
196 int l, rl, last, ignoring;
198 /* Short-circuit if we can. */
199 if (strchr (pmt, RL_PROMPT_START_IGNORE) == 0)
201 r = savestring (pmt);
208 r = ret = xmalloc (l + 1);
210 for (rl = ignoring = last = 0, p = pmt; p && *p; p++)
212 /* This code strips the invisible character string markers
213 RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE */
214 if (*p == RL_PROMPT_START_IGNORE)
219 else if (ignoring && *p == RL_PROMPT_END_IGNORE)
242 * Expand the prompt string into the various display components, if
245 * local_prompt = expanded last line of string in rl_display_prompt
246 * (portion after the final newline)
247 * local_prompt_prefix = portion before last newline of rl_display_prompt,
248 * expanded via expand_prompt
249 * visible_length = number of visible characters in local_prompt
250 * prefix_length = number of visible characters in local_prompt_prefix
252 * This function is called once per call to readline(). It may also be
253 * called arbitrarily to expand the primary prompt.
255 * The return value is the number of visible characters on the last line
256 * of the (possibly multi-line) prompt.
259 rl_expand_prompt (prompt)
265 /* Clear out any saved values. */
268 if (local_prompt_prefix)
269 free (local_prompt_prefix);
270 local_prompt = local_prompt_prefix = (char *)0;
273 if (prompt == 0 || *prompt == 0)
276 p = strrchr (prompt, '\n');
279 /* The prompt is only one line. */
280 local_prompt = expand_prompt (prompt, &visible_length, &last_invisible);
281 local_prompt_prefix = (char *)0;
282 return (visible_length);
286 /* The prompt spans multiple lines. */
288 local_prompt = expand_prompt (p, &visible_length, &last_invisible);
290 /* The portion of the prompt string up to and including the
291 final newline is now null-terminated. */
292 local_prompt_prefix = expand_prompt (prompt, &prefix_length, (int *)NULL);
294 return (prefix_length);
298 /* Basic redisplay algorithm. */
302 register int in, out, c, linenum, cursor_linenum;
304 int c_pos, inv_botlin, lb_botlin, lb_linenum;
306 char *prompt_this_line;
308 if (!readline_echoing_p)
311 if (!rl_display_prompt)
312 rl_display_prompt = "";
314 if (invisible_line == 0)
316 visible_line = xmalloc (line_size);
317 invisible_line = xmalloc (line_size);
318 for (in = 0; in < line_size; in++)
320 visible_line[in] = 0;
321 invisible_line[in] = 1;
324 /* should be enough, but then again, this is just for testing. */
325 inv_lbreaks = (int *)malloc (256 * sizeof (int));
326 vis_lbreaks = (int *)malloc (256 * sizeof (int));
327 inv_lbreaks[0] = vis_lbreaks[0] = 0;
332 /* Draw the line into the buffer. */
335 line = invisible_line;
336 out = inv_botlin = 0;
338 /* Mark the line as modified or not. We only do this for history
340 if (_rl_mark_modified_lines && current_history () && rl_undo_list)
346 /* If someone thought that the redisplay was handled, but the currently
347 visible line has a different modification state than the one about
348 to become visible, then correct the caller's misconception. */
349 if (visible_line[0] != invisible_line[0])
350 rl_display_fixed = 0;
352 /* If the prompt to be displayed is the `primary' readline prompt (the
353 one passed to readline()), use the values we have already expanded.
354 If not, use what's already in rl_display_prompt. WRAP_OFFSET is the
355 number of non-visible characters in the prompt string. */
356 if (rl_display_prompt == rl_prompt || local_prompt)
358 int local_len = local_prompt ? strlen (local_prompt) : 0;
359 if (local_prompt_prefix && forced_display)
360 _rl_output_some_chars (local_prompt_prefix, strlen (local_prompt_prefix));
364 strncpy (line + out, local_prompt, local_len);
368 wrap_offset = local_len - visible_length;
373 prompt_this_line = strrchr (rl_display_prompt, '\n');
374 if (!prompt_this_line)
375 prompt_this_line = rl_display_prompt;
381 _rl_output_some_chars (rl_display_prompt, prompt_this_line - rl_display_prompt);
382 /* Make sure we are at column zero even after a newline,
383 regardless of the state of terminal output processing. */
384 if (prompt_this_line[-2] != '\r')
389 pmtlen = strlen (prompt_this_line);
390 strncpy (line + out, prompt_this_line, pmtlen);
396 #define CHECK_LPOS() \
399 if (lpos >= screenwidth) \
401 inv_lbreaks[++newlines] = out; \
406 /* inv_lbreaks[i] is where line i starts in the buffer. */
407 inv_lbreaks[newlines = 0] = 0;
409 for (in = 0, lpos = out - wrap_offset; in < rl_end; in++)
411 c = (unsigned char)rl_line_buffer[in];
413 if (out + 8 >= line_size) /* XXX - 8 for \t */
416 visible_line = xrealloc (visible_line, line_size);
417 invisible_line = xrealloc (invisible_line, line_size);
418 line = invisible_line;
424 lb_linenum = newlines;
429 if (_rl_output_meta_chars == 0)
431 sprintf (line + out, "\\%o", c);
433 if (lpos + 4 >= screenwidth)
437 temp = screenwidth - lpos;
438 inv_lbreaks[++newlines] = out + temp;
452 #if defined (DISPLAY_TABS)
455 register int temp, newout;
456 newout = (out | (int)7) + 1;
458 if (lpos + temp >= screenwidth)
461 temp2 = screenwidth - lpos;
462 inv_lbreaks[++newlines] = out + temp2;
475 else if (c == '\n' && _rl_horizontal_scroll_mode == 0 && term_up && *term_up)
477 line[out++] = '\0'; /* XXX - sentinel */
478 inv_lbreaks[++newlines] = out;
481 else if (CTRL_CHAR (c) || c == RUBOUT)
485 line[out++] = CTRL_CHAR (c) ? UNCTRL (c) : '?';
498 lb_linenum = newlines;
501 inv_botlin = lb_botlin = newlines;
502 inv_lbreaks[newlines+1] = out;
503 cursor_linenum = lb_linenum;
505 /* C_POS == position in buffer where cursor should be placed. */
507 /* PWP: now is when things get a bit hairy. The visible and invisible
508 line buffers are really multiple lines, which would wrap every
509 (screenwidth - 1) characters. Go through each in turn, finding
510 the changed region and updating it. The line order is top to bottom. */
512 /* If we can move the cursor up and down, then use multiple lines,
513 otherwise, let long lines display in a single terminal line, and
514 horizontally scroll it. */
516 if (_rl_horizontal_scroll_mode == 0 && term_up && *term_up)
518 int nleft, pos, changed_screen_line;
520 if (!rl_display_fixed || forced_display)
524 /* If we have more than a screenful of material to display, then
525 only display a screenful. We should display the last screen,
527 if (out >= screenchars)
528 out = screenchars - 1;
530 /* The first line is at character position 0 in the buffer. The
531 second and subsequent lines start at inv_lbreaks[N], offset by
532 OFFSET (which has already been calculated above). */
534 #define W_OFFSET(line, offset) ((line) == 0 ? offset : 0)
535 #define VIS_LLEN(l) ((l) > _rl_vis_botlin ? 0 : (vis_lbreaks[l+1] - vis_lbreaks[l]))
536 #define INV_LLEN(l) (inv_lbreaks[l+1] - inv_lbreaks[l])
537 #define VIS_CHARS(line) (visible_line + vis_lbreaks[line])
538 #define VIS_LINE(line) ((line) > _rl_vis_botlin) ? "" : VIS_CHARS(line)
539 #define INV_LINE(line) (invisible_line + inv_lbreaks[line])
541 /* For each line in the buffer, do the updating display. */
542 for (linenum = 0; linenum <= inv_botlin; linenum++)
544 update_line (VIS_LINE(linenum), INV_LINE(linenum), linenum,
545 VIS_LLEN(linenum), INV_LLEN(linenum), inv_botlin);
547 /* If this is the line with the prompt, we might need to
548 compensate for invisible characters in the new line. Do
549 this only if there is not more than one new line (which
550 implies that we completely overwrite the old visible line)
551 and the new line is shorter than the old. Make sure we are
552 at the end of the new line before clearing. */
554 inv_botlin == 0 && _rl_last_c_pos == out &&
555 (wrap_offset > visible_wrap_offset) &&
556 (_rl_last_c_pos < visible_first_line_len))
558 nleft = screenwidth + wrap_offset - _rl_last_c_pos;
560 clear_to_eol (nleft);
563 /* Since the new first line is now visible, save its length. */
565 visible_first_line_len = (inv_botlin > 0) ? inv_lbreaks[1] : out - wrap_offset;
568 /* We may have deleted some lines. If so, clear the left over
569 blank ones at the bottom out. */
570 if (_rl_vis_botlin > inv_botlin)
573 for (; linenum <= _rl_vis_botlin; linenum++)
575 tt = VIS_CHARS (linenum);
576 _rl_move_vert (linenum);
577 _rl_move_cursor_relative (0, tt);
579 ((linenum == _rl_vis_botlin) ? strlen (tt) : screenwidth);
582 _rl_vis_botlin = inv_botlin;
584 /* CHANGED_SCREEN_LINE is set to 1 if we have moved to a
585 different screen line during this redisplay. */
586 changed_screen_line = _rl_last_v_pos != cursor_linenum;
587 if (changed_screen_line)
589 _rl_move_vert (cursor_linenum);
590 /* If we moved up to the line with the prompt using term_up,
591 the physical cursor position on the screen stays the same,
592 but the buffer position needs to be adjusted to account
593 for invisible characters. */
594 if (cursor_linenum == 0 && wrap_offset)
595 _rl_last_c_pos += wrap_offset;
598 /* We have to reprint the prompt if it contains invisible
599 characters, since it's not generally OK to just reprint
600 the characters from the current cursor position. But we
601 only need to reprint it if the cursor is before the last
602 invisible character in the prompt string. */
603 nleft = visible_length + wrap_offset;
604 if (cursor_linenum == 0 && wrap_offset > 0 && _rl_last_c_pos > 0 &&
605 _rl_last_c_pos <= last_invisible && local_prompt)
608 tputs (term_cr, 1, _rl_output_character_function);
609 _rl_output_some_chars (local_prompt, nleft);
610 _rl_last_c_pos = nleft;
613 /* Where on that line? And where does that line start
615 pos = inv_lbreaks[cursor_linenum];
616 /* nleft == number of characters in the line buffer between the
617 start of the line and the cursor position. */
620 /* Since _rl_backspace() doesn't know about invisible characters in the
621 prompt, and there's no good way to tell it, we compensate for
622 those characters here and call _rl_backspace() directly. */
623 if (wrap_offset && cursor_linenum == 0 && nleft < _rl_last_c_pos)
625 _rl_backspace (_rl_last_c_pos - nleft);
626 _rl_last_c_pos = nleft;
629 if (nleft != _rl_last_c_pos)
630 _rl_move_cursor_relative (nleft, &invisible_line[pos]);
633 else /* Do horizontal scrolling. */
635 #define M_OFFSET(margin, offset) ((margin) == 0 ? offset : 0)
636 int lmargin, ndisp, nleft, phys_c_pos, t;
638 /* Always at top line. */
641 /* Compute where in the buffer the displayed line should start. This
644 /* The number of characters that will be displayed before the cursor. */
645 ndisp = c_pos - wrap_offset;
646 nleft = visible_length + wrap_offset;
647 /* Where the new cursor position will be on the screen. This can be
648 longer than SCREENWIDTH; if it is, lmargin will be adjusted. */
649 phys_c_pos = c_pos - (last_lmargin ? last_lmargin : wrap_offset);
652 /* If the number of characters had already exceeded the screenwidth,
653 last_lmargin will be > 0. */
655 /* If the number of characters to be displayed is more than the screen
656 width, compute the starting offset so that the cursor is about
657 two-thirds of the way across the screen. */
658 if (phys_c_pos > screenwidth - 2)
660 lmargin = c_pos - (2 * t);
663 /* If the left margin would be in the middle of a prompt with
664 invisible characters, don't display the prompt at all. */
665 if (wrap_offset && lmargin > 0 && lmargin < nleft)
668 else if (ndisp < screenwidth - 2) /* XXX - was -1 */
670 else if (phys_c_pos < 1)
672 /* If we are moving back towards the beginning of the line and
673 the last margin is no longer correct, compute a new one. */
674 lmargin = ((c_pos - 1) / t) * t; /* XXX */
675 if (wrap_offset && lmargin > 0 && lmargin < nleft)
679 lmargin = last_lmargin;
681 /* If the first character on the screen isn't the first character
682 in the display line, indicate this with a special character. */
686 /* If SCREENWIDTH characters starting at LMARGIN do not encompass
687 the whole line, indicate that with a special characters at the
688 right edge of the screen. If LMARGIN is 0, we need to take the
689 wrap offset into account. */
690 t = lmargin + M_OFFSET (lmargin, wrap_offset) + screenwidth;
694 if (!rl_display_fixed || forced_display || lmargin != last_lmargin)
697 update_line (&visible_line[last_lmargin],
698 &invisible_line[lmargin],
700 screenwidth + visible_wrap_offset,
701 screenwidth + (lmargin ? 0 : wrap_offset),
704 /* If the visible new line is shorter than the old, but the number
705 of invisible characters is greater, and we are at the end of
706 the new line, we need to clear to eol. */
707 t = _rl_last_c_pos - M_OFFSET (lmargin, wrap_offset);
708 if ((M_OFFSET (lmargin, wrap_offset) > visible_wrap_offset) &&
709 (_rl_last_c_pos == out) &&
710 t < visible_first_line_len)
712 nleft = screenwidth - t;
713 clear_to_eol (nleft);
715 visible_first_line_len = out - lmargin - M_OFFSET (lmargin, wrap_offset);
716 if (visible_first_line_len > screenwidth)
717 visible_first_line_len = screenwidth;
719 _rl_move_cursor_relative (c_pos - lmargin, &invisible_line[lmargin]);
720 last_lmargin = lmargin;
723 fflush (rl_outstream);
725 /* Swap visible and non-visible lines. */
727 char *temp = visible_line;
728 int *itemp = vis_lbreaks;
729 visible_line = invisible_line;
730 invisible_line = temp;
731 vis_lbreaks = inv_lbreaks;
733 rl_display_fixed = 0;
734 /* If we are displaying on a single line, and last_lmargin is > 0, we
735 are not displaying any invisible characters, so set visible_wrap_offset
737 if (_rl_horizontal_scroll_mode && last_lmargin)
738 visible_wrap_offset = 0;
740 visible_wrap_offset = wrap_offset;
744 /* PWP: update_line() is based on finding the middle difference of each
745 line on the screen; vis:
747 /old first difference
748 /beginning of line | /old last same /old EOL
750 old: eddie> Oh, my little gruntle-buggy is to me, as lurgid as
751 new: eddie> Oh, my little buggy says to me, as lurgid as
753 \beginning of line | \new last same \new end of line
754 \new first difference
756 All are character pointers for the sake of speed. Special cases for
757 no differences, as well as for end of line additions must be handeled.
759 Could be made even smarter, but this works well enough */
761 update_line (old, new, current_line, omax, nmax, inv_botlin)
762 register char *old, *new;
763 int current_line, omax, nmax, inv_botlin;
765 register char *ofd, *ols, *oe, *nfd, *nls, *ne;
766 int temp, lendiff, wsatend, od, nd;
767 int current_invis_chars;
769 /* If we're at the right edge of a terminal that supports xn, we're
770 ready to wrap around, so do so. This fixes problems with knowing
771 the exact cursor position and cut-and-paste with certain terminal
772 emulators. In this calculation, TEMP is the physical screen
773 position of the cursor. */
774 temp = _rl_last_c_pos - W_OFFSET(_rl_last_v_pos, visible_wrap_offset);
775 if (temp == screenwidth && _rl_term_autowrap && !_rl_horizontal_scroll_mode
776 && _rl_last_v_pos == current_line - 1)
779 putc (new[0], rl_outstream);
781 putc (' ', rl_outstream);
782 _rl_last_c_pos = 1; /* XXX */
784 if (old[0] && new[0])
788 /* Find first difference. */
789 for (ofd = old, nfd = new;
790 (ofd - old < omax) && *ofd && (*ofd == *nfd);
794 /* Move to the end of the screen line. ND and OD are used to keep track
795 of the distance between ne and new and oe and old, respectively, to
796 move a subtraction out of each loop. */
797 for (od = ofd - old, oe = ofd; od < omax && *oe; oe++, od++);
798 for (nd = nfd - new, ne = nfd; nd < nmax && *ne; ne++, nd++);
800 /* If no difference, continue to next line. */
801 if (ofd == oe && nfd == ne)
804 wsatend = 1; /* flag for trailing whitespace */
805 ols = oe - 1; /* find last same */
807 while ((ols > ofd) && (nls > nfd) && (*ols == *nls))
820 else if (*ols != *nls)
822 if (*ols) /* don't step past the NUL */
828 /* count of invisible characters in the current invisible line. */
829 current_invis_chars = W_OFFSET (current_line, wrap_offset);
830 if (_rl_last_v_pos != current_line)
832 _rl_move_vert (current_line);
833 if (current_line == 0 && visible_wrap_offset)
834 _rl_last_c_pos += visible_wrap_offset;
837 /* If this is the first line and there are invisible characters in the
838 prompt string, and the prompt string has not changed, and the current
839 cursor position is before the last invisible character in the prompt,
840 and the index of the character to move to is past the end of the prompt
841 string, then redraw the entire prompt string. We can only do this
842 reliably if the terminal supports a `cr' capability.
844 This is not an efficiency hack -- there is a problem with redrawing
845 portions of the prompt string if they contain terminal escape
846 sequences (like drawing the `unbold' sequence without a corresponding
847 `bold') that manifests itself on certain terminals. */
849 lendiff = local_prompt ? strlen (local_prompt) : 0;
850 od = ofd - old; /* index of first difference in visible line */
851 if (current_line == 0 && !_rl_horizontal_scroll_mode &&
852 term_cr && lendiff > visible_length && _rl_last_c_pos > 0 &&
853 od > lendiff && _rl_last_c_pos < last_invisible)
855 tputs (term_cr, 1, _rl_output_character_function);
856 _rl_output_some_chars (local_prompt, lendiff);
857 _rl_last_c_pos = lendiff;
860 _rl_move_cursor_relative (od, old);
862 /* if (len (new) > len (old)) */
863 lendiff = (nls - nfd) - (ols - ofd);
865 /* If we are changing the number of invisible characters in a line, and
866 the spot of first difference is before the end of the invisible chars,
867 lendiff needs to be adjusted. */
868 if (current_line == 0 && !_rl_horizontal_scroll_mode &&
869 current_invis_chars != visible_wrap_offset)
871 temp = visible_wrap_offset - current_invis_chars;
875 /* Insert (diff (len (old), len (new)) ch. */
879 /* Non-zero if we're increasing the number of lines. */
880 int gl = current_line >= _rl_vis_botlin && inv_botlin > _rl_vis_botlin;
881 /* Sometimes it is cheaper to print the characters rather than
882 use the terminal's capabilities. If we're growing the number
883 of lines, make sure we actually cause the new line to wrap
884 around on auto-wrapping terminals. */
885 if (terminal_can_insert && ((2 * temp) >= lendiff || term_IC) && (!_rl_term_autowrap || !gl))
887 /* If lendiff > visible_length and _rl_last_c_pos == 0 and
888 _rl_horizontal_scroll_mode == 1, inserting the characters with
889 term_IC or term_ic will screw up the screen because of the
890 invisible characters. We need to just draw them. */
891 if (*ols && (!_rl_horizontal_scroll_mode || _rl_last_c_pos > 0 ||
892 lendiff <= visible_length || !current_invis_chars))
894 insert_some_chars (nfd, lendiff);
895 _rl_last_c_pos += lendiff;
899 /* At the end of a line the characters do not have to
900 be "inserted". They can just be placed on the screen. */
901 /* However, this screws up the rest of this block, which
902 assumes you've done the insert because you can. */
903 _rl_output_some_chars (nfd, lendiff);
904 _rl_last_c_pos += lendiff;
908 /* We have horizontal scrolling and we are not inserting at
909 the end. We have invisible characters in this line. This
911 _rl_output_some_chars (nfd, temp);
912 _rl_last_c_pos += temp;
915 /* Copy (new) chars to screen from first diff to last match. */
917 if ((temp - lendiff) > 0)
919 _rl_output_some_chars (nfd + lendiff, temp - lendiff);
920 _rl_last_c_pos += temp - lendiff;
925 /* cannot insert chars, write to EOL */
926 _rl_output_some_chars (nfd, temp);
927 _rl_last_c_pos += temp;
930 else /* Delete characters from line. */
932 /* If possible and inexpensive to use terminal deletion, then do so. */
933 if (term_dc && (2 * temp) >= -lendiff)
935 /* If all we're doing is erasing the invisible characters in the
936 prompt string, don't bother. It screws up the assumptions
937 about what's on the screen. */
938 if (_rl_horizontal_scroll_mode && _rl_last_c_pos == 0 &&
939 -lendiff == visible_wrap_offset)
943 delete_chars (-lendiff); /* delete (diff) characters */
945 /* Copy (new) chars to screen from first diff to last match */
949 _rl_output_some_chars (nfd, temp);
950 _rl_last_c_pos += temp;
953 /* Otherwise, print over the existing material. */
958 _rl_output_some_chars (nfd, temp);
959 _rl_last_c_pos += temp;
961 lendiff = (oe - old) - (ne - new);
962 if (_rl_term_autowrap && current_line < inv_botlin)
963 space_to_eol (lendiff);
965 clear_to_eol (lendiff);
970 /* Tell the update routines that we have moved onto a new (empty) line. */
975 visible_line[0] = '\0';
977 _rl_last_c_pos = _rl_last_v_pos = 0;
978 _rl_vis_botlin = last_lmargin = 0;
980 vis_lbreaks[0] = vis_lbreaks[1] = 0;
981 visible_wrap_offset = 0;
985 /* Actually update the display, period. */
987 rl_forced_update_display ()
991 register char *temp = visible_line;
998 (*rl_redisplay_function) ();
1002 /* Move the cursor from _rl_last_c_pos to NEW, which are buffer indices.
1003 DATA is the contents of the screen line of interest; i.e., where
1004 the movement is being done. */
1006 _rl_move_cursor_relative (new, data)
1012 /* If we don't have to do anything, then return. */
1013 if (_rl_last_c_pos == new) return;
1015 /* It may be faster to output a CR, and then move forwards instead
1016 of moving backwards. */
1017 /* i == current physical cursor position. */
1018 i = _rl_last_c_pos - W_OFFSET(_rl_last_v_pos, visible_wrap_offset);
1019 if (new == 0 || CR_FASTER (new, _rl_last_c_pos) ||
1020 (_rl_term_autowrap && i == screenwidth))
1022 #if defined (__MSDOS__)
1023 putc ('\r', rl_outstream);
1025 tputs (term_cr, 1, _rl_output_character_function);
1026 #endif /* !__MSDOS__ */
1030 if (_rl_last_c_pos < new)
1032 /* Move the cursor forward. We do it by printing the command
1033 to move the cursor forward if there is one, else print that
1034 portion of the output buffer again. Which is cheaper? */
1036 /* The above comment is left here for posterity. It is faster
1037 to print one character (non-control) than to print a control
1038 sequence telling the terminal to move forward one character.
1039 That kind of control is for people who don't know what the
1040 data is underneath the cursor. */
1041 #if defined (HACK_TERMCAP_MOTION)
1042 extern char *term_forward_char;
1044 if (term_forward_char)
1045 for (i = _rl_last_c_pos; i < new; i++)
1046 tputs (term_forward_char, 1, _rl_output_character_function);
1048 for (i = _rl_last_c_pos; i < new; i++)
1049 putc (data[i], rl_outstream);
1051 for (i = _rl_last_c_pos; i < new; i++)
1052 putc (data[i], rl_outstream);
1053 #endif /* HACK_TERMCAP_MOTION */
1055 else if (_rl_last_c_pos != new)
1056 _rl_backspace (_rl_last_c_pos - new);
1057 _rl_last_c_pos = new;
1060 /* PWP: move the cursor up or down. */
1065 register int delta, i;
1067 if (_rl_last_v_pos == to || to > screenheight)
1070 #if defined (__GO32__)
1074 ScreenGetCursor (&row, &col);
1075 ScreenSetCursor ((row + to - _rl_last_v_pos), col);
1077 #else /* !__GO32__ */
1079 if ((delta = to - _rl_last_v_pos) > 0)
1081 for (i = 0; i < delta; i++)
1082 putc ('\n', rl_outstream);
1083 tputs (term_cr, 1, _rl_output_character_function);
1088 if (term_up && *term_up)
1089 for (i = 0; i < -delta; i++)
1090 tputs (term_up, 1, _rl_output_character_function);
1092 #endif /* !__GO32__ */
1093 _rl_last_v_pos = to; /* Now TO is here */
1096 /* Physically print C on rl_outstream. This is for functions which know
1097 how to optimize the display. Return the number of characters output. */
1103 if (META_CHAR (c) && (_rl_output_meta_chars == 0))
1105 fprintf (rl_outstream, "M-");
1110 #if defined (DISPLAY_TABS)
1111 if ((CTRL_CHAR (c) && c != '\t') || c == RUBOUT)
1113 if (CTRL_CHAR (c) || c == RUBOUT)
1114 #endif /* !DISPLAY_TABS */
1116 fprintf (rl_outstream, "C-");
1118 c = CTRL_CHAR (c) ? UNCTRL (c) : '?';
1121 putc (c, rl_outstream);
1122 fflush (rl_outstream);
1127 rl_character_len (c, pos)
1128 register int c, pos;
1132 uc = (unsigned char)c;
1135 return ((_rl_output_meta_chars == 0) ? 4 : 1);
1139 #if defined (DISPLAY_TABS)
1140 return (((pos | 7) + 1) - pos);
1143 #endif /* !DISPLAY_TABS */
1146 if (CTRL_CHAR (c) || c == RUBOUT)
1149 return ((isprint (uc)) ? 1 : 2);
1152 /* How to print things in the "echo-area". The prompt is treated as a
1155 #if defined (USE_VARARGS)
1157 #if defined (PREFER_STDARG)
1158 rl_message (const char *format, ...)
1160 rl_message (va_alist)
1165 #if defined (PREFER_VARARGS)
1169 #if defined (PREFER_STDARG)
1170 va_start (args, format);
1173 format = va_arg (args, char *);
1176 vsprintf (msg_buf, format, args);
1179 rl_display_prompt = msg_buf;
1180 (*rl_redisplay_function) ();
1183 #else /* !USE_VARARGS */
1185 rl_message (format, arg1, arg2)
1188 sprintf (msg_buf, format, arg1, arg2);
1189 rl_display_prompt = msg_buf;
1190 (*rl_redisplay_function) ();
1193 #endif /* !USE_VARARGS */
1195 /* How to clear things from the "echo-area". */
1199 rl_display_prompt = rl_prompt;
1200 (*rl_redisplay_function) ();
1205 rl_reset_line_state ()
1209 rl_display_prompt = rl_prompt ? rl_prompt : "";
1214 static char *saved_local_prompt;
1215 static char *saved_local_prefix;
1216 static int saved_last_invisible;
1217 static int saved_visible_length;
1222 saved_local_prompt = local_prompt;
1223 saved_local_prefix = local_prompt_prefix;
1224 saved_last_invisible = last_invisible;
1225 saved_visible_length = visible_length;
1227 local_prompt = local_prompt_prefix = (char *)0;
1228 last_invisible = visible_length = 0;
1232 _rl_restore_prompt ()
1235 free (local_prompt);
1236 if (local_prompt_prefix)
1237 free (local_prompt_prefix);
1239 local_prompt = saved_local_prompt;
1240 local_prompt_prefix = saved_local_prefix;
1241 last_invisible = saved_last_invisible;
1242 visible_length = saved_visible_length;
1246 _rl_make_prompt_for_search (pchar)
1254 if (saved_local_prompt == 0)
1256 len = (rl_prompt && *rl_prompt) ? strlen (rl_prompt) : 0;
1257 pmt = xmalloc (len + 2);
1259 strcpy (pmt, rl_prompt);
1265 len = *saved_local_prompt ? strlen (saved_local_prompt) : 0;
1266 pmt = xmalloc (len + 2);
1268 strcpy (pmt, saved_local_prompt);
1271 local_prompt = savestring (pmt);
1272 last_invisible = saved_last_invisible;
1273 visible_length = saved_visible_length + 1;
1278 /* Quick redisplay hack when erasing characters at the end of the line. */
1280 _rl_erase_at_end_of_line (l)
1286 for (i = 0; i < l; i++)
1287 putc (' ', rl_outstream);
1289 for (i = 0; i < l; i++)
1290 visible_line[--_rl_last_c_pos] = '\0';
1294 /* Clear to the end of the line. COUNT is the minimum
1295 number of character spaces to clear, */
1297 clear_to_eol (count)
1300 #if !defined (__GO32__)
1303 tputs (term_clreol, 1, _rl_output_character_function);
1306 #endif /* !__GO32__ */
1307 space_to_eol (count);
1310 /* Clear to the end of the line using spaces. COUNT is the minimum
1311 number of character spaces to clear, */
1313 space_to_eol (count)
1318 for (i = 0; i < count; i++)
1319 putc (' ', rl_outstream);
1321 _rl_last_c_pos += count;
1324 /* Insert COUNT characters from STRING to the output stream. */
1326 insert_some_chars (string, count)
1330 #if defined (__GO32__)
1331 int row, col, width;
1334 ScreenGetCursor (&row, &col);
1335 width = ScreenCols ();
1336 row_start = ScreenPrimary + (row * width);
1338 memcpy (row_start + col + count, row_start + col, width - col - count);
1340 /* Place the text on the screen. */
1341 _rl_output_some_chars (string, count);
1344 /* If IC is defined, then we do not have to "enter" insert mode. */
1348 buffer = tgoto (term_IC, 0, count);
1349 tputs (buffer, 1, _rl_output_character_function);
1350 _rl_output_some_chars (string, count);
1356 /* If we have to turn on insert-mode, then do so. */
1357 if (term_im && *term_im)
1358 tputs (term_im, 1, _rl_output_character_function);
1360 /* If there is a special command for inserting characters, then
1361 use that first to open up the space. */
1362 if (term_ic && *term_ic)
1364 for (i = count; i--; )
1365 tputs (term_ic, 1, _rl_output_character_function);
1368 /* Print the text. */
1369 _rl_output_some_chars (string, count);
1371 /* If there is a string to turn off insert mode, we had best use
1373 if (term_ei && *term_ei)
1374 tputs (term_ei, 1, _rl_output_character_function);
1376 #endif /* !__GO32__ */
1379 /* Delete COUNT characters from the display line. */
1381 delete_chars (count)
1384 #if defined (__GO32__)
1385 int row, col, width;
1388 ScreenGetCursor (&row, &col);
1389 width = ScreenCols ();
1390 row_start = ScreenPrimary + (row * width);
1392 memcpy (row_start + col, row_start + col + count, width - col - count);
1393 memset (row_start + width - count, 0, count * 2);
1396 if (count > screenwidth) /* XXX */
1399 if (term_DC && *term_DC)
1402 buffer = tgoto (term_DC, count, count);
1403 tputs (buffer, count, _rl_output_character_function);
1407 if (term_dc && *term_dc)
1409 tputs (term_dc, 1, _rl_output_character_function);
1411 #endif /* !__GO32__ */
1420 /* If the cursor is the only thing on an otherwise-blank last line,
1421 compensate so we don't print an extra CRLF. */
1422 if (_rl_vis_botlin && _rl_last_c_pos == 0 &&
1423 visible_line[inv_lbreaks[_rl_vis_botlin]+1] == 0)
1428 _rl_move_vert (_rl_vis_botlin);
1429 /* If we've wrapped lines, remove the final xterm line-wrap flag. */
1430 if (full_lines && _rl_term_autowrap && (VIS_LLEN(_rl_vis_botlin) == screenwidth))
1433 last_line = &visible_line[inv_lbreaks[_rl_vis_botlin]];
1434 _rl_move_cursor_relative (screenwidth - 1, last_line);
1436 putc (last_line[screenwidth - 1], rl_outstream);
1440 fflush (rl_outstream);
1444 /* Move to the start of the current line. */
1450 tputs (term_cr, 1, _rl_output_character_function);
1455 /* Redisplay the current line after a SIGWINCH is received. */
1457 _rl_redisplay_after_sigwinch ()
1461 /* Clear the current line and put the cursor at column 0. Make sure
1462 the right thing happens if we have wrapped to a new screen line. */
1465 tputs (term_cr, 1, _rl_output_character_function);
1468 tputs (term_clreol, 1, _rl_output_character_function);
1471 space_to_eol (screenwidth);
1472 tputs (term_cr, 1, _rl_output_character_function);
1474 if (_rl_last_v_pos > 0)
1480 /* Redraw only the last line of a multi-line prompt. */
1481 t = strrchr (rl_display_prompt, '\n');
1484 oldp = rl_display_prompt;
1485 rl_display_prompt = ++t;
1486 rl_forced_update_display ();
1487 rl_display_prompt = oldp;
1490 rl_forced_update_display ();
1494 _rl_clean_up_for_exit ()
1496 if (readline_echoing_p)
1498 _rl_move_vert (_rl_vis_botlin);
1500 fflush (rl_outstream);
1501 rl_restart_output ();