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 *_rl_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 rl_voidfunc_t *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 /* Variables to keep track of the expanded prompt string, which may
147 include invisible characters. */
149 static char *local_prompt, *local_prompt_prefix;
150 static int prompt_visible_length, prompt_prefix_length;
152 /* The number of invisible characters in the line currently being
153 displayed on the screen. */
154 static int visible_wrap_offset;
156 /* The number of invisible characters in the prompt string. Static so it
157 can be shared between rl_redisplay and update_line */
158 static int wrap_offset;
160 /* The index of the last invisible character in the prompt string. */
161 static int prompt_last_invisible;
163 /* The length (buffer offset) of the first line of the last (possibly
164 multi-line) buffer displayed on the screen. */
165 static int visible_first_line_len;
167 /* Number of invisible characters on the first physical line of the prompt.
168 Only valid when the number of physical characters in the prompt exceeds
169 (or is equal to) _rl_screenwidth. */
170 static int prompt_invis_chars_first_line;
172 static int prompt_last_screen_line;
174 /* Expand the prompt string S and return the number of visible
175 characters in *LP, if LP is not null. This is currently more-or-less
176 a placeholder for expansion. LIP, if non-null is a place to store the
177 index of the last invisible character in the returned string. NIFLP,
178 if non-zero, is a place to store the number of invisible characters in
179 the first prompt line. */
181 /* Current implementation:
182 \001 (^A) start non-visible characters
183 \002 (^B) end non-visible characters
184 all characters except \001 and \002 (following a \001) are copied to
185 the returned string; all characters except those between \001 and
186 \002 are assumed to be `visible'. */
189 expand_prompt (pmt, lp, lip, niflp)
191 int *lp, *lip, *niflp;
194 int l, rl, last, ignoring, ninvis, invfl;
196 /* Short-circuit if we can. */
197 if (strchr (pmt, RL_PROMPT_START_IGNORE) == 0)
199 r = savestring (pmt);
206 r = ret = xmalloc (l + 1);
208 invfl = 0; /* invisible chars in first line of prompt */
210 for (rl = ignoring = last = ninvis = 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)
232 if (rl == _rl_screenwidth)
237 if (rl < _rl_screenwidth)
250 /* Just strip out RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE from
251 PMT and return the rest of PMT. */
253 _rl_strip_prompt (pmt)
258 ret = expand_prompt (pmt, (int *)NULL, (int *)NULL, (int *)NULL);
263 * Expand the prompt string into the various display components, if
266 * local_prompt = expanded last line of string in rl_display_prompt
267 * (portion after the final newline)
268 * local_prompt_prefix = portion before last newline of rl_display_prompt,
269 * expanded via expand_prompt
270 * prompt_visible_length = number of visible characters in local_prompt
271 * prompt_prefix_length = number of visible characters in local_prompt_prefix
273 * This function is called once per call to readline(). It may also be
274 * called arbitrarily to expand the primary prompt.
276 * The return value is the number of visible characters on the last line
277 * of the (possibly multi-line) prompt.
280 rl_expand_prompt (prompt)
286 /* Clear out any saved values. */
288 FREE (local_prompt_prefix);
290 local_prompt = local_prompt_prefix = (char *)0;
291 prompt_last_invisible = prompt_visible_length = 0;
293 if (prompt == 0 || *prompt == 0)
296 p = strrchr (prompt, '\n');
299 /* The prompt is only one logical line, though it might wrap. */
300 local_prompt = expand_prompt (prompt, &prompt_visible_length,
301 &prompt_last_invisible,
302 &prompt_invis_chars_first_line);
303 local_prompt_prefix = (char *)0;
304 return (prompt_visible_length);
308 /* The prompt spans multiple lines. */
310 local_prompt = expand_prompt (p, &prompt_visible_length,
311 &prompt_last_invisible,
312 &prompt_invis_chars_first_line);
314 /* The portion of the prompt string up to and including the
315 final newline is now null-terminated. */
316 local_prompt_prefix = expand_prompt (prompt, &prompt_prefix_length,
318 &prompt_invis_chars_first_line);
320 return (prompt_prefix_length);
324 /* Initialize the VISIBLE_LINE and INVISIBLE_LINE arrays, and their associated
325 arrays of line break markers. MINSIZE is the minimum size of VISIBLE_LINE
326 and INVISIBLE_LINE; if it is greater than LINE_SIZE, LINE_SIZE is
327 increased. If the lines have already been allocated, this ensures that
328 they can hold at least MINSIZE characters. */
330 init_line_structures (minsize)
335 if (invisible_line == 0) /* initialize it */
337 if (line_size < minsize)
339 visible_line = xmalloc (line_size);
340 invisible_line = xmalloc (line_size);
342 else if (line_size < minsize) /* ensure it can hold MINSIZE chars */
345 if (line_size < minsize)
347 visible_line = xrealloc (visible_line, line_size);
348 invisible_line = xrealloc (invisible_line, line_size);
351 for (n = minsize; n < line_size; n++)
354 invisible_line[n] = 1;
357 if (vis_lbreaks == 0)
359 /* should be enough. */
360 inv_lbsize = vis_lbsize = 256;
361 inv_lbreaks = (int *)xmalloc (inv_lbsize * sizeof (int));
362 vis_lbreaks = (int *)xmalloc (vis_lbsize * sizeof (int));
363 inv_lbreaks[0] = vis_lbreaks[0] = 0;
367 /* Basic redisplay algorithm. */
371 register int in, out, c, linenum, cursor_linenum;
373 int c_pos, inv_botlin, lb_botlin, lb_linenum;
374 int newlines, lpos, temp;
375 char *prompt_this_line;
377 if (!readline_echoing_p)
380 if (!rl_display_prompt)
381 rl_display_prompt = "";
383 if (invisible_line == 0)
385 init_line_structures (0);
389 /* Draw the line into the buffer. */
392 line = invisible_line;
393 out = inv_botlin = 0;
395 /* Mark the line as modified or not. We only do this for history
397 if (_rl_mark_modified_lines && current_history () && rl_undo_list)
403 /* If someone thought that the redisplay was handled, but the currently
404 visible line has a different modification state than the one about
405 to become visible, then correct the caller's misconception. */
406 if (visible_line[0] != invisible_line[0])
407 rl_display_fixed = 0;
409 /* If the prompt to be displayed is the `primary' readline prompt (the
410 one passed to readline()), use the values we have already expanded.
411 If not, use what's already in rl_display_prompt. WRAP_OFFSET is the
412 number of non-visible characters in the prompt string. */
413 if (rl_display_prompt == rl_prompt || local_prompt)
415 int local_len = local_prompt ? strlen (local_prompt) : 0;
416 if (local_prompt_prefix && forced_display)
417 _rl_output_some_chars (local_prompt_prefix, strlen (local_prompt_prefix));
421 temp = local_len + out + 2;
422 if (temp >= line_size)
424 line_size = (temp + 1024) - (temp % 1024);
425 visible_line = xrealloc (visible_line, line_size);
426 line = invisible_line = xrealloc (invisible_line, line_size);
428 strncpy (line + out, local_prompt, local_len);
432 wrap_offset = local_len - prompt_visible_length;
437 prompt_this_line = strrchr (rl_display_prompt, '\n');
438 if (!prompt_this_line)
439 prompt_this_line = rl_display_prompt;
443 pmtlen = prompt_this_line - rl_display_prompt; /* temp var */
446 _rl_output_some_chars (rl_display_prompt, pmtlen);
447 /* Make sure we are at column zero even after a newline,
448 regardless of the state of terminal output processing. */
449 if (pmtlen < 2 || prompt_this_line[-2] != '\r')
454 pmtlen = strlen (prompt_this_line);
455 temp = pmtlen + out + 2;
456 if (temp >= line_size)
458 line_size = (temp + 1024) - (temp % 1024);
459 visible_line = xrealloc (visible_line, line_size);
460 line = invisible_line = xrealloc (invisible_line, line_size);
462 strncpy (line + out, prompt_this_line, pmtlen);
465 wrap_offset = prompt_invis_chars_first_line = 0;
468 #define CHECK_INV_LBREAKS() \
470 if (newlines >= (inv_lbsize - 2)) \
473 inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
477 #define CHECK_LPOS() \
480 if (lpos >= _rl_screenwidth) \
482 if (newlines >= (inv_lbsize - 2)) \
485 inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
487 inv_lbreaks[++newlines] = out; \
492 /* inv_lbreaks[i] is where line i starts in the buffer. */
493 inv_lbreaks[newlines = 0] = 0;
494 lpos = out - wrap_offset;
496 /* prompt_invis_chars_first_line is the number of invisible characters in
497 the first physical line of the prompt.
498 wrap_offset - prompt_invis_chars_first_line is the number of invis
499 chars on the second line. */
501 /* what if lpos is already >= _rl_screenwidth before we start drawing the
502 contents of the command line? */
503 while (lpos >= _rl_screenwidth)
505 /* fix from Darin Johnson <darin@acuson.com> for prompt string with
506 invisible characters that is longer than the screen width. The
507 prompt_invis_chars_first_line variable could be made into an array
508 saying how many invisible characters there are per line, but that's
509 probably too much work for the benefit gained. How many people have
510 prompts that exceed two physical lines? */
511 temp = ((newlines + 1) * _rl_screenwidth) +
512 ((newlines == 0) ? prompt_invis_chars_first_line : 0) +
513 ((newlines == 1) ? wrap_offset : 0);
515 inv_lbreaks[++newlines] = temp;
516 lpos -= _rl_screenwidth;
519 prompt_last_screen_line = newlines;
521 /* Draw the rest of the line (after the prompt) into invisible_line, keeping
522 track of where the cursor is (c_pos), the number of the line containing
523 the cursor (lb_linenum), the last line number (lb_botlin and inv_botlin).
524 It maintains an array of line breaks for display (inv_lbreaks).
525 This handles expanding tabs for display and displaying meta characters. */
527 for (in = 0; in < rl_end; in++)
529 c = (unsigned char)rl_line_buffer[in];
531 if (out + 8 >= line_size) /* XXX - 8 for \t */
534 visible_line = xrealloc (visible_line, line_size);
535 invisible_line = xrealloc (invisible_line, line_size);
536 line = invisible_line;
542 lb_linenum = newlines;
547 if (_rl_output_meta_chars == 0)
549 sprintf (line + out, "\\%o", c);
551 if (lpos + 4 >= _rl_screenwidth)
553 temp = _rl_screenwidth - lpos;
554 CHECK_INV_LBREAKS ();
555 inv_lbreaks[++newlines] = out + temp;
569 #if defined (DISPLAY_TABS)
575 newout = (out | (int)7) + 1;
577 newout = out + 8 - lpos % 8;
580 if (lpos + temp >= _rl_screenwidth)
583 temp2 = _rl_screenwidth - lpos;
584 CHECK_INV_LBREAKS ();
585 inv_lbreaks[++newlines] = out + temp2;
598 else if (c == '\n' && _rl_horizontal_scroll_mode == 0 && _rl_term_up && *_rl_term_up)
600 line[out++] = '\0'; /* XXX - sentinel */
601 CHECK_INV_LBREAKS ();
602 inv_lbreaks[++newlines] = out;
605 else if (CTRL_CHAR (c) || c == RUBOUT)
609 line[out++] = CTRL_CHAR (c) ? UNCTRL (c) : '?';
622 lb_linenum = newlines;
625 inv_botlin = lb_botlin = newlines;
626 CHECK_INV_LBREAKS ();
627 inv_lbreaks[newlines+1] = out;
628 cursor_linenum = lb_linenum;
630 /* C_POS == position in buffer where cursor should be placed.
631 CURSOR_LINENUM == line number where the cursor should be placed. */
633 /* PWP: now is when things get a bit hairy. The visible and invisible
634 line buffers are really multiple lines, which would wrap every
635 (screenwidth - 1) characters. Go through each in turn, finding
636 the changed region and updating it. The line order is top to bottom. */
638 /* If we can move the cursor up and down, then use multiple lines,
639 otherwise, let long lines display in a single terminal line, and
640 horizontally scroll it. */
642 if (_rl_horizontal_scroll_mode == 0 && _rl_term_up && *_rl_term_up)
644 int nleft, pos, changed_screen_line;
646 if (!rl_display_fixed || forced_display)
650 /* If we have more than a screenful of material to display, then
651 only display a screenful. We should display the last screen,
653 if (out >= _rl_screenchars)
654 out = _rl_screenchars - 1;
656 /* The first line is at character position 0 in the buffer. The
657 second and subsequent lines start at inv_lbreaks[N], offset by
658 OFFSET (which has already been calculated above). */
660 #define W_OFFSET(line, offset) ((line) == 0 ? offset : 0)
661 #define VIS_LLEN(l) ((l) > _rl_vis_botlin ? 0 : (vis_lbreaks[l+1] - vis_lbreaks[l]))
662 #define INV_LLEN(l) (inv_lbreaks[l+1] - inv_lbreaks[l])
663 #define VIS_CHARS(line) (visible_line + vis_lbreaks[line])
664 #define VIS_LINE(line) ((line) > _rl_vis_botlin) ? "" : VIS_CHARS(line)
665 #define INV_LINE(line) (invisible_line + inv_lbreaks[line])
667 /* For each line in the buffer, do the updating display. */
668 for (linenum = 0; linenum <= inv_botlin; linenum++)
670 update_line (VIS_LINE(linenum), INV_LINE(linenum), linenum,
671 VIS_LLEN(linenum), INV_LLEN(linenum), inv_botlin);
673 /* If this is the line with the prompt, we might need to
674 compensate for invisible characters in the new line. Do
675 this only if there is not more than one new line (which
676 implies that we completely overwrite the old visible line)
677 and the new line is shorter than the old. Make sure we are
678 at the end of the new line before clearing. */
680 inv_botlin == 0 && _rl_last_c_pos == out &&
681 (wrap_offset > visible_wrap_offset) &&
682 (_rl_last_c_pos < visible_first_line_len))
684 nleft = _rl_screenwidth + wrap_offset - _rl_last_c_pos;
686 _rl_clear_to_eol (nleft);
689 /* Since the new first line is now visible, save its length. */
691 visible_first_line_len = (inv_botlin > 0) ? inv_lbreaks[1] : out - wrap_offset;
694 /* We may have deleted some lines. If so, clear the left over
695 blank ones at the bottom out. */
696 if (_rl_vis_botlin > inv_botlin)
699 for (; linenum <= _rl_vis_botlin; linenum++)
701 tt = VIS_CHARS (linenum);
702 _rl_move_vert (linenum);
703 _rl_move_cursor_relative (0, tt);
705 ((linenum == _rl_vis_botlin) ? strlen (tt) : _rl_screenwidth);
708 _rl_vis_botlin = inv_botlin;
710 /* CHANGED_SCREEN_LINE is set to 1 if we have moved to a
711 different screen line during this redisplay. */
712 changed_screen_line = _rl_last_v_pos != cursor_linenum;
713 if (changed_screen_line)
715 _rl_move_vert (cursor_linenum);
716 /* If we moved up to the line with the prompt using _rl_term_up,
717 the physical cursor position on the screen stays the same,
718 but the buffer position needs to be adjusted to account
719 for invisible characters. */
720 if (cursor_linenum == 0 && wrap_offset)
721 _rl_last_c_pos += wrap_offset;
724 /* We have to reprint the prompt if it contains invisible
725 characters, since it's not generally OK to just reprint
726 the characters from the current cursor position. But we
727 only need to reprint it if the cursor is before the last
728 invisible character in the prompt string. */
729 nleft = prompt_visible_length + wrap_offset;
730 if (cursor_linenum == 0 && wrap_offset > 0 && _rl_last_c_pos > 0 &&
731 _rl_last_c_pos <= prompt_last_invisible && local_prompt)
733 #if defined (__MSDOS__)
734 putc ('\r', rl_outstream);
737 tputs (_rl_term_cr, 1, _rl_output_character_function);
739 _rl_output_some_chars (local_prompt, nleft);
740 _rl_last_c_pos = nleft;
743 /* Where on that line? And where does that line start
745 pos = inv_lbreaks[cursor_linenum];
746 /* nleft == number of characters in the line buffer between the
747 start of the line and the cursor position. */
750 /* Since _rl_backspace() doesn't know about invisible characters in the
751 prompt, and there's no good way to tell it, we compensate for
752 those characters here and call _rl_backspace() directly. */
753 if (wrap_offset && cursor_linenum == 0 && nleft < _rl_last_c_pos)
755 _rl_backspace (_rl_last_c_pos - nleft);
756 _rl_last_c_pos = nleft;
759 if (nleft != _rl_last_c_pos)
760 _rl_move_cursor_relative (nleft, &invisible_line[pos]);
763 else /* Do horizontal scrolling. */
765 #define M_OFFSET(margin, offset) ((margin) == 0 ? offset : 0)
766 int lmargin, ndisp, nleft, phys_c_pos, t;
768 /* Always at top line. */
771 /* Compute where in the buffer the displayed line should start. This
774 /* The number of characters that will be displayed before the cursor. */
775 ndisp = c_pos - wrap_offset;
776 nleft = prompt_visible_length + wrap_offset;
777 /* Where the new cursor position will be on the screen. This can be
778 longer than SCREENWIDTH; if it is, lmargin will be adjusted. */
779 phys_c_pos = c_pos - (last_lmargin ? last_lmargin : wrap_offset);
780 t = _rl_screenwidth / 3;
782 /* If the number of characters had already exceeded the screenwidth,
783 last_lmargin will be > 0. */
785 /* If the number of characters to be displayed is more than the screen
786 width, compute the starting offset so that the cursor is about
787 two-thirds of the way across the screen. */
788 if (phys_c_pos > _rl_screenwidth - 2)
790 lmargin = c_pos - (2 * t);
793 /* If the left margin would be in the middle of a prompt with
794 invisible characters, don't display the prompt at all. */
795 if (wrap_offset && lmargin > 0 && lmargin < nleft)
798 else if (ndisp < _rl_screenwidth - 2) /* XXX - was -1 */
800 else if (phys_c_pos < 1)
802 /* If we are moving back towards the beginning of the line and
803 the last margin is no longer correct, compute a new one. */
804 lmargin = ((c_pos - 1) / t) * t; /* XXX */
805 if (wrap_offset && lmargin > 0 && lmargin < nleft)
809 lmargin = last_lmargin;
811 /* If the first character on the screen isn't the first character
812 in the display line, indicate this with a special character. */
816 /* If SCREENWIDTH characters starting at LMARGIN do not encompass
817 the whole line, indicate that with a special character at the
818 right edge of the screen. If LMARGIN is 0, we need to take the
819 wrap offset into account. */
820 t = lmargin + M_OFFSET (lmargin, wrap_offset) + _rl_screenwidth;
824 if (!rl_display_fixed || forced_display || lmargin != last_lmargin)
827 update_line (&visible_line[last_lmargin],
828 &invisible_line[lmargin],
830 _rl_screenwidth + visible_wrap_offset,
831 _rl_screenwidth + (lmargin ? 0 : wrap_offset),
834 /* If the visible new line is shorter than the old, but the number
835 of invisible characters is greater, and we are at the end of
836 the new line, we need to clear to eol. */
837 t = _rl_last_c_pos - M_OFFSET (lmargin, wrap_offset);
838 if ((M_OFFSET (lmargin, wrap_offset) > visible_wrap_offset) &&
839 (_rl_last_c_pos == out) &&
840 t < visible_first_line_len)
842 nleft = _rl_screenwidth - t;
843 _rl_clear_to_eol (nleft);
845 visible_first_line_len = out - lmargin - M_OFFSET (lmargin, wrap_offset);
846 if (visible_first_line_len > _rl_screenwidth)
847 visible_first_line_len = _rl_screenwidth;
849 _rl_move_cursor_relative (c_pos - lmargin, &invisible_line[lmargin]);
850 last_lmargin = lmargin;
853 fflush (rl_outstream);
855 /* Swap visible and non-visible lines. */
857 char *vtemp = visible_line;
858 int *itemp = vis_lbreaks, ntemp = vis_lbsize;
860 visible_line = invisible_line;
861 invisible_line = vtemp;
863 vis_lbreaks = inv_lbreaks;
866 vis_lbsize = inv_lbsize;
869 rl_display_fixed = 0;
870 /* If we are displaying on a single line, and last_lmargin is > 0, we
871 are not displaying any invisible characters, so set visible_wrap_offset
873 if (_rl_horizontal_scroll_mode && last_lmargin)
874 visible_wrap_offset = 0;
876 visible_wrap_offset = wrap_offset;
880 /* PWP: update_line() is based on finding the middle difference of each
881 line on the screen; vis:
883 /old first difference
884 /beginning of line | /old last same /old EOL
886 old: eddie> Oh, my little gruntle-buggy is to me, as lurgid as
887 new: eddie> Oh, my little buggy says to me, as lurgid as
889 \beginning of line | \new last same \new end of line
890 \new first difference
892 All are character pointers for the sake of speed. Special cases for
893 no differences, as well as for end of line additions must be handled.
895 Could be made even smarter, but this works well enough */
897 update_line (old, new, current_line, omax, nmax, inv_botlin)
898 register char *old, *new;
899 int current_line, omax, nmax, inv_botlin;
901 register char *ofd, *ols, *oe, *nfd, *nls, *ne;
902 int temp, lendiff, wsatend, od, nd;
903 int current_invis_chars;
905 /* If we're at the right edge of a terminal that supports xn, we're
906 ready to wrap around, so do so. This fixes problems with knowing
907 the exact cursor position and cut-and-paste with certain terminal
908 emulators. In this calculation, TEMP is the physical screen
909 position of the cursor. */
910 temp = _rl_last_c_pos - W_OFFSET(_rl_last_v_pos, visible_wrap_offset);
911 if (temp == _rl_screenwidth && _rl_term_autowrap && !_rl_horizontal_scroll_mode
912 && _rl_last_v_pos == current_line - 1)
915 putc (new[0], rl_outstream);
917 putc (' ', rl_outstream);
918 _rl_last_c_pos = 1; /* XXX */
920 if (old[0] && new[0])
924 /* Find first difference. */
925 for (ofd = old, nfd = new;
926 (ofd - old < omax) && *ofd && (*ofd == *nfd);
930 /* Move to the end of the screen line. ND and OD are used to keep track
931 of the distance between ne and new and oe and old, respectively, to
932 move a subtraction out of each loop. */
933 for (od = ofd - old, oe = ofd; od < omax && *oe; oe++, od++);
934 for (nd = nfd - new, ne = nfd; nd < nmax && *ne; ne++, nd++);
936 /* If no difference, continue to next line. */
937 if (ofd == oe && nfd == ne)
940 wsatend = 1; /* flag for trailing whitespace */
941 ols = oe - 1; /* find last same */
943 while ((ols > ofd) && (nls > nfd) && (*ols == *nls))
956 else if (*ols != *nls)
958 if (*ols) /* don't step past the NUL */
964 /* count of invisible characters in the current invisible line. */
965 current_invis_chars = W_OFFSET (current_line, wrap_offset);
966 if (_rl_last_v_pos != current_line)
968 _rl_move_vert (current_line);
969 if (current_line == 0 && visible_wrap_offset)
970 _rl_last_c_pos += visible_wrap_offset;
973 /* If this is the first line and there are invisible characters in the
974 prompt string, and the prompt string has not changed, and the current
975 cursor position is before the last invisible character in the prompt,
976 and the index of the character to move to is past the end of the prompt
977 string, then redraw the entire prompt string. We can only do this
978 reliably if the terminal supports a `cr' capability.
980 This is not an efficiency hack -- there is a problem with redrawing
981 portions of the prompt string if they contain terminal escape
982 sequences (like drawing the `unbold' sequence without a corresponding
983 `bold') that manifests itself on certain terminals. */
985 lendiff = local_prompt ? strlen (local_prompt) : 0;
986 od = ofd - old; /* index of first difference in visible line */
987 if (current_line == 0 && !_rl_horizontal_scroll_mode &&
988 _rl_term_cr && lendiff > prompt_visible_length && _rl_last_c_pos > 0 &&
989 od >= lendiff && _rl_last_c_pos <= prompt_last_invisible)
991 #if defined (__MSDOS__)
992 putc ('\r', rl_outstream);
994 tputs (_rl_term_cr, 1, _rl_output_character_function);
996 _rl_output_some_chars (local_prompt, lendiff);
997 _rl_last_c_pos = lendiff;
1000 _rl_move_cursor_relative (od, old);
1002 /* if (len (new) > len (old)) */
1003 lendiff = (nls - nfd) - (ols - ofd);
1005 /* If we are changing the number of invisible characters in a line, and
1006 the spot of first difference is before the end of the invisible chars,
1007 lendiff needs to be adjusted. */
1008 if (current_line == 0 && !_rl_horizontal_scroll_mode &&
1009 current_invis_chars != visible_wrap_offset)
1010 lendiff += visible_wrap_offset - current_invis_chars;
1012 /* Insert (diff (len (old), len (new)) ch. */
1016 /* Non-zero if we're increasing the number of lines. */
1017 int gl = current_line >= _rl_vis_botlin && inv_botlin > _rl_vis_botlin;
1018 /* Sometimes it is cheaper to print the characters rather than
1019 use the terminal's capabilities. If we're growing the number
1020 of lines, make sure we actually cause the new line to wrap
1021 around on auto-wrapping terminals. */
1022 if (_rl_terminal_can_insert && ((2 * temp) >= lendiff || _rl_term_IC) && (!_rl_term_autowrap || !gl))
1024 /* If lendiff > prompt_visible_length and _rl_last_c_pos == 0 and
1025 _rl_horizontal_scroll_mode == 1, inserting the characters with
1026 _rl_term_IC or _rl_term_ic will screw up the screen because of the
1027 invisible characters. We need to just draw them. */
1028 if (*ols && (!_rl_horizontal_scroll_mode || _rl_last_c_pos > 0 ||
1029 lendiff <= prompt_visible_length || !current_invis_chars))
1031 insert_some_chars (nfd, lendiff);
1032 _rl_last_c_pos += lendiff;
1036 /* At the end of a line the characters do not have to
1037 be "inserted". They can just be placed on the screen. */
1038 /* However, this screws up the rest of this block, which
1039 assumes you've done the insert because you can. */
1040 _rl_output_some_chars (nfd, lendiff);
1041 _rl_last_c_pos += lendiff;
1045 /* We have horizontal scrolling and we are not inserting at
1046 the end. We have invisible characters in this line. This
1047 is a dumb update. */
1048 _rl_output_some_chars (nfd, temp);
1049 _rl_last_c_pos += temp;
1052 /* Copy (new) chars to screen from first diff to last match. */
1054 if ((temp - lendiff) > 0)
1056 _rl_output_some_chars (nfd + lendiff, temp - lendiff);
1057 _rl_last_c_pos += temp - lendiff;
1062 /* cannot insert chars, write to EOL */
1063 _rl_output_some_chars (nfd, temp);
1064 _rl_last_c_pos += temp;
1067 else /* Delete characters from line. */
1069 /* If possible and inexpensive to use terminal deletion, then do so. */
1070 if (_rl_term_dc && (2 * temp) >= -lendiff)
1072 /* If all we're doing is erasing the invisible characters in the
1073 prompt string, don't bother. It screws up the assumptions
1074 about what's on the screen. */
1075 if (_rl_horizontal_scroll_mode && _rl_last_c_pos == 0 &&
1076 -lendiff == visible_wrap_offset)
1080 delete_chars (-lendiff); /* delete (diff) characters */
1082 /* Copy (new) chars to screen from first diff to last match */
1086 _rl_output_some_chars (nfd, temp);
1087 _rl_last_c_pos += temp;
1090 /* Otherwise, print over the existing material. */
1095 _rl_output_some_chars (nfd, temp);
1096 _rl_last_c_pos += temp;
1098 lendiff = (oe - old) - (ne - new);
1101 if (_rl_term_autowrap && current_line < inv_botlin)
1102 space_to_eol (lendiff);
1104 _rl_clear_to_eol (lendiff);
1110 /* Tell the update routines that we have moved onto a new (empty) line. */
1115 visible_line[0] = '\0';
1117 _rl_last_c_pos = _rl_last_v_pos = 0;
1118 _rl_vis_botlin = last_lmargin = 0;
1120 vis_lbreaks[0] = vis_lbreaks[1] = 0;
1121 visible_wrap_offset = 0;
1125 /* Tell the update routines that we have moved onto a new line with the
1126 prompt already displayed. Code originally from the version of readline
1127 distributed with CLISP. */
1129 rl_on_new_line_with_prompt ()
1131 int prompt_size, i, l, real_screenwidth, newlines;
1132 char *prompt_last_line;
1134 /* Initialize visible_line and invisible_line to ensure that they can hold
1135 the already-displayed prompt. */
1136 prompt_size = strlen (rl_prompt) + 1;
1137 init_line_structures (prompt_size);
1139 /* Make sure the line structures hold the already-displayed prompt for
1141 strcpy (visible_line, rl_prompt);
1142 strcpy (invisible_line, rl_prompt);
1144 /* If the prompt contains newlines, take the last tail. */
1145 prompt_last_line = strrchr (rl_prompt, '\n');
1146 if (!prompt_last_line)
1147 prompt_last_line = rl_prompt;
1149 l = strlen (prompt_last_line);
1152 /* Dissect prompt_last_line into screen lines. Note that here we have
1153 to use the real screenwidth. Readline's notion of screenwidth might be
1154 one less, see terminal.c. */
1155 real_screenwidth = _rl_screenwidth + (_rl_term_autowrap ? 0 : 1);
1156 _rl_last_v_pos = l / real_screenwidth;
1157 /* If the prompt length is a multiple of real_screenwidth, we don't know
1158 whether the cursor is at the end of the last line, or already at the
1159 beginning of the next line. Output a newline just to be safe. */
1160 if (l > 0 && (l % real_screenwidth) == 0)
1161 _rl_output_some_chars ("\n", 1);
1164 newlines = 0; i = 0;
1167 _rl_vis_botlin = newlines;
1168 vis_lbreaks[newlines++] = i;
1169 i += real_screenwidth;
1171 vis_lbreaks[newlines] = l;
1172 visible_wrap_offset = 0;
1177 /* Actually update the display, period. */
1179 rl_forced_update_display ()
1183 register char *temp = visible_line;
1190 (*rl_redisplay_function) ();
1194 /* Move the cursor from _rl_last_c_pos to NEW, which are buffer indices.
1195 DATA is the contents of the screen line of interest; i.e., where
1196 the movement is being done. */
1198 _rl_move_cursor_relative (new, data)
1204 /* If we don't have to do anything, then return. */
1205 if (_rl_last_c_pos == new) return;
1207 /* It may be faster to output a CR, and then move forwards instead
1208 of moving backwards. */
1209 /* i == current physical cursor position. */
1210 i = _rl_last_c_pos - W_OFFSET(_rl_last_v_pos, visible_wrap_offset);
1211 if (new == 0 || CR_FASTER (new, _rl_last_c_pos) ||
1212 (_rl_term_autowrap && i == _rl_screenwidth))
1214 #if defined (__MSDOS__)
1215 putc ('\r', rl_outstream);
1217 tputs (_rl_term_cr, 1, _rl_output_character_function);
1218 #endif /* !__MSDOS__ */
1222 if (_rl_last_c_pos < new)
1224 /* Move the cursor forward. We do it by printing the command
1225 to move the cursor forward if there is one, else print that
1226 portion of the output buffer again. Which is cheaper? */
1228 /* The above comment is left here for posterity. It is faster
1229 to print one character (non-control) than to print a control
1230 sequence telling the terminal to move forward one character.
1231 That kind of control is for people who don't know what the
1232 data is underneath the cursor. */
1233 #if defined (HACK_TERMCAP_MOTION)
1234 if (_rl_term_forward_char)
1235 for (i = _rl_last_c_pos; i < new; i++)
1236 tputs (_rl_term_forward_char, 1, _rl_output_character_function);
1238 for (i = _rl_last_c_pos; i < new; i++)
1239 putc (data[i], rl_outstream);
1241 for (i = _rl_last_c_pos; i < new; i++)
1242 putc (data[i], rl_outstream);
1243 #endif /* HACK_TERMCAP_MOTION */
1245 else if (_rl_last_c_pos > new)
1246 _rl_backspace (_rl_last_c_pos - new);
1247 _rl_last_c_pos = new;
1250 /* PWP: move the cursor up or down. */
1255 register int delta, i;
1257 if (_rl_last_v_pos == to || to > _rl_screenheight)
1260 if ((delta = to - _rl_last_v_pos) > 0)
1262 for (i = 0; i < delta; i++)
1263 putc ('\n', rl_outstream);
1264 #if defined (__MSDOS__)
1265 putc ('\r', rl_outstream);
1267 tputs (_rl_term_cr, 1, _rl_output_character_function);
1273 if (_rl_term_up && *_rl_term_up)
1274 for (i = 0; i < -delta; i++)
1275 tputs (_rl_term_up, 1, _rl_output_character_function);
1278 _rl_last_v_pos = to; /* Now TO is here */
1281 /* Physically print C on rl_outstream. This is for functions which know
1282 how to optimize the display. Return the number of characters output. */
1288 if (META_CHAR (c) && (_rl_output_meta_chars == 0))
1290 fprintf (rl_outstream, "M-");
1295 #if defined (DISPLAY_TABS)
1296 if ((CTRL_CHAR (c) && c != '\t') || c == RUBOUT)
1298 if (CTRL_CHAR (c) || c == RUBOUT)
1299 #endif /* !DISPLAY_TABS */
1301 fprintf (rl_outstream, "C-");
1303 c = CTRL_CHAR (c) ? UNCTRL (c) : '?';
1306 putc (c, rl_outstream);
1307 fflush (rl_outstream);
1312 rl_character_len (c, pos)
1313 register int c, pos;
1317 uc = (unsigned char)c;
1320 return ((_rl_output_meta_chars == 0) ? 4 : 1);
1324 #if defined (DISPLAY_TABS)
1325 return (((pos | 7) + 1) - pos);
1328 #endif /* !DISPLAY_TABS */
1331 if (CTRL_CHAR (c) || c == RUBOUT)
1334 return ((isprint (uc)) ? 1 : 2);
1337 /* How to print things in the "echo-area". The prompt is treated as a
1340 #if defined (USE_VARARGS)
1342 #if defined (PREFER_STDARG)
1343 rl_message (const char *format, ...)
1345 rl_message (va_alist)
1350 #if defined (PREFER_VARARGS)
1354 #if defined (PREFER_STDARG)
1355 va_start (args, format);
1358 format = va_arg (args, char *);
1361 vsprintf (msg_buf, format, args);
1364 rl_display_prompt = msg_buf;
1365 (*rl_redisplay_function) ();
1368 #else /* !USE_VARARGS */
1370 rl_message (format, arg1, arg2)
1373 sprintf (msg_buf, format, arg1, arg2);
1374 rl_display_prompt = msg_buf;
1375 (*rl_redisplay_function) ();
1378 #endif /* !USE_VARARGS */
1380 /* How to clear things from the "echo-area". */
1384 rl_display_prompt = rl_prompt;
1385 (*rl_redisplay_function) ();
1390 rl_reset_line_state ()
1394 rl_display_prompt = rl_prompt ? rl_prompt : "";
1399 static char *saved_local_prompt;
1400 static char *saved_local_prefix;
1401 static int saved_last_invisible;
1402 static int saved_visible_length;
1407 saved_local_prompt = local_prompt;
1408 saved_local_prefix = local_prompt_prefix;
1409 saved_last_invisible = prompt_last_invisible;
1410 saved_visible_length = prompt_visible_length;
1412 local_prompt = local_prompt_prefix = (char *)0;
1413 prompt_last_invisible = prompt_visible_length = 0;
1417 rl_restore_prompt ()
1419 FREE (local_prompt);
1420 FREE (local_prompt_prefix);
1422 local_prompt = saved_local_prompt;
1423 local_prompt_prefix = saved_local_prefix;
1424 prompt_last_invisible = saved_last_invisible;
1425 prompt_visible_length = saved_visible_length;
1429 _rl_make_prompt_for_search (pchar)
1437 if (saved_local_prompt == 0)
1439 len = (rl_prompt && *rl_prompt) ? strlen (rl_prompt) : 0;
1440 pmt = xmalloc (len + 2);
1442 strcpy (pmt, rl_prompt);
1448 len = *saved_local_prompt ? strlen (saved_local_prompt) : 0;
1449 pmt = xmalloc (len + 2);
1451 strcpy (pmt, saved_local_prompt);
1454 local_prompt = savestring (pmt);
1455 prompt_last_invisible = saved_last_invisible;
1456 prompt_visible_length = saved_visible_length + 1;
1461 /* Quick redisplay hack when erasing characters at the end of the line. */
1463 _rl_erase_at_end_of_line (l)
1469 for (i = 0; i < l; i++)
1470 putc (' ', rl_outstream);
1472 for (i = 0; i < l; i++)
1473 visible_line[--_rl_last_c_pos] = '\0';
1477 /* Clear to the end of the line. COUNT is the minimum
1478 number of character spaces to clear, */
1480 _rl_clear_to_eol (count)
1483 if (_rl_term_clreol)
1484 tputs (_rl_term_clreol, 1, _rl_output_character_function);
1486 space_to_eol (count);
1489 /* Clear to the end of the line using spaces. COUNT is the minimum
1490 number of character spaces to clear, */
1492 space_to_eol (count)
1497 for (i = 0; i < count; i++)
1498 putc (' ', rl_outstream);
1500 _rl_last_c_pos += count;
1506 if (_rl_term_clrpag)
1507 tputs (_rl_term_clrpag, 1, _rl_output_character_function);
1512 /* Insert COUNT characters from STRING to the output stream. */
1514 insert_some_chars (string, count)
1518 /* If IC is defined, then we do not have to "enter" insert mode. */
1522 buffer = tgoto (_rl_term_IC, 0, count);
1523 tputs (buffer, 1, _rl_output_character_function);
1524 _rl_output_some_chars (string, count);
1530 /* If we have to turn on insert-mode, then do so. */
1531 if (_rl_term_im && *_rl_term_im)
1532 tputs (_rl_term_im, 1, _rl_output_character_function);
1534 /* If there is a special command for inserting characters, then
1535 use that first to open up the space. */
1536 if (_rl_term_ic && *_rl_term_ic)
1538 for (i = count; i--; )
1539 tputs (_rl_term_ic, 1, _rl_output_character_function);
1542 /* Print the text. */
1543 _rl_output_some_chars (string, count);
1545 /* If there is a string to turn off insert mode, we had best use
1547 if (_rl_term_ei && *_rl_term_ei)
1548 tputs (_rl_term_ei, 1, _rl_output_character_function);
1552 /* Delete COUNT characters from the display line. */
1554 delete_chars (count)
1557 if (count > _rl_screenwidth) /* XXX */
1560 if (_rl_term_DC && *_rl_term_DC)
1563 buffer = tgoto (_rl_term_DC, count, count);
1564 tputs (buffer, count, _rl_output_character_function);
1568 if (_rl_term_dc && *_rl_term_dc)
1570 tputs (_rl_term_dc, 1, _rl_output_character_function);
1580 /* If the cursor is the only thing on an otherwise-blank last line,
1581 compensate so we don't print an extra CRLF. */
1582 if (_rl_vis_botlin && _rl_last_c_pos == 0 &&
1583 visible_line[vis_lbreaks[_rl_vis_botlin]] == 0)
1588 _rl_move_vert (_rl_vis_botlin);
1589 /* If we've wrapped lines, remove the final xterm line-wrap flag. */
1590 if (full_lines && _rl_term_autowrap && (VIS_LLEN(_rl_vis_botlin) == _rl_screenwidth))
1594 last_line = &visible_line[inv_lbreaks[_rl_vis_botlin]];
1596 last_line = &visible_line[vis_lbreaks[_rl_vis_botlin]];
1598 _rl_move_cursor_relative (_rl_screenwidth - 1, last_line);
1599 _rl_clear_to_eol (0);
1600 putc (last_line[_rl_screenwidth - 1], rl_outstream);
1604 fflush (rl_outstream);
1608 /* Move to the start of the current line. */
1614 #if defined (__MSDOS__)
1615 putc ('\r', rl_outstream);
1617 tputs (_rl_term_cr, 1, _rl_output_character_function);
1623 /* Redraw the last line of a multi-line prompt that may possibly contain
1624 terminal escape sequences. Called with the cursor at column 0 of the
1625 line to draw the prompt on. */
1630 char *oldp, *oldl, *oldlprefix;
1631 int oldlen, oldlast, oldplen, oldninvis;
1633 /* Geez, I should make this a struct. */
1634 oldp = rl_display_prompt;
1635 oldl = local_prompt;
1636 oldlprefix = local_prompt_prefix;
1637 oldlen = prompt_visible_length;
1638 oldplen = prompt_prefix_length;
1639 oldlast = prompt_last_invisible;
1640 oldninvis = prompt_invis_chars_first_line;
1642 rl_display_prompt = t;
1643 local_prompt = expand_prompt (t, &prompt_visible_length,
1644 &prompt_last_invisible,
1645 &prompt_invis_chars_first_line);
1646 local_prompt_prefix = (char *)NULL;
1647 rl_forced_update_display ();
1649 rl_display_prompt = oldp;
1650 local_prompt = oldl;
1651 local_prompt_prefix = oldlprefix;
1652 prompt_visible_length = oldlen;
1653 prompt_prefix_length = oldplen;
1654 prompt_last_invisible = oldlast;
1655 prompt_invis_chars_first_line = oldninvis;
1658 /* Redisplay the current line after a SIGWINCH is received. */
1660 _rl_redisplay_after_sigwinch ()
1664 /* Clear the current line and put the cursor at column 0. Make sure
1665 the right thing happens if we have wrapped to a new screen line. */
1668 #if defined (__MSDOS__)
1669 putc ('\r', rl_outstream);
1671 tputs (_rl_term_cr, 1, _rl_output_character_function);
1674 #if defined (__MSDOS__)
1675 space_to_eol (_rl_screenwidth);
1676 putc ('\r', rl_outstream);
1678 if (_rl_term_clreol)
1679 tputs (_rl_term_clreol, 1, _rl_output_character_function);
1682 space_to_eol (_rl_screenwidth);
1683 tputs (_rl_term_cr, 1, _rl_output_character_function);
1686 if (_rl_last_v_pos > 0)
1692 /* Redraw only the last line of a multi-line prompt. */
1693 t = strrchr (rl_display_prompt, '\n');
1695 redraw_prompt (++t);
1697 rl_forced_update_display ();
1701 _rl_clean_up_for_exit ()
1703 if (readline_echoing_p)
1705 _rl_move_vert (_rl_vis_botlin);
1707 fflush (rl_outstream);
1708 rl_restart_output (1, 0);
1713 _rl_erase_entire_line ()
1716 _rl_clear_to_eol (0);
1718 fflush (rl_outstream);
1721 /* return the `current display line' of the cursor -- the number of lines to
1722 move up to get to the first screen line of the current readline line. */
1724 _rl_current_display_line ()
1728 /* Find out whether or not there might be invisible characters in the
1730 if (rl_display_prompt == rl_prompt)
1731 nleft = _rl_last_c_pos - _rl_screenwidth - rl_visible_prompt_length;
1733 nleft = _rl_last_c_pos - _rl_screenwidth;
1736 ret = 1 + nleft / _rl_screenwidth;