1 /* display.c -- readline redisplay facility. */
3 /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
5 This file is part of the GNU Readline Library, a library for
6 reading lines of text with interactive input and history editing.
8 The GNU Readline Library is free software; you can redistribute it
9 and/or modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 1, or
11 (at your option) any later version.
13 The GNU Readline Library is distributed in the hope that it will be
14 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 The GNU General Public License is often shipped with GNU software, and
19 is generally kept in a file called COPYING or LICENSE. If you do not
20 have a copy of the license, write to the Free Software Foundation,
21 675 Mass Ave, Cambridge, MA 02139, USA. */
22 #define READLINE_LIBRARY
24 #if defined (HAVE_CONFIG_H)
29 #include <sys/types.h>
31 #if defined (HAVE_UNISTD_H)
33 #endif /* HAVE_UNISTD_H */
35 #if defined (HAVE_STDLIB_H)
38 # include "ansi_stdlib.h"
39 #endif /* HAVE_STDLIB_H */
41 #include "posixstat.h"
43 /* System-specific feature definitions and include files. */
46 /* Some standard library routines. */
50 #if !defined (strchr) && !defined (__STDC__)
51 extern char *strchr (), *strrchr ();
52 #endif /* !strchr && !__STDC__ */
54 /* Global and pseudo-global variables and functions
55 imported from readline.c. */
56 extern char *rl_prompt
;
57 extern int readline_echoing_p
;
58 extern char *term_clreol
, *term_im
, *term_ic
, *term_ei
, *term_DC
;
59 /* Termcap variables. */
60 extern char *term_up
, *term_dc
, *term_cr
, *term_IC
;
61 extern int screenheight
, screenwidth
, screenchars
;
62 extern int terminal_can_insert
, term_xn
;
64 extern void _rl_output_some_chars ();
65 extern int _rl_output_character_function ();
67 extern int _rl_output_meta_chars
;
68 extern int _rl_horizontal_scroll_mode
;
69 extern int _rl_mark_modified_lines
;
70 extern int _rl_prefer_visible_bell
;
72 /* Pseudo-global functions (local to the readline library) exported
74 void _rl_move_cursor_relative (), _rl_output_some_chars ();
75 void _rl_move_vert ();
77 static void update_line (), clear_to_eol (), space_to_eol ();
78 static void delete_chars (), insert_some_chars ();
80 extern char *xmalloc (), *xrealloc ();
82 /* Heuristic used to decide whether it is faster to move from CUR to NEW
83 by backing up or outputting a carriage return and moving forward. */
84 #define CR_FASTER(new, cur) (((new) + 1) < ((cur) - (new)))
86 /* **************************************************************** */
90 /* **************************************************************** */
92 /* This is the stuff that is hard for me. I never seem to write good
93 display routines in C. Let's see how I do this time. */
95 /* (PWP) Well... Good for a simple line updater, but totally ignores
96 the problems of input lines longer than the screen width.
98 update_line and the code that calls it makes a multiple line,
99 automatically wrapping line update. Careful attention needs
100 to be paid to the vertical position variables. */
102 /* Keep two buffers; one which reflects the current contents of the
103 screen, and the other to draw what we think the new contents should
104 be. Then compare the buffers, and make whatever changes to the
105 screen itself that we should. Finally, make the buffer that we
106 just drew into be the one which reflects the current contents of the
107 screen, and place the cursor where it belongs.
109 Commands that want to can fix the display themselves, and then let
110 this function know that the display has been fixed by setting the
111 RL_DISPLAY_FIXED variable. This is good for efficiency. */
113 /* Global variables declared here. */
114 /* What YOU turn on when you have handled all redisplay yourself. */
115 int rl_display_fixed
= 0;
117 /* The stuff that gets printed out before the actual text of the line.
118 This is usually pointing to rl_prompt. */
119 char *rl_display_prompt
= (char *)NULL
;
121 /* Pseudo-global variables declared here. */
122 /* The visible cursor position. If you print some text, adjust this. */
123 int _rl_last_c_pos
= 0;
124 int _rl_last_v_pos
= 0;
126 /* Number of lines currently on screen minus 1. */
127 int _rl_vis_botlin
= 0;
129 /* Variables used only in this file. */
130 /* The last left edge of text that was displayed. This is used when
131 doing horizontal scrolling. It shifts in thirds of a screenwidth. */
132 static int last_lmargin
= 0;
134 /* The line display buffers. One is the line currently displayed on
135 the screen. The other is the line about to be displayed. */
136 static char *visible_line
= (char *)NULL
;
137 static char *invisible_line
= (char *)NULL
;
139 /* A buffer for `modeline' messages. */
140 static char msg_buf
[128];
142 /* Non-zero forces the redisplay even if we thought it was unnecessary. */
143 static int forced_display
= 0;
145 /* Default and initial buffer size. Can grow. */
146 static int line_size
= 1024;
148 static char *last_prompt_string
= (char *)NULL
;
149 static char *local_prompt
, *local_prompt_prefix
;
150 static int visible_length
, prefix_length
;
152 /* The number of invisible characters in the line currently being
153 displayed on the screen. */
154 static int visible_wrap_offset
= 0;
156 /* The length (buffer offset) of the first line of the last (possibly
157 multi-line) buffer displayed on the screen. */
158 static int visible_first_line_len
= 0;
160 /* Expand the prompt string S and return the number of visible
161 characters in *LP, if LP is not null. This is currently more-or-less
162 a placeholder for expansion. */
164 /* Current implementation:
165 \001 (^A) start non-visible characters
166 \002 (^B) end non-visible characters
167 all characters except \001 and \002 (following a \001) are copied to
168 the returned string; all characters except those between \001 and
169 \002 are assumed to be `visible'. */
172 expand_prompt (pmt
, lp
)
179 /* Short-circuit if we can. */
180 if (strchr (pmt
, RL_PROMPT_START_IGNORE
) == 0)
182 r
= savestring (pmt
);
188 l
= pmt
? strlen (pmt
) : 0;
189 r
= ret
= xmalloc (l
+ 1);
191 for (rl
= ignoring
= 0, p
= pmt
; p
&& *p
; p
++)
193 /* This code strips the invisible character string markers
194 RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE */
195 if (*p
== RL_PROMPT_START_IGNORE
)
200 else if (ignoring
&& *p
== RL_PROMPT_END_IGNORE
)
220 * Expand the prompt string into the various display components, if
223 * local_prompt = expanded last line of string in rl_display_prompt
224 * (portion after the final newline)
225 * local_prompt_prefix = portion before last newline of rl_display_prompt,
226 * expanded via expand_prompt
227 * visible_length = number of visible characters in local_prompt
228 * prefix_length = number of visible characters in local_prompt_prefix
230 * This function is called once per call to readline(). It may also be
231 * called arbitrarily to expand the primary prompt.
233 * The return value is the number of visible characters on the last line
234 * of the (possibly multi-line) prompt.
237 rl_expand_prompt (prompt
)
243 /* Clear out any saved values. */
246 if (local_prompt_prefix
)
247 free (local_prompt_prefix
);
248 local_prompt
= local_prompt_prefix
= (char *)0;
250 if (prompt
== 0 || *prompt
== '\0')
253 p
= strrchr (prompt
, '\n');
256 /* The prompt is only one line. */
257 local_prompt
= expand_prompt (prompt
, &visible_length
);
258 local_prompt_prefix
= (char *)0;
259 return (visible_length
);
263 /* The prompt spans multiple lines. */
265 local_prompt
= expand_prompt (p
, &visible_length
);
267 /* The portion of the prompt string up to and including the
268 final newline is now null-terminated. */
269 local_prompt_prefix
= expand_prompt (prompt
, &prefix_length
);
271 return (prefix_length
);
275 /* Basic redisplay algorithm. */
279 register int in
, out
, c
, linenum
;
280 register char *line
= invisible_line
;
281 int c_pos
= 0, inv_botlin
= 0, wrap_offset
, wrap_column
;
282 char *prompt_this_line
;
284 if (!readline_echoing_p
)
287 if (!rl_display_prompt
)
288 rl_display_prompt
= "";
292 visible_line
= xmalloc (line_size
);
293 invisible_line
= xmalloc (line_size
);
294 line
= invisible_line
;
295 for (in
= 0; in
< line_size
; in
++)
297 visible_line
[in
] = 0;
298 invisible_line
[in
] = 1;
303 /* Draw the line into the buffer. */
306 /* Mark the line as modified or not. We only do this for history
309 if (_rl_mark_modified_lines
&& current_history () && rl_undo_list
)
315 /* If someone thought that the redisplay was handled, but the currently
316 visible line has a different modification state than the one about
317 to become visible, then correct the caller's misconception. */
318 if (visible_line
[0] != invisible_line
[0])
319 rl_display_fixed
= 0;
321 /* If the prompt to be displayed is the `primary' readline prompt (the
322 one passed to readline()), use the values we have already expanded.
323 If not, use what's already in rl_display_prompt. WRAP_OFFSET is the
324 number of non-visible characters in the prompt string. */
325 if (rl_display_prompt
== rl_prompt
)
327 int local_len
= local_prompt
? strlen (local_prompt
) : 0;
328 if (local_prompt_prefix
&& forced_display
)
329 _rl_output_some_chars (local_prompt_prefix
, strlen (local_prompt_prefix
));
332 strncpy (line
+ out
, local_prompt
, local_len
);
335 wrap_offset
= local_len
- visible_length
;
340 prompt_this_line
= strrchr (rl_display_prompt
, '\n');
341 if (!prompt_this_line
)
342 prompt_this_line
= rl_display_prompt
;
347 _rl_output_some_chars (rl_display_prompt
, prompt_this_line
- rl_display_prompt
);
350 pmtlen
= strlen (prompt_this_line
);
351 strncpy (line
+ out
, prompt_this_line
, pmtlen
);
357 for (in
= 0; in
< rl_end
; in
++)
359 c
= (unsigned char)rl_line_buffer
[in
];
361 if (out
+ 8 >= line_size
) /* XXX - 8 for \t */
364 visible_line
= xrealloc (visible_line
, line_size
);
365 invisible_line
= xrealloc (invisible_line
, line_size
);
366 line
= invisible_line
;
374 if (_rl_output_meta_chars
== 0)
376 sprintf (line
+ out
, "\\%o", c
);
382 #if defined (DISPLAY_TABS)
385 register int newout
= (out
| (int)7) + 1;
393 line
[out
++] = UNCTRL (c
); /* XXX was c ^ 0x40 */
407 /* C_POS == position in buffer where cursor should be placed. */
409 /* PWP: now is when things get a bit hairy. The visible and invisible
410 line buffers are really multiple lines, which would wrap every
411 (screenwidth - 1) characters. Go through each in turn, finding
412 the changed region and updating it. The line order is top to bottom. */
414 /* If we can move the cursor up and down, then use multiple lines,
415 otherwise, let long lines display in a single terminal line, and
416 horizontally scroll it. */
418 if (!_rl_horizontal_scroll_mode
&& term_up
&& *term_up
)
420 int total_screen_chars
= screenchars
;
421 int nleft
, cursor_linenum
, pos
, changed_screen_line
;
423 if (!rl_display_fixed
|| forced_display
)
427 /* If we have more than a screenful of material to display, then
428 only display a screenful. We should display the last screen,
429 not the first. I'll fix this in a minute. */
430 if (out
>= total_screen_chars
)
431 out
= total_screen_chars
- 1;
433 /* Number of screen lines to display. The first line wraps at
434 (screenwidth + wrap_offset) chars, the rest of the lines have
435 screenwidth chars. */
436 nleft
= out
- wrap_offset
+ term_xn
- 1;
437 inv_botlin
= (nleft
> 0) ? nleft
/ screenwidth
: 0;
439 /* The first line is at character position 0 in the buffer. The
440 second and subsequent lines start at N * screenwidth, offset by
441 OFFSET. OFFSET is wrap_offset for the invisible line and
442 visible_wrap_offset for the line currently displayed. */
444 #define W_OFFSET(line, offset) ((line) == 0 ? offset : 0)
445 #define L_OFFSET(n, offset) ((n) > 0 ? ((n) * screenwidth) + (offset) : 0)
446 #define VIS_CHARS(line) &visible_line[L_OFFSET((line), visible_wrap_offset)]
447 #define VIS_LINE(line) ((line) > _rl_vis_botlin) ? "" : VIS_CHARS(line)
448 #define INV_LINE(line) &invisible_line[L_OFFSET((line), wrap_offset)]
450 /* For each line in the buffer, do the updating display. */
451 for (linenum
= 0; linenum
<= inv_botlin
; linenum
++)
453 update_line (VIS_LINE(linenum
), INV_LINE(linenum
), linenum
,
454 screenwidth
+ W_OFFSET(linenum
, visible_wrap_offset
),
455 screenwidth
+ W_OFFSET(linenum
, wrap_offset
),
458 /* If this is the line with the prompt, we might need to
459 compensate for invisible characters in the new line. Do
460 this only if there is not more than one new line (which
461 implies that we completely overwrite the old visible line)
462 and the new line is shorter than the old. */
465 (wrap_offset
> visible_wrap_offset
) &&
466 (_rl_last_c_pos
< visible_first_line_len
))
468 nleft
= screenwidth
+ wrap_offset
- _rl_last_c_pos
;
470 clear_to_eol (nleft
);
473 /* Since the new first line is now visible, save its length. */
475 visible_first_line_len
= (inv_botlin
> 0) ? screenwidth
: out
- wrap_offset
;
478 /* We may have deleted some lines. If so, clear the left over
479 blank ones at the bottom out. */
480 if (_rl_vis_botlin
> inv_botlin
)
483 for (; linenum
<= _rl_vis_botlin
; linenum
++)
485 tt
= VIS_CHARS (linenum
);
486 _rl_move_vert (linenum
);
487 _rl_move_cursor_relative (0, tt
);
489 ((linenum
== _rl_vis_botlin
) ? strlen (tt
) : screenwidth
);
492 _rl_vis_botlin
= inv_botlin
;
494 /* Move the cursor where it should be. */
496 nleft
= c_pos
- wrap_offset
+ term_xn
- 1;
497 cursor_linenum
= (nleft
> 0) ? nleft
/ screenwidth
: 0;
499 /* CHANGED_SCREEN_LINE is set to 1 if we have moved to a
500 different screen line during this redisplay. */
501 changed_screen_line
= _rl_last_v_pos
!= cursor_linenum
;
502 if (changed_screen_line
)
504 _rl_move_vert (cursor_linenum
);
505 /* If we moved up to the line with the prompt using term_up,
506 the physical cursor position on the screen stays the same,
507 but the buffer position needs to be adjusted to account
508 for invisible characters. */
509 if (cursor_linenum
== 0 && wrap_offset
)
510 _rl_last_c_pos
+= wrap_offset
;
513 /* We have to reprint the prompt if it contains invisible
514 characters, since it's not generally OK to just reprint
515 the characters from the current cursor position. */
516 nleft
= visible_length
+ wrap_offset
;
517 if (cursor_linenum
== 0 && wrap_offset
> 0 && _rl_last_c_pos
> 0 &&
518 _rl_last_c_pos
<= nleft
&& local_prompt
)
521 tputs (term_cr
, 1, _rl_output_character_function
);
522 _rl_output_some_chars (local_prompt
, nleft
);
523 _rl_last_c_pos
= nleft
;
526 /* Where on that line? And where does that line start
528 pos
= L_OFFSET(cursor_linenum
, wrap_offset
);
529 /* nleft == number of characters in the line buffer between the
530 start of the line and the cursor position. */
533 /* Since backspace() doesn't know about invisible characters in the
534 prompt, and there's no good way to tell it, we compensate for
535 those characters here and call backspace() directly. */
536 if (wrap_offset
&& cursor_linenum
== 0 && nleft
< _rl_last_c_pos
)
538 backspace (_rl_last_c_pos
- nleft
);
539 _rl_last_c_pos
= nleft
;
542 if (nleft
!= _rl_last_c_pos
)
543 _rl_move_cursor_relative (nleft
, &invisible_line
[pos
]);
546 else /* Do horizontal scrolling. */
548 #define M_OFFSET(margin, offset) ((margin) == 0 ? offset : 0)
549 int lmargin
, ndisp
, nleft
, phys_c_pos
, t
;
551 /* Always at top line. */
554 /* Compute where in the buffer the displayed line should start. This
557 /* The number of characters that will be displayed before the cursor. */
558 ndisp
= c_pos
- wrap_offset
;
559 nleft
= visible_length
+ wrap_offset
;
560 /* Where the new cursor position will be on the screen. This can be
561 longer than SCREENWIDTH; if it is, lmargin will be adjusted. */
562 phys_c_pos
= c_pos
- (last_lmargin
? last_lmargin
: wrap_offset
);
565 /* If the number of characters had already exceeded the screenwidth,
566 last_lmargin will be > 0. */
568 /* If the number of characters to be displayed is more than the screen
569 width, compute the starting offset so that the cursor is about
570 two-thirds of the way across the screen. */
571 if (phys_c_pos
> screenwidth
- 2)
573 lmargin
= c_pos
- (2 * t
);
576 /* If the left margin would be in the middle of a prompt with
577 invisible characters, don't display the prompt at all. */
578 if (wrap_offset
&& lmargin
> 0 && lmargin
< nleft
)
581 else if (ndisp
< screenwidth
- 2) /* XXX - was -1 */
583 else if (phys_c_pos
< 1)
585 /* If we are moving back towards the beginning of the line and
586 the last margin is no longer correct, compute a new one. */
587 lmargin
= ((c_pos
- 1) / t
) * t
; /* XXX */
588 if (wrap_offset
&& lmargin
> 0 && lmargin
< nleft
)
592 lmargin
= last_lmargin
;
594 /* If the first character on the screen isn't the first character
595 in the display line, indicate this with a special character. */
599 /* If SCREENWIDTH characters starting at LMARGIN do not encompass
600 the whole line, indicate that with a special characters at the
601 right edge of the screen. If LMARGIN is 0, we need to take the
602 wrap offset into account. */
603 t
= lmargin
+ M_OFFSET (lmargin
, wrap_offset
) + screenwidth
;
607 if (!rl_display_fixed
|| forced_display
|| lmargin
!= last_lmargin
)
610 update_line (&visible_line
[last_lmargin
],
611 &invisible_line
[lmargin
],
613 screenwidth
+ visible_wrap_offset
,
614 screenwidth
+ (lmargin
? 0 : wrap_offset
),
617 /* If the visible new line is shorter than the old, but the number
618 of invisible characters is greater, and we are at the end of
619 the new line, we need to clear to eol. */
620 t
= _rl_last_c_pos
- M_OFFSET (lmargin
, wrap_offset
);
621 if ((M_OFFSET (lmargin
, wrap_offset
) > visible_wrap_offset
) &&
622 (_rl_last_c_pos
== out
) &&
623 t
< visible_first_line_len
)
625 nleft
= screenwidth
- t
;
626 clear_to_eol (nleft
);
628 visible_first_line_len
= out
- lmargin
- M_OFFSET (lmargin
, wrap_offset
);
629 if (visible_first_line_len
> screenwidth
)
630 visible_first_line_len
= screenwidth
;
632 _rl_move_cursor_relative (c_pos
- lmargin
, &invisible_line
[lmargin
]);
633 last_lmargin
= lmargin
;
636 fflush (rl_outstream
);
638 /* Swap visible and non-visible lines. */
640 char *temp
= visible_line
;
641 visible_line
= invisible_line
;
642 invisible_line
= temp
;
643 rl_display_fixed
= 0;
644 /* If we are displaying on a single line, and last_lmargin is > 0, we
645 are not displaying any invisible characters, so set visible_wrap_offset
647 if (_rl_horizontal_scroll_mode
&& last_lmargin
)
648 visible_wrap_offset
= 0;
650 visible_wrap_offset
= wrap_offset
;
654 /* PWP: update_line() is based on finding the middle difference of each
655 line on the screen; vis:
657 /old first difference
658 /beginning of line | /old last same /old EOL
660 old: eddie> Oh, my little gruntle-buggy is to me, as lurgid as
661 new: eddie> Oh, my little buggy says to me, as lurgid as
663 \beginning of line | \new last same \new end of line
664 \new first difference
666 All are character pointers for the sake of speed. Special cases for
667 no differences, as well as for end of line additions must be handeled.
669 Could be made even smarter, but this works well enough */
671 update_line (old
, new, current_line
, omax
, nmax
, inv_botlin
)
672 register char *old
, *new;
673 int current_line
, omax
, nmax
;
675 register char *ofd
, *ols
, *oe
, *nfd
, *nls
, *ne
;
676 int temp
, lendiff
, wsatend
, od
, nd
;
678 /* If we're at the right edge of a terminal that supports xn, we're
679 ready to wrap around, so do so. This fixes problems with knowing
680 the exact cursor position and cut-and-paste with certain terminal
681 emulators. In this calculation, TEMP is the physical screen
682 position of the cursor. */
683 temp
= _rl_last_c_pos
- W_OFFSET(_rl_last_v_pos
, visible_wrap_offset
);
684 if (temp
== screenwidth
&& term_xn
&& !_rl_horizontal_scroll_mode
685 && _rl_last_v_pos
== current_line
- 1)
688 putc (new[0], rl_outstream
);
690 putc (' ', rl_outstream
);
691 _rl_last_c_pos
= 1; /* XXX */
693 if (old
[0] && new[0])
697 /* Find first difference. */
698 for (ofd
= old
, nfd
= new;
699 (ofd
- old
< omax
) && *ofd
&& (*ofd
== *nfd
);
703 /* Move to the end of the screen line. ND and OD are used to keep track
704 of the distance between ne and new and oe and old, respectively, to
705 move a subtraction out of each loop. */
706 for (od
= ofd
- old
, oe
= ofd
; od
< omax
&& *oe
; oe
++, od
++);
707 for (nd
= nfd
- new, ne
= nfd
; nd
< nmax
&& *ne
; ne
++, nd
++);
709 /* If no difference, continue to next line. */
710 if (ofd
== oe
&& nfd
== ne
)
713 wsatend
= 1; /* flag for trailing whitespace */
714 ols
= oe
- 1; /* find last same */
716 while ((ols
> ofd
) && (nls
> nfd
) && (*ols
== *nls
))
729 else if (*ols
!= *nls
)
731 if (*ols
) /* don't step past the NUL */
737 _rl_move_vert (current_line
);
739 /* If this is the first line and there are invisible characters in the
740 prompt string, and the prompt string has not changed, then redraw
741 the entire prompt string. We can only do this reliably if the
742 terminal supports a `cr' capability.
744 This is more than just an efficiency hack -- there is a problem with
745 redrawing portions of the prompt string if they contain terminal
746 escape sequences (like drawing the `unbold' sequence without a
747 corresponding `bold') that manifests itself on certain terminals. */
749 lendiff
= local_prompt
? strlen (local_prompt
) : 0;
750 if (current_line
== 0 && !_rl_horizontal_scroll_mode
&&
751 lendiff
> visible_length
&&
752 _rl_last_c_pos
> 0 && (ofd
- old
) >= lendiff
&& term_cr
)
754 tputs (term_cr
, 1, _rl_output_character_function
);
755 _rl_output_some_chars (local_prompt
, lendiff
);
756 _rl_last_c_pos
= lendiff
;
759 _rl_move_cursor_relative (ofd
- old
, old
);
761 /* if (len (new) > len (old)) */
762 lendiff
= (nls
- nfd
) - (ols
- ofd
);
764 /* Insert (diff (len (old), len (new)) ch. */
768 /* Non-zero if we're increasing the number of lines. */
769 int gl
= current_line
>= _rl_vis_botlin
&& inv_botlin
> _rl_vis_botlin
;
770 /* Sometimes it is cheaper to print the characters rather than
771 use the terminal's capabilities. If we're growing the number
772 of lines, make sure we actually cause the new line to wrap
773 around on auto-wrapping terminals. */
774 if (terminal_can_insert
&& ((2 * temp
) >= lendiff
|| term_IC
) && (!term_xn
|| !gl
))
776 /* If lendiff > visible_length and _rl_last_c_pos == 0 and
777 _rl_horizontal_scroll_mode == 1, inserting the characters with
778 term_IC or term_ic will screw up the screen because of the
779 invisible characters. We need to just draw them. */
780 if (*ols
&& (!_rl_horizontal_scroll_mode
|| _rl_last_c_pos
> 0 ||
781 lendiff
<= visible_length
))
783 insert_some_chars (nfd
, lendiff
);
784 _rl_last_c_pos
+= lendiff
;
788 /* At the end of a line the characters do not have to
789 be "inserted". They can just be placed on the screen. */
790 _rl_output_some_chars (nfd
, lendiff
);
791 _rl_last_c_pos
+= lendiff
;
793 /* Copy (new) chars to screen from first diff to last match. */
795 if ((temp
- lendiff
) > 0)
797 _rl_output_some_chars (nfd
+ lendiff
, temp
- lendiff
);
798 _rl_last_c_pos
+= temp
- lendiff
;
803 /* cannot insert chars, write to EOL */
804 _rl_output_some_chars (nfd
, temp
);
805 _rl_last_c_pos
+= temp
;
808 else /* Delete characters from line. */
810 /* If possible and inexpensive to use terminal deletion, then do so. */
811 if (term_dc
&& (2 * temp
) >= -lendiff
)
813 /* If all we're doing is erasing the invisible characters in the
814 prompt string, don't bother. It screws up the assumptions
815 about what's on the screen. */
816 if (_rl_horizontal_scroll_mode
&& _rl_last_c_pos
== 0 &&
817 -lendiff
== visible_wrap_offset
)
821 delete_chars (-lendiff
); /* delete (diff) characters */
823 /* Copy (new) chars to screen from first diff to last match */
827 _rl_output_some_chars (nfd
, temp
);
828 _rl_last_c_pos
+= temp
;
831 /* Otherwise, print over the existing material. */
836 _rl_output_some_chars (nfd
, temp
);
837 _rl_last_c_pos
+= temp
;
839 lendiff
= (oe
- old
) - (ne
- new);
840 if (term_xn
&& current_line
< inv_botlin
)
841 space_to_eol (lendiff
);
843 clear_to_eol (lendiff
);
848 /* Tell the update routines that we have moved onto a new (empty) line. */
852 visible_line
[0] = '\0';
854 _rl_last_c_pos
= _rl_last_v_pos
= 0;
855 _rl_vis_botlin
= last_lmargin
= 0;
859 /* Actually update the display, period. */
860 rl_forced_update_display ()
864 register char *temp
= visible_line
;
866 while (*temp
) *temp
++ = '\0';
874 /* Move the cursor from _rl_last_c_pos to NEW, which are buffer indices.
875 DATA is the contents of the screen line of interest; i.e., where
876 the movement is being done. */
878 _rl_move_cursor_relative (new, data
)
884 /* If we don't have to do anything, then return. */
885 if (_rl_last_c_pos
== new) return;
887 /* It may be faster to output a CR, and then move forwards instead
888 of moving backwards. */
889 /* i == current physical cursor position. */
890 i
= _rl_last_c_pos
- W_OFFSET(_rl_last_v_pos
, visible_wrap_offset
);
891 if (CR_FASTER (new, _rl_last_c_pos
) || (term_xn
&& i
== screenwidth
))
893 #if defined (__MSDOS__)
894 putc ('\r', rl_outstream
);
896 tputs (term_cr
, 1, _rl_output_character_function
);
897 #endif /* !__MSDOS__ */
901 if (_rl_last_c_pos
< new)
903 /* Move the cursor forward. We do it by printing the command
904 to move the cursor forward if there is one, else print that
905 portion of the output buffer again. Which is cheaper? */
907 /* The above comment is left here for posterity. It is faster
908 to print one character (non-control) than to print a control
909 sequence telling the terminal to move forward one character.
910 That kind of control is for people who don't know what the
911 data is underneath the cursor. */
912 #if defined (HACK_TERMCAP_MOTION)
913 extern char *term_forward_char
;
915 if (term_forward_char
)
916 for (i
= _rl_last_c_pos
; i
< new; i
++)
917 tputs (term_forward_char
, 1, _rl_output_character_function
);
919 for (i
= _rl_last_c_pos
; i
< new; i
++)
920 putc (data
[i
], rl_outstream
);
922 for (i
= _rl_last_c_pos
; i
< new; i
++)
923 putc (data
[i
], rl_outstream
);
924 #endif /* HACK_TERMCAP_MOTION */
926 else if (_rl_last_c_pos
!= new)
927 backspace (_rl_last_c_pos
- new);
928 _rl_last_c_pos
= new;
931 /* PWP: move the cursor up or down. */
936 register int delta
, i
;
938 if (_rl_last_v_pos
== to
|| to
> screenheight
)
941 #if defined (__GO32__)
945 ScreenGetCursor (&row
, &col
);
946 ScreenSetCursor ((row
+ to
- _rl_last_v_pos
), col
);
948 #else /* !__GO32__ */
950 if ((delta
= to
- _rl_last_v_pos
) > 0)
952 for (i
= 0; i
< delta
; i
++)
953 putc ('\n', rl_outstream
);
954 tputs (term_cr
, 1, _rl_output_character_function
);
959 if (term_up
&& *term_up
)
960 for (i
= 0; i
< -delta
; i
++)
961 tputs (term_up
, 1, _rl_output_character_function
);
963 #endif /* !__GO32__ */
964 _rl_last_v_pos
= to
; /* Now TO is here */
967 /* Physically print C on rl_outstream. This is for functions which know
968 how to optimize the display. Return the number of characters output. */
973 if (META_CHAR (c
) && (_rl_output_meta_chars
== 0))
975 fprintf (rl_outstream
, "M-");
980 #if defined (DISPLAY_TABS)
981 if (c
< 32 && c
!= '\t')
984 #endif /* !DISPLAY_TABS */
986 fprintf (rl_outstream
, "C-");
991 putc (c
, rl_outstream
);
992 fflush (rl_outstream
);
997 rl_character_len (c
, pos
)
1002 uc
= (unsigned char)c
;
1005 return ((_rl_output_meta_chars
== 0) ? 4 : 1);
1009 #if defined (DISPLAY_TABS)
1010 return (((pos
| 7) + 1) - pos
);
1013 #endif /* !DISPLAY_TABS */
1016 return ((isprint (uc
)) ? 1 : 2);
1019 /* How to print things in the "echo-area". The prompt is treated as a
1022 #if defined (HAVE_VARARGS_H)
1023 rl_message (va_alist
)
1030 format
= va_arg (args
, char *);
1031 vsprintf (msg_buf
, format
, args
);
1034 rl_display_prompt
= msg_buf
;
1038 #else /* !HAVE_VARARGS_H */
1039 rl_message (format
, arg1
, arg2
)
1042 sprintf (msg_buf
, format
, arg1
, arg2
);
1043 rl_display_prompt
= msg_buf
;
1047 #endif /* !HAVE_VARARGS_H */
1049 /* How to clear things from the "echo-area". */
1052 rl_display_prompt
= rl_prompt
;
1057 rl_reset_line_state ()
1061 rl_display_prompt
= rl_prompt
? rl_prompt
: "";
1066 /* Quick redisplay hack when erasing characters at the end of the line. */
1068 _rl_erase_at_end_of_line (l
)
1074 for (i
= 0; i
< l
; i
++)
1075 putc (' ', rl_outstream
);
1077 for (i
= 0; i
< l
; i
++)
1078 visible_line
[--_rl_last_c_pos
] = '\0';
1082 /* Clear to the end of the line. COUNT is the minimum
1083 number of character spaces to clear, */
1085 clear_to_eol (count
)
1088 #if !defined (__GO32__)
1091 tputs (term_clreol
, 1, _rl_output_character_function
);
1094 #endif /* !__GO32__ */
1095 space_to_eol (count
);
1098 /* Clear to the end of the line using spaces. COUNT is the minimum
1099 number of character spaces to clear, */
1101 space_to_eol (count
)
1106 for (i
= 0; i
< count
; i
++)
1107 putc (' ', rl_outstream
);
1109 _rl_last_c_pos
+= count
;
1112 /* Insert COUNT characters from STRING to the output stream. */
1114 insert_some_chars (string
, count
)
1118 #if defined (__GO32__)
1119 int row
, col
, width
;
1122 ScreenGetCursor (&row
, &col
);
1123 width
= ScreenCols ();
1124 row_start
= ScreenPrimary
+ (row
* width
);
1126 memcpy (row_start
+ col
+ count
, row_start
+ col
, width
- col
- count
);
1128 /* Place the text on the screen. */
1129 _rl_output_some_chars (string
, count
);
1132 /* If IC is defined, then we do not have to "enter" insert mode. */
1135 char *tgoto (), *buffer
;
1136 buffer
= tgoto (term_IC
, 0, count
);
1137 tputs (buffer
, 1, _rl_output_character_function
);
1138 _rl_output_some_chars (string
, count
);
1144 /* If we have to turn on insert-mode, then do so. */
1145 if (term_im
&& *term_im
)
1146 tputs (term_im
, 1, _rl_output_character_function
);
1148 /* If there is a special command for inserting characters, then
1149 use that first to open up the space. */
1150 if (term_ic
&& *term_ic
)
1152 for (i
= count
; i
--; )
1153 tputs (term_ic
, 1, _rl_output_character_function
);
1156 /* Print the text. */
1157 _rl_output_some_chars (string
, count
);
1159 /* If there is a string to turn off insert mode, we had best use
1161 if (term_ei
&& *term_ei
)
1162 tputs (term_ei
, 1, _rl_output_character_function
);
1164 #endif /* !__GO32__ */
1167 /* Delete COUNT characters from the display line. */
1169 delete_chars (count
)
1172 #if defined (__GO32__)
1173 int row
, col
, width
;
1176 ScreenGetCursor (&row
, &col
);
1177 width
= ScreenCols ();
1178 row_start
= ScreenPrimary
+ (row
* width
);
1180 memcpy (row_start
+ col
, row_start
+ col
+ count
, width
- col
- count
);
1181 memset (row_start
+ width
- count
, 0, count
* 2);
1184 if (count
> screenwidth
) /* XXX */
1187 if (term_DC
&& *term_DC
)
1189 char *tgoto (), *buffer
;
1190 buffer
= tgoto (term_DC
, count
, count
);
1191 tputs (buffer
, count
, _rl_output_character_function
);
1195 if (term_dc
&& *term_dc
)
1197 tputs (term_dc
, 1, _rl_output_character_function
);
1199 #endif /* !__GO32__ */
1208 if (_rl_vis_botlin
&& visible_line
[screenwidth
* _rl_vis_botlin
] == 0)
1213 _rl_move_vert (_rl_vis_botlin
);
1214 if (full_lines
&& term_xn
)
1216 /* Remove final line-wrap flag in xterm. */
1218 last_line
= &visible_line
[screenwidth
* _rl_vis_botlin
];
1219 _rl_move_cursor_relative (screenwidth
- 1, last_line
);
1221 putc (last_line
[screenwidth
- 1], rl_outstream
);
1225 fflush (rl_outstream
);
1229 /* Move to the start of the current line. */
1235 tputs (term_cr
, 1, _rl_output_character_function
);
1240 /* Redisplay the current line after a SIGWINCH is received. */
1242 _rl_redisplay_after_sigwinch ()
1246 /* Clear the current line and put the cursor at column 0. Make sure
1247 the right thing happens if we have wrapped to a new screen line. */
1250 tputs (term_cr
, 1, _rl_output_character_function
);
1253 tputs (term_clreol
, 1, _rl_output_character_function
);
1256 space_to_eol (screenwidth
);
1257 tputs (term_cr
, 1, _rl_output_character_function
);
1259 if (_rl_last_v_pos
> 0)
1265 /* Redraw only the last line of a multi-line prompt. */
1266 t
= strrchr (rl_display_prompt
, '\n');
1269 oldp
= rl_display_prompt
;
1270 rl_display_prompt
= ++t
;
1271 rl_forced_update_display ();
1272 rl_display_prompt
= oldp
;
1275 rl_forced_update_display ();