1 /* display.c -- readline redisplay facility. */
3 /* Copyright (C) 1987-2022 Free Software Foundation, Inc.
5 This file is part of the GNU Readline Library (Readline), a library
6 for reading lines of text with interactive input and history editing.
8 Readline is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 Readline is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Readline. If not, see <http://www.gnu.org/licenses/>.
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 */
48 /* System-specific feature definitions and include files. */
52 /* Termcap library stuff. */
55 /* Some standard library routines. */
59 #include "rlprivate.h"
62 #if !defined (strchr) && !defined (__STDC__)
63 extern char *strchr (), *strrchr ();
64 #endif /* !strchr && !__STDC__ */
66 static void putc_face (int, int, char *);
67 static void puts_face (const char *, const char *, int);
68 static void norm_face (char *, int);
70 static void update_line (char *, char *, char *, char *, int, int, int, int);
71 static void space_to_eol (int);
72 static void delete_chars (int);
73 static void insert_some_chars (char *, int, int);
74 static void open_some_spaces (int);
75 static void cr (void);
76 static void redraw_prompt (char *);
77 static void _rl_move_cursor_relative (int, const char *, const char *);
79 /* Values for FLAGS */
80 #define PMT_MULTILINE 0x01
82 static char *expand_prompt (char *, int, int *, int *, int *, int *);
84 #define DEFAULT_LINE_BUFFER_SIZE 1024
86 /* State of visible and invisible lines. */
93 #if defined (HANDLE_MULTIBYTE)
99 /* The line display buffers. One is the line currently displayed on
100 the screen. The other is the line about to be displayed. */
101 static struct line_state line_state_array
[2];
102 static struct line_state
*line_state_visible
= &line_state_array
[0];
103 static struct line_state
*line_state_invisible
= &line_state_array
[1];
104 static int line_structures_initialized
= 0;
106 /* Backwards-compatible names. */
107 #define inv_lbreaks (line_state_invisible->lbreaks)
108 #define inv_lbsize (line_state_invisible->lbsize)
109 #define vis_lbreaks (line_state_visible->lbreaks)
110 #define vis_lbsize (line_state_visible->lbsize)
112 #define visible_line (line_state_visible->line)
113 #define vis_face (line_state_visible->lface)
114 #define invisible_line (line_state_invisible->line)
115 #define inv_face (line_state_invisible->lface)
117 #if defined (HANDLE_MULTIBYTE)
118 static int _rl_col_width (const char *, int, int, int);
120 # define _rl_col_width(l, s, e, f) (((e) <= (s)) ? 0 : (e) - (s))
123 /* Heuristic used to decide whether it is faster to move from CUR to NEW
124 by backing up or outputting a carriage return and moving forward. CUR
125 and NEW are either both buffer positions or absolute screen positions. */
126 #define CR_FASTER(new, cur) (((new) + 1) < ((cur) - (new)))
128 /* _rl_last_c_pos is an absolute cursor position in multibyte locales and a
129 buffer index in others. This macro is used when deciding whether the
130 current cursor position is in the middle of a prompt string containing
131 invisible characters. XXX - might need to take `modmark' into account. */
132 /* XXX - only valid when tested against _rl_last_c_pos; buffer indices need
133 to use prompt_last_invisible directly. */
134 #define PROMPT_ENDING_INDEX \
135 ((MB_CUR_MAX > 1 && rl_byte_oriented == 0) ? prompt_physical_chars : prompt_last_invisible+1)
137 #define FACE_NORMAL '0'
138 #define FACE_STANDOUT '1'
139 #define FACE_INVALID ((char)1)
141 /* **************************************************************** */
145 /* **************************************************************** */
147 /* This is the stuff that is hard for me. I never seem to write good
148 display routines in C. Let's see how I do this time. */
150 /* (PWP) Well... Good for a simple line updater, but totally ignores
151 the problems of input lines longer than the screen width.
153 update_line and the code that calls it makes a multiple line,
154 automatically wrapping line update. Careful attention needs
155 to be paid to the vertical position variables. */
157 /* Keep two buffers; one which reflects the current contents of the
158 screen, and the other to draw what we think the new contents should
159 be. Then compare the buffers, and make whatever changes to the
160 screen itself that we should. Finally, make the buffer that we
161 just drew into be the one which reflects the current contents of the
162 screen, and place the cursor where it belongs.
164 Commands that want to can fix the display themselves, and then let
165 this function know that the display has been fixed by setting the
166 RL_DISPLAY_FIXED variable. This is good for efficiency. */
168 /* Application-specific redisplay function. */
169 rl_voidfunc_t
*rl_redisplay_function
= rl_redisplay
;
171 /* Global variables declared here. */
172 /* What YOU turn on when you have handled all redisplay yourself. */
173 int rl_display_fixed
= 0;
175 /* The stuff that gets printed out before the actual text of the line.
176 This is usually pointing to rl_prompt. */
177 char *rl_display_prompt
= (char *)NULL
;
179 /* Variables used to include the editing mode in the prompt. */
180 char *_rl_emacs_mode_str
;
181 int _rl_emacs_modestr_len
;
183 char *_rl_vi_ins_mode_str
;
184 int _rl_vi_ins_modestr_len
;
186 char *_rl_vi_cmd_mode_str
;
187 int _rl_vi_cmd_modestr_len
;
189 /* Pseudo-global variables declared here. */
191 /* Hints for other parts of readline to give to the display engine. */
192 int _rl_suppress_redisplay
= 0;
193 int _rl_want_redisplay
= 0;
195 /* The visible cursor position. If you print some text, adjust this. */
196 /* NOTE: _rl_last_c_pos is used as a buffer index when not in a locale
197 supporting multibyte characters, and an absolute cursor position when
198 in such a locale. This is an artifact of the donated multibyte support.
199 Care must be taken when modifying its value. */
200 int _rl_last_c_pos
= 0;
201 int _rl_last_v_pos
= 0;
203 /* Number of physical lines consumed by the current line buffer currently
204 on screen minus 1. */
205 int _rl_vis_botlin
= 0;
207 static int _rl_quick_redisplay
= 0;
209 /* This is a hint update_line gives to rl_redisplay that it has adjusted the
210 value of _rl_last_c_pos *and* taken the presence of any invisible chars in
211 the prompt into account. rl_redisplay notes this and does not do the
212 adjustment itself. */
213 static int cpos_adjusted
;
215 /* The index into the line buffer corresponding to the cursor position */
216 static int cpos_buffer_position
;
218 /* A flag to note when we're displaying the first line of the prompt */
219 static int displaying_prompt_first_line
;
220 /* The number of multibyte characters in the prompt, if any */
221 static int prompt_multibyte_chars
;
223 static int _rl_inv_botlin
= 0;
225 /* Variables used only in this file. */
226 /* The last left edge of text that was displayed. This is used when
227 doing horizontal scrolling. It shifts in thirds of a screenwidth. */
228 static int last_lmargin
;
230 /* A buffer for `modeline' messages. */
231 static char *msg_buf
= 0;
232 static int msg_bufsiz
= 0;
234 /* Non-zero forces the redisplay even if we thought it was unnecessary. */
235 static int forced_display
;
237 /* Default and initial buffer size. Can grow. */
238 static int line_size
= 0;
240 /* Set to a non-zero value if horizontal scrolling has been enabled
241 automatically because the terminal was resized to height 1. */
242 static int horizontal_scrolling_autoset
= 0; /* explicit initialization */
244 /* Variables to keep track of the expanded prompt string, which may
245 include invisible characters. */
247 static char *local_prompt
, *local_prompt_prefix
;
248 static int local_prompt_len
;
249 static int prompt_prefix_length
;
250 /* Number of chars in the buffer that contribute to visible chars on the screen.
251 This might be different from the number of physical chars in the presence
252 of multibyte characters */
253 static int prompt_visible_length
;
255 /* The number of invisible characters in the line currently being
256 displayed on the screen. */
257 static int visible_wrap_offset
;
259 /* The number of invisible characters in the prompt string. Static so it
260 can be shared between rl_redisplay and update_line */
261 static int wrap_offset
;
263 /* The index of the last invisible character in the prompt string. */
264 static int prompt_last_invisible
;
266 /* The length (buffer offset) of the first line of the last (possibly
267 multi-line) buffer displayed on the screen. */
268 static int visible_first_line_len
;
270 /* Number of invisible characters on the first physical line of the prompt.
271 Only valid when the number of physical characters in the prompt exceeds
272 (or is equal to) _rl_screenwidth. */
273 static int prompt_invis_chars_first_line
;
275 static int prompt_last_screen_line
;
277 static int prompt_physical_chars
;
279 /* An array of indexes into the prompt string where we will break physical
280 screen lines. It's easier to compute in expand_prompt and use later in
281 rl_redisplay instead of having rl_redisplay try to guess about invisible
282 characters in the prompt or use heuristics about where they are. */
283 static int *local_prompt_newlines
;
285 /* set to a non-zero value by rl_redisplay if we are marking modified history
286 lines and the current line is so marked. */
289 static int line_totbytes
;
291 /* Variables to save and restore prompt and display information. */
293 /* These are getting numerous enough that it's time to create a struct. */
295 static char *saved_local_prompt
;
296 static char *saved_local_prefix
;
297 static int *saved_local_prompt_newlines
;
299 static int saved_last_invisible
;
300 static int saved_visible_length
;
301 static int saved_prefix_length
;
302 static int saved_local_length
;
303 static int saved_invis_chars_first_line
;
304 static int saved_physical_chars
;
306 /* Return a string indicating the editing mode, for use in the prompt. */
309 prompt_modestr (int *lenp
)
311 if (rl_editing_mode
== emacs_mode
)
314 *lenp
= _rl_emacs_mode_str
? _rl_emacs_modestr_len
: RL_EMACS_MODESTR_DEFLEN
;
315 return _rl_emacs_mode_str
? _rl_emacs_mode_str
: RL_EMACS_MODESTR_DEFAULT
;
317 else if (_rl_keymap
== vi_insertion_keymap
)
320 *lenp
= _rl_vi_ins_mode_str
? _rl_vi_ins_modestr_len
: RL_VI_INS_MODESTR_DEFLEN
;
321 return _rl_vi_ins_mode_str
? _rl_vi_ins_mode_str
: RL_VI_INS_MODESTR_DEFAULT
; /* vi insert mode */
326 *lenp
= _rl_vi_cmd_mode_str
? _rl_vi_cmd_modestr_len
: RL_VI_CMD_MODESTR_DEFLEN
;
327 return _rl_vi_cmd_mode_str
? _rl_vi_cmd_mode_str
: RL_VI_CMD_MODESTR_DEFAULT
; /* vi command mode */
331 /* Expand the prompt string S and return the number of visible
332 characters in *LP, if LP is not null. This is currently more-or-less
333 a placeholder for expansion. LIP, if non-null is a place to store the
334 index of the last invisible character in the returned string. NIFLP,
335 if non-zero, is a place to store the number of invisible characters in
336 the first prompt line. The previous are used as byte counts -- indexes
337 into a character buffer. *VLP gets the number of physical characters in
338 the expanded prompt (visible length) */
340 /* Current implementation:
341 \001 (^A) start non-visible characters
342 \002 (^B) end non-visible characters
343 all characters except \001 and \002 (following a \001) are copied to
344 the returned string; all characters except those between \001 and
345 \002 are assumed to be `visible'. */
347 /* Possible values for FLAGS:
348 PMT_MULTILINE caller indicates that this is part of a multiline prompt
351 /* This approximates the number of lines the prompt will take when displayed */
352 #define APPROX_DIV(n, d) (((n) < (d)) ? 1 : ((n) / (d)) + 1)
355 expand_prompt (char *pmt
, int flags
, int *lp
, int *lip
, int *niflp
, int *vlp
)
357 char *r
, *ret
, *p
, *igstart
, *nprompt
, *ms
;
358 int l
, rl
, last
, ignoring
, ninvis
, invfl
, invflset
, ind
, pind
, physchars
;
359 int mlen
, newlines
, newlines_guess
, bound
, can_add_invis
;
362 /* We only expand the mode string for the last line of a multiline prompt
363 (a prompt with embedded newlines). */
364 ms
= (((pmt
== rl_prompt
) ^ (flags
& PMT_MULTILINE
)) && _rl_show_mode_in_prompt
) ? prompt_modestr (&mlen
) : 0;
368 nprompt
= (char *)xmalloc (l
+ mlen
+ 1);
369 memcpy (nprompt
, ms
, mlen
);
370 strcpy (nprompt
+ mlen
, pmt
);
376 mb_cur_max
= MB_CUR_MAX
;
378 if (_rl_screenwidth
== 0)
379 _rl_get_screen_size (0, 0); /* avoid division by zero */
381 /* Short-circuit if we can. We can do this if we are treating the prompt as
382 a sequence of bytes and there are no invisible characters in the prompt
383 to deal with. Since we populate local_prompt_newlines, we have to run
384 through the rest of the function if this prompt looks like it's going to
385 be longer than one screen line. */
386 if ((mb_cur_max
<= 1 || rl_byte_oriented
) && strchr (nprompt
, RL_PROMPT_START_IGNORE
) == 0)
388 l
= strlen (nprompt
);
389 if (l
< (_rl_screenwidth
> 0 ? _rl_screenwidth
: 80))
391 r
= (nprompt
== pmt
) ? savestring (pmt
) : nprompt
;
401 local_prompt_newlines
= (int *) xrealloc (local_prompt_newlines
, sizeof (int) * 2);
402 local_prompt_newlines
[0] = 0;
403 local_prompt_newlines
[1] = -1;
409 l
= strlen (nprompt
); /* XXX */
410 r
= ret
= (char *)xmalloc (l
+ 1);
412 /* Guess at how many screen lines the prompt will take to size the array that
413 keeps track of where the line wraps happen */
414 newlines_guess
= (_rl_screenwidth
> 0) ? APPROX_DIV(l
, _rl_screenwidth
) : APPROX_DIV(l
, 80);
415 local_prompt_newlines
= (int *) xrealloc (local_prompt_newlines
, sizeof (int) * (newlines_guess
+ 1));
416 local_prompt_newlines
[newlines
= 0] = 0;
417 for (rl
= 1; rl
<= newlines_guess
; rl
++)
418 local_prompt_newlines
[rl
] = -1;
420 rl
= physchars
= 0; /* mode string now part of nprompt */
421 invfl
= 0; /* invisible chars in first line of prompt */
422 invflset
= 0; /* we only want to set invfl once */
423 igstart
= 0; /* we're not ignoring any characters yet */
425 for (ignoring
= last
= ninvis
= 0, p
= nprompt
; p
&& *p
; p
++)
427 /* This code strips the invisible character string markers
428 RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE */
429 if (ignoring
== 0 && *p
== RL_PROMPT_START_IGNORE
) /* XXX - check ignoring? */
435 else if (ignoring
&& *p
== RL_PROMPT_END_IGNORE
)
438 /* If we have a run of invisible characters, adjust local_prompt_newlines
439 to add them, since update_line expects them to be counted before
440 wrapping the line. */
443 local_prompt_newlines
[newlines
] = r
- ret
;
444 /* If we're adding to the number of invisible characters on the
445 first line of the prompt, but we've already set the number of
446 invisible characters on that line, we need to adjust the
448 if (invflset
&& newlines
== 1)
451 if (p
!= (igstart
+ 1))
457 #if defined (HANDLE_MULTIBYTE)
458 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
461 ind
= _rl_find_next_mbchar (nprompt
, pind
, 1, MB_FIND_NONZERO
);
467 /* rl ends up being assigned to prompt_visible_length,
468 which is the number of characters in the buffer that
469 contribute to characters on the screen, which might
470 not be the same as the number of physical characters
471 on the screen in the presence of multibyte characters */
473 physchars
+= _rl_col_width (nprompt
, pind
, ind
, 0);
476 ninvis
+= ind
- pind
;
477 p
--; /* compensate for later increment */
485 rl
++; /* visible length byte counter */
489 ninvis
++; /* invisible chars byte counter */
492 if (invflset
== 0 && physchars
>= _rl_screenwidth
)
498 if (physchars
>= (bound
= (newlines
+ 1) * _rl_screenwidth
) && local_prompt_newlines
[newlines
+1] == -1)
501 if (physchars
> bound
) /* should rarely happen */
503 #if defined (HANDLE_MULTIBYTE)
504 *r
= '\0'; /* need null-termination for strlen */
505 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
506 new = _rl_find_prev_mbchar (ret
, r
- ret
, MB_FIND_ANY
);
509 new = r
- ret
- (physchars
- bound
); /* XXX */
513 local_prompt_newlines
[++newlines
] = new;
516 /* What if a physical character of width >= 2 is split? There is
517 code that wraps before the physical screen width if the character
518 width would exceed it, but it needs to be checked against this
519 code and local_prompt_newlines[]. */
521 can_add_invis
= (physchars
== bound
);
525 if (rl
<= _rl_screenwidth
)
544 /* Just strip out RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE from
545 PMT and return the rest of PMT. */
547 _rl_strip_prompt (char *pmt
)
551 ret
= expand_prompt (pmt
, 0, (int *)NULL
, (int *)NULL
, (int *)NULL
, (int *)NULL
);
556 _rl_reset_prompt (void)
558 rl_visible_prompt_length
= rl_expand_prompt (rl_prompt
);
562 * Expand the prompt string into the various display components, if
565 * local_prompt = expanded last line of string in rl_display_prompt
566 * (portion after the final newline)
567 * local_prompt_prefix = portion before last newline of rl_display_prompt,
568 * expanded via expand_prompt
569 * prompt_visible_length = number of visible characters in local_prompt
570 * prompt_prefix_length = number of visible characters in local_prompt_prefix
572 * It also tries to keep track of the number of invisible characters in the
573 * prompt string, and where they are.
575 * This function is called once per call to readline(). It may also be
576 * called arbitrarily to expand the primary prompt.
578 * The return value is the number of visible characters on the last line
579 * of the (possibly multi-line) prompt. In this case, multi-line means
580 * there are embedded newlines in the prompt string itself, not that the
581 * number of physical characters exceeds the screen width and the prompt
585 rl_expand_prompt (char *prompt
)
590 /* Clear out any saved values. */
592 FREE (local_prompt_prefix
);
594 local_prompt
= local_prompt_prefix
= (char *)0;
595 local_prompt_len
= 0;
596 prompt_last_invisible
= prompt_invis_chars_first_line
= 0;
597 prompt_visible_length
= prompt_physical_chars
= 0;
599 if (prompt
== 0 || *prompt
== 0)
602 p
= strrchr (prompt
, '\n');
605 /* The prompt is only one logical line, though it might wrap. */
606 local_prompt
= expand_prompt (prompt
, 0, &prompt_visible_length
,
607 &prompt_last_invisible
,
608 &prompt_invis_chars_first_line
,
609 &prompt_physical_chars
);
610 local_prompt_prefix
= (char *)0;
611 local_prompt_len
= local_prompt
? strlen (local_prompt
) : 0;
612 return (prompt_visible_length
);
616 /* The prompt spans multiple lines. */
619 /* The portion of the prompt string up to and including the
620 final newline is now null-terminated. */
621 local_prompt_prefix
= expand_prompt (prompt
, PMT_MULTILINE
,
622 &prompt_prefix_length
,
628 local_prompt
= expand_prompt (p
, PMT_MULTILINE
,
629 &prompt_visible_length
,
630 &prompt_last_invisible
,
631 &prompt_invis_chars_first_line
,
632 &prompt_physical_chars
);
633 local_prompt_len
= local_prompt
? strlen (local_prompt
) : 0;
634 return (prompt_prefix_length
);
638 /* Allocate the various line structures, making sure they can hold MINSIZE
639 bytes. If the existing line size can accommodate MINSIZE bytes, don't do
642 realloc_line (int minsize
)
647 minimum_size
= DEFAULT_LINE_BUFFER_SIZE
;
648 if (minsize
< minimum_size
)
649 minsize
= minimum_size
;
650 if (minsize
<= _rl_screenwidth
) /* XXX - for gdb */
651 minsize
= _rl_screenwidth
+ 1;
652 if (line_size
>= minsize
)
655 newsize
= minimum_size
;
656 while (newsize
< minsize
)
659 visible_line
= (char *)xrealloc (visible_line
, newsize
);
660 vis_face
= (char *)xrealloc (vis_face
, newsize
);
662 invisible_line
= (char *)xrealloc (invisible_line
, newsize
);
663 inv_face
= (char *)xrealloc (inv_face
, newsize
);
665 delta
= newsize
- line_size
;
666 memset (visible_line
+ line_size
, 0, delta
);
667 memset (vis_face
+ line_size
, FACE_NORMAL
, delta
);
668 memset (invisible_line
+ line_size
, 1, delta
);
669 memset (inv_face
+ line_size
, FACE_INVALID
, delta
);
674 /* Initialize the VISIBLE_LINE and INVISIBLE_LINE arrays, and their associated
675 arrays of line break markers. MINSIZE is the minimum size of VISIBLE_LINE
676 and INVISIBLE_LINE; if it is greater than LINE_SIZE, LINE_SIZE is
677 increased. If the lines have already been allocated, this ensures that
678 they can hold at least MINSIZE characters. */
680 init_line_structures (int minsize
)
682 if (invisible_line
== 0) /* initialize it */
684 if (line_size
> minsize
)
687 realloc_line (minsize
);
689 if (vis_lbreaks
== 0)
691 /* should be enough. */
692 inv_lbsize
= vis_lbsize
= 256;
694 #if defined (HANDLE_MULTIBYTE)
695 line_state_visible
->wbsize
= vis_lbsize
;
696 line_state_visible
->wrapped_line
= (int *)xmalloc (line_state_visible
->wbsize
* sizeof (int));
698 line_state_invisible
->wbsize
= inv_lbsize
;
699 line_state_invisible
->wrapped_line
= (int *)xmalloc (line_state_invisible
->wbsize
* sizeof (int));
702 inv_lbreaks
= (int *)xmalloc (inv_lbsize
* sizeof (int));
703 vis_lbreaks
= (int *)xmalloc (vis_lbsize
* sizeof (int));
704 inv_lbreaks
[0] = vis_lbreaks
[0] = 0;
707 line_structures_initialized
= 1;
710 /* Convenience functions to add chars to the invisible line that update the
711 face information at the same time. */
712 static void /* XXX - change this */
713 invis_addc (int *outp
, char c
, char face
)
715 realloc_line (*outp
+ 1);
716 invisible_line
[*outp
] = c
;
717 inv_face
[*outp
] = face
;
722 invis_adds (int *outp
, const char *str
, int n
, char face
)
726 for (i
= 0; i
< n
; i
++)
727 invis_addc (outp
, str
[i
], face
);
731 invis_nul (int *outp
)
733 invis_addc (outp
, '\0', 0);
738 set_active_region (int *beg
, int *end
)
740 if (rl_point
>= 0 && rl_point
<= rl_end
&& rl_mark
>= 0 && rl_mark
<= rl_end
)
742 *beg
= (rl_mark
< rl_point
) ? rl_mark
: rl_point
;
743 *end
= (rl_mark
< rl_point
) ? rl_point
: rl_mark
;
747 /* Do whatever tests are necessary and tell update_line that it can do a
748 quick, dumb redisplay on the assumption that there are so many
749 differences between the old and new lines that it would be a waste to
750 compute all the differences.
751 Right now, it just sets _rl_quick_redisplay if the current visible line
752 is a single line (so we don't have to move vertically or mess with line
755 _rl_optimize_redisplay (void)
757 if (_rl_vis_botlin
== 0)
758 _rl_quick_redisplay
= 1;
761 /* Basic redisplay algorithm. See comments inline. */
765 int in
, out
, c
, linenum
, cursor_linenum
;
766 int inv_botlin
, lb_botlin
, lb_linenum
, o_cpos
;
767 int newlines
, lpos
, temp
, n0
, num
, prompt_lines_estimate
;
768 char *prompt_this_line
;
770 int hl_begin
, hl_end
;
771 int mb_cur_max
= MB_CUR_MAX
;
772 #if defined (HANDLE_MULTIBYTE)
777 int _rl_wrapped_multicolumn
= 0;
780 if (_rl_echoing_p
== 0)
783 /* Block keyboard interrupts because this function manipulates global
786 RL_SETSTATE (RL_STATE_REDISPLAYING
);
788 cur_face
= FACE_NORMAL
;
789 /* Can turn this into an array for multiple highlighted objects in addition
791 hl_begin
= hl_end
= -1;
793 if (rl_mark_active_p ())
794 set_active_region (&hl_begin
, &hl_end
);
796 if (!rl_display_prompt
)
797 rl_display_prompt
= "";
799 if (line_structures_initialized
== 0)
801 init_line_structures (0);
804 else if (line_size
<= _rl_screenwidth
)
805 init_line_structures (_rl_screenwidth
+ 1);
807 /* Enable horizontal scrolling automatically for terminals of height 1
808 where wrapping lines doesn't work. Disable it as soon as the terminal
809 height is increased again if it was automatically enabled. */
810 if (_rl_screenheight
<= 1)
812 if (_rl_horizontal_scroll_mode
== 0)
813 horizontal_scrolling_autoset
= 1;
814 _rl_horizontal_scroll_mode
= 1;
816 else if (horizontal_scrolling_autoset
)
817 _rl_horizontal_scroll_mode
= 0;
819 /* Draw the line into the buffer. */
820 cpos_buffer_position
= -1;
822 prompt_multibyte_chars
= prompt_visible_length
- prompt_physical_chars
;
824 out
= inv_botlin
= 0;
826 /* Mark the line as modified or not. We only do this for history
829 if (_rl_mark_modified_lines
&& current_history () && rl_undo_list
)
831 invis_addc (&out
, '*', cur_face
);
836 /* If someone thought that the redisplay was handled, but the currently
837 visible line has a different modification state than the one about
838 to become visible, then correct the caller's misconception. */
839 if (visible_line
[0] != invisible_line
[0])
840 rl_display_fixed
= 0;
842 /* If the prompt to be displayed is the `primary' readline prompt (the
843 one passed to readline()), use the values we have already expanded.
844 If not, use what's already in rl_display_prompt. WRAP_OFFSET is the
845 number of non-visible characters (bytes) in the prompt string. */
846 /* This is where we output the characters in the prompt before the last
847 newline, if any. If there aren't any embedded newlines, we don't
848 write anything. Copy the last line of the prompt string into the line in
850 if (rl_display_prompt
== rl_prompt
|| local_prompt
)
852 if (local_prompt_prefix
&& forced_display
)
853 _rl_output_some_chars (local_prompt_prefix
, strlen (local_prompt_prefix
));
855 if (local_prompt_len
> 0)
856 invis_adds (&out
, local_prompt
, local_prompt_len
, cur_face
);
858 wrap_offset
= local_prompt_len
- prompt_visible_length
;
863 prompt_this_line
= strrchr (rl_display_prompt
, '\n');
864 if (!prompt_this_line
)
865 prompt_this_line
= rl_display_prompt
;
869 pmtlen
= prompt_this_line
- rl_display_prompt
; /* temp var */
872 _rl_output_some_chars (rl_display_prompt
, pmtlen
);
873 /* Make sure we are at column zero even after a newline,
874 regardless of the state of terminal output processing. */
875 if (pmtlen
< 2 || prompt_this_line
[-2] != '\r')
880 prompt_physical_chars
= pmtlen
= strlen (prompt_this_line
); /* XXX */
881 invis_adds (&out
, prompt_this_line
, pmtlen
, cur_face
);
883 wrap_offset
= prompt_invis_chars_first_line
= 0;
886 #if defined (HANDLE_MULTIBYTE)
887 #define CHECK_INV_LBREAKS() \
889 if (newlines >= (inv_lbsize - 2)) \
892 inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
894 if (newlines >= (line_state_invisible->wbsize - 2)) \
896 line_state_invisible->wbsize *= 2; \
897 line_state_invisible->wrapped_line = (int *)xrealloc (line_state_invisible->wrapped_line, line_state_invisible->wbsize * sizeof(int)); \
901 #define CHECK_INV_LBREAKS() \
903 if (newlines >= (inv_lbsize - 2)) \
906 inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
909 #endif /* !HANDLE_MULTIBYTE */
911 #if defined (HANDLE_MULTIBYTE)
912 #define CHECK_LPOS() \
915 if (lpos >= _rl_screenwidth) \
917 if (newlines >= (inv_lbsize - 2)) \
920 inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
922 inv_lbreaks[++newlines] = out; \
923 if (newlines >= (line_state_invisible->wbsize - 2)) \
925 line_state_invisible->wbsize *= 2; \
926 line_state_invisible->wrapped_line = (int *)xrealloc (line_state_invisible->wrapped_line, line_state_invisible->wbsize * sizeof(int)); \
928 line_state_invisible->wrapped_line[newlines] = _rl_wrapped_multicolumn; \
933 #define CHECK_LPOS() \
936 if (lpos >= _rl_screenwidth) \
938 if (newlines >= (inv_lbsize - 2)) \
941 inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
943 inv_lbreaks[++newlines] = out; \
949 /* inv_lbreaks[i] is where line i starts in the buffer. */
950 inv_lbreaks
[newlines
= 0] = 0;
951 /* lpos is a physical cursor position, so it needs to be adjusted by the
952 number of invisible characters in the prompt, per line. We compute
953 the line breaks in the prompt string in expand_prompt, taking invisible
954 characters into account, and if lpos exceeds the screen width, we copy
955 the data in the loop below. */
956 lpos
= prompt_physical_chars
+ modmark
;
958 #if defined (HANDLE_MULTIBYTE)
959 memset (line_state_invisible
->wrapped_line
, 0, line_state_invisible
->wbsize
* sizeof (int));
963 /* prompt_invis_chars_first_line is the number of invisible characters (bytes)
964 in the first physical line of the prompt.
965 wrap_offset - prompt_invis_chars_first_line is usually the number of
966 invis chars on the second (or, more generally, last) line. */
968 /* This is zero-based, used to set the newlines */
969 prompt_lines_estimate
= lpos
/ _rl_screenwidth
;
971 /* what if lpos is already >= _rl_screenwidth before we start drawing the
972 contents of the command line? */
973 if (lpos
>= _rl_screenwidth
)
977 /* first copy the linebreaks array we computed in expand_prompt */
978 while (local_prompt_newlines
[newlines
+1] != -1)
980 temp
= local_prompt_newlines
[newlines
+1];
981 inv_lbreaks
[++newlines
] = temp
;
984 /* Now set lpos from the last newline */
985 if (mb_cur_max
> 1 && rl_byte_oriented
== 0 && prompt_multibyte_chars
> 0)
986 lpos
= _rl_col_width (local_prompt
, temp
, local_prompt_len
, 1) - (wrap_offset
- prompt_invis_chars_first_line
);
988 lpos
-= (_rl_screenwidth
* newlines
);
991 prompt_last_screen_line
= newlines
;
993 /* Draw the rest of the line (after the prompt) into invisible_line, keeping
994 track of where the cursor is (cpos_buffer_position), the number of the
995 line containing the cursor (lb_linenum), the last line number (lb_botlin
997 It maintains an array of line breaks for display (inv_lbreaks).
998 This handles expanding tabs for display and displaying meta characters. */
1000 #if defined (HANDLE_MULTIBYTE)
1002 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
1004 memset (&ps
, 0, sizeof (mbstate_t));
1005 if (_rl_utf8locale
&& UTF8_SINGLEBYTE(rl_line_buffer
[0]))
1007 wc
= (WCHAR_T
)rl_line_buffer
[0];
1011 wc_bytes
= MBRTOWC (&wc
, rl_line_buffer
, rl_end
, &ps
);
1017 for (in
= 0; in
< rl_end
; in
++)
1021 cur_face
= FACE_STANDOUT
;
1022 else if (in
== hl_end
)
1023 cur_face
= FACE_NORMAL
;
1025 c
= (unsigned char)rl_line_buffer
[in
];
1027 #if defined (HANDLE_MULTIBYTE)
1028 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
1030 if (MB_INVALIDCH (wc_bytes
))
1032 /* Byte sequence is invalid or shortened. Assume that the
1033 first byte represents a character. */
1035 /* Assume that a character occupies a single column. */
1037 memset (&ps
, 0, sizeof (mbstate_t));
1039 else if (MB_NULLWCH (wc_bytes
))
1040 break; /* Found '\0' */
1043 temp
= WCWIDTH (wc
);
1044 wc_width
= (temp
>= 0) ? temp
: 1;
1051 cpos_buffer_position
= out
;
1052 lb_linenum
= newlines
;
1055 #if defined (HANDLE_MULTIBYTE)
1056 if (META_CHAR (c
) && _rl_output_meta_chars
== 0) /* XXX - clean up */
1061 if (_rl_output_meta_chars
== 0)
1066 olen
= sprintf (obuf
, "\\%o", c
);
1068 if (lpos
+ olen
>= _rl_screenwidth
)
1070 temp
= _rl_screenwidth
- lpos
;
1071 CHECK_INV_LBREAKS ();
1072 inv_lbreaks
[++newlines
] = out
+ temp
;
1073 #if defined (HANDLE_MULTIBYTE)
1074 line_state_invisible
->wrapped_line
[newlines
] = _rl_wrapped_multicolumn
;
1081 for (temp
= 0; temp
< olen
; temp
++)
1083 invis_addc (&out
, obuf
[temp
], cur_face
);
1089 invis_addc (&out
, c
, cur_face
);
1093 #if defined (DISPLAY_TABS)
1096 register int newout
;
1098 newout
= out
+ 8 - lpos
% 8;
1099 temp
= newout
- out
;
1100 if (lpos
+ temp
>= _rl_screenwidth
)
1103 temp2
= _rl_screenwidth
- lpos
;
1104 CHECK_INV_LBREAKS ();
1105 inv_lbreaks
[++newlines
] = out
+ temp2
;
1106 #if defined (HANDLE_MULTIBYTE)
1107 line_state_invisible
->wrapped_line
[newlines
] = _rl_wrapped_multicolumn
;
1109 lpos
= temp
- temp2
;
1110 while (out
< newout
)
1111 invis_addc (&out
, ' ', cur_face
);
1115 while (out
< newout
)
1116 invis_addc (&out
, ' ', cur_face
);
1121 else if (c
== '\n' && _rl_horizontal_scroll_mode
== 0 && _rl_term_up
&& *_rl_term_up
)
1123 invis_addc (&out
, '\0', cur_face
);
1124 CHECK_INV_LBREAKS ();
1125 inv_lbreaks
[++newlines
] = out
;
1126 #if defined (HANDLE_MULTIBYTE)
1127 line_state_invisible
->wrapped_line
[newlines
] = _rl_wrapped_multicolumn
;
1131 else if (CTRL_CHAR (c
) || c
== RUBOUT
)
1133 invis_addc (&out
, '^', cur_face
);
1135 invis_addc (&out
, CTRL_CHAR (c
) ? UNCTRL (c
) : '?', cur_face
);
1140 #if defined (HANDLE_MULTIBYTE)
1141 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
1145 _rl_wrapped_multicolumn
= 0;
1147 if (_rl_screenwidth
< lpos
+ wc_width
)
1148 for (i
= lpos
; i
< _rl_screenwidth
; i
++)
1150 /* The space will be removed in update_line() */
1151 invis_addc (&out
, ' ', cur_face
);
1152 _rl_wrapped_multicolumn
++;
1157 cpos_buffer_position
= out
;
1158 lb_linenum
= newlines
;
1160 for (i
= in
; i
< in
+wc_bytes
; i
++)
1161 invis_addc (&out
, rl_line_buffer
[i
], cur_face
);
1162 for (i
= 0; i
< wc_width
; i
++)
1167 invis_addc (&out
, c
, cur_face
);
1171 invis_addc (&out
, c
, cur_face
);
1176 #if defined (HANDLE_MULTIBYTE)
1177 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
1180 if (_rl_utf8locale
&& UTF8_SINGLEBYTE(rl_line_buffer
[in
]))
1182 wc
= (WCHAR_T
)rl_line_buffer
[in
];
1184 memset (&ps
, 0, sizeof (mbstate_t)); /* re-init state */
1187 wc_bytes
= MBRTOWC (&wc
, rl_line_buffer
+ in
, rl_end
- in
, &ps
);
1194 line_totbytes
= out
;
1195 if (cpos_buffer_position
< 0)
1197 cpos_buffer_position
= out
;
1198 lb_linenum
= newlines
;
1201 /* If we are switching from one line to multiple wrapped lines, we don't
1202 want to do a dumb update (or we want to make it smarter). */
1203 if (_rl_quick_redisplay
&& newlines
> 0)
1204 _rl_quick_redisplay
= 0;
1206 inv_botlin
= lb_botlin
= _rl_inv_botlin
= newlines
;
1207 CHECK_INV_LBREAKS ();
1208 inv_lbreaks
[newlines
+1] = out
;
1209 #if defined (HANDLE_MULTIBYTE)
1210 /* This should be 0 anyway */
1211 line_state_invisible
->wrapped_line
[newlines
+1] = _rl_wrapped_multicolumn
;
1213 cursor_linenum
= lb_linenum
;
1215 /* CPOS_BUFFER_POSITION == position in buffer where cursor should be placed.
1216 CURSOR_LINENUM == line number where the cursor should be placed. */
1218 /* PWP: now is when things get a bit hairy. The visible and invisible
1219 line buffers are really multiple lines, which would wrap every
1220 (screenwidth - 1) characters. Go through each in turn, finding
1221 the changed region and updating it. The line order is top to bottom. */
1223 /* If we can move the cursor up and down, then use multiple lines,
1224 otherwise, let long lines display in a single terminal line, and
1225 horizontally scroll it. */
1226 displaying_prompt_first_line
= 1;
1227 if (_rl_horizontal_scroll_mode
== 0 && _rl_term_up
&& *_rl_term_up
)
1229 int nleft
, pos
, changed_screen_line
, tx
;
1231 if (!rl_display_fixed
|| forced_display
)
1235 /* If we have more than a screenful of material to display, then
1236 only display a screenful. We should display the last screen,
1238 if (out
>= _rl_screenchars
)
1240 #if defined (HANDLE_MULTIBYTE)
1241 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
1242 out
= _rl_find_prev_mbchar (invisible_line
, _rl_screenchars
, MB_FIND_ANY
);
1245 out
= _rl_screenchars
- 1;
1248 /* The first line is at character position 0 in the buffer. The
1249 second and subsequent lines start at inv_lbreaks[N], offset by
1250 OFFSET (which has already been calculated above). */
1252 #define INVIS_FIRST() (prompt_physical_chars > _rl_screenwidth ? prompt_invis_chars_first_line : wrap_offset)
1253 #define WRAP_OFFSET(line, offset) ((line == 0) \
1254 ? (offset ? INVIS_FIRST() : 0) \
1255 : ((line == prompt_last_screen_line) ? wrap_offset-prompt_invis_chars_first_line : 0))
1256 #define W_OFFSET(line, offset) ((line) == 0 ? offset : 0)
1257 #define VIS_LLEN(l) ((l) > _rl_vis_botlin ? 0 : (vis_lbreaks[l+1] - vis_lbreaks[l]))
1258 #define INV_LLEN(l) (inv_lbreaks[l+1] - inv_lbreaks[l])
1259 #define VIS_CHARS(line) (visible_line + vis_lbreaks[line])
1260 #define VIS_FACE(line) (vis_face + vis_lbreaks[line])
1261 #define VIS_LINE(line) ((line) > _rl_vis_botlin) ? "" : VIS_CHARS(line)
1262 #define VIS_LINE_FACE(line) ((line) > _rl_vis_botlin) ? "" : VIS_FACE(line)
1263 #define INV_LINE(line) (invisible_line + inv_lbreaks[line])
1264 #define INV_LINE_FACE(line) (inv_face + inv_lbreaks[line])
1266 #define OLD_CPOS_IN_PROMPT() (cpos_adjusted == 0 && \
1267 _rl_last_c_pos != o_cpos && \
1268 _rl_last_c_pos > wrap_offset && \
1269 o_cpos < prompt_last_invisible)
1272 /* We don't want to highlight anything that's going to be off the top
1273 of the display; if the current line takes up more than an entire
1274 screen, just mark the lines that won't be displayed as having a
1276 It's imperfect, but better than display corruption. */
1277 if (rl_mark_active_p () && inv_botlin
> _rl_screenheight
)
1281 extra
= inv_botlin
- _rl_screenheight
;
1282 for (linenum
= 0; linenum
<= extra
; linenum
++)
1283 norm_face (INV_LINE_FACE(linenum
), INV_LLEN (linenum
));
1286 /* For each line in the buffer, do the updating display. */
1287 for (linenum
= 0; linenum
<= inv_botlin
; linenum
++)
1289 /* This can lead us astray if we execute a program that changes
1290 the locale from a non-multibyte to a multibyte one. */
1291 o_cpos
= _rl_last_c_pos
;
1293 update_line (VIS_LINE(linenum
), VIS_LINE_FACE(linenum
),
1294 INV_LINE(linenum
), INV_LINE_FACE(linenum
),
1296 VIS_LLEN(linenum
), INV_LLEN(linenum
), inv_botlin
);
1298 /* update_line potentially changes _rl_last_c_pos, but doesn't
1299 take invisible characters into account, since _rl_last_c_pos
1300 is an absolute cursor position in a multibyte locale. We
1301 choose to (mostly) compensate for that here, rather than
1302 change update_line itself. There are several cases in which
1303 update_line adjusts _rl_last_c_pos itself (so it can pass
1304 _rl_move_cursor_relative accurate values); it communicates
1305 this back by setting cpos_adjusted. If we assume that
1306 _rl_last_c_pos is correct (an absolute cursor position) each
1307 time update_line is called, then we can assume in our
1308 calculations that o_cpos does not need to be adjusted by
1310 if (linenum
== 0 && (mb_cur_max
> 1 && rl_byte_oriented
== 0) && OLD_CPOS_IN_PROMPT())
1311 _rl_last_c_pos
-= prompt_invis_chars_first_line
; /* XXX - was wrap_offset */
1312 else if (cpos_adjusted
== 0 &&
1313 linenum
== prompt_last_screen_line
&&
1314 prompt_physical_chars
> _rl_screenwidth
&&
1315 (mb_cur_max
> 1 && rl_byte_oriented
== 0) &&
1316 _rl_last_c_pos
!= o_cpos
&&
1317 _rl_last_c_pos
> (prompt_last_invisible
- _rl_screenwidth
- prompt_invis_chars_first_line
)) /* XXX - rethink this last one */
1318 /* This assumes that all the invisible characters are split
1319 between the first and last lines of the prompt, if the
1320 prompt consumes more than two lines. It's usually right */
1321 /* XXX - not sure this is ever executed */
1322 _rl_last_c_pos
-= (wrap_offset
-prompt_invis_chars_first_line
);
1324 /* If this is the line with the prompt, we might need to
1325 compensate for invisible characters in the new line. Do
1326 this only if there is not more than one new line (which
1327 implies that we completely overwrite the old visible line)
1328 and the new line is shorter than the old. Make sure we are
1329 at the end of the new line before clearing. */
1331 inv_botlin
== 0 && _rl_last_c_pos
== out
&&
1332 (wrap_offset
> visible_wrap_offset
) &&
1333 (_rl_last_c_pos
< visible_first_line_len
))
1335 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
1336 nleft
= _rl_screenwidth
- _rl_last_c_pos
;
1338 nleft
= _rl_screenwidth
+ wrap_offset
- _rl_last_c_pos
;
1340 _rl_clear_to_eol (nleft
);
1343 /* This segment is intended to handle the case where the old
1344 visible prompt has invisible characters and the new line
1345 to be displayed needs to clear the rest of the old characters
1346 out (e.g., when printing the i-search prompt): in general,
1347 the case of the new line being shorter than the old. We need
1348 to be at the end of the new line and the old line needs to be
1349 longer than the current cursor position. It's not perfect,
1350 since it uses the byte length of the first line, but this will
1351 at worst result in some extra clear-to-end-of-lines. We can't
1352 use the prompt length variables because they may not
1353 correspond to the visible line (see printing the i-search
1354 prompt above). The tests for differing numbers of invisible
1355 characters may not matter and can probably be removed. */
1356 else if (linenum
== 0 &&
1357 linenum
== prompt_last_screen_line
&&
1358 _rl_last_c_pos
== out
&&
1359 _rl_last_c_pos
< visible_first_line_len
&&
1360 visible_wrap_offset
&&
1361 visible_wrap_offset
!= wrap_offset
)
1363 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
1364 nleft
= _rl_screenwidth
- _rl_last_c_pos
;
1366 nleft
= _rl_screenwidth
+ wrap_offset
- _rl_last_c_pos
;
1368 _rl_clear_to_eol (nleft
);
1372 /* Since the new first line is now visible, save its length. */
1374 visible_first_line_len
= (inv_botlin
> 0) ? inv_lbreaks
[1] : out
- wrap_offset
;
1377 /* We may have deleted some lines. If so, clear the left over
1378 blank ones at the bottom out. */
1379 if (_rl_vis_botlin
> inv_botlin
)
1382 for (; linenum
<= _rl_vis_botlin
; linenum
++)
1384 tt
= VIS_CHARS (linenum
);
1385 _rl_move_vert (linenum
);
1386 _rl_move_cursor_relative (0, tt
, VIS_FACE(linenum
));
1388 ((linenum
== _rl_vis_botlin
) ? strlen (tt
) : _rl_screenwidth
);
1391 _rl_vis_botlin
= inv_botlin
;
1393 /* CHANGED_SCREEN_LINE is set to 1 if we have moved to a
1394 different screen line during this redisplay. */
1395 changed_screen_line
= _rl_last_v_pos
!= cursor_linenum
;
1396 if (changed_screen_line
)
1398 _rl_move_vert (cursor_linenum
);
1399 /* If we moved up to the line with the prompt using _rl_term_up,
1400 the physical cursor position on the screen stays the same,
1401 but the buffer position needs to be adjusted to account
1402 for invisible characters. */
1403 if ((mb_cur_max
== 1 || rl_byte_oriented
) && cursor_linenum
== 0 && wrap_offset
)
1404 _rl_last_c_pos
+= wrap_offset
;
1407 /* Now we move the cursor to where it needs to be. First, make
1408 sure we are on the correct line (cursor_linenum). */
1410 /* We have to reprint the prompt if it contains invisible
1411 characters, since it's not generally OK to just reprint
1412 the characters from the current cursor position. But we
1413 only need to reprint it if the cursor is before the last
1414 invisible character in the prompt string. */
1415 /* XXX - why not use local_prompt_len? */
1416 nleft
= prompt_visible_length
+ wrap_offset
;
1417 if (cursor_linenum
== 0 && wrap_offset
> 0 && _rl_last_c_pos
> 0 &&
1418 _rl_last_c_pos
< PROMPT_ENDING_INDEX
&& local_prompt
)
1422 _rl_output_some_chars ("*", 1);
1424 _rl_output_some_chars (local_prompt
, nleft
);
1425 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
1426 _rl_last_c_pos
= _rl_col_width (local_prompt
, 0, nleft
, 1) - wrap_offset
+ modmark
;
1428 _rl_last_c_pos
= nleft
+ modmark
;
1431 /* Where on that line? And where does that line start
1433 pos
= inv_lbreaks
[cursor_linenum
];
1434 /* nleft == number of characters (bytes) in the line buffer between
1435 the start of the line and the desired cursor position. */
1436 nleft
= cpos_buffer_position
- pos
;
1438 /* NLEFT is now a number of characters in a buffer. When in a
1439 multibyte locale, however, _rl_last_c_pos is an absolute cursor
1440 position that doesn't take invisible characters in the prompt
1441 into account. We use a fudge factor to compensate. */
1443 /* Since _rl_backspace() doesn't know about invisible characters in
1444 the prompt, and there's no good way to tell it, we compensate for
1445 those characters here and call _rl_backspace() directly if
1447 if (wrap_offset
&& cursor_linenum
== 0 && nleft
< _rl_last_c_pos
)
1449 /* TX == new physical cursor position in multibyte locale. */
1450 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
1451 tx
= _rl_col_width (&visible_line
[pos
], 0, nleft
, 1) - visible_wrap_offset
;
1454 if (tx
>= 0 && _rl_last_c_pos
> tx
)
1456 _rl_backspace (_rl_last_c_pos
- tx
); /* XXX */
1457 _rl_last_c_pos
= tx
;
1461 /* We need to note that in a multibyte locale we are dealing with
1462 _rl_last_c_pos as an absolute cursor position, but moving to a
1463 point specified by a buffer position (NLEFT) that doesn't take
1464 invisible characters into account. */
1465 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
1466 _rl_move_cursor_relative (nleft
, &invisible_line
[pos
], &inv_face
[pos
]);
1467 else if (nleft
!= _rl_last_c_pos
)
1468 _rl_move_cursor_relative (nleft
, &invisible_line
[pos
], &inv_face
[pos
]);
1471 else /* Do horizontal scrolling. Much simpler */
1473 #define M_OFFSET(margin, offset) ((margin) == 0 ? offset : 0)
1474 int lmargin
, ndisp
, nleft
, phys_c_pos
, t
;
1476 /* Always at top line. */
1479 /* Compute where in the buffer the displayed line should start. This
1482 /* The number of characters that will be displayed before the cursor. */
1483 ndisp
= cpos_buffer_position
- wrap_offset
;
1484 nleft
= prompt_visible_length
+ wrap_offset
;
1485 /* Where the new cursor position will be on the screen. This can be
1486 longer than SCREENWIDTH; if it is, lmargin will be adjusted. */
1487 phys_c_pos
= cpos_buffer_position
- (last_lmargin
? last_lmargin
: wrap_offset
);
1488 t
= _rl_screenwidth
/ 3;
1490 /* If the number of characters had already exceeded the screenwidth,
1491 last_lmargin will be > 0. */
1493 /* If the number of characters to be displayed is more than the screen
1494 width, compute the starting offset so that the cursor is about
1495 two-thirds of the way across the screen. */
1496 if (phys_c_pos
> _rl_screenwidth
- 2)
1498 lmargin
= cpos_buffer_position
- (2 * t
);
1501 /* If the left margin would be in the middle of a prompt with
1502 invisible characters, don't display the prompt at all. */
1503 if (wrap_offset
&& lmargin
> 0 && lmargin
< nleft
)
1506 else if (ndisp
< _rl_screenwidth
- 2) /* XXX - was -1 */
1508 else if (phys_c_pos
< 1)
1510 /* If we are moving back towards the beginning of the line and
1511 the last margin is no longer correct, compute a new one. */
1512 lmargin
= ((cpos_buffer_position
- 1) / t
) * t
; /* XXX */
1513 if (wrap_offset
&& lmargin
> 0 && lmargin
< nleft
)
1517 lmargin
= last_lmargin
;
1519 displaying_prompt_first_line
= lmargin
< nleft
;
1521 /* If the first character on the screen isn't the first character
1522 in the display line, indicate this with a special character. */
1524 invisible_line
[lmargin
] = '<';
1526 /* If SCREENWIDTH characters starting at LMARGIN do not encompass
1527 the whole line, indicate that with a special character at the
1528 right edge of the screen. If LMARGIN is 0, we need to take the
1529 wrap offset into account. */
1530 t
= lmargin
+ M_OFFSET (lmargin
, wrap_offset
) + _rl_screenwidth
;
1531 if (t
> 0 && t
< out
)
1532 invisible_line
[t
- 1] = '>';
1534 if (rl_display_fixed
== 0 || forced_display
|| lmargin
!= last_lmargin
)
1537 o_cpos
= _rl_last_c_pos
;
1539 update_line (&visible_line
[last_lmargin
], &vis_face
[last_lmargin
],
1540 &invisible_line
[lmargin
], &inv_face
[lmargin
],
1542 _rl_screenwidth
+ visible_wrap_offset
,
1543 _rl_screenwidth
+ (lmargin
? 0 : wrap_offset
),
1546 if ((mb_cur_max
> 1 && rl_byte_oriented
== 0) &&
1547 displaying_prompt_first_line
&& OLD_CPOS_IN_PROMPT())
1548 _rl_last_c_pos
-= prompt_invis_chars_first_line
; /* XXX - was wrap_offset */
1550 /* If the visible new line is shorter than the old, but the number
1551 of invisible characters is greater, and we are at the end of
1552 the new line, we need to clear to eol. */
1553 t
= _rl_last_c_pos
- M_OFFSET (lmargin
, wrap_offset
);
1554 if ((M_OFFSET (lmargin
, wrap_offset
) > visible_wrap_offset
) &&
1555 (_rl_last_c_pos
== out
) && displaying_prompt_first_line
&&
1556 t
< visible_first_line_len
)
1558 nleft
= _rl_screenwidth
- t
;
1559 _rl_clear_to_eol (nleft
);
1561 visible_first_line_len
= out
- lmargin
- M_OFFSET (lmargin
, wrap_offset
);
1562 if (visible_first_line_len
> _rl_screenwidth
)
1563 visible_first_line_len
= _rl_screenwidth
;
1565 _rl_move_cursor_relative (cpos_buffer_position
- lmargin
, &invisible_line
[lmargin
], &inv_face
[lmargin
]);
1566 last_lmargin
= lmargin
;
1569 fflush (rl_outstream
);
1571 /* Swap visible and non-visible lines. */
1573 struct line_state
*vtemp
= line_state_visible
;
1575 line_state_visible
= line_state_invisible
;
1576 line_state_invisible
= vtemp
;
1578 rl_display_fixed
= 0;
1579 /* If we are displaying on a single line, and last_lmargin is > 0, we
1580 are not displaying any invisible characters, so set visible_wrap_offset
1582 if (_rl_horizontal_scroll_mode
&& last_lmargin
)
1583 visible_wrap_offset
= 0;
1585 visible_wrap_offset
= wrap_offset
;
1587 _rl_quick_redisplay
= 0;
1590 RL_UNSETSTATE (RL_STATE_REDISPLAYING
);
1591 _rl_release_sigint ();
1595 putc_face (int c
, int face
, char *cur_face
)
1601 if (cf
!= FACE_NORMAL
&& cf
!= FACE_STANDOUT
)
1603 if (face
!= FACE_NORMAL
&& face
!= FACE_STANDOUT
)
1605 if (face
== FACE_STANDOUT
&& cf
== FACE_NORMAL
)
1606 _rl_region_color_on ();
1607 if (face
== FACE_NORMAL
&& cf
== FACE_STANDOUT
)
1608 _rl_region_color_off ();
1612 putc (c
, rl_outstream
);
1616 puts_face (const char *str
, const char *face
, int n
)
1621 for (cur_face
= FACE_NORMAL
, i
= 0; i
< n
; i
++)
1622 putc_face ((unsigned char) str
[i
], face
[i
], &cur_face
);
1623 putc_face (EOF
, FACE_NORMAL
, &cur_face
);
1627 norm_face (char *face
, int n
)
1629 memset (face
, FACE_NORMAL
, n
);
1632 #define ADJUST_CPOS(x) do { _rl_last_c_pos -= (x) ; cpos_adjusted = 1; } while (0)
1634 /* PWP: update_line() is based on finding the middle difference of each
1635 line on the screen; vis:
1637 /old first difference
1638 /beginning of line | /old last same /old EOL
1640 old: eddie> Oh, my little gruntle-buggy is to me, as lurgid as
1641 new: eddie> Oh, my little buggy says to me, as lurgid as
1643 \beginning of line | \new last same \new end of line
1644 \new first difference
1646 All are character pointers for the sake of speed. Special cases for
1647 no differences, as well as for end of line additions must be handled.
1649 Could be made even smarter, but this works well enough */
1651 update_line (char *old
, char *old_face
, char *new, char *new_face
, int current_line
, int omax
, int nmax
, int inv_botlin
)
1653 char *ofd
, *ols
, *oe
, *nfd
, *nls
, *ne
;
1654 char *ofdf
, *nfdf
, *olsf
, *nlsf
;
1655 int temp
, lendiff
, wsatend
, od
, nd
, twidth
, o_cpos
;
1656 int current_invis_chars
;
1657 int col_lendiff
, col_temp
;
1658 int bytes_to_insert
;
1659 int mb_cur_max
= MB_CUR_MAX
;
1660 #if defined (HANDLE_MULTIBYTE)
1661 mbstate_t ps_new
, ps_old
;
1662 int new_offset
, old_offset
;
1665 /* If we're at the right edge of a terminal that supports xn, we're
1666 ready to wrap around, so do so. This fixes problems with knowing
1667 the exact cursor position and cut-and-paste with certain terminal
1668 emulators. In this calculation, TEMP is the physical screen
1669 position of the cursor. */
1670 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
1671 temp
= _rl_last_c_pos
;
1673 temp
= _rl_last_c_pos
- WRAP_OFFSET (_rl_last_v_pos
, visible_wrap_offset
);
1674 if (temp
== _rl_screenwidth
&& _rl_term_autowrap
&& !_rl_horizontal_scroll_mode
1675 && _rl_last_v_pos
== current_line
- 1)
1677 /* We're going to wrap around by writing the first character of NEW to
1678 the screen and dealing with changes to what's visible by modifying
1679 OLD to match it. Complicated by the presence of multi-width
1680 characters at the end of the line or beginning of the new one. */
1681 /* old is always somewhere in visible_line; new is always somewhere in
1682 invisible_line. These should always be null-terminated. */
1683 #if defined (HANDLE_MULTIBYTE)
1684 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
1688 int oldwidth
, newwidth
;
1689 int oldbytes
, newbytes
;
1692 /* This fixes only double-column characters, but if the wrapped
1693 character consumes more than three columns, spaces will be
1694 inserted in the string buffer. */
1695 /* XXX remember that we are working on the invisible line right now;
1696 we don't swap visible and invisible until just before rl_redisplay
1698 /* This will remove the extra placeholder space we added with
1699 _rl_wrapped_multicolumn */
1700 if (current_line
< line_state_invisible
->wbsize
&& line_state_invisible
->wrapped_line
[current_line
] > 0)
1701 _rl_clear_to_eol (line_state_invisible
->wrapped_line
[current_line
]);
1703 /* 1. how many screen positions does first char in old consume? */
1704 memset (&ps
, 0, sizeof (mbstate_t));
1705 ret
= MBRTOWC (&wc
, old
, mb_cur_max
, &ps
);
1707 if (MB_INVALIDCH (ret
))
1712 else if (MB_NULLWCH (ret
))
1715 oldwidth
= WCWIDTH (wc
);
1719 /* 2. how many screen positions does the first char in new consume? */
1720 memset (&ps
, 0, sizeof (mbstate_t));
1721 ret
= MBRTOWC (&wc
, new, mb_cur_max
, &ps
);
1723 if (MB_INVALIDCH (ret
))
1728 else if (MB_NULLWCH (ret
))
1731 newwidth
= WCWIDTH (wc
);
1735 /* 3. if the new width is less than the old width, we need to keep
1736 going in new until we have consumed at least that many screen
1737 positions, and figure out how many bytes that will take */
1738 while (newbytes
< nmax
&& newwidth
< oldwidth
)
1742 ret
= MBRTOWC (&wc
, new+newbytes
, mb_cur_max
, &ps
);
1743 if (MB_INVALIDCH (ret
))
1748 else if (MB_NULLWCH (ret
))
1753 newwidth
+= (t
>= 0) ? t
: 1;
1757 /* 4. If the new width is more than the old width, keep going in old
1758 until we have consumed exactly that many screen positions, and
1759 figure out how many bytes that will take. This is an optimization */
1760 while (oldbytes
< omax
&& oldwidth
< newwidth
)
1764 ret
= MBRTOWC (&wc
, old
+oldbytes
, mb_cur_max
, &ps
);
1765 if (MB_INVALIDCH (ret
))
1770 else if (MB_NULLWCH (ret
))
1775 oldwidth
+= (t
>= 0) ? t
: 1;
1779 /* 5. write the first newbytes of new, which takes newwidth. This is
1780 where the screen wrapping takes place, and we are now writing
1781 characters onto the new line. We need to fix up old so it
1782 accurately reflects what is on the screen after the
1783 _rl_output_some_chars below. */
1789 puts_face (new, new_face
, newbytes
);
1790 _rl_last_c_pos
= newwidth
;
1793 /* 5a. If the number of screen positions doesn't match, punt
1794 and do a dumb update.
1795 5b. If the number of bytes is greater in the new line than
1796 the old, do a dumb update, because there is no guarantee we
1797 can extend the old line enough to fit the new bytes. */
1798 if (newwidth
!= oldwidth
|| newbytes
> oldbytes
)
1804 ofdf
= old_face
+ oldbytes
;
1805 nfdf
= new_face
+ newbytes
;
1809 if (oldbytes
!= 0 && newbytes
!= 0)
1811 /* We have written as many bytes from new as we need to
1812 consume the first character of old. Fix up `old' so it
1813 reflects the new screen contents. We use +1 in the
1814 memmove call to copy the trailing NUL. */
1815 /* (strlen(old+oldbytes) == (omax - oldbytes - 1)) */
1817 /* Don't bother trying to fit the bytes if the number of bytes
1819 if (oldbytes
!= newbytes
)
1821 memmove (old
+newbytes
, old
+oldbytes
, strlen (old
+oldbytes
) + 1);
1822 memmove (old_face
+newbytes
, old_face
+oldbytes
, strlen (old
+oldbytes
) + 1);
1824 memcpy (old
, new, newbytes
);
1825 memcpy (old_face
, new_face
, newbytes
);
1826 j
= newbytes
- oldbytes
;
1828 /* Fix up indices if we copy data from one line to another */
1829 for (i
= current_line
+1; j
!= 0 && i
<= inv_botlin
+1 && i
<=_rl_vis_botlin
+1; i
++)
1830 vis_lbreaks
[i
] += j
;
1835 putc (' ', rl_outstream
);
1838 if (old
[0] && new[0])
1841 old_face
[0] = new_face
[0];
1849 puts_face (new, new_face
, 1);
1851 putc (' ', rl_outstream
);
1854 if (old
[0] && new[0])
1857 old_face
[0] = new_face
[0];
1862 /* We know that we are dealing with a single screen line here */
1863 if (_rl_quick_redisplay
)
1869 for (od
= 0, oe
= ofd
; od
< omax
&& *oe
; oe
++, od
++);
1870 for (nd
= 0, ne
= nfd
; nd
< nmax
&& *ne
; ne
++, nd
++);
1872 _rl_move_cursor_relative (0, old
, old_face
);
1874 bytes_to_insert
= ne
- nfd
;
1875 if (bytes_to_insert
< local_prompt_len
) /* ??? */
1878 /* output the prompt, output the line contents, clear the rest */
1879 _rl_output_some_chars (nfd
, local_prompt_len
);
1880 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
1881 _rl_last_c_pos
= prompt_physical_chars
;
1883 _rl_last_c_pos
= local_prompt_len
;
1885 bytes_to_insert
-= local_prompt_len
;
1886 if (bytes_to_insert
> 0)
1888 puts_face (new+local_prompt_len
, nfdf
+local_prompt_len
, bytes_to_insert
);
1889 if (mb_cur_max
> 1 && rl_byte_oriented
)
1890 _rl_last_c_pos
+= _rl_col_width (new, local_prompt_len
, ne
-new, 1);
1892 _rl_last_c_pos
+= bytes_to_insert
;
1895 /* See comments at dumb_update: for an explanation of this heuristic */
1897 goto clear_rest_of_line
;
1898 else if ((nmax
- W_OFFSET(current_line
, wrap_offset
)) < (omax
- W_OFFSET (current_line
, visible_wrap_offset
)))
1899 goto clear_rest_of_line
;
1904 /* Find first difference. */
1905 #if defined (HANDLE_MULTIBYTE)
1906 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
1908 /* See if the old line is a subset of the new line, so that the
1909 only change is adding characters. */
1910 temp
= (omax
< nmax
) ? omax
: nmax
;
1911 if (memcmp (old
, new, temp
) == 0 && memcmp (old_face
, new_face
, temp
) == 0)
1913 new_offset
= old_offset
= temp
; /* adding at the end */
1915 ofdf
= old_face
+ temp
;
1917 nfdf
= new_face
+ temp
;
1921 memset (&ps_new
, 0, sizeof(mbstate_t));
1922 memset (&ps_old
, 0, sizeof(mbstate_t));
1924 /* Are the old and new lines the same? */
1925 if (omax
== nmax
&& memcmp (new, old
, omax
) == 0 && memcmp (new_face
, old_face
, omax
) == 0)
1930 ofdf
= old_face
+ omax
;
1932 nfdf
= new_face
+ nmax
;
1936 /* Go through the line from the beginning and find the first
1937 difference. We assume that faces change at (possibly multi-
1938 byte) character boundaries. */
1939 new_offset
= old_offset
= 0;
1940 for (ofd
= old
, ofdf
= old_face
, nfd
= new, nfdf
= new_face
;
1941 (ofd
- old
< omax
) && *ofd
&&
1942 _rl_compare_chars(old
, old_offset
, &ps_old
, new, new_offset
, &ps_new
) &&
1945 old_offset
= _rl_find_next_mbchar (old
, old_offset
, 1, MB_FIND_ANY
);
1946 new_offset
= _rl_find_next_mbchar (new, new_offset
, 1, MB_FIND_ANY
);
1948 ofd
= old
+ old_offset
;
1949 ofdf
= old_face
+ old_offset
;
1950 nfd
= new + new_offset
;
1951 nfdf
= new_face
+ new_offset
;
1958 for (ofd
= old
, ofdf
= old_face
, nfd
= new, nfdf
= new_face
;
1959 (ofd
- old
< omax
) && *ofd
&& (*ofd
== *nfd
) && (*ofdf
== *nfdf
);
1960 ofd
++, nfd
++, ofdf
++, nfdf
++)
1963 /* Move to the end of the screen line. ND and OD are used to keep track
1964 of the distance between ne and new and oe and old, respectively, to
1965 move a subtraction out of each loop. */
1966 for (od
= ofd
- old
, oe
= ofd
; od
< omax
&& *oe
; oe
++, od
++);
1967 for (nd
= nfd
- new, ne
= nfd
; nd
< nmax
&& *ne
; ne
++, nd
++);
1969 /* If no difference, continue to next line. */
1970 if (ofd
== oe
&& nfd
== ne
)
1973 #if defined (HANDLE_MULTIBYTE)
1974 if (mb_cur_max
> 1 && rl_byte_oriented
== 0 && _rl_utf8locale
)
1977 mbstate_t ps
= { 0 };
1980 /* If the first character in the difference is a zero-width character,
1981 assume it's a combining character and back one up so the two base
1982 characters no longer compare equivalently. */
1983 t
= MBRTOWC (&wc
, ofd
, mb_cur_max
, &ps
);
1984 if (t
> 0 && UNICODE_COMBINING_CHAR (wc
) && WCWIDTH (wc
) == 0)
1986 old_offset
= _rl_find_prev_mbchar (old
, ofd
- old
, MB_FIND_ANY
);
1987 new_offset
= _rl_find_prev_mbchar (new, nfd
- new, MB_FIND_ANY
);
1988 ofd
= old
+ old_offset
; /* equal by definition */
1989 ofdf
= old_face
+ old_offset
;
1990 nfd
= new + new_offset
;
1991 nfdf
= new_face
+ new_offset
;
1996 wsatend
= 1; /* flag for trailing whitespace */
1998 #if defined (HANDLE_MULTIBYTE)
1999 /* Find the last character that is the same between the two lines. This
2000 bounds the region that needs to change. */
2001 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
2003 ols
= old
+ _rl_find_prev_mbchar (old
, oe
- old
, MB_FIND_ANY
);
2004 olsf
= old_face
+ (ols
- old
);
2005 nls
= new + _rl_find_prev_mbchar (new, ne
- new, MB_FIND_ANY
);
2006 nlsf
= new_face
+ (nls
- new);
2008 while ((ols
> ofd
) && (nls
> nfd
))
2010 memset (&ps_old
, 0, sizeof (mbstate_t));
2011 memset (&ps_new
, 0, sizeof (mbstate_t));
2013 if (_rl_compare_chars (old
, ols
- old
, &ps_old
, new, nls
- new, &ps_new
) == 0 ||
2020 ols
= old
+ _rl_find_prev_mbchar (old
, ols
- old
, MB_FIND_ANY
);
2021 olsf
= old_face
+ (ols
- old
);
2022 nls
= new + _rl_find_prev_mbchar (new, nls
- new, MB_FIND_ANY
);
2023 nlsf
= new_face
+ (nls
- new);
2028 #endif /* HANDLE_MULTIBYTE */
2029 ols
= oe
- 1; /* find last same */
2030 olsf
= old_face
+ (ols
- old
);
2032 nlsf
= new_face
+ (nls
- new);
2033 while ((ols
> ofd
) && (nls
> nfd
) && (*ols
== *nls
) && (*olsf
== *nlsf
))
2040 #if defined (HANDLE_MULTIBYTE)
2047 olsf
= old_face
+ (ols
- old
);
2049 nlsf
= new_face
+ (nls
- new);
2051 #if defined (HANDLE_MULTIBYTE)
2052 /* This may not work for stateful encoding, but who cares? To handle
2053 stateful encoding properly, we have to scan each string from the
2054 beginning and compare. */
2055 else if (_rl_compare_chars (ols
, 0, NULL
, nls
, 0, NULL
) == 0 || *olsf
!= *nlsf
)
2057 else if (*ols
!= *nls
|| *olsf
!= *nlsf
)
2060 if (*ols
) /* don't step past the NUL */
2062 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
2063 ols
= old
+ _rl_find_next_mbchar (old
, ols
- old
, 1, MB_FIND_ANY
);
2069 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
2070 nls
= new + _rl_find_next_mbchar (new, nls
- new, 1, MB_FIND_ANY
);
2074 olsf
= old_face
+ (ols
- old
);
2075 nlsf
= new_face
+ (nls
- new);
2078 /* count of invisible characters in the current invisible line. */
2079 current_invis_chars
= W_OFFSET (current_line
, wrap_offset
);
2080 if (_rl_last_v_pos
!= current_line
)
2082 _rl_move_vert (current_line
);
2083 /* We have moved up to a new screen line. This line may or may not have
2084 invisible characters on it, but we do our best to recalculate
2085 visible_wrap_offset based on what we know. */
2086 if (current_line
== 0)
2087 visible_wrap_offset
= prompt_invis_chars_first_line
; /* XXX */
2088 #if 0 /* XXX - not yet */
2089 else if (current_line
== prompt_last_screen_line
&& wrap_offset
> prompt_invis_chars_first_line
)
2090 visible_wrap_offset
= wrap_offset
- prompt_invis_chars_first_line
2092 if ((mb_cur_max
== 1 || rl_byte_oriented
) && current_line
== 0 && visible_wrap_offset
)
2093 _rl_last_c_pos
+= visible_wrap_offset
;
2096 /* If this is the first line and there are invisible characters in the
2097 prompt string, and the prompt string has not changed, and the current
2098 cursor position is before the last invisible character in the prompt,
2099 and the index of the character to move to is past the end of the prompt
2100 string, then redraw the entire prompt string. We can only do this
2101 reliably if the terminal supports a `cr' capability.
2103 This can also happen if the prompt string has changed, and the first
2104 difference in the line is in the middle of the prompt string, after a
2105 sequence of invisible characters (worst case) and before the end of
2106 the prompt. In this case, we have to redraw the entire prompt string
2107 so that the entire sequence of invisible characters is drawn. We need
2108 to handle the worst case, when the difference is after (or in the middle
2109 of) a sequence of invisible characters that changes the text color and
2110 before the sequence that restores the text color to normal. Then we have
2111 to make sure that the lines still differ -- if they don't, we can
2114 This is not an efficiency hack -- there is a problem with redrawing
2115 portions of the prompt string if they contain terminal escape
2116 sequences (like drawing the `unbold' sequence without a corresponding
2117 `bold') that manifests itself on certain terminals. */
2119 lendiff
= local_prompt_len
;
2122 od
= ofd
- old
; /* index of first difference in visible line */
2123 nd
= nfd
- new; /* nd, od are buffer indexes */
2124 if (current_line
== 0 && !_rl_horizontal_scroll_mode
&&
2125 _rl_term_cr
&& lendiff
> prompt_visible_length
&& _rl_last_c_pos
> 0 &&
2126 (((od
> 0 || nd
> 0) && (od
<= prompt_last_invisible
|| nd
<= prompt_last_invisible
)) ||
2127 ((od
>= lendiff
) && _rl_last_c_pos
< PROMPT_ENDING_INDEX
)))
2131 _rl_output_some_chars ("*", 1);
2132 _rl_output_some_chars (local_prompt
, lendiff
);
2133 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
2135 /* If we just output the entire prompt string we can take advantage
2136 of knowing the number of physical characters in the prompt. If
2137 the prompt wraps lines (lendiff clamped at nmax), we can't. */
2138 if (lendiff
== local_prompt_len
)
2139 _rl_last_c_pos
= prompt_physical_chars
+ modmark
;
2141 /* We take wrap_offset into account here so we can pass correct
2142 information to _rl_move_cursor_relative. */
2143 _rl_last_c_pos
= _rl_col_width (local_prompt
, 0, lendiff
, 1) - wrap_offset
+ modmark
;
2147 _rl_last_c_pos
= lendiff
+ modmark
;
2149 /* Now if we have printed the prompt string because the first difference
2150 was within the prompt, see if we need to recompute where the lines
2151 differ. Check whether where we are now is past the last place where
2152 the old and new lines are the same and short-circuit now if we are. */
2153 if ((od
<= prompt_last_invisible
|| nd
<= prompt_last_invisible
) &&
2155 lendiff
> (ols
-old
) && lendiff
> (nls
-new))
2158 /* XXX - we need to fix up our calculations if we are now past the
2159 old ofd/nfd and the prompt length (or line length) has changed.
2160 We punt on the problem and do a dumb update. We'd like to be able
2161 to just output the prompt from the beginning of the line up to the
2162 first difference, but you don't know the number of invisible
2163 characters in that case.
2164 This needs a lot of work to be efficient, but it usually doesn't matter. */
2165 if ((od
<= prompt_last_invisible
|| nd
<= prompt_last_invisible
))
2167 nfd
= new + lendiff
; /* number of characters we output above */
2168 nfdf
= new_face
+ lendiff
;
2171 /* Do a dumb update and return */
2176 puts_face (nfd
, nfdf
, temp
);
2177 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
2179 _rl_last_c_pos
+= _rl_col_width (new, nd
, ne
- new, 1);
2180 /* Need to adjust here based on wrap_offset. Guess that if
2181 this is the line containing the last line of the prompt
2182 we need to adjust by
2183 wrap_offset-prompt_invis_chars_first_line
2184 on the assumption that this is the number of invisible
2185 characters in the last line of the prompt. */
2186 if (wrap_offset
> prompt_invis_chars_first_line
&&
2187 current_line
== prompt_last_screen_line
&&
2188 prompt_physical_chars
> _rl_screenwidth
&&
2189 _rl_horizontal_scroll_mode
== 0)
2190 ADJUST_CPOS (wrap_offset
- prompt_invis_chars_first_line
);
2192 /* If we just output a new line including the prompt, and
2193 the prompt includes invisible characters, we need to
2194 account for them in the _rl_last_c_pos calculation, since
2195 _rl_col_width does not. This happens when other code does
2196 a goto dumb_update; */
2197 else if (current_line
== 0 &&
2199 prompt_invis_chars_first_line
&&
2200 local_prompt_len
<= temp
&&
2201 wrap_offset
>= prompt_invis_chars_first_line
&&
2202 _rl_horizontal_scroll_mode
== 0)
2203 ADJUST_CPOS (prompt_invis_chars_first_line
);
2206 _rl_last_c_pos
+= temp
;
2208 /* This is a useful heuristic, but what we really want is to clear
2209 if the new number of visible screen characters is less than the
2210 old number of visible screen characters. If the prompt has changed,
2211 we don't really have enough information about the visible line to
2212 know for sure, so we use another heuristic calclulation below. */
2214 goto clear_rest_of_line
; /* XXX */
2215 else if ((nmax
- W_OFFSET(current_line
, wrap_offset
)) < (omax
- W_OFFSET (current_line
, visible_wrap_offset
)))
2216 goto clear_rest_of_line
;
2222 o_cpos
= _rl_last_c_pos
;
2224 /* When this function returns, _rl_last_c_pos is correct, and an absolute
2225 cursor position in multibyte mode, but a buffer index when not in a
2226 multibyte locale. */
2227 _rl_move_cursor_relative (od
, old
, old_face
);
2229 #if defined (HANDLE_MULTIBYTE)
2230 /* We need to indicate that the cursor position is correct in the presence of
2231 invisible characters in the prompt string. Let's see if setting this when
2232 we make sure we're at the end of the drawn prompt string works. */
2233 if (current_line
== 0 && mb_cur_max
> 1 && rl_byte_oriented
== 0 &&
2234 (_rl_last_c_pos
> 0 || o_cpos
> 0) &&
2235 _rl_last_c_pos
== prompt_physical_chars
)
2239 /* if (len (new) > len (old))
2240 lendiff == difference in buffer (bytes)
2241 col_lendiff == difference on screen (columns)
2242 When not using multibyte characters, these are equal */
2243 lendiff
= (nls
- nfd
) - (ols
- ofd
);
2244 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
2246 int newchars
, newwidth
, newind
;
2247 int oldchars
, oldwidth
, oldind
;
2249 newchars
= nls
- new;
2250 oldchars
= ols
- old
;
2252 /* If we can do it, try to adjust nls and ols so that nls-new will
2253 contain the entire new prompt string. That way we can use
2254 prompt_physical_chars and not have to recompute column widths.
2255 _rl_col_width adds wrap_offset and expects the caller to compensate,
2256 which we do below, so we do the same thing if we don't call
2258 We don't have to compare, since we know the characters are the same.
2259 The check of differing numbers of invisible chars may be extraneous.
2260 XXX - experimental */
2261 if (current_line
== 0 && nfd
== new && newchars
> prompt_last_invisible
&&
2262 newchars
<= local_prompt_len
&&
2263 local_prompt_len
<= nmax
&&
2264 current_invis_chars
!= visible_wrap_offset
)
2266 while (newchars
< nmax
&& oldchars
< omax
&& newchars
< local_prompt_len
)
2268 #if defined (HANDLE_MULTIBYTE)
2269 newind
= _rl_find_next_mbchar (new, newchars
, 1, MB_FIND_NONZERO
);
2270 oldind
= _rl_find_next_mbchar (old
, oldchars
, 1, MB_FIND_NONZERO
);
2272 nls
+= newind
- newchars
;
2273 ols
+= oldind
- oldchars
;
2279 newchars
++; oldchars
++;
2282 newwidth
= (newchars
== local_prompt_len
) ? prompt_physical_chars
+ wrap_offset
2283 : _rl_col_width (new, 0, nls
- new, 1);
2284 /* if we changed nls and ols, we need to recompute lendiff */
2285 lendiff
= (nls
- nfd
) - (ols
- ofd
);
2287 nlsf
= new_face
+ (nls
- new);
2288 olsf
= old_face
+ (ols
- old
);
2291 newwidth
= _rl_col_width (new, nfd
- new, nls
- new, 1);
2293 oldwidth
= _rl_col_width (old
, ofd
- old
, ols
- old
, 1);
2295 col_lendiff
= newwidth
- oldwidth
;
2298 col_lendiff
= lendiff
;
2300 /* col_lendiff uses _rl_col_width(), which doesn't know about whether or not
2301 the multibyte characters it counts are invisible, so unless we're printing
2302 the entire prompt string (in which case we can use prompt_physical_chars)
2303 the count is short by the number of bytes in the invisible multibyte
2304 characters - the number of multibyte characters.
2306 We don't have a good way to solve this without moving to something like
2307 a bitmap that indicates which characters are visible and which are
2308 invisible. We fix it up (imperfectly) in the caller and by trying to use
2309 the entire prompt string wherever we can. */
2311 /* If we are changing the number of invisible characters in a line, and
2312 the spot of first difference is before the end of the invisible chars,
2313 lendiff needs to be adjusted. */
2314 if (current_line
== 0 && current_invis_chars
!= visible_wrap_offset
)
2316 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
2318 lendiff
+= visible_wrap_offset
- current_invis_chars
;
2319 col_lendiff
+= visible_wrap_offset
- current_invis_chars
;
2323 lendiff
+= visible_wrap_offset
- current_invis_chars
;
2324 col_lendiff
= lendiff
;
2328 /* We use temp as a count of the number of bytes from the first difference
2329 to the end of the new line. col_temp is the corresponding number of
2330 screen columns. A `dumb' update moves to the spot of first difference
2331 and writes TEMP bytes. */
2332 /* Insert (diff (len (old), len (new)) ch. */
2334 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
2335 col_temp
= _rl_col_width (new, nfd
- new, ne
- new, 1);
2339 /* how many bytes from the new line buffer to write to the display */
2340 bytes_to_insert
= nls
- nfd
;
2342 /* col_lendiff > 0 if we are adding characters to the line */
2343 if (col_lendiff
> 0) /* XXX - was lendiff */
2345 /* Non-zero if we're increasing the number of lines. */
2346 int gl
= current_line
>= _rl_vis_botlin
&& inv_botlin
> _rl_vis_botlin
;
2348 /* If col_lendiff is > 0, implying that the new string takes up more
2349 screen real estate than the old, but lendiff is < 0, meaning that it
2350 takes fewer bytes, we need to just output the characters starting
2351 from the first difference. These will overwrite what is on the
2352 display, so there's no reason to do a smart update. This can really
2353 only happen in a multibyte environment. */
2356 puts_face (nfd
, nfdf
, temp
);
2357 _rl_last_c_pos
+= col_temp
;
2358 /* If nfd begins before any invisible characters in the prompt,
2359 adjust _rl_last_c_pos to account for wrap_offset and set
2360 cpos_adjusted to let the caller know. */
2361 if (current_line
== 0 && displaying_prompt_first_line
&& wrap_offset
&& ((nfd
- new) <= prompt_last_invisible
))
2362 ADJUST_CPOS (wrap_offset
); /* XXX - prompt_invis_chars_first_line? */
2365 /* Sometimes it is cheaper to print the characters rather than
2366 use the terminal's capabilities. If we're growing the number
2367 of lines, make sure we actually cause the new line to wrap
2368 around on auto-wrapping terminals. */
2369 else if (_rl_terminal_can_insert
&& ((2 * col_temp
) >= col_lendiff
|| _rl_term_IC
) && (!_rl_term_autowrap
|| !gl
))
2371 /* If lendiff > prompt_visible_length and _rl_last_c_pos == 0 and
2372 _rl_horizontal_scroll_mode == 1, inserting the characters with
2373 _rl_term_IC or _rl_term_ic will screw up the screen because of the
2374 invisible characters. We need to just draw them. */
2375 /* The same thing happens if we're trying to draw before the last
2376 invisible character in the prompt string or we're increasing the
2377 number of invisible characters in the line and we're not drawing
2378 the entire prompt string. */
2379 if (*ols
&& ((_rl_horizontal_scroll_mode
&&
2380 _rl_last_c_pos
== 0 &&
2381 lendiff
> prompt_visible_length
&&
2382 current_invis_chars
> 0) == 0) &&
2383 (((mb_cur_max
> 1 && rl_byte_oriented
== 0) &&
2384 current_line
== 0 && wrap_offset
&&
2385 ((nfd
- new) <= prompt_last_invisible
) &&
2386 (col_lendiff
< prompt_visible_length
)) == 0) &&
2387 (visible_wrap_offset
>= current_invis_chars
))
2389 open_some_spaces (col_lendiff
);
2390 puts_face (nfd
, nfdf
, bytes_to_insert
);
2391 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
2392 _rl_last_c_pos
+= _rl_col_width (nfd
, 0, bytes_to_insert
, 1);
2394 _rl_last_c_pos
+= bytes_to_insert
;
2396 else if ((mb_cur_max
== 1 || rl_byte_oriented
!= 0) && *ols
== 0 && lendiff
> 0)
2398 /* At the end of a line the characters do not have to
2399 be "inserted". They can just be placed on the screen. */
2400 puts_face (nfd
, nfdf
, temp
);
2401 _rl_last_c_pos
+= col_temp
;
2404 else /* just write from first difference to end of new line */
2406 puts_face (nfd
, nfdf
, temp
);
2407 _rl_last_c_pos
+= col_temp
;
2408 /* If nfd begins before the last invisible character in the
2409 prompt, adjust _rl_last_c_pos to account for wrap_offset
2410 and set cpos_adjusted to let the caller know. */
2411 if ((mb_cur_max
> 1 && rl_byte_oriented
== 0) && current_line
== 0 && displaying_prompt_first_line
&& wrap_offset
&& ((nfd
- new) <= prompt_last_invisible
))
2412 ADJUST_CPOS (wrap_offset
); /* XXX - prompt_invis_chars_first_line? */
2416 if (bytes_to_insert
> lendiff
)
2418 /* If nfd begins before the last invisible character in the
2419 prompt, adjust _rl_last_c_pos to account for wrap_offset
2420 and set cpos_adjusted to let the caller know. */
2421 if ((mb_cur_max
> 1 && rl_byte_oriented
== 0) && current_line
== 0 && displaying_prompt_first_line
&& wrap_offset
&& ((nfd
- new) <= prompt_last_invisible
))
2422 ADJUST_CPOS (wrap_offset
); /* XXX - prompt_invis_chars_first_line? */
2427 /* cannot insert chars, write to EOL */
2428 puts_face (nfd
, nfdf
, temp
);
2429 _rl_last_c_pos
+= col_temp
;
2430 /* If we're in a multibyte locale and were before the last invisible
2431 char in the current line (which implies we just output some invisible
2432 characters) we need to adjust _rl_last_c_pos, since it represents
2433 a physical character position. */
2434 /* The current_line*rl_screenwidth+prompt_invis_chars_first_line is a
2435 crude attempt to compute how far into the new line buffer we are.
2436 It doesn't work well in the face of multibyte characters and needs
2437 to be rethought. XXX */
2438 if ((mb_cur_max
> 1 && rl_byte_oriented
== 0) &&
2439 current_line
== prompt_last_screen_line
&& wrap_offset
&&
2440 displaying_prompt_first_line
&&
2441 wrap_offset
!= prompt_invis_chars_first_line
&&
2442 ((nfd
-new) < (prompt_last_invisible
-(current_line
*_rl_screenwidth
+prompt_invis_chars_first_line
))))
2443 ADJUST_CPOS (wrap_offset
- prompt_invis_chars_first_line
);
2445 /* What happens if wrap_offset == prompt_invis_chars_first_line
2446 and we are drawing the first line (current_line == 0), or if we
2447 are drawing the first line and changing the number of invisible
2448 characters in the line? If we're starting to draw before the last
2449 invisible character in the prompt, we need to adjust by
2450 _rl_last_c_pos -= prompt_invis_chars_first_line. This can happen
2451 when we finish reading a digit argument (with the "(arg: N)"
2452 prompt) and are switching back to displaying a line with a prompt
2453 containing invisible characters, since we have to redraw the
2454 entire prompt string. */
2455 if ((mb_cur_max
> 1 && rl_byte_oriented
== 0) &&
2456 current_line
== 0 && wrap_offset
&&
2457 displaying_prompt_first_line
&&
2458 wrap_offset
== prompt_invis_chars_first_line
&&
2459 visible_wrap_offset
!= current_invis_chars
&&
2460 visible_wrap_offset
!= prompt_invis_chars_first_line
&&
2461 ((nfd
-new) < prompt_last_invisible
))
2462 ADJUST_CPOS (prompt_invis_chars_first_line
);
2465 else /* Delete characters from line. */
2467 /* If possible and inexpensive to use terminal deletion, then do so. */
2468 if (_rl_term_dc
&& (2 * col_temp
) >= -col_lendiff
)
2470 /* If all we're doing is erasing the invisible characters in the
2471 prompt string, don't bother. It screws up the assumptions
2472 about what's on the screen. */
2473 if (_rl_horizontal_scroll_mode
&& _rl_last_c_pos
== 0 &&
2474 displaying_prompt_first_line
&&
2475 -lendiff
== visible_wrap_offset
)
2478 /* If we have moved lmargin and we're shrinking the line, we've
2479 already moved the cursor to the first character of the new line,
2480 so deleting -col_lendiff characters will mess up the cursor
2481 position calculation */
2482 if (_rl_horizontal_scroll_mode
&& displaying_prompt_first_line
== 0 &&
2483 col_lendiff
&& _rl_last_c_pos
< -col_lendiff
)
2487 delete_chars (-col_lendiff
); /* delete (diff) characters */
2489 /* Copy (new) chars to screen from first diff to last match,
2490 overwriting what is there. */
2491 if (bytes_to_insert
> 0)
2493 /* If nfd begins at the prompt, or before the invisible
2494 characters in the prompt, we need to adjust _rl_last_c_pos
2495 in a multibyte locale to account for the wrap offset and
2496 set cpos_adjusted accordingly. */
2497 puts_face (nfd
, nfdf
, bytes_to_insert
);
2498 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
2500 /* This still doesn't take into account whether or not the
2501 characters that this counts are invisible. */
2502 _rl_last_c_pos
+= _rl_col_width (nfd
, 0, bytes_to_insert
, 1);
2503 if (current_line
== 0 && wrap_offset
&&
2504 displaying_prompt_first_line
&&
2505 prompt_invis_chars_first_line
&&
2506 _rl_last_c_pos
>= prompt_invis_chars_first_line
&&
2507 ((nfd
- new) <= prompt_last_invisible
))
2508 ADJUST_CPOS (prompt_invis_chars_first_line
);
2511 #ifdef HANDLE_MULTIBYTE
2512 /* If we write a non-space into the last screen column,
2513 remove the note that we added a space to compensate for
2514 a multibyte double-width character that didn't fit, since
2515 it's only valid for what was previously there. */
2516 /* XXX - watch this */
2517 if (_rl_last_c_pos
== _rl_screenwidth
&&
2518 line_state_invisible
->wrapped_line
[current_line
+1] &&
2519 nfd
[bytes_to_insert
-1] != ' ')
2520 line_state_invisible
->wrapped_line
[current_line
+1] = 0;
2525 _rl_last_c_pos
+= bytes_to_insert
;
2527 /* XXX - we only want to do this if we are at the end of the line
2528 so we move there with _rl_move_cursor_relative */
2529 if (_rl_horizontal_scroll_mode
&& ((oe
-old
) > (ne
-new)))
2531 _rl_move_cursor_relative (ne
-new, new, new_face
);
2532 goto clear_rest_of_line
;
2536 /* Otherwise, print over the existing material. */
2541 /* If nfd begins at the prompt, or before the invisible
2542 characters in the prompt, we need to adjust _rl_last_c_pos
2543 in a multibyte locale to account for the wrap offset and
2544 set cpos_adjusted accordingly. */
2545 puts_face (nfd
, nfdf
, temp
);
2546 _rl_last_c_pos
+= col_temp
; /* XXX */
2547 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
2549 if (current_line
== 0 && wrap_offset
&&
2550 displaying_prompt_first_line
&&
2551 _rl_last_c_pos
> wrap_offset
&&
2552 ((nfd
- new) <= prompt_last_invisible
))
2553 ADJUST_CPOS (wrap_offset
); /* XXX - prompt_invis_chars_first_line? */
2557 lendiff
= (oe
- old
) - (ne
- new);
2558 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
2559 col_lendiff
= _rl_col_width (old
, 0, oe
- old
, 1) - _rl_col_width (new, 0, ne
- new, 1);
2561 col_lendiff
= lendiff
;
2563 /* If we've already printed over the entire width of the screen,
2564 including the old material, then col_lendiff doesn't matter and
2565 space_to_eol will insert too many spaces. XXX - maybe we should
2566 adjust col_lendiff based on the difference between _rl_last_c_pos
2567 and _rl_screenwidth */
2568 if (col_lendiff
&& ((mb_cur_max
== 1 || rl_byte_oriented
) || (_rl_last_c_pos
< _rl_screenwidth
)))
2570 if (_rl_term_autowrap
&& current_line
< inv_botlin
)
2571 space_to_eol (col_lendiff
);
2573 _rl_clear_to_eol (col_lendiff
);
2579 /* Tell the update routines that we have moved onto a new (empty) line. */
2581 rl_on_new_line (void)
2584 visible_line
[0] = '\0';
2586 _rl_last_c_pos
= _rl_last_v_pos
= 0;
2587 _rl_vis_botlin
= last_lmargin
= 0;
2589 vis_lbreaks
[0] = vis_lbreaks
[1] = 0;
2590 visible_wrap_offset
= 0;
2594 /* Clear all screen lines occupied by the current readline line buffer
2597 rl_clear_visible_line (void)
2601 /* Make sure we move to column 0 so we clear the entire line */
2605 /* Move to the last screen line of the current visible line */
2606 _rl_move_vert (_rl_vis_botlin
);
2608 /* And erase screen lines going up to line 0 (first visible line) */
2609 for (curr_line
= _rl_last_v_pos
; curr_line
>= 0; curr_line
--)
2611 _rl_move_vert (curr_line
);
2612 _rl_clear_to_eol (_rl_screenwidth
);
2613 _rl_cr (); /* in case we use space_to_eol() */
2619 /* Tell the update routines that we have moved onto a new line with the
2620 prompt already displayed. Code originally from the version of readline
2621 distributed with CLISP. rl_expand_prompt must have already been called
2622 (explicitly or implicitly). This still doesn't work exactly right; it
2623 should use expand_prompt() */
2625 rl_on_new_line_with_prompt (void)
2627 int prompt_size
, i
, l
, real_screenwidth
, newlines
;
2628 char *prompt_last_line
, *lprompt
;
2630 /* Initialize visible_line and invisible_line to ensure that they can hold
2631 the already-displayed prompt. */
2632 prompt_size
= strlen (rl_prompt
) + 1;
2633 init_line_structures (prompt_size
);
2635 /* Make sure the line structures hold the already-displayed prompt for
2637 lprompt
= local_prompt
? local_prompt
: rl_prompt
;
2638 strcpy (visible_line
, lprompt
);
2639 strcpy (invisible_line
, lprompt
);
2641 /* If the prompt contains newlines, take the last tail. */
2642 prompt_last_line
= strrchr (rl_prompt
, '\n');
2643 if (!prompt_last_line
)
2644 prompt_last_line
= rl_prompt
;
2646 l
= strlen (prompt_last_line
);
2647 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
2648 _rl_last_c_pos
= _rl_col_width (prompt_last_line
, 0, l
, 1); /* XXX */
2652 /* Dissect prompt_last_line into screen lines. Note that here we have
2653 to use the real screenwidth. Readline's notion of screenwidth might be
2654 one less, see terminal.c. */
2655 real_screenwidth
= _rl_screenwidth
+ (_rl_term_autowrap
? 0 : 1);
2656 _rl_last_v_pos
= l
/ real_screenwidth
;
2657 /* If the prompt length is a multiple of real_screenwidth, we don't know
2658 whether the cursor is at the end of the last line, or already at the
2659 beginning of the next line. Output a newline just to be safe. */
2660 if (l
> 0 && (l
% real_screenwidth
) == 0)
2661 _rl_output_some_chars ("\n", 1);
2664 newlines
= 0; i
= 0;
2667 _rl_vis_botlin
= newlines
;
2668 vis_lbreaks
[newlines
++] = i
;
2669 i
+= real_screenwidth
;
2671 vis_lbreaks
[newlines
] = l
;
2672 visible_wrap_offset
= 0;
2674 rl_display_prompt
= rl_prompt
; /* XXX - make sure it's set */
2679 /* Actually update the display, period. */
2681 rl_forced_update_display (void)
2683 register char *temp
;
2686 memset (visible_line
, 0, line_size
);
2690 (*rl_redisplay_function
) ();
2694 /* Redraw only the last line of a multi-line prompt. */
2696 rl_redraw_prompt_last_line (void)
2700 t
= strrchr (rl_display_prompt
, '\n');
2702 redraw_prompt (++t
);
2704 rl_forced_update_display ();
2707 /* Move the cursor from _rl_last_c_pos to NEW, which are buffer indices.
2708 (Well, when we don't have multibyte characters, _rl_last_c_pos is a
2710 DATA is the contents of the screen line of interest; i.e., where
2711 the movement is being done.
2712 DATA is always the visible line or the invisible line */
2714 _rl_move_cursor_relative (int new, const char *data
, const char *dataf
)
2717 int woff
; /* number of invisible chars on current line */
2718 int cpos
, dpos
; /* current and desired cursor positions */
2721 int mb_cur_max
= MB_CUR_MAX
;
2723 woff
= WRAP_OFFSET (_rl_last_v_pos
, wrap_offset
);
2724 cpos
= _rl_last_c_pos
;
2726 if (cpos
== 0 && cpos
== new)
2729 #if defined (HANDLE_MULTIBYTE)
2730 /* If we have multibyte characters, NEW is indexed by the buffer point in
2731 a multibyte string, but _rl_last_c_pos is the display position. In
2732 this case, NEW's display position is not obvious and must be
2733 calculated. We need to account for invisible characters in this line,
2734 as long as we are past them and they are counted by _rl_col_width. */
2735 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
2738 /* Try to short-circuit common cases and eliminate a bunch of multibyte
2739 character function calls. */
2740 /* 1. prompt string */
2741 if (new == local_prompt_len
&& memcmp (data
, local_prompt
, new) == 0)
2743 dpos
= prompt_physical_chars
;
2747 /* 2. prompt_string + line contents */
2748 else if (new > local_prompt_len
&& local_prompt
&& memcmp (data
, local_prompt
, local_prompt_len
) == 0)
2750 dpos
= prompt_physical_chars
+ _rl_col_width (data
, local_prompt_len
, new, 1);
2755 dpos
= _rl_col_width (data
, 0, new, 1);
2757 if (displaying_prompt_first_line
== 0)
2760 /* yet another special case: printing the last line of a prompt with
2761 multibyte characters and invisible characters whose printable length
2762 exceeds the screen width with the last invisible character
2763 (prompt_last_invisible) in the last line. IN_INVISLINE is the
2764 offset of DATA in invisible_line */
2766 if (data
> invisible_line
&& data
< invisible_line
+inv_lbreaks
[_rl_inv_botlin
+1])
2767 in_invisline
= data
- invisible_line
;
2769 /* Use NEW when comparing against the last invisible character in the
2770 prompt string, since they're both buffer indices and DPOS is a
2771 desired display position. */
2772 /* NEW is relative to the current displayed line, while
2773 PROMPT_LAST_INVISIBLE is relative to the entire (wrapped) line.
2774 Need a way to reconcile these two variables by turning NEW into a
2775 buffer position relative to the start of the line */
2776 if (adjust
&& ((new > prompt_last_invisible
) || /* XXX - don't use woff here */
2777 (new+in_invisline
> prompt_last_invisible
) || /* invisible line */
2778 (prompt_physical_chars
>= _rl_screenwidth
&& /* visible line */
2779 _rl_last_v_pos
== prompt_last_screen_line
&&
2780 wrap_offset
>= woff
&& dpos
>= woff
&&
2781 new > (prompt_last_invisible
-(vis_lbreaks
[_rl_last_v_pos
])-wrap_offset
))))
2782 /* XXX last comparison might need to be >= */
2785 /* Since this will be assigned to _rl_last_c_pos at the end (more
2786 precisely, _rl_last_c_pos == dpos when this function returns),
2787 let the caller know. */
2795 /* If we don't have to do anything, then return. */
2799 /* It may be faster to output a CR, and then move forwards instead
2800 of moving backwards. */
2801 /* i == current physical cursor position. */
2802 #if defined (HANDLE_MULTIBYTE)
2803 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
2807 i
= _rl_last_c_pos
- woff
;
2808 if (dpos
== 0 || CR_FASTER (dpos
, _rl_last_c_pos
) ||
2809 (_rl_term_autowrap
&& i
== _rl_screenwidth
))
2812 cpos
= _rl_last_c_pos
= 0;
2817 /* Move the cursor forward. We do it by printing the command
2818 to move the cursor forward if there is one, else print that
2819 portion of the output buffer again. Which is cheaper? */
2821 /* The above comment is left here for posterity. It is faster
2822 to print one character (non-control) than to print a control
2823 sequence telling the terminal to move forward one character.
2824 That kind of control is for people who don't know what the
2825 data is underneath the cursor. */
2827 /* However, we need a handle on where the current display position is
2828 in the buffer for the immediately preceding comment to be true.
2829 In multibyte locales, we don't currently have that info available.
2830 Without it, we don't know where the data we have to display begins
2831 in the buffer and we have to go back to the beginning of the screen
2832 line. In this case, we can use the terminal sequence to move forward
2833 if it's available. */
2834 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
2836 if (_rl_term_forward_char
)
2838 for (i
= cpos
; i
< dpos
; i
++)
2839 tputs (_rl_term_forward_char
, 1, _rl_output_character_function
);
2844 puts_face (data
, dataf
, new);
2848 puts_face (data
+ cpos
, dataf
+ cpos
, new - cpos
);
2851 #if defined (HANDLE_MULTIBYTE)
2852 /* NEW points to the buffer point, but _rl_last_c_pos is the display point.
2853 The byte length of the string is probably bigger than the column width
2854 of the string, which means that if NEW == _rl_last_c_pos, then NEW's
2855 display point is less than _rl_last_c_pos. */
2857 else if (cpos
> dpos
)
2858 _rl_backspace (cpos
- dpos
);
2860 _rl_last_c_pos
= dpos
;
2863 /* PWP: move the cursor up or down. */
2865 _rl_move_vert (int to
)
2867 register int delta
, i
;
2869 if (_rl_last_v_pos
== to
|| to
> _rl_screenheight
)
2872 if ((delta
= to
- _rl_last_v_pos
) > 0)
2874 for (i
= 0; i
< delta
; i
++)
2875 putc ('\n', rl_outstream
);
2884 fflush (rl_outstream
);
2885 ScreenGetCursor (&row
, &col
);
2886 ScreenSetCursor (row
+ delta
, col
);
2889 if (_rl_term_up
&& *_rl_term_up
)
2890 for (i
= 0; i
< -delta
; i
++)
2891 tputs (_rl_term_up
, 1, _rl_output_character_function
);
2892 #endif /* !__DJGPP__ */
2895 _rl_last_v_pos
= to
; /* Now TO is here */
2898 /* Physically print C on rl_outstream. This is for functions which know
2899 how to optimize the display. Return the number of characters output. */
2901 rl_show_char (int c
)
2904 if (META_CHAR (c
) && (_rl_output_meta_chars
== 0))
2906 fprintf (rl_outstream
, "M-");
2911 #if defined (DISPLAY_TABS)
2912 if ((CTRL_CHAR (c
) && c
!= '\t') || c
== RUBOUT
)
2914 if (CTRL_CHAR (c
) || c
== RUBOUT
)
2915 #endif /* !DISPLAY_TABS */
2917 fprintf (rl_outstream
, "C-");
2919 c
= CTRL_CHAR (c
) ? UNCTRL (c
) : '?';
2922 putc (c
, rl_outstream
);
2923 fflush (rl_outstream
);
2928 rl_character_len (int c
, int pos
)
2932 uc
= (unsigned char)c
;
2935 return ((_rl_output_meta_chars
== 0) ? 4 : 1);
2939 #if defined (DISPLAY_TABS)
2940 return (((pos
| 7) + 1) - pos
);
2943 #endif /* !DISPLAY_TABS */
2946 if (CTRL_CHAR (c
) || c
== RUBOUT
)
2949 return ((ISPRINT (uc
)) ? 1 : 2);
2951 /* How to print things in the "echo-area". The prompt is treated as a
2953 static int msg_saved_prompt
= 0;
2955 #if defined (USE_VARARGS)
2957 #if defined (PREFER_STDARG)
2958 rl_message (const char *format
, ...)
2960 rl_message (va_alist
)
2965 #if defined (PREFER_VARARGS)
2968 #if defined (HAVE_VSNPRINTF)
2972 #if defined (PREFER_STDARG)
2973 va_start (args
, format
);
2976 format
= va_arg (args
, char *);
2980 msg_buf
= xmalloc (msg_bufsiz
= 128);
2982 #if defined (HAVE_VSNPRINTF)
2983 bneed
= vsnprintf (msg_buf
, msg_bufsiz
, format
, args
);
2984 if (bneed
>= msg_bufsiz
- 1)
2986 msg_bufsiz
= bneed
+ 1;
2987 msg_buf
= xrealloc (msg_buf
, msg_bufsiz
);
2990 #if defined (PREFER_STDARG)
2991 va_start (args
, format
);
2994 format
= va_arg (args
, char *);
2996 vsnprintf (msg_buf
, msg_bufsiz
- 1, format
, args
);
2999 vsprintf (msg_buf
, format
, args
);
3000 msg_buf
[msg_bufsiz
- 1] = '\0'; /* overflow? */
3004 if (saved_local_prompt
== 0)
3007 msg_saved_prompt
= 1;
3009 else if (local_prompt
!= saved_local_prompt
)
3011 FREE (local_prompt
);
3012 FREE (local_prompt_prefix
);
3013 local_prompt
= (char *)NULL
;
3015 rl_display_prompt
= msg_buf
;
3016 local_prompt
= expand_prompt (msg_buf
, 0, &prompt_visible_length
,
3017 &prompt_last_invisible
,
3018 &prompt_invis_chars_first_line
,
3019 &prompt_physical_chars
);
3020 local_prompt_prefix
= (char *)NULL
;
3021 local_prompt_len
= local_prompt
? strlen (local_prompt
) : 0;
3022 (*rl_redisplay_function
) ();
3026 #else /* !USE_VARARGS */
3028 rl_message (format
, arg1
, arg2
)
3032 msg_buf
= xmalloc (msg_bufsiz
= 128);
3034 sprintf (msg_buf
, format
, arg1
, arg2
);
3035 msg_buf
[msg_bufsiz
- 1] = '\0'; /* overflow? */
3037 rl_display_prompt
= msg_buf
;
3038 if (saved_local_prompt
== 0)
3041 msg_saved_prompt
= 1;
3043 else if (local_prompt
!= saved_local_prompt
)
3045 FREE (local_prompt
);
3046 FREE (local_prompt_prefix
);
3047 local_prompt
= (char *)NULL
;
3049 local_prompt
= expand_prompt (msg_buf
, 0, &prompt_visible_length
,
3050 &prompt_last_invisible
,
3051 &prompt_invis_chars_first_line
,
3052 &prompt_physical_chars
);
3053 local_prompt_prefix
= (char *)NULL
;
3054 local_prompt_len
= local_prompt
? strlen (local_prompt
) : 0;
3055 (*rl_redisplay_function
) ();
3059 #endif /* !USE_VARARGS */
3061 /* How to clear things from the "echo-area". */
3063 rl_clear_message (void)
3065 rl_display_prompt
= rl_prompt
;
3066 if (msg_saved_prompt
)
3068 rl_restore_prompt ();
3069 msg_saved_prompt
= 0;
3071 (*rl_redisplay_function
) ();
3076 rl_reset_line_state (void)
3080 rl_display_prompt
= rl_prompt
? rl_prompt
: "";
3085 /* Save all of the variables associated with the prompt and its display. Most
3086 of the complexity is dealing with the invisible characters in the prompt
3087 string and where they are. There are enough of these that I should consider
3090 rl_save_prompt (void)
3092 saved_local_prompt
= local_prompt
;
3093 saved_local_prefix
= local_prompt_prefix
;
3094 saved_prefix_length
= prompt_prefix_length
;
3095 saved_local_length
= local_prompt_len
;
3096 saved_last_invisible
= prompt_last_invisible
;
3097 saved_visible_length
= prompt_visible_length
;
3098 saved_invis_chars_first_line
= prompt_invis_chars_first_line
;
3099 saved_physical_chars
= prompt_physical_chars
;
3100 saved_local_prompt_newlines
= local_prompt_newlines
;
3102 local_prompt
= local_prompt_prefix
= (char *)0;
3103 local_prompt_len
= 0;
3104 local_prompt_newlines
= (int *)0;
3106 prompt_last_invisible
= prompt_visible_length
= prompt_prefix_length
= 0;
3107 prompt_invis_chars_first_line
= prompt_physical_chars
= 0;
3111 rl_restore_prompt (void)
3113 FREE (local_prompt
);
3114 FREE (local_prompt_prefix
);
3115 FREE (local_prompt_newlines
);
3117 local_prompt
= saved_local_prompt
;
3118 local_prompt_prefix
= saved_local_prefix
;
3119 local_prompt_len
= saved_local_length
;
3120 local_prompt_newlines
= saved_local_prompt_newlines
;
3122 prompt_prefix_length
= saved_prefix_length
;
3123 prompt_last_invisible
= saved_last_invisible
;
3124 prompt_visible_length
= saved_visible_length
;
3125 prompt_invis_chars_first_line
= saved_invis_chars_first_line
;
3126 prompt_physical_chars
= saved_physical_chars
;
3128 /* can test saved_local_prompt to see if prompt info has been saved. */
3129 saved_local_prompt
= saved_local_prefix
= (char *)0;
3130 saved_local_length
= 0;
3131 saved_last_invisible
= saved_visible_length
= saved_prefix_length
= 0;
3132 saved_invis_chars_first_line
= saved_physical_chars
= 0;
3133 saved_local_prompt_newlines
= 0;
3137 _rl_make_prompt_for_search (int pchar
)
3144 /* We've saved the prompt, and can do anything with the various prompt
3145 strings we need before they're restored. We want the unexpanded
3146 portion of the prompt string after any final newline. */
3147 p
= rl_prompt
? strrchr (rl_prompt
, '\n') : 0;
3150 len
= (rl_prompt
&& *rl_prompt
) ? strlen (rl_prompt
) : 0;
3151 pmt
= (char *)xmalloc (len
+ 2);
3153 strcpy (pmt
, rl_prompt
);
3161 pmt
= (char *)xmalloc (len
+ 2);
3168 /* will be overwritten by expand_prompt, called from rl_message */
3169 prompt_physical_chars
= saved_physical_chars
+ 1;
3173 /* Quick redisplay hack when erasing characters at the end of the line. */
3175 _rl_erase_at_end_of_line (int l
)
3180 for (i
= 0; i
< l
; i
++)
3181 putc (' ', rl_outstream
);
3183 for (i
= 0; i
< l
; i
++)
3184 visible_line
[--_rl_last_c_pos
] = '\0';
3188 /* Clear to the end of the line. COUNT is the minimum
3189 number of character spaces to clear, but we use a terminal escape
3190 sequence if available. */
3192 _rl_clear_to_eol (int count
)
3195 if (_rl_term_clreol
)
3196 tputs (_rl_term_clreol
, 1, _rl_output_character_function
);
3200 space_to_eol (count
);
3203 /* Clear to the end of the line using spaces. COUNT is the minimum
3204 number of character spaces to clear, */
3206 space_to_eol (int count
)
3210 for (i
= 0; i
< count
; i
++)
3211 putc (' ', rl_outstream
);
3213 _rl_last_c_pos
+= count
;
3217 _rl_clear_screen (int clrscr
)
3219 #if defined (__DJGPP__)
3221 ScreenSetCursor (0, 0);
3223 if (_rl_term_clrpag
)
3225 tputs (_rl_term_clrpag
, 1, _rl_output_character_function
);
3226 if (clrscr
&& _rl_term_clrscroll
)
3227 tputs (_rl_term_clrscroll
, 1, _rl_output_character_function
);
3231 #endif /* __DJGPP__ */
3234 /* Insert COUNT characters from STRING to the output stream at column COL. */
3236 insert_some_chars (char *string
, int count
, int col
)
3238 open_some_spaces (col
);
3239 _rl_output_some_chars (string
, count
);
3242 /* Insert COL spaces, keeping the cursor at the same position. We follow the
3243 ncurses documentation and use either im/ei with explicit spaces, or IC/ic
3244 by itself. We assume there will either be ei or we don't need to use it. */
3246 open_some_spaces (int col
)
3248 #if !defined (__MSDOS__) && (!defined (__MINGW32__) || defined (NCURSES_VERSION))
3252 /* If IC is defined, then we do not have to "enter" insert mode. */
3255 buffer
= tgoto (_rl_term_IC
, 0, col
);
3256 tputs (buffer
, 1, _rl_output_character_function
);
3258 else if (_rl_term_im
&& *_rl_term_im
)
3260 tputs (_rl_term_im
, 1, _rl_output_character_function
);
3261 /* just output the desired number of spaces */
3262 for (i
= col
; i
--; )
3263 _rl_output_character_function (' ');
3264 /* If there is a string to turn off insert mode, use it now. */
3265 if (_rl_term_ei
&& *_rl_term_ei
)
3266 tputs (_rl_term_ei
, 1, _rl_output_character_function
);
3267 /* and move back the right number of spaces */
3268 _rl_backspace (col
);
3270 else if (_rl_term_ic
&& *_rl_term_ic
)
3272 /* If there is a special command for inserting characters, then
3273 use that first to open up the space. */
3274 for (i
= col
; i
--; )
3275 tputs (_rl_term_ic
, 1, _rl_output_character_function
);
3277 #endif /* !__MSDOS__ && (!__MINGW32__ || NCURSES_VERSION)*/
3280 /* Delete COUNT characters from the display line. */
3282 delete_chars (int count
)
3284 if (count
> _rl_screenwidth
) /* XXX */
3287 #if !defined (__MSDOS__) && (!defined (__MINGW32__) || defined (NCURSES_VERSION))
3288 if (_rl_term_DC
&& *_rl_term_DC
)
3291 buffer
= tgoto (_rl_term_DC
, count
, count
);
3292 tputs (buffer
, count
, _rl_output_character_function
);
3296 if (_rl_term_dc
&& *_rl_term_dc
)
3298 tputs (_rl_term_dc
, 1, _rl_output_character_function
);
3300 #endif /* !__MSDOS__ && (!__MINGW32__ || NCURSES_VERSION)*/
3304 _rl_update_final (void)
3306 int full_lines
, woff
, botline_length
;
3308 if (line_structures_initialized
== 0)
3312 /* If the cursor is the only thing on an otherwise-blank last line,
3313 compensate so we don't print an extra CRLF. */
3314 if (_rl_vis_botlin
&& _rl_last_c_pos
== 0 &&
3315 visible_line
[vis_lbreaks
[_rl_vis_botlin
]] == 0)
3320 _rl_move_vert (_rl_vis_botlin
);
3321 woff
= W_OFFSET(_rl_vis_botlin
, wrap_offset
);
3322 botline_length
= VIS_LLEN(_rl_vis_botlin
) - woff
;
3323 /* If we've wrapped lines, remove the final xterm line-wrap flag. */
3324 if (full_lines
&& _rl_term_autowrap
&& botline_length
== _rl_screenwidth
)
3326 char *last_line
, *last_face
;
3328 /* LAST_LINE includes invisible characters, so if you want to get the
3329 last character of the first line, you have to take WOFF into account.
3330 This needs to be done for both calls to _rl_move_cursor_relative,
3331 which takes a buffer position as the first argument, and any direct
3332 subscripts of LAST_LINE. */
3333 last_line
= &visible_line
[vis_lbreaks
[_rl_vis_botlin
]]; /* = VIS_CHARS(_rl_vis_botlin); */
3334 last_face
= &vis_face
[vis_lbreaks
[_rl_vis_botlin
]]; /* = VIS_CHARS(_rl_vis_botlin); */
3335 cpos_buffer_position
= -1; /* don't know where we are in buffer */
3336 _rl_move_cursor_relative (_rl_screenwidth
- 1 + woff
, last_line
, last_face
); /* XXX */
3337 _rl_clear_to_eol (0);
3338 puts_face (&last_line
[_rl_screenwidth
- 1 + woff
],
3339 &last_face
[_rl_screenwidth
- 1 + woff
], 1);
3342 if (botline_length
> 0 || _rl_last_c_pos
> 0)
3344 fflush (rl_outstream
);
3348 /* Move to the start of the current line. */
3356 /* Redraw the last line of a multi-line prompt that may possibly contain
3357 terminal escape sequences. Called with the cursor at column 0 of the
3358 line to draw the prompt on. */
3360 redraw_prompt (char *t
)
3364 oldp
= rl_display_prompt
;
3367 rl_display_prompt
= t
;
3368 local_prompt
= expand_prompt (t
, PMT_MULTILINE
,
3369 &prompt_visible_length
,
3370 &prompt_last_invisible
,
3371 &prompt_invis_chars_first_line
,
3372 &prompt_physical_chars
);
3373 local_prompt_prefix
= (char *)NULL
;
3374 local_prompt_len
= local_prompt
? strlen (local_prompt
) : 0;
3376 rl_forced_update_display ();
3378 rl_display_prompt
= oldp
;
3379 rl_restore_prompt();
3382 /* Redisplay the current line after a SIGWINCH is received. */
3384 _rl_redisplay_after_sigwinch (void)
3388 /* Clear the last line (assuming that the screen size change will result in
3389 either more or fewer characters on that line only) and put the cursor at
3390 column 0. Make sure the right thing happens if we have wrapped to a new
3394 rl_clear_visible_line ();
3395 if (_rl_last_v_pos
> 0)
3401 if (_rl_screenwidth
< prompt_visible_length
)
3402 _rl_reset_prompt (); /* update local_prompt_newlines array */
3404 /* Redraw only the last line of a multi-line prompt. */
3405 t
= strrchr (rl_display_prompt
, '\n');
3407 redraw_prompt (++t
);
3409 rl_forced_update_display ();
3413 _rl_clean_up_for_exit (void)
3417 if (_rl_vis_botlin
> 0) /* minor optimization plus bug fix */
3418 _rl_move_vert (_rl_vis_botlin
);
3420 fflush (rl_outstream
);
3421 rl_restart_output (1, 0);
3426 _rl_erase_entire_line (void)
3429 _rl_clear_to_eol (0);
3431 fflush (rl_outstream
);
3437 fflush (rl_outstream
);
3440 /* return the `current display line' of the cursor -- the number of lines to
3441 move up to get to the first screen line of the current readline line. */
3443 _rl_current_display_line (void)
3447 /* Find out whether or not there might be invisible characters in the
3449 if (rl_display_prompt
== rl_prompt
)
3450 nleft
= _rl_last_c_pos
- _rl_screenwidth
- rl_visible_prompt_length
;
3452 nleft
= _rl_last_c_pos
- _rl_screenwidth
;
3455 ret
= 1 + nleft
/ _rl_screenwidth
;
3463 _rl_refresh_line (void)
3465 rl_clear_visible_line ();
3466 rl_redraw_prompt_last_line ();
3467 rl_keep_mark_active ();
3470 #if defined (HANDLE_MULTIBYTE)
3471 /* Calculate the number of screen columns occupied by STR from START to END.
3472 In the case of multibyte characters with stateful encoding, we have to
3473 scan from the beginning of the string to take the state into account. */
3475 _rl_col_width (const char *str
, int start
, int end
, int flags
)
3479 int tmp
, point
, width
, max
;
3483 if (MB_CUR_MAX
== 1 || rl_byte_oriented
)
3484 /* this can happen in some cases where it's inconvenient to check */
3485 return (end
- start
);
3487 memset (&ps
, 0, sizeof (mbstate_t));
3492 /* Try to short-circuit common cases. The adjustment to remove wrap_offset
3493 is done by the caller. */
3494 /* 1. prompt string */
3495 if (flags
&& start
== 0 && end
== local_prompt_len
&& memcmp (str
, local_prompt
, local_prompt_len
) == 0)
3496 return (prompt_physical_chars
+ wrap_offset
);
3497 /* 2. prompt string + line contents */
3498 else if (flags
&& start
== 0 && local_prompt_len
> 0 && end
> local_prompt_len
&& local_prompt
&& memcmp (str
, local_prompt
, local_prompt_len
) == 0)
3500 tmp
= prompt_physical_chars
+ wrap_offset
;
3501 /* XXX - try to call ourselves recursively with non-prompt portion */
3502 tmp
+= _rl_col_width (str
, local_prompt_len
, end
, flags
);
3506 while (point
< start
)
3508 if (_rl_utf8locale
&& UTF8_SINGLEBYTE(str
[point
]))
3510 memset (&ps
, 0, sizeof (mbstate_t));
3514 tmp
= mbrlen (str
+ point
, max
, &ps
);
3515 if (MB_INVALIDCH ((size_t)tmp
))
3517 /* In this case, the bytes are invalid or too short to compose a
3518 multibyte character, so we assume that the first byte represents
3519 a single character. */
3523 /* Clear the state of the byte sequence, because in this case the
3524 effect of mbstate is undefined. */
3525 memset (&ps
, 0, sizeof (mbstate_t));
3527 else if (MB_NULLWCH (tmp
))
3528 break; /* Found '\0' */
3536 /* If START is not a byte that starts a character, then POINT will be
3537 greater than START. In this case, assume that (POINT - START) gives
3538 a byte count that is the number of columns of difference. */
3539 width
= point
- start
;
3543 if (_rl_utf8locale
&& UTF8_SINGLEBYTE(str
[point
]))
3546 wc
= (WCHAR_T
) str
[point
];
3549 tmp
= MBRTOWC (&wc
, str
+ point
, max
, &ps
);
3550 if (MB_INVALIDCH ((size_t)tmp
))
3552 /* In this case, the bytes are invalid or too short to compose a
3553 multibyte character, so we assume that the first byte represents
3554 a single character. */
3558 /* and assume that the byte occupies a single column. */
3561 /* Clear the state of the byte sequence, because in this case the
3562 effect of mbstate is undefined. */
3563 memset (&ps
, 0, sizeof (mbstate_t));
3565 else if (MB_NULLWCH (tmp
))
3566 break; /* Found '\0' */
3572 width
+= (tmp
>= 0) ? tmp
: 1;
3576 width
+= point
- end
;
3580 #endif /* HANDLE_MULTIBYTE */