1 /* $FreeBSD: src/contrib/libreadline/display.c,v 1.3.2.2 2000/07/06 23:04:23 ache Exp $ */
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. */
48 /* Termcap library stuff. */
51 /* Some standard library routines. */
55 #include "rlprivate.h"
58 #if !defined (strchr) && !defined (__STDC__)
59 extern char *strchr (), *strrchr ();
60 #endif /* !strchr && !__STDC__ */
62 #if defined (HACK_TERMCAP_MOTION)
63 extern char *term_forward_char;
66 static void update_line __P((char *, char *, int, int, int, int));
67 static void space_to_eol __P((int));
68 static void delete_chars __P((int));
69 static void insert_some_chars __P((char *, int));
70 static void cr __P((void));
72 static int *inv_lbreaks, *vis_lbreaks;
73 static int inv_lbsize, vis_lbsize;
75 /* Heuristic used to decide whether it is faster to move from CUR to NEW
76 by backing up or outputting a carriage return and moving forward. */
77 #define CR_FASTER(new, cur) (((new) + 1) < ((cur) - (new)))
79 /* **************************************************************** */
83 /* **************************************************************** */
85 /* This is the stuff that is hard for me. I never seem to write good
86 display routines in C. Let's see how I do this time. */
88 /* (PWP) Well... Good for a simple line updater, but totally ignores
89 the problems of input lines longer than the screen width.
91 update_line and the code that calls it makes a multiple line,
92 automatically wrapping line update. Careful attention needs
93 to be paid to the vertical position variables. */
95 /* Keep two buffers; one which reflects the current contents of the
96 screen, and the other to draw what we think the new contents should
97 be. Then compare the buffers, and make whatever changes to the
98 screen itself that we should. Finally, make the buffer that we
99 just drew into be the one which reflects the current contents of the
100 screen, and place the cursor where it belongs.
102 Commands that want to can fix the display themselves, and then let
103 this function know that the display has been fixed by setting the
104 RL_DISPLAY_FIXED variable. This is good for efficiency. */
106 /* Application-specific redisplay function. */
107 VFunction *rl_redisplay_function = rl_redisplay;
109 /* Global variables declared here. */
110 /* What YOU turn on when you have handled all redisplay yourself. */
111 int rl_display_fixed = 0;
113 int _rl_suppress_redisplay = 0;
115 /* The stuff that gets printed out before the actual text of the line.
116 This is usually pointing to rl_prompt. */
117 char *rl_display_prompt = (char *)NULL;
119 /* Pseudo-global variables declared here. */
120 /* The visible cursor position. If you print some text, adjust this. */
121 int _rl_last_c_pos = 0;
122 int _rl_last_v_pos = 0;
124 /* Number of lines currently on screen minus 1. */
125 int _rl_vis_botlin = 0;
127 /* Variables used only in this file. */
128 /* The last left edge of text that was displayed. This is used when
129 doing horizontal scrolling. It shifts in thirds of a screenwidth. */
130 static int last_lmargin;
132 /* The line display buffers. One is the line currently displayed on
133 the screen. The other is the line about to be displayed. */
134 static char *visible_line = (char *)NULL;
135 static char *invisible_line = (char *)NULL;
137 /* A buffer for `modeline' messages. */
138 static char msg_buf[128];
140 /* Non-zero forces the redisplay even if we thought it was unnecessary. */
141 static int forced_display;
143 /* Default and initial buffer size. Can grow. */
144 static int line_size = 1024;
146 static char *local_prompt, *local_prompt_prefix;
147 static int visible_length, prefix_length;
149 /* The number of invisible characters in the line currently being
150 displayed on the screen. */
151 static int visible_wrap_offset;
153 /* static so it can be shared between rl_redisplay and update_line */
154 static int wrap_offset;
156 /* The index of the last invisible_character in the prompt string. */
157 static int last_invisible;
159 /* The length (buffer offset) of the first line of the last (possibly
160 multi-line) buffer displayed on the screen. */
161 static int visible_first_line_len;
163 /* Expand the prompt string S and return the number of visible
164 characters in *LP, if LP is not null. This is currently more-or-less
165 a placeholder for expansion. LIP, if non-null is a place to store the
166 index of the last invisible character in the returned string. */
168 /* Current implementation:
169 \001 (^A) start non-visible characters
170 \002 (^B) end non-visible characters
171 all characters except \001 and \002 (following a \001) are copied to
172 the returned string; all characters except those between \001 and
173 \002 are assumed to be `visible'. */
176 expand_prompt (pmt, lp, lip)
181 int l, rl, last, ignoring;
183 /* Short-circuit if we can. */
184 if (strchr (pmt, RL_PROMPT_START_IGNORE) == 0)
186 r = savestring (pmt);
193 r = ret = xmalloc (l + 1);
195 for (rl = ignoring = last = 0, p = pmt; p && *p; p++)
197 /* This code strips the invisible character string markers
198 RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE */
199 if (*p == RL_PROMPT_START_IGNORE)
204 else if (ignoring && *p == RL_PROMPT_END_IGNORE)
226 /* Just strip out RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE from
227 PMT and return the rest of PMT. */
229 _rl_strip_prompt (pmt)
234 ret = expand_prompt (pmt, (int *)NULL, (int *)NULL);
239 * Expand the prompt string into the various display components, if
242 * local_prompt = expanded last line of string in rl_display_prompt
243 * (portion after the final newline)
244 * local_prompt_prefix = portion before last newline of rl_display_prompt,
245 * expanded via expand_prompt
246 * visible_length = number of visible characters in local_prompt
247 * prefix_length = number of visible characters in local_prompt_prefix
249 * This function is called once per call to readline(). It may also be
250 * called arbitrarily to expand the primary prompt.
252 * The return value is the number of visible characters on the last line
253 * of the (possibly multi-line) prompt.
256 rl_expand_prompt (prompt)
262 /* Clear out any saved values. */
265 if (local_prompt_prefix)
266 free (local_prompt_prefix);
267 local_prompt = local_prompt_prefix = (char *)0;
268 last_invisible = visible_length = 0;
270 if (prompt == 0 || *prompt == 0)
273 p = strrchr (prompt, '\n');
276 /* The prompt is only one line. */
277 local_prompt = expand_prompt (prompt, &visible_length, &last_invisible);
278 local_prompt_prefix = (char *)0;
279 return (visible_length);
283 /* The prompt spans multiple lines. */
285 local_prompt = expand_prompt (p, &visible_length, &last_invisible);
287 /* The portion of the prompt string up to and including the
288 final newline is now null-terminated. */
289 local_prompt_prefix = expand_prompt (prompt, &prefix_length, (int *)NULL);
291 return (prefix_length);
295 /* Initialize the VISIBLE_LINE and INVISIBLE_LINE arrays, and their associated
296 arrays of line break markers. MINSIZE is the minimum size of VISIBLE_LINE
297 and INVISIBLE_LINE; if it is greater than LINE_SIZE, LINE_SIZE is
298 increased. If the lines have already been allocated, this ensures that
299 they can hold at least MINSIZE characters. */
301 init_line_structures (minsize)
306 if (invisible_line == 0) /* initialize it */
308 if (line_size < minsize)
310 visible_line = xmalloc (line_size);
311 invisible_line = xmalloc (line_size);
313 else if (line_size < minsize) /* ensure it can hold MINSIZE chars */
316 if (line_size < minsize)
318 visible_line = xrealloc (visible_line, line_size);
319 invisible_line = xrealloc (invisible_line, line_size);
322 for (n = minsize; n < line_size; n++)
325 invisible_line[n] = 1;
328 if (vis_lbreaks == 0)
330 /* should be enough. */
331 inv_lbsize = vis_lbsize = 256;
332 inv_lbreaks = (int *)xmalloc (inv_lbsize * sizeof (int));
333 vis_lbreaks = (int *)xmalloc (vis_lbsize * sizeof (int));
334 inv_lbreaks[0] = vis_lbreaks[0] = 0;
338 /* Basic redisplay algorithm. */
342 register int in, out, c, linenum, cursor_linenum;
344 int c_pos, inv_botlin, lb_botlin, lb_linenum;
345 int newlines, lpos, temp;
346 char *prompt_this_line;
348 if (!readline_echoing_p)
351 if (!rl_display_prompt)
352 rl_display_prompt = "";
354 if (invisible_line == 0)
356 init_line_structures (0);
360 /* Draw the line into the buffer. */
363 line = invisible_line;
364 out = inv_botlin = 0;
366 /* Mark the line as modified or not. We only do this for history
368 if (_rl_mark_modified_lines && current_history () && rl_undo_list)
374 /* If someone thought that the redisplay was handled, but the currently
375 visible line has a different modification state than the one about
376 to become visible, then correct the caller's misconception. */
377 if (visible_line[0] != invisible_line[0])
378 rl_display_fixed = 0;
380 /* If the prompt to be displayed is the `primary' readline prompt (the
381 one passed to readline()), use the values we have already expanded.
382 If not, use what's already in rl_display_prompt. WRAP_OFFSET is the
383 number of non-visible characters in the prompt string. */
384 if (rl_display_prompt == rl_prompt || local_prompt)
386 int local_len = local_prompt ? strlen (local_prompt) : 0;
387 if (local_prompt_prefix && forced_display)
388 _rl_output_some_chars (local_prompt_prefix, strlen (local_prompt_prefix));
392 temp = local_len + out + 2;
393 if (temp >= line_size)
395 line_size = (temp + 1024) - (temp % 1024);
396 visible_line = xrealloc (visible_line, line_size);
397 line = invisible_line = xrealloc (invisible_line, line_size);
399 strncpy (line + out, local_prompt, local_len);
403 wrap_offset = local_len - visible_length;
408 prompt_this_line = strrchr (rl_display_prompt, '\n');
409 if (!prompt_this_line)
410 prompt_this_line = rl_display_prompt;
414 pmtlen = prompt_this_line - rl_display_prompt; /* temp var */
417 _rl_output_some_chars (rl_display_prompt, pmtlen);
418 /* Make sure we are at column zero even after a newline,
419 regardless of the state of terminal output processing. */
420 if (pmtlen < 2 || prompt_this_line[-2] != '\r')
425 pmtlen = strlen (prompt_this_line);
426 temp = pmtlen + out + 2;
427 if (temp >= line_size)
429 line_size = (temp + 1024) - (temp % 1024);
430 visible_line = xrealloc (visible_line, line_size);
431 line = invisible_line = xrealloc (invisible_line, line_size);
433 strncpy (line + out, prompt_this_line, pmtlen);
439 #define CHECK_INV_LBREAKS() \
441 if (newlines >= (inv_lbsize - 2)) \
444 inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
448 #define CHECK_LPOS() \
451 if (lpos >= screenwidth) \
453 if (newlines >= (inv_lbsize - 2)) \
456 inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
458 inv_lbreaks[++newlines] = out; \
463 /* inv_lbreaks[i] is where line i starts in the buffer. */
464 inv_lbreaks[newlines = 0] = 0;
465 lpos = out - wrap_offset;
467 /* XXX - what if lpos is already >= screenwidth before we start drawing the
468 contents of the command line? */
469 while (lpos >= screenwidth)
471 /* XXX - possible fix from Darin Johnson <darin@acuson.com> for prompt
472 string with invisible characters that is longer than the screen
473 width. XXX - this doesn't work right if invisible characters have
474 to be put on the second screen line -- it adds too much (the number
475 of invisible chars after the screenwidth). */
476 temp = ((newlines + 1) * screenwidth) + ((newlines == 0) ? wrap_offset : 0);
478 inv_lbreaks[++newlines] = temp;
483 for (in = 0; in < rl_end; in++)
485 c = (unsigned char)rl_line_buffer[in];
487 if (out + 8 >= line_size) /* XXX - 8 for \t */
490 visible_line = xrealloc (visible_line, line_size);
491 invisible_line = xrealloc (invisible_line, line_size);
492 line = invisible_line;
498 lb_linenum = newlines;
503 if (_rl_output_meta_chars == 0)
505 sprintf (line + out, "\\%o", c);
507 if (lpos + 4 >= screenwidth)
509 temp = screenwidth - lpos;
510 CHECK_INV_LBREAKS ();
511 inv_lbreaks[++newlines] = out + temp;
525 #if defined (DISPLAY_TABS)
528 register int temp, newout;
531 newout = (out | (int)7) + 1;
533 newout = out + 8 - lpos % 8;
536 if (lpos + temp >= screenwidth)
539 temp2 = screenwidth - lpos;
540 CHECK_INV_LBREAKS ();
541 inv_lbreaks[++newlines] = out + temp2;
554 else if (c == '\n' && _rl_horizontal_scroll_mode == 0 && term_up && *term_up)
556 line[out++] = '\0'; /* XXX - sentinel */
557 CHECK_INV_LBREAKS ();
558 inv_lbreaks[++newlines] = out;
561 else if (CTRL_CHAR (c) || c == RUBOUT)
565 line[out++] = CTRL_CHAR (c) ? UNCTRL (c) : '?';
578 lb_linenum = newlines;
581 inv_botlin = lb_botlin = newlines;
582 CHECK_INV_LBREAKS ();
583 inv_lbreaks[newlines+1] = out;
584 cursor_linenum = lb_linenum;
586 /* C_POS == position in buffer where cursor should be placed. */
588 /* PWP: now is when things get a bit hairy. The visible and invisible
589 line buffers are really multiple lines, which would wrap every
590 (screenwidth - 1) characters. Go through each in turn, finding
591 the changed region and updating it. The line order is top to bottom. */
593 /* If we can move the cursor up and down, then use multiple lines,
594 otherwise, let long lines display in a single terminal line, and
595 horizontally scroll it. */
597 if (_rl_horizontal_scroll_mode == 0 && term_up && *term_up)
599 int nleft, pos, changed_screen_line;
601 if (!rl_display_fixed || forced_display)
605 /* If we have more than a screenful of material to display, then
606 only display a screenful. We should display the last screen,
608 if (out >= screenchars)
609 out = screenchars - 1;
611 /* The first line is at character position 0 in the buffer. The
612 second and subsequent lines start at inv_lbreaks[N], offset by
613 OFFSET (which has already been calculated above). */
615 #define W_OFFSET(line, offset) ((line) == 0 ? offset : 0)
616 #define VIS_LLEN(l) ((l) > _rl_vis_botlin ? 0 : (vis_lbreaks[l+1] - vis_lbreaks[l]))
617 #define INV_LLEN(l) (inv_lbreaks[l+1] - inv_lbreaks[l])
618 #define VIS_CHARS(line) (visible_line + vis_lbreaks[line])
619 #define VIS_LINE(line) ((line) > _rl_vis_botlin) ? "" : VIS_CHARS(line)
620 #define INV_LINE(line) (invisible_line + inv_lbreaks[line])
622 /* For each line in the buffer, do the updating display. */
623 for (linenum = 0; linenum <= inv_botlin; linenum++)
625 update_line (VIS_LINE(linenum), INV_LINE(linenum), linenum,
626 VIS_LLEN(linenum), INV_LLEN(linenum), inv_botlin);
628 /* If this is the line with the prompt, we might need to
629 compensate for invisible characters in the new line. Do
630 this only if there is not more than one new line (which
631 implies that we completely overwrite the old visible line)
632 and the new line is shorter than the old. Make sure we are
633 at the end of the new line before clearing. */
635 inv_botlin == 0 && _rl_last_c_pos == out &&
636 (wrap_offset > visible_wrap_offset) &&
637 (_rl_last_c_pos < visible_first_line_len))
639 nleft = screenwidth + wrap_offset - _rl_last_c_pos;
641 _rl_clear_to_eol (nleft);
644 /* Since the new first line is now visible, save its length. */
646 visible_first_line_len = (inv_botlin > 0) ? inv_lbreaks[1] : out - wrap_offset;
649 /* We may have deleted some lines. If so, clear the left over
650 blank ones at the bottom out. */
651 if (_rl_vis_botlin > inv_botlin)
654 for (; linenum <= _rl_vis_botlin; linenum++)
656 tt = VIS_CHARS (linenum);
657 _rl_move_vert (linenum);
658 _rl_move_cursor_relative (0, tt);
660 ((linenum == _rl_vis_botlin) ? strlen (tt) : screenwidth);
663 _rl_vis_botlin = inv_botlin;
665 /* CHANGED_SCREEN_LINE is set to 1 if we have moved to a
666 different screen line during this redisplay. */
667 changed_screen_line = _rl_last_v_pos != cursor_linenum;
668 if (changed_screen_line)
670 _rl_move_vert (cursor_linenum);
671 /* If we moved up to the line with the prompt using term_up,
672 the physical cursor position on the screen stays the same,
673 but the buffer position needs to be adjusted to account
674 for invisible characters. */
675 if (cursor_linenum == 0 && wrap_offset)
676 _rl_last_c_pos += wrap_offset;
679 /* We have to reprint the prompt if it contains invisible
680 characters, since it's not generally OK to just reprint
681 the characters from the current cursor position. But we
682 only need to reprint it if the cursor is before the last
683 invisible character in the prompt string. */
684 nleft = visible_length + wrap_offset;
685 if (cursor_linenum == 0 && wrap_offset > 0 && _rl_last_c_pos > 0 &&
686 _rl_last_c_pos <= last_invisible && local_prompt)
688 #if defined (__MSDOS__)
689 putc ('\r', rl_outstream);
692 tputs (term_cr, 1, _rl_output_character_function);
694 _rl_output_some_chars (local_prompt, nleft);
695 _rl_last_c_pos = nleft;
698 /* Where on that line? And where does that line start
700 pos = inv_lbreaks[cursor_linenum];
701 /* nleft == number of characters in the line buffer between the
702 start of the line and the cursor position. */
705 /* Since _rl_backspace() doesn't know about invisible characters in the
706 prompt, and there's no good way to tell it, we compensate for
707 those characters here and call _rl_backspace() directly. */
708 if (wrap_offset && cursor_linenum == 0 && nleft < _rl_last_c_pos)
710 _rl_backspace (_rl_last_c_pos - nleft);
711 _rl_last_c_pos = nleft;
714 if (nleft != _rl_last_c_pos)
715 _rl_move_cursor_relative (nleft, &invisible_line[pos]);
718 else /* Do horizontal scrolling. */
720 #define M_OFFSET(margin, offset) ((margin) == 0 ? offset : 0)
721 int lmargin, ndisp, nleft, phys_c_pos, t;
723 /* Always at top line. */
726 /* Compute where in the buffer the displayed line should start. This
729 /* The number of characters that will be displayed before the cursor. */
730 ndisp = c_pos - wrap_offset;
731 nleft = visible_length + wrap_offset;
732 /* Where the new cursor position will be on the screen. This can be
733 longer than SCREENWIDTH; if it is, lmargin will be adjusted. */
734 phys_c_pos = c_pos - (last_lmargin ? last_lmargin : wrap_offset);
737 /* If the number of characters had already exceeded the screenwidth,
738 last_lmargin will be > 0. */
740 /* If the number of characters to be displayed is more than the screen
741 width, compute the starting offset so that the cursor is about
742 two-thirds of the way across the screen. */
743 if (phys_c_pos > screenwidth - 2)
745 lmargin = c_pos - (2 * t);
748 /* If the left margin would be in the middle of a prompt with
749 invisible characters, don't display the prompt at all. */
750 if (wrap_offset && lmargin > 0 && lmargin < nleft)
753 else if (ndisp < screenwidth - 2) /* XXX - was -1 */
755 else if (phys_c_pos < 1)
757 /* If we are moving back towards the beginning of the line and
758 the last margin is no longer correct, compute a new one. */
759 lmargin = ((c_pos - 1) / t) * t; /* XXX */
760 if (wrap_offset && lmargin > 0 && lmargin < nleft)
764 lmargin = last_lmargin;
766 /* If the first character on the screen isn't the first character
767 in the display line, indicate this with a special character. */
771 /* If SCREENWIDTH characters starting at LMARGIN do not encompass
772 the whole line, indicate that with a special character at the
773 right edge of the screen. If LMARGIN is 0, we need to take the
774 wrap offset into account. */
775 t = lmargin + M_OFFSET (lmargin, wrap_offset) + screenwidth;
779 if (!rl_display_fixed || forced_display || lmargin != last_lmargin)
782 update_line (&visible_line[last_lmargin],
783 &invisible_line[lmargin],
785 screenwidth + visible_wrap_offset,
786 screenwidth + (lmargin ? 0 : wrap_offset),
789 /* If the visible new line is shorter than the old, but the number
790 of invisible characters is greater, and we are at the end of
791 the new line, we need to clear to eol. */
792 t = _rl_last_c_pos - M_OFFSET (lmargin, wrap_offset);
793 if ((M_OFFSET (lmargin, wrap_offset) > visible_wrap_offset) &&
794 (_rl_last_c_pos == out) &&
795 t < visible_first_line_len)
797 nleft = screenwidth - t;
798 _rl_clear_to_eol (nleft);
800 visible_first_line_len = out - lmargin - M_OFFSET (lmargin, wrap_offset);
801 if (visible_first_line_len > screenwidth)
802 visible_first_line_len = screenwidth;
804 _rl_move_cursor_relative (c_pos - lmargin, &invisible_line[lmargin]);
805 last_lmargin = lmargin;
808 fflush (rl_outstream);
810 /* Swap visible and non-visible lines. */
812 char *temp = visible_line;
813 int *itemp = vis_lbreaks, ntemp = vis_lbsize;
815 visible_line = invisible_line;
816 invisible_line = temp;
818 vis_lbreaks = inv_lbreaks;
821 vis_lbsize = inv_lbsize;
824 rl_display_fixed = 0;
825 /* If we are displaying on a single line, and last_lmargin is > 0, we
826 are not displaying any invisible characters, so set visible_wrap_offset
828 if (_rl_horizontal_scroll_mode && last_lmargin)
829 visible_wrap_offset = 0;
831 visible_wrap_offset = wrap_offset;
835 /* PWP: update_line() is based on finding the middle difference of each
836 line on the screen; vis:
838 /old first difference
839 /beginning of line | /old last same /old EOL
841 old: eddie> Oh, my little gruntle-buggy is to me, as lurgid as
842 new: eddie> Oh, my little buggy says to me, as lurgid as
844 \beginning of line | \new last same \new end of line
845 \new first difference
847 All are character pointers for the sake of speed. Special cases for
848 no differences, as well as for end of line additions must be handled.
850 Could be made even smarter, but this works well enough */
852 update_line (old, new, current_line, omax, nmax, inv_botlin)
853 register char *old, *new;
854 int current_line, omax, nmax, inv_botlin;
856 register char *ofd, *ols, *oe, *nfd, *nls, *ne;
857 int temp, lendiff, wsatend, od, nd;
858 int current_invis_chars;
860 /* If we're at the right edge of a terminal that supports xn, we're
861 ready to wrap around, so do so. This fixes problems with knowing
862 the exact cursor position and cut-and-paste with certain terminal
863 emulators. In this calculation, TEMP is the physical screen
864 position of the cursor. */
865 temp = _rl_last_c_pos - W_OFFSET(_rl_last_v_pos, visible_wrap_offset);
866 if (temp == screenwidth && _rl_term_autowrap && !_rl_horizontal_scroll_mode
867 && _rl_last_v_pos == current_line - 1)
870 putc (new[0], rl_outstream);
872 putc (' ', rl_outstream);
873 _rl_last_c_pos = 1; /* XXX */
875 if (old[0] && new[0])
879 /* Find first difference. */
880 for (ofd = old, nfd = new;
881 (ofd - old < omax) && *ofd && (*ofd == *nfd);
885 /* Move to the end of the screen line. ND and OD are used to keep track
886 of the distance between ne and new and oe and old, respectively, to
887 move a subtraction out of each loop. */
888 for (od = ofd - old, oe = ofd; od < omax && *oe; oe++, od++);
889 for (nd = nfd - new, ne = nfd; nd < nmax && *ne; ne++, nd++);
891 /* If no difference, continue to next line. */
892 if (ofd == oe && nfd == ne)
895 wsatend = 1; /* flag for trailing whitespace */
896 ols = oe - 1; /* find last same */
898 while ((ols > ofd) && (nls > nfd) && (*ols == *nls))
911 else if (*ols != *nls)
913 if (*ols) /* don't step past the NUL */
919 /* count of invisible characters in the current invisible line. */
920 current_invis_chars = W_OFFSET (current_line, wrap_offset);
921 if (_rl_last_v_pos != current_line)
923 _rl_move_vert (current_line);
924 if (current_line == 0 && visible_wrap_offset)
925 _rl_last_c_pos += visible_wrap_offset;
928 /* If this is the first line and there are invisible characters in the
929 prompt string, and the prompt string has not changed, and the current
930 cursor position is before the last invisible character in the prompt,
931 and the index of the character to move to is past the end of the prompt
932 string, then redraw the entire prompt string. We can only do this
933 reliably if the terminal supports a `cr' capability.
935 This is not an efficiency hack -- there is a problem with redrawing
936 portions of the prompt string if they contain terminal escape
937 sequences (like drawing the `unbold' sequence without a corresponding
938 `bold') that manifests itself on certain terminals. */
940 lendiff = local_prompt ? strlen (local_prompt) : 0;
941 od = ofd - old; /* index of first difference in visible line */
942 if (current_line == 0 && !_rl_horizontal_scroll_mode &&
943 term_cr && lendiff > visible_length && _rl_last_c_pos > 0 &&
944 od > lendiff && _rl_last_c_pos < last_invisible)
946 #if defined (__MSDOS__)
947 putc ('\r', rl_outstream);
949 tputs (term_cr, 1, _rl_output_character_function);
951 _rl_output_some_chars (local_prompt, lendiff);
952 _rl_last_c_pos = lendiff;
955 _rl_move_cursor_relative (od, old);
957 /* if (len (new) > len (old)) */
958 lendiff = (nls - nfd) - (ols - ofd);
960 /* If we are changing the number of invisible characters in a line, and
961 the spot of first difference is before the end of the invisible chars,
962 lendiff needs to be adjusted. */
963 if (current_line == 0 && !_rl_horizontal_scroll_mode &&
964 current_invis_chars != visible_wrap_offset)
965 lendiff += visible_wrap_offset - current_invis_chars;
967 /* Insert (diff (len (old), len (new)) ch. */
971 /* Non-zero if we're increasing the number of lines. */
972 int gl = current_line >= _rl_vis_botlin && inv_botlin > _rl_vis_botlin;
973 /* Sometimes it is cheaper to print the characters rather than
974 use the terminal's capabilities. If we're growing the number
975 of lines, make sure we actually cause the new line to wrap
976 around on auto-wrapping terminals. */
977 if (terminal_can_insert && ((2 * temp) >= lendiff || term_IC) && (!_rl_term_autowrap || !gl))
979 /* If lendiff > visible_length and _rl_last_c_pos == 0 and
980 _rl_horizontal_scroll_mode == 1, inserting the characters with
981 term_IC or term_ic will screw up the screen because of the
982 invisible characters. We need to just draw them. */
983 if (*ols && (!_rl_horizontal_scroll_mode || _rl_last_c_pos > 0 ||
984 lendiff <= visible_length || !current_invis_chars))
986 insert_some_chars (nfd, lendiff);
987 _rl_last_c_pos += lendiff;
991 /* At the end of a line the characters do not have to
992 be "inserted". They can just be placed on the screen. */
993 /* However, this screws up the rest of this block, which
994 assumes you've done the insert because you can. */
995 _rl_output_some_chars (nfd, lendiff);
996 _rl_last_c_pos += lendiff;
1000 /* We have horizontal scrolling and we are not inserting at
1001 the end. We have invisible characters in this line. This
1002 is a dumb update. */
1003 _rl_output_some_chars (nfd, temp);
1004 _rl_last_c_pos += temp;
1007 /* Copy (new) chars to screen from first diff to last match. */
1009 if ((temp - lendiff) > 0)
1011 _rl_output_some_chars (nfd + lendiff, temp - lendiff);
1012 _rl_last_c_pos += temp - lendiff;
1017 /* cannot insert chars, write to EOL */
1018 _rl_output_some_chars (nfd, temp);
1019 _rl_last_c_pos += temp;
1022 else /* Delete characters from line. */
1024 /* If possible and inexpensive to use terminal deletion, then do so. */
1025 if (term_dc && (2 * temp) >= -lendiff)
1027 /* If all we're doing is erasing the invisible characters in the
1028 prompt string, don't bother. It screws up the assumptions
1029 about what's on the screen. */
1030 if (_rl_horizontal_scroll_mode && _rl_last_c_pos == 0 &&
1031 -lendiff == visible_wrap_offset)
1035 delete_chars (-lendiff); /* delete (diff) characters */
1037 /* Copy (new) chars to screen from first diff to last match */
1041 _rl_output_some_chars (nfd, temp);
1042 _rl_last_c_pos += temp;
1045 /* Otherwise, print over the existing material. */
1050 _rl_output_some_chars (nfd, temp);
1051 _rl_last_c_pos += temp;
1053 lendiff = (oe - old) - (ne - new);
1056 if (_rl_term_autowrap && current_line < inv_botlin)
1057 space_to_eol (lendiff);
1059 _rl_clear_to_eol (lendiff);
1065 /* Tell the update routines that we have moved onto a new (empty) line. */
1070 visible_line[0] = '\0';
1072 _rl_last_c_pos = _rl_last_v_pos = 0;
1073 _rl_vis_botlin = last_lmargin = 0;
1075 vis_lbreaks[0] = vis_lbreaks[1] = 0;
1076 visible_wrap_offset = 0;
1080 /* Tell the update routines that we have moved onto a new line with the
1081 prompt already displayed. Code originally from the version of readline
1082 distributed with CLISP. */
1084 rl_on_new_line_with_prompt ()
1086 int prompt_size, i, l, real_screenwidth, newlines;
1087 char *prompt_last_line;
1089 /* Initialize visible_line and invisible_line to ensure that they can hold
1090 the already-displayed prompt. */
1091 prompt_size = strlen (rl_prompt) + 1;
1092 init_line_structures (prompt_size);
1094 /* Make sure the line structures hold the already-displayed prompt for
1096 strcpy (visible_line, rl_prompt);
1097 strcpy (invisible_line, rl_prompt);
1099 /* If the prompt contains newlines, take the last tail. */
1100 prompt_last_line = strrchr (rl_prompt, '\n');
1101 if (!prompt_last_line)
1102 prompt_last_line = rl_prompt;
1104 l = strlen (prompt_last_line);
1107 /* Dissect prompt_last_line into screen lines. Note that here we have
1108 to use the real screenwidth. Readline's notion of screenwidth might be
1109 one less, see terminal.c. */
1110 real_screenwidth = screenwidth + (_rl_term_autowrap ? 0 : 1);
1111 _rl_last_v_pos = l / real_screenwidth;
1112 /* If the prompt length is a multiple of real_screenwidth, we don't know
1113 whether the cursor is at the end of the last line, or already at the
1114 beginning of the next line. Output a newline just to be safe. */
1115 if (l > 0 && (l % real_screenwidth) == 0)
1116 _rl_output_some_chars ("\n", 1);
1119 newlines = 0; i = 0;
1122 _rl_vis_botlin = newlines;
1123 vis_lbreaks[newlines++] = i;
1124 i += real_screenwidth;
1126 vis_lbreaks[newlines] = l;
1127 visible_wrap_offset = 0;
1132 /* Actually update the display, period. */
1134 rl_forced_update_display ()
1138 register char *temp = visible_line;
1145 (*rl_redisplay_function) ();
1149 /* Move the cursor from _rl_last_c_pos to NEW, which are buffer indices.
1150 DATA is the contents of the screen line of interest; i.e., where
1151 the movement is being done. */
1153 _rl_move_cursor_relative (new, data)
1159 /* If we don't have to do anything, then return. */
1160 if (_rl_last_c_pos == new) return;
1162 /* It may be faster to output a CR, and then move forwards instead
1163 of moving backwards. */
1164 /* i == current physical cursor position. */
1165 i = _rl_last_c_pos - W_OFFSET(_rl_last_v_pos, visible_wrap_offset);
1166 if (new == 0 || CR_FASTER (new, _rl_last_c_pos) ||
1167 (_rl_term_autowrap && i == screenwidth))
1169 #if defined (__MSDOS__)
1170 putc ('\r', rl_outstream);
1172 tputs (term_cr, 1, _rl_output_character_function);
1173 #endif /* !__MSDOS__ */
1177 if (_rl_last_c_pos < new)
1179 /* Move the cursor forward. We do it by printing the command
1180 to move the cursor forward if there is one, else print that
1181 portion of the output buffer again. Which is cheaper? */
1183 /* The above comment is left here for posterity. It is faster
1184 to print one character (non-control) than to print a control
1185 sequence telling the terminal to move forward one character.
1186 That kind of control is for people who don't know what the
1187 data is underneath the cursor. */
1188 #if defined (HACK_TERMCAP_MOTION)
1189 if (term_forward_char)
1190 for (i = _rl_last_c_pos; i < new; i++)
1191 tputs (term_forward_char, 1, _rl_output_character_function);
1193 for (i = _rl_last_c_pos; i < new; i++)
1194 putc (data[i], rl_outstream);
1196 for (i = _rl_last_c_pos; i < new; i++)
1197 putc (data[i], rl_outstream);
1198 #endif /* HACK_TERMCAP_MOTION */
1200 else if (_rl_last_c_pos > new)
1201 _rl_backspace (_rl_last_c_pos - new);
1202 _rl_last_c_pos = new;
1205 /* PWP: move the cursor up or down. */
1210 register int delta, i;
1212 if (_rl_last_v_pos == to || to > screenheight)
1215 if ((delta = to - _rl_last_v_pos) > 0)
1217 for (i = 0; i < delta; i++)
1218 putc ('\n', rl_outstream);
1219 #if defined (__MSDOS__)
1220 putc ('\r', rl_outstream);
1222 tputs (term_cr, 1, _rl_output_character_function);
1228 if (term_up && *term_up)
1229 for (i = 0; i < -delta; i++)
1230 tputs (term_up, 1, _rl_output_character_function);
1233 _rl_last_v_pos = to; /* Now TO is here */
1236 /* Physically print C on rl_outstream. This is for functions which know
1237 how to optimize the display. Return the number of characters output. */
1243 if (META_CHAR (c) && (_rl_output_meta_chars == 0))
1245 fprintf (rl_outstream, "M-");
1250 #if defined (DISPLAY_TABS)
1251 if ((CTRL_CHAR (c) && c != '\t') || c == RUBOUT)
1253 if (CTRL_CHAR (c) || c == RUBOUT)
1254 #endif /* !DISPLAY_TABS */
1256 fprintf (rl_outstream, "C-");
1258 c = CTRL_CHAR (c) ? UNCTRL (c) : '?';
1261 putc (c, rl_outstream);
1262 fflush (rl_outstream);
1267 rl_character_len (c, pos)
1268 register int c, pos;
1272 uc = (unsigned char)c;
1275 return ((_rl_output_meta_chars == 0) ? 4 : 1);
1279 #if defined (DISPLAY_TABS)
1280 return (((pos | 7) + 1) - pos);
1283 #endif /* !DISPLAY_TABS */
1286 if (CTRL_CHAR (c) || c == RUBOUT)
1289 return ((isprint (uc)) ? 1 : 2);
1292 /* How to print things in the "echo-area". The prompt is treated as a
1295 #if defined (USE_VARARGS)
1297 #if defined (PREFER_STDARG)
1298 rl_message (const char *format, ...)
1300 rl_message (va_alist)
1305 #if defined (PREFER_VARARGS)
1309 #if defined (PREFER_STDARG)
1310 va_start (args, format);
1313 format = va_arg (args, char *);
1316 vsprintf (msg_buf, format, args);
1319 rl_display_prompt = msg_buf;
1320 (*rl_redisplay_function) ();
1323 #else /* !USE_VARARGS */
1325 rl_message (format, arg1, arg2)
1328 sprintf (msg_buf, format, arg1, arg2);
1329 rl_display_prompt = msg_buf;
1330 (*rl_redisplay_function) ();
1333 #endif /* !USE_VARARGS */
1335 /* How to clear things from the "echo-area". */
1339 rl_display_prompt = rl_prompt;
1340 (*rl_redisplay_function) ();
1345 rl_reset_line_state ()
1349 rl_display_prompt = rl_prompt ? rl_prompt : "";
1354 static char *saved_local_prompt;
1355 static char *saved_local_prefix;
1356 static int saved_last_invisible;
1357 static int saved_visible_length;
1362 saved_local_prompt = local_prompt;
1363 saved_local_prefix = local_prompt_prefix;
1364 saved_last_invisible = last_invisible;
1365 saved_visible_length = visible_length;
1367 local_prompt = local_prompt_prefix = (char *)0;
1368 last_invisible = visible_length = 0;
1372 rl_restore_prompt ()
1375 free (local_prompt);
1376 if (local_prompt_prefix)
1377 free (local_prompt_prefix);
1379 local_prompt = saved_local_prompt;
1380 local_prompt_prefix = saved_local_prefix;
1381 last_invisible = saved_last_invisible;
1382 visible_length = saved_visible_length;
1386 _rl_make_prompt_for_search (pchar)
1394 if (saved_local_prompt == 0)
1396 len = (rl_prompt && *rl_prompt) ? strlen (rl_prompt) : 0;
1397 pmt = xmalloc (len + 2);
1399 strcpy (pmt, rl_prompt);
1405 len = *saved_local_prompt ? strlen (saved_local_prompt) : 0;
1406 pmt = xmalloc (len + 2);
1408 strcpy (pmt, saved_local_prompt);
1411 local_prompt = savestring (pmt);
1412 last_invisible = saved_last_invisible;
1413 visible_length = saved_visible_length + 1;
1418 /* Quick redisplay hack when erasing characters at the end of the line. */
1420 _rl_erase_at_end_of_line (l)
1426 for (i = 0; i < l; i++)
1427 putc (' ', rl_outstream);
1429 for (i = 0; i < l; i++)
1430 visible_line[--_rl_last_c_pos] = '\0';
1434 /* Clear to the end of the line. COUNT is the minimum
1435 number of character spaces to clear, */
1437 _rl_clear_to_eol (count)
1441 tputs (term_clreol, 1, _rl_output_character_function);
1443 space_to_eol (count);
1446 /* Clear to the end of the line using spaces. COUNT is the minimum
1447 number of character spaces to clear, */
1449 space_to_eol (count)
1454 for (i = 0; i < count; i++)
1455 putc (' ', rl_outstream);
1457 _rl_last_c_pos += count;
1464 tputs (term_clrpag, 1, _rl_output_character_function);
1469 /* Insert COUNT characters from STRING to the output stream. */
1471 insert_some_chars (string, count)
1475 /* If IC is defined, then we do not have to "enter" insert mode. */
1479 buffer = tgoto (term_IC, 0, count);
1480 tputs (buffer, 1, _rl_output_character_function);
1481 _rl_output_some_chars (string, count);
1487 /* If we have to turn on insert-mode, then do so. */
1488 if (term_im && *term_im)
1489 tputs (term_im, 1, _rl_output_character_function);
1491 /* If there is a special command for inserting characters, then
1492 use that first to open up the space. */
1493 if (term_ic && *term_ic)
1495 for (i = count; i--; )
1496 tputs (term_ic, 1, _rl_output_character_function);
1499 /* Print the text. */
1500 _rl_output_some_chars (string, count);
1502 /* If there is a string to turn off insert mode, we had best use
1504 if (term_ei && *term_ei)
1505 tputs (term_ei, 1, _rl_output_character_function);
1509 /* Delete COUNT characters from the display line. */
1511 delete_chars (count)
1514 if (count > screenwidth) /* XXX */
1517 if (term_DC && *term_DC)
1520 buffer = tgoto (term_DC, count, count);
1521 tputs (buffer, count, _rl_output_character_function);
1525 if (term_dc && *term_dc)
1527 tputs (term_dc, 1, _rl_output_character_function);
1537 /* If the cursor is the only thing on an otherwise-blank last line,
1538 compensate so we don't print an extra CRLF. */
1539 if (_rl_vis_botlin && _rl_last_c_pos == 0 &&
1540 visible_line[vis_lbreaks[_rl_vis_botlin]] == 0)
1545 _rl_move_vert (_rl_vis_botlin);
1546 /* If we've wrapped lines, remove the final xterm line-wrap flag. */
1547 if (full_lines && _rl_term_autowrap && (VIS_LLEN(_rl_vis_botlin) == screenwidth))
1551 last_line = &visible_line[inv_lbreaks[_rl_vis_botlin]];
1553 last_line = &visible_line[vis_lbreaks[_rl_vis_botlin]];
1555 _rl_move_cursor_relative (screenwidth - 1, last_line);
1556 _rl_clear_to_eol (0);
1557 putc (last_line[screenwidth - 1], rl_outstream);
1561 fflush (rl_outstream);
1565 /* Move to the start of the current line. */
1571 #if defined (__MSDOS__)
1572 putc ('\r', rl_outstream);
1574 tputs (term_cr, 1, _rl_output_character_function);
1580 /* Redraw the last line of a multi-line prompt that may possibly contain
1581 terminal escape sequences. Called with the cursor at column 0 of the
1582 line to draw the prompt on. */
1587 char *oldp, *oldl, *oldlprefix;
1588 int oldlen, oldlast, oldplen;
1590 /* Geez, I should make this a struct. */
1591 oldp = rl_display_prompt;
1592 oldl = local_prompt;
1593 oldlprefix = local_prompt_prefix;
1594 oldlen = visible_length;
1595 oldplen = prefix_length;
1596 oldlast = last_invisible;
1598 rl_display_prompt = t;
1599 local_prompt = expand_prompt (t, &visible_length, &last_invisible);
1600 local_prompt_prefix = (char *)NULL;
1601 rl_forced_update_display ();
1603 rl_display_prompt = oldp;
1604 local_prompt = oldl;
1605 local_prompt_prefix = oldlprefix;
1606 visible_length = oldlen;
1607 prefix_length = oldplen;
1608 last_invisible = oldlast;
1611 /* Redisplay the current line after a SIGWINCH is received. */
1613 _rl_redisplay_after_sigwinch ()
1617 /* Clear the current line and put the cursor at column 0. Make sure
1618 the right thing happens if we have wrapped to a new screen line. */
1621 #if defined (__MSDOS__)
1622 putc ('\r', rl_outstream);
1624 tputs (term_cr, 1, _rl_output_character_function);
1627 #if defined (__MSDOS__)
1628 space_to_eol (screenwidth);
1629 putc ('\r', rl_outstream);
1632 tputs (term_clreol, 1, _rl_output_character_function);
1635 space_to_eol (screenwidth);
1636 tputs (term_cr, 1, _rl_output_character_function);
1639 if (_rl_last_v_pos > 0)
1645 /* Redraw only the last line of a multi-line prompt. */
1646 t = strrchr (rl_display_prompt, '\n');
1648 redraw_prompt (++t);
1650 rl_forced_update_display ();
1654 _rl_clean_up_for_exit ()
1656 if (readline_echoing_p)
1658 _rl_move_vert (_rl_vis_botlin);
1660 fflush (rl_outstream);
1661 rl_restart_output (1, 0);
1666 _rl_erase_entire_line ()
1669 _rl_clear_to_eol (0);
1671 fflush (rl_outstream);
1674 /* return the `current display line' of the cursor -- the number of lines to
1675 move up to get to the first screen line of the current readline line. */
1677 _rl_current_display_line ()
1681 /* Find out whether or not there might be invisible characters in the
1683 if (rl_display_prompt == rl_prompt)
1684 nleft = _rl_last_c_pos - screenwidth - rl_visible_prompt_length;
1686 nleft = _rl_last_c_pos - screenwidth;
1689 ret = 1 + nleft / screenwidth;