1 /* display.c -- readline redisplay facility. */
3 /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
5 This file is part of the GNU Readline Library, a library for
6 reading lines of text with interactive input and history editing.
8 The GNU Readline Library is free software; you can redistribute it
9 and/or modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2, or
11 (at your option) any later version.
13 The GNU Readline Library is distributed in the hope that it will be
14 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 The GNU General Public License is often shipped with GNU software, and
19 is generally kept in a file called COPYING or LICENSE. If you do not
20 have a copy of the license, write to the Free Software Foundation,
21 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
22 #define READLINE_LIBRARY
24 #if defined (HAVE_CONFIG_H)
28 #include <sys/types.h>
30 #if defined (HAVE_UNISTD_H)
32 #endif /* HAVE_UNISTD_H */
34 #include "posixstat.h"
36 #if defined (HAVE_STDLIB_H)
39 # include "ansi_stdlib.h"
40 #endif /* HAVE_STDLIB_H */
44 /* System-specific feature definitions and include files. */
47 /* Termcap library stuff. */
50 /* Some standard library routines. */
54 #include "rlprivate.h"
57 #if !defined (strchr) && !defined (__STDC__)
58 extern char *strchr (), *strrchr ();
59 #endif /* !strchr && !__STDC__ */
61 #if defined (HACK_TERMCAP_MOTION)
62 extern char *term_forward_char;
65 static void update_line __P((char *, char *, int, int, int, int));
66 static void space_to_eol __P((int));
67 static void delete_chars __P((int));
68 static void insert_some_chars __P((char *, int));
69 static void cr __P((void));
71 static int *inv_lbreaks, *vis_lbreaks;
72 static int inv_lbsize, vis_lbsize;
74 /* Heuristic used to decide whether it is faster to move from CUR to NEW
75 by backing up or outputting a carriage return and moving forward. */
76 #define CR_FASTER(new, cur) (((new) + 1) < ((cur) - (new)))
78 /* **************************************************************** */
82 /* **************************************************************** */
84 /* This is the stuff that is hard for me. I never seem to write good
85 display routines in C. Let's see how I do this time. */
87 /* (PWP) Well... Good for a simple line updater, but totally ignores
88 the problems of input lines longer than the screen width.
90 update_line and the code that calls it makes a multiple line,
91 automatically wrapping line update. Careful attention needs
92 to be paid to the vertical position variables. */
94 /* Keep two buffers; one which reflects the current contents of the
95 screen, and the other to draw what we think the new contents should
96 be. Then compare the buffers, and make whatever changes to the
97 screen itself that we should. Finally, make the buffer that we
98 just drew into be the one which reflects the current contents of the
99 screen, and place the cursor where it belongs.
101 Commands that want to can fix the display themselves, and then let
102 this function know that the display has been fixed by setting the
103 RL_DISPLAY_FIXED variable. This is good for efficiency. */
105 /* Application-specific redisplay function. */
106 VFunction *rl_redisplay_function = rl_redisplay;
108 /* Global variables declared here. */
109 /* What YOU turn on when you have handled all redisplay yourself. */
110 int rl_display_fixed = 0;
112 int _rl_suppress_redisplay = 0;
114 /* The stuff that gets printed out before the actual text of the line.
115 This is usually pointing to rl_prompt. */
116 char *rl_display_prompt = (char *)NULL;
118 /* Pseudo-global variables declared here. */
119 /* The visible cursor position. If you print some text, adjust this. */
120 int _rl_last_c_pos = 0;
121 int _rl_last_v_pos = 0;
123 /* Number of lines currently on screen minus 1. */
124 int _rl_vis_botlin = 0;
126 /* Variables used only in this file. */
127 /* The last left edge of text that was displayed. This is used when
128 doing horizontal scrolling. It shifts in thirds of a screenwidth. */
129 static int last_lmargin;
131 /* The line display buffers. One is the line currently displayed on
132 the screen. The other is the line about to be displayed. */
133 static char *visible_line = (char *)NULL;
134 static char *invisible_line = (char *)NULL;
136 /* A buffer for `modeline' messages. */
137 static char msg_buf[128];
139 /* Non-zero forces the redisplay even if we thought it was unnecessary. */
140 static int forced_display;
142 /* Default and initial buffer size. Can grow. */
143 static int line_size = 1024;
145 static char *local_prompt, *local_prompt_prefix;
146 static int visible_length, prefix_length;
148 /* The number of invisible characters in the line currently being
149 displayed on the screen. */
150 static int visible_wrap_offset;
152 /* static so it can be shared between rl_redisplay and update_line */
153 static int wrap_offset;
155 /* The index of the last invisible_character in the prompt string. */
156 static int last_invisible;
158 /* The length (buffer offset) of the first line of the last (possibly
159 multi-line) buffer displayed on the screen. */
160 static int visible_first_line_len;
162 /* Expand the prompt string S and return the number of visible
163 characters in *LP, if LP is not null. This is currently more-or-less
164 a placeholder for expansion. LIP, if non-null is a place to store the
165 index of the last invisible character in the returned string. */
167 /* Current implementation:
168 \001 (^A) start non-visible characters
169 \002 (^B) end non-visible characters
170 all characters except \001 and \002 (following a \001) are copied to
171 the returned string; all characters except those between \001 and
172 \002 are assumed to be `visible'. */
175 expand_prompt (pmt, lp, lip)
180 int l, rl, last, ignoring;
182 /* Short-circuit if we can. */
183 if (strchr (pmt, RL_PROMPT_START_IGNORE) == 0)
185 r = savestring (pmt);
192 r = ret = xmalloc (l + 1);
194 for (rl = ignoring = last = 0, p = pmt; p && *p; p++)
196 /* This code strips the invisible character string markers
197 RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE */
198 if (*p == RL_PROMPT_START_IGNORE)
203 else if (ignoring && *p == RL_PROMPT_END_IGNORE)
225 /* Just strip out RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE from
226 PMT and return the rest of PMT. */
228 _rl_strip_prompt (pmt)
233 ret = expand_prompt (pmt, (int *)NULL, (int *)NULL);
238 * Expand the prompt string into the various display components, if
241 * local_prompt = expanded last line of string in rl_display_prompt
242 * (portion after the final newline)
243 * local_prompt_prefix = portion before last newline of rl_display_prompt,
244 * expanded via expand_prompt
245 * visible_length = number of visible characters in local_prompt
246 * prefix_length = number of visible characters in local_prompt_prefix
248 * This function is called once per call to readline(). It may also be
249 * called arbitrarily to expand the primary prompt.
251 * The return value is the number of visible characters on the last line
252 * of the (possibly multi-line) prompt.
255 rl_expand_prompt (prompt)
261 /* Clear out any saved values. */
264 if (local_prompt_prefix)
265 free (local_prompt_prefix);
266 local_prompt = local_prompt_prefix = (char *)0;
267 last_invisible = visible_length = 0;
269 if (prompt == 0 || *prompt == 0)
272 p = strrchr (prompt, '\n');
275 /* The prompt is only one line. */
276 local_prompt = expand_prompt (prompt, &visible_length, &last_invisible);
277 local_prompt_prefix = (char *)0;
278 return (visible_length);
282 /* The prompt spans multiple lines. */
284 local_prompt = expand_prompt (p, &visible_length, &last_invisible);
286 /* The portion of the prompt string up to and including the
287 final newline is now null-terminated. */
288 local_prompt_prefix = expand_prompt (prompt, &prefix_length, (int *)NULL);
290 return (prefix_length);
294 /* Initialize the VISIBLE_LINE and INVISIBLE_LINE arrays, and their associated
295 arrays of line break markers. MINSIZE is the minimum size of VISIBLE_LINE
296 and INVISIBLE_LINE; if it is greater than LINE_SIZE, LINE_SIZE is
297 increased. If the lines have already been allocated, this ensures that
298 they can hold at least MINSIZE characters. */
300 init_line_structures (minsize)
305 if (invisible_line == 0) /* initialize it */
307 if (line_size < minsize)
309 visible_line = xmalloc (line_size);
310 invisible_line = xmalloc (line_size);
312 else if (line_size < minsize) /* ensure it can hold MINSIZE chars */
315 if (line_size < minsize)
317 visible_line = xrealloc (visible_line, line_size);
318 invisible_line = xrealloc (invisible_line, line_size);
321 for (n = minsize; n < line_size; n++)
324 invisible_line[n] = 1;
327 if (vis_lbreaks == 0)
329 /* should be enough. */
330 inv_lbsize = vis_lbsize = 256;
331 inv_lbreaks = (int *)xmalloc (inv_lbsize * sizeof (int));
332 vis_lbreaks = (int *)xmalloc (vis_lbsize * sizeof (int));
333 inv_lbreaks[0] = vis_lbreaks[0] = 0;
337 /* Basic redisplay algorithm. */
341 register int in, out, c, linenum, cursor_linenum;
343 int c_pos, inv_botlin, lb_botlin, lb_linenum;
344 int newlines, lpos, temp;
345 char *prompt_this_line;
347 if (!readline_echoing_p)
350 if (!rl_display_prompt)
351 rl_display_prompt = "";
353 if (invisible_line == 0)
355 init_line_structures (0);
359 /* Draw the line into the buffer. */
362 line = invisible_line;
363 out = inv_botlin = 0;
365 /* Mark the line as modified or not. We only do this for history
367 if (_rl_mark_modified_lines && current_history () && rl_undo_list)
373 /* If someone thought that the redisplay was handled, but the currently
374 visible line has a different modification state than the one about
375 to become visible, then correct the caller's misconception. */
376 if (visible_line[0] != invisible_line[0])
377 rl_display_fixed = 0;
379 /* If the prompt to be displayed is the `primary' readline prompt (the
380 one passed to readline()), use the values we have already expanded.
381 If not, use what's already in rl_display_prompt. WRAP_OFFSET is the
382 number of non-visible characters in the prompt string. */
383 if (rl_display_prompt == rl_prompt || local_prompt)
385 int local_len = local_prompt ? strlen (local_prompt) : 0;
386 if (local_prompt_prefix && forced_display)
387 _rl_output_some_chars (local_prompt_prefix, strlen (local_prompt_prefix));
391 temp = local_len + out + 2;
392 if (temp >= line_size)
394 line_size = (temp + 1024) - (temp % 1024);
395 visible_line = xrealloc (visible_line, line_size);
396 line = invisible_line = xrealloc (invisible_line, line_size);
398 strncpy (line + out, local_prompt, local_len);
402 wrap_offset = local_len - visible_length;
407 prompt_this_line = strrchr (rl_display_prompt, '\n');
408 if (!prompt_this_line)
409 prompt_this_line = rl_display_prompt;
413 pmtlen = prompt_this_line - rl_display_prompt; /* temp var */
416 _rl_output_some_chars (rl_display_prompt, pmtlen);
417 /* Make sure we are at column zero even after a newline,
418 regardless of the state of terminal output processing. */
419 if (pmtlen < 2 || prompt_this_line[-2] != '\r')
424 pmtlen = strlen (prompt_this_line);
425 temp = pmtlen + out + 2;
426 if (temp >= line_size)
428 line_size = (temp + 1024) - (temp % 1024);
429 visible_line = xrealloc (visible_line, line_size);
430 line = invisible_line = xrealloc (invisible_line, line_size);
432 strncpy (line + out, prompt_this_line, pmtlen);
438 #define CHECK_INV_LBREAKS() \
440 if (newlines >= (inv_lbsize - 2)) \
443 inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
447 #define CHECK_LPOS() \
450 if (lpos >= screenwidth) \
452 if (newlines >= (inv_lbsize - 2)) \
455 inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
457 inv_lbreaks[++newlines] = out; \
462 /* inv_lbreaks[i] is where line i starts in the buffer. */
463 inv_lbreaks[newlines = 0] = 0;
464 lpos = out - wrap_offset;
466 /* XXX - what if lpos is already >= screenwidth before we start drawing the
467 contents of the command line? */
468 while (lpos >= screenwidth)
470 /* XXX - possible fix from Darin Johnson <darin@acuson.com> for prompt
471 string with invisible characters that is longer than the screen
472 width. XXX - this doesn't work right if invisible characters have
473 to be put on the second screen line -- it adds too much (the number
474 of invisible chars after the screenwidth). */
475 temp = ((newlines + 1) * screenwidth) + ((newlines == 0) ? wrap_offset : 0);
477 inv_lbreaks[++newlines] = temp;
482 for (in = 0; in < rl_end; in++)
484 c = (unsigned char)rl_line_buffer[in];
486 if (out + 8 >= line_size) /* XXX - 8 for \t */
489 visible_line = xrealloc (visible_line, line_size);
490 invisible_line = xrealloc (invisible_line, line_size);
491 line = invisible_line;
497 lb_linenum = newlines;
502 if (_rl_output_meta_chars == 0)
504 sprintf (line + out, "\\%o", c);
506 if (lpos + 4 >= screenwidth)
508 temp = screenwidth - lpos;
509 CHECK_INV_LBREAKS ();
510 inv_lbreaks[++newlines] = out + temp;
524 #if defined (DISPLAY_TABS)
527 register int temp, newout;
530 newout = (out | (int)7) + 1;
532 newout = out + 8 - lpos % 8;
535 if (lpos + temp >= screenwidth)
538 temp2 = screenwidth - lpos;
539 CHECK_INV_LBREAKS ();
540 inv_lbreaks[++newlines] = out + temp2;
553 else if (c == '\n' && _rl_horizontal_scroll_mode == 0 && term_up && *term_up)
555 line[out++] = '\0'; /* XXX - sentinel */
556 CHECK_INV_LBREAKS ();
557 inv_lbreaks[++newlines] = out;
560 else if (CTRL_CHAR (c) || c == RUBOUT)
564 line[out++] = CTRL_CHAR (c) ? UNCTRL (c) : '?';
577 lb_linenum = newlines;
580 inv_botlin = lb_botlin = newlines;
581 CHECK_INV_LBREAKS ();
582 inv_lbreaks[newlines+1] = out;
583 cursor_linenum = lb_linenum;
585 /* C_POS == position in buffer where cursor should be placed. */
587 /* PWP: now is when things get a bit hairy. The visible and invisible
588 line buffers are really multiple lines, which would wrap every
589 (screenwidth - 1) characters. Go through each in turn, finding
590 the changed region and updating it. The line order is top to bottom. */
592 /* If we can move the cursor up and down, then use multiple lines,
593 otherwise, let long lines display in a single terminal line, and
594 horizontally scroll it. */
596 if (_rl_horizontal_scroll_mode == 0 && term_up && *term_up)
598 int nleft, pos, changed_screen_line;
600 if (!rl_display_fixed || forced_display)
604 /* If we have more than a screenful of material to display, then
605 only display a screenful. We should display the last screen,
607 if (out >= screenchars)
608 out = screenchars - 1;
610 /* The first line is at character position 0 in the buffer. The
611 second and subsequent lines start at inv_lbreaks[N], offset by
612 OFFSET (which has already been calculated above). */
614 #define W_OFFSET(line, offset) ((line) == 0 ? offset : 0)
615 #define VIS_LLEN(l) ((l) > _rl_vis_botlin ? 0 : (vis_lbreaks[l+1] - vis_lbreaks[l]))
616 #define INV_LLEN(l) (inv_lbreaks[l+1] - inv_lbreaks[l])
617 #define VIS_CHARS(line) (visible_line + vis_lbreaks[line])
618 #define VIS_LINE(line) ((line) > _rl_vis_botlin) ? "" : VIS_CHARS(line)
619 #define INV_LINE(line) (invisible_line + inv_lbreaks[line])
621 /* For each line in the buffer, do the updating display. */
622 for (linenum = 0; linenum <= inv_botlin; linenum++)
624 update_line (VIS_LINE(linenum), INV_LINE(linenum), linenum,
625 VIS_LLEN(linenum), INV_LLEN(linenum), inv_botlin);
627 /* If this is the line with the prompt, we might need to
628 compensate for invisible characters in the new line. Do
629 this only if there is not more than one new line (which
630 implies that we completely overwrite the old visible line)
631 and the new line is shorter than the old. Make sure we are
632 at the end of the new line before clearing. */
634 inv_botlin == 0 && _rl_last_c_pos == out &&
635 (wrap_offset > visible_wrap_offset) &&
636 (_rl_last_c_pos < visible_first_line_len))
638 nleft = screenwidth + wrap_offset - _rl_last_c_pos;
640 _rl_clear_to_eol (nleft);
643 /* Since the new first line is now visible, save its length. */
645 visible_first_line_len = (inv_botlin > 0) ? inv_lbreaks[1] : out - wrap_offset;
648 /* We may have deleted some lines. If so, clear the left over
649 blank ones at the bottom out. */
650 if (_rl_vis_botlin > inv_botlin)
653 for (; linenum <= _rl_vis_botlin; linenum++)
655 tt = VIS_CHARS (linenum);
656 _rl_move_vert (linenum);
657 _rl_move_cursor_relative (0, tt);
659 ((linenum == _rl_vis_botlin) ? strlen (tt) : screenwidth);
662 _rl_vis_botlin = inv_botlin;
664 /* CHANGED_SCREEN_LINE is set to 1 if we have moved to a
665 different screen line during this redisplay. */
666 changed_screen_line = _rl_last_v_pos != cursor_linenum;
667 if (changed_screen_line)
669 _rl_move_vert (cursor_linenum);
670 /* If we moved up to the line with the prompt using term_up,
671 the physical cursor position on the screen stays the same,
672 but the buffer position needs to be adjusted to account
673 for invisible characters. */
674 if (cursor_linenum == 0 && wrap_offset)
675 _rl_last_c_pos += wrap_offset;
678 /* We have to reprint the prompt if it contains invisible
679 characters, since it's not generally OK to just reprint
680 the characters from the current cursor position. But we
681 only need to reprint it if the cursor is before the last
682 invisible character in the prompt string. */
683 nleft = visible_length + wrap_offset;
684 if (cursor_linenum == 0 && wrap_offset > 0 && _rl_last_c_pos > 0 &&
685 _rl_last_c_pos <= last_invisible && local_prompt)
687 #if defined (__MSDOS__)
688 putc ('\r', rl_outstream);
691 tputs (term_cr, 1, _rl_output_character_function);
693 _rl_output_some_chars (local_prompt, nleft);
694 _rl_last_c_pos = nleft;
697 /* Where on that line? And where does that line start
699 pos = inv_lbreaks[cursor_linenum];
700 /* nleft == number of characters in the line buffer between the
701 start of the line and the cursor position. */
704 /* Since _rl_backspace() doesn't know about invisible characters in the
705 prompt, and there's no good way to tell it, we compensate for
706 those characters here and call _rl_backspace() directly. */
707 if (wrap_offset && cursor_linenum == 0 && nleft < _rl_last_c_pos)
709 _rl_backspace (_rl_last_c_pos - nleft);
710 _rl_last_c_pos = nleft;
713 if (nleft != _rl_last_c_pos)
714 _rl_move_cursor_relative (nleft, &invisible_line[pos]);
717 else /* Do horizontal scrolling. */
719 #define M_OFFSET(margin, offset) ((margin) == 0 ? offset : 0)
720 int lmargin, ndisp, nleft, phys_c_pos, t;
722 /* Always at top line. */
725 /* Compute where in the buffer the displayed line should start. This
728 /* The number of characters that will be displayed before the cursor. */
729 ndisp = c_pos - wrap_offset;
730 nleft = visible_length + wrap_offset;
731 /* Where the new cursor position will be on the screen. This can be
732 longer than SCREENWIDTH; if it is, lmargin will be adjusted. */
733 phys_c_pos = c_pos - (last_lmargin ? last_lmargin : wrap_offset);
736 /* If the number of characters had already exceeded the screenwidth,
737 last_lmargin will be > 0. */
739 /* If the number of characters to be displayed is more than the screen
740 width, compute the starting offset so that the cursor is about
741 two-thirds of the way across the screen. */
742 if (phys_c_pos > screenwidth - 2)
744 lmargin = c_pos - (2 * t);
747 /* If the left margin would be in the middle of a prompt with
748 invisible characters, don't display the prompt at all. */
749 if (wrap_offset && lmargin > 0 && lmargin < nleft)
752 else if (ndisp < screenwidth - 2) /* XXX - was -1 */
754 else if (phys_c_pos < 1)
756 /* If we are moving back towards the beginning of the line and
757 the last margin is no longer correct, compute a new one. */
758 lmargin = ((c_pos - 1) / t) * t; /* XXX */
759 if (wrap_offset && lmargin > 0 && lmargin < nleft)
763 lmargin = last_lmargin;
765 /* If the first character on the screen isn't the first character
766 in the display line, indicate this with a special character. */
770 /* If SCREENWIDTH characters starting at LMARGIN do not encompass
771 the whole line, indicate that with a special character at the
772 right edge of the screen. If LMARGIN is 0, we need to take the
773 wrap offset into account. */
774 t = lmargin + M_OFFSET (lmargin, wrap_offset) + screenwidth;
778 if (!rl_display_fixed || forced_display || lmargin != last_lmargin)
781 update_line (&visible_line[last_lmargin],
782 &invisible_line[lmargin],
784 screenwidth + visible_wrap_offset,
785 screenwidth + (lmargin ? 0 : wrap_offset),
788 /* If the visible new line is shorter than the old, but the number
789 of invisible characters is greater, and we are at the end of
790 the new line, we need to clear to eol. */
791 t = _rl_last_c_pos - M_OFFSET (lmargin, wrap_offset);
792 if ((M_OFFSET (lmargin, wrap_offset) > visible_wrap_offset) &&
793 (_rl_last_c_pos == out) &&
794 t < visible_first_line_len)
796 nleft = screenwidth - t;
797 _rl_clear_to_eol (nleft);
799 visible_first_line_len = out - lmargin - M_OFFSET (lmargin, wrap_offset);
800 if (visible_first_line_len > screenwidth)
801 visible_first_line_len = screenwidth;
803 _rl_move_cursor_relative (c_pos - lmargin, &invisible_line[lmargin]);
804 last_lmargin = lmargin;
807 fflush (rl_outstream);
809 /* Swap visible and non-visible lines. */
811 char *temp = visible_line;
812 int *itemp = vis_lbreaks, ntemp = vis_lbsize;
814 visible_line = invisible_line;
815 invisible_line = temp;
817 vis_lbreaks = inv_lbreaks;
820 vis_lbsize = inv_lbsize;
823 rl_display_fixed = 0;
824 /* If we are displaying on a single line, and last_lmargin is > 0, we
825 are not displaying any invisible characters, so set visible_wrap_offset
827 if (_rl_horizontal_scroll_mode && last_lmargin)
828 visible_wrap_offset = 0;
830 visible_wrap_offset = wrap_offset;
834 /* PWP: update_line() is based on finding the middle difference of each
835 line on the screen; vis:
837 /old first difference
838 /beginning of line | /old last same /old EOL
840 old: eddie> Oh, my little gruntle-buggy is to me, as lurgid as
841 new: eddie> Oh, my little buggy says to me, as lurgid as
843 \beginning of line | \new last same \new end of line
844 \new first difference
846 All are character pointers for the sake of speed. Special cases for
847 no differences, as well as for end of line additions must be handled.
849 Could be made even smarter, but this works well enough */
851 update_line (old, new, current_line, omax, nmax, inv_botlin)
852 register char *old, *new;
853 int current_line, omax, nmax, inv_botlin;
855 register char *ofd, *ols, *oe, *nfd, *nls, *ne;
856 int temp, lendiff, wsatend, od, nd;
857 int current_invis_chars;
859 /* If we're at the right edge of a terminal that supports xn, we're
860 ready to wrap around, so do so. This fixes problems with knowing
861 the exact cursor position and cut-and-paste with certain terminal
862 emulators. In this calculation, TEMP is the physical screen
863 position of the cursor. */
864 temp = _rl_last_c_pos - W_OFFSET(_rl_last_v_pos, visible_wrap_offset);
865 if (temp == screenwidth && _rl_term_autowrap && !_rl_horizontal_scroll_mode
866 && _rl_last_v_pos == current_line - 1)
869 putc (new[0], rl_outstream);
871 putc (' ', rl_outstream);
872 _rl_last_c_pos = 1; /* XXX */
874 if (old[0] && new[0])
878 /* Find first difference. */
879 for (ofd = old, nfd = new;
880 (ofd - old < omax) && *ofd && (*ofd == *nfd);
884 /* Move to the end of the screen line. ND and OD are used to keep track
885 of the distance between ne and new and oe and old, respectively, to
886 move a subtraction out of each loop. */
887 for (od = ofd - old, oe = ofd; od < omax && *oe; oe++, od++);
888 for (nd = nfd - new, ne = nfd; nd < nmax && *ne; ne++, nd++);
890 /* If no difference, continue to next line. */
891 if (ofd == oe && nfd == ne)
894 wsatend = 1; /* flag for trailing whitespace */
895 ols = oe - 1; /* find last same */
897 while ((ols > ofd) && (nls > nfd) && (*ols == *nls))
910 else if (*ols != *nls)
912 if (*ols) /* don't step past the NUL */
918 /* count of invisible characters in the current invisible line. */
919 current_invis_chars = W_OFFSET (current_line, wrap_offset);
920 if (_rl_last_v_pos != current_line)
922 _rl_move_vert (current_line);
923 if (current_line == 0 && visible_wrap_offset)
924 _rl_last_c_pos += visible_wrap_offset;
927 /* If this is the first line and there are invisible characters in the
928 prompt string, and the prompt string has not changed, and the current
929 cursor position is before the last invisible character in the prompt,
930 and the index of the character to move to is past the end of the prompt
931 string, then redraw the entire prompt string. We can only do this
932 reliably if the terminal supports a `cr' capability.
934 This is not an efficiency hack -- there is a problem with redrawing
935 portions of the prompt string if they contain terminal escape
936 sequences (like drawing the `unbold' sequence without a corresponding
937 `bold') that manifests itself on certain terminals. */
939 lendiff = local_prompt ? strlen (local_prompt) : 0;
940 od = ofd - old; /* index of first difference in visible line */
941 if (current_line == 0 && !_rl_horizontal_scroll_mode &&
942 term_cr && lendiff > visible_length && _rl_last_c_pos > 0 &&
943 od > lendiff && _rl_last_c_pos < last_invisible)
945 #if defined (__MSDOS__)
946 putc ('\r', rl_outstream);
948 tputs (term_cr, 1, _rl_output_character_function);
950 _rl_output_some_chars (local_prompt, lendiff);
951 _rl_last_c_pos = lendiff;
954 _rl_move_cursor_relative (od, old);
956 /* if (len (new) > len (old)) */
957 lendiff = (nls - nfd) - (ols - ofd);
959 /* If we are changing the number of invisible characters in a line, and
960 the spot of first difference is before the end of the invisible chars,
961 lendiff needs to be adjusted. */
962 if (current_line == 0 && !_rl_horizontal_scroll_mode &&
963 current_invis_chars != visible_wrap_offset)
964 lendiff += visible_wrap_offset - current_invis_chars;
966 /* Insert (diff (len (old), len (new)) ch. */
970 /* Non-zero if we're increasing the number of lines. */
971 int gl = current_line >= _rl_vis_botlin && inv_botlin > _rl_vis_botlin;
972 /* Sometimes it is cheaper to print the characters rather than
973 use the terminal's capabilities. If we're growing the number
974 of lines, make sure we actually cause the new line to wrap
975 around on auto-wrapping terminals. */
976 if (terminal_can_insert && ((2 * temp) >= lendiff || term_IC) && (!_rl_term_autowrap || !gl))
978 /* If lendiff > visible_length and _rl_last_c_pos == 0 and
979 _rl_horizontal_scroll_mode == 1, inserting the characters with
980 term_IC or term_ic will screw up the screen because of the
981 invisible characters. We need to just draw them. */
982 if (*ols && (!_rl_horizontal_scroll_mode || _rl_last_c_pos > 0 ||
983 lendiff <= visible_length || !current_invis_chars))
985 insert_some_chars (nfd, lendiff);
986 _rl_last_c_pos += lendiff;
990 /* At the end of a line the characters do not have to
991 be "inserted". They can just be placed on the screen. */
992 /* However, this screws up the rest of this block, which
993 assumes you've done the insert because you can. */
994 _rl_output_some_chars (nfd, lendiff);
995 _rl_last_c_pos += lendiff;
999 /* We have horizontal scrolling and we are not inserting at
1000 the end. We have invisible characters in this line. This
1001 is a dumb update. */
1002 _rl_output_some_chars (nfd, temp);
1003 _rl_last_c_pos += temp;
1006 /* Copy (new) chars to screen from first diff to last match. */
1008 if ((temp - lendiff) > 0)
1010 _rl_output_some_chars (nfd + lendiff, temp - lendiff);
1011 _rl_last_c_pos += temp - lendiff;
1016 /* cannot insert chars, write to EOL */
1017 _rl_output_some_chars (nfd, temp);
1018 _rl_last_c_pos += temp;
1021 else /* Delete characters from line. */
1023 /* If possible and inexpensive to use terminal deletion, then do so. */
1024 if (term_dc && (2 * temp) >= -lendiff)
1026 /* If all we're doing is erasing the invisible characters in the
1027 prompt string, don't bother. It screws up the assumptions
1028 about what's on the screen. */
1029 if (_rl_horizontal_scroll_mode && _rl_last_c_pos == 0 &&
1030 -lendiff == visible_wrap_offset)
1034 delete_chars (-lendiff); /* delete (diff) characters */
1036 /* Copy (new) chars to screen from first diff to last match */
1040 _rl_output_some_chars (nfd, temp);
1041 _rl_last_c_pos += temp;
1044 /* Otherwise, print over the existing material. */
1049 _rl_output_some_chars (nfd, temp);
1050 _rl_last_c_pos += temp;
1052 lendiff = (oe - old) - (ne - new);
1055 if (_rl_term_autowrap && current_line < inv_botlin)
1056 space_to_eol (lendiff);
1058 _rl_clear_to_eol (lendiff);
1064 /* Tell the update routines that we have moved onto a new (empty) line. */
1069 visible_line[0] = '\0';
1071 _rl_last_c_pos = _rl_last_v_pos = 0;
1072 _rl_vis_botlin = last_lmargin = 0;
1074 vis_lbreaks[0] = vis_lbreaks[1] = 0;
1075 visible_wrap_offset = 0;
1079 /* Tell the update routines that we have moved onto a new line with the
1080 prompt already displayed. Code originally from the version of readline
1081 distributed with CLISP. */
1083 rl_on_new_line_with_prompt ()
1085 int prompt_size, i, l, real_screenwidth, newlines;
1086 char *prompt_last_line;
1088 /* Initialize visible_line and invisible_line to ensure that they can hold
1089 the already-displayed prompt. */
1090 prompt_size = strlen (rl_prompt) + 1;
1091 init_line_structures (prompt_size);
1093 /* Make sure the line structures hold the already-displayed prompt for
1095 strcpy (visible_line, rl_prompt);
1096 strcpy (invisible_line, rl_prompt);
1098 /* If the prompt contains newlines, take the last tail. */
1099 prompt_last_line = strrchr (rl_prompt, '\n');
1100 if (!prompt_last_line)
1101 prompt_last_line = rl_prompt;
1103 l = strlen (prompt_last_line);
1106 /* Dissect prompt_last_line into screen lines. Note that here we have
1107 to use the real screenwidth. Readline's notion of screenwidth might be
1108 one less, see terminal.c. */
1109 real_screenwidth = screenwidth + (_rl_term_autowrap ? 0 : 1);
1110 _rl_last_v_pos = l / real_screenwidth;
1111 /* If the prompt length is a multiple of real_screenwidth, we don't know
1112 whether the cursor is at the end of the last line, or already at the
1113 beginning of the next line. Output a newline just to be safe. */
1114 if (l > 0 && (l % real_screenwidth) == 0)
1115 _rl_output_some_chars ("\n", 1);
1118 newlines = 0; i = 0;
1121 _rl_vis_botlin = newlines;
1122 vis_lbreaks[newlines++] = i;
1123 i += real_screenwidth;
1125 vis_lbreaks[newlines] = l;
1126 visible_wrap_offset = 0;
1131 /* Actually update the display, period. */
1133 rl_forced_update_display ()
1137 register char *temp = visible_line;
1144 (*rl_redisplay_function) ();
1148 /* Move the cursor from _rl_last_c_pos to NEW, which are buffer indices.
1149 DATA is the contents of the screen line of interest; i.e., where
1150 the movement is being done. */
1152 _rl_move_cursor_relative (new, data)
1158 /* If we don't have to do anything, then return. */
1159 if (_rl_last_c_pos == new) return;
1161 /* It may be faster to output a CR, and then move forwards instead
1162 of moving backwards. */
1163 /* i == current physical cursor position. */
1164 i = _rl_last_c_pos - W_OFFSET(_rl_last_v_pos, visible_wrap_offset);
1165 if (new == 0 || CR_FASTER (new, _rl_last_c_pos) ||
1166 (_rl_term_autowrap && i == screenwidth))
1168 #if defined (__MSDOS__)
1169 putc ('\r', rl_outstream);
1171 tputs (term_cr, 1, _rl_output_character_function);
1172 #endif /* !__MSDOS__ */
1176 if (_rl_last_c_pos < new)
1178 /* Move the cursor forward. We do it by printing the command
1179 to move the cursor forward if there is one, else print that
1180 portion of the output buffer again. Which is cheaper? */
1182 /* The above comment is left here for posterity. It is faster
1183 to print one character (non-control) than to print a control
1184 sequence telling the terminal to move forward one character.
1185 That kind of control is for people who don't know what the
1186 data is underneath the cursor. */
1187 #if defined (HACK_TERMCAP_MOTION)
1188 if (term_forward_char)
1189 for (i = _rl_last_c_pos; i < new; i++)
1190 tputs (term_forward_char, 1, _rl_output_character_function);
1192 for (i = _rl_last_c_pos; i < new; i++)
1193 putc (data[i], rl_outstream);
1195 for (i = _rl_last_c_pos; i < new; i++)
1196 putc (data[i], rl_outstream);
1197 #endif /* HACK_TERMCAP_MOTION */
1199 else if (_rl_last_c_pos > new)
1200 _rl_backspace (_rl_last_c_pos - new);
1201 _rl_last_c_pos = new;
1204 /* PWP: move the cursor up or down. */
1209 register int delta, i;
1211 if (_rl_last_v_pos == to || to > screenheight)
1214 if ((delta = to - _rl_last_v_pos) > 0)
1216 for (i = 0; i < delta; i++)
1217 putc ('\n', rl_outstream);
1218 #if defined (__MSDOS__)
1219 putc ('\r', rl_outstream);
1221 tputs (term_cr, 1, _rl_output_character_function);
1227 if (term_up && *term_up)
1228 for (i = 0; i < -delta; i++)
1229 tputs (term_up, 1, _rl_output_character_function);
1232 _rl_last_v_pos = to; /* Now TO is here */
1235 /* Physically print C on rl_outstream. This is for functions which know
1236 how to optimize the display. Return the number of characters output. */
1242 if (META_CHAR (c) && (_rl_output_meta_chars == 0))
1244 fprintf (rl_outstream, "M-");
1249 #if defined (DISPLAY_TABS)
1250 if ((CTRL_CHAR (c) && c != '\t') || c == RUBOUT)
1252 if (CTRL_CHAR (c) || c == RUBOUT)
1253 #endif /* !DISPLAY_TABS */
1255 fprintf (rl_outstream, "C-");
1257 c = CTRL_CHAR (c) ? UNCTRL (c) : '?';
1260 putc (c, rl_outstream);
1261 fflush (rl_outstream);
1266 rl_character_len (c, pos)
1267 register int c, pos;
1271 uc = (unsigned char)c;
1274 return ((_rl_output_meta_chars == 0) ? 4 : 1);
1278 #if defined (DISPLAY_TABS)
1279 return (((pos | 7) + 1) - pos);
1282 #endif /* !DISPLAY_TABS */
1285 if (CTRL_CHAR (c) || c == RUBOUT)
1288 return ((isprint (uc)) ? 1 : 2);
1291 /* How to print things in the "echo-area". The prompt is treated as a
1294 #if defined (USE_VARARGS)
1296 #if defined (PREFER_STDARG)
1297 rl_message (const char *format, ...)
1299 rl_message (va_alist)
1304 #if defined (PREFER_VARARGS)
1308 #if defined (PREFER_STDARG)
1309 va_start (args, format);
1312 format = va_arg (args, char *);
1315 vsprintf (msg_buf, format, args);
1318 rl_display_prompt = msg_buf;
1319 (*rl_redisplay_function) ();
1322 #else /* !USE_VARARGS */
1324 rl_message (format, arg1, arg2)
1327 sprintf (msg_buf, format, arg1, arg2);
1328 rl_display_prompt = msg_buf;
1329 (*rl_redisplay_function) ();
1332 #endif /* !USE_VARARGS */
1334 /* How to clear things from the "echo-area". */
1338 rl_display_prompt = rl_prompt;
1339 (*rl_redisplay_function) ();
1344 rl_reset_line_state ()
1348 rl_display_prompt = rl_prompt ? rl_prompt : "";
1353 static char *saved_local_prompt;
1354 static char *saved_local_prefix;
1355 static int saved_last_invisible;
1356 static int saved_visible_length;
1361 saved_local_prompt = local_prompt;
1362 saved_local_prefix = local_prompt_prefix;
1363 saved_last_invisible = last_invisible;
1364 saved_visible_length = visible_length;
1366 local_prompt = local_prompt_prefix = (char *)0;
1367 last_invisible = visible_length = 0;
1371 rl_restore_prompt ()
1374 free (local_prompt);
1375 if (local_prompt_prefix)
1376 free (local_prompt_prefix);
1378 local_prompt = saved_local_prompt;
1379 local_prompt_prefix = saved_local_prefix;
1380 last_invisible = saved_last_invisible;
1381 visible_length = saved_visible_length;
1385 _rl_make_prompt_for_search (pchar)
1393 if (saved_local_prompt == 0)
1395 len = (rl_prompt && *rl_prompt) ? strlen (rl_prompt) : 0;
1396 pmt = xmalloc (len + 2);
1398 strcpy (pmt, rl_prompt);
1404 len = *saved_local_prompt ? strlen (saved_local_prompt) : 0;
1405 pmt = xmalloc (len + 2);
1407 strcpy (pmt, saved_local_prompt);
1410 local_prompt = savestring (pmt);
1411 last_invisible = saved_last_invisible;
1412 visible_length = saved_visible_length + 1;
1417 /* Quick redisplay hack when erasing characters at the end of the line. */
1419 _rl_erase_at_end_of_line (l)
1425 for (i = 0; i < l; i++)
1426 putc (' ', rl_outstream);
1428 for (i = 0; i < l; i++)
1429 visible_line[--_rl_last_c_pos] = '\0';
1433 /* Clear to the end of the line. COUNT is the minimum
1434 number of character spaces to clear, */
1436 _rl_clear_to_eol (count)
1440 tputs (term_clreol, 1, _rl_output_character_function);
1442 space_to_eol (count);
1445 /* Clear to the end of the line using spaces. COUNT is the minimum
1446 number of character spaces to clear, */
1448 space_to_eol (count)
1453 for (i = 0; i < count; i++)
1454 putc (' ', rl_outstream);
1456 _rl_last_c_pos += count;
1463 tputs (term_clrpag, 1, _rl_output_character_function);
1468 /* Insert COUNT characters from STRING to the output stream. */
1470 insert_some_chars (string, count)
1474 /* If IC is defined, then we do not have to "enter" insert mode. */
1478 buffer = tgoto (term_IC, 0, count);
1479 tputs (buffer, 1, _rl_output_character_function);
1480 _rl_output_some_chars (string, count);
1486 /* If we have to turn on insert-mode, then do so. */
1487 if (term_im && *term_im)
1488 tputs (term_im, 1, _rl_output_character_function);
1490 /* If there is a special command for inserting characters, then
1491 use that first to open up the space. */
1492 if (term_ic && *term_ic)
1494 for (i = count; i--; )
1495 tputs (term_ic, 1, _rl_output_character_function);
1498 /* Print the text. */
1499 _rl_output_some_chars (string, count);
1501 /* If there is a string to turn off insert mode, we had best use
1503 if (term_ei && *term_ei)
1504 tputs (term_ei, 1, _rl_output_character_function);
1508 /* Delete COUNT characters from the display line. */
1510 delete_chars (count)
1513 if (count > screenwidth) /* XXX */
1516 if (term_DC && *term_DC)
1519 buffer = tgoto (term_DC, count, count);
1520 tputs (buffer, count, _rl_output_character_function);
1524 if (term_dc && *term_dc)
1526 tputs (term_dc, 1, _rl_output_character_function);
1536 /* If the cursor is the only thing on an otherwise-blank last line,
1537 compensate so we don't print an extra CRLF. */
1538 if (_rl_vis_botlin && _rl_last_c_pos == 0 &&
1539 visible_line[vis_lbreaks[_rl_vis_botlin]] == 0)
1544 _rl_move_vert (_rl_vis_botlin);
1545 /* If we've wrapped lines, remove the final xterm line-wrap flag. */
1546 if (full_lines && _rl_term_autowrap && (VIS_LLEN(_rl_vis_botlin) == screenwidth))
1550 last_line = &visible_line[inv_lbreaks[_rl_vis_botlin]];
1552 last_line = &visible_line[vis_lbreaks[_rl_vis_botlin]];
1554 _rl_move_cursor_relative (screenwidth - 1, last_line);
1555 _rl_clear_to_eol (0);
1556 putc (last_line[screenwidth - 1], rl_outstream);
1560 fflush (rl_outstream);
1564 /* Move to the start of the current line. */
1570 #if defined (__MSDOS__)
1571 putc ('\r', rl_outstream);
1573 tputs (term_cr, 1, _rl_output_character_function);
1579 /* Redraw the last line of a multi-line prompt that may possibly contain
1580 terminal escape sequences. Called with the cursor at column 0 of the
1581 line to draw the prompt on. */
1586 char *oldp, *oldl, *oldlprefix;
1587 int oldlen, oldlast, oldplen;
1589 /* Geez, I should make this a struct. */
1590 oldp = rl_display_prompt;
1591 oldl = local_prompt;
1592 oldlprefix = local_prompt_prefix;
1593 oldlen = visible_length;
1594 oldplen = prefix_length;
1595 oldlast = last_invisible;
1597 rl_display_prompt = t;
1598 local_prompt = expand_prompt (t, &visible_length, &last_invisible);
1599 local_prompt_prefix = (char *)NULL;
1600 rl_forced_update_display ();
1602 rl_display_prompt = oldp;
1603 local_prompt = oldl;
1604 local_prompt_prefix = oldlprefix;
1605 visible_length = oldlen;
1606 prefix_length = oldplen;
1607 last_invisible = oldlast;
1610 /* Redisplay the current line after a SIGWINCH is received. */
1612 _rl_redisplay_after_sigwinch ()
1616 /* Clear the current line and put the cursor at column 0. Make sure
1617 the right thing happens if we have wrapped to a new screen line. */
1620 #if defined (__MSDOS__)
1621 putc ('\r', rl_outstream);
1623 tputs (term_cr, 1, _rl_output_character_function);
1626 #if defined (__MSDOS__)
1627 space_to_eol (screenwidth);
1628 putc ('\r', rl_outstream);
1631 tputs (term_clreol, 1, _rl_output_character_function);
1634 space_to_eol (screenwidth);
1635 tputs (term_cr, 1, _rl_output_character_function);
1638 if (_rl_last_v_pos > 0)
1644 /* Redraw only the last line of a multi-line prompt. */
1645 t = strrchr (rl_display_prompt, '\n');
1647 redraw_prompt (++t);
1649 rl_forced_update_display ();
1653 _rl_clean_up_for_exit ()
1655 if (readline_echoing_p)
1657 _rl_move_vert (_rl_vis_botlin);
1659 fflush (rl_outstream);
1660 rl_restart_output (1, 0);
1665 _rl_erase_entire_line ()
1668 _rl_clear_to_eol (0);
1670 fflush (rl_outstream);
1673 /* return the `current display line' of the cursor -- the number of lines to
1674 move up to get to the first screen line of the current readline line. */
1676 _rl_current_display_line ()
1680 /* Find out whether or not there might be invisible characters in the
1682 if (rl_display_prompt == rl_prompt)
1683 nleft = _rl_last_c_pos - screenwidth - rl_visible_prompt_length;
1685 nleft = _rl_last_c_pos - screenwidth;
1688 ret = 1 + nleft / screenwidth;