1 /* display.c -- readline redisplay facility. */
3 /* Copyright (C) 1987-2015 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 update_line
PARAMS((char *, char *, int, int, int, int));
67 static void space_to_eol
PARAMS((int));
68 static void delete_chars
PARAMS((int));
69 static void insert_some_chars
PARAMS((char *, int, int));
70 static void open_some_spaces
PARAMS((int));
71 static void cr
PARAMS((void));
72 static void redraw_prompt
PARAMS((char *));
74 /* Values for FLAGS */
75 #define PMT_MULTILINE 0x01
77 static char *expand_prompt
PARAMS((char *, int, int *, int *, int *, int *));
79 /* State of visible and invisible lines. */
85 #if defined (HANDLE_MULTIBYTE)
91 /* The line display buffers. One is the line currently displayed on
92 the screen. The other is the line about to be displayed. */
93 static struct line_state line_state_array
[2];
94 static struct line_state
*line_state_visible
= &line_state_array
[0];
95 static struct line_state
*line_state_invisible
= &line_state_array
[1];
96 static int line_structures_initialized
= 0;
98 /* Backwards-compatible names. */
99 #define inv_lbreaks (line_state_invisible->lbreaks)
100 #define inv_lbsize (line_state_invisible->lbsize)
101 #define vis_lbreaks (line_state_visible->lbreaks)
102 #define vis_lbsize (line_state_visible->lbsize)
104 #define visible_line (line_state_visible->line)
105 #define invisible_line (line_state_invisible->line)
107 #if defined (HANDLE_MULTIBYTE)
108 static int _rl_col_width
PARAMS((const char *, int, int, int));
110 # define _rl_col_width(l, s, e, f) (((e) <= (s)) ? 0 : (e) - (s))
113 /* Heuristic used to decide whether it is faster to move from CUR to NEW
114 by backing up or outputting a carriage return and moving forward. CUR
115 and NEW are either both buffer positions or absolute screen positions. */
116 #define CR_FASTER(new, cur) (((new) + 1) < ((cur) - (new)))
118 /* _rl_last_c_pos is an absolute cursor position in multibyte locales and a
119 buffer index in others. This macro is used when deciding whether the
120 current cursor position is in the middle of a prompt string containing
121 invisible characters. XXX - might need to take `modmark' into account. */
122 /* XXX - only valid when tested against _rl_last_c_pos; buffer indices need
123 to use prompt_last_invisible directly. */
124 #define PROMPT_ENDING_INDEX \
125 ((MB_CUR_MAX > 1 && rl_byte_oriented == 0) ? prompt_physical_chars : prompt_last_invisible+1)
128 /* **************************************************************** */
132 /* **************************************************************** */
134 /* This is the stuff that is hard for me. I never seem to write good
135 display routines in C. Let's see how I do this time. */
137 /* (PWP) Well... Good for a simple line updater, but totally ignores
138 the problems of input lines longer than the screen width.
140 update_line and the code that calls it makes a multiple line,
141 automatically wrapping line update. Careful attention needs
142 to be paid to the vertical position variables. */
144 /* Keep two buffers; one which reflects the current contents of the
145 screen, and the other to draw what we think the new contents should
146 be. Then compare the buffers, and make whatever changes to the
147 screen itself that we should. Finally, make the buffer that we
148 just drew into be the one which reflects the current contents of the
149 screen, and place the cursor where it belongs.
151 Commands that want to can fix the display themselves, and then let
152 this function know that the display has been fixed by setting the
153 RL_DISPLAY_FIXED variable. This is good for efficiency. */
155 /* Application-specific redisplay function. */
156 rl_voidfunc_t
*rl_redisplay_function
= rl_redisplay
;
158 /* Global variables declared here. */
159 /* What YOU turn on when you have handled all redisplay yourself. */
160 int rl_display_fixed
= 0;
162 int _rl_suppress_redisplay
= 0;
163 int _rl_want_redisplay
= 0;
165 /* The stuff that gets printed out before the actual text of the line.
166 This is usually pointing to rl_prompt. */
167 char *rl_display_prompt
= (char *)NULL
;
169 /* Variables used to include the editing mode in the prompt. */
170 char *_rl_emacs_mode_str
;
171 int _rl_emacs_modestr_len
;
173 char *_rl_vi_ins_mode_str
;
174 int _rl_vi_ins_modestr_len
;
176 char *_rl_vi_cmd_mode_str
;
177 int _rl_vi_cmd_modestr_len
;
179 /* Pseudo-global variables declared here. */
181 /* The visible cursor position. If you print some text, adjust this. */
182 /* NOTE: _rl_last_c_pos is used as a buffer index when not in a locale
183 supporting multibyte characters, and an absolute cursor position when
184 in such a locale. This is an artifact of the donated multibyte support.
185 Care must be taken when modifying its value. */
186 int _rl_last_c_pos
= 0;
187 int _rl_last_v_pos
= 0;
189 static int cpos_adjusted
;
190 static int cpos_buffer_position
;
191 static int displaying_prompt_first_line
;
192 static int prompt_multibyte_chars
;
194 /* Number of lines currently on screen minus 1. */
195 int _rl_vis_botlin
= 0;
197 static int _rl_inv_botlin
= 0;
199 /* Variables used only in this file. */
200 /* The last left edge of text that was displayed. This is used when
201 doing horizontal scrolling. It shifts in thirds of a screenwidth. */
202 static int last_lmargin
;
204 /* A buffer for `modeline' messages. */
205 static char *msg_buf
= 0;
206 static int msg_bufsiz
= 0;
208 /* Non-zero forces the redisplay even if we thought it was unnecessary. */
209 static int forced_display
;
211 /* Default and initial buffer size. Can grow. */
212 static int line_size
= 1024;
214 /* Variables to keep track of the expanded prompt string, which may
215 include invisible characters. */
217 static char *local_prompt
, *local_prompt_prefix
;
218 static int local_prompt_len
;
219 static int prompt_visible_length
, prompt_prefix_length
;
221 /* The number of invisible characters in the line currently being
222 displayed on the screen. */
223 static int visible_wrap_offset
;
225 /* The number of invisible characters in the prompt string. Static so it
226 can be shared between rl_redisplay and update_line */
227 static int wrap_offset
;
229 /* The index of the last invisible character in the prompt string. */
230 static int prompt_last_invisible
;
232 /* The length (buffer offset) of the first line of the last (possibly
233 multi-line) buffer displayed on the screen. */
234 static int visible_first_line_len
;
236 /* Number of invisible characters on the first physical line of the prompt.
237 Only valid when the number of physical characters in the prompt exceeds
238 (or is equal to) _rl_screenwidth. */
239 static int prompt_invis_chars_first_line
;
241 static int prompt_last_screen_line
;
243 static int prompt_physical_chars
;
245 /* set to a non-zero value by rl_redisplay if we are marking modified history
246 lines and the current line is so marked. */
249 /* Variables to save and restore prompt and display information. */
251 /* These are getting numerous enough that it's time to create a struct. */
253 static char *saved_local_prompt
;
254 static char *saved_local_prefix
;
255 static int saved_last_invisible
;
256 static int saved_visible_length
;
257 static int saved_prefix_length
;
258 static int saved_local_length
;
259 static int saved_invis_chars_first_line
;
260 static int saved_physical_chars
;
262 /* Return a string indicating the editing mode, for use in the prompt. */
265 prompt_modestr (lenp
)
268 if (rl_editing_mode
== emacs_mode
)
271 *lenp
= _rl_emacs_mode_str
? _rl_emacs_modestr_len
: RL_EMACS_MODESTR_DEFLEN
;
272 return _rl_emacs_mode_str
? _rl_emacs_mode_str
: RL_EMACS_MODESTR_DEFAULT
;
274 else if (_rl_keymap
== vi_insertion_keymap
)
277 *lenp
= _rl_vi_ins_mode_str
? _rl_vi_ins_modestr_len
: RL_VI_INS_MODESTR_DEFLEN
;
278 return _rl_vi_ins_mode_str
? _rl_vi_ins_mode_str
: RL_VI_INS_MODESTR_DEFAULT
; /* vi insert mode */
283 *lenp
= _rl_vi_cmd_mode_str
? _rl_vi_cmd_modestr_len
: RL_VI_CMD_MODESTR_DEFLEN
;
284 return _rl_vi_cmd_mode_str
? _rl_vi_cmd_mode_str
: RL_VI_CMD_MODESTR_DEFAULT
; /* vi command mode */
288 /* Expand the prompt string S and return the number of visible
289 characters in *LP, if LP is not null. This is currently more-or-less
290 a placeholder for expansion. LIP, if non-null is a place to store the
291 index of the last invisible character in the returned string. NIFLP,
292 if non-zero, is a place to store the number of invisible characters in
293 the first prompt line. The previous are used as byte counts -- indexes
294 into a character buffer. */
296 /* Current implementation:
297 \001 (^A) start non-visible characters
298 \002 (^B) end non-visible characters
299 all characters except \001 and \002 (following a \001) are copied to
300 the returned string; all characters except those between \001 and
301 \002 are assumed to be `visible'. */
303 /* Possible values for FLAGS:
304 PMT_MULTILINE caller indicates that this is part of a multiline prompt
308 expand_prompt (pmt
, flags
, lp
, lip
, niflp
, vlp
)
311 int *lp
, *lip
, *niflp
, *vlp
;
313 char *r
, *ret
, *p
, *igstart
, *nprompt
, *ms
;
314 int l
, rl
, last
, ignoring
, ninvis
, invfl
, invflset
, ind
, pind
, physchars
;
317 /* We only expand the mode string for the last line of a multiline prompt
318 (a prompt with embedded newlines). */
319 ms
= (((pmt
== rl_prompt
) ^ (flags
& PMT_MULTILINE
)) && _rl_show_mode_in_prompt
) ? prompt_modestr (&mlen
) : 0;
323 nprompt
= (char *)xmalloc (l
+ mlen
+ 1);
324 memcpy (nprompt
, ms
, mlen
);
325 strcpy (nprompt
+ mlen
, pmt
);
330 /* Short-circuit if we can. */
331 if ((MB_CUR_MAX
<= 1 || rl_byte_oriented
) && strchr (nprompt
, RL_PROMPT_START_IGNORE
) == 0)
333 r
= (nprompt
== pmt
) ? savestring (pmt
) : nprompt
;
342 *vlp
= lp
? *lp
: strlen (r
);
346 l
= strlen (nprompt
); /* XXX */
347 r
= ret
= (char *)xmalloc (l
+ 1);
349 rl
= physchars
= 0; /* mode string now part of nprompt */
350 invfl
= 0; /* invisible chars in first line of prompt */
351 invflset
= 0; /* we only want to set invfl once */
354 for (ignoring
= last
= ninvis
= 0, p
= nprompt
; p
&& *p
; p
++)
356 /* This code strips the invisible character string markers
357 RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE */
358 if (ignoring
== 0 && *p
== RL_PROMPT_START_IGNORE
) /* XXX - check ignoring? */
364 else if (ignoring
&& *p
== RL_PROMPT_END_IGNORE
)
367 if (p
!= (igstart
+ 1))
373 #if defined (HANDLE_MULTIBYTE)
374 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
377 ind
= _rl_find_next_mbchar (nprompt
, pind
, 1, MB_FIND_NONZERO
);
383 /* rl ends up being assigned to prompt_visible_length,
384 which is the number of characters in the buffer that
385 contribute to characters on the screen, which might
386 not be the same as the number of physical characters
387 on the screen in the presence of multibyte characters */
389 physchars
+= _rl_col_width (nprompt
, pind
, ind
, 0);
392 ninvis
+= ind
- pind
;
393 p
--; /* compensate for later increment */
401 rl
++; /* visible length byte counter */
405 ninvis
++; /* invisible chars byte counter */
408 if (invflset
== 0 && rl
>= _rl_screenwidth
)
416 if (rl
< _rl_screenwidth
)
435 /* Just strip out RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE from
436 PMT and return the rest of PMT. */
438 _rl_strip_prompt (pmt
)
443 ret
= expand_prompt (pmt
, 0, (int *)NULL
, (int *)NULL
, (int *)NULL
, (int *)NULL
);
450 rl_visible_prompt_length
= rl_expand_prompt (rl_prompt
);
454 * Expand the prompt string into the various display components, if
457 * local_prompt = expanded last line of string in rl_display_prompt
458 * (portion after the final newline)
459 * local_prompt_prefix = portion before last newline of rl_display_prompt,
460 * expanded via expand_prompt
461 * prompt_visible_length = number of visible characters in local_prompt
462 * prompt_prefix_length = number of visible characters in local_prompt_prefix
464 * This function is called once per call to readline(). It may also be
465 * called arbitrarily to expand the primary prompt.
467 * The return value is the number of visible characters on the last line
468 * of the (possibly multi-line) prompt.
471 rl_expand_prompt (prompt
)
477 /* Clear out any saved values. */
479 FREE (local_prompt_prefix
);
481 local_prompt
= local_prompt_prefix
= (char *)0;
482 local_prompt_len
= 0;
483 prompt_last_invisible
= prompt_invis_chars_first_line
= 0;
484 prompt_visible_length
= prompt_physical_chars
= 0;
486 if (prompt
== 0 || *prompt
== 0)
489 p
= strrchr (prompt
, '\n');
492 /* The prompt is only one logical line, though it might wrap. */
493 local_prompt
= expand_prompt (prompt
, 0, &prompt_visible_length
,
494 &prompt_last_invisible
,
495 &prompt_invis_chars_first_line
,
496 &prompt_physical_chars
);
497 local_prompt_prefix
= (char *)0;
498 local_prompt_len
= local_prompt
? strlen (local_prompt
) : 0;
499 return (prompt_visible_length
);
503 /* The prompt spans multiple lines. */
505 local_prompt
= expand_prompt (p
, PMT_MULTILINE
,
506 &prompt_visible_length
,
507 &prompt_last_invisible
,
508 &prompt_invis_chars_first_line
,
509 &prompt_physical_chars
);
511 /* The portion of the prompt string up to and including the
512 final newline is now null-terminated. */
513 local_prompt_prefix
= expand_prompt (prompt
, PMT_MULTILINE
,
514 &prompt_prefix_length
,
519 local_prompt_len
= local_prompt
? strlen (local_prompt
) : 0;
520 return (prompt_prefix_length
);
524 /* Initialize the VISIBLE_LINE and INVISIBLE_LINE arrays, and their associated
525 arrays of line break markers. MINSIZE is the minimum size of VISIBLE_LINE
526 and INVISIBLE_LINE; if it is greater than LINE_SIZE, LINE_SIZE is
527 increased. If the lines have already been allocated, this ensures that
528 they can hold at least MINSIZE characters. */
530 init_line_structures (minsize
)
535 if (invisible_line
== 0) /* initialize it */
537 if (line_size
< minsize
)
539 visible_line
= (char *)xmalloc (line_size
);
540 invisible_line
= (char *)xmalloc (line_size
);
542 else if (line_size
< minsize
) /* ensure it can hold MINSIZE chars */
545 if (line_size
< minsize
)
547 visible_line
= (char *)xrealloc (visible_line
, line_size
);
548 invisible_line
= (char *)xrealloc (invisible_line
, line_size
);
551 for (n
= minsize
; n
< line_size
; n
++)
554 invisible_line
[n
] = 1;
557 if (vis_lbreaks
== 0)
559 /* should be enough. */
560 inv_lbsize
= vis_lbsize
= 256;
562 #if defined (HANDLE_MULTIBYTE)
563 line_state_visible
->wbsize
= vis_lbsize
;
564 line_state_visible
->wrapped_line
= (int *)xmalloc (line_state_visible
->wbsize
* sizeof (int));
566 line_state_invisible
->wbsize
= inv_lbsize
;
567 line_state_invisible
->wrapped_line
= (int *)xmalloc (line_state_invisible
->wbsize
* sizeof (int));
570 inv_lbreaks
= (int *)xmalloc (inv_lbsize
* sizeof (int));
571 vis_lbreaks
= (int *)xmalloc (vis_lbsize
* sizeof (int));
572 inv_lbreaks
[0] = vis_lbreaks
[0] = 0;
575 line_structures_initialized
= 1;
578 /* Basic redisplay algorithm. */
582 register int in
, out
, c
, linenum
, cursor_linenum
;
584 int inv_botlin
, lb_botlin
, lb_linenum
, o_cpos
;
585 int newlines
, lpos
, temp
, n0
, num
, prompt_lines_estimate
;
586 char *prompt_this_line
;
587 int mb_cur_max
= MB_CUR_MAX
;
588 #if defined (HANDLE_MULTIBYTE)
593 int _rl_wrapped_multicolumn
= 0;
596 if (_rl_echoing_p
== 0)
599 /* Block keyboard interrupts because this function manipulates global
602 RL_SETSTATE (RL_STATE_REDISPLAYING
);
604 if (!rl_display_prompt
)
605 rl_display_prompt
= "";
607 if (line_structures_initialized
== 0)
609 init_line_structures (0);
613 /* Draw the line into the buffer. */
614 cpos_buffer_position
= -1;
616 prompt_multibyte_chars
= prompt_visible_length
- prompt_physical_chars
;
618 line
= invisible_line
;
619 out
= inv_botlin
= 0;
621 /* Mark the line as modified or not. We only do this for history
624 if (_rl_mark_modified_lines
&& current_history () && rl_undo_list
)
631 /* If someone thought that the redisplay was handled, but the currently
632 visible line has a different modification state than the one about
633 to become visible, then correct the caller's misconception. */
634 if (visible_line
[0] != invisible_line
[0])
635 rl_display_fixed
= 0;
637 /* If the prompt to be displayed is the `primary' readline prompt (the
638 one passed to readline()), use the values we have already expanded.
639 If not, use what's already in rl_display_prompt. WRAP_OFFSET is the
640 number of non-visible characters in the prompt string. */
641 if (rl_display_prompt
== rl_prompt
|| local_prompt
)
643 if (local_prompt_prefix
&& forced_display
)
644 _rl_output_some_chars (local_prompt_prefix
, strlen (local_prompt_prefix
));
646 if (local_prompt_len
> 0)
648 temp
= local_prompt_len
+ out
+ 2;
649 if (temp
>= line_size
)
651 line_size
= (temp
+ 1024) - (temp
% 1024);
652 visible_line
= (char *)xrealloc (visible_line
, line_size
);
653 line
= invisible_line
= (char *)xrealloc (invisible_line
, line_size
);
655 strncpy (line
+ out
, local_prompt
, local_prompt_len
);
656 out
+= local_prompt_len
;
659 wrap_offset
= local_prompt_len
- prompt_visible_length
;
664 prompt_this_line
= strrchr (rl_display_prompt
, '\n');
665 if (!prompt_this_line
)
666 prompt_this_line
= rl_display_prompt
;
670 pmtlen
= prompt_this_line
- rl_display_prompt
; /* temp var */
673 _rl_output_some_chars (rl_display_prompt
, pmtlen
);
674 /* Make sure we are at column zero even after a newline,
675 regardless of the state of terminal output processing. */
676 if (pmtlen
< 2 || prompt_this_line
[-2] != '\r')
681 prompt_physical_chars
= pmtlen
= strlen (prompt_this_line
);
682 temp
= pmtlen
+ out
+ 2;
683 if (temp
>= line_size
)
685 line_size
= (temp
+ 1024) - (temp
% 1024);
686 visible_line
= (char *)xrealloc (visible_line
, line_size
);
687 line
= invisible_line
= (char *)xrealloc (invisible_line
, line_size
);
689 strncpy (line
+ out
, prompt_this_line
, pmtlen
);
692 wrap_offset
= prompt_invis_chars_first_line
= 0;
695 #define CHECK_INV_LBREAKS() \
697 if (newlines >= (inv_lbsize - 2)) \
700 inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
704 #if defined (HANDLE_MULTIBYTE)
705 #define CHECK_LPOS() \
708 if (lpos >= _rl_screenwidth) \
710 if (newlines >= (inv_lbsize - 2)) \
713 inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
715 inv_lbreaks[++newlines] = out; \
716 if (newlines >= (line_state_invisible->wbsize - 1)) \
718 line_state_invisible->wbsize *= 2; \
719 line_state_invisible->wrapped_line = (int *)xrealloc (line_state_invisible->wrapped_line, line_state_invisible->wbsize * sizeof(int)); \
721 line_state_invisible->wrapped_line[newlines] = _rl_wrapped_multicolumn; \
726 #define CHECK_LPOS() \
729 if (lpos >= _rl_screenwidth) \
731 if (newlines >= (inv_lbsize - 2)) \
734 inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
736 inv_lbreaks[++newlines] = out; \
742 /* inv_lbreaks[i] is where line i starts in the buffer. */
743 inv_lbreaks
[newlines
= 0] = 0;
744 /* lpos is a physical cursor position, so it needs to be adjusted by the
745 number of invisible characters in the prompt, per line */
746 lpos
= prompt_physical_chars
+ modmark
;
748 #if defined (HANDLE_MULTIBYTE)
749 memset (line_state_invisible
->wrapped_line
, 0, line_state_invisible
->wbsize
* sizeof (int));
753 /* prompt_invis_chars_first_line is the number of invisible characters in
754 the first physical line of the prompt.
755 wrap_offset - prompt_invis_chars_first_line is the number of invis
756 chars on the second (or, more generally, last) line. */
758 /* This is zero-based, used to set the newlines */
759 prompt_lines_estimate
= lpos
/ _rl_screenwidth
;
761 /* what if lpos is already >= _rl_screenwidth before we start drawing the
762 contents of the command line? */
763 while (lpos
>= _rl_screenwidth
)
766 int nocorrect
, wadjust
;
769 /* Adjust depending on the invisible characters in the line. We use a
770 heuristic based on experience: invisible characters nearly always
771 appear in the first and last lines of the prompt */
772 wadjust
= (newlines
== 0)
773 ? prompt_invis_chars_first_line
774 : ((newlines
== prompt_lines_estimate
) ? wrap_offset
: prompt_invis_chars_first_line
);
776 /* fix from Darin Johnson <darin@acuson.com> for prompt string with
777 invisible characters that is longer than the screen width. The
778 prompt_invis_chars_first_line variable could be made into an array
779 saying how many invisible characters there are per line, but that's
780 probably too much work for the benefit gained. How many people have
781 prompts that exceed two physical lines?
782 Additional logic fix from Edward Catmur <ed@catmur.co.uk> */
783 #if defined (HANDLE_MULTIBYTE)
784 if (mb_cur_max
> 1 && rl_byte_oriented
== 0 && prompt_multibyte_chars
> 0)
788 temp
= local_prompt_len
;
791 /* This has to take invisible characters in the prompt into
793 z
= _rl_col_width (local_prompt
, n0
, num
, 1) - wadjust
;
794 if (z
> _rl_screenwidth
)
796 num
= _rl_find_prev_mbchar (local_prompt
, num
, MB_FIND_ANY
);
799 else if (z
== _rl_screenwidth
)
801 /* If we are in the middle or at the end of a multibyte
802 character, we want to move to the start, then find out
803 where it ends so we know where to insert the newline.
804 If this isn't a multibyte character, its the same as num++ */
805 p
= _rl_find_prev_mbchar (local_prompt
, num
, MB_FIND_ANY
);
806 num
= _rl_find_next_mbchar (local_prompt
, p
, 1, MB_FIND_ANY
);
814 #endif /* !HANDLE_MULTIBYTE */
815 temp
= ((newlines
+ 1) * _rl_screenwidth
);
817 /* Now account for invisible characters in the current line. */
818 /* XXX - this assumes that the invisible characters may be split, but only
819 between the first and the last lines. */
823 inv_lbreaks
[++newlines
] = temp
;
824 #if defined (HANDLE_MULTIBYTE)
825 /* lpos is a physical cursor position, so it needs to take the invisible
826 characters into account. */
827 if (mb_cur_max
> 1 && rl_byte_oriented
== 0 && prompt_multibyte_chars
> 0)
828 lpos
-= _rl_col_width (local_prompt
, n0
, num
, 1) - wadjust
;
831 lpos
-= _rl_screenwidth
; /* all physical cursor positions */
834 prompt_last_screen_line
= newlines
;
836 /* Draw the rest of the line (after the prompt) into invisible_line, keeping
837 track of where the cursor is (cpos_buffer_position), the number of the line containing
838 the cursor (lb_linenum), the last line number (lb_botlin and inv_botlin).
839 It maintains an array of line breaks for display (inv_lbreaks).
840 This handles expanding tabs for display and displaying meta characters. */
842 #if defined (HANDLE_MULTIBYTE)
844 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
846 memset (&ps
, 0, sizeof (mbstate_t));
847 /* XXX - what if wc_bytes ends up <= 0? check for MB_INVALIDCH */
848 wc_bytes
= mbrtowc (&wc
, rl_line_buffer
, rl_end
, &ps
);
854 for (in
= 0; in
< rl_end
; in
++)
857 c
= (unsigned char)rl_line_buffer
[in
];
859 #if defined (HANDLE_MULTIBYTE)
860 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
862 if (MB_INVALIDCH (wc_bytes
))
864 /* Byte sequence is invalid or shortened. Assume that the
865 first byte represents a character. */
867 /* Assume that a character occupies a single column. */
869 memset (&ps
, 0, sizeof (mbstate_t));
871 else if (MB_NULLWCH (wc_bytes
))
872 break; /* Found '\0' */
876 wc_width
= (temp
>= 0) ? temp
: 1;
881 if (out
+ 8 >= line_size
) /* XXX - 8 for \t */
884 visible_line
= (char *)xrealloc (visible_line
, line_size
);
885 invisible_line
= (char *)xrealloc (invisible_line
, line_size
);
886 line
= invisible_line
;
891 cpos_buffer_position
= out
;
892 lb_linenum
= newlines
;
895 #if defined (HANDLE_MULTIBYTE)
896 if (META_CHAR (c
) && _rl_output_meta_chars
== 0) /* XXX - clean up */
901 if (_rl_output_meta_chars
== 0)
903 sprintf (line
+ out
, "\\%o", c
);
905 if (lpos
+ 4 >= _rl_screenwidth
)
907 temp
= _rl_screenwidth
- lpos
;
908 CHECK_INV_LBREAKS ();
909 inv_lbreaks
[++newlines
] = out
+ temp
;
923 #if defined (DISPLAY_TABS)
929 newout
= (out
| (int)7) + 1;
931 newout
= out
+ 8 - lpos
% 8;
934 if (lpos
+ temp
>= _rl_screenwidth
)
937 temp2
= _rl_screenwidth
- lpos
;
938 CHECK_INV_LBREAKS ();
939 inv_lbreaks
[++newlines
] = out
+ temp2
;
952 else if (c
== '\n' && _rl_horizontal_scroll_mode
== 0 && _rl_term_up
&& *_rl_term_up
)
954 line
[out
++] = '\0'; /* XXX - sentinel */
955 CHECK_INV_LBREAKS ();
956 inv_lbreaks
[++newlines
] = out
;
959 else if (CTRL_CHAR (c
) || c
== RUBOUT
)
963 line
[out
++] = CTRL_CHAR (c
) ? UNCTRL (c
) : '?';
968 #if defined (HANDLE_MULTIBYTE)
969 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
973 _rl_wrapped_multicolumn
= 0;
975 if (_rl_screenwidth
< lpos
+ wc_width
)
976 for (i
= lpos
; i
< _rl_screenwidth
; i
++)
978 /* The space will be removed in update_line() */
980 _rl_wrapped_multicolumn
++;
985 cpos_buffer_position
= out
;
986 lb_linenum
= newlines
;
988 for (i
= in
; i
< in
+wc_bytes
; i
++)
989 line
[out
++] = rl_line_buffer
[i
];
990 for (i
= 0; i
< wc_width
; i
++)
1004 #if defined (HANDLE_MULTIBYTE)
1005 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
1008 /* XXX - what if wc_bytes ends up <= 0? check for MB_INVALIDCH */
1009 wc_bytes
= mbrtowc (&wc
, rl_line_buffer
+ in
, rl_end
- in
, &ps
);
1017 if (cpos_buffer_position
< 0)
1019 cpos_buffer_position
= out
;
1020 lb_linenum
= newlines
;
1023 inv_botlin
= lb_botlin
= _rl_inv_botlin
= newlines
;
1024 CHECK_INV_LBREAKS ();
1025 inv_lbreaks
[newlines
+1] = out
;
1026 cursor_linenum
= lb_linenum
;
1028 /* CPOS_BUFFER_POSITION == position in buffer where cursor should be placed.
1029 CURSOR_LINENUM == line number where the cursor should be placed. */
1031 /* PWP: now is when things get a bit hairy. The visible and invisible
1032 line buffers are really multiple lines, which would wrap every
1033 (screenwidth - 1) characters. Go through each in turn, finding
1034 the changed region and updating it. The line order is top to bottom. */
1036 /* If we can move the cursor up and down, then use multiple lines,
1037 otherwise, let long lines display in a single terminal line, and
1038 horizontally scroll it. */
1039 displaying_prompt_first_line
= 1;
1040 if (_rl_horizontal_scroll_mode
== 0 && _rl_term_up
&& *_rl_term_up
)
1042 int nleft
, pos
, changed_screen_line
, tx
;
1044 if (!rl_display_fixed
|| forced_display
)
1048 /* If we have more than a screenful of material to display, then
1049 only display a screenful. We should display the last screen,
1051 if (out
>= _rl_screenchars
)
1053 #if defined (HANDLE_MULTIBYTE)
1054 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
1055 out
= _rl_find_prev_mbchar (line
, _rl_screenchars
, MB_FIND_ANY
);
1058 out
= _rl_screenchars
- 1;
1061 /* The first line is at character position 0 in the buffer. The
1062 second and subsequent lines start at inv_lbreaks[N], offset by
1063 OFFSET (which has already been calculated above). */
1065 #define INVIS_FIRST() (prompt_physical_chars > _rl_screenwidth ? prompt_invis_chars_first_line : wrap_offset)
1066 #define WRAP_OFFSET(line, offset) ((line == 0) \
1067 ? (offset ? INVIS_FIRST() : 0) \
1068 : ((line == prompt_last_screen_line) ? wrap_offset-prompt_invis_chars_first_line : 0))
1069 #define W_OFFSET(line, offset) ((line) == 0 ? offset : 0)
1070 #define VIS_LLEN(l) ((l) > _rl_vis_botlin ? 0 : (vis_lbreaks[l+1] - vis_lbreaks[l]))
1071 #define INV_LLEN(l) (inv_lbreaks[l+1] - inv_lbreaks[l])
1072 #define VIS_CHARS(line) (visible_line + vis_lbreaks[line])
1073 #define VIS_LINE(line) ((line) > _rl_vis_botlin) ? "" : VIS_CHARS(line)
1074 #define INV_LINE(line) (invisible_line + inv_lbreaks[line])
1076 #define OLD_CPOS_IN_PROMPT() (cpos_adjusted == 0 && \
1077 _rl_last_c_pos != o_cpos && \
1078 _rl_last_c_pos > wrap_offset && \
1079 o_cpos < prompt_last_invisible)
1081 /* For each line in the buffer, do the updating display. */
1082 for (linenum
= 0; linenum
<= inv_botlin
; linenum
++)
1084 /* This can lead us astray if we execute a program that changes
1085 the locale from a non-multibyte to a multibyte one. */
1086 o_cpos
= _rl_last_c_pos
;
1088 update_line (VIS_LINE(linenum
), INV_LINE(linenum
), linenum
,
1089 VIS_LLEN(linenum
), INV_LLEN(linenum
), inv_botlin
);
1091 /* update_line potentially changes _rl_last_c_pos, but doesn't
1092 take invisible characters into account, since _rl_last_c_pos
1093 is an absolute cursor position in a multibyte locale. See
1094 if compensating here is the right thing, or if we have to
1095 change update_line itself. There are several cases in which
1096 update_line adjusts _rl_last_c_pos itself (so it can pass
1097 _rl_move_cursor_relative accurate values); it communicates
1098 this back by setting cpos_adjusted. If we assume that
1099 _rl_last_c_pos is correct (an absolute cursor position) each
1100 time update_line is called, then we can assume in our
1101 calculations that o_cpos does not need to be adjusted by
1103 if (linenum
== 0 && (mb_cur_max
> 1 && rl_byte_oriented
== 0) && OLD_CPOS_IN_PROMPT())
1104 _rl_last_c_pos
-= prompt_invis_chars_first_line
; /* XXX - was wrap_offset */
1105 else if (linenum
== prompt_last_screen_line
&& prompt_physical_chars
> _rl_screenwidth
&&
1106 (mb_cur_max
> 1 && rl_byte_oriented
== 0) &&
1107 cpos_adjusted
== 0 &&
1108 _rl_last_c_pos
!= o_cpos
&&
1109 _rl_last_c_pos
> (prompt_last_invisible
- _rl_screenwidth
- prompt_invis_chars_first_line
))
1110 _rl_last_c_pos
-= (wrap_offset
-prompt_invis_chars_first_line
);
1112 /* If this is the line with the prompt, we might need to
1113 compensate for invisible characters in the new line. Do
1114 this only if there is not more than one new line (which
1115 implies that we completely overwrite the old visible line)
1116 and the new line is shorter than the old. Make sure we are
1117 at the end of the new line before clearing. */
1119 inv_botlin
== 0 && _rl_last_c_pos
== out
&&
1120 (wrap_offset
> visible_wrap_offset
) &&
1121 (_rl_last_c_pos
< visible_first_line_len
))
1123 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
1124 nleft
= _rl_screenwidth
- _rl_last_c_pos
;
1126 nleft
= _rl_screenwidth
+ wrap_offset
- _rl_last_c_pos
;
1128 _rl_clear_to_eol (nleft
);
1131 /* This segment is intended to handle the case where the prompt
1132 has invisible characters on the second line and the new line
1133 to be displayed needs to clear the rest of the old characters
1134 out (e.g., when printing the i-search prompt). In general,
1135 the case of the new line being shorter than the old.
1137 else if (linenum
== prompt_last_screen_line
&&
1138 prompt_physical_chars
> _rl_screenwidth
&&
1139 wrap_offset
!= prompt_invis_chars_first_line
&&
1140 _rl_last_c_pos
== out
&&
1144 /* Since the new first line is now visible, save its length. */
1146 visible_first_line_len
= (inv_botlin
> 0) ? inv_lbreaks
[1] : out
- wrap_offset
;
1149 /* We may have deleted some lines. If so, clear the left over
1150 blank ones at the bottom out. */
1151 if (_rl_vis_botlin
> inv_botlin
)
1154 for (; linenum
<= _rl_vis_botlin
; linenum
++)
1156 tt
= VIS_CHARS (linenum
);
1157 _rl_move_vert (linenum
);
1158 _rl_move_cursor_relative (0, tt
);
1160 ((linenum
== _rl_vis_botlin
) ? strlen (tt
) : _rl_screenwidth
);
1163 _rl_vis_botlin
= inv_botlin
;
1165 /* CHANGED_SCREEN_LINE is set to 1 if we have moved to a
1166 different screen line during this redisplay. */
1167 changed_screen_line
= _rl_last_v_pos
!= cursor_linenum
;
1168 if (changed_screen_line
)
1170 _rl_move_vert (cursor_linenum
);
1171 /* If we moved up to the line with the prompt using _rl_term_up,
1172 the physical cursor position on the screen stays the same,
1173 but the buffer position needs to be adjusted to account
1174 for invisible characters. */
1175 if ((mb_cur_max
== 1 || rl_byte_oriented
) && cursor_linenum
== 0 && wrap_offset
)
1176 _rl_last_c_pos
+= wrap_offset
;
1179 /* We have to reprint the prompt if it contains invisible
1180 characters, since it's not generally OK to just reprint
1181 the characters from the current cursor position. But we
1182 only need to reprint it if the cursor is before the last
1183 invisible character in the prompt string. */
1184 nleft
= prompt_visible_length
+ wrap_offset
;
1185 if (cursor_linenum
== 0 && wrap_offset
> 0 && _rl_last_c_pos
> 0 &&
1187 _rl_last_c_pos
<= PROMPT_ENDING_INDEX
&& local_prompt
)
1189 _rl_last_c_pos
< PROMPT_ENDING_INDEX
&& local_prompt
)
1192 #if defined (__MSDOS__)
1193 putc ('\r', rl_outstream
);
1196 tputs (_rl_term_cr
, 1, _rl_output_character_function
);
1199 _rl_output_some_chars ("*", 1);
1201 _rl_output_some_chars (local_prompt
, nleft
);
1202 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
1203 _rl_last_c_pos
= _rl_col_width (local_prompt
, 0, nleft
, 1) - wrap_offset
+ modmark
;
1205 _rl_last_c_pos
= nleft
+ modmark
;
1208 /* Where on that line? And where does that line start
1210 pos
= inv_lbreaks
[cursor_linenum
];
1211 /* nleft == number of characters in the line buffer between the
1212 start of the line and the desired cursor position. */
1213 nleft
= cpos_buffer_position
- pos
;
1215 /* NLEFT is now a number of characters in a buffer. When in a
1216 multibyte locale, however, _rl_last_c_pos is an absolute cursor
1217 position that doesn't take invisible characters in the prompt
1218 into account. We use a fudge factor to compensate. */
1220 /* Since _rl_backspace() doesn't know about invisible characters in the
1221 prompt, and there's no good way to tell it, we compensate for
1222 those characters here and call _rl_backspace() directly. */
1223 if (wrap_offset
&& cursor_linenum
== 0 && nleft
< _rl_last_c_pos
)
1225 /* TX == new physical cursor position in multibyte locale. */
1226 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
1227 tx
= _rl_col_width (&visible_line
[pos
], 0, nleft
, 1) - visible_wrap_offset
;
1230 if (tx
>= 0 && _rl_last_c_pos
> tx
)
1232 _rl_backspace (_rl_last_c_pos
- tx
); /* XXX */
1233 _rl_last_c_pos
= tx
;
1237 /* We need to note that in a multibyte locale we are dealing with
1238 _rl_last_c_pos as an absolute cursor position, but moving to a
1239 point specified by a buffer position (NLEFT) that doesn't take
1240 invisible characters into account. */
1241 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
1242 _rl_move_cursor_relative (nleft
, &invisible_line
[pos
]);
1243 else if (nleft
!= _rl_last_c_pos
)
1244 _rl_move_cursor_relative (nleft
, &invisible_line
[pos
]);
1247 else /* Do horizontal scrolling. */
1249 #define M_OFFSET(margin, offset) ((margin) == 0 ? offset : 0)
1250 int lmargin
, ndisp
, nleft
, phys_c_pos
, t
;
1252 /* Always at top line. */
1255 /* Compute where in the buffer the displayed line should start. This
1258 /* The number of characters that will be displayed before the cursor. */
1259 ndisp
= cpos_buffer_position
- wrap_offset
;
1260 nleft
= prompt_visible_length
+ wrap_offset
;
1261 /* Where the new cursor position will be on the screen. This can be
1262 longer than SCREENWIDTH; if it is, lmargin will be adjusted. */
1263 phys_c_pos
= cpos_buffer_position
- (last_lmargin
? last_lmargin
: wrap_offset
);
1264 t
= _rl_screenwidth
/ 3;
1266 /* If the number of characters had already exceeded the screenwidth,
1267 last_lmargin will be > 0. */
1269 /* If the number of characters to be displayed is more than the screen
1270 width, compute the starting offset so that the cursor is about
1271 two-thirds of the way across the screen. */
1272 if (phys_c_pos
> _rl_screenwidth
- 2)
1274 lmargin
= cpos_buffer_position
- (2 * t
);
1277 /* If the left margin would be in the middle of a prompt with
1278 invisible characters, don't display the prompt at all. */
1279 if (wrap_offset
&& lmargin
> 0 && lmargin
< nleft
)
1282 else if (ndisp
< _rl_screenwidth
- 2) /* XXX - was -1 */
1284 else if (phys_c_pos
< 1)
1286 /* If we are moving back towards the beginning of the line and
1287 the last margin is no longer correct, compute a new one. */
1288 lmargin
= ((cpos_buffer_position
- 1) / t
) * t
; /* XXX */
1289 if (wrap_offset
&& lmargin
> 0 && lmargin
< nleft
)
1293 lmargin
= last_lmargin
;
1295 displaying_prompt_first_line
= lmargin
< nleft
;
1297 /* If the first character on the screen isn't the first character
1298 in the display line, indicate this with a special character. */
1300 line
[lmargin
] = '<';
1302 /* If SCREENWIDTH characters starting at LMARGIN do not encompass
1303 the whole line, indicate that with a special character at the
1304 right edge of the screen. If LMARGIN is 0, we need to take the
1305 wrap offset into account. */
1306 t
= lmargin
+ M_OFFSET (lmargin
, wrap_offset
) + _rl_screenwidth
;
1310 if (rl_display_fixed
== 0 || forced_display
|| lmargin
!= last_lmargin
)
1313 o_cpos
= _rl_last_c_pos
;
1315 update_line (&visible_line
[last_lmargin
],
1316 &invisible_line
[lmargin
],
1318 _rl_screenwidth
+ visible_wrap_offset
,
1319 _rl_screenwidth
+ (lmargin
? 0 : wrap_offset
),
1322 if ((mb_cur_max
> 1 && rl_byte_oriented
== 0) &&
1323 displaying_prompt_first_line
&& OLD_CPOS_IN_PROMPT())
1324 _rl_last_c_pos
-= prompt_invis_chars_first_line
; /* XXX - was wrap_offset */
1326 /* If the visible new line is shorter than the old, but the number
1327 of invisible characters is greater, and we are at the end of
1328 the new line, we need to clear to eol. */
1329 t
= _rl_last_c_pos
- M_OFFSET (lmargin
, wrap_offset
);
1330 if ((M_OFFSET (lmargin
, wrap_offset
) > visible_wrap_offset
) &&
1331 (_rl_last_c_pos
== out
) && displaying_prompt_first_line
&&
1332 t
< visible_first_line_len
)
1334 nleft
= _rl_screenwidth
- t
;
1335 _rl_clear_to_eol (nleft
);
1337 visible_first_line_len
= out
- lmargin
- M_OFFSET (lmargin
, wrap_offset
);
1338 if (visible_first_line_len
> _rl_screenwidth
)
1339 visible_first_line_len
= _rl_screenwidth
;
1341 _rl_move_cursor_relative (cpos_buffer_position
- lmargin
, &invisible_line
[lmargin
]);
1342 last_lmargin
= lmargin
;
1345 fflush (rl_outstream
);
1347 /* Swap visible and non-visible lines. */
1349 struct line_state
*vtemp
= line_state_visible
;
1351 line_state_visible
= line_state_invisible
;
1352 line_state_invisible
= vtemp
;
1354 rl_display_fixed
= 0;
1355 /* If we are displaying on a single line, and last_lmargin is > 0, we
1356 are not displaying any invisible characters, so set visible_wrap_offset
1358 if (_rl_horizontal_scroll_mode
&& last_lmargin
)
1359 visible_wrap_offset
= 0;
1361 visible_wrap_offset
= wrap_offset
;
1364 RL_UNSETSTATE (RL_STATE_REDISPLAYING
);
1365 _rl_release_sigint ();
1368 /* PWP: update_line() is based on finding the middle difference of each
1369 line on the screen; vis:
1371 /old first difference
1372 /beginning of line | /old last same /old EOL
1374 old: eddie> Oh, my little gruntle-buggy is to me, as lurgid as
1375 new: eddie> Oh, my little buggy says to me, as lurgid as
1377 \beginning of line | \new last same \new end of line
1378 \new first difference
1380 All are character pointers for the sake of speed. Special cases for
1381 no differences, as well as for end of line additions must be handled.
1383 Could be made even smarter, but this works well enough */
1385 update_line (old
, new, current_line
, omax
, nmax
, inv_botlin
)
1386 register char *old
, *new;
1387 int current_line
, omax
, nmax
, inv_botlin
;
1389 register char *ofd
, *ols
, *oe
, *nfd
, *nls
, *ne
;
1390 int temp
, lendiff
, wsatend
, od
, nd
, twidth
, o_cpos
;
1391 int current_invis_chars
;
1392 int col_lendiff
, col_temp
;
1393 int bytes_to_insert
;
1394 int mb_cur_max
= MB_CUR_MAX
;
1395 #if defined (HANDLE_MULTIBYTE)
1396 mbstate_t ps_new
, ps_old
;
1397 int new_offset
, old_offset
;
1400 /* If we're at the right edge of a terminal that supports xn, we're
1401 ready to wrap around, so do so. This fixes problems with knowing
1402 the exact cursor position and cut-and-paste with certain terminal
1403 emulators. In this calculation, TEMP is the physical screen
1404 position of the cursor. */
1405 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
1406 temp
= _rl_last_c_pos
;
1408 temp
= _rl_last_c_pos
- WRAP_OFFSET (_rl_last_v_pos
, visible_wrap_offset
);
1409 if (temp
== _rl_screenwidth
&& _rl_term_autowrap
&& !_rl_horizontal_scroll_mode
1410 && _rl_last_v_pos
== current_line
- 1)
1412 #if defined (HANDLE_MULTIBYTE)
1413 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
1417 int tempwidth
, bytes
;
1420 /* This fixes only double-column characters, but if the wrapped
1421 character consumes more than three columns, spaces will be
1422 inserted in the string buffer. */
1423 if (current_line
< line_state_visible
->wbsize
&& line_state_visible
->wrapped_line
[current_line
] > 0)
1424 _rl_clear_to_eol (line_state_visible
->wrapped_line
[current_line
]);
1426 memset (&ps
, 0, sizeof (mbstate_t));
1427 ret
= mbrtowc (&wc
, new, mb_cur_max
, &ps
);
1428 if (MB_INVALIDCH (ret
))
1433 else if (MB_NULLWCH (ret
))
1436 tempwidth
= WCWIDTH (wc
);
1442 for (count
= 0; count
< bytes
; count
++)
1443 putc (new[count
], rl_outstream
);
1444 _rl_last_c_pos
= tempwidth
;
1446 memset (&ps
, 0, sizeof (mbstate_t));
1447 ret
= mbrtowc (&wc
, old
, mb_cur_max
, &ps
);
1448 if (ret
!= 0 && bytes
!= 0)
1450 if (MB_INVALIDCH (ret
))
1452 memmove (old
+bytes
, old
+ret
, strlen (old
+ret
));
1453 memcpy (old
, new, bytes
);
1454 /* Fix up indices if we copy data from one line to another */
1455 omax
+= bytes
- ret
;
1456 for (i
= current_line
+1; i
<= inv_botlin
+1; i
++)
1457 vis_lbreaks
[i
] += bytes
- ret
;
1462 putc (' ', rl_outstream
);
1465 if (old
[0] && new[0])
1473 putc (new[0], rl_outstream
);
1475 putc (' ', rl_outstream
);
1478 if (old
[0] && new[0])
1484 /* Find first difference. */
1485 #if defined (HANDLE_MULTIBYTE)
1486 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
1488 /* See if the old line is a subset of the new line, so that the
1489 only change is adding characters. */
1490 temp
= (omax
< nmax
) ? omax
: nmax
;
1491 if (memcmp (old
, new, temp
) == 0) /* adding at the end */
1493 new_offset
= old_offset
= temp
;
1499 memset (&ps_new
, 0, sizeof(mbstate_t));
1500 memset (&ps_old
, 0, sizeof(mbstate_t));
1502 if (omax
== nmax
&& STREQN (new, old
, omax
))
1511 new_offset
= old_offset
= 0;
1512 for (ofd
= old
, nfd
= new;
1513 (ofd
- old
< omax
) && *ofd
&&
1514 _rl_compare_chars(old
, old_offset
, &ps_old
, new, new_offset
, &ps_new
); )
1516 old_offset
= _rl_find_next_mbchar (old
, old_offset
, 1, MB_FIND_ANY
);
1517 new_offset
= _rl_find_next_mbchar (new, new_offset
, 1, MB_FIND_ANY
);
1519 ofd
= old
+ old_offset
;
1520 nfd
= new + new_offset
;
1527 for (ofd
= old
, nfd
= new;
1528 (ofd
- old
< omax
) && *ofd
&& (*ofd
== *nfd
);
1532 /* Move to the end of the screen line. ND and OD are used to keep track
1533 of the distance between ne and new and oe and old, respectively, to
1534 move a subtraction out of each loop. */
1535 for (od
= ofd
- old
, oe
= ofd
; od
< omax
&& *oe
; oe
++, od
++);
1536 for (nd
= nfd
- new, ne
= nfd
; nd
< nmax
&& *ne
; ne
++, nd
++);
1538 /* If no difference, continue to next line. */
1539 if (ofd
== oe
&& nfd
== ne
)
1542 #if defined (HANDLE_MULTIBYTE)
1543 if (mb_cur_max
> 1 && rl_byte_oriented
== 0 && _rl_utf8locale
)
1546 mbstate_t ps
= { 0 };
1549 /* If the first character in the difference is a zero-width character,
1550 assume it's a combining character and back one up so the two base
1551 characters no longer compare equivalently. */
1552 t
= mbrtowc (&wc
, ofd
, mb_cur_max
, &ps
);
1553 if (t
> 0 && UNICODE_COMBINING_CHAR (wc
) && WCWIDTH (wc
) == 0)
1555 old_offset
= _rl_find_prev_mbchar (old
, ofd
- old
, MB_FIND_ANY
);
1556 new_offset
= _rl_find_prev_mbchar (new, nfd
- new, MB_FIND_ANY
);
1557 ofd
= old
+ old_offset
; /* equal by definition */
1558 nfd
= new + new_offset
;
1563 wsatend
= 1; /* flag for trailing whitespace */
1565 #if defined (HANDLE_MULTIBYTE)
1566 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
1568 ols
= old
+ _rl_find_prev_mbchar (old
, oe
- old
, MB_FIND_ANY
);
1569 nls
= new + _rl_find_prev_mbchar (new, ne
- new, MB_FIND_ANY
);
1571 while ((ols
> ofd
) && (nls
> nfd
))
1573 memset (&ps_old
, 0, sizeof (mbstate_t));
1574 memset (&ps_new
, 0, sizeof (mbstate_t));
1577 /* On advice from jir@yamato.ibm.com */
1578 _rl_adjust_point (old
, ols
- old
, &ps_old
);
1579 _rl_adjust_point (new, nls
- new, &ps_new
);
1582 if (_rl_compare_chars (old
, ols
- old
, &ps_old
, new, nls
- new, &ps_new
) == 0)
1588 ols
= old
+ _rl_find_prev_mbchar (old
, ols
- old
, MB_FIND_ANY
);
1589 nls
= new + _rl_find_prev_mbchar (new, nls
- new, MB_FIND_ANY
);
1594 #endif /* HANDLE_MULTIBYTE */
1595 ols
= oe
- 1; /* find last same */
1597 while ((ols
> ofd
) && (nls
> nfd
) && (*ols
== *nls
))
1604 #if defined (HANDLE_MULTIBYTE)
1613 #if defined (HANDLE_MULTIBYTE)
1614 /* This may not work for stateful encoding, but who cares? To handle
1615 stateful encoding properly, we have to scan each string from the
1616 beginning and compare. */
1617 else if (_rl_compare_chars (ols
, 0, NULL
, nls
, 0, NULL
) == 0)
1619 else if (*ols
!= *nls
)
1622 if (*ols
) /* don't step past the NUL */
1624 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
1625 ols
= old
+ _rl_find_next_mbchar (old
, ols
- old
, 1, MB_FIND_ANY
);
1631 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
1632 nls
= new + _rl_find_next_mbchar (new, nls
- new, 1, MB_FIND_ANY
);
1638 /* count of invisible characters in the current invisible line. */
1639 current_invis_chars
= W_OFFSET (current_line
, wrap_offset
);
1640 if (_rl_last_v_pos
!= current_line
)
1642 _rl_move_vert (current_line
);
1643 /* We have moved up to a new screen line. This line may or may not have
1644 invisible characters on it, but we do our best to recalculate
1645 visible_wrap_offset based on what we know. */
1646 if (current_line
== 0)
1647 visible_wrap_offset
= prompt_invis_chars_first_line
; /* XXX */
1648 if ((mb_cur_max
== 1 || rl_byte_oriented
) && current_line
== 0 && visible_wrap_offset
)
1649 _rl_last_c_pos
+= visible_wrap_offset
;
1652 /* If this is the first line and there are invisible characters in the
1653 prompt string, and the prompt string has not changed, and the current
1654 cursor position is before the last invisible character in the prompt,
1655 and the index of the character to move to is past the end of the prompt
1656 string, then redraw the entire prompt string. We can only do this
1657 reliably if the terminal supports a `cr' capability.
1659 This can also happen if the prompt string has changed, and the first
1660 difference in the line is in the middle of the prompt string, after a
1661 sequence of invisible characters (worst case) and before the end of
1662 the prompt. In this case, we have to redraw the entire prompt string
1663 so that the entire sequence of invisible characters is drawn. We need
1664 to handle the worst case, when the difference is after (or in the middle
1665 of) a sequence of invisible characters that changes the text color and
1666 before the sequence that restores the text color to normal. Then we have
1667 to make sure that the lines still differ -- if they don't, we can
1670 This is not an efficiency hack -- there is a problem with redrawing
1671 portions of the prompt string if they contain terminal escape
1672 sequences (like drawing the `unbold' sequence without a corresponding
1673 `bold') that manifests itself on certain terminals. */
1675 lendiff
= local_prompt_len
;
1678 od
= ofd
- old
; /* index of first difference in visible line */
1679 nd
= nfd
- new; /* nd, od are buffer indexes */
1680 if (current_line
== 0 && !_rl_horizontal_scroll_mode
&&
1681 _rl_term_cr
&& lendiff
> prompt_visible_length
&& _rl_last_c_pos
> 0 &&
1682 (((od
> 0 || nd
> 0) && (od
<= prompt_last_invisible
|| nd
<= prompt_last_invisible
)) ||
1683 ((od
>= lendiff
) && _rl_last_c_pos
< PROMPT_ENDING_INDEX
)))
1685 #if defined (__MSDOS__)
1686 putc ('\r', rl_outstream
);
1688 tputs (_rl_term_cr
, 1, _rl_output_character_function
);
1691 _rl_output_some_chars ("*", 1);
1692 _rl_output_some_chars (local_prompt
, lendiff
);
1693 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
1695 /* We take wrap_offset into account here so we can pass correct
1696 information to _rl_move_cursor_relative. */
1697 _rl_last_c_pos
= _rl_col_width (local_prompt
, 0, lendiff
, 1) - wrap_offset
+ modmark
;
1701 _rl_last_c_pos
= lendiff
+ modmark
;
1703 /* Now if we have printed the prompt string because the first difference
1704 was within the prompt, see if we need to recompute where the lines
1705 differ. Check whether where we are now is past the last place where
1706 the old and new lines are the same and short-circuit now if we are. */
1707 if ((od
<= prompt_last_invisible
|| nd
<= prompt_last_invisible
) &&
1709 lendiff
> (ols
-old
) && lendiff
> (nls
-new))
1712 /* XXX - we need to fix up our calculations if we are now past the
1713 old ofd/nfd and the prompt length (or line length) has changed.
1714 We punt on the problem and do a dumb update. We'd like to be able
1715 to just output the prompt from the beginning of the line up to the
1716 first difference, but you don't know the number of invisible
1717 characters in that case.
1718 This needs a lot of work to be efficient. */
1719 if ((od
<= prompt_last_invisible
|| nd
<= prompt_last_invisible
))
1721 nfd
= new + lendiff
; /* number of characters we output above */
1724 /* Do a dumb update and return */
1728 _rl_output_some_chars (nfd
, temp
);
1729 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
1730 _rl_last_c_pos
+= _rl_col_width (new, nd
, ne
- new, 1);
1732 _rl_last_c_pos
+= temp
;
1735 goto clear_rest_of_line
; /* XXX */
1741 o_cpos
= _rl_last_c_pos
;
1743 /* When this function returns, _rl_last_c_pos is correct, and an absolute
1744 cursor position in multibyte mode, but a buffer index when not in a
1745 multibyte locale. */
1746 _rl_move_cursor_relative (od
, old
);
1748 #if defined (HANDLE_MULTIBYTE)
1749 /* We need to indicate that the cursor position is correct in the presence of
1750 invisible characters in the prompt string. Let's see if setting this when
1751 we make sure we're at the end of the drawn prompt string works. */
1752 if (current_line
== 0 && mb_cur_max
> 1 && rl_byte_oriented
== 0 &&
1753 (_rl_last_c_pos
> 0 || o_cpos
> 0) &&
1754 _rl_last_c_pos
== prompt_physical_chars
)
1758 /* if (len (new) > len (old))
1759 lendiff == difference in buffer (bytes)
1760 col_lendiff == difference on screen (columns)
1761 When not using multibyte characters, these are equal */
1762 lendiff
= (nls
- nfd
) - (ols
- ofd
);
1763 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
1764 col_lendiff
= _rl_col_width (new, nfd
- new, nls
- new, 1) - _rl_col_width (old
, ofd
- old
, ols
- old
, 1);
1766 col_lendiff
= lendiff
;
1768 /* If we are changing the number of invisible characters in a line, and
1769 the spot of first difference is before the end of the invisible chars,
1770 lendiff needs to be adjusted. */
1771 if (current_line
== 0 && /* !_rl_horizontal_scroll_mode && */
1772 current_invis_chars
!= visible_wrap_offset
)
1774 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
1776 lendiff
+= visible_wrap_offset
- current_invis_chars
;
1777 col_lendiff
+= visible_wrap_offset
- current_invis_chars
;
1781 lendiff
+= visible_wrap_offset
- current_invis_chars
;
1782 col_lendiff
= lendiff
;
1786 /* We use temp as a count of the number of bytes from the first difference
1787 to the end of the new line. col_temp is the corresponding number of
1788 screen columns. A `dumb' update moves to the spot of first difference
1789 and writes TEMP bytes. */
1790 /* Insert (diff (len (old), len (new)) ch. */
1792 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
1793 col_temp
= _rl_col_width (new, nfd
- new, ne
- new, 1);
1797 /* how many bytes from the new line buffer to write to the display */
1798 bytes_to_insert
= nls
- nfd
;
1800 /* col_lendiff > 0 if we are adding characters to the line */
1801 if (col_lendiff
> 0) /* XXX - was lendiff */
1803 /* Non-zero if we're increasing the number of lines. */
1804 int gl
= current_line
>= _rl_vis_botlin
&& inv_botlin
> _rl_vis_botlin
;
1805 /* If col_lendiff is > 0, implying that the new string takes up more
1806 screen real estate than the old, but lendiff is < 0, meaning that it
1807 takes fewer bytes, we need to just output the characters starting
1808 from the first difference. These will overwrite what is on the
1809 display, so there's no reason to do a smart update. This can really
1810 only happen in a multibyte environment. */
1813 _rl_output_some_chars (nfd
, temp
);
1814 _rl_last_c_pos
+= col_temp
; /* XXX - was _rl_col_width (nfd, 0, temp, 1); */
1815 /* If nfd begins before any invisible characters in the prompt,
1816 adjust _rl_last_c_pos to account for wrap_offset and set
1817 cpos_adjusted to let the caller know. */
1818 if (current_line
== 0 && displaying_prompt_first_line
&& wrap_offset
&& ((nfd
- new) <= prompt_last_invisible
))
1820 _rl_last_c_pos
-= wrap_offset
;
1825 /* Sometimes it is cheaper to print the characters rather than
1826 use the terminal's capabilities. If we're growing the number
1827 of lines, make sure we actually cause the new line to wrap
1828 around on auto-wrapping terminals. */
1829 else if (_rl_terminal_can_insert
&& ((2 * col_temp
) >= col_lendiff
|| _rl_term_IC
) && (!_rl_term_autowrap
|| !gl
))
1831 /* If lendiff > prompt_visible_length and _rl_last_c_pos == 0 and
1832 _rl_horizontal_scroll_mode == 1, inserting the characters with
1833 _rl_term_IC or _rl_term_ic will screw up the screen because of the
1834 invisible characters. We need to just draw them. */
1835 /* The same thing happens if we're trying to draw before the last
1836 invisible character in the prompt string or we're increasing the
1837 number of invisible characters in the line and we're not drawing
1838 the entire prompt string. */
1839 if (*ols
&& ((_rl_horizontal_scroll_mode
&&
1840 _rl_last_c_pos
== 0 &&
1841 lendiff
> prompt_visible_length
&&
1842 current_invis_chars
> 0) == 0) &&
1843 (((mb_cur_max
> 1 && rl_byte_oriented
== 0) &&
1844 current_line
== 0 && wrap_offset
&&
1845 ((nfd
- new) <= prompt_last_invisible
) &&
1846 (col_lendiff
< prompt_visible_length
)) == 0) &&
1847 (visible_wrap_offset
>= current_invis_chars
))
1849 open_some_spaces (col_lendiff
);
1850 _rl_output_some_chars (nfd
, bytes_to_insert
);
1851 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
1852 _rl_last_c_pos
+= _rl_col_width (nfd
, 0, bytes_to_insert
, 1);
1854 _rl_last_c_pos
+= bytes_to_insert
;
1856 else if ((mb_cur_max
== 1 || rl_byte_oriented
!= 0) && *ols
== 0 && lendiff
> 0)
1858 /* At the end of a line the characters do not have to
1859 be "inserted". They can just be placed on the screen. */
1860 _rl_output_some_chars (nfd
, temp
);
1861 _rl_last_c_pos
+= col_temp
;
1864 else /* just write from first difference to end of new line */
1866 _rl_output_some_chars (nfd
, temp
);
1867 _rl_last_c_pos
+= col_temp
;
1868 /* If nfd begins before the last invisible character in the
1869 prompt, adjust _rl_last_c_pos to account for wrap_offset
1870 and set cpos_adjusted to let the caller know. */
1871 if ((mb_cur_max
> 1 && rl_byte_oriented
== 0) && current_line
== 0 && displaying_prompt_first_line
&& wrap_offset
&& ((nfd
- new) <= prompt_last_invisible
))
1873 _rl_last_c_pos
-= wrap_offset
;
1879 if (bytes_to_insert
> lendiff
)
1881 /* If nfd begins before the last invisible character in the
1882 prompt, adjust _rl_last_c_pos to account for wrap_offset
1883 and set cpos_adjusted to let the caller know. */
1884 if ((mb_cur_max
> 1 && rl_byte_oriented
== 0) && current_line
== 0 && displaying_prompt_first_line
&& wrap_offset
&& ((nfd
- new) <= prompt_last_invisible
))
1886 _rl_last_c_pos
-= wrap_offset
;
1893 /* cannot insert chars, write to EOL */
1894 _rl_output_some_chars (nfd
, temp
);
1895 _rl_last_c_pos
+= col_temp
;
1896 /* If we're in a multibyte locale and were before the last invisible
1897 char in the current line (which implies we just output some invisible
1898 characters) we need to adjust _rl_last_c_pos, since it represents
1899 a physical character position. */
1900 /* The current_line*rl_screenwidth+prompt_invis_chars_first_line is a
1901 crude attempt to compute how far into the new line buffer we are.
1902 It doesn't work well in the face of multibyte characters and needs
1903 to be rethought. XXX */
1904 if ((mb_cur_max
> 1 && rl_byte_oriented
== 0) &&
1905 current_line
== prompt_last_screen_line
&& wrap_offset
&&
1906 displaying_prompt_first_line
&&
1907 wrap_offset
!= prompt_invis_chars_first_line
&&
1908 ((nfd
-new) < (prompt_last_invisible
-(current_line
*_rl_screenwidth
+prompt_invis_chars_first_line
))))
1910 _rl_last_c_pos
-= wrap_offset
- prompt_invis_chars_first_line
;
1915 else /* Delete characters from line. */
1917 /* If possible and inexpensive to use terminal deletion, then do so. */
1918 if (_rl_term_dc
&& (2 * col_temp
) >= -col_lendiff
)
1920 /* If all we're doing is erasing the invisible characters in the
1921 prompt string, don't bother. It screws up the assumptions
1922 about what's on the screen. */
1923 if (_rl_horizontal_scroll_mode
&& _rl_last_c_pos
== 0 &&
1924 displaying_prompt_first_line
&&
1925 -lendiff
== visible_wrap_offset
)
1928 /* If we have moved lmargin and we're shrinking the line, we've
1929 already moved the cursor to the first character of the new line,
1930 so deleting -col_lendiff characters will mess up the cursor
1931 position calculation */
1932 if (_rl_horizontal_scroll_mode
&& displaying_prompt_first_line
== 0 &&
1933 col_lendiff
&& _rl_last_c_pos
< -col_lendiff
)
1937 delete_chars (-col_lendiff
); /* delete (diff) characters */
1939 /* Copy (new) chars to screen from first diff to last match,
1940 overwriting what is there. */
1941 if (bytes_to_insert
> 0)
1943 /* If nfd begins at the prompt, or before the invisible
1944 characters in the prompt, we need to adjust _rl_last_c_pos
1945 in a multibyte locale to account for the wrap offset and
1946 set cpos_adjusted accordingly. */
1947 _rl_output_some_chars (nfd
, bytes_to_insert
);
1948 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
1950 _rl_last_c_pos
+= _rl_col_width (nfd
, 0, bytes_to_insert
, 1);
1951 if (current_line
== 0 && wrap_offset
&&
1952 displaying_prompt_first_line
&&
1953 _rl_last_c_pos
> wrap_offset
&&
1954 ((nfd
- new) <= prompt_last_invisible
))
1956 _rl_last_c_pos
-= wrap_offset
;
1961 _rl_last_c_pos
+= bytes_to_insert
;
1963 /* XXX - we only want to do this if we are at the end of the line
1964 so we move there with _rl_move_cursor_relative */
1965 if (_rl_horizontal_scroll_mode
&& ((oe
-old
) > (ne
-new)))
1967 _rl_move_cursor_relative (ne
-new, new);
1968 goto clear_rest_of_line
;
1972 /* Otherwise, print over the existing material. */
1977 /* If nfd begins at the prompt, or before the invisible
1978 characters in the prompt, we need to adjust _rl_last_c_pos
1979 in a multibyte locale to account for the wrap offset and
1980 set cpos_adjusted accordingly. */
1981 _rl_output_some_chars (nfd
, temp
);
1982 _rl_last_c_pos
+= col_temp
; /* XXX */
1983 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
1985 if (current_line
== 0 && wrap_offset
&&
1986 displaying_prompt_first_line
&&
1987 _rl_last_c_pos
> wrap_offset
&&
1988 ((nfd
- new) <= prompt_last_invisible
))
1990 _rl_last_c_pos
-= wrap_offset
;
1996 lendiff
= (oe
- old
) - (ne
- new);
1997 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
1998 col_lendiff
= _rl_col_width (old
, 0, oe
- old
, 1) - _rl_col_width (new, 0, ne
- new, 1);
2000 col_lendiff
= lendiff
;
2002 /* If we've already printed over the entire width of the screen,
2003 including the old material, then col_lendiff doesn't matter and
2004 space_to_eol will insert too many spaces. XXX - maybe we should
2005 adjust col_lendiff based on the difference between _rl_last_c_pos
2006 and _rl_screenwidth */
2007 if (col_lendiff
&& ((mb_cur_max
== 1 || rl_byte_oriented
) || (_rl_last_c_pos
< _rl_screenwidth
)))
2009 if (_rl_term_autowrap
&& current_line
< inv_botlin
)
2010 space_to_eol (col_lendiff
);
2012 _rl_clear_to_eol (col_lendiff
);
2018 /* Tell the update routines that we have moved onto a new (empty) line. */
2023 visible_line
[0] = '\0';
2025 _rl_last_c_pos
= _rl_last_v_pos
= 0;
2026 _rl_vis_botlin
= last_lmargin
= 0;
2028 vis_lbreaks
[0] = vis_lbreaks
[1] = 0;
2029 visible_wrap_offset
= 0;
2033 /* Clear all screen lines occupied by the current readline line buffer
2036 rl_clear_visible_line ()
2040 /* Make sure we move to column 0 so we clear the entire line */
2041 #if defined (__MSDOS__)
2042 putc ('\r', rl_outstream
);
2044 tputs (_rl_term_cr
, 1, _rl_output_character_function
);
2048 /* Move to the last screen line of the current visible line */
2049 _rl_move_vert (_rl_vis_botlin
);
2051 /* And erase screen lines going up to line 0 (first visible line) */
2052 for (curr_line
= _rl_last_v_pos
; curr_line
>= 0; curr_line
--)
2054 _rl_move_vert (curr_line
);
2055 _rl_clear_to_eol (0);
2061 /* Tell the update routines that we have moved onto a new line with the
2062 prompt already displayed. Code originally from the version of readline
2063 distributed with CLISP. rl_expand_prompt must have already been called
2064 (explicitly or implicitly). This still doesn't work exactly right. */
2066 rl_on_new_line_with_prompt ()
2068 int prompt_size
, i
, l
, real_screenwidth
, newlines
;
2069 char *prompt_last_line
, *lprompt
;
2071 /* Initialize visible_line and invisible_line to ensure that they can hold
2072 the already-displayed prompt. */
2073 prompt_size
= strlen (rl_prompt
) + 1;
2074 init_line_structures (prompt_size
);
2076 /* Make sure the line structures hold the already-displayed prompt for
2078 lprompt
= local_prompt
? local_prompt
: rl_prompt
;
2079 strcpy (visible_line
, lprompt
);
2080 strcpy (invisible_line
, lprompt
);
2082 /* If the prompt contains newlines, take the last tail. */
2083 prompt_last_line
= strrchr (rl_prompt
, '\n');
2084 if (!prompt_last_line
)
2085 prompt_last_line
= rl_prompt
;
2087 l
= strlen (prompt_last_line
);
2088 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
2089 _rl_last_c_pos
= _rl_col_width (prompt_last_line
, 0, l
, 1); /* XXX */
2093 /* Dissect prompt_last_line into screen lines. Note that here we have
2094 to use the real screenwidth. Readline's notion of screenwidth might be
2095 one less, see terminal.c. */
2096 real_screenwidth
= _rl_screenwidth
+ (_rl_term_autowrap
? 0 : 1);
2097 _rl_last_v_pos
= l
/ real_screenwidth
;
2098 /* If the prompt length is a multiple of real_screenwidth, we don't know
2099 whether the cursor is at the end of the last line, or already at the
2100 beginning of the next line. Output a newline just to be safe. */
2101 if (l
> 0 && (l
% real_screenwidth
) == 0)
2102 _rl_output_some_chars ("\n", 1);
2105 newlines
= 0; i
= 0;
2108 _rl_vis_botlin
= newlines
;
2109 vis_lbreaks
[newlines
++] = i
;
2110 i
+= real_screenwidth
;
2112 vis_lbreaks
[newlines
] = l
;
2113 visible_wrap_offset
= 0;
2115 rl_display_prompt
= rl_prompt
; /* XXX - make sure it's set */
2120 /* Actually update the display, period. */
2122 rl_forced_update_display ()
2124 register char *temp
;
2128 temp
= visible_line
;
2134 (*rl_redisplay_function
) ();
2138 /* Redraw only the last line of a multi-line prompt. */
2140 rl_redraw_prompt_last_line ()
2144 t
= strrchr (rl_display_prompt
, '\n');
2146 redraw_prompt (++t
);
2148 rl_forced_update_display ();
2151 /* Move the cursor from _rl_last_c_pos to NEW, which are buffer indices.
2152 (Well, when we don't have multibyte characters, _rl_last_c_pos is a
2154 DATA is the contents of the screen line of interest; i.e., where
2155 the movement is being done.
2156 DATA is always the visible line or the invisible line */
2158 _rl_move_cursor_relative (new, data
)
2163 int woff
; /* number of invisible chars on current line */
2164 int cpos
, dpos
; /* current and desired cursor positions */
2167 int mb_cur_max
= MB_CUR_MAX
;
2169 woff
= WRAP_OFFSET (_rl_last_v_pos
, wrap_offset
);
2170 cpos
= _rl_last_c_pos
;
2172 if (cpos
== 0 && cpos
== new)
2175 #if defined (HANDLE_MULTIBYTE)
2176 /* If we have multibyte characters, NEW is indexed by the buffer point in
2177 a multibyte string, but _rl_last_c_pos is the display position. In
2178 this case, NEW's display position is not obvious and must be
2179 calculated. We need to account for invisible characters in this line,
2180 as long as we are past them and they are counted by _rl_col_width. */
2181 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
2184 /* Try to short-circuit common cases and eliminate a bunch of multibyte
2185 character function calls. */
2186 /* 1. prompt string */
2187 if (new == local_prompt_len
&& memcmp (data
, local_prompt
, new) == 0)
2189 dpos
= prompt_physical_chars
;
2193 /* 2. prompt_string + line contents */
2194 else if (new > local_prompt_len
&& local_prompt
&& memcmp (data
, local_prompt
, local_prompt_len
) == 0)
2196 dpos
= prompt_physical_chars
+ _rl_col_width (data
, local_prompt_len
, new, 1);
2201 dpos
= _rl_col_width (data
, 0, new, 1);
2203 if (displaying_prompt_first_line
== 0)
2206 /* yet another special case: printing the last line of a prompt with
2207 multibyte characters and invisible characters whose printable length
2208 exceeds the screen width with the last invisible character
2209 (prompt_last_invisible) in the last line. IN_INVISLINE is the
2210 offset of DATA in invisible_line */
2212 if (data
> invisible_line
&& data
< invisible_line
+inv_lbreaks
[_rl_inv_botlin
+1])
2213 in_invisline
= data
- invisible_line
;
2215 /* Use NEW when comparing against the last invisible character in the
2216 prompt string, since they're both buffer indices and DPOS is a
2217 desired display position. */
2218 /* NEW is relative to the current displayed line, while
2219 PROMPT_LAST_INVISIBLE is relative to the entire (wrapped) line.
2220 Need a way to reconcile these two variables by turning NEW into a
2221 buffer position relative to the start of the line */
2222 if (adjust
&& ((new > prompt_last_invisible
) || /* XXX - don't use woff here */
2223 (new+in_invisline
> prompt_last_invisible
) || /* invisible line */
2224 (prompt_physical_chars
>= _rl_screenwidth
&& /* visible line */
2225 _rl_last_v_pos
== prompt_last_screen_line
&&
2226 wrap_offset
>= woff
&& dpos
>= woff
&&
2227 new > (prompt_last_invisible
-(vis_lbreaks
[_rl_last_v_pos
])-wrap_offset
))))
2228 /* XXX last comparison might need to be >= */
2231 /* Since this will be assigned to _rl_last_c_pos at the end (more
2232 precisely, _rl_last_c_pos == dpos when this function returns),
2233 let the caller know. */
2241 /* If we don't have to do anything, then return. */
2245 /* It may be faster to output a CR, and then move forwards instead
2246 of moving backwards. */
2247 /* i == current physical cursor position. */
2248 #if defined (HANDLE_MULTIBYTE)
2249 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
2253 i
= _rl_last_c_pos
- woff
;
2254 if (dpos
== 0 || CR_FASTER (dpos
, _rl_last_c_pos
) ||
2255 (_rl_term_autowrap
&& i
== _rl_screenwidth
))
2257 #if defined (__MSDOS__)
2258 putc ('\r', rl_outstream
);
2260 tputs (_rl_term_cr
, 1, _rl_output_character_function
);
2261 #endif /* !__MSDOS__ */
2262 cpos
= _rl_last_c_pos
= 0;
2267 /* Move the cursor forward. We do it by printing the command
2268 to move the cursor forward if there is one, else print that
2269 portion of the output buffer again. Which is cheaper? */
2271 /* The above comment is left here for posterity. It is faster
2272 to print one character (non-control) than to print a control
2273 sequence telling the terminal to move forward one character.
2274 That kind of control is for people who don't know what the
2275 data is underneath the cursor. */
2277 /* However, we need a handle on where the current display position is
2278 in the buffer for the immediately preceding comment to be true.
2279 In multibyte locales, we don't currently have that info available.
2280 Without it, we don't know where the data we have to display begins
2281 in the buffer and we have to go back to the beginning of the screen
2282 line. In this case, we can use the terminal sequence to move forward
2283 if it's available. */
2284 if (mb_cur_max
> 1 && rl_byte_oriented
== 0)
2286 if (_rl_term_forward_char
)
2288 for (i
= cpos
; i
< dpos
; i
++)
2289 tputs (_rl_term_forward_char
, 1, _rl_output_character_function
);
2293 tputs (_rl_term_cr
, 1, _rl_output_character_function
);
2294 for (i
= 0; i
< new; i
++)
2295 putc (data
[i
], rl_outstream
);
2299 for (i
= cpos
; i
< new; i
++)
2300 putc (data
[i
], rl_outstream
);
2303 #if defined (HANDLE_MULTIBYTE)
2304 /* NEW points to the buffer point, but _rl_last_c_pos is the display point.
2305 The byte length of the string is probably bigger than the column width
2306 of the string, which means that if NEW == _rl_last_c_pos, then NEW's
2307 display point is less than _rl_last_c_pos. */
2309 else if (cpos
> dpos
)
2310 _rl_backspace (cpos
- dpos
);
2312 _rl_last_c_pos
= dpos
;
2315 /* PWP: move the cursor up or down. */
2320 register int delta
, i
;
2322 if (_rl_last_v_pos
== to
|| to
> _rl_screenheight
)
2325 if ((delta
= to
- _rl_last_v_pos
) > 0)
2327 for (i
= 0; i
< delta
; i
++)
2328 putc ('\n', rl_outstream
);
2329 #if defined (__MSDOS__)
2330 putc ('\r', rl_outstream
);
2332 tputs (_rl_term_cr
, 1, _rl_output_character_function
);
2341 fflush (rl_outstream
);
2342 ScreenGetCursor (&row
, &col
);
2343 ScreenSetCursor (row
+ delta
, col
);
2346 if (_rl_term_up
&& *_rl_term_up
)
2347 for (i
= 0; i
< -delta
; i
++)
2348 tputs (_rl_term_up
, 1, _rl_output_character_function
);
2349 #endif /* !__DJGPP__ */
2352 _rl_last_v_pos
= to
; /* Now TO is here */
2355 /* Physically print C on rl_outstream. This is for functions which know
2356 how to optimize the display. Return the number of characters output. */
2362 if (META_CHAR (c
) && (_rl_output_meta_chars
== 0))
2364 fprintf (rl_outstream
, "M-");
2369 #if defined (DISPLAY_TABS)
2370 if ((CTRL_CHAR (c
) && c
!= '\t') || c
== RUBOUT
)
2372 if (CTRL_CHAR (c
) || c
== RUBOUT
)
2373 #endif /* !DISPLAY_TABS */
2375 fprintf (rl_outstream
, "C-");
2377 c
= CTRL_CHAR (c
) ? UNCTRL (c
) : '?';
2380 putc (c
, rl_outstream
);
2381 fflush (rl_outstream
);
2386 rl_character_len (c
, pos
)
2387 register int c
, pos
;
2391 uc
= (unsigned char)c
;
2394 return ((_rl_output_meta_chars
== 0) ? 4 : 1);
2398 #if defined (DISPLAY_TABS)
2399 return (((pos
| 7) + 1) - pos
);
2402 #endif /* !DISPLAY_TABS */
2405 if (CTRL_CHAR (c
) || c
== RUBOUT
)
2408 return ((ISPRINT (uc
)) ? 1 : 2);
2410 /* How to print things in the "echo-area". The prompt is treated as a
2412 static int msg_saved_prompt
= 0;
2414 #if defined (USE_VARARGS)
2416 #if defined (PREFER_STDARG)
2417 rl_message (const char *format
, ...)
2419 rl_message (va_alist
)
2424 #if defined (PREFER_VARARGS)
2427 #if defined (HAVE_VSNPRINTF)
2431 #if defined (PREFER_STDARG)
2432 va_start (args
, format
);
2435 format
= va_arg (args
, char *);
2439 msg_buf
= xmalloc (msg_bufsiz
= 128);
2441 #if defined (HAVE_VSNPRINTF)
2442 bneed
= vsnprintf (msg_buf
, msg_bufsiz
, format
, args
);
2443 if (bneed
>= msg_bufsiz
- 1)
2445 msg_bufsiz
= bneed
+ 1;
2446 msg_buf
= xrealloc (msg_buf
, msg_bufsiz
);
2449 #if defined (PREFER_STDARG)
2450 va_start (args
, format
);
2453 format
= va_arg (args
, char *);
2455 vsnprintf (msg_buf
, msg_bufsiz
- 1, format
, args
);
2458 vsprintf (msg_buf
, format
, args
);
2459 msg_buf
[msg_bufsiz
- 1] = '\0'; /* overflow? */
2463 if (saved_local_prompt
== 0)
2466 msg_saved_prompt
= 1;
2468 else if (local_prompt
!= saved_local_prompt
)
2470 FREE (local_prompt
);
2471 FREE (local_prompt_prefix
);
2472 local_prompt
= (char *)NULL
;
2474 rl_display_prompt
= msg_buf
;
2475 local_prompt
= expand_prompt (msg_buf
, 0, &prompt_visible_length
,
2476 &prompt_last_invisible
,
2477 &prompt_invis_chars_first_line
,
2478 &prompt_physical_chars
);
2479 local_prompt_prefix
= (char *)NULL
;
2480 local_prompt_len
= local_prompt
? strlen (local_prompt
) : 0;
2481 (*rl_redisplay_function
) ();
2485 #else /* !USE_VARARGS */
2487 rl_message (format
, arg1
, arg2
)
2491 msg_buf
= xmalloc (msg_bufsiz
= 128);
2493 sprintf (msg_buf
, format
, arg1
, arg2
);
2494 msg_buf
[msg_bufsiz
- 1] = '\0'; /* overflow? */
2496 rl_display_prompt
= msg_buf
;
2497 if (saved_local_prompt
== 0)
2500 msg_saved_prompt
= 1;
2502 else if (local_prompt
!= saved_local_prompt
)
2504 FREE (local_prompt
);
2505 FREE (local_prompt_prefix
);
2506 local_prompt
= (char *)NULL
;
2508 local_prompt
= expand_prompt (msg_buf
, 0, &prompt_visible_length
,
2509 &prompt_last_invisible
,
2510 &prompt_invis_chars_first_line
,
2511 &prompt_physical_chars
);
2512 local_prompt_prefix
= (char *)NULL
;
2513 local_prompt_len
= local_prompt
? strlen (local_prompt
) : 0;
2514 (*rl_redisplay_function
) ();
2518 #endif /* !USE_VARARGS */
2520 /* How to clear things from the "echo-area". */
2524 rl_display_prompt
= rl_prompt
;
2525 if (msg_saved_prompt
)
2527 rl_restore_prompt ();
2528 msg_saved_prompt
= 0;
2530 (*rl_redisplay_function
) ();
2535 rl_reset_line_state ()
2539 rl_display_prompt
= rl_prompt
? rl_prompt
: "";
2547 saved_local_prompt
= local_prompt
;
2548 saved_local_prefix
= local_prompt_prefix
;
2549 saved_prefix_length
= prompt_prefix_length
;
2550 saved_local_length
= local_prompt_len
;
2551 saved_last_invisible
= prompt_last_invisible
;
2552 saved_visible_length
= prompt_visible_length
;
2553 saved_invis_chars_first_line
= prompt_invis_chars_first_line
;
2554 saved_physical_chars
= prompt_physical_chars
;
2556 local_prompt
= local_prompt_prefix
= (char *)0;
2557 local_prompt_len
= 0;
2558 prompt_last_invisible
= prompt_visible_length
= prompt_prefix_length
= 0;
2559 prompt_invis_chars_first_line
= prompt_physical_chars
= 0;
2563 rl_restore_prompt ()
2565 FREE (local_prompt
);
2566 FREE (local_prompt_prefix
);
2568 local_prompt
= saved_local_prompt
;
2569 local_prompt_prefix
= saved_local_prefix
;
2570 local_prompt_len
= saved_local_length
;
2571 prompt_prefix_length
= saved_prefix_length
;
2572 prompt_last_invisible
= saved_last_invisible
;
2573 prompt_visible_length
= saved_visible_length
;
2574 prompt_invis_chars_first_line
= saved_invis_chars_first_line
;
2575 prompt_physical_chars
= saved_physical_chars
;
2577 /* can test saved_local_prompt to see if prompt info has been saved. */
2578 saved_local_prompt
= saved_local_prefix
= (char *)0;
2579 saved_local_length
= 0;
2580 saved_last_invisible
= saved_visible_length
= saved_prefix_length
= 0;
2581 saved_invis_chars_first_line
= saved_physical_chars
= 0;
2585 _rl_make_prompt_for_search (pchar
)
2593 /* We've saved the prompt, and can do anything with the various prompt
2594 strings we need before they're restored. We want the unexpanded
2595 portion of the prompt string after any final newline. */
2596 p
= rl_prompt
? strrchr (rl_prompt
, '\n') : 0;
2599 len
= (rl_prompt
&& *rl_prompt
) ? strlen (rl_prompt
) : 0;
2600 pmt
= (char *)xmalloc (len
+ 2);
2602 strcpy (pmt
, rl_prompt
);
2610 pmt
= (char *)xmalloc (len
+ 2);
2617 /* will be overwritten by expand_prompt, called from rl_message */
2618 prompt_physical_chars
= saved_physical_chars
+ 1;
2622 /* Quick redisplay hack when erasing characters at the end of the line. */
2624 _rl_erase_at_end_of_line (l
)
2630 for (i
= 0; i
< l
; i
++)
2631 putc (' ', rl_outstream
);
2633 for (i
= 0; i
< l
; i
++)
2634 visible_line
[--_rl_last_c_pos
] = '\0';
2638 /* Clear to the end of the line. COUNT is the minimum
2639 number of character spaces to clear, */
2641 _rl_clear_to_eol (count
)
2645 if (_rl_term_clreol
)
2646 tputs (_rl_term_clreol
, 1, _rl_output_character_function
);
2650 space_to_eol (count
);
2653 /* Clear to the end of the line using spaces. COUNT is the minimum
2654 number of character spaces to clear, */
2656 space_to_eol (count
)
2661 for (i
= 0; i
< count
; i
++)
2662 putc (' ', rl_outstream
);
2664 _rl_last_c_pos
+= count
;
2671 if (_rl_term_clrpag
)
2672 tputs (_rl_term_clrpag
, 1, _rl_output_character_function
);
2677 ScreenSetCursor (0, 0);
2678 #endif /* __DJGPP__ */
2681 /* Insert COUNT characters from STRING to the output stream at column COL. */
2683 insert_some_chars (string
, count
, col
)
2687 open_some_spaces (col
);
2688 _rl_output_some_chars (string
, count
);
2691 /* Insert COL spaces, keeping the cursor at the same position. We follow the
2692 ncurses documentation and use either im/ei with explicit spaces, or IC/ic
2693 by itself. We assume there will either be ei or we don't need to use it. */
2695 open_some_spaces (col
)
2698 #if !defined (__MSDOS__) && (!defined (__MINGW32__) || defined (NCURSES_VERSION))
2702 /* If IC is defined, then we do not have to "enter" insert mode. */
2705 buffer
= tgoto (_rl_term_IC
, 0, col
);
2706 tputs (buffer
, 1, _rl_output_character_function
);
2708 else if (_rl_term_im
&& *_rl_term_im
)
2710 tputs (_rl_term_im
, 1, _rl_output_character_function
);
2711 /* just output the desired number of spaces */
2712 for (i
= col
; i
--; )
2713 _rl_output_character_function (' ');
2714 /* If there is a string to turn off insert mode, use it now. */
2715 if (_rl_term_ei
&& *_rl_term_ei
)
2716 tputs (_rl_term_ei
, 1, _rl_output_character_function
);
2717 /* and move back the right number of spaces */
2718 _rl_backspace (col
);
2720 else if (_rl_term_ic
&& *_rl_term_ic
)
2722 /* If there is a special command for inserting characters, then
2723 use that first to open up the space. */
2724 for (i
= col
; i
--; )
2725 tputs (_rl_term_ic
, 1, _rl_output_character_function
);
2727 #endif /* !__MSDOS__ && (!__MINGW32__ || NCURSES_VERSION)*/
2730 /* Delete COUNT characters from the display line. */
2732 delete_chars (count
)
2735 if (count
> _rl_screenwidth
) /* XXX */
2738 #if !defined (__MSDOS__) && (!defined (__MINGW32__) || defined (NCURSES_VERSION))
2739 if (_rl_term_DC
&& *_rl_term_DC
)
2742 buffer
= tgoto (_rl_term_DC
, count
, count
);
2743 tputs (buffer
, count
, _rl_output_character_function
);
2747 if (_rl_term_dc
&& *_rl_term_dc
)
2749 tputs (_rl_term_dc
, 1, _rl_output_character_function
);
2751 #endif /* !__MSDOS__ && (!__MINGW32__ || NCURSES_VERSION)*/
2760 /* If the cursor is the only thing on an otherwise-blank last line,
2761 compensate so we don't print an extra CRLF. */
2762 if (_rl_vis_botlin
&& _rl_last_c_pos
== 0 &&
2763 visible_line
[vis_lbreaks
[_rl_vis_botlin
]] == 0)
2768 _rl_move_vert (_rl_vis_botlin
);
2769 /* If we've wrapped lines, remove the final xterm line-wrap flag. */
2770 if (full_lines
&& _rl_term_autowrap
&& (VIS_LLEN(_rl_vis_botlin
) == _rl_screenwidth
))
2774 last_line
= &visible_line
[vis_lbreaks
[_rl_vis_botlin
]];
2775 cpos_buffer_position
= -1; /* don't know where we are in buffer */
2776 _rl_move_cursor_relative (_rl_screenwidth
- 1, last_line
); /* XXX */
2777 _rl_clear_to_eol (0);
2778 putc (last_line
[_rl_screenwidth
- 1], rl_outstream
);
2782 fflush (rl_outstream
);
2786 /* Move to the start of the current line. */
2792 #if defined (__MSDOS__)
2793 putc ('\r', rl_outstream
);
2795 tputs (_rl_term_cr
, 1, _rl_output_character_function
);
2801 /* Redraw the last line of a multi-line prompt that may possibly contain
2802 terminal escape sequences. Called with the cursor at column 0 of the
2803 line to draw the prompt on. */
2810 oldp
= rl_display_prompt
;
2813 rl_display_prompt
= t
;
2814 local_prompt
= expand_prompt (t
, PMT_MULTILINE
,
2815 &prompt_visible_length
,
2816 &prompt_last_invisible
,
2817 &prompt_invis_chars_first_line
,
2818 &prompt_physical_chars
);
2819 local_prompt_prefix
= (char *)NULL
;
2820 local_prompt_len
= local_prompt
? strlen (local_prompt
) : 0;
2822 rl_forced_update_display ();
2824 rl_display_prompt
= oldp
;
2825 rl_restore_prompt();
2828 /* Redisplay the current line after a SIGWINCH is received. */
2830 _rl_redisplay_after_sigwinch ()
2834 /* Clear the last line (assuming that the screen size change will result in
2835 either more or fewer characters on that line only) and put the cursor at
2836 column 0. Make sure the right thing happens if we have wrapped to a new
2840 _rl_move_vert (_rl_vis_botlin
);
2842 #if defined (__MSDOS__)
2843 putc ('\r', rl_outstream
);
2845 tputs (_rl_term_cr
, 1, _rl_output_character_function
);
2848 #if defined (__MSDOS__)
2849 space_to_eol (_rl_screenwidth
);
2850 putc ('\r', rl_outstream
);
2852 if (_rl_term_clreol
)
2853 tputs (_rl_term_clreol
, 1, _rl_output_character_function
);
2856 space_to_eol (_rl_screenwidth
);
2857 tputs (_rl_term_cr
, 1, _rl_output_character_function
);
2860 if (_rl_last_v_pos
> 0)
2866 /* Redraw only the last line of a multi-line prompt. */
2867 t
= strrchr (rl_display_prompt
, '\n');
2869 redraw_prompt (++t
);
2871 rl_forced_update_display ();
2875 _rl_clean_up_for_exit ()
2879 if (_rl_vis_botlin
> 0) /* minor optimization plus bug fix */
2880 _rl_move_vert (_rl_vis_botlin
);
2882 fflush (rl_outstream
);
2883 rl_restart_output (1, 0);
2888 _rl_erase_entire_line ()
2891 _rl_clear_to_eol (0);
2893 fflush (rl_outstream
);
2899 fflush (rl_outstream
);
2902 /* return the `current display line' of the cursor -- the number of lines to
2903 move up to get to the first screen line of the current readline line. */
2905 _rl_current_display_line ()
2909 /* Find out whether or not there might be invisible characters in the
2911 if (rl_display_prompt
== rl_prompt
)
2912 nleft
= _rl_last_c_pos
- _rl_screenwidth
- rl_visible_prompt_length
;
2914 nleft
= _rl_last_c_pos
- _rl_screenwidth
;
2917 ret
= 1 + nleft
/ _rl_screenwidth
;
2924 #if defined (HANDLE_MULTIBYTE)
2925 /* Calculate the number of screen columns occupied by STR from START to END.
2926 In the case of multibyte characters with stateful encoding, we have to
2927 scan from the beginning of the string to take the state into account. */
2929 _rl_col_width (str
, start
, end
, flags
)
2931 int start
, end
, flags
;
2935 int tmp
, point
, width
, max
;
2939 if (MB_CUR_MAX
== 1 || rl_byte_oriented
)
2940 /* this can happen in some cases where it's inconvenient to check */
2941 return (end
- start
);
2943 memset (&ps
, 0, sizeof (mbstate_t));
2948 /* Try to short-circuit common cases. The adjustment to remove wrap_offset
2949 is done by the caller. */
2950 /* 1. prompt string */
2951 if (flags
&& start
== 0 && end
== local_prompt_len
&& memcmp (str
, local_prompt
, local_prompt_len
) == 0)
2952 return (prompt_physical_chars
+ wrap_offset
);
2953 /* 2. prompt string + line contents */
2954 else if (flags
&& start
== 0 && local_prompt_len
> 0 && end
> local_prompt_len
&& local_prompt
&& memcmp (str
, local_prompt
, local_prompt_len
) == 0)
2956 tmp
= prompt_physical_chars
+ wrap_offset
;
2957 /* XXX - try to call ourselves recursively with non-prompt portion */
2958 tmp
+= _rl_col_width (str
, local_prompt_len
, end
, flags
);
2962 while (point
< start
)
2964 tmp
= mbrlen (str
+ point
, max
, &ps
);
2965 if (MB_INVALIDCH ((size_t)tmp
))
2967 /* In this case, the bytes are invalid or too short to compose a
2968 multibyte character, so we assume that the first byte represents
2969 a single character. */
2973 /* Clear the state of the byte sequence, because in this case the
2974 effect of mbstate is undefined. */
2975 memset (&ps
, 0, sizeof (mbstate_t));
2977 else if (MB_NULLWCH (tmp
))
2978 break; /* Found '\0' */
2986 /* If START is not a byte that starts a character, then POINT will be
2987 greater than START. In this case, assume that (POINT - START) gives
2988 a byte count that is the number of columns of difference. */
2989 width
= point
- start
;
2993 tmp
= mbrtowc (&wc
, str
+ point
, max
, &ps
);
2994 if (MB_INVALIDCH ((size_t)tmp
))
2996 /* In this case, the bytes are invalid or too short to compose a
2997 multibyte character, so we assume that the first byte represents
2998 a single character. */
3002 /* and assume that the byte occupies a single column. */
3005 /* Clear the state of the byte sequence, because in this case the
3006 effect of mbstate is undefined. */
3007 memset (&ps
, 0, sizeof (mbstate_t));
3009 else if (MB_NULLWCH (tmp
))
3010 break; /* Found '\0' */
3016 width
+= (tmp
>= 0) ? tmp
: 1;
3020 width
+= point
- end
;
3024 #endif /* HANDLE_MULTIBYTE */