1 /* display.c -- readline redisplay facility. */
3 /* Copyright (C) 1987-2005 Free Software Foundation, Inc.
5 This file is part of the GNU Readline Library, a library for
6 reading lines of text with interactive input and history editing.
8 The GNU Readline Library is free software; you can redistribute it
9 and/or modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2, or
11 (at your option) any later version.
13 The GNU Readline Library is distributed in the hope that it will be
14 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 The GNU General Public License is often shipped with GNU software, and
19 is generally kept in a file called COPYING or LICENSE. If you do not
20 have a copy of the license, write to the Free Software Foundation,
21 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
22 #define READLINE_LIBRARY
24 #if defined (HAVE_CONFIG_H)
28 #include <sys/types.h>
30 #if defined (HAVE_UNISTD_H)
32 #endif /* HAVE_UNISTD_H */
34 #include "posixstat.h"
36 #if defined (HAVE_STDLIB_H)
39 # include "ansi_stdlib.h"
40 #endif /* HAVE_STDLIB_H */
44 /* System-specific feature definitions and include files. */
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
PARAMS((char *, char *, int, int, int, int));
67 static void space_to_eol
PARAMS((int));
68 static void delete_chars
PARAMS((int));
69 static void insert_some_chars
PARAMS((char *, int, int));
70 static void cr
PARAMS((void));
72 #if defined (HANDLE_MULTIBYTE)
73 static int _rl_col_width
PARAMS((const char *, int, int));
74 static int *_rl_wrapped_line
;
76 # define _rl_col_width(l, s, e) (((e) <= (s)) ? 0 : (e) - (s))
79 static int *inv_lbreaks
, *vis_lbreaks
;
80 static int inv_lbsize
, vis_lbsize
;
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 /* Application-specific redisplay function. */
114 rl_voidfunc_t
*rl_redisplay_function
= rl_redisplay
;
116 /* Global variables declared here. */
117 /* What YOU turn on when you have handled all redisplay yourself. */
118 int rl_display_fixed
= 0;
120 int _rl_suppress_redisplay
= 0;
121 int _rl_want_redisplay
= 0;
123 /* The stuff that gets printed out before the actual text of the line.
124 This is usually pointing to rl_prompt. */
125 char *rl_display_prompt
= (char *)NULL
;
127 /* Pseudo-global variables declared here. */
129 /* The visible cursor position. If you print some text, adjust this. */
130 /* NOTE: _rl_last_c_pos is used as a buffer index when not in a locale
131 supporting multibyte characters, and an absolute cursor position when
132 in such a locale. This is an artifact of the donated multibyte support.
133 Care must be taken when modifying its value. */
134 int _rl_last_c_pos
= 0;
135 int _rl_last_v_pos
= 0;
137 static int cpos_adjusted
;
139 /* Number of lines currently on screen minus 1. */
140 int _rl_vis_botlin
= 0;
142 /* Variables used only in this file. */
143 /* The last left edge of text that was displayed. This is used when
144 doing horizontal scrolling. It shifts in thirds of a screenwidth. */
145 static int last_lmargin
;
147 /* The line display buffers. One is the line currently displayed on
148 the screen. The other is the line about to be displayed. */
149 static char *visible_line
= (char *)NULL
;
150 static char *invisible_line
= (char *)NULL
;
152 /* A buffer for `modeline' messages. */
153 static char msg_buf
[128];
155 /* Non-zero forces the redisplay even if we thought it was unnecessary. */
156 static int forced_display
;
158 /* Default and initial buffer size. Can grow. */
159 static int line_size
= 1024;
161 /* Variables to keep track of the expanded prompt string, which may
162 include invisible characters. */
164 static char *local_prompt
, *local_prompt_prefix
;
165 static int prompt_visible_length
, prompt_prefix_length
;
167 /* The number of invisible characters in the line currently being
168 displayed on the screen. */
169 static int visible_wrap_offset
;
171 /* The number of invisible characters in the prompt string. Static so it
172 can be shared between rl_redisplay and update_line */
173 static int wrap_offset
;
175 /* The index of the last invisible character in the prompt string. */
176 static int prompt_last_invisible
;
178 /* The length (buffer offset) of the first line of the last (possibly
179 multi-line) buffer displayed on the screen. */
180 static int visible_first_line_len
;
182 /* Number of invisible characters on the first physical line of the prompt.
183 Only valid when the number of physical characters in the prompt exceeds
184 (or is equal to) _rl_screenwidth. */
185 static int prompt_invis_chars_first_line
;
187 static int prompt_last_screen_line
;
189 static int prompt_physical_chars
;
191 /* Variables to save and restore prompt and display information. */
193 /* These are getting numerous enough that it's time to create a struct. */
195 static char *saved_local_prompt
;
196 static char *saved_local_prefix
;
197 static int saved_last_invisible
;
198 static int saved_visible_length
;
199 static int saved_prefix_length
;
200 static int saved_invis_chars_first_line
;
201 static int saved_physical_chars
;
203 /* Expand the prompt string S and return the number of visible
204 characters in *LP, if LP is not null. This is currently more-or-less
205 a placeholder for expansion. LIP, if non-null is a place to store the
206 index of the last invisible character in the returned string. NIFLP,
207 if non-zero, is a place to store the number of invisible characters in
208 the first prompt line. The previous are used as byte counts -- indexes
209 into a character buffer. */
211 /* Current implementation:
212 \001 (^A) start non-visible characters
213 \002 (^B) end non-visible characters
214 all characters except \001 and \002 (following a \001) are copied to
215 the returned string; all characters except those between \001 and
216 \002 are assumed to be `visible'. */
219 expand_prompt (pmt
, lp
, lip
, niflp
, vlp
)
221 int *lp
, *lip
, *niflp
, *vlp
;
224 int l
, rl
, last
, ignoring
, ninvis
, invfl
, invflset
, ind
, pind
, physchars
;
226 /* Short-circuit if we can. */
227 if ((MB_CUR_MAX
<= 1 || rl_byte_oriented
) && strchr (pmt
, RL_PROMPT_START_IGNORE
) == 0)
229 r
= savestring (pmt
);
237 *vlp
= lp
? *lp
: strlen (r
);
242 r
= ret
= (char *)xmalloc (l
+ 1);
244 invfl
= 0; /* invisible chars in first line of prompt */
245 invflset
= 0; /* we only want to set invfl once */
247 for (rl
= ignoring
= last
= ninvis
= physchars
= 0, p
= pmt
; p
&& *p
; p
++)
249 /* This code strips the invisible character string markers
250 RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE */
251 if (*p
== RL_PROMPT_START_IGNORE
)
256 else if (ignoring
&& *p
== RL_PROMPT_END_IGNORE
)
259 if (p
[-1] != RL_PROMPT_START_IGNORE
)
265 #if defined (HANDLE_MULTIBYTE)
266 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
269 ind
= _rl_find_next_mbchar (pmt
, pind
, 1, MB_FIND_NONZERO
);
276 physchars
+= _rl_col_width (pmt
, pind
, ind
);
279 ninvis
+= ind
- pind
;
280 p
--; /* compensate for later increment */
288 rl
++; /* visible length byte counter */
292 ninvis
++; /* invisible chars byte counter */
295 if (invflset
== 0 && rl
>= _rl_screenwidth
)
303 if (rl
< _rl_screenwidth
)
318 /* Just strip out RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE from
319 PMT and return the rest of PMT. */
321 _rl_strip_prompt (pmt
)
326 ret
= expand_prompt (pmt
, (int *)NULL
, (int *)NULL
, (int *)NULL
, (int *)NULL
);
331 * Expand the prompt string into the various display components, if
334 * local_prompt = expanded last line of string in rl_display_prompt
335 * (portion after the final newline)
336 * local_prompt_prefix = portion before last newline of rl_display_prompt,
337 * expanded via expand_prompt
338 * prompt_visible_length = number of visible characters in local_prompt
339 * prompt_prefix_length = number of visible characters in local_prompt_prefix
341 * This function is called once per call to readline(). It may also be
342 * called arbitrarily to expand the primary prompt.
344 * The return value is the number of visible characters on the last line
345 * of the (possibly multi-line) prompt.
348 rl_expand_prompt (prompt
)
354 /* Clear out any saved values. */
356 FREE (local_prompt_prefix
);
358 local_prompt
= local_prompt_prefix
= (char *)0;
359 prompt_last_invisible
= prompt_invis_chars_first_line
= 0;
360 prompt_visible_length
= prompt_physical_chars
= 0;
362 if (prompt
== 0 || *prompt
== 0)
365 p
= strrchr (prompt
, '\n');
368 /* The prompt is only one logical line, though it might wrap. */
369 local_prompt
= expand_prompt (prompt
, &prompt_visible_length
,
370 &prompt_last_invisible
,
371 &prompt_invis_chars_first_line
,
372 &prompt_physical_chars
);
373 local_prompt_prefix
= (char *)0;
374 return (prompt_visible_length
);
378 /* The prompt spans multiple lines. */
380 local_prompt
= expand_prompt (p
, &prompt_visible_length
,
381 &prompt_last_invisible
,
383 &prompt_physical_chars
);
385 /* The portion of the prompt string up to and including the
386 final newline is now null-terminated. */
387 local_prompt_prefix
= expand_prompt (prompt
, &prompt_prefix_length
,
389 &prompt_invis_chars_first_line
,
392 return (prompt_prefix_length
);
396 /* Initialize the VISIBLE_LINE and INVISIBLE_LINE arrays, and their associated
397 arrays of line break markers. MINSIZE is the minimum size of VISIBLE_LINE
398 and INVISIBLE_LINE; if it is greater than LINE_SIZE, LINE_SIZE is
399 increased. If the lines have already been allocated, this ensures that
400 they can hold at least MINSIZE characters. */
402 init_line_structures (minsize
)
407 if (invisible_line
== 0) /* initialize it */
409 if (line_size
< minsize
)
411 visible_line
= (char *)xmalloc (line_size
);
412 invisible_line
= (char *)xmalloc (line_size
);
414 else if (line_size
< minsize
) /* ensure it can hold MINSIZE chars */
417 if (line_size
< minsize
)
419 visible_line
= (char *)xrealloc (visible_line
, line_size
);
420 invisible_line
= (char *)xrealloc (invisible_line
, line_size
);
423 for (n
= minsize
; n
< line_size
; n
++)
426 invisible_line
[n
] = 1;
429 if (vis_lbreaks
== 0)
431 /* should be enough. */
432 inv_lbsize
= vis_lbsize
= 256;
433 inv_lbreaks
= (int *)xmalloc (inv_lbsize
* sizeof (int));
434 vis_lbreaks
= (int *)xmalloc (vis_lbsize
* sizeof (int));
435 #if defined (HANDLE_MULTIBYTE)
436 _rl_wrapped_line
= (int *)xmalloc (vis_lbsize
* sizeof (int));
438 inv_lbreaks
[0] = vis_lbreaks
[0] = 0;
442 /* Basic redisplay algorithm. */
446 register int in
, out
, c
, linenum
, cursor_linenum
;
448 int c_pos
, inv_botlin
, lb_botlin
, lb_linenum
, o_cpos
;
449 int newlines
, lpos
, temp
, modmark
, n0
, num
;
450 char *prompt_this_line
;
451 #if defined (HANDLE_MULTIBYTE)
456 int _rl_wrapped_multicolumn
= 0;
459 if (!readline_echoing_p
)
462 if (!rl_display_prompt
)
463 rl_display_prompt
= "";
465 if (invisible_line
== 0 || vis_lbreaks
== 0)
467 init_line_structures (0);
471 /* Draw the line into the buffer. */
474 line
= invisible_line
;
475 out
= inv_botlin
= 0;
477 /* Mark the line as modified or not. We only do this for history
480 if (_rl_mark_modified_lines
&& current_history () && rl_undo_list
)
487 /* If someone thought that the redisplay was handled, but the currently
488 visible line has a different modification state than the one about
489 to become visible, then correct the caller's misconception. */
490 if (visible_line
[0] != invisible_line
[0])
491 rl_display_fixed
= 0;
493 /* If the prompt to be displayed is the `primary' readline prompt (the
494 one passed to readline()), use the values we have already expanded.
495 If not, use what's already in rl_display_prompt. WRAP_OFFSET is the
496 number of non-visible characters in the prompt string. */
497 if (rl_display_prompt
== rl_prompt
|| local_prompt
)
499 int local_len
= local_prompt
? strlen (local_prompt
) : 0;
500 if (local_prompt_prefix
&& forced_display
)
501 _rl_output_some_chars (local_prompt_prefix
, strlen (local_prompt_prefix
));
505 temp
= local_len
+ out
+ 2;
506 if (temp
>= line_size
)
508 line_size
= (temp
+ 1024) - (temp
% 1024);
509 visible_line
= (char *)xrealloc (visible_line
, line_size
);
510 line
= invisible_line
= (char *)xrealloc (invisible_line
, line_size
);
512 strncpy (line
+ out
, local_prompt
, local_len
);
516 wrap_offset
= local_len
- prompt_visible_length
;
521 prompt_this_line
= strrchr (rl_display_prompt
, '\n');
522 if (!prompt_this_line
)
523 prompt_this_line
= rl_display_prompt
;
527 pmtlen
= prompt_this_line
- rl_display_prompt
; /* temp var */
530 _rl_output_some_chars (rl_display_prompt
, pmtlen
);
531 /* Make sure we are at column zero even after a newline,
532 regardless of the state of terminal output processing. */
533 if (pmtlen
< 2 || prompt_this_line
[-2] != '\r')
538 prompt_physical_chars
= pmtlen
= strlen (prompt_this_line
);
539 temp
= pmtlen
+ out
+ 2;
540 if (temp
>= line_size
)
542 line_size
= (temp
+ 1024) - (temp
% 1024);
543 visible_line
= (char *)xrealloc (visible_line
, line_size
);
544 line
= invisible_line
= (char *)xrealloc (invisible_line
, line_size
);
546 strncpy (line
+ out
, prompt_this_line
, pmtlen
);
549 wrap_offset
= prompt_invis_chars_first_line
= 0;
552 #define CHECK_INV_LBREAKS() \
554 if (newlines >= (inv_lbsize - 2)) \
557 inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
561 #if defined (HANDLE_MULTIBYTE)
562 #define CHECK_LPOS() \
565 if (lpos >= _rl_screenwidth) \
567 if (newlines >= (inv_lbsize - 2)) \
570 inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
571 _rl_wrapped_line = (int *)xrealloc (_rl_wrapped_line, inv_lbsize * sizeof (int)); \
573 inv_lbreaks[++newlines] = out; \
574 _rl_wrapped_line[newlines] = _rl_wrapped_multicolumn; \
579 #define CHECK_LPOS() \
582 if (lpos >= _rl_screenwidth) \
584 if (newlines >= (inv_lbsize - 2)) \
587 inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
589 inv_lbreaks[++newlines] = out; \
595 /* inv_lbreaks[i] is where line i starts in the buffer. */
596 inv_lbreaks
[newlines
= 0] = 0;
598 lpos
= out
- wrap_offset
;
600 lpos
= prompt_physical_chars
+ modmark
;
603 #if defined (HANDLE_MULTIBYTE)
604 memset (_rl_wrapped_line
, 0, vis_lbsize
);
608 /* prompt_invis_chars_first_line is the number of invisible characters in
609 the first physical line of the prompt.
610 wrap_offset - prompt_invis_chars_first_line is the number of invis
611 chars on the second line. */
613 /* what if lpos is already >= _rl_screenwidth before we start drawing the
614 contents of the command line? */
615 while (lpos
>= _rl_screenwidth
)
617 /* fix from Darin Johnson <darin@acuson.com> for prompt string with
618 invisible characters that is longer than the screen width. The
619 prompt_invis_chars_first_line variable could be made into an array
620 saying how many invisible characters there are per line, but that's
621 probably too much work for the benefit gained. How many people have
622 prompts that exceed two physical lines?
623 Additional logic fix from Edward Catmur <ed@catmur.co.uk> */
624 #if defined (HANDLE_MULTIBYTE)
626 temp
= local_prompt
? strlen (local_prompt
) : 0;
629 if (_rl_col_width (local_prompt
, n0
, num
) > _rl_screenwidth
)
631 num
= _rl_find_prev_mbchar (local_prompt
, num
, MB_FIND_ANY
);
638 temp
= ((newlines
+ 1) * _rl_screenwidth
) +
639 #endif /* !HANDLE_MULTIBYTE */
640 ((local_prompt_prefix
== 0) ? ((newlines
== 0) ? prompt_invis_chars_first_line
641 : ((newlines
== 1) ? wrap_offset
: 0))
642 : ((newlines
== 0) ? wrap_offset
:0));
644 inv_lbreaks
[++newlines
] = temp
;
645 #if defined (HANDLE_MULTIBYTE)
646 lpos
-= _rl_col_width (local_prompt
, n0
, num
);
648 lpos
-= _rl_screenwidth
;
652 prompt_last_screen_line
= newlines
;
654 /* Draw the rest of the line (after the prompt) into invisible_line, keeping
655 track of where the cursor is (c_pos), the number of the line containing
656 the cursor (lb_linenum), the last line number (lb_botlin and inv_botlin).
657 It maintains an array of line breaks for display (inv_lbreaks).
658 This handles expanding tabs for display and displaying meta characters. */
660 #if defined (HANDLE_MULTIBYTE)
662 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
664 memset (&ps
, 0, sizeof (mbstate_t));
665 wc_bytes
= mbrtowc (&wc
, rl_line_buffer
, rl_end
, &ps
);
671 for (in
= 0; in
< rl_end
; in
++)
674 c
= (unsigned char)rl_line_buffer
[in
];
676 #if defined (HANDLE_MULTIBYTE)
677 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
679 if (MB_INVALIDCH (wc_bytes
))
681 /* Byte sequence is invalid or shortened. Assume that the
682 first byte represents a character. */
684 /* Assume that a character occupies a single column. */
686 memset (&ps
, 0, sizeof (mbstate_t));
688 else if (MB_NULLWCH (wc_bytes
))
689 break; /* Found '\0' */
693 wc_width
= (temp
>= 0) ? temp
: 1;
698 if (out
+ 8 >= line_size
) /* XXX - 8 for \t */
701 visible_line
= (char *)xrealloc (visible_line
, line_size
);
702 invisible_line
= (char *)xrealloc (invisible_line
, line_size
);
703 line
= invisible_line
;
709 lb_linenum
= newlines
;
712 #if defined (HANDLE_MULTIBYTE)
713 if (META_CHAR (c
) && _rl_output_meta_chars
== 0) /* XXX - clean up */
718 if (_rl_output_meta_chars
== 0)
720 sprintf (line
+ out
, "\\%o", c
);
722 if (lpos
+ 4 >= _rl_screenwidth
)
724 temp
= _rl_screenwidth
- lpos
;
725 CHECK_INV_LBREAKS ();
726 inv_lbreaks
[++newlines
] = out
+ temp
;
740 #if defined (DISPLAY_TABS)
746 newout
= (out
| (int)7) + 1;
748 newout
= out
+ 8 - lpos
% 8;
751 if (lpos
+ temp
>= _rl_screenwidth
)
754 temp2
= _rl_screenwidth
- lpos
;
755 CHECK_INV_LBREAKS ();
756 inv_lbreaks
[++newlines
] = out
+ temp2
;
769 else if (c
== '\n' && _rl_horizontal_scroll_mode
== 0 && _rl_term_up
&& *_rl_term_up
)
771 line
[out
++] = '\0'; /* XXX - sentinel */
772 CHECK_INV_LBREAKS ();
773 inv_lbreaks
[++newlines
] = out
;
776 else if (CTRL_CHAR (c
) || c
== RUBOUT
)
780 line
[out
++] = CTRL_CHAR (c
) ? UNCTRL (c
) : '?';
785 #if defined (HANDLE_MULTIBYTE)
786 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
790 _rl_wrapped_multicolumn
= 0;
792 if (_rl_screenwidth
< lpos
+ wc_width
)
793 for (i
= lpos
; i
< _rl_screenwidth
; i
++)
795 /* The space will be removed in update_line() */
797 _rl_wrapped_multicolumn
++;
803 lb_linenum
= newlines
;
805 for (i
= in
; i
< in
+wc_bytes
; i
++)
806 line
[out
++] = rl_line_buffer
[i
];
807 for (i
= 0; i
< wc_width
; i
++)
821 #if defined (HANDLE_MULTIBYTE)
822 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
825 wc_bytes
= mbrtowc (&wc
, rl_line_buffer
+ in
, rl_end
- in
, &ps
);
836 lb_linenum
= newlines
;
839 inv_botlin
= lb_botlin
= newlines
;
840 CHECK_INV_LBREAKS ();
841 inv_lbreaks
[newlines
+1] = out
;
842 cursor_linenum
= lb_linenum
;
844 /* C_POS == position in buffer where cursor should be placed.
845 CURSOR_LINENUM == line number where the cursor should be placed. */
847 /* PWP: now is when things get a bit hairy. The visible and invisible
848 line buffers are really multiple lines, which would wrap every
849 (screenwidth - 1) characters. Go through each in turn, finding
850 the changed region and updating it. The line order is top to bottom. */
852 /* If we can move the cursor up and down, then use multiple lines,
853 otherwise, let long lines display in a single terminal line, and
854 horizontally scroll it. */
856 if (_rl_horizontal_scroll_mode
== 0 && _rl_term_up
&& *_rl_term_up
)
858 int nleft
, pos
, changed_screen_line
, tx
;
860 if (!rl_display_fixed
|| forced_display
)
864 /* If we have more than a screenful of material to display, then
865 only display a screenful. We should display the last screen,
867 if (out
>= _rl_screenchars
)
869 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
870 out
= _rl_find_prev_mbchar (line
, _rl_screenchars
, MB_FIND_ANY
);
872 out
= _rl_screenchars
- 1;
875 /* The first line is at character position 0 in the buffer. The
876 second and subsequent lines start at inv_lbreaks[N], offset by
877 OFFSET (which has already been calculated above). */
879 #define W_OFFSET(line, offset) ((line) == 0 ? offset : 0)
880 #define VIS_LLEN(l) ((l) > _rl_vis_botlin ? 0 : (vis_lbreaks[l+1] - vis_lbreaks[l]))
881 #define INV_LLEN(l) (inv_lbreaks[l+1] - inv_lbreaks[l])
882 #define VIS_CHARS(line) (visible_line + vis_lbreaks[line])
883 #define VIS_LINE(line) ((line) > _rl_vis_botlin) ? "" : VIS_CHARS(line)
884 #define INV_LINE(line) (invisible_line + inv_lbreaks[line])
886 /* For each line in the buffer, do the updating display. */
887 for (linenum
= 0; linenum
<= inv_botlin
; linenum
++)
889 o_cpos
= _rl_last_c_pos
;
891 update_line (VIS_LINE(linenum
), INV_LINE(linenum
), linenum
,
892 VIS_LLEN(linenum
), INV_LLEN(linenum
), inv_botlin
);
894 /* update_line potentially changes _rl_last_c_pos, but doesn't
895 take invisible characters into account, since _rl_last_c_pos
896 is an absolute cursor position in a multibyte locale. See
897 if compensating here is the right thing, or if we have to
898 change update_line itself. There is one case in which
899 update_line adjusts _rl_last_c_pos itself (so it can pass
900 _rl_move_cursor_relative accurate values); it communicates
901 this back by setting cpos_adjusted */
902 if (linenum
== 0 && (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0) &&
903 cpos_adjusted
== 0 &&
904 _rl_last_c_pos
!= o_cpos
&&
905 _rl_last_c_pos
> wrap_offset
&&
906 o_cpos
< prompt_last_invisible
)
907 _rl_last_c_pos
-= wrap_offset
;
909 /* If this is the line with the prompt, we might need to
910 compensate for invisible characters in the new line. Do
911 this only if there is not more than one new line (which
912 implies that we completely overwrite the old visible line)
913 and the new line is shorter than the old. Make sure we are
914 at the end of the new line before clearing. */
916 inv_botlin
== 0 && _rl_last_c_pos
== out
&&
917 (wrap_offset
> visible_wrap_offset
) &&
918 (_rl_last_c_pos
< visible_first_line_len
))
920 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
921 nleft
= _rl_screenwidth
- _rl_last_c_pos
;
923 nleft
= _rl_screenwidth
+ wrap_offset
- _rl_last_c_pos
;
925 _rl_clear_to_eol (nleft
);
928 /* Since the new first line is now visible, save its length. */
930 visible_first_line_len
= (inv_botlin
> 0) ? inv_lbreaks
[1] : out
- wrap_offset
;
933 /* We may have deleted some lines. If so, clear the left over
934 blank ones at the bottom out. */
935 if (_rl_vis_botlin
> inv_botlin
)
938 for (; linenum
<= _rl_vis_botlin
; linenum
++)
940 tt
= VIS_CHARS (linenum
);
941 _rl_move_vert (linenum
);
942 _rl_move_cursor_relative (0, tt
);
944 ((linenum
== _rl_vis_botlin
) ? strlen (tt
) : _rl_screenwidth
);
947 _rl_vis_botlin
= inv_botlin
;
949 /* CHANGED_SCREEN_LINE is set to 1 if we have moved to a
950 different screen line during this redisplay. */
951 changed_screen_line
= _rl_last_v_pos
!= cursor_linenum
;
952 if (changed_screen_line
)
954 _rl_move_vert (cursor_linenum
);
955 /* If we moved up to the line with the prompt using _rl_term_up,
956 the physical cursor position on the screen stays the same,
957 but the buffer position needs to be adjusted to account
958 for invisible characters. */
959 if ((MB_CUR_MAX
== 1 || rl_byte_oriented
) && cursor_linenum
== 0 && wrap_offset
)
960 _rl_last_c_pos
+= wrap_offset
;
963 /* We have to reprint the prompt if it contains invisible
964 characters, since it's not generally OK to just reprint
965 the characters from the current cursor position. But we
966 only need to reprint it if the cursor is before the last
967 invisible character in the prompt string. */
968 nleft
= prompt_visible_length
+ wrap_offset
;
969 if (cursor_linenum
== 0 && wrap_offset
> 0 && _rl_last_c_pos
> 0 &&
970 _rl_last_c_pos
<= prompt_last_invisible
&& local_prompt
)
972 #if defined (__MSDOS__)
973 putc ('\r', rl_outstream
);
976 tputs (_rl_term_cr
, 1, _rl_output_character_function
);
978 _rl_output_some_chars (local_prompt
, nleft
);
979 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
980 _rl_last_c_pos
= _rl_col_width (local_prompt
, 0, nleft
) - wrap_offset
;
982 _rl_last_c_pos
= nleft
;
985 /* Where on that line? And where does that line start
987 pos
= inv_lbreaks
[cursor_linenum
];
988 /* nleft == number of characters in the line buffer between the
989 start of the line and the cursor position. */
992 /* NLEFT is now a number of characters in a buffer. When in a
993 multibyte locale, however, _rl_last_c_pos is an absolute cursor
994 position that doesn't take invisible characters in the prompt
995 into account. We use a fudge factor to compensate. */
997 /* Since _rl_backspace() doesn't know about invisible characters in the
998 prompt, and there's no good way to tell it, we compensate for
999 those characters here and call _rl_backspace() directly. */
1000 if (wrap_offset
&& cursor_linenum
== 0 && nleft
< _rl_last_c_pos
)
1002 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
1003 tx
= _rl_col_width (&visible_line
[pos
], 0, nleft
) - visible_wrap_offset
;
1006 if (_rl_last_c_pos
> tx
)
1008 _rl_backspace (_rl_last_c_pos
- tx
); /* XXX */
1009 _rl_last_c_pos
= tx
;
1013 /* We need to note that in a multibyte locale we are dealing with
1014 _rl_last_c_pos as an absolute cursor position, but moving to a
1015 point specified by a buffer position (NLEFT) that doesn't take
1016 invisible characters into account. */
1017 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
1018 _rl_move_cursor_relative (nleft
, &invisible_line
[pos
]);
1019 else if (nleft
!= _rl_last_c_pos
)
1020 _rl_move_cursor_relative (nleft
, &invisible_line
[pos
]);
1023 else /* Do horizontal scrolling. */
1025 #define M_OFFSET(margin, offset) ((margin) == 0 ? offset : 0)
1026 int lmargin
, ndisp
, nleft
, phys_c_pos
, t
;
1028 /* Always at top line. */
1031 /* Compute where in the buffer the displayed line should start. This
1034 /* The number of characters that will be displayed before the cursor. */
1035 ndisp
= c_pos
- wrap_offset
;
1036 nleft
= prompt_visible_length
+ wrap_offset
;
1037 /* Where the new cursor position will be on the screen. This can be
1038 longer than SCREENWIDTH; if it is, lmargin will be adjusted. */
1039 phys_c_pos
= c_pos
- (last_lmargin
? last_lmargin
: wrap_offset
);
1040 t
= _rl_screenwidth
/ 3;
1042 /* If the number of characters had already exceeded the screenwidth,
1043 last_lmargin will be > 0. */
1045 /* If the number of characters to be displayed is more than the screen
1046 width, compute the starting offset so that the cursor is about
1047 two-thirds of the way across the screen. */
1048 if (phys_c_pos
> _rl_screenwidth
- 2)
1050 lmargin
= c_pos
- (2 * t
);
1053 /* If the left margin would be in the middle of a prompt with
1054 invisible characters, don't display the prompt at all. */
1055 if (wrap_offset
&& lmargin
> 0 && lmargin
< nleft
)
1058 else if (ndisp
< _rl_screenwidth
- 2) /* XXX - was -1 */
1060 else if (phys_c_pos
< 1)
1062 /* If we are moving back towards the beginning of the line and
1063 the last margin is no longer correct, compute a new one. */
1064 lmargin
= ((c_pos
- 1) / t
) * t
; /* XXX */
1065 if (wrap_offset
&& lmargin
> 0 && lmargin
< nleft
)
1069 lmargin
= last_lmargin
;
1071 /* If the first character on the screen isn't the first character
1072 in the display line, indicate this with a special character. */
1074 line
[lmargin
] = '<';
1076 /* If SCREENWIDTH characters starting at LMARGIN do not encompass
1077 the whole line, indicate that with a special character at the
1078 right edge of the screen. If LMARGIN is 0, we need to take the
1079 wrap offset into account. */
1080 t
= lmargin
+ M_OFFSET (lmargin
, wrap_offset
) + _rl_screenwidth
;
1084 if (!rl_display_fixed
|| forced_display
|| lmargin
!= last_lmargin
)
1087 update_line (&visible_line
[last_lmargin
],
1088 &invisible_line
[lmargin
],
1090 _rl_screenwidth
+ visible_wrap_offset
,
1091 _rl_screenwidth
+ (lmargin
? 0 : wrap_offset
),
1094 /* If the visible new line is shorter than the old, but the number
1095 of invisible characters is greater, and we are at the end of
1096 the new line, we need to clear to eol. */
1097 t
= _rl_last_c_pos
- M_OFFSET (lmargin
, wrap_offset
);
1098 if ((M_OFFSET (lmargin
, wrap_offset
) > visible_wrap_offset
) &&
1099 (_rl_last_c_pos
== out
) &&
1100 t
< visible_first_line_len
)
1102 nleft
= _rl_screenwidth
- t
;
1103 _rl_clear_to_eol (nleft
);
1105 visible_first_line_len
= out
- lmargin
- M_OFFSET (lmargin
, wrap_offset
);
1106 if (visible_first_line_len
> _rl_screenwidth
)
1107 visible_first_line_len
= _rl_screenwidth
;
1109 _rl_move_cursor_relative (c_pos
- lmargin
, &invisible_line
[lmargin
]);
1110 last_lmargin
= lmargin
;
1113 fflush (rl_outstream
);
1115 /* Swap visible and non-visible lines. */
1117 char *vtemp
= visible_line
;
1118 int *itemp
= vis_lbreaks
, ntemp
= vis_lbsize
;
1120 visible_line
= invisible_line
;
1121 invisible_line
= vtemp
;
1123 vis_lbreaks
= inv_lbreaks
;
1124 inv_lbreaks
= itemp
;
1126 vis_lbsize
= inv_lbsize
;
1129 rl_display_fixed
= 0;
1130 /* If we are displaying on a single line, and last_lmargin is > 0, we
1131 are not displaying any invisible characters, so set visible_wrap_offset
1133 if (_rl_horizontal_scroll_mode
&& last_lmargin
)
1134 visible_wrap_offset
= 0;
1136 visible_wrap_offset
= wrap_offset
;
1140 /* PWP: update_line() is based on finding the middle difference of each
1141 line on the screen; vis:
1143 /old first difference
1144 /beginning of line | /old last same /old EOL
1146 old: eddie> Oh, my little gruntle-buggy is to me, as lurgid as
1147 new: eddie> Oh, my little buggy says to me, as lurgid as
1149 \beginning of line | \new last same \new end of line
1150 \new first difference
1152 All are character pointers for the sake of speed. Special cases for
1153 no differences, as well as for end of line additions must be handled.
1155 Could be made even smarter, but this works well enough */
1157 update_line (old
, new, current_line
, omax
, nmax
, inv_botlin
)
1158 register char *old
, *new;
1159 int current_line
, omax
, nmax
, inv_botlin
;
1161 register char *ofd
, *ols
, *oe
, *nfd
, *nls
, *ne
;
1162 int temp
, lendiff
, wsatend
, od
, nd
;
1163 int current_invis_chars
;
1164 int col_lendiff
, col_temp
;
1165 #if defined (HANDLE_MULTIBYTE)
1166 mbstate_t ps_new
, ps_old
;
1167 int new_offset
, old_offset
, tmp
;
1170 /* If we're at the right edge of a terminal that supports xn, we're
1171 ready to wrap around, so do so. This fixes problems with knowing
1172 the exact cursor position and cut-and-paste with certain terminal
1173 emulators. In this calculation, TEMP is the physical screen
1174 position of the cursor. */
1175 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
1176 temp
= _rl_last_c_pos
;
1178 temp
= _rl_last_c_pos
- W_OFFSET(_rl_last_v_pos
, visible_wrap_offset
);
1179 if (temp
== _rl_screenwidth
&& _rl_term_autowrap
&& !_rl_horizontal_scroll_mode
1180 && _rl_last_v_pos
== current_line
- 1)
1182 #if defined (HANDLE_MULTIBYTE)
1183 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
1187 int tempwidth
, bytes
;
1190 /* This fixes only double-column characters, but if the wrapped
1191 character comsumes more than three columns, spaces will be
1192 inserted in the string buffer. */
1193 if (_rl_wrapped_line
[current_line
] > 0)
1194 _rl_clear_to_eol (_rl_wrapped_line
[current_line
]);
1196 memset (&ps
, 0, sizeof (mbstate_t));
1197 ret
= mbrtowc (&wc
, new, MB_CUR_MAX
, &ps
);
1198 if (MB_INVALIDCH (ret
))
1203 else if (MB_NULLWCH (ret
))
1206 tempwidth
= wcwidth (wc
);
1212 for (count
= 0; count
< bytes
; count
++)
1213 putc (new[count
], rl_outstream
);
1214 _rl_last_c_pos
= tempwidth
;
1216 memset (&ps
, 0, sizeof (mbstate_t));
1217 ret
= mbrtowc (&wc
, old
, MB_CUR_MAX
, &ps
);
1218 if (ret
!= 0 && bytes
!= 0)
1220 if (MB_INVALIDCH (ret
))
1221 memmove (old
+bytes
, old
+1, strlen (old
+1));
1223 memmove (old
+bytes
, old
+ret
, strlen (old
+ret
));
1224 memcpy (old
, new, bytes
);
1229 putc (' ', rl_outstream
);
1232 if (old
[0] && new[0])
1240 putc (new[0], rl_outstream
);
1242 putc (' ', rl_outstream
);
1245 if (old
[0] && new[0])
1251 /* Find first difference. */
1252 #if defined (HANDLE_MULTIBYTE)
1253 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
1255 /* See if the old line is a subset of the new line, so that the
1256 only change is adding characters. */
1257 temp
= (omax
< nmax
) ? omax
: nmax
;
1258 if (memcmp (old
, new, temp
) == 0)
1265 memset (&ps_new
, 0, sizeof(mbstate_t));
1266 memset (&ps_old
, 0, sizeof(mbstate_t));
1268 if (omax
== nmax
&& STREQN (new, old
, omax
))
1275 new_offset
= old_offset
= 0;
1276 for (ofd
= old
, nfd
= new;
1277 (ofd
- old
< omax
) && *ofd
&&
1278 _rl_compare_chars(old
, old_offset
, &ps_old
, new, new_offset
, &ps_new
); )
1280 old_offset
= _rl_find_next_mbchar (old
, old_offset
, 1, MB_FIND_ANY
);
1281 new_offset
= _rl_find_next_mbchar (new, new_offset
, 1, MB_FIND_ANY
);
1282 ofd
= old
+ old_offset
;
1283 nfd
= new + new_offset
;
1290 for (ofd
= old
, nfd
= new;
1291 (ofd
- old
< omax
) && *ofd
&& (*ofd
== *nfd
);
1295 /* Move to the end of the screen line. ND and OD are used to keep track
1296 of the distance between ne and new and oe and old, respectively, to
1297 move a subtraction out of each loop. */
1298 for (od
= ofd
- old
, oe
= ofd
; od
< omax
&& *oe
; oe
++, od
++);
1299 for (nd
= nfd
- new, ne
= nfd
; nd
< nmax
&& *ne
; ne
++, nd
++);
1301 /* If no difference, continue to next line. */
1302 if (ofd
== oe
&& nfd
== ne
)
1305 wsatend
= 1; /* flag for trailing whitespace */
1307 #if defined (HANDLE_MULTIBYTE)
1308 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
1310 ols
= old
+ _rl_find_prev_mbchar (old
, oe
- old
, MB_FIND_ANY
);
1311 nls
= new + _rl_find_prev_mbchar (new, ne
- new, MB_FIND_ANY
);
1312 while ((ols
> ofd
) && (nls
> nfd
))
1314 memset (&ps_old
, 0, sizeof (mbstate_t));
1315 memset (&ps_new
, 0, sizeof (mbstate_t));
1318 /* On advice from jir@yamato.ibm.com */
1319 _rl_adjust_point (old
, ols
- old
, &ps_old
);
1320 _rl_adjust_point (new, nls
- new, &ps_new
);
1323 if (_rl_compare_chars (old
, ols
- old
, &ps_old
, new, nls
- new, &ps_new
) == 0)
1329 ols
= old
+ _rl_find_prev_mbchar (old
, ols
- old
, MB_FIND_ANY
);
1330 nls
= new + _rl_find_prev_mbchar (new, nls
- new, MB_FIND_ANY
);
1335 #endif /* HANDLE_MULTIBYTE */
1336 ols
= oe
- 1; /* find last same */
1338 while ((ols
> ofd
) && (nls
> nfd
) && (*ols
== *nls
))
1345 #if defined (HANDLE_MULTIBYTE)
1354 #if defined (HANDLE_MULTIBYTE)
1355 /* This may not work for stateful encoding, but who cares? To handle
1356 stateful encoding properly, we have to scan each string from the
1357 beginning and compare. */
1358 else if (_rl_compare_chars (ols
, 0, NULL
, nls
, 0, NULL
) == 0)
1360 else if (*ols
!= *nls
)
1363 if (*ols
) /* don't step past the NUL */
1365 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
1366 ols
= old
+ _rl_find_next_mbchar (old
, ols
- old
, 1, MB_FIND_ANY
);
1372 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
1373 nls
= new + _rl_find_next_mbchar (new, nls
- new, 1, MB_FIND_ANY
);
1379 /* count of invisible characters in the current invisible line. */
1380 current_invis_chars
= W_OFFSET (current_line
, wrap_offset
);
1381 if (_rl_last_v_pos
!= current_line
)
1383 _rl_move_vert (current_line
);
1384 if ((MB_CUR_MAX
== 1 || rl_byte_oriented
) && current_line
== 0 && visible_wrap_offset
)
1385 _rl_last_c_pos
+= visible_wrap_offset
;
1388 /* If this is the first line and there are invisible characters in the
1389 prompt string, and the prompt string has not changed, and the current
1390 cursor position is before the last invisible character in the prompt,
1391 and the index of the character to move to is past the end of the prompt
1392 string, then redraw the entire prompt string. We can only do this
1393 reliably if the terminal supports a `cr' capability.
1395 This is not an efficiency hack -- there is a problem with redrawing
1396 portions of the prompt string if they contain terminal escape
1397 sequences (like drawing the `unbold' sequence without a corresponding
1398 `bold') that manifests itself on certain terminals. */
1400 lendiff
= local_prompt
? strlen (local_prompt
) : 0;
1401 od
= ofd
- old
; /* index of first difference in visible line */
1402 if (current_line
== 0 && !_rl_horizontal_scroll_mode
&&
1403 _rl_term_cr
&& lendiff
> prompt_visible_length
&& _rl_last_c_pos
> 0 &&
1404 od
>= lendiff
&& _rl_last_c_pos
<= prompt_last_invisible
)
1406 #if defined (__MSDOS__)
1407 putc ('\r', rl_outstream
);
1409 tputs (_rl_term_cr
, 1, _rl_output_character_function
);
1411 _rl_output_some_chars (local_prompt
, lendiff
);
1412 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
1414 /* We take wrap_offset into account here so we can pass correct
1415 information to _rl_move_cursor_relative. */
1416 _rl_last_c_pos
= _rl_col_width (local_prompt
, 0, lendiff
) - wrap_offset
;
1420 _rl_last_c_pos
= lendiff
;
1423 _rl_move_cursor_relative (od
, old
);
1425 /* if (len (new) > len (old))
1426 lendiff == difference in buffer
1427 col_lendiff == difference on screen
1428 When not using multibyte characters, these are equal */
1429 lendiff
= (nls
- nfd
) - (ols
- ofd
);
1430 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
1431 col_lendiff
= _rl_col_width (new, nfd
- new, nls
- new) - _rl_col_width (old
, ofd
- old
, ols
- old
);
1433 col_lendiff
= lendiff
;
1435 /* If we are changing the number of invisible characters in a line, and
1436 the spot of first difference is before the end of the invisible chars,
1437 lendiff needs to be adjusted. */
1438 if (current_line
== 0 && !_rl_horizontal_scroll_mode
&&
1439 current_invis_chars
!= visible_wrap_offset
)
1441 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
1443 lendiff
+= visible_wrap_offset
- current_invis_chars
;
1444 col_lendiff
+= visible_wrap_offset
- current_invis_chars
;
1448 lendiff
+= visible_wrap_offset
- current_invis_chars
;
1449 col_lendiff
= lendiff
;
1453 /* Insert (diff (len (old), len (new)) ch. */
1455 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
1456 col_temp
= _rl_col_width (new, nfd
- new, ne
- new);
1460 if (col_lendiff
> 0) /* XXX - was lendiff */
1462 /* Non-zero if we're increasing the number of lines. */
1463 int gl
= current_line
>= _rl_vis_botlin
&& inv_botlin
> _rl_vis_botlin
;
1464 /* Sometimes it is cheaper to print the characters rather than
1465 use the terminal's capabilities. If we're growing the number
1466 of lines, make sure we actually cause the new line to wrap
1467 around on auto-wrapping terminals. */
1468 if (_rl_terminal_can_insert
&& ((2 * col_temp
) >= col_lendiff
|| _rl_term_IC
) && (!_rl_term_autowrap
|| !gl
))
1470 /* If lendiff > prompt_visible_length and _rl_last_c_pos == 0 and
1471 _rl_horizontal_scroll_mode == 1, inserting the characters with
1472 _rl_term_IC or _rl_term_ic will screw up the screen because of the
1473 invisible characters. We need to just draw them. */
1474 if (*ols
&& (!_rl_horizontal_scroll_mode
|| _rl_last_c_pos
> 0 ||
1475 lendiff
<= prompt_visible_length
|| !current_invis_chars
))
1477 insert_some_chars (nfd
, lendiff
, col_lendiff
);
1478 _rl_last_c_pos
+= col_lendiff
;
1480 else if ((MB_CUR_MAX
== 1 || rl_byte_oriented
!= 0) && *ols
== 0 && lendiff
> 0)
1482 /* At the end of a line the characters do not have to
1483 be "inserted". They can just be placed on the screen. */
1484 /* However, this screws up the rest of this block, which
1485 assumes you've done the insert because you can. */
1486 _rl_output_some_chars (nfd
, lendiff
);
1487 _rl_last_c_pos
+= col_lendiff
;
1491 /* We have horizontal scrolling and we are not inserting at
1492 the end. We have invisible characters in this line. This
1493 is a dumb update. */
1494 _rl_output_some_chars (nfd
, temp
);
1495 _rl_last_c_pos
+= col_temp
;
1498 /* Copy (new) chars to screen from first diff to last match. */
1500 if ((temp
- lendiff
) > 0)
1502 _rl_output_some_chars (nfd
+ lendiff
, temp
- lendiff
);
1504 /* XXX -- this bears closer inspection. Fixes a redisplay bug
1505 reported against bash-3.0-alpha by Andreas Schwab involving
1506 multibyte characters and prompt strings with invisible
1507 characters, but was previously disabled. */
1508 _rl_last_c_pos
+= _rl_col_width (nfd
+lendiff
, 0, temp
-col_lendiff
);
1510 _rl_last_c_pos
+= _rl_col_width (nfd
+lendiff
, 0, temp
-lendiff
);
1516 /* cannot insert chars, write to EOL */
1517 _rl_output_some_chars (nfd
, temp
);
1518 _rl_last_c_pos
+= col_temp
;
1519 /* If we're in a multibyte locale and were before the last invisible
1520 char in the current line (which implies we just output some invisible
1521 characters) we need to adjust _rl_last_c_pos, since it represents
1522 a physical character position. */
1525 else /* Delete characters from line. */
1527 /* If possible and inexpensive to use terminal deletion, then do so. */
1528 if (_rl_term_dc
&& (2 * col_temp
) >= -col_lendiff
)
1530 /* If all we're doing is erasing the invisible characters in the
1531 prompt string, don't bother. It screws up the assumptions
1532 about what's on the screen. */
1533 if (_rl_horizontal_scroll_mode
&& _rl_last_c_pos
== 0 &&
1534 -lendiff
== visible_wrap_offset
)
1538 delete_chars (-col_lendiff
); /* delete (diff) characters */
1540 /* Copy (new) chars to screen from first diff to last match */
1544 _rl_output_some_chars (nfd
, temp
);
1545 _rl_last_c_pos
+= _rl_col_width (nfd
, 0, temp
);;
1548 /* Otherwise, print over the existing material. */
1553 _rl_output_some_chars (nfd
, temp
);
1554 _rl_last_c_pos
+= col_temp
; /* XXX */
1556 lendiff
= (oe
- old
) - (ne
- new);
1557 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
1558 col_lendiff
= _rl_col_width (old
, 0, oe
- old
) - _rl_col_width (new, 0, ne
- new);
1560 col_lendiff
= lendiff
;
1564 if (_rl_term_autowrap
&& current_line
< inv_botlin
)
1565 space_to_eol (col_lendiff
);
1567 _rl_clear_to_eol (col_lendiff
);
1573 /* Tell the update routines that we have moved onto a new (empty) line. */
1578 visible_line
[0] = '\0';
1580 _rl_last_c_pos
= _rl_last_v_pos
= 0;
1581 _rl_vis_botlin
= last_lmargin
= 0;
1583 vis_lbreaks
[0] = vis_lbreaks
[1] = 0;
1584 visible_wrap_offset
= 0;
1588 /* Tell the update routines that we have moved onto a new line with the
1589 prompt already displayed. Code originally from the version of readline
1590 distributed with CLISP. rl_expand_prompt must have already been called
1591 (explicitly or implicitly). This still doesn't work exactly right. */
1593 rl_on_new_line_with_prompt ()
1595 int prompt_size
, i
, l
, real_screenwidth
, newlines
;
1596 char *prompt_last_line
, *lprompt
;
1598 /* Initialize visible_line and invisible_line to ensure that they can hold
1599 the already-displayed prompt. */
1600 prompt_size
= strlen (rl_prompt
) + 1;
1601 init_line_structures (prompt_size
);
1603 /* Make sure the line structures hold the already-displayed prompt for
1605 lprompt
= local_prompt
? local_prompt
: rl_prompt
;
1606 strcpy (visible_line
, lprompt
);
1607 strcpy (invisible_line
, lprompt
);
1609 /* If the prompt contains newlines, take the last tail. */
1610 prompt_last_line
= strrchr (rl_prompt
, '\n');
1611 if (!prompt_last_line
)
1612 prompt_last_line
= rl_prompt
;
1614 l
= strlen (prompt_last_line
);
1615 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
1616 _rl_last_c_pos
= _rl_col_width (prompt_last_line
, 0, l
); /* XXX */
1620 /* Dissect prompt_last_line into screen lines. Note that here we have
1621 to use the real screenwidth. Readline's notion of screenwidth might be
1622 one less, see terminal.c. */
1623 real_screenwidth
= _rl_screenwidth
+ (_rl_term_autowrap
? 0 : 1);
1624 _rl_last_v_pos
= l
/ real_screenwidth
;
1625 /* If the prompt length is a multiple of real_screenwidth, we don't know
1626 whether the cursor is at the end of the last line, or already at the
1627 beginning of the next line. Output a newline just to be safe. */
1628 if (l
> 0 && (l
% real_screenwidth
) == 0)
1629 _rl_output_some_chars ("\n", 1);
1632 newlines
= 0; i
= 0;
1635 _rl_vis_botlin
= newlines
;
1636 vis_lbreaks
[newlines
++] = i
;
1637 i
+= real_screenwidth
;
1639 vis_lbreaks
[newlines
] = l
;
1640 visible_wrap_offset
= 0;
1642 rl_display_prompt
= rl_prompt
; /* XXX - make sure it's set */
1647 /* Actually update the display, period. */
1649 rl_forced_update_display ()
1653 register char *temp
= visible_line
;
1660 (*rl_redisplay_function
) ();
1664 /* Move the cursor from _rl_last_c_pos to NEW, which are buffer indices.
1665 (Well, when we don't have multibyte characters, _rl_last_c_pos is a
1667 DATA is the contents of the screen line of interest; i.e., where
1668 the movement is being done. */
1670 _rl_move_cursor_relative (new, data
)
1675 int woff
; /* number of invisible chars on current line */
1676 int cpos
, dpos
; /* current and desired cursor positions */
1678 woff
= W_OFFSET (_rl_last_v_pos
, wrap_offset
);
1679 cpos
= _rl_last_c_pos
;
1680 #if defined (HANDLE_MULTIBYTE)
1681 /* If we have multibyte characters, NEW is indexed by the buffer point in
1682 a multibyte string, but _rl_last_c_pos is the display position. In
1683 this case, NEW's display position is not obvious and must be
1684 calculated. We need to account for invisible characters in this line,
1685 as long as we are past them and they are counted by _rl_col_width. */
1686 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
1688 dpos
= _rl_col_width (data
, 0, new);
1696 /* If we don't have to do anything, then return. */
1700 /* It may be faster to output a CR, and then move forwards instead
1701 of moving backwards. */
1702 /* i == current physical cursor position. */
1703 #if defined (HANDLE_MULTIBYTE)
1704 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
1708 i
= _rl_last_c_pos
- woff
;
1709 if (new == 0 || CR_FASTER (new, _rl_last_c_pos
) ||
1710 (_rl_term_autowrap
&& i
== _rl_screenwidth
))
1712 #if defined (__MSDOS__)
1713 putc ('\r', rl_outstream
);
1715 tputs (_rl_term_cr
, 1, _rl_output_character_function
);
1716 #endif /* !__MSDOS__ */
1717 cpos
= _rl_last_c_pos
= 0;
1722 /* Move the cursor forward. We do it by printing the command
1723 to move the cursor forward if there is one, else print that
1724 portion of the output buffer again. Which is cheaper? */
1726 /* The above comment is left here for posterity. It is faster
1727 to print one character (non-control) than to print a control
1728 sequence telling the terminal to move forward one character.
1729 That kind of control is for people who don't know what the
1730 data is underneath the cursor. */
1731 #if defined (HACK_TERMCAP_MOTION)
1732 if (_rl_term_forward_char
)
1734 for (i
= cpos
; i
< dpos
; i
++)
1735 tputs (_rl_term_forward_char
, 1, _rl_output_character_function
);
1738 #endif /* HACK_TERMCAP_MOTION */
1739 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
1741 tputs (_rl_term_cr
, 1, _rl_output_character_function
);
1742 for (i
= 0; i
< new; i
++)
1743 putc (data
[i
], rl_outstream
);
1746 for (i
= cpos
; i
< new; i
++)
1747 putc (data
[i
], rl_outstream
);
1750 #if defined (HANDLE_MULTIBYTE)
1751 /* NEW points to the buffer point, but _rl_last_c_pos is the display point.
1752 The byte length of the string is probably bigger than the column width
1753 of the string, which means that if NEW == _rl_last_c_pos, then NEW's
1754 display point is less than _rl_last_c_pos. */
1756 else if (cpos
> dpos
)
1757 _rl_backspace (cpos
- dpos
);
1759 _rl_last_c_pos
= dpos
;
1762 /* PWP: move the cursor up or down. */
1767 register int delta
, i
;
1769 if (_rl_last_v_pos
== to
|| to
> _rl_screenheight
)
1772 if ((delta
= to
- _rl_last_v_pos
) > 0)
1774 for (i
= 0; i
< delta
; i
++)
1775 putc ('\n', rl_outstream
);
1776 #if defined (__MSDOS__)
1777 putc ('\r', rl_outstream
);
1779 tputs (_rl_term_cr
, 1, _rl_output_character_function
);
1785 if (_rl_term_up
&& *_rl_term_up
)
1786 for (i
= 0; i
< -delta
; i
++)
1787 tputs (_rl_term_up
, 1, _rl_output_character_function
);
1790 _rl_last_v_pos
= to
; /* Now TO is here */
1793 /* Physically print C on rl_outstream. This is for functions which know
1794 how to optimize the display. Return the number of characters output. */
1800 if (META_CHAR (c
) && (_rl_output_meta_chars
== 0))
1802 fprintf (rl_outstream
, "M-");
1807 #if defined (DISPLAY_TABS)
1808 if ((CTRL_CHAR (c
) && c
!= '\t') || c
== RUBOUT
)
1810 if (CTRL_CHAR (c
) || c
== RUBOUT
)
1811 #endif /* !DISPLAY_TABS */
1813 fprintf (rl_outstream
, "C-");
1815 c
= CTRL_CHAR (c
) ? UNCTRL (c
) : '?';
1818 putc (c
, rl_outstream
);
1819 fflush (rl_outstream
);
1824 rl_character_len (c
, pos
)
1825 register int c
, pos
;
1829 uc
= (unsigned char)c
;
1832 return ((_rl_output_meta_chars
== 0) ? 4 : 1);
1836 #if defined (DISPLAY_TABS)
1837 return (((pos
| 7) + 1) - pos
);
1840 #endif /* !DISPLAY_TABS */
1843 if (CTRL_CHAR (c
) || c
== RUBOUT
)
1846 return ((ISPRINT (uc
)) ? 1 : 2);
1848 /* How to print things in the "echo-area". The prompt is treated as a
1850 static int msg_saved_prompt
= 0;
1852 #if defined (USE_VARARGS)
1854 #if defined (PREFER_STDARG)
1855 rl_message (const char *format
, ...)
1857 rl_message (va_alist
)
1862 #if defined (PREFER_VARARGS)
1866 #if defined (PREFER_STDARG)
1867 va_start (args
, format
);
1870 format
= va_arg (args
, char *);
1873 #if defined (HAVE_VSNPRINTF)
1874 vsnprintf (msg_buf
, sizeof (msg_buf
) - 1, format
, args
);
1876 vsprintf (msg_buf
, format
, args
);
1877 msg_buf
[sizeof(msg_buf
) - 1] = '\0'; /* overflow? */
1881 if (saved_local_prompt
== 0)
1884 msg_saved_prompt
= 1;
1886 rl_display_prompt
= msg_buf
;
1887 local_prompt
= expand_prompt (msg_buf
, &prompt_visible_length
,
1888 &prompt_last_invisible
,
1889 &prompt_invis_chars_first_line
,
1890 &prompt_physical_chars
);
1891 local_prompt_prefix
= (char *)NULL
;
1892 (*rl_redisplay_function
) ();
1896 #else /* !USE_VARARGS */
1898 rl_message (format
, arg1
, arg2
)
1901 sprintf (msg_buf
, format
, arg1
, arg2
);
1902 msg_buf
[sizeof(msg_buf
) - 1] = '\0'; /* overflow? */
1904 rl_display_prompt
= msg_buf
;
1905 if (saved_local_prompt
== 0)
1908 msg_saved_prompt
= 1;
1910 local_prompt
= expand_prompt (msg_buf
, &prompt_visible_length
,
1911 &prompt_last_invisible
,
1912 &prompt_invis_chars_first_line
,
1913 &prompt_physical_chars
);
1914 local_prompt_prefix
= (char *)NULL
;
1915 (*rl_redisplay_function
) ();
1919 #endif /* !USE_VARARGS */
1921 /* How to clear things from the "echo-area". */
1925 rl_display_prompt
= rl_prompt
;
1926 if (msg_saved_prompt
)
1928 rl_restore_prompt ();
1929 msg_saved_prompt
= 0;
1931 (*rl_redisplay_function
) ();
1936 rl_reset_line_state ()
1940 rl_display_prompt
= rl_prompt
? rl_prompt
: "";
1948 saved_local_prompt
= local_prompt
;
1949 saved_local_prefix
= local_prompt_prefix
;
1950 saved_prefix_length
= prompt_prefix_length
;
1951 saved_last_invisible
= prompt_last_invisible
;
1952 saved_visible_length
= prompt_visible_length
;
1953 saved_invis_chars_first_line
= prompt_invis_chars_first_line
;
1954 saved_physical_chars
= prompt_physical_chars
;
1956 local_prompt
= local_prompt_prefix
= (char *)0;
1957 prompt_last_invisible
= prompt_visible_length
= prompt_prefix_length
= 0;
1958 prompt_invis_chars_first_line
= prompt_physical_chars
= 0;
1962 rl_restore_prompt ()
1964 FREE (local_prompt
);
1965 FREE (local_prompt_prefix
);
1967 local_prompt
= saved_local_prompt
;
1968 local_prompt_prefix
= saved_local_prefix
;
1969 prompt_prefix_length
= saved_prefix_length
;
1970 prompt_last_invisible
= saved_last_invisible
;
1971 prompt_visible_length
= saved_visible_length
;
1972 prompt_invis_chars_first_line
= saved_invis_chars_first_line
;
1973 prompt_physical_chars
= saved_physical_chars
;
1975 /* can test saved_local_prompt to see if prompt info has been saved. */
1976 saved_local_prompt
= saved_local_prefix
= (char *)0;
1977 saved_last_invisible
= saved_visible_length
= saved_prefix_length
= 0;
1978 saved_invis_chars_first_line
= saved_physical_chars
= 0;
1982 _rl_make_prompt_for_search (pchar
)
1990 if (saved_local_prompt
== 0)
1992 len
= (rl_prompt
&& *rl_prompt
) ? strlen (rl_prompt
) : 0;
1993 pmt
= (char *)xmalloc (len
+ 2);
1995 strcpy (pmt
, rl_prompt
);
2001 len
= *saved_local_prompt
? strlen (saved_local_prompt
) : 0;
2002 pmt
= (char *)xmalloc (len
+ 2);
2004 strcpy (pmt
, saved_local_prompt
);
2007 local_prompt
= savestring (pmt
);
2008 prompt_last_invisible
= saved_last_invisible
;
2009 prompt_visible_length
= saved_visible_length
+ 1;
2012 prompt_physical_chars
= saved_physical_chars
+ 1;
2017 /* Quick redisplay hack when erasing characters at the end of the line. */
2019 _rl_erase_at_end_of_line (l
)
2025 for (i
= 0; i
< l
; i
++)
2026 putc (' ', rl_outstream
);
2028 for (i
= 0; i
< l
; i
++)
2029 visible_line
[--_rl_last_c_pos
] = '\0';
2033 /* Clear to the end of the line. COUNT is the minimum
2034 number of character spaces to clear, */
2036 _rl_clear_to_eol (count
)
2039 if (_rl_term_clreol
)
2040 tputs (_rl_term_clreol
, 1, _rl_output_character_function
);
2042 space_to_eol (count
);
2045 /* Clear to the end of the line using spaces. COUNT is the minimum
2046 number of character spaces to clear, */
2048 space_to_eol (count
)
2053 for (i
= 0; i
< count
; i
++)
2054 putc (' ', rl_outstream
);
2056 _rl_last_c_pos
+= count
;
2062 if (_rl_term_clrpag
)
2063 tputs (_rl_term_clrpag
, 1, _rl_output_character_function
);
2068 /* Insert COUNT characters from STRING to the output stream at column COL. */
2070 insert_some_chars (string
, count
, col
)
2074 #if defined (__MSDOS__) || defined (__MINGW32__)
2075 _rl_output_some_chars (string
, count
);
2078 if (MB_CUR_MAX
== 1 || rl_byte_oriented
)
2080 fprintf(stderr
, "readline: debug: insert_some_chars: count (%d) != col (%d)\n", count
, col
);
2082 /* If IC is defined, then we do not have to "enter" insert mode. */
2087 buffer
= tgoto (_rl_term_IC
, 0, col
);
2088 tputs (buffer
, 1, _rl_output_character_function
);
2089 _rl_output_some_chars (string
, count
);
2095 /* If we have to turn on insert-mode, then do so. */
2096 if (_rl_term_im
&& *_rl_term_im
)
2097 tputs (_rl_term_im
, 1, _rl_output_character_function
);
2099 /* If there is a special command for inserting characters, then
2100 use that first to open up the space. */
2101 if (_rl_term_ic
&& *_rl_term_ic
)
2103 for (i
= col
; i
--; )
2104 tputs (_rl_term_ic
, 1, _rl_output_character_function
);
2107 /* Print the text. */
2108 _rl_output_some_chars (string
, count
);
2110 /* If there is a string to turn off insert mode, we had best use
2112 if (_rl_term_ei
&& *_rl_term_ei
)
2113 tputs (_rl_term_ei
, 1, _rl_output_character_function
);
2115 #endif /* __MSDOS__ || __MINGW32__ */
2118 /* Delete COUNT characters from the display line. */
2120 delete_chars (count
)
2123 if (count
> _rl_screenwidth
) /* XXX */
2126 #if !defined (__MSDOS__) && !defined (__MINGW32__)
2127 if (_rl_term_DC
&& *_rl_term_DC
)
2130 buffer
= tgoto (_rl_term_DC
, count
, count
);
2131 tputs (buffer
, count
, _rl_output_character_function
);
2135 if (_rl_term_dc
&& *_rl_term_dc
)
2137 tputs (_rl_term_dc
, 1, _rl_output_character_function
);
2139 #endif /* !__MSDOS__ && !__MINGW32__ */
2148 /* If the cursor is the only thing on an otherwise-blank last line,
2149 compensate so we don't print an extra CRLF. */
2150 if (_rl_vis_botlin
&& _rl_last_c_pos
== 0 &&
2151 visible_line
[vis_lbreaks
[_rl_vis_botlin
]] == 0)
2156 _rl_move_vert (_rl_vis_botlin
);
2157 /* If we've wrapped lines, remove the final xterm line-wrap flag. */
2158 if (full_lines
&& _rl_term_autowrap
&& (VIS_LLEN(_rl_vis_botlin
) == _rl_screenwidth
))
2162 last_line
= &visible_line
[vis_lbreaks
[_rl_vis_botlin
]];
2163 _rl_move_cursor_relative (_rl_screenwidth
- 1, last_line
);
2164 _rl_clear_to_eol (0);
2165 putc (last_line
[_rl_screenwidth
- 1], rl_outstream
);
2169 fflush (rl_outstream
);
2173 /* Move to the start of the current line. */
2179 #if defined (__MSDOS__)
2180 putc ('\r', rl_outstream
);
2182 tputs (_rl_term_cr
, 1, _rl_output_character_function
);
2188 /* Redraw the last line of a multi-line prompt that may possibly contain
2189 terminal escape sequences. Called with the cursor at column 0 of the
2190 line to draw the prompt on. */
2197 oldp
= rl_display_prompt
;
2200 rl_display_prompt
= t
;
2201 local_prompt
= expand_prompt (t
, &prompt_visible_length
,
2202 &prompt_last_invisible
,
2203 &prompt_invis_chars_first_line
,
2204 &prompt_physical_chars
);
2205 local_prompt_prefix
= (char *)NULL
;
2207 rl_forced_update_display ();
2209 rl_display_prompt
= oldp
;
2210 rl_restore_prompt();
2213 /* Redisplay the current line after a SIGWINCH is received. */
2215 _rl_redisplay_after_sigwinch ()
2219 /* Clear the current line and put the cursor at column 0. Make sure
2220 the right thing happens if we have wrapped to a new screen line. */
2223 #if defined (__MSDOS__)
2224 putc ('\r', rl_outstream
);
2226 tputs (_rl_term_cr
, 1, _rl_output_character_function
);
2229 #if defined (__MSDOS__)
2230 space_to_eol (_rl_screenwidth
);
2231 putc ('\r', rl_outstream
);
2233 if (_rl_term_clreol
)
2234 tputs (_rl_term_clreol
, 1, _rl_output_character_function
);
2237 space_to_eol (_rl_screenwidth
);
2238 tputs (_rl_term_cr
, 1, _rl_output_character_function
);
2241 if (_rl_last_v_pos
> 0)
2247 /* Redraw only the last line of a multi-line prompt. */
2248 t
= strrchr (rl_display_prompt
, '\n');
2250 redraw_prompt (++t
);
2252 rl_forced_update_display ();
2256 _rl_clean_up_for_exit ()
2258 if (readline_echoing_p
)
2260 _rl_move_vert (_rl_vis_botlin
);
2262 fflush (rl_outstream
);
2263 rl_restart_output (1, 0);
2268 _rl_erase_entire_line ()
2271 _rl_clear_to_eol (0);
2273 fflush (rl_outstream
);
2276 /* return the `current display line' of the cursor -- the number of lines to
2277 move up to get to the first screen line of the current readline line. */
2279 _rl_current_display_line ()
2283 /* Find out whether or not there might be invisible characters in the
2285 if (rl_display_prompt
== rl_prompt
)
2286 nleft
= _rl_last_c_pos
- _rl_screenwidth
- rl_visible_prompt_length
;
2288 nleft
= _rl_last_c_pos
- _rl_screenwidth
;
2291 ret
= 1 + nleft
/ _rl_screenwidth
;
2298 #if defined (HANDLE_MULTIBYTE)
2299 /* Calculate the number of screen columns occupied by STR from START to END.
2300 In the case of multibyte characters with stateful encoding, we have to
2301 scan from the beginning of the string to take the state into account. */
2303 _rl_col_width (str
, start
, end
)
2309 int tmp
, point
, width
, max
;
2317 while (point
< start
)
2319 tmp
= mbrlen (str
+ point
, max
, &ps
);
2320 if (MB_INVALIDCH ((size_t)tmp
))
2322 /* In this case, the bytes are invalid or too short to compose a
2323 multibyte character, so we assume that the first byte represents
2324 a single character. */
2328 /* Clear the state of the byte sequence, because in this case the
2329 effect of mbstate is undefined. */
2330 memset (&ps
, 0, sizeof (mbstate_t));
2332 else if (MB_NULLWCH (tmp
))
2333 break; /* Found '\0' */
2341 /* If START is not a byte that starts a character, then POINT will be
2342 greater than START. In this case, assume that (POINT - START) gives
2343 a byte count that is the number of columns of difference. */
2344 width
= point
- start
;
2348 tmp
= mbrtowc (&wc
, str
+ point
, max
, &ps
);
2349 if (MB_INVALIDCH ((size_t)tmp
))
2351 /* In this case, the bytes are invalid or too short to compose a
2352 multibyte character, so we assume that the first byte represents
2353 a single character. */
2357 /* and assume that the byte occupies a single column. */
2360 /* Clear the state of the byte sequence, because in this case the
2361 effect of mbstate is undefined. */
2362 memset (&ps
, 0, sizeof (mbstate_t));
2364 else if (MB_NULLWCH (tmp
))
2365 break; /* Found '\0' */
2371 width
+= (tmp
>= 0) ? tmp
: 1;
2375 width
+= point
- end
;
2379 #endif /* HANDLE_MULTIBYTE */