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