]> git.ipfire.org Git - thirdparty/bash.git/blame - lib/readline/display.c
commit bash-20051027 snapshot
[thirdparty/bash.git] / lib / readline / display.c
CommitLineData
726f6388
JA
1/* display.c -- readline redisplay facility. */
2
cc87ba64 3/* Copyright (C) 1987-2005 Free Software Foundation, Inc.
726f6388
JA
4
5 This file is part of the GNU Readline Library, a library for
6 reading lines of text with interactive input and history editing.
7
8 The GNU Readline Library is free software; you can redistribute it
9 and/or modify it under the terms of the GNU General Public License
bb70624e 10 as published by the Free Software Foundation; either version 2, or
726f6388
JA
11 (at your option) any later version.
12
13 The GNU Readline Library is distributed in the hope that it will be
14 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 The GNU General Public License is often shipped with GNU software, and
19 is generally kept in a file called COPYING or LICENSE. If you do not
20 have a copy of the license, write to the Free Software Foundation,
bb70624e 21 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
726f6388
JA
22#define READLINE_LIBRARY
23
24#if defined (HAVE_CONFIG_H)
ccc6cda3 25# include <config.h>
726f6388
JA
26#endif
27
726f6388
JA
28#include <sys/types.h>
29
30#if defined (HAVE_UNISTD_H)
31# include <unistd.h>
32#endif /* HAVE_UNISTD_H */
33
d166f048
JA
34#include "posixstat.h"
35
726f6388
JA
36#if defined (HAVE_STDLIB_H)
37# include <stdlib.h>
38#else
39# include "ansi_stdlib.h"
40#endif /* HAVE_STDLIB_H */
41
d166f048
JA
42#include <stdio.h>
43
726f6388
JA
44/* System-specific feature definitions and include files. */
45#include "rldefs.h"
7117c2d2 46#include "rlmbutil.h"
726f6388 47
ccc6cda3
JA
48/* Termcap library stuff. */
49#include "tcap.h"
50
726f6388
JA
51/* Some standard library routines. */
52#include "readline.h"
53#include "history.h"
54
bb70624e
JA
55#include "rlprivate.h"
56#include "xmalloc.h"
57
726f6388
JA
58#if !defined (strchr) && !defined (__STDC__)
59extern char *strchr (), *strrchr ();
60#endif /* !strchr && !__STDC__ */
61
bb70624e 62#if defined (HACK_TERMCAP_MOTION)
28ef6c31 63extern char *_rl_term_forward_char;
cce855bc 64#endif
726f6388 65
f73dda09
JA
66static void update_line PARAMS((char *, char *, int, int, int, int));
67static void space_to_eol PARAMS((int));
68static void delete_chars PARAMS((int));
7117c2d2 69static void insert_some_chars PARAMS((char *, int, int));
f73dda09 70static void cr PARAMS((void));
ccc6cda3 71
7117c2d2 72#if defined (HANDLE_MULTIBYTE)
d3a24ed2 73static int _rl_col_width PARAMS((const char *, int, int));
7117c2d2
JA
74static int *_rl_wrapped_line;
75#else
76# define _rl_col_width(l, s, e) (((e) <= (s)) ? 0 : (e) - (s))
77#endif
78
ccc6cda3 79static int *inv_lbreaks, *vis_lbreaks;
bb70624e 80static int inv_lbsize, vis_lbsize;
726f6388
JA
81
82/* Heuristic used to decide whether it is faster to move from CUR to NEW
83 by backing up or outputting a carriage return and moving forward. */
84#define CR_FASTER(new, cur) (((new) + 1) < ((cur) - (new)))
85
86/* **************************************************************** */
87/* */
88/* Display stuff */
89/* */
90/* **************************************************************** */
91
92/* This is the stuff that is hard for me. I never seem to write good
93 display routines in C. Let's see how I do this time. */
94
95/* (PWP) Well... Good for a simple line updater, but totally ignores
96 the problems of input lines longer than the screen width.
97
98 update_line and the code that calls it makes a multiple line,
99 automatically wrapping line update. Careful attention needs
100 to be paid to the vertical position variables. */
101
102/* Keep two buffers; one which reflects the current contents of the
103 screen, and the other to draw what we think the new contents should
104 be. Then compare the buffers, and make whatever changes to the
105 screen itself that we should. Finally, make the buffer that we
106 just drew into be the one which reflects the current contents of the
107 screen, and place the cursor where it belongs.
108
109 Commands that want to can fix the display themselves, and then let
110 this function know that the display has been fixed by setting the
111 RL_DISPLAY_FIXED variable. This is good for efficiency. */
112
ccc6cda3 113/* Application-specific redisplay function. */
28ef6c31 114rl_voidfunc_t *rl_redisplay_function = rl_redisplay;
ccc6cda3 115
726f6388
JA
116/* Global variables declared here. */
117/* What YOU turn on when you have handled all redisplay yourself. */
118int rl_display_fixed = 0;
119
ccc6cda3 120int _rl_suppress_redisplay = 0;
798a67db 121int _rl_want_redisplay = 0;
ccc6cda3 122
726f6388
JA
123/* The stuff that gets printed out before the actual text of the line.
124 This is usually pointing to rl_prompt. */
125char *rl_display_prompt = (char *)NULL;
126
127/* Pseudo-global variables declared here. */
177d51f7 128
726f6388 129/* The visible cursor position. If you print some text, adjust this. */
177d51f7
CR
130/* NOTE: _rl_last_c_pos is used as a buffer index when not in a locale
131 supporting multibyte characters, and an absolute cursor position when
132 in such a locale. This is an artifact of the donated multibyte support.
133 Care must be taken when modifying its value. */
726f6388
JA
134int _rl_last_c_pos = 0;
135int _rl_last_v_pos = 0;
136
137/* Number of lines currently on screen minus 1. */
138int _rl_vis_botlin = 0;
139
140/* Variables used only in this file. */
141/* The last left edge of text that was displayed. This is used when
142 doing horizontal scrolling. It shifts in thirds of a screenwidth. */
ccc6cda3 143static int last_lmargin;
726f6388
JA
144
145/* The line display buffers. One is the line currently displayed on
146 the screen. The other is the line about to be displayed. */
147static char *visible_line = (char *)NULL;
148static char *invisible_line = (char *)NULL;
149
150/* A buffer for `modeline' messages. */
151static char msg_buf[128];
152
153/* Non-zero forces the redisplay even if we thought it was unnecessary. */
ccc6cda3 154static int forced_display;
726f6388
JA
155
156/* Default and initial buffer size. Can grow. */
157static int line_size = 1024;
158
28ef6c31
JA
159/* Variables to keep track of the expanded prompt string, which may
160 include invisible characters. */
161
726f6388 162static char *local_prompt, *local_prompt_prefix;
28ef6c31 163static int prompt_visible_length, prompt_prefix_length;
726f6388
JA
164
165/* The number of invisible characters in the line currently being
166 displayed on the screen. */
ccc6cda3
JA
167static int visible_wrap_offset;
168
28ef6c31
JA
169/* The number of invisible characters in the prompt string. Static so it
170 can be shared between rl_redisplay and update_line */
ccc6cda3
JA
171static int wrap_offset;
172
28ef6c31
JA
173/* The index of the last invisible character in the prompt string. */
174static int prompt_last_invisible;
726f6388
JA
175
176/* The length (buffer offset) of the first line of the last (possibly
177 multi-line) buffer displayed on the screen. */
ccc6cda3 178static int visible_first_line_len;
726f6388 179
28ef6c31
JA
180/* Number of invisible characters on the first physical line of the prompt.
181 Only valid when the number of physical characters in the prompt exceeds
182 (or is equal to) _rl_screenwidth. */
183static int prompt_invis_chars_first_line;
184
185static int prompt_last_screen_line;
186
43cdcad8
CR
187static int prompt_physical_chars;
188
6e70dbff
CR
189/* Variables to save and restore prompt and display information. */
190
191/* These are getting numerous enough that it's time to create a struct. */
192
193static char *saved_local_prompt;
194static char *saved_local_prefix;
195static int saved_last_invisible;
196static int saved_visible_length;
197static int saved_prefix_length;
198static int saved_invis_chars_first_line;
199static int saved_physical_chars;
200
726f6388
JA
201/* Expand the prompt string S and return the number of visible
202 characters in *LP, if LP is not null. This is currently more-or-less
ccc6cda3 203 a placeholder for expansion. LIP, if non-null is a place to store the
28ef6c31
JA
204 index of the last invisible character in the returned string. NIFLP,
205 if non-zero, is a place to store the number of invisible characters in
43cdcad8
CR
206 the first prompt line. The previous are used as byte counts -- indexes
207 into a character buffer. */
726f6388
JA
208
209/* Current implementation:
210 \001 (^A) start non-visible characters
211 \002 (^B) end non-visible characters
212 all characters except \001 and \002 (following a \001) are copied to
213 the returned string; all characters except those between \001 and
214 \002 are assumed to be `visible'. */
215
216static char *
43cdcad8 217expand_prompt (pmt, lp, lip, niflp, vlp)
726f6388 218 char *pmt;
43cdcad8 219 int *lp, *lip, *niflp, *vlp;
726f6388
JA
220{
221 char *r, *ret, *p;
b954488b 222 int l, rl, last, ignoring, ninvis, invfl, invflset, ind, pind, physchars;
726f6388
JA
223
224 /* Short-circuit if we can. */
43cdcad8 225 if ((MB_CUR_MAX <= 1 || rl_byte_oriented) && strchr (pmt, RL_PROMPT_START_IGNORE) == 0)
726f6388
JA
226 {
227 r = savestring (pmt);
228 if (lp)
229 *lp = strlen (r);
43cdcad8
CR
230 if (lip)
231 *lip = 0;
232 if (niflp)
233 *niflp = 0;
234 if (vlp)
235 *vlp = lp ? *lp : strlen (r);
726f6388
JA
236 return r;
237 }
238
ccc6cda3 239 l = strlen (pmt);
f73dda09 240 r = ret = (char *)xmalloc (l + 1);
28ef6c31
JA
241
242 invfl = 0; /* invisible chars in first line of prompt */
b954488b 243 invflset = 0; /* we only want to set invfl once */
28ef6c31 244
43cdcad8 245 for (rl = ignoring = last = ninvis = physchars = 0, p = pmt; p && *p; p++)
726f6388
JA
246 {
247 /* This code strips the invisible character string markers
248 RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE */
249 if (*p == RL_PROMPT_START_IGNORE)
250 {
251 ignoring++;
252 continue;
253 }
254 else if (ignoring && *p == RL_PROMPT_END_IGNORE)
255 {
256 ignoring = 0;
1569c106
CR
257 if (p[-1] != RL_PROMPT_START_IGNORE)
258 last = r - ret - 1;
726f6388
JA
259 continue;
260 }
261 else
262 {
43cdcad8
CR
263#if defined (HANDLE_MULTIBYTE)
264 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
265 {
266 pind = p - pmt;
267 ind = _rl_find_next_mbchar (pmt, pind, 1, MB_FIND_NONZERO);
268 l = ind - pind;
269 while (l--)
270 *r++ = *p++;
271 if (!ignoring)
37c41ab1
CR
272 {
273 rl += ind - pind;
274 physchars += _rl_col_width (pmt, pind, ind);
275 }
43cdcad8
CR
276 else
277 ninvis += ind - pind;
278 p--; /* compensate for later increment */
279 }
28ef6c31 280 else
43cdcad8
CR
281#endif
282 {
283 *r++ = *p;
284 if (!ignoring)
37c41ab1
CR
285 {
286 rl++; /* visible length byte counter */
287 physchars++;
288 }
43cdcad8
CR
289 else
290 ninvis++; /* invisible chars byte counter */
291 }
292
b954488b
CR
293 if (invflset == 0 && rl >= _rl_screenwidth)
294 {
295 invfl = ninvis;
296 invflset = 1;
297 }
726f6388
JA
298 }
299 }
300
28ef6c31
JA
301 if (rl < _rl_screenwidth)
302 invfl = ninvis;
303
726f6388
JA
304 *r = '\0';
305 if (lp)
306 *lp = rl;
ccc6cda3
JA
307 if (lip)
308 *lip = last;
28ef6c31
JA
309 if (niflp)
310 *niflp = invfl;
43cdcad8
CR
311 if (vlp)
312 *vlp = physchars;
726f6388
JA
313 return ret;
314}
315
bb70624e
JA
316/* Just strip out RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE from
317 PMT and return the rest of PMT. */
318char *
319_rl_strip_prompt (pmt)
320 char *pmt;
321{
322 char *ret;
323
43cdcad8 324 ret = expand_prompt (pmt, (int *)NULL, (int *)NULL, (int *)NULL, (int *)NULL);
bb70624e
JA
325 return ret;
326}
327
726f6388
JA
328/*
329 * Expand the prompt string into the various display components, if
330 * necessary.
331 *
332 * local_prompt = expanded last line of string in rl_display_prompt
333 * (portion after the final newline)
334 * local_prompt_prefix = portion before last newline of rl_display_prompt,
335 * expanded via expand_prompt
28ef6c31
JA
336 * prompt_visible_length = number of visible characters in local_prompt
337 * prompt_prefix_length = number of visible characters in local_prompt_prefix
726f6388
JA
338 *
339 * This function is called once per call to readline(). It may also be
340 * called arbitrarily to expand the primary prompt.
341 *
342 * The return value is the number of visible characters on the last line
343 * of the (possibly multi-line) prompt.
344 */
345int
346rl_expand_prompt (prompt)
347 char *prompt;
348{
349 char *p, *t;
350 int c;
351
352 /* Clear out any saved values. */
28ef6c31
JA
353 FREE (local_prompt);
354 FREE (local_prompt_prefix);
355
726f6388 356 local_prompt = local_prompt_prefix = (char *)0;
ec2199bd
CR
357 prompt_last_invisible = prompt_invis_chars_first_line = 0;
358 prompt_visible_length = prompt_physical_chars = 0;
726f6388 359
ccc6cda3 360 if (prompt == 0 || *prompt == 0)
726f6388
JA
361 return (0);
362
363 p = strrchr (prompt, '\n');
364 if (!p)
365 {
28ef6c31
JA
366 /* The prompt is only one logical line, though it might wrap. */
367 local_prompt = expand_prompt (prompt, &prompt_visible_length,
368 &prompt_last_invisible,
43cdcad8
CR
369 &prompt_invis_chars_first_line,
370 &prompt_physical_chars);
726f6388 371 local_prompt_prefix = (char *)0;
28ef6c31 372 return (prompt_visible_length);
726f6388
JA
373 }
374 else
375 {
376 /* The prompt spans multiple lines. */
377 t = ++p;
28ef6c31
JA
378 local_prompt = expand_prompt (p, &prompt_visible_length,
379 &prompt_last_invisible,
43cdcad8 380 (int *)NULL,
e6e3b444 381 &prompt_physical_chars);
726f6388
JA
382 c = *t; *t = '\0';
383 /* The portion of the prompt string up to and including the
384 final newline is now null-terminated. */
28ef6c31
JA
385 local_prompt_prefix = expand_prompt (prompt, &prompt_prefix_length,
386 (int *)NULL,
43cdcad8 387 &prompt_invis_chars_first_line,
e6e3b444 388 (int *)NULL);
726f6388 389 *t = c;
28ef6c31 390 return (prompt_prefix_length);
726f6388
JA
391 }
392}
393
bb70624e
JA
394/* Initialize the VISIBLE_LINE and INVISIBLE_LINE arrays, and their associated
395 arrays of line break markers. MINSIZE is the minimum size of VISIBLE_LINE
396 and INVISIBLE_LINE; if it is greater than LINE_SIZE, LINE_SIZE is
397 increased. If the lines have already been allocated, this ensures that
398 they can hold at least MINSIZE characters. */
399static void
400init_line_structures (minsize)
401 int minsize;
402{
403 register int n;
404
405 if (invisible_line == 0) /* initialize it */
406 {
407 if (line_size < minsize)
408 line_size = minsize;
f73dda09
JA
409 visible_line = (char *)xmalloc (line_size);
410 invisible_line = (char *)xmalloc (line_size);
bb70624e
JA
411 }
412 else if (line_size < minsize) /* ensure it can hold MINSIZE chars */
413 {
414 line_size *= 2;
415 if (line_size < minsize)
416 line_size = minsize;
f73dda09
JA
417 visible_line = (char *)xrealloc (visible_line, line_size);
418 invisible_line = (char *)xrealloc (invisible_line, line_size);
bb70624e
JA
419 }
420
421 for (n = minsize; n < line_size; n++)
422 {
423 visible_line[n] = 0;
424 invisible_line[n] = 1;
425 }
426
427 if (vis_lbreaks == 0)
428 {
429 /* should be enough. */
430 inv_lbsize = vis_lbsize = 256;
431 inv_lbreaks = (int *)xmalloc (inv_lbsize * sizeof (int));
432 vis_lbreaks = (int *)xmalloc (vis_lbsize * sizeof (int));
7117c2d2
JA
433#if defined (HANDLE_MULTIBYTE)
434 _rl_wrapped_line = (int *)xmalloc (vis_lbsize * sizeof (int));
435#endif
bb70624e
JA
436 inv_lbreaks[0] = vis_lbreaks[0] = 0;
437 }
438}
439
726f6388
JA
440/* Basic redisplay algorithm. */
441void
442rl_redisplay ()
443{
ccc6cda3
JA
444 register int in, out, c, linenum, cursor_linenum;
445 register char *line;
446 int c_pos, inv_botlin, lb_botlin, lb_linenum;
37c41ab1 447 int newlines, lpos, temp, modmark, n0, num;
726f6388 448 char *prompt_this_line;
7117c2d2
JA
449#if defined (HANDLE_MULTIBYTE)
450 wchar_t wc;
451 size_t wc_bytes;
452 int wc_width;
453 mbstate_t ps;
454 int _rl_wrapped_multicolumn = 0;
455#endif
726f6388
JA
456
457 if (!readline_echoing_p)
458 return;
459
460 if (!rl_display_prompt)
461 rl_display_prompt = "";
462
055a1bf5 463 if (invisible_line == 0 || vis_lbreaks == 0)
726f6388 464 {
bb70624e 465 init_line_structures (0);
726f6388
JA
466 rl_on_new_line ();
467 }
468
469 /* Draw the line into the buffer. */
470 c_pos = -1;
471
ccc6cda3
JA
472 line = invisible_line;
473 out = inv_botlin = 0;
474
726f6388
JA
475 /* Mark the line as modified or not. We only do this for history
476 lines. */
43cdcad8 477 modmark = 0;
726f6388
JA
478 if (_rl_mark_modified_lines && current_history () && rl_undo_list)
479 {
480 line[out++] = '*';
481 line[out] = '\0';
43cdcad8 482 modmark = 1;
726f6388
JA
483 }
484
485 /* If someone thought that the redisplay was handled, but the currently
486 visible line has a different modification state than the one about
487 to become visible, then correct the caller's misconception. */
488 if (visible_line[0] != invisible_line[0])
489 rl_display_fixed = 0;
490
491 /* If the prompt to be displayed is the `primary' readline prompt (the
492 one passed to readline()), use the values we have already expanded.
493 If not, use what's already in rl_display_prompt. WRAP_OFFSET is the
494 number of non-visible characters in the prompt string. */
ccc6cda3 495 if (rl_display_prompt == rl_prompt || local_prompt)
726f6388
JA
496 {
497 int local_len = local_prompt ? strlen (local_prompt) : 0;
498 if (local_prompt_prefix && forced_display)
499 _rl_output_some_chars (local_prompt_prefix, strlen (local_prompt_prefix));
500
501 if (local_len > 0)
ccc6cda3 502 {
b72432fd
JA
503 temp = local_len + out + 2;
504 if (temp >= line_size)
505 {
506 line_size = (temp + 1024) - (temp % 1024);
f73dda09
JA
507 visible_line = (char *)xrealloc (visible_line, line_size);
508 line = invisible_line = (char *)xrealloc (invisible_line, line_size);
b72432fd 509 }
ccc6cda3
JA
510 strncpy (line + out, local_prompt, local_len);
511 out += local_len;
512 }
726f6388 513 line[out] = '\0';
28ef6c31 514 wrap_offset = local_len - prompt_visible_length;
726f6388
JA
515 }
516 else
517 {
518 int pmtlen;
519 prompt_this_line = strrchr (rl_display_prompt, '\n');
520 if (!prompt_this_line)
521 prompt_this_line = rl_display_prompt;
522 else
523 {
524 prompt_this_line++;
bb70624e 525 pmtlen = prompt_this_line - rl_display_prompt; /* temp var */
726f6388 526 if (forced_display)
ccc6cda3 527 {
bb70624e 528 _rl_output_some_chars (rl_display_prompt, pmtlen);
ccc6cda3
JA
529 /* Make sure we are at column zero even after a newline,
530 regardless of the state of terminal output processing. */
bb70624e 531 if (pmtlen < 2 || prompt_this_line[-2] != '\r')
ccc6cda3
JA
532 cr ();
533 }
726f6388
JA
534 }
535
762a763b 536 prompt_physical_chars = pmtlen = strlen (prompt_this_line);
b72432fd
JA
537 temp = pmtlen + out + 2;
538 if (temp >= line_size)
539 {
540 line_size = (temp + 1024) - (temp % 1024);
f73dda09
JA
541 visible_line = (char *)xrealloc (visible_line, line_size);
542 line = invisible_line = (char *)xrealloc (invisible_line, line_size);
b72432fd 543 }
726f6388
JA
544 strncpy (line + out, prompt_this_line, pmtlen);
545 out += pmtlen;
546 line[out] = '\0';
28ef6c31 547 wrap_offset = prompt_invis_chars_first_line = 0;
726f6388
JA
548 }
549
bb70624e
JA
550#define CHECK_INV_LBREAKS() \
551 do { \
552 if (newlines >= (inv_lbsize - 2)) \
553 { \
554 inv_lbsize *= 2; \
555 inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
556 } \
557 } while (0)
7117c2d2
JA
558
559#if defined (HANDLE_MULTIBYTE)
ccc6cda3
JA
560#define CHECK_LPOS() \
561 do { \
b72432fd 562 lpos++; \
28ef6c31 563 if (lpos >= _rl_screenwidth) \
b72432fd 564 { \
bb70624e
JA
565 if (newlines >= (inv_lbsize - 2)) \
566 { \
567 inv_lbsize *= 2; \
568 inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
7117c2d2 569 _rl_wrapped_line = (int *)xrealloc (_rl_wrapped_line, inv_lbsize * sizeof (int)); \
bb70624e 570 } \
b72432fd 571 inv_lbreaks[++newlines] = out; \
7117c2d2 572 _rl_wrapped_line[newlines] = _rl_wrapped_multicolumn; \
b72432fd
JA
573 lpos = 0; \
574 } \
ccc6cda3 575 } while (0)
7117c2d2
JA
576#else
577#define CHECK_LPOS() \
578 do { \
579 lpos++; \
580 if (lpos >= _rl_screenwidth) \
581 { \
582 if (newlines >= (inv_lbsize - 2)) \
583 { \
584 inv_lbsize *= 2; \
585 inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
586 } \
587 inv_lbreaks[++newlines] = out; \
588 lpos = 0; \
589 } \
590 } while (0)
591#endif
ccc6cda3
JA
592
593 /* inv_lbreaks[i] is where line i starts in the buffer. */
594 inv_lbreaks[newlines = 0] = 0;
43cdcad8 595#if 0
d166f048 596 lpos = out - wrap_offset;
43cdcad8
CR
597#else
598 lpos = prompt_physical_chars + modmark;
599#endif
600
7117c2d2
JA
601#if defined (HANDLE_MULTIBYTE)
602 memset (_rl_wrapped_line, 0, vis_lbsize);
37c41ab1 603 num = 0;
7117c2d2 604#endif
d166f048 605
28ef6c31
JA
606 /* prompt_invis_chars_first_line is the number of invisible characters in
607 the first physical line of the prompt.
608 wrap_offset - prompt_invis_chars_first_line is the number of invis
609 chars on the second line. */
610
611 /* what if lpos is already >= _rl_screenwidth before we start drawing the
d166f048 612 contents of the command line? */
28ef6c31 613 while (lpos >= _rl_screenwidth)
d166f048 614 {
28ef6c31
JA
615 /* fix from Darin Johnson <darin@acuson.com> for prompt string with
616 invisible characters that is longer than the screen width. The
617 prompt_invis_chars_first_line variable could be made into an array
618 saying how many invisible characters there are per line, but that's
619 probably too much work for the benefit gained. How many people have
545f34cf
CR
620 prompts that exceed two physical lines?
621 Additional logic fix from Edward Catmur <ed@catmur.co.uk> */
37c41ab1
CR
622#if defined (HANDLE_MULTIBYTE)
623 n0 = num;
624 temp = local_prompt ? strlen (local_prompt) : 0;
625 while (num < temp)
626 {
627 if (_rl_col_width (local_prompt, n0, num) > _rl_screenwidth)
628 {
629 num = _rl_find_prev_mbchar (local_prompt, num, MB_FIND_ANY);
630 break;
631 }
632 num++;
633 }
634 temp = num +
635#else
28ef6c31 636 temp = ((newlines + 1) * _rl_screenwidth) +
37c41ab1 637#endif /* !HANDLE_MULTIBYTE */
545f34cf
CR
638 ((local_prompt_prefix == 0) ? ((newlines == 0) ? prompt_invis_chars_first_line
639 : ((newlines == 1) ? wrap_offset : 0))
640 : ((newlines == 0) ? wrap_offset :0));
641
d166f048 642 inv_lbreaks[++newlines] = temp;
37c41ab1
CR
643#if defined (HANDLE_MULTIBYTE)
644 lpos -= _rl_col_width (local_prompt, n0, num);
645#else
28ef6c31 646 lpos -= _rl_screenwidth;
37c41ab1 647#endif
d166f048 648 }
ccc6cda3 649
28ef6c31
JA
650 prompt_last_screen_line = newlines;
651
652 /* Draw the rest of the line (after the prompt) into invisible_line, keeping
653 track of where the cursor is (c_pos), the number of the line containing
654 the cursor (lb_linenum), the last line number (lb_botlin and inv_botlin).
655 It maintains an array of line breaks for display (inv_lbreaks).
656 This handles expanding tabs for display and displaying meta characters. */
d166f048 657 lb_linenum = 0;
7117c2d2
JA
658#if defined (HANDLE_MULTIBYTE)
659 in = 0;
660 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
661 {
662 memset (&ps, 0, sizeof (mbstate_t));
663 wc_bytes = mbrtowc (&wc, rl_line_buffer, rl_end, &ps);
664 }
665 else
666 wc_bytes = 1;
667 while (in < rl_end)
668#else
d166f048 669 for (in = 0; in < rl_end; in++)
7117c2d2 670#endif
726f6388
JA
671 {
672 c = (unsigned char)rl_line_buffer[in];
673
7117c2d2
JA
674#if defined (HANDLE_MULTIBYTE)
675 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
676 {
d3a24ed2 677 if (MB_INVALIDCH (wc_bytes))
7117c2d2
JA
678 {
679 /* Byte sequence is invalid or shortened. Assume that the
680 first byte represents a character. */
681 wc_bytes = 1;
682 /* Assume that a character occupies a single column. */
683 wc_width = 1;
684 memset (&ps, 0, sizeof (mbstate_t));
685 }
d3a24ed2 686 else if (MB_NULLWCH (wc_bytes))
7117c2d2
JA
687 break; /* Found '\0' */
688 else
689 {
690 temp = wcwidth (wc);
d3a24ed2 691 wc_width = (temp >= 0) ? temp : 1;
7117c2d2
JA
692 }
693 }
694#endif
695
726f6388
JA
696 if (out + 8 >= line_size) /* XXX - 8 for \t */
697 {
698 line_size *= 2;
f73dda09
JA
699 visible_line = (char *)xrealloc (visible_line, line_size);
700 invisible_line = (char *)xrealloc (invisible_line, line_size);
726f6388
JA
701 line = invisible_line;
702 }
703
704 if (in == rl_point)
ccc6cda3
JA
705 {
706 c_pos = out;
707 lb_linenum = newlines;
708 }
726f6388 709
7117c2d2
JA
710#if defined (HANDLE_MULTIBYTE)
711 if (META_CHAR (c) && _rl_output_meta_chars == 0) /* XXX - clean up */
712#else
726f6388 713 if (META_CHAR (c))
7117c2d2 714#endif
726f6388
JA
715 {
716 if (_rl_output_meta_chars == 0)
717 {
718 sprintf (line + out, "\\%o", c);
ccc6cda3 719
28ef6c31 720 if (lpos + 4 >= _rl_screenwidth)
ccc6cda3 721 {
28ef6c31 722 temp = _rl_screenwidth - lpos;
bb70624e 723 CHECK_INV_LBREAKS ();
ccc6cda3
JA
724 inv_lbreaks[++newlines] = out + temp;
725 lpos = 4 - temp;
726 }
727 else
728 lpos += 4;
729
726f6388
JA
730 out += 4;
731 }
732 else
ccc6cda3
JA
733 {
734 line[out++] = c;
735 CHECK_LPOS();
736 }
726f6388
JA
737 }
738#if defined (DISPLAY_TABS)
739 else if (c == '\t')
740 {
28ef6c31 741 register int newout;
b72432fd
JA
742
743#if 0
ccc6cda3 744 newout = (out | (int)7) + 1;
b72432fd
JA
745#else
746 newout = out + 8 - lpos % 8;
747#endif
ccc6cda3 748 temp = newout - out;
28ef6c31 749 if (lpos + temp >= _rl_screenwidth)
ccc6cda3
JA
750 {
751 register int temp2;
28ef6c31 752 temp2 = _rl_screenwidth - lpos;
bb70624e 753 CHECK_INV_LBREAKS ();
ccc6cda3
JA
754 inv_lbreaks[++newlines] = out + temp2;
755 lpos = temp - temp2;
756 while (out < newout)
757 line[out++] = ' ';
758 }
759 else
760 {
761 while (out < newout)
762 line[out++] = ' ';
763 lpos += temp;
764 }
726f6388
JA
765 }
766#endif
28ef6c31 767 else if (c == '\n' && _rl_horizontal_scroll_mode == 0 && _rl_term_up && *_rl_term_up)
b72432fd
JA
768 {
769 line[out++] = '\0'; /* XXX - sentinel */
bb70624e 770 CHECK_INV_LBREAKS ();
b72432fd
JA
771 inv_lbreaks[++newlines] = out;
772 lpos = 0;
773 }
ccc6cda3 774 else if (CTRL_CHAR (c) || c == RUBOUT)
726f6388
JA
775 {
776 line[out++] = '^';
ccc6cda3
JA
777 CHECK_LPOS();
778 line[out++] = CTRL_CHAR (c) ? UNCTRL (c) : '?';
779 CHECK_LPOS();
726f6388 780 }
ccc6cda3 781 else
726f6388 782 {
7117c2d2
JA
783#if defined (HANDLE_MULTIBYTE)
784 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
785 {
786 register int i;
787
788 _rl_wrapped_multicolumn = 0;
789
790 if (_rl_screenwidth < lpos + wc_width)
791 for (i = lpos; i < _rl_screenwidth; i++)
792 {
793 /* The space will be removed in update_line() */
794 line[out++] = ' ';
795 _rl_wrapped_multicolumn++;
796 CHECK_LPOS();
797 }
798 if (in == rl_point)
799 {
800 c_pos = out;
801 lb_linenum = newlines;
802 }
803 for (i = in; i < in+wc_bytes; i++)
804 line[out++] = rl_line_buffer[i];
805 for (i = 0; i < wc_width; i++)
806 CHECK_LPOS();
807 }
808 else
809 {
810 line[out++] = c;
811 CHECK_LPOS();
812 }
813#else
ccc6cda3
JA
814 line[out++] = c;
815 CHECK_LPOS();
7117c2d2
JA
816#endif
817 }
818
819#if defined (HANDLE_MULTIBYTE)
820 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
821 {
822 in += wc_bytes;
823 wc_bytes = mbrtowc (&wc, rl_line_buffer + in, rl_end - in, &ps);
726f6388 824 }
7117c2d2
JA
825 else
826 in++;
827#endif
828
726f6388
JA
829 }
830 line[out] = '\0';
831 if (c_pos < 0)
ccc6cda3
JA
832 {
833 c_pos = out;
834 lb_linenum = newlines;
835 }
836
837 inv_botlin = lb_botlin = newlines;
bb70624e 838 CHECK_INV_LBREAKS ();
ccc6cda3
JA
839 inv_lbreaks[newlines+1] = out;
840 cursor_linenum = lb_linenum;
726f6388 841
28ef6c31
JA
842 /* C_POS == position in buffer where cursor should be placed.
843 CURSOR_LINENUM == line number where the cursor should be placed. */
726f6388
JA
844
845 /* PWP: now is when things get a bit hairy. The visible and invisible
846 line buffers are really multiple lines, which would wrap every
847 (screenwidth - 1) characters. Go through each in turn, finding
848 the changed region and updating it. The line order is top to bottom. */
849
850 /* If we can move the cursor up and down, then use multiple lines,
851 otherwise, let long lines display in a single terminal line, and
852 horizontally scroll it. */
853
28ef6c31 854 if (_rl_horizontal_scroll_mode == 0 && _rl_term_up && *_rl_term_up)
726f6388 855 {
177d51f7 856 int nleft, pos, changed_screen_line, tx;
726f6388
JA
857
858 if (!rl_display_fixed || forced_display)
859 {
860 forced_display = 0;
861
862 /* If we have more than a screenful of material to display, then
863 only display a screenful. We should display the last screen,
ccc6cda3 864 not the first. */
28ef6c31 865 if (out >= _rl_screenchars)
7117c2d2
JA
866 {
867 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
868 out = _rl_find_prev_mbchar (line, _rl_screenchars, MB_FIND_ANY);
869 else
870 out = _rl_screenchars - 1;
871 }
726f6388
JA
872
873 /* The first line is at character position 0 in the buffer. The
ccc6cda3
JA
874 second and subsequent lines start at inv_lbreaks[N], offset by
875 OFFSET (which has already been calculated above). */
726f6388
JA
876
877#define W_OFFSET(line, offset) ((line) == 0 ? offset : 0)
ccc6cda3
JA
878#define VIS_LLEN(l) ((l) > _rl_vis_botlin ? 0 : (vis_lbreaks[l+1] - vis_lbreaks[l]))
879#define INV_LLEN(l) (inv_lbreaks[l+1] - inv_lbreaks[l])
880#define VIS_CHARS(line) (visible_line + vis_lbreaks[line])
726f6388 881#define VIS_LINE(line) ((line) > _rl_vis_botlin) ? "" : VIS_CHARS(line)
ccc6cda3 882#define INV_LINE(line) (invisible_line + inv_lbreaks[line])
726f6388
JA
883
884 /* For each line in the buffer, do the updating display. */
885 for (linenum = 0; linenum <= inv_botlin; linenum++)
886 {
887 update_line (VIS_LINE(linenum), INV_LINE(linenum), linenum,
ccc6cda3 888 VIS_LLEN(linenum), INV_LLEN(linenum), inv_botlin);
726f6388
JA
889
890 /* If this is the line with the prompt, we might need to
891 compensate for invisible characters in the new line. Do
892 this only if there is not more than one new line (which
893 implies that we completely overwrite the old visible line)
ccc6cda3
JA
894 and the new line is shorter than the old. Make sure we are
895 at the end of the new line before clearing. */
726f6388 896 if (linenum == 0 &&
ccc6cda3 897 inv_botlin == 0 && _rl_last_c_pos == out &&
726f6388
JA
898 (wrap_offset > visible_wrap_offset) &&
899 (_rl_last_c_pos < visible_first_line_len))
900 {
177d51f7
CR
901 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
902 nleft = _rl_screenwidth - _rl_last_c_pos;
903 else
904 nleft = _rl_screenwidth + wrap_offset - _rl_last_c_pos;
726f6388 905 if (nleft)
d166f048 906 _rl_clear_to_eol (nleft);
726f6388
JA
907 }
908
909 /* Since the new first line is now visible, save its length. */
910 if (linenum == 0)
ccc6cda3 911 visible_first_line_len = (inv_botlin > 0) ? inv_lbreaks[1] : out - wrap_offset;
726f6388
JA
912 }
913
914 /* We may have deleted some lines. If so, clear the left over
915 blank ones at the bottom out. */
916 if (_rl_vis_botlin > inv_botlin)
917 {
918 char *tt;
919 for (; linenum <= _rl_vis_botlin; linenum++)
920 {
921 tt = VIS_CHARS (linenum);
922 _rl_move_vert (linenum);
923 _rl_move_cursor_relative (0, tt);
d166f048 924 _rl_clear_to_eol
28ef6c31 925 ((linenum == _rl_vis_botlin) ? strlen (tt) : _rl_screenwidth);
726f6388
JA
926 }
927 }
928 _rl_vis_botlin = inv_botlin;
929
726f6388
JA
930 /* CHANGED_SCREEN_LINE is set to 1 if we have moved to a
931 different screen line during this redisplay. */
932 changed_screen_line = _rl_last_v_pos != cursor_linenum;
933 if (changed_screen_line)
934 {
935 _rl_move_vert (cursor_linenum);
28ef6c31 936 /* If we moved up to the line with the prompt using _rl_term_up,
b72432fd
JA
937 the physical cursor position on the screen stays the same,
938 but the buffer position needs to be adjusted to account
939 for invisible characters. */
177d51f7 940 if ((MB_CUR_MAX == 1 || rl_byte_oriented) && cursor_linenum == 0 && wrap_offset)
b72432fd 941 _rl_last_c_pos += wrap_offset;
726f6388
JA
942 }
943
944 /* We have to reprint the prompt if it contains invisible
945 characters, since it's not generally OK to just reprint
ccc6cda3
JA
946 the characters from the current cursor position. But we
947 only need to reprint it if the cursor is before the last
948 invisible character in the prompt string. */
28ef6c31 949 nleft = prompt_visible_length + wrap_offset;
726f6388 950 if (cursor_linenum == 0 && wrap_offset > 0 && _rl_last_c_pos > 0 &&
28ef6c31 951 _rl_last_c_pos <= prompt_last_invisible && local_prompt)
726f6388 952 {
bb70624e
JA
953#if defined (__MSDOS__)
954 putc ('\r', rl_outstream);
955#else
28ef6c31
JA
956 if (_rl_term_cr)
957 tputs (_rl_term_cr, 1, _rl_output_character_function);
bb70624e 958#endif
726f6388 959 _rl_output_some_chars (local_prompt, nleft);
7117c2d2 960 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
5e13499c 961 _rl_last_c_pos = _rl_col_width (local_prompt, 0, nleft);
7117c2d2
JA
962 else
963 _rl_last_c_pos = nleft;
726f6388
JA
964 }
965
966 /* Where on that line? And where does that line start
967 in the buffer? */
ccc6cda3 968 pos = inv_lbreaks[cursor_linenum];
726f6388
JA
969 /* nleft == number of characters in the line buffer between the
970 start of the line and the cursor position. */
971 nleft = c_pos - pos;
972
ccc6cda3 973 /* Since _rl_backspace() doesn't know about invisible characters in the
726f6388 974 prompt, and there's no good way to tell it, we compensate for
ccc6cda3 975 those characters here and call _rl_backspace() directly. */
726f6388
JA
976 if (wrap_offset && cursor_linenum == 0 && nleft < _rl_last_c_pos)
977 {
7117c2d2 978 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
177d51f7 979 tx = _rl_col_width (&visible_line[pos], 0, nleft);
7117c2d2 980 else
177d51f7 981 tx = nleft;
d11b8b46
CR
982 if (_rl_last_c_pos != tx)
983 {
984 _rl_backspace (_rl_last_c_pos - tx); /* XXX */
985 _rl_last_c_pos = tx;
986 }
726f6388
JA
987 }
988
7117c2d2
JA
989 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
990 _rl_move_cursor_relative (nleft, &invisible_line[pos]);
991 else if (nleft != _rl_last_c_pos)
726f6388
JA
992 _rl_move_cursor_relative (nleft, &invisible_line[pos]);
993 }
994 }
995 else /* Do horizontal scrolling. */
996 {
997#define M_OFFSET(margin, offset) ((margin) == 0 ? offset : 0)
998 int lmargin, ndisp, nleft, phys_c_pos, t;
999
1000 /* Always at top line. */
1001 _rl_last_v_pos = 0;
1002
1003 /* Compute where in the buffer the displayed line should start. This
1004 will be LMARGIN. */
1005
1006 /* The number of characters that will be displayed before the cursor. */
1007 ndisp = c_pos - wrap_offset;
28ef6c31 1008 nleft = prompt_visible_length + wrap_offset;
726f6388 1009 /* Where the new cursor position will be on the screen. This can be
b72432fd 1010 longer than SCREENWIDTH; if it is, lmargin will be adjusted. */
726f6388 1011 phys_c_pos = c_pos - (last_lmargin ? last_lmargin : wrap_offset);
28ef6c31 1012 t = _rl_screenwidth / 3;
726f6388
JA
1013
1014 /* If the number of characters had already exceeded the screenwidth,
b72432fd 1015 last_lmargin will be > 0. */
726f6388
JA
1016
1017 /* If the number of characters to be displayed is more than the screen
b72432fd
JA
1018 width, compute the starting offset so that the cursor is about
1019 two-thirds of the way across the screen. */
28ef6c31 1020 if (phys_c_pos > _rl_screenwidth - 2)
726f6388
JA
1021 {
1022 lmargin = c_pos - (2 * t);
1023 if (lmargin < 0)
1024 lmargin = 0;
1025 /* If the left margin would be in the middle of a prompt with
1026 invisible characters, don't display the prompt at all. */
1027 if (wrap_offset && lmargin > 0 && lmargin < nleft)
1028 lmargin = nleft;
1029 }
28ef6c31 1030 else if (ndisp < _rl_screenwidth - 2) /* XXX - was -1 */
b72432fd 1031 lmargin = 0;
726f6388
JA
1032 else if (phys_c_pos < 1)
1033 {
1034 /* If we are moving back towards the beginning of the line and
1035 the last margin is no longer correct, compute a new one. */
1036 lmargin = ((c_pos - 1) / t) * t; /* XXX */
1037 if (wrap_offset && lmargin > 0 && lmargin < nleft)
1038 lmargin = nleft;
1039 }
1040 else
b72432fd 1041 lmargin = last_lmargin;
726f6388
JA
1042
1043 /* If the first character on the screen isn't the first character
1044 in the display line, indicate this with a special character. */
1045 if (lmargin > 0)
1046 line[lmargin] = '<';
1047
1048 /* If SCREENWIDTH characters starting at LMARGIN do not encompass
b72432fd
JA
1049 the whole line, indicate that with a special character at the
1050 right edge of the screen. If LMARGIN is 0, we need to take the
1051 wrap offset into account. */
28ef6c31 1052 t = lmargin + M_OFFSET (lmargin, wrap_offset) + _rl_screenwidth;
726f6388 1053 if (t < out)
b72432fd 1054 line[t - 1] = '>';
726f6388
JA
1055
1056 if (!rl_display_fixed || forced_display || lmargin != last_lmargin)
1057 {
1058 forced_display = 0;
1059 update_line (&visible_line[last_lmargin],
1060 &invisible_line[lmargin],
1061 0,
28ef6c31
JA
1062 _rl_screenwidth + visible_wrap_offset,
1063 _rl_screenwidth + (lmargin ? 0 : wrap_offset),
726f6388
JA
1064 0);
1065
1066 /* If the visible new line is shorter than the old, but the number
1067 of invisible characters is greater, and we are at the end of
1068 the new line, we need to clear to eol. */
1069 t = _rl_last_c_pos - M_OFFSET (lmargin, wrap_offset);
1070 if ((M_OFFSET (lmargin, wrap_offset) > visible_wrap_offset) &&
1071 (_rl_last_c_pos == out) &&
1072 t < visible_first_line_len)
1073 {
28ef6c31 1074 nleft = _rl_screenwidth - t;
d166f048 1075 _rl_clear_to_eol (nleft);
726f6388
JA
1076 }
1077 visible_first_line_len = out - lmargin - M_OFFSET (lmargin, wrap_offset);
28ef6c31
JA
1078 if (visible_first_line_len > _rl_screenwidth)
1079 visible_first_line_len = _rl_screenwidth;
726f6388
JA
1080
1081 _rl_move_cursor_relative (c_pos - lmargin, &invisible_line[lmargin]);
1082 last_lmargin = lmargin;
1083 }
1084 }
1085 fflush (rl_outstream);
1086
1087 /* Swap visible and non-visible lines. */
1088 {
28ef6c31 1089 char *vtemp = visible_line;
bb70624e
JA
1090 int *itemp = vis_lbreaks, ntemp = vis_lbsize;
1091
726f6388 1092 visible_line = invisible_line;
28ef6c31 1093 invisible_line = vtemp;
bb70624e 1094
ccc6cda3
JA
1095 vis_lbreaks = inv_lbreaks;
1096 inv_lbreaks = itemp;
bb70624e
JA
1097
1098 vis_lbsize = inv_lbsize;
1099 inv_lbsize = ntemp;
1100
726f6388
JA
1101 rl_display_fixed = 0;
1102 /* If we are displaying on a single line, and last_lmargin is > 0, we
1103 are not displaying any invisible characters, so set visible_wrap_offset
1104 to 0. */
1105 if (_rl_horizontal_scroll_mode && last_lmargin)
1106 visible_wrap_offset = 0;
1107 else
1108 visible_wrap_offset = wrap_offset;
1109 }
1110}
1111
1112/* PWP: update_line() is based on finding the middle difference of each
1113 line on the screen; vis:
1114
1115 /old first difference
1116 /beginning of line | /old last same /old EOL
1117 v v v v
1118old: eddie> Oh, my little gruntle-buggy is to me, as lurgid as
1119new: eddie> Oh, my little buggy says to me, as lurgid as
1120 ^ ^ ^ ^
1121 \beginning of line | \new last same \new end of line
1122 \new first difference
1123
1124 All are character pointers for the sake of speed. Special cases for
b72432fd 1125 no differences, as well as for end of line additions must be handled.
726f6388
JA
1126
1127 Could be made even smarter, but this works well enough */
1128static void
1129update_line (old, new, current_line, omax, nmax, inv_botlin)
1130 register char *old, *new;
ccc6cda3 1131 int current_line, omax, nmax, inv_botlin;
726f6388
JA
1132{
1133 register char *ofd, *ols, *oe, *nfd, *nls, *ne;
1134 int temp, lendiff, wsatend, od, nd;
ccc6cda3 1135 int current_invis_chars;
7117c2d2
JA
1136 int col_lendiff, col_temp;
1137#if defined (HANDLE_MULTIBYTE)
1138 mbstate_t ps_new, ps_old;
1139 int new_offset, old_offset, tmp;
1140#endif
726f6388
JA
1141
1142 /* If we're at the right edge of a terminal that supports xn, we're
1143 ready to wrap around, so do so. This fixes problems with knowing
1144 the exact cursor position and cut-and-paste with certain terminal
1145 emulators. In this calculation, TEMP is the physical screen
1146 position of the cursor. */
177d51f7
CR
1147 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1148 temp = _rl_last_c_pos;
1149 else
1150 temp = _rl_last_c_pos - W_OFFSET(_rl_last_v_pos, visible_wrap_offset);
28ef6c31 1151 if (temp == _rl_screenwidth && _rl_term_autowrap && !_rl_horizontal_scroll_mode
7117c2d2 1152 && _rl_last_v_pos == current_line - 1)
726f6388 1153 {
7117c2d2
JA
1154#if defined (HANDLE_MULTIBYTE)
1155 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1156 {
1157 wchar_t wc;
1158 mbstate_t ps;
1159 int tempwidth, bytes;
1160 size_t ret;
1161
1162 /* This fixes only double-column characters, but if the wrapped
1163 character comsumes more than three columns, spaces will be
1164 inserted in the string buffer. */
1165 if (_rl_wrapped_line[current_line] > 0)
1166 _rl_clear_to_eol (_rl_wrapped_line[current_line]);
1167
1168 memset (&ps, 0, sizeof (mbstate_t));
1169 ret = mbrtowc (&wc, new, MB_CUR_MAX, &ps);
d3a24ed2 1170 if (MB_INVALIDCH (ret))
7117c2d2
JA
1171 {
1172 tempwidth = 1;
1173 ret = 1;
1174 }
d3a24ed2 1175 else if (MB_NULLWCH (ret))
7117c2d2
JA
1176 tempwidth = 0;
1177 else
1178 tempwidth = wcwidth (wc);
1179
1180 if (tempwidth > 0)
1181 {
1182 int count;
1183 bytes = ret;
1184 for (count = 0; count < bytes; count++)
1185 putc (new[count], rl_outstream);
1186 _rl_last_c_pos = tempwidth;
1187 _rl_last_v_pos++;
1188 memset (&ps, 0, sizeof (mbstate_t));
1189 ret = mbrtowc (&wc, old, MB_CUR_MAX, &ps);
1190 if (ret != 0 && bytes != 0)
1191 {
d3a24ed2 1192 if (MB_INVALIDCH (ret))
7117c2d2
JA
1193 memmove (old+bytes, old+1, strlen (old+1));
1194 else
1195 memmove (old+bytes, old+ret, strlen (old+ret));
1196 memcpy (old, new, bytes);
1197 }
1198 }
1199 else
1200 {
1201 putc (' ', rl_outstream);
1202 _rl_last_c_pos = 1;
1203 _rl_last_v_pos++;
1204 if (old[0] && new[0])
1205 old[0] = new[0];
1206 }
1207 }
726f6388 1208 else
7117c2d2
JA
1209#endif
1210 {
1211 if (new[0])
1212 putc (new[0], rl_outstream);
1213 else
1214 putc (' ', rl_outstream);
1215 _rl_last_c_pos = 1; /* XXX */
1216 _rl_last_v_pos++;
1217 if (old[0] && new[0])
1218 old[0] = new[0];
1219 }
726f6388 1220 }
7117c2d2 1221
726f6388
JA
1222
1223 /* Find first difference. */
7117c2d2
JA
1224#if defined (HANDLE_MULTIBYTE)
1225 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1226 {
5e13499c
CR
1227 /* See if the old line is a subset of the new line, so that the
1228 only change is adding characters. */
1229 temp = (omax < nmax) ? omax : nmax;
1230 if (memcmp (old, new, temp) == 0)
7117c2d2 1231 {
5e13499c
CR
1232 ofd = old + temp;
1233 nfd = new + temp;
d3a24ed2
CR
1234 }
1235 else
5e13499c
CR
1236 {
1237 memset (&ps_new, 0, sizeof(mbstate_t));
1238 memset (&ps_old, 0, sizeof(mbstate_t));
1239
1240 if (omax == nmax && STREQN (new, old, omax))
d3a24ed2 1241 {
5e13499c
CR
1242 ofd = old + omax;
1243 nfd = new + nmax;
1244 }
1245 else
1246 {
1247 new_offset = old_offset = 0;
1248 for (ofd = old, nfd = new;
1249 (ofd - old < omax) && *ofd &&
1250 _rl_compare_chars(old, old_offset, &ps_old, new, new_offset, &ps_new); )
1251 {
1252 old_offset = _rl_find_next_mbchar (old, old_offset, 1, MB_FIND_ANY);
1253 new_offset = _rl_find_next_mbchar (new, new_offset, 1, MB_FIND_ANY);
1254 ofd = old + old_offset;
1255 nfd = new + new_offset;
1256 }
d3a24ed2 1257 }
7117c2d2
JA
1258 }
1259 }
1260 else
1261#endif
726f6388
JA
1262 for (ofd = old, nfd = new;
1263 (ofd - old < omax) && *ofd && (*ofd == *nfd);
1264 ofd++, nfd++)
1265 ;
1266
1267 /* Move to the end of the screen line. ND and OD are used to keep track
1268 of the distance between ne and new and oe and old, respectively, to
1269 move a subtraction out of each loop. */
1270 for (od = ofd - old, oe = ofd; od < omax && *oe; oe++, od++);
1271 for (nd = nfd - new, ne = nfd; nd < nmax && *ne; ne++, nd++);
1272
1273 /* If no difference, continue to next line. */
1274 if (ofd == oe && nfd == ne)
1275 return;
1276
1277 wsatend = 1; /* flag for trailing whitespace */
7117c2d2
JA
1278
1279#if defined (HANDLE_MULTIBYTE)
1280 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1281 {
1282 ols = old + _rl_find_prev_mbchar (old, oe - old, MB_FIND_ANY);
1283 nls = new + _rl_find_prev_mbchar (new, ne - new, MB_FIND_ANY);
1284 while ((ols > ofd) && (nls > nfd))
1285 {
1286 memset (&ps_old, 0, sizeof (mbstate_t));
1287 memset (&ps_new, 0, sizeof (mbstate_t));
1288
d3a24ed2
CR
1289#if 0
1290 /* On advice from jir@yamato.ibm.com */
7117c2d2
JA
1291 _rl_adjust_point (old, ols - old, &ps_old);
1292 _rl_adjust_point (new, nls - new, &ps_new);
d3a24ed2 1293#endif
7117c2d2
JA
1294
1295 if (_rl_compare_chars (old, ols - old, &ps_old, new, nls - new, &ps_new) == 0)
1296 break;
1297
1298 if (*ols == ' ')
1299 wsatend = 0;
1300
1301 ols = old + _rl_find_prev_mbchar (old, ols - old, MB_FIND_ANY);
1302 nls = new + _rl_find_prev_mbchar (new, nls - new, MB_FIND_ANY);
1303 }
1304 }
1305 else
1306 {
1307#endif /* HANDLE_MULTIBYTE */
726f6388
JA
1308 ols = oe - 1; /* find last same */
1309 nls = ne - 1;
1310 while ((ols > ofd) && (nls > nfd) && (*ols == *nls))
1311 {
1312 if (*ols != ' ')
1313 wsatend = 0;
1314 ols--;
1315 nls--;
1316 }
7117c2d2
JA
1317#if defined (HANDLE_MULTIBYTE)
1318 }
1319#endif
726f6388
JA
1320
1321 if (wsatend)
1322 {
1323 ols = oe;
1324 nls = ne;
1325 }
7117c2d2
JA
1326#if defined (HANDLE_MULTIBYTE)
1327 /* This may not work for stateful encoding, but who cares? To handle
1328 stateful encoding properly, we have to scan each string from the
1329 beginning and compare. */
1330 else if (_rl_compare_chars (ols, 0, NULL, nls, 0, NULL) == 0)
1331#else
726f6388 1332 else if (*ols != *nls)
7117c2d2 1333#endif
726f6388
JA
1334 {
1335 if (*ols) /* don't step past the NUL */
7117c2d2
JA
1336 {
1337 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1338 ols = old + _rl_find_next_mbchar (old, ols - old, 1, MB_FIND_ANY);
1339 else
1340 ols++;
1341 }
726f6388 1342 if (*nls)
7117c2d2
JA
1343 {
1344 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1345 nls = new + _rl_find_next_mbchar (new, nls - new, 1, MB_FIND_ANY);
1346 else
1347 nls++;
1348 }
726f6388
JA
1349 }
1350
ccc6cda3
JA
1351 /* count of invisible characters in the current invisible line. */
1352 current_invis_chars = W_OFFSET (current_line, wrap_offset);
1353 if (_rl_last_v_pos != current_line)
1354 {
1355 _rl_move_vert (current_line);
177d51f7 1356 if ((MB_CUR_MAX == 1 || rl_byte_oriented) && current_line == 0 && visible_wrap_offset)
ccc6cda3
JA
1357 _rl_last_c_pos += visible_wrap_offset;
1358 }
726f6388
JA
1359
1360 /* If this is the first line and there are invisible characters in the
ccc6cda3
JA
1361 prompt string, and the prompt string has not changed, and the current
1362 cursor position is before the last invisible character in the prompt,
1363 and the index of the character to move to is past the end of the prompt
1364 string, then redraw the entire prompt string. We can only do this
1365 reliably if the terminal supports a `cr' capability.
726f6388 1366
ccc6cda3
JA
1367 This is not an efficiency hack -- there is a problem with redrawing
1368 portions of the prompt string if they contain terminal escape
1369 sequences (like drawing the `unbold' sequence without a corresponding
1370 `bold') that manifests itself on certain terminals. */
726f6388
JA
1371
1372 lendiff = local_prompt ? strlen (local_prompt) : 0;
ccc6cda3 1373 od = ofd - old; /* index of first difference in visible line */
726f6388 1374 if (current_line == 0 && !_rl_horizontal_scroll_mode &&
28ef6c31
JA
1375 _rl_term_cr && lendiff > prompt_visible_length && _rl_last_c_pos > 0 &&
1376 od >= lendiff && _rl_last_c_pos <= prompt_last_invisible)
726f6388 1377 {
bb70624e
JA
1378#if defined (__MSDOS__)
1379 putc ('\r', rl_outstream);
1380#else
28ef6c31 1381 tputs (_rl_term_cr, 1, _rl_output_character_function);
bb70624e 1382#endif
726f6388 1383 _rl_output_some_chars (local_prompt, lendiff);
7117c2d2
JA
1384 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1385 _rl_last_c_pos = _rl_col_width (local_prompt, 0, lendiff);
1386 else
1387 _rl_last_c_pos = lendiff;
726f6388
JA
1388 }
1389
ccc6cda3 1390 _rl_move_cursor_relative (od, old);
726f6388 1391
7117c2d2
JA
1392 /* if (len (new) > len (old))
1393 lendiff == difference in buffer
1394 col_lendiff == difference on screen
1395 When not using multibyte characters, these are equal */
726f6388 1396 lendiff = (nls - nfd) - (ols - ofd);
7117c2d2
JA
1397 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1398 col_lendiff = _rl_col_width (new, nfd - new, nls - new) - _rl_col_width (old, ofd - old, ols - old);
1399 else
1400 col_lendiff = lendiff;
726f6388 1401
ccc6cda3
JA
1402 /* If we are changing the number of invisible characters in a line, and
1403 the spot of first difference is before the end of the invisible chars,
1404 lendiff needs to be adjusted. */
1405 if (current_line == 0 && !_rl_horizontal_scroll_mode &&
1406 current_invis_chars != visible_wrap_offset)
7117c2d2
JA
1407 {
1408 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1409 {
1410 lendiff += visible_wrap_offset - current_invis_chars;
1411 col_lendiff += visible_wrap_offset - current_invis_chars;
1412 }
1413 else
1414 {
1415 lendiff += visible_wrap_offset - current_invis_chars;
1416 col_lendiff = lendiff;
1417 }
1418 }
ccc6cda3 1419
726f6388
JA
1420 /* Insert (diff (len (old), len (new)) ch. */
1421 temp = ne - nfd;
7117c2d2
JA
1422 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1423 col_temp = _rl_col_width (new, nfd - new, ne - new);
1424 else
1425 col_temp = temp;
1426
1427 if (col_lendiff > 0) /* XXX - was lendiff */
726f6388
JA
1428 {
1429 /* Non-zero if we're increasing the number of lines. */
1430 int gl = current_line >= _rl_vis_botlin && inv_botlin > _rl_vis_botlin;
1431 /* Sometimes it is cheaper to print the characters rather than
1432 use the terminal's capabilities. If we're growing the number
1433 of lines, make sure we actually cause the new line to wrap
1434 around on auto-wrapping terminals. */
7117c2d2 1435 if (_rl_terminal_can_insert && ((2 * col_temp) >= col_lendiff || _rl_term_IC) && (!_rl_term_autowrap || !gl))
726f6388 1436 {
28ef6c31 1437 /* If lendiff > prompt_visible_length and _rl_last_c_pos == 0 and
726f6388 1438 _rl_horizontal_scroll_mode == 1, inserting the characters with
28ef6c31 1439 _rl_term_IC or _rl_term_ic will screw up the screen because of the
726f6388
JA
1440 invisible characters. We need to just draw them. */
1441 if (*ols && (!_rl_horizontal_scroll_mode || _rl_last_c_pos > 0 ||
28ef6c31 1442 lendiff <= prompt_visible_length || !current_invis_chars))
726f6388 1443 {
7117c2d2
JA
1444 insert_some_chars (nfd, lendiff, col_lendiff);
1445 _rl_last_c_pos += col_lendiff;
726f6388 1446 }
d90269dd 1447 else if ((MB_CUR_MAX == 1 || rl_byte_oriented != 0) && *ols == 0 && lendiff > 0)
726f6388
JA
1448 {
1449 /* At the end of a line the characters do not have to
1450 be "inserted". They can just be placed on the screen. */
ccc6cda3 1451 /* However, this screws up the rest of this block, which
b72432fd 1452 assumes you've done the insert because you can. */
726f6388 1453 _rl_output_some_chars (nfd, lendiff);
7117c2d2 1454 _rl_last_c_pos += col_lendiff;
726f6388 1455 }
ccc6cda3
JA
1456 else
1457 {
1458 /* We have horizontal scrolling and we are not inserting at
1459 the end. We have invisible characters in this line. This
1460 is a dumb update. */
1461 _rl_output_some_chars (nfd, temp);
7117c2d2 1462 _rl_last_c_pos += col_temp;
ccc6cda3
JA
1463 return;
1464 }
726f6388
JA
1465 /* Copy (new) chars to screen from first diff to last match. */
1466 temp = nls - nfd;
1467 if ((temp - lendiff) > 0)
1468 {
1469 _rl_output_some_chars (nfd + lendiff, temp - lendiff);
5e13499c
CR
1470#if 1
1471 /* XXX -- this bears closer inspection. Fixes a redisplay bug
1472 reported against bash-3.0-alpha by Andreas Schwab involving
1473 multibyte characters and prompt strings with invisible
1474 characters, but was previously disabled. */
7117c2d2 1475 _rl_last_c_pos += _rl_col_width (nfd+lendiff, 0, temp-col_lendiff);
d3a24ed2
CR
1476#else
1477 _rl_last_c_pos += _rl_col_width (nfd+lendiff, 0, temp-lendiff);
7117c2d2 1478#endif
726f6388
JA
1479 }
1480 }
1481 else
1482 {
1483 /* cannot insert chars, write to EOL */
1484 _rl_output_some_chars (nfd, temp);
7117c2d2 1485 _rl_last_c_pos += col_temp;
726f6388
JA
1486 }
1487 }
1488 else /* Delete characters from line. */
1489 {
1490 /* If possible and inexpensive to use terminal deletion, then do so. */
7117c2d2 1491 if (_rl_term_dc && (2 * col_temp) >= -col_lendiff)
726f6388
JA
1492 {
1493 /* If all we're doing is erasing the invisible characters in the
1494 prompt string, don't bother. It screws up the assumptions
1495 about what's on the screen. */
1496 if (_rl_horizontal_scroll_mode && _rl_last_c_pos == 0 &&
1497 -lendiff == visible_wrap_offset)
7117c2d2 1498 col_lendiff = 0;
726f6388 1499
7117c2d2
JA
1500 if (col_lendiff)
1501 delete_chars (-col_lendiff); /* delete (diff) characters */
726f6388
JA
1502
1503 /* Copy (new) chars to screen from first diff to last match */
1504 temp = nls - nfd;
1505 if (temp > 0)
1506 {
1507 _rl_output_some_chars (nfd, temp);
7117c2d2 1508 _rl_last_c_pos += _rl_col_width (nfd, 0, temp);;
726f6388
JA
1509 }
1510 }
1511 /* Otherwise, print over the existing material. */
1512 else
1513 {
1514 if (temp > 0)
1515 {
1516 _rl_output_some_chars (nfd, temp);
7117c2d2 1517 _rl_last_c_pos += col_temp;
726f6388
JA
1518 }
1519 lendiff = (oe - old) - (ne - new);
7117c2d2
JA
1520 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1521 col_lendiff = _rl_col_width (old, 0, oe - old) - _rl_col_width (new, 0, ne - new);
1522 else
1523 col_lendiff = lendiff;
1524
1525 if (col_lendiff)
b72432fd
JA
1526 {
1527 if (_rl_term_autowrap && current_line < inv_botlin)
7117c2d2 1528 space_to_eol (col_lendiff);
b72432fd 1529 else
7117c2d2 1530 _rl_clear_to_eol (col_lendiff);
b72432fd 1531 }
726f6388
JA
1532 }
1533 }
1534}
1535
1536/* Tell the update routines that we have moved onto a new (empty) line. */
ccc6cda3 1537int
726f6388
JA
1538rl_on_new_line ()
1539{
1540 if (visible_line)
1541 visible_line[0] = '\0';
1542
1543 _rl_last_c_pos = _rl_last_v_pos = 0;
1544 _rl_vis_botlin = last_lmargin = 0;
ccc6cda3
JA
1545 if (vis_lbreaks)
1546 vis_lbreaks[0] = vis_lbreaks[1] = 0;
1547 visible_wrap_offset = 0;
726f6388
JA
1548 return 0;
1549}
1550
bb70624e
JA
1551/* Tell the update routines that we have moved onto a new line with the
1552 prompt already displayed. Code originally from the version of readline
61deeb13
CR
1553 distributed with CLISP. rl_expand_prompt must have already been called
1554 (explicitly or implicitly). This still doesn't work exactly right. */
bb70624e
JA
1555int
1556rl_on_new_line_with_prompt ()
1557{
1558 int prompt_size, i, l, real_screenwidth, newlines;
61deeb13 1559 char *prompt_last_line, *lprompt;
bb70624e
JA
1560
1561 /* Initialize visible_line and invisible_line to ensure that they can hold
1562 the already-displayed prompt. */
1563 prompt_size = strlen (rl_prompt) + 1;
1564 init_line_structures (prompt_size);
1565
1566 /* Make sure the line structures hold the already-displayed prompt for
1567 redisplay. */
61deeb13
CR
1568 lprompt = local_prompt ? local_prompt : rl_prompt;
1569 strcpy (visible_line, lprompt);
1570 strcpy (invisible_line, lprompt);
bb70624e
JA
1571
1572 /* If the prompt contains newlines, take the last tail. */
1573 prompt_last_line = strrchr (rl_prompt, '\n');
1574 if (!prompt_last_line)
1575 prompt_last_line = rl_prompt;
1576
1577 l = strlen (prompt_last_line);
7117c2d2
JA
1578 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1579 _rl_last_c_pos = _rl_col_width (prompt_last_line, 0, l);
1580 else
1581 _rl_last_c_pos = l;
bb70624e
JA
1582
1583 /* Dissect prompt_last_line into screen lines. Note that here we have
1584 to use the real screenwidth. Readline's notion of screenwidth might be
1585 one less, see terminal.c. */
28ef6c31 1586 real_screenwidth = _rl_screenwidth + (_rl_term_autowrap ? 0 : 1);
bb70624e
JA
1587 _rl_last_v_pos = l / real_screenwidth;
1588 /* If the prompt length is a multiple of real_screenwidth, we don't know
1589 whether the cursor is at the end of the last line, or already at the
1590 beginning of the next line. Output a newline just to be safe. */
1591 if (l > 0 && (l % real_screenwidth) == 0)
1592 _rl_output_some_chars ("\n", 1);
1593 last_lmargin = 0;
1594
1595 newlines = 0; i = 0;
1596 while (i <= l)
1597 {
1598 _rl_vis_botlin = newlines;
1599 vis_lbreaks[newlines++] = i;
1600 i += real_screenwidth;
1601 }
1602 vis_lbreaks[newlines] = l;
1603 visible_wrap_offset = 0;
1604
61deeb13
CR
1605 rl_display_prompt = rl_prompt; /* XXX - make sure it's set */
1606
bb70624e
JA
1607 return 0;
1608}
1609
726f6388 1610/* Actually update the display, period. */
ccc6cda3 1611int
726f6388
JA
1612rl_forced_update_display ()
1613{
1614 if (visible_line)
1615 {
1616 register char *temp = visible_line;
1617
ccc6cda3 1618 while (*temp)
b72432fd 1619 *temp++ = '\0';
726f6388
JA
1620 }
1621 rl_on_new_line ();
1622 forced_display++;
ccc6cda3 1623 (*rl_redisplay_function) ();
726f6388
JA
1624 return 0;
1625}
1626
1627/* Move the cursor from _rl_last_c_pos to NEW, which are buffer indices.
1628 DATA is the contents of the screen line of interest; i.e., where
1629 the movement is being done. */
1630void
1631_rl_move_cursor_relative (new, data)
1632 int new;
28ef6c31 1633 const char *data;
726f6388
JA
1634{
1635 register int i;
1636
1637 /* If we don't have to do anything, then return. */
7117c2d2
JA
1638#if defined (HANDLE_MULTIBYTE)
1639 /* If we have multibyte characters, NEW is indexed by the buffer point in
1640 a multibyte string, but _rl_last_c_pos is the display position. In
d3a24ed2
CR
1641 this case, NEW's display position is not obvious and must be
1642 calculated. */
1643 if (MB_CUR_MAX == 1 || rl_byte_oriented)
1644 {
1645 if (_rl_last_c_pos == new)
1646 return;
1647 }
1648 else if (_rl_last_c_pos == _rl_col_width (data, 0, new))
1649 return;
7117c2d2 1650#else
726f6388 1651 if (_rl_last_c_pos == new) return;
7117c2d2 1652#endif
726f6388
JA
1653
1654 /* It may be faster to output a CR, and then move forwards instead
1655 of moving backwards. */
1656 /* i == current physical cursor position. */
1657 i = _rl_last_c_pos - W_OFFSET(_rl_last_v_pos, visible_wrap_offset);
ccc6cda3 1658 if (new == 0 || CR_FASTER (new, _rl_last_c_pos) ||
28ef6c31 1659 (_rl_term_autowrap && i == _rl_screenwidth))
726f6388
JA
1660 {
1661#if defined (__MSDOS__)
1662 putc ('\r', rl_outstream);
1663#else
28ef6c31 1664 tputs (_rl_term_cr, 1, _rl_output_character_function);
726f6388
JA
1665#endif /* !__MSDOS__ */
1666 _rl_last_c_pos = 0;
1667 }
1668
1669 if (_rl_last_c_pos < new)
1670 {
1671 /* Move the cursor forward. We do it by printing the command
1672 to move the cursor forward if there is one, else print that
1673 portion of the output buffer again. Which is cheaper? */
1674
1675 /* The above comment is left here for posterity. It is faster
1676 to print one character (non-control) than to print a control
1677 sequence telling the terminal to move forward one character.
1678 That kind of control is for people who don't know what the
1679 data is underneath the cursor. */
1680#if defined (HACK_TERMCAP_MOTION)
28ef6c31 1681 if (_rl_term_forward_char)
7117c2d2
JA
1682 {
1683 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1684 {
1685 int width;
1686 width = _rl_col_width (data, _rl_last_c_pos, new);
1687 for (i = 0; i < width; i++)
1688 tputs (_rl_term_forward_char, 1, _rl_output_character_function);
1689 }
1690 else
1691 {
1692 for (i = _rl_last_c_pos; i < new; i++)
1693 tputs (_rl_term_forward_char, 1, _rl_output_character_function);
1694 }
1695 }
1696 else if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1697 {
1698 tputs (_rl_term_cr, 1, _rl_output_character_function);
1699 for (i = 0; i < new; i++)
1700 putc (data[i], rl_outstream);
1701 }
1702 else
726f6388 1703 for (i = _rl_last_c_pos; i < new; i++)
7117c2d2
JA
1704 putc (data[i], rl_outstream);
1705
1706#else /* !HACK_TERMCAP_MOTION */
1707
1708 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1709 {
1710 tputs (_rl_term_cr, 1, _rl_output_character_function);
1711 for (i = 0; i < new; i++)
1712 putc (data[i], rl_outstream);
1713 }
726f6388
JA
1714 else
1715 for (i = _rl_last_c_pos; i < new; i++)
1716 putc (data[i], rl_outstream);
7117c2d2
JA
1717
1718#endif /* !HACK_TERMCAP_MOTION */
1719
726f6388 1720 }
7117c2d2
JA
1721#if defined (HANDLE_MULTIBYTE)
1722 /* NEW points to the buffer point, but _rl_last_c_pos is the display point.
1723 The byte length of the string is probably bigger than the column width
1724 of the string, which means that if NEW == _rl_last_c_pos, then NEW's
1725 display point is less than _rl_last_c_pos. */
1726 else if (_rl_last_c_pos >= new)
1727#else
b72432fd 1728 else if (_rl_last_c_pos > new)
7117c2d2
JA
1729#endif
1730 {
1731 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
d3a24ed2 1732 _rl_backspace (_rl_last_c_pos - _rl_col_width (data, 0, new));
7117c2d2
JA
1733 else
1734 _rl_backspace (_rl_last_c_pos - new);
1735 }
1736
1737 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1738 _rl_last_c_pos = _rl_col_width (data, 0, new);
1739 else
1740 _rl_last_c_pos = new;
726f6388
JA
1741}
1742
1743/* PWP: move the cursor up or down. */
1744void
1745_rl_move_vert (to)
1746 int to;
1747{
1748 register int delta, i;
1749
28ef6c31 1750 if (_rl_last_v_pos == to || to > _rl_screenheight)
726f6388
JA
1751 return;
1752
726f6388
JA
1753 if ((delta = to - _rl_last_v_pos) > 0)
1754 {
1755 for (i = 0; i < delta; i++)
1756 putc ('\n', rl_outstream);
bb70624e
JA
1757#if defined (__MSDOS__)
1758 putc ('\r', rl_outstream);
1759#else
28ef6c31 1760 tputs (_rl_term_cr, 1, _rl_output_character_function);
bb70624e 1761#endif
726f6388
JA
1762 _rl_last_c_pos = 0;
1763 }
1764 else
1765 { /* delta < 0 */
28ef6c31 1766 if (_rl_term_up && *_rl_term_up)
726f6388 1767 for (i = 0; i < -delta; i++)
28ef6c31 1768 tputs (_rl_term_up, 1, _rl_output_character_function);
726f6388 1769 }
bb70624e 1770
726f6388
JA
1771 _rl_last_v_pos = to; /* Now TO is here */
1772}
1773
1774/* Physically print C on rl_outstream. This is for functions which know
1775 how to optimize the display. Return the number of characters output. */
ccc6cda3 1776int
726f6388
JA
1777rl_show_char (c)
1778 int c;
1779{
1780 int n = 1;
1781 if (META_CHAR (c) && (_rl_output_meta_chars == 0))
1782 {
1783 fprintf (rl_outstream, "M-");
1784 n += 2;
1785 c = UNMETA (c);
1786 }
1787
1788#if defined (DISPLAY_TABS)
ccc6cda3 1789 if ((CTRL_CHAR (c) && c != '\t') || c == RUBOUT)
726f6388 1790#else
ccc6cda3 1791 if (CTRL_CHAR (c) || c == RUBOUT)
726f6388
JA
1792#endif /* !DISPLAY_TABS */
1793 {
1794 fprintf (rl_outstream, "C-");
1795 n += 2;
ccc6cda3 1796 c = CTRL_CHAR (c) ? UNCTRL (c) : '?';
726f6388
JA
1797 }
1798
1799 putc (c, rl_outstream);
1800 fflush (rl_outstream);
1801 return n;
1802}
1803
1804int
1805rl_character_len (c, pos)
1806 register int c, pos;
1807{
1808 unsigned char uc;
1809
1810 uc = (unsigned char)c;
1811
1812 if (META_CHAR (uc))
1813 return ((_rl_output_meta_chars == 0) ? 4 : 1);
1814
1815 if (uc == '\t')
1816 {
1817#if defined (DISPLAY_TABS)
1818 return (((pos | 7) + 1) - pos);
1819#else
1820 return (2);
1821#endif /* !DISPLAY_TABS */
1822 }
1823
ccc6cda3
JA
1824 if (CTRL_CHAR (c) || c == RUBOUT)
1825 return (2);
1826
f73dda09 1827 return ((ISPRINT (uc)) ? 1 : 2);
726f6388 1828}
726f6388
JA
1829/* How to print things in the "echo-area". The prompt is treated as a
1830 mini-modeline. */
6e70dbff 1831static int msg_saved_prompt = 0;
726f6388 1832
ccc6cda3
JA
1833#if defined (USE_VARARGS)
1834int
1835#if defined (PREFER_STDARG)
1836rl_message (const char *format, ...)
1837#else
726f6388
JA
1838rl_message (va_alist)
1839 va_dcl
ccc6cda3 1840#endif
726f6388 1841{
726f6388 1842 va_list args;
ccc6cda3
JA
1843#if defined (PREFER_VARARGS)
1844 char *format;
1845#endif
726f6388 1846
ccc6cda3
JA
1847#if defined (PREFER_STDARG)
1848 va_start (args, format);
1849#else
726f6388
JA
1850 va_start (args);
1851 format = va_arg (args, char *);
ccc6cda3
JA
1852#endif
1853
f73dda09
JA
1854#if defined (HAVE_VSNPRINTF)
1855 vsnprintf (msg_buf, sizeof (msg_buf) - 1, format, args);
1856#else
726f6388 1857 vsprintf (msg_buf, format, args);
f73dda09
JA
1858 msg_buf[sizeof(msg_buf) - 1] = '\0'; /* overflow? */
1859#endif
726f6388
JA
1860 va_end (args);
1861
6e70dbff
CR
1862 if (saved_local_prompt == 0)
1863 {
1864 rl_save_prompt ();
1865 msg_saved_prompt = 1;
1866 }
726f6388 1867 rl_display_prompt = msg_buf;
6e70dbff
CR
1868 local_prompt = expand_prompt (msg_buf, &prompt_visible_length,
1869 &prompt_last_invisible,
1870 &prompt_invis_chars_first_line,
1871 &prompt_physical_chars);
1872 local_prompt_prefix = (char *)NULL;
ccc6cda3 1873 (*rl_redisplay_function) ();
6e70dbff 1874
726f6388
JA
1875 return 0;
1876}
ccc6cda3
JA
1877#else /* !USE_VARARGS */
1878int
726f6388
JA
1879rl_message (format, arg1, arg2)
1880 char *format;
1881{
1882 sprintf (msg_buf, format, arg1, arg2);
f73dda09 1883 msg_buf[sizeof(msg_buf) - 1] = '\0'; /* overflow? */
6e70dbff 1884
726f6388 1885 rl_display_prompt = msg_buf;
6e70dbff
CR
1886 if (saved_local_prompt == 0)
1887 {
1888 rl_save_prompt ();
1889 msg_saved_prompt = 1;
1890 }
1891 local_prompt = expand_prompt (msg_buf, &prompt_visible_length,
1892 &prompt_last_invisible,
1893 &prompt_invis_chars_first_line,
1894 &prompt_physical_chars);
1895 local_prompt_prefix = (char *)NULL;
ccc6cda3 1896 (*rl_redisplay_function) ();
6e70dbff 1897
726f6388
JA
1898 return 0;
1899}
ccc6cda3 1900#endif /* !USE_VARARGS */
726f6388
JA
1901
1902/* How to clear things from the "echo-area". */
ccc6cda3 1903int
726f6388
JA
1904rl_clear_message ()
1905{
1906 rl_display_prompt = rl_prompt;
6e70dbff
CR
1907 if (msg_saved_prompt)
1908 {
1909 rl_restore_prompt ();
1910 msg_saved_prompt = 0;
1911 }
ccc6cda3 1912 (*rl_redisplay_function) ();
726f6388
JA
1913 return 0;
1914}
1915
ccc6cda3 1916int
726f6388
JA
1917rl_reset_line_state ()
1918{
1919 rl_on_new_line ();
1920
1921 rl_display_prompt = rl_prompt ? rl_prompt : "";
1922 forced_display = 1;
1923 return 0;
1924}
1925
ccc6cda3 1926void
b72432fd 1927rl_save_prompt ()
ccc6cda3
JA
1928{
1929 saved_local_prompt = local_prompt;
1930 saved_local_prefix = local_prompt_prefix;
6e70dbff 1931 saved_prefix_length = prompt_prefix_length;
28ef6c31
JA
1932 saved_last_invisible = prompt_last_invisible;
1933 saved_visible_length = prompt_visible_length;
d3a24ed2 1934 saved_invis_chars_first_line = prompt_invis_chars_first_line;
762a763b 1935 saved_physical_chars = prompt_physical_chars;
ccc6cda3
JA
1936
1937 local_prompt = local_prompt_prefix = (char *)0;
6e70dbff 1938 prompt_last_invisible = prompt_visible_length = prompt_prefix_length = 0;
762a763b 1939 prompt_invis_chars_first_line = prompt_physical_chars = 0;
ccc6cda3
JA
1940}
1941
1942void
b72432fd 1943rl_restore_prompt ()
ccc6cda3 1944{
28ef6c31
JA
1945 FREE (local_prompt);
1946 FREE (local_prompt_prefix);
ccc6cda3
JA
1947
1948 local_prompt = saved_local_prompt;
1949 local_prompt_prefix = saved_local_prefix;
6e70dbff 1950 prompt_prefix_length = saved_prefix_length;
28ef6c31
JA
1951 prompt_last_invisible = saved_last_invisible;
1952 prompt_visible_length = saved_visible_length;
d3a24ed2 1953 prompt_invis_chars_first_line = saved_invis_chars_first_line;
762a763b 1954 prompt_physical_chars = saved_physical_chars;
6e70dbff
CR
1955
1956 /* can test saved_local_prompt to see if prompt info has been saved. */
1957 saved_local_prompt = saved_local_prefix = (char *)0;
1958 saved_last_invisible = saved_visible_length = saved_prefix_length = 0;
1959 saved_invis_chars_first_line = saved_physical_chars = 0;
ccc6cda3
JA
1960}
1961
1962char *
1963_rl_make_prompt_for_search (pchar)
1964 int pchar;
1965{
1966 int len;
1967 char *pmt;
1968
b72432fd 1969 rl_save_prompt ();
ccc6cda3
JA
1970
1971 if (saved_local_prompt == 0)
1972 {
1973 len = (rl_prompt && *rl_prompt) ? strlen (rl_prompt) : 0;
f73dda09 1974 pmt = (char *)xmalloc (len + 2);
ccc6cda3 1975 if (len)
b72432fd 1976 strcpy (pmt, rl_prompt);
ccc6cda3
JA
1977 pmt[len] = pchar;
1978 pmt[len+1] = '\0';
1979 }
1980 else
1981 {
1982 len = *saved_local_prompt ? strlen (saved_local_prompt) : 0;
f73dda09 1983 pmt = (char *)xmalloc (len + 2);
ccc6cda3 1984 if (len)
b72432fd 1985 strcpy (pmt, saved_local_prompt);
ccc6cda3
JA
1986 pmt[len] = pchar;
1987 pmt[len+1] = '\0';
1988 local_prompt = savestring (pmt);
28ef6c31
JA
1989 prompt_last_invisible = saved_last_invisible;
1990 prompt_visible_length = saved_visible_length + 1;
ccc6cda3 1991 }
762a763b 1992
6e70dbff
CR
1993 prompt_physical_chars = saved_physical_chars + 1;
1994
ccc6cda3
JA
1995 return pmt;
1996}
1997
726f6388
JA
1998/* Quick redisplay hack when erasing characters at the end of the line. */
1999void
2000_rl_erase_at_end_of_line (l)
2001 int l;
2002{
2003 register int i;
2004
ccc6cda3 2005 _rl_backspace (l);
726f6388
JA
2006 for (i = 0; i < l; i++)
2007 putc (' ', rl_outstream);
ccc6cda3 2008 _rl_backspace (l);
726f6388
JA
2009 for (i = 0; i < l; i++)
2010 visible_line[--_rl_last_c_pos] = '\0';
2011 rl_display_fixed++;
2012}
2013
2014/* Clear to the end of the line. COUNT is the minimum
2015 number of character spaces to clear, */
d166f048
JA
2016void
2017_rl_clear_to_eol (count)
726f6388
JA
2018 int count;
2019{
28ef6c31
JA
2020 if (_rl_term_clreol)
2021 tputs (_rl_term_clreol, 1, _rl_output_character_function);
d166f048 2022 else if (count)
726f6388
JA
2023 space_to_eol (count);
2024}
2025
2026/* Clear to the end of the line using spaces. COUNT is the minimum
2027 number of character spaces to clear, */
2028static void
2029space_to_eol (count)
2030 int count;
2031{
2032 register int i;
2033
2034 for (i = 0; i < count; i++)
2035 putc (' ', rl_outstream);
2036
2037 _rl_last_c_pos += count;
2038}
2039
d166f048
JA
2040void
2041_rl_clear_screen ()
2042{
28ef6c31
JA
2043 if (_rl_term_clrpag)
2044 tputs (_rl_term_clrpag, 1, _rl_output_character_function);
d166f048 2045 else
28ef6c31 2046 rl_crlf ();
d166f048
JA
2047}
2048
7117c2d2 2049/* Insert COUNT characters from STRING to the output stream at column COL. */
726f6388 2050static void
7117c2d2 2051insert_some_chars (string, count, col)
726f6388 2052 char *string;
7117c2d2 2053 int count, col;
726f6388 2054{
9607141c
CR
2055#if defined (__MSDOS__) || defined (__MINGW32__)
2056 _rl_output_some_chars (string, count);
2057#else
7117c2d2
JA
2058 /* DEBUGGING */
2059 if (MB_CUR_MAX == 1 || rl_byte_oriented)
2060 if (count != col)
2061 fprintf(stderr, "readline: debug: insert_some_chars: count (%d) != col (%d)\n", count, col);
2062
726f6388 2063 /* If IC is defined, then we do not have to "enter" insert mode. */
28ef6c31 2064 if (_rl_term_IC)
726f6388 2065 {
ccc6cda3 2066 char *buffer;
7117c2d2
JA
2067
2068 buffer = tgoto (_rl_term_IC, 0, col);
726f6388
JA
2069 tputs (buffer, 1, _rl_output_character_function);
2070 _rl_output_some_chars (string, count);
2071 }
2072 else
2073 {
2074 register int i;
2075
2076 /* If we have to turn on insert-mode, then do so. */
28ef6c31
JA
2077 if (_rl_term_im && *_rl_term_im)
2078 tputs (_rl_term_im, 1, _rl_output_character_function);
726f6388
JA
2079
2080 /* If there is a special command for inserting characters, then
2081 use that first to open up the space. */
28ef6c31 2082 if (_rl_term_ic && *_rl_term_ic)
726f6388 2083 {
7117c2d2 2084 for (i = col; i--; )
28ef6c31 2085 tputs (_rl_term_ic, 1, _rl_output_character_function);
726f6388
JA
2086 }
2087
2088 /* Print the text. */
2089 _rl_output_some_chars (string, count);
2090
2091 /* If there is a string to turn off insert mode, we had best use
2092 it now. */
28ef6c31
JA
2093 if (_rl_term_ei && *_rl_term_ei)
2094 tputs (_rl_term_ei, 1, _rl_output_character_function);
726f6388 2095 }
9607141c 2096#endif /* __MSDOS__ || __MINGW32__ */
726f6388
JA
2097}
2098
2099/* Delete COUNT characters from the display line. */
2100static void
2101delete_chars (count)
2102 int count;
2103{
28ef6c31 2104 if (count > _rl_screenwidth) /* XXX */
726f6388
JA
2105 return;
2106
9607141c 2107#if !defined (__MSDOS__) && !defined (__MINGW32__)
28ef6c31 2108 if (_rl_term_DC && *_rl_term_DC)
726f6388 2109 {
ccc6cda3 2110 char *buffer;
28ef6c31 2111 buffer = tgoto (_rl_term_DC, count, count);
726f6388
JA
2112 tputs (buffer, count, _rl_output_character_function);
2113 }
2114 else
2115 {
28ef6c31 2116 if (_rl_term_dc && *_rl_term_dc)
726f6388 2117 while (count--)
28ef6c31 2118 tputs (_rl_term_dc, 1, _rl_output_character_function);
726f6388 2119 }
9607141c 2120#endif /* !__MSDOS__ && !__MINGW32__ */
726f6388
JA
2121}
2122
2123void
2124_rl_update_final ()
2125{
2126 int full_lines;
2127
2128 full_lines = 0;
ccc6cda3
JA
2129 /* If the cursor is the only thing on an otherwise-blank last line,
2130 compensate so we don't print an extra CRLF. */
2131 if (_rl_vis_botlin && _rl_last_c_pos == 0 &&
d166f048 2132 visible_line[vis_lbreaks[_rl_vis_botlin]] == 0)
726f6388
JA
2133 {
2134 _rl_vis_botlin--;
2135 full_lines = 1;
2136 }
2137 _rl_move_vert (_rl_vis_botlin);
ccc6cda3 2138 /* If we've wrapped lines, remove the final xterm line-wrap flag. */
28ef6c31 2139 if (full_lines && _rl_term_autowrap && (VIS_LLEN(_rl_vis_botlin) == _rl_screenwidth))
726f6388 2140 {
726f6388 2141 char *last_line;
7117c2d2 2142
bb70624e 2143 last_line = &visible_line[vis_lbreaks[_rl_vis_botlin]];
28ef6c31 2144 _rl_move_cursor_relative (_rl_screenwidth - 1, last_line);
d166f048 2145 _rl_clear_to_eol (0);
28ef6c31 2146 putc (last_line[_rl_screenwidth - 1], rl_outstream);
726f6388
JA
2147 }
2148 _rl_vis_botlin = 0;
28ef6c31 2149 rl_crlf ();
726f6388
JA
2150 fflush (rl_outstream);
2151 rl_display_fixed++;
2152}
2153
2154/* Move to the start of the current line. */
2155static void
2156cr ()
2157{
28ef6c31 2158 if (_rl_term_cr)
726f6388 2159 {
bb70624e
JA
2160#if defined (__MSDOS__)
2161 putc ('\r', rl_outstream);
2162#else
28ef6c31 2163 tputs (_rl_term_cr, 1, _rl_output_character_function);
bb70624e 2164#endif
726f6388
JA
2165 _rl_last_c_pos = 0;
2166 }
2167}
2168
bb70624e
JA
2169/* Redraw the last line of a multi-line prompt that may possibly contain
2170 terminal escape sequences. Called with the cursor at column 0 of the
2171 line to draw the prompt on. */
2172static void
2173redraw_prompt (t)
2174 char *t;
2175{
6e70dbff 2176 char *oldp;
bb70624e 2177
bb70624e 2178 oldp = rl_display_prompt;
6e70dbff 2179 rl_save_prompt ();
bb70624e
JA
2180
2181 rl_display_prompt = t;
28ef6c31
JA
2182 local_prompt = expand_prompt (t, &prompt_visible_length,
2183 &prompt_last_invisible,
43cdcad8
CR
2184 &prompt_invis_chars_first_line,
2185 &prompt_physical_chars);
bb70624e 2186 local_prompt_prefix = (char *)NULL;
6e70dbff 2187
bb70624e
JA
2188 rl_forced_update_display ();
2189
2190 rl_display_prompt = oldp;
6e70dbff 2191 rl_restore_prompt();
bb70624e
JA
2192}
2193
726f6388
JA
2194/* Redisplay the current line after a SIGWINCH is received. */
2195void
2196_rl_redisplay_after_sigwinch ()
2197{
bb70624e 2198 char *t;
726f6388
JA
2199
2200 /* Clear the current line and put the cursor at column 0. Make sure
2201 the right thing happens if we have wrapped to a new screen line. */
28ef6c31 2202 if (_rl_term_cr)
726f6388 2203 {
bb70624e
JA
2204#if defined (__MSDOS__)
2205 putc ('\r', rl_outstream);
2206#else
28ef6c31 2207 tputs (_rl_term_cr, 1, _rl_output_character_function);
bb70624e 2208#endif
726f6388 2209 _rl_last_c_pos = 0;
bb70624e 2210#if defined (__MSDOS__)
28ef6c31 2211 space_to_eol (_rl_screenwidth);
bb70624e
JA
2212 putc ('\r', rl_outstream);
2213#else
28ef6c31
JA
2214 if (_rl_term_clreol)
2215 tputs (_rl_term_clreol, 1, _rl_output_character_function);
726f6388
JA
2216 else
2217 {
28ef6c31
JA
2218 space_to_eol (_rl_screenwidth);
2219 tputs (_rl_term_cr, 1, _rl_output_character_function);
726f6388 2220 }
bb70624e 2221#endif
726f6388
JA
2222 if (_rl_last_v_pos > 0)
2223 _rl_move_vert (0);
2224 }
2225 else
28ef6c31 2226 rl_crlf ();
726f6388
JA
2227
2228 /* Redraw only the last line of a multi-line prompt. */
2229 t = strrchr (rl_display_prompt, '\n');
2230 if (t)
bb70624e 2231 redraw_prompt (++t);
726f6388
JA
2232 else
2233 rl_forced_update_display ();
2234}
ccc6cda3
JA
2235
2236void
2237_rl_clean_up_for_exit ()
2238{
2239 if (readline_echoing_p)
2240 {
2241 _rl_move_vert (_rl_vis_botlin);
2242 _rl_vis_botlin = 0;
2243 fflush (rl_outstream);
b72432fd 2244 rl_restart_output (1, 0);
ccc6cda3
JA
2245 }
2246}
b72432fd
JA
2247
2248void
2249_rl_erase_entire_line ()
2250{
2251 cr ();
2252 _rl_clear_to_eol (0);
2253 cr ();
2254 fflush (rl_outstream);
2255}
bb70624e
JA
2256
2257/* return the `current display line' of the cursor -- the number of lines to
2258 move up to get to the first screen line of the current readline line. */
2259int
2260_rl_current_display_line ()
2261{
2262 int ret, nleft;
2263
2264 /* Find out whether or not there might be invisible characters in the
2265 editing buffer. */
2266 if (rl_display_prompt == rl_prompt)
28ef6c31 2267 nleft = _rl_last_c_pos - _rl_screenwidth - rl_visible_prompt_length;
bb70624e 2268 else
28ef6c31 2269 nleft = _rl_last_c_pos - _rl_screenwidth;
bb70624e
JA
2270
2271 if (nleft > 0)
28ef6c31 2272 ret = 1 + nleft / _rl_screenwidth;
bb70624e
JA
2273 else
2274 ret = 0;
2275
2276 return ret;
2277}
7117c2d2
JA
2278
2279#if defined (HANDLE_MULTIBYTE)
2280/* Calculate the number of screen columns occupied by STR from START to END.
2281 In the case of multibyte characters with stateful encoding, we have to
2282 scan from the beginning of the string to take the state into account. */
2283static int
2284_rl_col_width (str, start, end)
d3a24ed2 2285 const char *str;
7117c2d2
JA
2286 int start, end;
2287{
2288 wchar_t wc;
2289 mbstate_t ps = {0};
2290 int tmp, point, width, max;
2291
2292 if (end <= start)
2293 return 0;
2294
2295 point = 0;
2296 max = end;
2297
2298 while (point < start)
2299 {
2300 tmp = mbrlen (str + point, max, &ps);
d3a24ed2 2301 if (MB_INVALIDCH ((size_t)tmp))
7117c2d2
JA
2302 {
2303 /* In this case, the bytes are invalid or too short to compose a
2304 multibyte character, so we assume that the first byte represents
2305 a single character. */
2306 point++;
2307 max--;
2308
2309 /* Clear the state of the byte sequence, because in this case the
2310 effect of mbstate is undefined. */
2311 memset (&ps, 0, sizeof (mbstate_t));
2312 }
d3a24ed2 2313 else if (MB_NULLWCH (tmp))
5e13499c 2314 break; /* Found '\0' */
7117c2d2
JA
2315 else
2316 {
2317 point += tmp;
2318 max -= tmp;
2319 }
2320 }
2321
2322 /* If START is not a byte that starts a character, then POINT will be
2323 greater than START. In this case, assume that (POINT - START) gives
2324 a byte count that is the number of columns of difference. */
2325 width = point - start;
2326
2327 while (point < end)
2328 {
2329 tmp = mbrtowc (&wc, str + point, max, &ps);
d3a24ed2 2330 if (MB_INVALIDCH ((size_t)tmp))
7117c2d2
JA
2331 {
2332 /* In this case, the bytes are invalid or too short to compose a
2333 multibyte character, so we assume that the first byte represents
2334 a single character. */
2335 point++;
2336 max--;
2337
2338 /* and assume that the byte occupies a single column. */
2339 width++;
2340
2341 /* Clear the state of the byte sequence, because in this case the
2342 effect of mbstate is undefined. */
2343 memset (&ps, 0, sizeof (mbstate_t));
2344 }
d3a24ed2 2345 else if (MB_NULLWCH (tmp))
5e13499c 2346 break; /* Found '\0' */
7117c2d2
JA
2347 else
2348 {
2349 point += tmp;
2350 max -= tmp;
2351 tmp = wcwidth(wc);
2352 width += (tmp >= 0) ? tmp : 1;
2353 }
2354 }
2355
2356 width += point - end;
2357
2358 return width;
2359}
2360#endif /* HANDLE_MULTIBYTE */