1 /* $FreeBSD: src/contrib/libreadline/display.c,v 1.3.2.2 2000/07/06 23:04:23 ache Exp $ */
2 /* $DragonFly: src/contrib/libreadline/Attic/display.c,v 1.2 2003/06/17 04:24:03 dillon Exp $ */
3 /* display.c -- readline redisplay facility. */
5 /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
7 This file is part of the GNU Readline Library, a library for
8 reading lines of text with interactive input and history editing.
10 The GNU Readline Library is free software; you can redistribute it
11 and/or modify it under the terms of the GNU General Public License
12 as published by the Free Software Foundation; either version 2, or
13 (at your option) any later version.
15 The GNU Readline Library is distributed in the hope that it will be
16 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
17 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 The GNU General Public License is often shipped with GNU software, and
21 is generally kept in a file called COPYING or LICENSE. If you do not
22 have a copy of the license, write to the Free Software Foundation,
23 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
24 #define READLINE_LIBRARY
26 #if defined (HAVE_CONFIG_H)
30 #include <sys/types.h>
32 #if defined (HAVE_UNISTD_H)
34 #endif /* HAVE_UNISTD_H */
36 #include "posixstat.h"
38 #if defined (HAVE_STDLIB_H)
41 # include "ansi_stdlib.h"
42 #endif /* HAVE_STDLIB_H */
46 /* System-specific feature definitions and include files. */
49 /* Termcap library stuff. */
52 /* Some standard library routines. */
56 #include "rlprivate.h"
59 #if !defined (strchr) && !defined (__STDC__)
60 extern char *strchr (), *strrchr ();
61 #endif /* !strchr && !__STDC__ */
63 #if defined (HACK_TERMCAP_MOTION)
64 extern char *term_forward_char
;
67 static void update_line
__P((char *, char *, int, int, int, int));
68 static void space_to_eol
__P((int));
69 static void delete_chars
__P((int));
70 static void insert_some_chars
__P((char *, int));
71 static void cr
__P((void));
73 static int *inv_lbreaks
, *vis_lbreaks
;
74 static int inv_lbsize
, vis_lbsize
;
76 /* Heuristic used to decide whether it is faster to move from CUR to NEW
77 by backing up or outputting a carriage return and moving forward. */
78 #define CR_FASTER(new, cur) (((new) + 1) < ((cur) - (new)))
80 /* **************************************************************** */
84 /* **************************************************************** */
86 /* This is the stuff that is hard for me. I never seem to write good
87 display routines in C. Let's see how I do this time. */
89 /* (PWP) Well... Good for a simple line updater, but totally ignores
90 the problems of input lines longer than the screen width.
92 update_line and the code that calls it makes a multiple line,
93 automatically wrapping line update. Careful attention needs
94 to be paid to the vertical position variables. */
96 /* Keep two buffers; one which reflects the current contents of the
97 screen, and the other to draw what we think the new contents should
98 be. Then compare the buffers, and make whatever changes to the
99 screen itself that we should. Finally, make the buffer that we
100 just drew into be the one which reflects the current contents of the
101 screen, and place the cursor where it belongs.
103 Commands that want to can fix the display themselves, and then let
104 this function know that the display has been fixed by setting the
105 RL_DISPLAY_FIXED variable. This is good for efficiency. */
107 /* Application-specific redisplay function. */
108 VFunction
*rl_redisplay_function
= rl_redisplay
;
110 /* Global variables declared here. */
111 /* What YOU turn on when you have handled all redisplay yourself. */
112 int rl_display_fixed
= 0;
114 int _rl_suppress_redisplay
= 0;
116 /* The stuff that gets printed out before the actual text of the line.
117 This is usually pointing to rl_prompt. */
118 char *rl_display_prompt
= (char *)NULL
;
120 /* Pseudo-global variables declared here. */
121 /* The visible cursor position. If you print some text, adjust this. */
122 int _rl_last_c_pos
= 0;
123 int _rl_last_v_pos
= 0;
125 /* Number of lines currently on screen minus 1. */
126 int _rl_vis_botlin
= 0;
128 /* Variables used only in this file. */
129 /* The last left edge of text that was displayed. This is used when
130 doing horizontal scrolling. It shifts in thirds of a screenwidth. */
131 static int last_lmargin
;
133 /* The line display buffers. One is the line currently displayed on
134 the screen. The other is the line about to be displayed. */
135 static char *visible_line
= (char *)NULL
;
136 static char *invisible_line
= (char *)NULL
;
138 /* A buffer for `modeline' messages. */
139 static char msg_buf
[128];
141 /* Non-zero forces the redisplay even if we thought it was unnecessary. */
142 static int forced_display
;
144 /* Default and initial buffer size. Can grow. */
145 static int line_size
= 1024;
147 static char *local_prompt
, *local_prompt_prefix
;
148 static int visible_length
, prefix_length
;
150 /* The number of invisible characters in the line currently being
151 displayed on the screen. */
152 static int visible_wrap_offset
;
154 /* static so it can be shared between rl_redisplay and update_line */
155 static int wrap_offset
;
157 /* The index of the last invisible_character in the prompt string. */
158 static int last_invisible
;
160 /* The length (buffer offset) of the first line of the last (possibly
161 multi-line) buffer displayed on the screen. */
162 static int visible_first_line_len
;
164 /* Expand the prompt string S and return the number of visible
165 characters in *LP, if LP is not null. This is currently more-or-less
166 a placeholder for expansion. LIP, if non-null is a place to store the
167 index of the last invisible character in the returned string. */
169 /* Current implementation:
170 \001 (^A) start non-visible characters
171 \002 (^B) end non-visible characters
172 all characters except \001 and \002 (following a \001) are copied to
173 the returned string; all characters except those between \001 and
174 \002 are assumed to be `visible'. */
177 expand_prompt (pmt
, lp
, lip
)
182 int l
, rl
, last
, ignoring
;
184 /* Short-circuit if we can. */
185 if (strchr (pmt
, RL_PROMPT_START_IGNORE
) == 0)
187 r
= savestring (pmt
);
194 r
= ret
= xmalloc (l
+ 1);
196 for (rl
= ignoring
= last
= 0, p
= pmt
; p
&& *p
; p
++)
198 /* This code strips the invisible character string markers
199 RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE */
200 if (*p
== RL_PROMPT_START_IGNORE
)
205 else if (ignoring
&& *p
== RL_PROMPT_END_IGNORE
)
227 /* Just strip out RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE from
228 PMT and return the rest of PMT. */
230 _rl_strip_prompt (pmt
)
235 ret
= expand_prompt (pmt
, (int *)NULL
, (int *)NULL
);
240 * Expand the prompt string into the various display components, if
243 * local_prompt = expanded last line of string in rl_display_prompt
244 * (portion after the final newline)
245 * local_prompt_prefix = portion before last newline of rl_display_prompt,
246 * expanded via expand_prompt
247 * visible_length = number of visible characters in local_prompt
248 * prefix_length = number of visible characters in local_prompt_prefix
250 * This function is called once per call to readline(). It may also be
251 * called arbitrarily to expand the primary prompt.
253 * The return value is the number of visible characters on the last line
254 * of the (possibly multi-line) prompt.
257 rl_expand_prompt (prompt
)
263 /* Clear out any saved values. */
266 if (local_prompt_prefix
)
267 free (local_prompt_prefix
);
268 local_prompt
= local_prompt_prefix
= (char *)0;
269 last_invisible
= visible_length
= 0;
271 if (prompt
== 0 || *prompt
== 0)
274 p
= strrchr (prompt
, '\n');
277 /* The prompt is only one line. */
278 local_prompt
= expand_prompt (prompt
, &visible_length
, &last_invisible
);
279 local_prompt_prefix
= (char *)0;
280 return (visible_length
);
284 /* The prompt spans multiple lines. */
286 local_prompt
= expand_prompt (p
, &visible_length
, &last_invisible
);
288 /* The portion of the prompt string up to and including the
289 final newline is now null-terminated. */
290 local_prompt_prefix
= expand_prompt (prompt
, &prefix_length
, (int *)NULL
);
292 return (prefix_length
);
296 /* Initialize the VISIBLE_LINE and INVISIBLE_LINE arrays, and their associated
297 arrays of line break markers. MINSIZE is the minimum size of VISIBLE_LINE
298 and INVISIBLE_LINE; if it is greater than LINE_SIZE, LINE_SIZE is
299 increased. If the lines have already been allocated, this ensures that
300 they can hold at least MINSIZE characters. */
302 init_line_structures (minsize
)
307 if (invisible_line
== 0) /* initialize it */
309 if (line_size
< minsize
)
311 visible_line
= xmalloc (line_size
);
312 invisible_line
= xmalloc (line_size
);
314 else if (line_size
< minsize
) /* ensure it can hold MINSIZE chars */
317 if (line_size
< minsize
)
319 visible_line
= xrealloc (visible_line
, line_size
);
320 invisible_line
= xrealloc (invisible_line
, line_size
);
323 for (n
= minsize
; n
< line_size
; n
++)
326 invisible_line
[n
] = 1;
329 if (vis_lbreaks
== 0)
331 /* should be enough. */
332 inv_lbsize
= vis_lbsize
= 256;
333 inv_lbreaks
= (int *)xmalloc (inv_lbsize
* sizeof (int));
334 vis_lbreaks
= (int *)xmalloc (vis_lbsize
* sizeof (int));
335 inv_lbreaks
[0] = vis_lbreaks
[0] = 0;
339 /* Basic redisplay algorithm. */
343 register int in
, out
, c
, linenum
, cursor_linenum
;
345 int c_pos
, inv_botlin
, lb_botlin
, lb_linenum
;
346 int newlines
, lpos
, temp
;
347 char *prompt_this_line
;
349 if (!readline_echoing_p
)
352 if (!rl_display_prompt
)
353 rl_display_prompt
= "";
355 if (invisible_line
== 0)
357 init_line_structures (0);
361 /* Draw the line into the buffer. */
364 line
= invisible_line
;
365 out
= inv_botlin
= 0;
367 /* Mark the line as modified or not. We only do this for history
369 if (_rl_mark_modified_lines
&& current_history () && rl_undo_list
)
375 /* If someone thought that the redisplay was handled, but the currently
376 visible line has a different modification state than the one about
377 to become visible, then correct the caller's misconception. */
378 if (visible_line
[0] != invisible_line
[0])
379 rl_display_fixed
= 0;
381 /* If the prompt to be displayed is the `primary' readline prompt (the
382 one passed to readline()), use the values we have already expanded.
383 If not, use what's already in rl_display_prompt. WRAP_OFFSET is the
384 number of non-visible characters in the prompt string. */
385 if (rl_display_prompt
== rl_prompt
|| local_prompt
)
387 int local_len
= local_prompt
? strlen (local_prompt
) : 0;
388 if (local_prompt_prefix
&& forced_display
)
389 _rl_output_some_chars (local_prompt_prefix
, strlen (local_prompt_prefix
));
393 temp
= local_len
+ out
+ 2;
394 if (temp
>= line_size
)
396 line_size
= (temp
+ 1024) - (temp
% 1024);
397 visible_line
= xrealloc (visible_line
, line_size
);
398 line
= invisible_line
= xrealloc (invisible_line
, line_size
);
400 strncpy (line
+ out
, local_prompt
, local_len
);
404 wrap_offset
= local_len
- visible_length
;
409 prompt_this_line
= strrchr (rl_display_prompt
, '\n');
410 if (!prompt_this_line
)
411 prompt_this_line
= rl_display_prompt
;
415 pmtlen
= prompt_this_line
- rl_display_prompt
; /* temp var */
418 _rl_output_some_chars (rl_display_prompt
, pmtlen
);
419 /* Make sure we are at column zero even after a newline,
420 regardless of the state of terminal output processing. */
421 if (pmtlen
< 2 || prompt_this_line
[-2] != '\r')
426 pmtlen
= strlen (prompt_this_line
);
427 temp
= pmtlen
+ out
+ 2;
428 if (temp
>= line_size
)
430 line_size
= (temp
+ 1024) - (temp
% 1024);
431 visible_line
= xrealloc (visible_line
, line_size
);
432 line
= invisible_line
= xrealloc (invisible_line
, line_size
);
434 strncpy (line
+ out
, prompt_this_line
, pmtlen
);
440 #define CHECK_INV_LBREAKS() \
442 if (newlines >= (inv_lbsize - 2)) \
445 inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
449 #define CHECK_LPOS() \
452 if (lpos >= screenwidth) \
454 if (newlines >= (inv_lbsize - 2)) \
457 inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
459 inv_lbreaks[++newlines] = out; \
464 /* inv_lbreaks[i] is where line i starts in the buffer. */
465 inv_lbreaks
[newlines
= 0] = 0;
466 lpos
= out
- wrap_offset
;
468 /* XXX - what if lpos is already >= screenwidth before we start drawing the
469 contents of the command line? */
470 while (lpos
>= screenwidth
)
472 /* XXX - possible fix from Darin Johnson <darin@acuson.com> for prompt
473 string with invisible characters that is longer than the screen
474 width. XXX - this doesn't work right if invisible characters have
475 to be put on the second screen line -- it adds too much (the number
476 of invisible chars after the screenwidth). */
477 temp
= ((newlines
+ 1) * screenwidth
) + ((newlines
== 0) ? wrap_offset
: 0);
479 inv_lbreaks
[++newlines
] = temp
;
484 for (in
= 0; in
< rl_end
; in
++)
486 c
= (unsigned char)rl_line_buffer
[in
];
488 if (out
+ 8 >= line_size
) /* XXX - 8 for \t */
491 visible_line
= xrealloc (visible_line
, line_size
);
492 invisible_line
= xrealloc (invisible_line
, line_size
);
493 line
= invisible_line
;
499 lb_linenum
= newlines
;
504 if (_rl_output_meta_chars
== 0)
506 sprintf (line
+ out
, "\\%o", c
);
508 if (lpos
+ 4 >= screenwidth
)
510 temp
= screenwidth
- lpos
;
511 CHECK_INV_LBREAKS ();
512 inv_lbreaks
[++newlines
] = out
+ temp
;
526 #if defined (DISPLAY_TABS)
529 register int temp
, newout
;
532 newout
= (out
| (int)7) + 1;
534 newout
= out
+ 8 - lpos
% 8;
537 if (lpos
+ temp
>= screenwidth
)
540 temp2
= screenwidth
- lpos
;
541 CHECK_INV_LBREAKS ();
542 inv_lbreaks
[++newlines
] = out
+ temp2
;
555 else if (c
== '\n' && _rl_horizontal_scroll_mode
== 0 && term_up
&& *term_up
)
557 line
[out
++] = '\0'; /* XXX - sentinel */
558 CHECK_INV_LBREAKS ();
559 inv_lbreaks
[++newlines
] = out
;
562 else if (CTRL_CHAR (c
) || c
== RUBOUT
)
566 line
[out
++] = CTRL_CHAR (c
) ? UNCTRL (c
) : '?';
579 lb_linenum
= newlines
;
582 inv_botlin
= lb_botlin
= newlines
;
583 CHECK_INV_LBREAKS ();
584 inv_lbreaks
[newlines
+1] = out
;
585 cursor_linenum
= lb_linenum
;
587 /* C_POS == position in buffer where cursor should be placed. */
589 /* PWP: now is when things get a bit hairy. The visible and invisible
590 line buffers are really multiple lines, which would wrap every
591 (screenwidth - 1) characters. Go through each in turn, finding
592 the changed region and updating it. The line order is top to bottom. */
594 /* If we can move the cursor up and down, then use multiple lines,
595 otherwise, let long lines display in a single terminal line, and
596 horizontally scroll it. */
598 if (_rl_horizontal_scroll_mode
== 0 && term_up
&& *term_up
)
600 int nleft
, pos
, changed_screen_line
;
602 if (!rl_display_fixed
|| forced_display
)
606 /* If we have more than a screenful of material to display, then
607 only display a screenful. We should display the last screen,
609 if (out
>= screenchars
)
610 out
= screenchars
- 1;
612 /* The first line is at character position 0 in the buffer. The
613 second and subsequent lines start at inv_lbreaks[N], offset by
614 OFFSET (which has already been calculated above). */
616 #define W_OFFSET(line, offset) ((line) == 0 ? offset : 0)
617 #define VIS_LLEN(l) ((l) > _rl_vis_botlin ? 0 : (vis_lbreaks[l+1] - vis_lbreaks[l]))
618 #define INV_LLEN(l) (inv_lbreaks[l+1] - inv_lbreaks[l])
619 #define VIS_CHARS(line) (visible_line + vis_lbreaks[line])
620 #define VIS_LINE(line) ((line) > _rl_vis_botlin) ? "" : VIS_CHARS(line)
621 #define INV_LINE(line) (invisible_line + inv_lbreaks[line])
623 /* For each line in the buffer, do the updating display. */
624 for (linenum
= 0; linenum
<= inv_botlin
; linenum
++)
626 update_line (VIS_LINE(linenum
), INV_LINE(linenum
), linenum
,
627 VIS_LLEN(linenum
), INV_LLEN(linenum
), inv_botlin
);
629 /* If this is the line with the prompt, we might need to
630 compensate for invisible characters in the new line. Do
631 this only if there is not more than one new line (which
632 implies that we completely overwrite the old visible line)
633 and the new line is shorter than the old. Make sure we are
634 at the end of the new line before clearing. */
636 inv_botlin
== 0 && _rl_last_c_pos
== out
&&
637 (wrap_offset
> visible_wrap_offset
) &&
638 (_rl_last_c_pos
< visible_first_line_len
))
640 nleft
= screenwidth
+ wrap_offset
- _rl_last_c_pos
;
642 _rl_clear_to_eol (nleft
);
645 /* Since the new first line is now visible, save its length. */
647 visible_first_line_len
= (inv_botlin
> 0) ? inv_lbreaks
[1] : out
- wrap_offset
;
650 /* We may have deleted some lines. If so, clear the left over
651 blank ones at the bottom out. */
652 if (_rl_vis_botlin
> inv_botlin
)
655 for (; linenum
<= _rl_vis_botlin
; linenum
++)
657 tt
= VIS_CHARS (linenum
);
658 _rl_move_vert (linenum
);
659 _rl_move_cursor_relative (0, tt
);
661 ((linenum
== _rl_vis_botlin
) ? strlen (tt
) : screenwidth
);
664 _rl_vis_botlin
= inv_botlin
;
666 /* CHANGED_SCREEN_LINE is set to 1 if we have moved to a
667 different screen line during this redisplay. */
668 changed_screen_line
= _rl_last_v_pos
!= cursor_linenum
;
669 if (changed_screen_line
)
671 _rl_move_vert (cursor_linenum
);
672 /* If we moved up to the line with the prompt using term_up,
673 the physical cursor position on the screen stays the same,
674 but the buffer position needs to be adjusted to account
675 for invisible characters. */
676 if (cursor_linenum
== 0 && wrap_offset
)
677 _rl_last_c_pos
+= wrap_offset
;
680 /* We have to reprint the prompt if it contains invisible
681 characters, since it's not generally OK to just reprint
682 the characters from the current cursor position. But we
683 only need to reprint it if the cursor is before the last
684 invisible character in the prompt string. */
685 nleft
= visible_length
+ wrap_offset
;
686 if (cursor_linenum
== 0 && wrap_offset
> 0 && _rl_last_c_pos
> 0 &&
687 _rl_last_c_pos
<= last_invisible
&& local_prompt
)
689 #if defined (__MSDOS__)
690 putc ('\r', rl_outstream
);
693 tputs (term_cr
, 1, _rl_output_character_function
);
695 _rl_output_some_chars (local_prompt
, nleft
);
696 _rl_last_c_pos
= nleft
;
699 /* Where on that line? And where does that line start
701 pos
= inv_lbreaks
[cursor_linenum
];
702 /* nleft == number of characters in the line buffer between the
703 start of the line and the cursor position. */
706 /* Since _rl_backspace() doesn't know about invisible characters in the
707 prompt, and there's no good way to tell it, we compensate for
708 those characters here and call _rl_backspace() directly. */
709 if (wrap_offset
&& cursor_linenum
== 0 && nleft
< _rl_last_c_pos
)
711 _rl_backspace (_rl_last_c_pos
- nleft
);
712 _rl_last_c_pos
= nleft
;
715 if (nleft
!= _rl_last_c_pos
)
716 _rl_move_cursor_relative (nleft
, &invisible_line
[pos
]);
719 else /* Do horizontal scrolling. */
721 #define M_OFFSET(margin, offset) ((margin) == 0 ? offset : 0)
722 int lmargin
, ndisp
, nleft
, phys_c_pos
, t
;
724 /* Always at top line. */
727 /* Compute where in the buffer the displayed line should start. This
730 /* The number of characters that will be displayed before the cursor. */
731 ndisp
= c_pos
- wrap_offset
;
732 nleft
= visible_length
+ wrap_offset
;
733 /* Where the new cursor position will be on the screen. This can be
734 longer than SCREENWIDTH; if it is, lmargin will be adjusted. */
735 phys_c_pos
= c_pos
- (last_lmargin
? last_lmargin
: wrap_offset
);
738 /* If the number of characters had already exceeded the screenwidth,
739 last_lmargin will be > 0. */
741 /* If the number of characters to be displayed is more than the screen
742 width, compute the starting offset so that the cursor is about
743 two-thirds of the way across the screen. */
744 if (phys_c_pos
> screenwidth
- 2)
746 lmargin
= c_pos
- (2 * t
);
749 /* If the left margin would be in the middle of a prompt with
750 invisible characters, don't display the prompt at all. */
751 if (wrap_offset
&& lmargin
> 0 && lmargin
< nleft
)
754 else if (ndisp
< screenwidth
- 2) /* XXX - was -1 */
756 else if (phys_c_pos
< 1)
758 /* If we are moving back towards the beginning of the line and
759 the last margin is no longer correct, compute a new one. */
760 lmargin
= ((c_pos
- 1) / t
) * t
; /* XXX */
761 if (wrap_offset
&& lmargin
> 0 && lmargin
< nleft
)
765 lmargin
= last_lmargin
;
767 /* If the first character on the screen isn't the first character
768 in the display line, indicate this with a special character. */
772 /* If SCREENWIDTH characters starting at LMARGIN do not encompass
773 the whole line, indicate that with a special character at the
774 right edge of the screen. If LMARGIN is 0, we need to take the
775 wrap offset into account. */
776 t
= lmargin
+ M_OFFSET (lmargin
, wrap_offset
) + screenwidth
;
780 if (!rl_display_fixed
|| forced_display
|| lmargin
!= last_lmargin
)
783 update_line (&visible_line
[last_lmargin
],
784 &invisible_line
[lmargin
],
786 screenwidth
+ visible_wrap_offset
,
787 screenwidth
+ (lmargin
? 0 : wrap_offset
),
790 /* If the visible new line is shorter than the old, but the number
791 of invisible characters is greater, and we are at the end of
792 the new line, we need to clear to eol. */
793 t
= _rl_last_c_pos
- M_OFFSET (lmargin
, wrap_offset
);
794 if ((M_OFFSET (lmargin
, wrap_offset
) > visible_wrap_offset
) &&
795 (_rl_last_c_pos
== out
) &&
796 t
< visible_first_line_len
)
798 nleft
= screenwidth
- t
;
799 _rl_clear_to_eol (nleft
);
801 visible_first_line_len
= out
- lmargin
- M_OFFSET (lmargin
, wrap_offset
);
802 if (visible_first_line_len
> screenwidth
)
803 visible_first_line_len
= screenwidth
;
805 _rl_move_cursor_relative (c_pos
- lmargin
, &invisible_line
[lmargin
]);
806 last_lmargin
= lmargin
;
809 fflush (rl_outstream
);
811 /* Swap visible and non-visible lines. */
813 char *temp
= visible_line
;
814 int *itemp
= vis_lbreaks
, ntemp
= vis_lbsize
;
816 visible_line
= invisible_line
;
817 invisible_line
= temp
;
819 vis_lbreaks
= inv_lbreaks
;
822 vis_lbsize
= inv_lbsize
;
825 rl_display_fixed
= 0;
826 /* If we are displaying on a single line, and last_lmargin is > 0, we
827 are not displaying any invisible characters, so set visible_wrap_offset
829 if (_rl_horizontal_scroll_mode
&& last_lmargin
)
830 visible_wrap_offset
= 0;
832 visible_wrap_offset
= wrap_offset
;
836 /* PWP: update_line() is based on finding the middle difference of each
837 line on the screen; vis:
839 /old first difference
840 /beginning of line | /old last same /old EOL
842 old: eddie> Oh, my little gruntle-buggy is to me, as lurgid as
843 new: eddie> Oh, my little buggy says to me, as lurgid as
845 \beginning of line | \new last same \new end of line
846 \new first difference
848 All are character pointers for the sake of speed. Special cases for
849 no differences, as well as for end of line additions must be handled.
851 Could be made even smarter, but this works well enough */
853 update_line (old
, new, current_line
, omax
, nmax
, inv_botlin
)
854 register char *old
, *new;
855 int current_line
, omax
, nmax
, inv_botlin
;
857 register char *ofd
, *ols
, *oe
, *nfd
, *nls
, *ne
;
858 int temp
, lendiff
, wsatend
, od
, nd
;
859 int current_invis_chars
;
861 /* If we're at the right edge of a terminal that supports xn, we're
862 ready to wrap around, so do so. This fixes problems with knowing
863 the exact cursor position and cut-and-paste with certain terminal
864 emulators. In this calculation, TEMP is the physical screen
865 position of the cursor. */
866 temp
= _rl_last_c_pos
- W_OFFSET(_rl_last_v_pos
, visible_wrap_offset
);
867 if (temp
== screenwidth
&& _rl_term_autowrap
&& !_rl_horizontal_scroll_mode
868 && _rl_last_v_pos
== current_line
- 1)
871 putc (new[0], rl_outstream
);
873 putc (' ', rl_outstream
);
874 _rl_last_c_pos
= 1; /* XXX */
876 if (old
[0] && new[0])
880 /* Find first difference. */
881 for (ofd
= old
, nfd
= new;
882 (ofd
- old
< omax
) && *ofd
&& (*ofd
== *nfd
);
886 /* Move to the end of the screen line. ND and OD are used to keep track
887 of the distance between ne and new and oe and old, respectively, to
888 move a subtraction out of each loop. */
889 for (od
= ofd
- old
, oe
= ofd
; od
< omax
&& *oe
; oe
++, od
++);
890 for (nd
= nfd
- new, ne
= nfd
; nd
< nmax
&& *ne
; ne
++, nd
++);
892 /* If no difference, continue to next line. */
893 if (ofd
== oe
&& nfd
== ne
)
896 wsatend
= 1; /* flag for trailing whitespace */
897 ols
= oe
- 1; /* find last same */
899 while ((ols
> ofd
) && (nls
> nfd
) && (*ols
== *nls
))
912 else if (*ols
!= *nls
)
914 if (*ols
) /* don't step past the NUL */
920 /* count of invisible characters in the current invisible line. */
921 current_invis_chars
= W_OFFSET (current_line
, wrap_offset
);
922 if (_rl_last_v_pos
!= current_line
)
924 _rl_move_vert (current_line
);
925 if (current_line
== 0 && visible_wrap_offset
)
926 _rl_last_c_pos
+= visible_wrap_offset
;
929 /* If this is the first line and there are invisible characters in the
930 prompt string, and the prompt string has not changed, and the current
931 cursor position is before the last invisible character in the prompt,
932 and the index of the character to move to is past the end of the prompt
933 string, then redraw the entire prompt string. We can only do this
934 reliably if the terminal supports a `cr' capability.
936 This is not an efficiency hack -- there is a problem with redrawing
937 portions of the prompt string if they contain terminal escape
938 sequences (like drawing the `unbold' sequence without a corresponding
939 `bold') that manifests itself on certain terminals. */
941 lendiff
= local_prompt
? strlen (local_prompt
) : 0;
942 od
= ofd
- old
; /* index of first difference in visible line */
943 if (current_line
== 0 && !_rl_horizontal_scroll_mode
&&
944 term_cr
&& lendiff
> visible_length
&& _rl_last_c_pos
> 0 &&
945 od
> lendiff
&& _rl_last_c_pos
< last_invisible
)
947 #if defined (__MSDOS__)
948 putc ('\r', rl_outstream
);
950 tputs (term_cr
, 1, _rl_output_character_function
);
952 _rl_output_some_chars (local_prompt
, lendiff
);
953 _rl_last_c_pos
= lendiff
;
956 _rl_move_cursor_relative (od
, old
);
958 /* if (len (new) > len (old)) */
959 lendiff
= (nls
- nfd
) - (ols
- ofd
);
961 /* If we are changing the number of invisible characters in a line, and
962 the spot of first difference is before the end of the invisible chars,
963 lendiff needs to be adjusted. */
964 if (current_line
== 0 && !_rl_horizontal_scroll_mode
&&
965 current_invis_chars
!= visible_wrap_offset
)
966 lendiff
+= visible_wrap_offset
- current_invis_chars
;
968 /* Insert (diff (len (old), len (new)) ch. */
972 /* Non-zero if we're increasing the number of lines. */
973 int gl
= current_line
>= _rl_vis_botlin
&& inv_botlin
> _rl_vis_botlin
;
974 /* Sometimes it is cheaper to print the characters rather than
975 use the terminal's capabilities. If we're growing the number
976 of lines, make sure we actually cause the new line to wrap
977 around on auto-wrapping terminals. */
978 if (terminal_can_insert
&& ((2 * temp
) >= lendiff
|| term_IC
) && (!_rl_term_autowrap
|| !gl
))
980 /* If lendiff > visible_length and _rl_last_c_pos == 0 and
981 _rl_horizontal_scroll_mode == 1, inserting the characters with
982 term_IC or term_ic will screw up the screen because of the
983 invisible characters. We need to just draw them. */
984 if (*ols
&& (!_rl_horizontal_scroll_mode
|| _rl_last_c_pos
> 0 ||
985 lendiff
<= visible_length
|| !current_invis_chars
))
987 insert_some_chars (nfd
, lendiff
);
988 _rl_last_c_pos
+= lendiff
;
992 /* At the end of a line the characters do not have to
993 be "inserted". They can just be placed on the screen. */
994 /* However, this screws up the rest of this block, which
995 assumes you've done the insert because you can. */
996 _rl_output_some_chars (nfd
, lendiff
);
997 _rl_last_c_pos
+= lendiff
;
1001 /* We have horizontal scrolling and we are not inserting at
1002 the end. We have invisible characters in this line. This
1003 is a dumb update. */
1004 _rl_output_some_chars (nfd
, temp
);
1005 _rl_last_c_pos
+= temp
;
1008 /* Copy (new) chars to screen from first diff to last match. */
1010 if ((temp
- lendiff
) > 0)
1012 _rl_output_some_chars (nfd
+ lendiff
, temp
- lendiff
);
1013 _rl_last_c_pos
+= temp
- lendiff
;
1018 /* cannot insert chars, write to EOL */
1019 _rl_output_some_chars (nfd
, temp
);
1020 _rl_last_c_pos
+= temp
;
1023 else /* Delete characters from line. */
1025 /* If possible and inexpensive to use terminal deletion, then do so. */
1026 if (term_dc
&& (2 * temp
) >= -lendiff
)
1028 /* If all we're doing is erasing the invisible characters in the
1029 prompt string, don't bother. It screws up the assumptions
1030 about what's on the screen. */
1031 if (_rl_horizontal_scroll_mode
&& _rl_last_c_pos
== 0 &&
1032 -lendiff
== visible_wrap_offset
)
1036 delete_chars (-lendiff
); /* delete (diff) characters */
1038 /* Copy (new) chars to screen from first diff to last match */
1042 _rl_output_some_chars (nfd
, temp
);
1043 _rl_last_c_pos
+= temp
;
1046 /* Otherwise, print over the existing material. */
1051 _rl_output_some_chars (nfd
, temp
);
1052 _rl_last_c_pos
+= temp
;
1054 lendiff
= (oe
- old
) - (ne
- new);
1057 if (_rl_term_autowrap
&& current_line
< inv_botlin
)
1058 space_to_eol (lendiff
);
1060 _rl_clear_to_eol (lendiff
);
1066 /* Tell the update routines that we have moved onto a new (empty) line. */
1071 visible_line
[0] = '\0';
1073 _rl_last_c_pos
= _rl_last_v_pos
= 0;
1074 _rl_vis_botlin
= last_lmargin
= 0;
1076 vis_lbreaks
[0] = vis_lbreaks
[1] = 0;
1077 visible_wrap_offset
= 0;
1081 /* Tell the update routines that we have moved onto a new line with the
1082 prompt already displayed. Code originally from the version of readline
1083 distributed with CLISP. */
1085 rl_on_new_line_with_prompt ()
1087 int prompt_size
, i
, l
, real_screenwidth
, newlines
;
1088 char *prompt_last_line
;
1090 /* Initialize visible_line and invisible_line to ensure that they can hold
1091 the already-displayed prompt. */
1092 prompt_size
= strlen (rl_prompt
) + 1;
1093 init_line_structures (prompt_size
);
1095 /* Make sure the line structures hold the already-displayed prompt for
1097 strcpy (visible_line
, rl_prompt
);
1098 strcpy (invisible_line
, rl_prompt
);
1100 /* If the prompt contains newlines, take the last tail. */
1101 prompt_last_line
= strrchr (rl_prompt
, '\n');
1102 if (!prompt_last_line
)
1103 prompt_last_line
= rl_prompt
;
1105 l
= strlen (prompt_last_line
);
1108 /* Dissect prompt_last_line into screen lines. Note that here we have
1109 to use the real screenwidth. Readline's notion of screenwidth might be
1110 one less, see terminal.c. */
1111 real_screenwidth
= screenwidth
+ (_rl_term_autowrap
? 0 : 1);
1112 _rl_last_v_pos
= l
/ real_screenwidth
;
1113 /* If the prompt length is a multiple of real_screenwidth, we don't know
1114 whether the cursor is at the end of the last line, or already at the
1115 beginning of the next line. Output a newline just to be safe. */
1116 if (l
> 0 && (l
% real_screenwidth
) == 0)
1117 _rl_output_some_chars ("\n", 1);
1120 newlines
= 0; i
= 0;
1123 _rl_vis_botlin
= newlines
;
1124 vis_lbreaks
[newlines
++] = i
;
1125 i
+= real_screenwidth
;
1127 vis_lbreaks
[newlines
] = l
;
1128 visible_wrap_offset
= 0;
1133 /* Actually update the display, period. */
1135 rl_forced_update_display ()
1139 register char *temp
= visible_line
;
1146 (*rl_redisplay_function
) ();
1150 /* Move the cursor from _rl_last_c_pos to NEW, which are buffer indices.
1151 DATA is the contents of the screen line of interest; i.e., where
1152 the movement is being done. */
1154 _rl_move_cursor_relative (new, data
)
1160 /* If we don't have to do anything, then return. */
1161 if (_rl_last_c_pos
== new) return;
1163 /* It may be faster to output a CR, and then move forwards instead
1164 of moving backwards. */
1165 /* i == current physical cursor position. */
1166 i
= _rl_last_c_pos
- W_OFFSET(_rl_last_v_pos
, visible_wrap_offset
);
1167 if (new == 0 || CR_FASTER (new, _rl_last_c_pos
) ||
1168 (_rl_term_autowrap
&& i
== screenwidth
))
1170 #if defined (__MSDOS__)
1171 putc ('\r', rl_outstream
);
1173 tputs (term_cr
, 1, _rl_output_character_function
);
1174 #endif /* !__MSDOS__ */
1178 if (_rl_last_c_pos
< new)
1180 /* Move the cursor forward. We do it by printing the command
1181 to move the cursor forward if there is one, else print that
1182 portion of the output buffer again. Which is cheaper? */
1184 /* The above comment is left here for posterity. It is faster
1185 to print one character (non-control) than to print a control
1186 sequence telling the terminal to move forward one character.
1187 That kind of control is for people who don't know what the
1188 data is underneath the cursor. */
1189 #if defined (HACK_TERMCAP_MOTION)
1190 if (term_forward_char
)
1191 for (i
= _rl_last_c_pos
; i
< new; i
++)
1192 tputs (term_forward_char
, 1, _rl_output_character_function
);
1194 for (i
= _rl_last_c_pos
; i
< new; i
++)
1195 putc (data
[i
], rl_outstream
);
1197 for (i
= _rl_last_c_pos
; i
< new; i
++)
1198 putc (data
[i
], rl_outstream
);
1199 #endif /* HACK_TERMCAP_MOTION */
1201 else if (_rl_last_c_pos
> new)
1202 _rl_backspace (_rl_last_c_pos
- new);
1203 _rl_last_c_pos
= new;
1206 /* PWP: move the cursor up or down. */
1211 register int delta
, i
;
1213 if (_rl_last_v_pos
== to
|| to
> screenheight
)
1216 if ((delta
= to
- _rl_last_v_pos
) > 0)
1218 for (i
= 0; i
< delta
; i
++)
1219 putc ('\n', rl_outstream
);
1220 #if defined (__MSDOS__)
1221 putc ('\r', rl_outstream
);
1223 tputs (term_cr
, 1, _rl_output_character_function
);
1229 if (term_up
&& *term_up
)
1230 for (i
= 0; i
< -delta
; i
++)
1231 tputs (term_up
, 1, _rl_output_character_function
);
1234 _rl_last_v_pos
= to
; /* Now TO is here */
1237 /* Physically print C on rl_outstream. This is for functions which know
1238 how to optimize the display. Return the number of characters output. */
1244 if (META_CHAR (c
) && (_rl_output_meta_chars
== 0))
1246 fprintf (rl_outstream
, "M-");
1251 #if defined (DISPLAY_TABS)
1252 if ((CTRL_CHAR (c
) && c
!= '\t') || c
== RUBOUT
)
1254 if (CTRL_CHAR (c
) || c
== RUBOUT
)
1255 #endif /* !DISPLAY_TABS */
1257 fprintf (rl_outstream
, "C-");
1259 c
= CTRL_CHAR (c
) ? UNCTRL (c
) : '?';
1262 putc (c
, rl_outstream
);
1263 fflush (rl_outstream
);
1268 rl_character_len (c
, pos
)
1269 register int c
, pos
;
1273 uc
= (unsigned char)c
;
1276 return ((_rl_output_meta_chars
== 0) ? 4 : 1);
1280 #if defined (DISPLAY_TABS)
1281 return (((pos
| 7) + 1) - pos
);
1284 #endif /* !DISPLAY_TABS */
1287 if (CTRL_CHAR (c
) || c
== RUBOUT
)
1290 return ((isprint (uc
)) ? 1 : 2);
1293 /* How to print things in the "echo-area". The prompt is treated as a
1296 #if defined (USE_VARARGS)
1298 #if defined (PREFER_STDARG)
1299 rl_message (const char *format
, ...)
1301 rl_message (va_alist
)
1306 #if defined (PREFER_VARARGS)
1310 #if defined (PREFER_STDARG)
1311 va_start (args
, format
);
1314 format
= va_arg (args
, char *);
1317 vsprintf (msg_buf
, format
, args
);
1320 rl_display_prompt
= msg_buf
;
1321 (*rl_redisplay_function
) ();
1324 #else /* !USE_VARARGS */
1326 rl_message (format
, arg1
, arg2
)
1329 sprintf (msg_buf
, format
, arg1
, arg2
);
1330 rl_display_prompt
= msg_buf
;
1331 (*rl_redisplay_function
) ();
1334 #endif /* !USE_VARARGS */
1336 /* How to clear things from the "echo-area". */
1340 rl_display_prompt
= rl_prompt
;
1341 (*rl_redisplay_function
) ();
1346 rl_reset_line_state ()
1350 rl_display_prompt
= rl_prompt
? rl_prompt
: "";
1355 static char *saved_local_prompt
;
1356 static char *saved_local_prefix
;
1357 static int saved_last_invisible
;
1358 static int saved_visible_length
;
1363 saved_local_prompt
= local_prompt
;
1364 saved_local_prefix
= local_prompt_prefix
;
1365 saved_last_invisible
= last_invisible
;
1366 saved_visible_length
= visible_length
;
1368 local_prompt
= local_prompt_prefix
= (char *)0;
1369 last_invisible
= visible_length
= 0;
1373 rl_restore_prompt ()
1376 free (local_prompt
);
1377 if (local_prompt_prefix
)
1378 free (local_prompt_prefix
);
1380 local_prompt
= saved_local_prompt
;
1381 local_prompt_prefix
= saved_local_prefix
;
1382 last_invisible
= saved_last_invisible
;
1383 visible_length
= saved_visible_length
;
1387 _rl_make_prompt_for_search (pchar
)
1395 if (saved_local_prompt
== 0)
1397 len
= (rl_prompt
&& *rl_prompt
) ? strlen (rl_prompt
) : 0;
1398 pmt
= xmalloc (len
+ 2);
1400 strcpy (pmt
, rl_prompt
);
1406 len
= *saved_local_prompt
? strlen (saved_local_prompt
) : 0;
1407 pmt
= xmalloc (len
+ 2);
1409 strcpy (pmt
, saved_local_prompt
);
1412 local_prompt
= savestring (pmt
);
1413 last_invisible
= saved_last_invisible
;
1414 visible_length
= saved_visible_length
+ 1;
1419 /* Quick redisplay hack when erasing characters at the end of the line. */
1421 _rl_erase_at_end_of_line (l
)
1427 for (i
= 0; i
< l
; i
++)
1428 putc (' ', rl_outstream
);
1430 for (i
= 0; i
< l
; i
++)
1431 visible_line
[--_rl_last_c_pos
] = '\0';
1435 /* Clear to the end of the line. COUNT is the minimum
1436 number of character spaces to clear, */
1438 _rl_clear_to_eol (count
)
1442 tputs (term_clreol
, 1, _rl_output_character_function
);
1444 space_to_eol (count
);
1447 /* Clear to the end of the line using spaces. COUNT is the minimum
1448 number of character spaces to clear, */
1450 space_to_eol (count
)
1455 for (i
= 0; i
< count
; i
++)
1456 putc (' ', rl_outstream
);
1458 _rl_last_c_pos
+= count
;
1465 tputs (term_clrpag
, 1, _rl_output_character_function
);
1470 /* Insert COUNT characters from STRING to the output stream. */
1472 insert_some_chars (string
, count
)
1476 /* If IC is defined, then we do not have to "enter" insert mode. */
1480 buffer
= tgoto (term_IC
, 0, count
);
1481 tputs (buffer
, 1, _rl_output_character_function
);
1482 _rl_output_some_chars (string
, count
);
1488 /* If we have to turn on insert-mode, then do so. */
1489 if (term_im
&& *term_im
)
1490 tputs (term_im
, 1, _rl_output_character_function
);
1492 /* If there is a special command for inserting characters, then
1493 use that first to open up the space. */
1494 if (term_ic
&& *term_ic
)
1496 for (i
= count
; i
--; )
1497 tputs (term_ic
, 1, _rl_output_character_function
);
1500 /* Print the text. */
1501 _rl_output_some_chars (string
, count
);
1503 /* If there is a string to turn off insert mode, we had best use
1505 if (term_ei
&& *term_ei
)
1506 tputs (term_ei
, 1, _rl_output_character_function
);
1510 /* Delete COUNT characters from the display line. */
1512 delete_chars (count
)
1515 if (count
> screenwidth
) /* XXX */
1518 if (term_DC
&& *term_DC
)
1521 buffer
= tgoto (term_DC
, count
, count
);
1522 tputs (buffer
, count
, _rl_output_character_function
);
1526 if (term_dc
&& *term_dc
)
1528 tputs (term_dc
, 1, _rl_output_character_function
);
1538 /* If the cursor is the only thing on an otherwise-blank last line,
1539 compensate so we don't print an extra CRLF. */
1540 if (_rl_vis_botlin
&& _rl_last_c_pos
== 0 &&
1541 visible_line
[vis_lbreaks
[_rl_vis_botlin
]] == 0)
1546 _rl_move_vert (_rl_vis_botlin
);
1547 /* If we've wrapped lines, remove the final xterm line-wrap flag. */
1548 if (full_lines
&& _rl_term_autowrap
&& (VIS_LLEN(_rl_vis_botlin
) == screenwidth
))
1552 last_line
= &visible_line
[inv_lbreaks
[_rl_vis_botlin
]];
1554 last_line
= &visible_line
[vis_lbreaks
[_rl_vis_botlin
]];
1556 _rl_move_cursor_relative (screenwidth
- 1, last_line
);
1557 _rl_clear_to_eol (0);
1558 putc (last_line
[screenwidth
- 1], rl_outstream
);
1562 fflush (rl_outstream
);
1566 /* Move to the start of the current line. */
1572 #if defined (__MSDOS__)
1573 putc ('\r', rl_outstream
);
1575 tputs (term_cr
, 1, _rl_output_character_function
);
1581 /* Redraw the last line of a multi-line prompt that may possibly contain
1582 terminal escape sequences. Called with the cursor at column 0 of the
1583 line to draw the prompt on. */
1588 char *oldp
, *oldl
, *oldlprefix
;
1589 int oldlen
, oldlast
, oldplen
;
1591 /* Geez, I should make this a struct. */
1592 oldp
= rl_display_prompt
;
1593 oldl
= local_prompt
;
1594 oldlprefix
= local_prompt_prefix
;
1595 oldlen
= visible_length
;
1596 oldplen
= prefix_length
;
1597 oldlast
= last_invisible
;
1599 rl_display_prompt
= t
;
1600 local_prompt
= expand_prompt (t
, &visible_length
, &last_invisible
);
1601 local_prompt_prefix
= (char *)NULL
;
1602 rl_forced_update_display ();
1604 rl_display_prompt
= oldp
;
1605 local_prompt
= oldl
;
1606 local_prompt_prefix
= oldlprefix
;
1607 visible_length
= oldlen
;
1608 prefix_length
= oldplen
;
1609 last_invisible
= oldlast
;
1612 /* Redisplay the current line after a SIGWINCH is received. */
1614 _rl_redisplay_after_sigwinch ()
1618 /* Clear the current line and put the cursor at column 0. Make sure
1619 the right thing happens if we have wrapped to a new screen line. */
1622 #if defined (__MSDOS__)
1623 putc ('\r', rl_outstream
);
1625 tputs (term_cr
, 1, _rl_output_character_function
);
1628 #if defined (__MSDOS__)
1629 space_to_eol (screenwidth
);
1630 putc ('\r', rl_outstream
);
1633 tputs (term_clreol
, 1, _rl_output_character_function
);
1636 space_to_eol (screenwidth
);
1637 tputs (term_cr
, 1, _rl_output_character_function
);
1640 if (_rl_last_v_pos
> 0)
1646 /* Redraw only the last line of a multi-line prompt. */
1647 t
= strrchr (rl_display_prompt
, '\n');
1649 redraw_prompt (++t
);
1651 rl_forced_update_display ();
1655 _rl_clean_up_for_exit ()
1657 if (readline_echoing_p
)
1659 _rl_move_vert (_rl_vis_botlin
);
1661 fflush (rl_outstream
);
1662 rl_restart_output (1, 0);
1667 _rl_erase_entire_line ()
1670 _rl_clear_to_eol (0);
1672 fflush (rl_outstream
);
1675 /* return the `current display line' of the cursor -- the number of lines to
1676 move up to get to the first screen line of the current readline line. */
1678 _rl_current_display_line ()
1682 /* Find out whether or not there might be invisible characters in the
1684 if (rl_display_prompt
== rl_prompt
)
1685 nleft
= _rl_last_c_pos
- screenwidth
- rl_visible_prompt_length
;
1687 nleft
= _rl_last_c_pos
- screenwidth
;
1690 ret
= 1 + nleft
/ screenwidth
;