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