]> git.ipfire.org Git - thirdparty/bash.git/blame - lib/readline/display.c
Imported from ../bash-2.05.tar.gz.
[thirdparty/bash.git] / lib / readline / display.c
CommitLineData
726f6388
JA
1/* display.c -- readline redisplay facility. */
2
3/* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
4
5 This file is part of the GNU Readline Library, a library for
6 reading lines of text with interactive input and history editing.
7
8 The GNU Readline Library is free software; you can redistribute it
9 and/or modify it under the terms of the GNU General Public License
bb70624e 10 as published by the Free Software Foundation; either version 2, or
726f6388
JA
11 (at your option) any later version.
12
13 The GNU Readline Library is distributed in the hope that it will be
14 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 The GNU General Public License is often shipped with GNU software, and
19 is generally kept in a file called COPYING or LICENSE. If you do not
20 have a copy of the license, write to the Free Software Foundation,
bb70624e 21 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
726f6388
JA
22#define READLINE_LIBRARY
23
24#if defined (HAVE_CONFIG_H)
ccc6cda3 25# include <config.h>
726f6388
JA
26#endif
27
726f6388
JA
28#include <sys/types.h>
29
30#if defined (HAVE_UNISTD_H)
31# include <unistd.h>
32#endif /* HAVE_UNISTD_H */
33
d166f048
JA
34#include "posixstat.h"
35
726f6388
JA
36#if defined (HAVE_STDLIB_H)
37# include <stdlib.h>
38#else
39# include "ansi_stdlib.h"
40#endif /* HAVE_STDLIB_H */
41
d166f048
JA
42#include <stdio.h>
43
726f6388
JA
44/* System-specific feature definitions and include files. */
45#include "rldefs.h"
46
ccc6cda3
JA
47/* Termcap library stuff. */
48#include "tcap.h"
49
726f6388
JA
50/* Some standard library routines. */
51#include "readline.h"
52#include "history.h"
53
bb70624e
JA
54#include "rlprivate.h"
55#include "xmalloc.h"
56
726f6388
JA
57#if !defined (strchr) && !defined (__STDC__)
58extern char *strchr (), *strrchr ();
59#endif /* !strchr && !__STDC__ */
60
bb70624e 61#if defined (HACK_TERMCAP_MOTION)
28ef6c31 62extern char *_rl_term_forward_char;
cce855bc 63#endif
726f6388 64
bb70624e
JA
65static void update_line __P((char *, char *, int, int, int, int));
66static void space_to_eol __P((int));
67static void delete_chars __P((int));
68static void insert_some_chars __P((char *, int));
69static void cr __P((void));
ccc6cda3
JA
70
71static int *inv_lbreaks, *vis_lbreaks;
bb70624e 72static int inv_lbsize, vis_lbsize;
726f6388
JA
73
74/* Heuristic used to decide whether it is faster to move from CUR to NEW
75 by backing up or outputting a carriage return and moving forward. */
76#define CR_FASTER(new, cur) (((new) + 1) < ((cur) - (new)))
77
78/* **************************************************************** */
79/* */
80/* Display stuff */
81/* */
82/* **************************************************************** */
83
84/* This is the stuff that is hard for me. I never seem to write good
85 display routines in C. Let's see how I do this time. */
86
87/* (PWP) Well... Good for a simple line updater, but totally ignores
88 the problems of input lines longer than the screen width.
89
90 update_line and the code that calls it makes a multiple line,
91 automatically wrapping line update. Careful attention needs
92 to be paid to the vertical position variables. */
93
94/* Keep two buffers; one which reflects the current contents of the
95 screen, and the other to draw what we think the new contents should
96 be. Then compare the buffers, and make whatever changes to the
97 screen itself that we should. Finally, make the buffer that we
98 just drew into be the one which reflects the current contents of the
99 screen, and place the cursor where it belongs.
100
101 Commands that want to can fix the display themselves, and then let
102 this function know that the display has been fixed by setting the
103 RL_DISPLAY_FIXED variable. This is good for efficiency. */
104
ccc6cda3 105/* Application-specific redisplay function. */
28ef6c31 106rl_voidfunc_t *rl_redisplay_function = rl_redisplay;
ccc6cda3 107
726f6388
JA
108/* Global variables declared here. */
109/* What YOU turn on when you have handled all redisplay yourself. */
110int rl_display_fixed = 0;
111
ccc6cda3
JA
112int _rl_suppress_redisplay = 0;
113
726f6388
JA
114/* The stuff that gets printed out before the actual text of the line.
115 This is usually pointing to rl_prompt. */
116char *rl_display_prompt = (char *)NULL;
117
118/* Pseudo-global variables declared here. */
119/* The visible cursor position. If you print some text, adjust this. */
120int _rl_last_c_pos = 0;
121int _rl_last_v_pos = 0;
122
123/* Number of lines currently on screen minus 1. */
124int _rl_vis_botlin = 0;
125
126/* Variables used only in this file. */
127/* The last left edge of text that was displayed. This is used when
128 doing horizontal scrolling. It shifts in thirds of a screenwidth. */
ccc6cda3 129static int last_lmargin;
726f6388
JA
130
131/* The line display buffers. One is the line currently displayed on
132 the screen. The other is the line about to be displayed. */
133static char *visible_line = (char *)NULL;
134static char *invisible_line = (char *)NULL;
135
136/* A buffer for `modeline' messages. */
137static char msg_buf[128];
138
139/* Non-zero forces the redisplay even if we thought it was unnecessary. */
ccc6cda3 140static int forced_display;
726f6388
JA
141
142/* Default and initial buffer size. Can grow. */
143static int line_size = 1024;
144
28ef6c31
JA
145/* Variables to keep track of the expanded prompt string, which may
146 include invisible characters. */
147
726f6388 148static char *local_prompt, *local_prompt_prefix;
28ef6c31 149static int prompt_visible_length, prompt_prefix_length;
726f6388
JA
150
151/* The number of invisible characters in the line currently being
152 displayed on the screen. */
ccc6cda3
JA
153static int visible_wrap_offset;
154
28ef6c31
JA
155/* The number of invisible characters in the prompt string. Static so it
156 can be shared between rl_redisplay and update_line */
ccc6cda3
JA
157static int wrap_offset;
158
28ef6c31
JA
159/* The index of the last invisible character in the prompt string. */
160static int prompt_last_invisible;
726f6388
JA
161
162/* The length (buffer offset) of the first line of the last (possibly
163 multi-line) buffer displayed on the screen. */
ccc6cda3 164static int visible_first_line_len;
726f6388 165
28ef6c31
JA
166/* Number of invisible characters on the first physical line of the prompt.
167 Only valid when the number of physical characters in the prompt exceeds
168 (or is equal to) _rl_screenwidth. */
169static int prompt_invis_chars_first_line;
170
171static int prompt_last_screen_line;
172
726f6388
JA
173/* Expand the prompt string S and return the number of visible
174 characters in *LP, if LP is not null. This is currently more-or-less
ccc6cda3 175 a placeholder for expansion. LIP, if non-null is a place to store the
28ef6c31
JA
176 index of the last invisible character in the returned string. NIFLP,
177 if non-zero, is a place to store the number of invisible characters in
178 the first prompt line. */
726f6388
JA
179
180/* Current implementation:
181 \001 (^A) start non-visible characters
182 \002 (^B) end non-visible characters
183 all characters except \001 and \002 (following a \001) are copied to
184 the returned string; all characters except those between \001 and
185 \002 are assumed to be `visible'. */
186
187static char *
28ef6c31 188expand_prompt (pmt, lp, lip, niflp)
726f6388 189 char *pmt;
28ef6c31 190 int *lp, *lip, *niflp;
726f6388
JA
191{
192 char *r, *ret, *p;
28ef6c31 193 int l, rl, last, ignoring, ninvis, invfl;
726f6388
JA
194
195 /* Short-circuit if we can. */
196 if (strchr (pmt, RL_PROMPT_START_IGNORE) == 0)
197 {
198 r = savestring (pmt);
199 if (lp)
200 *lp = strlen (r);
201 return r;
202 }
203
ccc6cda3 204 l = strlen (pmt);
726f6388 205 r = ret = xmalloc (l + 1);
28ef6c31
JA
206
207 invfl = 0; /* invisible chars in first line of prompt */
208
209 for (rl = ignoring = last = ninvis = 0, p = pmt; p && *p; p++)
726f6388
JA
210 {
211 /* This code strips the invisible character string markers
212 RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE */
213 if (*p == RL_PROMPT_START_IGNORE)
214 {
215 ignoring++;
216 continue;
217 }
218 else if (ignoring && *p == RL_PROMPT_END_IGNORE)
219 {
220 ignoring = 0;
ccc6cda3 221 last = r - ret - 1;
726f6388
JA
222 continue;
223 }
224 else
225 {
226 *r++ = *p;
227 if (!ignoring)
228 rl++;
28ef6c31
JA
229 else
230 ninvis++;
231 if (rl == _rl_screenwidth)
232 invfl = ninvis;
726f6388
JA
233 }
234 }
235
28ef6c31
JA
236 if (rl < _rl_screenwidth)
237 invfl = ninvis;
238
726f6388
JA
239 *r = '\0';
240 if (lp)
241 *lp = rl;
ccc6cda3
JA
242 if (lip)
243 *lip = last;
28ef6c31
JA
244 if (niflp)
245 *niflp = invfl;
726f6388
JA
246 return ret;
247}
248
bb70624e
JA
249/* Just strip out RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE from
250 PMT and return the rest of PMT. */
251char *
252_rl_strip_prompt (pmt)
253 char *pmt;
254{
255 char *ret;
256
28ef6c31 257 ret = expand_prompt (pmt, (int *)NULL, (int *)NULL, (int *)NULL);
bb70624e
JA
258 return ret;
259}
260
726f6388
JA
261/*
262 * Expand the prompt string into the various display components, if
263 * necessary.
264 *
265 * local_prompt = expanded last line of string in rl_display_prompt
266 * (portion after the final newline)
267 * local_prompt_prefix = portion before last newline of rl_display_prompt,
268 * expanded via expand_prompt
28ef6c31
JA
269 * prompt_visible_length = number of visible characters in local_prompt
270 * prompt_prefix_length = number of visible characters in local_prompt_prefix
726f6388
JA
271 *
272 * This function is called once per call to readline(). It may also be
273 * called arbitrarily to expand the primary prompt.
274 *
275 * The return value is the number of visible characters on the last line
276 * of the (possibly multi-line) prompt.
277 */
278int
279rl_expand_prompt (prompt)
280 char *prompt;
281{
282 char *p, *t;
283 int c;
284
285 /* Clear out any saved values. */
28ef6c31
JA
286 FREE (local_prompt);
287 FREE (local_prompt_prefix);
288
726f6388 289 local_prompt = local_prompt_prefix = (char *)0;
28ef6c31 290 prompt_last_invisible = prompt_visible_length = 0;
726f6388 291
ccc6cda3 292 if (prompt == 0 || *prompt == 0)
726f6388
JA
293 return (0);
294
295 p = strrchr (prompt, '\n');
296 if (!p)
297 {
28ef6c31
JA
298 /* The prompt is only one logical line, though it might wrap. */
299 local_prompt = expand_prompt (prompt, &prompt_visible_length,
300 &prompt_last_invisible,
301 &prompt_invis_chars_first_line);
726f6388 302 local_prompt_prefix = (char *)0;
28ef6c31 303 return (prompt_visible_length);
726f6388
JA
304 }
305 else
306 {
307 /* The prompt spans multiple lines. */
308 t = ++p;
28ef6c31
JA
309 local_prompt = expand_prompt (p, &prompt_visible_length,
310 &prompt_last_invisible,
311 &prompt_invis_chars_first_line);
726f6388
JA
312 c = *t; *t = '\0';
313 /* The portion of the prompt string up to and including the
314 final newline is now null-terminated. */
28ef6c31
JA
315 local_prompt_prefix = expand_prompt (prompt, &prompt_prefix_length,
316 (int *)NULL,
317 &prompt_invis_chars_first_line);
726f6388 318 *t = c;
28ef6c31 319 return (prompt_prefix_length);
726f6388
JA
320 }
321}
322
bb70624e
JA
323/* Initialize the VISIBLE_LINE and INVISIBLE_LINE arrays, and their associated
324 arrays of line break markers. MINSIZE is the minimum size of VISIBLE_LINE
325 and INVISIBLE_LINE; if it is greater than LINE_SIZE, LINE_SIZE is
326 increased. If the lines have already been allocated, this ensures that
327 they can hold at least MINSIZE characters. */
328static void
329init_line_structures (minsize)
330 int minsize;
331{
332 register int n;
333
334 if (invisible_line == 0) /* initialize it */
335 {
336 if (line_size < minsize)
337 line_size = minsize;
338 visible_line = xmalloc (line_size);
339 invisible_line = xmalloc (line_size);
340 }
341 else if (line_size < minsize) /* ensure it can hold MINSIZE chars */
342 {
343 line_size *= 2;
344 if (line_size < minsize)
345 line_size = minsize;
346 visible_line = xrealloc (visible_line, line_size);
347 invisible_line = xrealloc (invisible_line, line_size);
348 }
349
350 for (n = minsize; n < line_size; n++)
351 {
352 visible_line[n] = 0;
353 invisible_line[n] = 1;
354 }
355
356 if (vis_lbreaks == 0)
357 {
358 /* should be enough. */
359 inv_lbsize = vis_lbsize = 256;
360 inv_lbreaks = (int *)xmalloc (inv_lbsize * sizeof (int));
361 vis_lbreaks = (int *)xmalloc (vis_lbsize * sizeof (int));
362 inv_lbreaks[0] = vis_lbreaks[0] = 0;
363 }
364}
365
726f6388
JA
366/* Basic redisplay algorithm. */
367void
368rl_redisplay ()
369{
ccc6cda3
JA
370 register int in, out, c, linenum, cursor_linenum;
371 register char *line;
372 int c_pos, inv_botlin, lb_botlin, lb_linenum;
d166f048 373 int newlines, lpos, temp;
726f6388
JA
374 char *prompt_this_line;
375
376 if (!readline_echoing_p)
377 return;
378
379 if (!rl_display_prompt)
380 rl_display_prompt = "";
381
ccc6cda3 382 if (invisible_line == 0)
726f6388 383 {
bb70624e 384 init_line_structures (0);
726f6388
JA
385 rl_on_new_line ();
386 }
387
388 /* Draw the line into the buffer. */
389 c_pos = -1;
390
ccc6cda3
JA
391 line = invisible_line;
392 out = inv_botlin = 0;
393
726f6388
JA
394 /* Mark the line as modified or not. We only do this for history
395 lines. */
726f6388
JA
396 if (_rl_mark_modified_lines && current_history () && rl_undo_list)
397 {
398 line[out++] = '*';
399 line[out] = '\0';
400 }
401
402 /* If someone thought that the redisplay was handled, but the currently
403 visible line has a different modification state than the one about
404 to become visible, then correct the caller's misconception. */
405 if (visible_line[0] != invisible_line[0])
406 rl_display_fixed = 0;
407
408 /* If the prompt to be displayed is the `primary' readline prompt (the
409 one passed to readline()), use the values we have already expanded.
410 If not, use what's already in rl_display_prompt. WRAP_OFFSET is the
411 number of non-visible characters in the prompt string. */
ccc6cda3 412 if (rl_display_prompt == rl_prompt || local_prompt)
726f6388
JA
413 {
414 int local_len = local_prompt ? strlen (local_prompt) : 0;
415 if (local_prompt_prefix && forced_display)
416 _rl_output_some_chars (local_prompt_prefix, strlen (local_prompt_prefix));
417
418 if (local_len > 0)
ccc6cda3 419 {
b72432fd
JA
420 temp = local_len + out + 2;
421 if (temp >= line_size)
422 {
423 line_size = (temp + 1024) - (temp % 1024);
424 visible_line = xrealloc (visible_line, line_size);
425 line = invisible_line = xrealloc (invisible_line, line_size);
426 }
ccc6cda3
JA
427 strncpy (line + out, local_prompt, local_len);
428 out += local_len;
429 }
726f6388 430 line[out] = '\0';
28ef6c31 431 wrap_offset = local_len - prompt_visible_length;
726f6388
JA
432 }
433 else
434 {
435 int pmtlen;
436 prompt_this_line = strrchr (rl_display_prompt, '\n');
437 if (!prompt_this_line)
438 prompt_this_line = rl_display_prompt;
439 else
440 {
441 prompt_this_line++;
bb70624e 442 pmtlen = prompt_this_line - rl_display_prompt; /* temp var */
726f6388 443 if (forced_display)
ccc6cda3 444 {
bb70624e 445 _rl_output_some_chars (rl_display_prompt, pmtlen);
ccc6cda3
JA
446 /* Make sure we are at column zero even after a newline,
447 regardless of the state of terminal output processing. */
bb70624e 448 if (pmtlen < 2 || prompt_this_line[-2] != '\r')
ccc6cda3
JA
449 cr ();
450 }
726f6388
JA
451 }
452
453 pmtlen = strlen (prompt_this_line);
b72432fd
JA
454 temp = pmtlen + out + 2;
455 if (temp >= line_size)
456 {
457 line_size = (temp + 1024) - (temp % 1024);
458 visible_line = xrealloc (visible_line, line_size);
459 line = invisible_line = xrealloc (invisible_line, line_size);
460 }
726f6388
JA
461 strncpy (line + out, prompt_this_line, pmtlen);
462 out += pmtlen;
463 line[out] = '\0';
28ef6c31 464 wrap_offset = prompt_invis_chars_first_line = 0;
726f6388
JA
465 }
466
bb70624e
JA
467#define CHECK_INV_LBREAKS() \
468 do { \
469 if (newlines >= (inv_lbsize - 2)) \
470 { \
471 inv_lbsize *= 2; \
472 inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
473 } \
474 } while (0)
475
ccc6cda3
JA
476#define CHECK_LPOS() \
477 do { \
b72432fd 478 lpos++; \
28ef6c31 479 if (lpos >= _rl_screenwidth) \
b72432fd 480 { \
bb70624e
JA
481 if (newlines >= (inv_lbsize - 2)) \
482 { \
483 inv_lbsize *= 2; \
484 inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
485 } \
b72432fd
JA
486 inv_lbreaks[++newlines] = out; \
487 lpos = 0; \
488 } \
ccc6cda3
JA
489 } while (0)
490
491 /* inv_lbreaks[i] is where line i starts in the buffer. */
492 inv_lbreaks[newlines = 0] = 0;
d166f048
JA
493 lpos = out - wrap_offset;
494
28ef6c31
JA
495 /* prompt_invis_chars_first_line is the number of invisible characters in
496 the first physical line of the prompt.
497 wrap_offset - prompt_invis_chars_first_line is the number of invis
498 chars on the second line. */
499
500 /* what if lpos is already >= _rl_screenwidth before we start drawing the
d166f048 501 contents of the command line? */
28ef6c31 502 while (lpos >= _rl_screenwidth)
d166f048 503 {
28ef6c31
JA
504 /* fix from Darin Johnson <darin@acuson.com> for prompt string with
505 invisible characters that is longer than the screen width. The
506 prompt_invis_chars_first_line variable could be made into an array
507 saying how many invisible characters there are per line, but that's
508 probably too much work for the benefit gained. How many people have
509 prompts that exceed two physical lines? */
510 temp = ((newlines + 1) * _rl_screenwidth) +
511 ((newlines == 0) ? prompt_invis_chars_first_line : 0) +
512 ((newlines == 1) ? wrap_offset : 0);
bb70624e 513
d166f048 514 inv_lbreaks[++newlines] = temp;
28ef6c31 515 lpos -= _rl_screenwidth;
d166f048 516 }
ccc6cda3 517
28ef6c31
JA
518 prompt_last_screen_line = newlines;
519
520 /* Draw the rest of the line (after the prompt) into invisible_line, keeping
521 track of where the cursor is (c_pos), the number of the line containing
522 the cursor (lb_linenum), the last line number (lb_botlin and inv_botlin).
523 It maintains an array of line breaks for display (inv_lbreaks).
524 This handles expanding tabs for display and displaying meta characters. */
d166f048
JA
525 lb_linenum = 0;
526 for (in = 0; in < rl_end; in++)
726f6388
JA
527 {
528 c = (unsigned char)rl_line_buffer[in];
529
530 if (out + 8 >= line_size) /* XXX - 8 for \t */
531 {
532 line_size *= 2;
533 visible_line = xrealloc (visible_line, line_size);
534 invisible_line = xrealloc (invisible_line, line_size);
535 line = invisible_line;
536 }
537
538 if (in == rl_point)
ccc6cda3
JA
539 {
540 c_pos = out;
541 lb_linenum = newlines;
542 }
726f6388
JA
543
544 if (META_CHAR (c))
545 {
546 if (_rl_output_meta_chars == 0)
547 {
548 sprintf (line + out, "\\%o", c);
ccc6cda3 549
28ef6c31 550 if (lpos + 4 >= _rl_screenwidth)
ccc6cda3 551 {
28ef6c31 552 temp = _rl_screenwidth - lpos;
bb70624e 553 CHECK_INV_LBREAKS ();
ccc6cda3
JA
554 inv_lbreaks[++newlines] = out + temp;
555 lpos = 4 - temp;
556 }
557 else
558 lpos += 4;
559
726f6388
JA
560 out += 4;
561 }
562 else
ccc6cda3
JA
563 {
564 line[out++] = c;
565 CHECK_LPOS();
566 }
726f6388
JA
567 }
568#if defined (DISPLAY_TABS)
569 else if (c == '\t')
570 {
28ef6c31 571 register int newout;
b72432fd
JA
572
573#if 0
ccc6cda3 574 newout = (out | (int)7) + 1;
b72432fd
JA
575#else
576 newout = out + 8 - lpos % 8;
577#endif
ccc6cda3 578 temp = newout - out;
28ef6c31 579 if (lpos + temp >= _rl_screenwidth)
ccc6cda3
JA
580 {
581 register int temp2;
28ef6c31 582 temp2 = _rl_screenwidth - lpos;
bb70624e 583 CHECK_INV_LBREAKS ();
ccc6cda3
JA
584 inv_lbreaks[++newlines] = out + temp2;
585 lpos = temp - temp2;
586 while (out < newout)
587 line[out++] = ' ';
588 }
589 else
590 {
591 while (out < newout)
592 line[out++] = ' ';
593 lpos += temp;
594 }
726f6388
JA
595 }
596#endif
28ef6c31 597 else if (c == '\n' && _rl_horizontal_scroll_mode == 0 && _rl_term_up && *_rl_term_up)
b72432fd
JA
598 {
599 line[out++] = '\0'; /* XXX - sentinel */
bb70624e 600 CHECK_INV_LBREAKS ();
b72432fd
JA
601 inv_lbreaks[++newlines] = out;
602 lpos = 0;
603 }
ccc6cda3 604 else if (CTRL_CHAR (c) || c == RUBOUT)
726f6388
JA
605 {
606 line[out++] = '^';
ccc6cda3
JA
607 CHECK_LPOS();
608 line[out++] = CTRL_CHAR (c) ? UNCTRL (c) : '?';
609 CHECK_LPOS();
726f6388 610 }
ccc6cda3 611 else
726f6388 612 {
ccc6cda3
JA
613 line[out++] = c;
614 CHECK_LPOS();
726f6388 615 }
726f6388
JA
616 }
617 line[out] = '\0';
618 if (c_pos < 0)
ccc6cda3
JA
619 {
620 c_pos = out;
621 lb_linenum = newlines;
622 }
623
624 inv_botlin = lb_botlin = newlines;
bb70624e 625 CHECK_INV_LBREAKS ();
ccc6cda3
JA
626 inv_lbreaks[newlines+1] = out;
627 cursor_linenum = lb_linenum;
726f6388 628
28ef6c31
JA
629 /* C_POS == position in buffer where cursor should be placed.
630 CURSOR_LINENUM == line number where the cursor should be placed. */
726f6388
JA
631
632 /* PWP: now is when things get a bit hairy. The visible and invisible
633 line buffers are really multiple lines, which would wrap every
634 (screenwidth - 1) characters. Go through each in turn, finding
635 the changed region and updating it. The line order is top to bottom. */
636
637 /* If we can move the cursor up and down, then use multiple lines,
638 otherwise, let long lines display in a single terminal line, and
639 horizontally scroll it. */
640
28ef6c31 641 if (_rl_horizontal_scroll_mode == 0 && _rl_term_up && *_rl_term_up)
726f6388 642 {
ccc6cda3 643 int nleft, pos, changed_screen_line;
726f6388
JA
644
645 if (!rl_display_fixed || forced_display)
646 {
647 forced_display = 0;
648
649 /* If we have more than a screenful of material to display, then
650 only display a screenful. We should display the last screen,
ccc6cda3 651 not the first. */
28ef6c31
JA
652 if (out >= _rl_screenchars)
653 out = _rl_screenchars - 1;
726f6388
JA
654
655 /* The first line is at character position 0 in the buffer. The
ccc6cda3
JA
656 second and subsequent lines start at inv_lbreaks[N], offset by
657 OFFSET (which has already been calculated above). */
726f6388
JA
658
659#define W_OFFSET(line, offset) ((line) == 0 ? offset : 0)
ccc6cda3
JA
660#define VIS_LLEN(l) ((l) > _rl_vis_botlin ? 0 : (vis_lbreaks[l+1] - vis_lbreaks[l]))
661#define INV_LLEN(l) (inv_lbreaks[l+1] - inv_lbreaks[l])
662#define VIS_CHARS(line) (visible_line + vis_lbreaks[line])
726f6388 663#define VIS_LINE(line) ((line) > _rl_vis_botlin) ? "" : VIS_CHARS(line)
ccc6cda3 664#define INV_LINE(line) (invisible_line + inv_lbreaks[line])
726f6388
JA
665
666 /* For each line in the buffer, do the updating display. */
667 for (linenum = 0; linenum <= inv_botlin; linenum++)
668 {
669 update_line (VIS_LINE(linenum), INV_LINE(linenum), linenum,
ccc6cda3 670 VIS_LLEN(linenum), INV_LLEN(linenum), inv_botlin);
726f6388
JA
671
672 /* If this is the line with the prompt, we might need to
673 compensate for invisible characters in the new line. Do
674 this only if there is not more than one new line (which
675 implies that we completely overwrite the old visible line)
ccc6cda3
JA
676 and the new line is shorter than the old. Make sure we are
677 at the end of the new line before clearing. */
726f6388 678 if (linenum == 0 &&
ccc6cda3 679 inv_botlin == 0 && _rl_last_c_pos == out &&
726f6388
JA
680 (wrap_offset > visible_wrap_offset) &&
681 (_rl_last_c_pos < visible_first_line_len))
682 {
28ef6c31 683 nleft = _rl_screenwidth + wrap_offset - _rl_last_c_pos;
726f6388 684 if (nleft)
d166f048 685 _rl_clear_to_eol (nleft);
726f6388
JA
686 }
687
688 /* Since the new first line is now visible, save its length. */
689 if (linenum == 0)
ccc6cda3 690 visible_first_line_len = (inv_botlin > 0) ? inv_lbreaks[1] : out - wrap_offset;
726f6388
JA
691 }
692
693 /* We may have deleted some lines. If so, clear the left over
694 blank ones at the bottom out. */
695 if (_rl_vis_botlin > inv_botlin)
696 {
697 char *tt;
698 for (; linenum <= _rl_vis_botlin; linenum++)
699 {
700 tt = VIS_CHARS (linenum);
701 _rl_move_vert (linenum);
702 _rl_move_cursor_relative (0, tt);
d166f048 703 _rl_clear_to_eol
28ef6c31 704 ((linenum == _rl_vis_botlin) ? strlen (tt) : _rl_screenwidth);
726f6388
JA
705 }
706 }
707 _rl_vis_botlin = inv_botlin;
708
726f6388
JA
709 /* CHANGED_SCREEN_LINE is set to 1 if we have moved to a
710 different screen line during this redisplay. */
711 changed_screen_line = _rl_last_v_pos != cursor_linenum;
712 if (changed_screen_line)
713 {
714 _rl_move_vert (cursor_linenum);
28ef6c31 715 /* If we moved up to the line with the prompt using _rl_term_up,
b72432fd
JA
716 the physical cursor position on the screen stays the same,
717 but the buffer position needs to be adjusted to account
718 for invisible characters. */
726f6388 719 if (cursor_linenum == 0 && wrap_offset)
b72432fd 720 _rl_last_c_pos += wrap_offset;
726f6388
JA
721 }
722
723 /* We have to reprint the prompt if it contains invisible
724 characters, since it's not generally OK to just reprint
ccc6cda3
JA
725 the characters from the current cursor position. But we
726 only need to reprint it if the cursor is before the last
727 invisible character in the prompt string. */
28ef6c31 728 nleft = prompt_visible_length + wrap_offset;
726f6388 729 if (cursor_linenum == 0 && wrap_offset > 0 && _rl_last_c_pos > 0 &&
28ef6c31 730 _rl_last_c_pos <= prompt_last_invisible && local_prompt)
726f6388 731 {
bb70624e
JA
732#if defined (__MSDOS__)
733 putc ('\r', rl_outstream);
734#else
28ef6c31
JA
735 if (_rl_term_cr)
736 tputs (_rl_term_cr, 1, _rl_output_character_function);
bb70624e 737#endif
726f6388
JA
738 _rl_output_some_chars (local_prompt, nleft);
739 _rl_last_c_pos = nleft;
740 }
741
742 /* Where on that line? And where does that line start
743 in the buffer? */
ccc6cda3 744 pos = inv_lbreaks[cursor_linenum];
726f6388
JA
745 /* nleft == number of characters in the line buffer between the
746 start of the line and the cursor position. */
747 nleft = c_pos - pos;
748
ccc6cda3 749 /* Since _rl_backspace() doesn't know about invisible characters in the
726f6388 750 prompt, and there's no good way to tell it, we compensate for
ccc6cda3 751 those characters here and call _rl_backspace() directly. */
726f6388
JA
752 if (wrap_offset && cursor_linenum == 0 && nleft < _rl_last_c_pos)
753 {
ccc6cda3 754 _rl_backspace (_rl_last_c_pos - nleft);
726f6388
JA
755 _rl_last_c_pos = nleft;
756 }
757
758 if (nleft != _rl_last_c_pos)
759 _rl_move_cursor_relative (nleft, &invisible_line[pos]);
760 }
761 }
762 else /* Do horizontal scrolling. */
763 {
764#define M_OFFSET(margin, offset) ((margin) == 0 ? offset : 0)
765 int lmargin, ndisp, nleft, phys_c_pos, t;
766
767 /* Always at top line. */
768 _rl_last_v_pos = 0;
769
770 /* Compute where in the buffer the displayed line should start. This
771 will be LMARGIN. */
772
773 /* The number of characters that will be displayed before the cursor. */
774 ndisp = c_pos - wrap_offset;
28ef6c31 775 nleft = prompt_visible_length + wrap_offset;
726f6388 776 /* Where the new cursor position will be on the screen. This can be
b72432fd 777 longer than SCREENWIDTH; if it is, lmargin will be adjusted. */
726f6388 778 phys_c_pos = c_pos - (last_lmargin ? last_lmargin : wrap_offset);
28ef6c31 779 t = _rl_screenwidth / 3;
726f6388
JA
780
781 /* If the number of characters had already exceeded the screenwidth,
b72432fd 782 last_lmargin will be > 0. */
726f6388
JA
783
784 /* If the number of characters to be displayed is more than the screen
b72432fd
JA
785 width, compute the starting offset so that the cursor is about
786 two-thirds of the way across the screen. */
28ef6c31 787 if (phys_c_pos > _rl_screenwidth - 2)
726f6388
JA
788 {
789 lmargin = c_pos - (2 * t);
790 if (lmargin < 0)
791 lmargin = 0;
792 /* If the left margin would be in the middle of a prompt with
793 invisible characters, don't display the prompt at all. */
794 if (wrap_offset && lmargin > 0 && lmargin < nleft)
795 lmargin = nleft;
796 }
28ef6c31 797 else if (ndisp < _rl_screenwidth - 2) /* XXX - was -1 */
b72432fd 798 lmargin = 0;
726f6388
JA
799 else if (phys_c_pos < 1)
800 {
801 /* If we are moving back towards the beginning of the line and
802 the last margin is no longer correct, compute a new one. */
803 lmargin = ((c_pos - 1) / t) * t; /* XXX */
804 if (wrap_offset && lmargin > 0 && lmargin < nleft)
805 lmargin = nleft;
806 }
807 else
b72432fd 808 lmargin = last_lmargin;
726f6388
JA
809
810 /* If the first character on the screen isn't the first character
811 in the display line, indicate this with a special character. */
812 if (lmargin > 0)
813 line[lmargin] = '<';
814
815 /* If SCREENWIDTH characters starting at LMARGIN do not encompass
b72432fd
JA
816 the whole line, indicate that with a special character at the
817 right edge of the screen. If LMARGIN is 0, we need to take the
818 wrap offset into account. */
28ef6c31 819 t = lmargin + M_OFFSET (lmargin, wrap_offset) + _rl_screenwidth;
726f6388 820 if (t < out)
b72432fd 821 line[t - 1] = '>';
726f6388
JA
822
823 if (!rl_display_fixed || forced_display || lmargin != last_lmargin)
824 {
825 forced_display = 0;
826 update_line (&visible_line[last_lmargin],
827 &invisible_line[lmargin],
828 0,
28ef6c31
JA
829 _rl_screenwidth + visible_wrap_offset,
830 _rl_screenwidth + (lmargin ? 0 : wrap_offset),
726f6388
JA
831 0);
832
833 /* If the visible new line is shorter than the old, but the number
834 of invisible characters is greater, and we are at the end of
835 the new line, we need to clear to eol. */
836 t = _rl_last_c_pos - M_OFFSET (lmargin, wrap_offset);
837 if ((M_OFFSET (lmargin, wrap_offset) > visible_wrap_offset) &&
838 (_rl_last_c_pos == out) &&
839 t < visible_first_line_len)
840 {
28ef6c31 841 nleft = _rl_screenwidth - t;
d166f048 842 _rl_clear_to_eol (nleft);
726f6388
JA
843 }
844 visible_first_line_len = out - lmargin - M_OFFSET (lmargin, wrap_offset);
28ef6c31
JA
845 if (visible_first_line_len > _rl_screenwidth)
846 visible_first_line_len = _rl_screenwidth;
726f6388
JA
847
848 _rl_move_cursor_relative (c_pos - lmargin, &invisible_line[lmargin]);
849 last_lmargin = lmargin;
850 }
851 }
852 fflush (rl_outstream);
853
854 /* Swap visible and non-visible lines. */
855 {
28ef6c31 856 char *vtemp = visible_line;
bb70624e
JA
857 int *itemp = vis_lbreaks, ntemp = vis_lbsize;
858
726f6388 859 visible_line = invisible_line;
28ef6c31 860 invisible_line = vtemp;
bb70624e 861
ccc6cda3
JA
862 vis_lbreaks = inv_lbreaks;
863 inv_lbreaks = itemp;
bb70624e
JA
864
865 vis_lbsize = inv_lbsize;
866 inv_lbsize = ntemp;
867
726f6388
JA
868 rl_display_fixed = 0;
869 /* If we are displaying on a single line, and last_lmargin is > 0, we
870 are not displaying any invisible characters, so set visible_wrap_offset
871 to 0. */
872 if (_rl_horizontal_scroll_mode && last_lmargin)
873 visible_wrap_offset = 0;
874 else
875 visible_wrap_offset = wrap_offset;
876 }
877}
878
879/* PWP: update_line() is based on finding the middle difference of each
880 line on the screen; vis:
881
882 /old first difference
883 /beginning of line | /old last same /old EOL
884 v v v v
885old: eddie> Oh, my little gruntle-buggy is to me, as lurgid as
886new: eddie> Oh, my little buggy says to me, as lurgid as
887 ^ ^ ^ ^
888 \beginning of line | \new last same \new end of line
889 \new first difference
890
891 All are character pointers for the sake of speed. Special cases for
b72432fd 892 no differences, as well as for end of line additions must be handled.
726f6388
JA
893
894 Could be made even smarter, but this works well enough */
895static void
896update_line (old, new, current_line, omax, nmax, inv_botlin)
897 register char *old, *new;
ccc6cda3 898 int current_line, omax, nmax, inv_botlin;
726f6388
JA
899{
900 register char *ofd, *ols, *oe, *nfd, *nls, *ne;
901 int temp, lendiff, wsatend, od, nd;
ccc6cda3 902 int current_invis_chars;
726f6388
JA
903
904 /* If we're at the right edge of a terminal that supports xn, we're
905 ready to wrap around, so do so. This fixes problems with knowing
906 the exact cursor position and cut-and-paste with certain terminal
907 emulators. In this calculation, TEMP is the physical screen
908 position of the cursor. */
909 temp = _rl_last_c_pos - W_OFFSET(_rl_last_v_pos, visible_wrap_offset);
28ef6c31 910 if (temp == _rl_screenwidth && _rl_term_autowrap && !_rl_horizontal_scroll_mode
726f6388
JA
911 && _rl_last_v_pos == current_line - 1)
912 {
913 if (new[0])
914 putc (new[0], rl_outstream);
915 else
916 putc (' ', rl_outstream);
917 _rl_last_c_pos = 1; /* XXX */
918 _rl_last_v_pos++;
919 if (old[0] && new[0])
b72432fd 920 old[0] = new[0];
726f6388
JA
921 }
922
923 /* Find first difference. */
924 for (ofd = old, nfd = new;
925 (ofd - old < omax) && *ofd && (*ofd == *nfd);
926 ofd++, nfd++)
927 ;
928
929 /* Move to the end of the screen line. ND and OD are used to keep track
930 of the distance between ne and new and oe and old, respectively, to
931 move a subtraction out of each loop. */
932 for (od = ofd - old, oe = ofd; od < omax && *oe; oe++, od++);
933 for (nd = nfd - new, ne = nfd; nd < nmax && *ne; ne++, nd++);
934
935 /* If no difference, continue to next line. */
936 if (ofd == oe && nfd == ne)
937 return;
938
939 wsatend = 1; /* flag for trailing whitespace */
940 ols = oe - 1; /* find last same */
941 nls = ne - 1;
942 while ((ols > ofd) && (nls > nfd) && (*ols == *nls))
943 {
944 if (*ols != ' ')
945 wsatend = 0;
946 ols--;
947 nls--;
948 }
949
950 if (wsatend)
951 {
952 ols = oe;
953 nls = ne;
954 }
955 else if (*ols != *nls)
956 {
957 if (*ols) /* don't step past the NUL */
958 ols++;
959 if (*nls)
960 nls++;
961 }
962
ccc6cda3
JA
963 /* count of invisible characters in the current invisible line. */
964 current_invis_chars = W_OFFSET (current_line, wrap_offset);
965 if (_rl_last_v_pos != current_line)
966 {
967 _rl_move_vert (current_line);
968 if (current_line == 0 && visible_wrap_offset)
969 _rl_last_c_pos += visible_wrap_offset;
970 }
726f6388
JA
971
972 /* If this is the first line and there are invisible characters in the
ccc6cda3
JA
973 prompt string, and the prompt string has not changed, and the current
974 cursor position is before the last invisible character in the prompt,
975 and the index of the character to move to is past the end of the prompt
976 string, then redraw the entire prompt string. We can only do this
977 reliably if the terminal supports a `cr' capability.
726f6388 978
ccc6cda3
JA
979 This is not an efficiency hack -- there is a problem with redrawing
980 portions of the prompt string if they contain terminal escape
981 sequences (like drawing the `unbold' sequence without a corresponding
982 `bold') that manifests itself on certain terminals. */
726f6388
JA
983
984 lendiff = local_prompt ? strlen (local_prompt) : 0;
ccc6cda3 985 od = ofd - old; /* index of first difference in visible line */
726f6388 986 if (current_line == 0 && !_rl_horizontal_scroll_mode &&
28ef6c31
JA
987 _rl_term_cr && lendiff > prompt_visible_length && _rl_last_c_pos > 0 &&
988 od >= lendiff && _rl_last_c_pos <= prompt_last_invisible)
726f6388 989 {
bb70624e
JA
990#if defined (__MSDOS__)
991 putc ('\r', rl_outstream);
992#else
28ef6c31 993 tputs (_rl_term_cr, 1, _rl_output_character_function);
bb70624e 994#endif
726f6388
JA
995 _rl_output_some_chars (local_prompt, lendiff);
996 _rl_last_c_pos = lendiff;
997 }
998
ccc6cda3 999 _rl_move_cursor_relative (od, old);
726f6388
JA
1000
1001 /* if (len (new) > len (old)) */
1002 lendiff = (nls - nfd) - (ols - ofd);
1003
ccc6cda3
JA
1004 /* If we are changing the number of invisible characters in a line, and
1005 the spot of first difference is before the end of the invisible chars,
1006 lendiff needs to be adjusted. */
1007 if (current_line == 0 && !_rl_horizontal_scroll_mode &&
1008 current_invis_chars != visible_wrap_offset)
b72432fd 1009 lendiff += visible_wrap_offset - current_invis_chars;
ccc6cda3 1010
726f6388
JA
1011 /* Insert (diff (len (old), len (new)) ch. */
1012 temp = ne - nfd;
1013 if (lendiff > 0)
1014 {
1015 /* Non-zero if we're increasing the number of lines. */
1016 int gl = current_line >= _rl_vis_botlin && inv_botlin > _rl_vis_botlin;
1017 /* Sometimes it is cheaper to print the characters rather than
1018 use the terminal's capabilities. If we're growing the number
1019 of lines, make sure we actually cause the new line to wrap
1020 around on auto-wrapping terminals. */
28ef6c31 1021 if (_rl_terminal_can_insert && ((2 * temp) >= lendiff || _rl_term_IC) && (!_rl_term_autowrap || !gl))
726f6388 1022 {
28ef6c31 1023 /* If lendiff > prompt_visible_length and _rl_last_c_pos == 0 and
726f6388 1024 _rl_horizontal_scroll_mode == 1, inserting the characters with
28ef6c31 1025 _rl_term_IC or _rl_term_ic will screw up the screen because of the
726f6388
JA
1026 invisible characters. We need to just draw them. */
1027 if (*ols && (!_rl_horizontal_scroll_mode || _rl_last_c_pos > 0 ||
28ef6c31 1028 lendiff <= prompt_visible_length || !current_invis_chars))
726f6388
JA
1029 {
1030 insert_some_chars (nfd, lendiff);
1031 _rl_last_c_pos += lendiff;
1032 }
ccc6cda3 1033 else if (*ols == 0)
726f6388
JA
1034 {
1035 /* At the end of a line the characters do not have to
1036 be "inserted". They can just be placed on the screen. */
ccc6cda3 1037 /* However, this screws up the rest of this block, which
b72432fd 1038 assumes you've done the insert because you can. */
726f6388
JA
1039 _rl_output_some_chars (nfd, lendiff);
1040 _rl_last_c_pos += lendiff;
1041 }
ccc6cda3
JA
1042 else
1043 {
1044 /* We have horizontal scrolling and we are not inserting at
1045 the end. We have invisible characters in this line. This
1046 is a dumb update. */
1047 _rl_output_some_chars (nfd, temp);
1048 _rl_last_c_pos += temp;
1049 return;
1050 }
726f6388
JA
1051 /* Copy (new) chars to screen from first diff to last match. */
1052 temp = nls - nfd;
1053 if ((temp - lendiff) > 0)
1054 {
1055 _rl_output_some_chars (nfd + lendiff, temp - lendiff);
1056 _rl_last_c_pos += temp - lendiff;
1057 }
1058 }
1059 else
1060 {
1061 /* cannot insert chars, write to EOL */
1062 _rl_output_some_chars (nfd, temp);
1063 _rl_last_c_pos += temp;
1064 }
1065 }
1066 else /* Delete characters from line. */
1067 {
1068 /* If possible and inexpensive to use terminal deletion, then do so. */
28ef6c31 1069 if (_rl_term_dc && (2 * temp) >= -lendiff)
726f6388
JA
1070 {
1071 /* If all we're doing is erasing the invisible characters in the
1072 prompt string, don't bother. It screws up the assumptions
1073 about what's on the screen. */
1074 if (_rl_horizontal_scroll_mode && _rl_last_c_pos == 0 &&
1075 -lendiff == visible_wrap_offset)
1076 lendiff = 0;
1077
1078 if (lendiff)
1079 delete_chars (-lendiff); /* delete (diff) characters */
1080
1081 /* Copy (new) chars to screen from first diff to last match */
1082 temp = nls - nfd;
1083 if (temp > 0)
1084 {
1085 _rl_output_some_chars (nfd, temp);
1086 _rl_last_c_pos += temp;
1087 }
1088 }
1089 /* Otherwise, print over the existing material. */
1090 else
1091 {
1092 if (temp > 0)
1093 {
1094 _rl_output_some_chars (nfd, temp);
1095 _rl_last_c_pos += temp;
1096 }
1097 lendiff = (oe - old) - (ne - new);
b72432fd
JA
1098 if (lendiff)
1099 {
1100 if (_rl_term_autowrap && current_line < inv_botlin)
1101 space_to_eol (lendiff);
1102 else
1103 _rl_clear_to_eol (lendiff);
1104 }
726f6388
JA
1105 }
1106 }
1107}
1108
1109/* Tell the update routines that we have moved onto a new (empty) line. */
ccc6cda3 1110int
726f6388
JA
1111rl_on_new_line ()
1112{
1113 if (visible_line)
1114 visible_line[0] = '\0';
1115
1116 _rl_last_c_pos = _rl_last_v_pos = 0;
1117 _rl_vis_botlin = last_lmargin = 0;
ccc6cda3
JA
1118 if (vis_lbreaks)
1119 vis_lbreaks[0] = vis_lbreaks[1] = 0;
1120 visible_wrap_offset = 0;
726f6388
JA
1121 return 0;
1122}
1123
bb70624e
JA
1124/* Tell the update routines that we have moved onto a new line with the
1125 prompt already displayed. Code originally from the version of readline
1126 distributed with CLISP. */
1127int
1128rl_on_new_line_with_prompt ()
1129{
1130 int prompt_size, i, l, real_screenwidth, newlines;
1131 char *prompt_last_line;
1132
1133 /* Initialize visible_line and invisible_line to ensure that they can hold
1134 the already-displayed prompt. */
1135 prompt_size = strlen (rl_prompt) + 1;
1136 init_line_structures (prompt_size);
1137
1138 /* Make sure the line structures hold the already-displayed prompt for
1139 redisplay. */
1140 strcpy (visible_line, rl_prompt);
1141 strcpy (invisible_line, rl_prompt);
1142
1143 /* If the prompt contains newlines, take the last tail. */
1144 prompt_last_line = strrchr (rl_prompt, '\n');
1145 if (!prompt_last_line)
1146 prompt_last_line = rl_prompt;
1147
1148 l = strlen (prompt_last_line);
1149 _rl_last_c_pos = l;
1150
1151 /* Dissect prompt_last_line into screen lines. Note that here we have
1152 to use the real screenwidth. Readline's notion of screenwidth might be
1153 one less, see terminal.c. */
28ef6c31 1154 real_screenwidth = _rl_screenwidth + (_rl_term_autowrap ? 0 : 1);
bb70624e
JA
1155 _rl_last_v_pos = l / real_screenwidth;
1156 /* If the prompt length is a multiple of real_screenwidth, we don't know
1157 whether the cursor is at the end of the last line, or already at the
1158 beginning of the next line. Output a newline just to be safe. */
1159 if (l > 0 && (l % real_screenwidth) == 0)
1160 _rl_output_some_chars ("\n", 1);
1161 last_lmargin = 0;
1162
1163 newlines = 0; i = 0;
1164 while (i <= l)
1165 {
1166 _rl_vis_botlin = newlines;
1167 vis_lbreaks[newlines++] = i;
1168 i += real_screenwidth;
1169 }
1170 vis_lbreaks[newlines] = l;
1171 visible_wrap_offset = 0;
1172
1173 return 0;
1174}
1175
726f6388 1176/* Actually update the display, period. */
ccc6cda3 1177int
726f6388
JA
1178rl_forced_update_display ()
1179{
1180 if (visible_line)
1181 {
1182 register char *temp = visible_line;
1183
ccc6cda3 1184 while (*temp)
b72432fd 1185 *temp++ = '\0';
726f6388
JA
1186 }
1187 rl_on_new_line ();
1188 forced_display++;
ccc6cda3 1189 (*rl_redisplay_function) ();
726f6388
JA
1190 return 0;
1191}
1192
1193/* Move the cursor from _rl_last_c_pos to NEW, which are buffer indices.
1194 DATA is the contents of the screen line of interest; i.e., where
1195 the movement is being done. */
1196void
1197_rl_move_cursor_relative (new, data)
1198 int new;
28ef6c31 1199 const char *data;
726f6388
JA
1200{
1201 register int i;
1202
1203 /* If we don't have to do anything, then return. */
1204 if (_rl_last_c_pos == new) return;
1205
1206 /* It may be faster to output a CR, and then move forwards instead
1207 of moving backwards. */
1208 /* i == current physical cursor position. */
1209 i = _rl_last_c_pos - W_OFFSET(_rl_last_v_pos, visible_wrap_offset);
ccc6cda3 1210 if (new == 0 || CR_FASTER (new, _rl_last_c_pos) ||
28ef6c31 1211 (_rl_term_autowrap && i == _rl_screenwidth))
726f6388
JA
1212 {
1213#if defined (__MSDOS__)
1214 putc ('\r', rl_outstream);
1215#else
28ef6c31 1216 tputs (_rl_term_cr, 1, _rl_output_character_function);
726f6388
JA
1217#endif /* !__MSDOS__ */
1218 _rl_last_c_pos = 0;
1219 }
1220
1221 if (_rl_last_c_pos < new)
1222 {
1223 /* Move the cursor forward. We do it by printing the command
1224 to move the cursor forward if there is one, else print that
1225 portion of the output buffer again. Which is cheaper? */
1226
1227 /* The above comment is left here for posterity. It is faster
1228 to print one character (non-control) than to print a control
1229 sequence telling the terminal to move forward one character.
1230 That kind of control is for people who don't know what the
1231 data is underneath the cursor. */
1232#if defined (HACK_TERMCAP_MOTION)
28ef6c31 1233 if (_rl_term_forward_char)
726f6388 1234 for (i = _rl_last_c_pos; i < new; i++)
28ef6c31 1235 tputs (_rl_term_forward_char, 1, _rl_output_character_function);
726f6388
JA
1236 else
1237 for (i = _rl_last_c_pos; i < new; i++)
1238 putc (data[i], rl_outstream);
1239#else
1240 for (i = _rl_last_c_pos; i < new; i++)
1241 putc (data[i], rl_outstream);
1242#endif /* HACK_TERMCAP_MOTION */
1243 }
b72432fd 1244 else if (_rl_last_c_pos > new)
ccc6cda3 1245 _rl_backspace (_rl_last_c_pos - new);
726f6388
JA
1246 _rl_last_c_pos = new;
1247}
1248
1249/* PWP: move the cursor up or down. */
1250void
1251_rl_move_vert (to)
1252 int to;
1253{
1254 register int delta, i;
1255
28ef6c31 1256 if (_rl_last_v_pos == to || to > _rl_screenheight)
726f6388
JA
1257 return;
1258
726f6388
JA
1259 if ((delta = to - _rl_last_v_pos) > 0)
1260 {
1261 for (i = 0; i < delta; i++)
1262 putc ('\n', rl_outstream);
bb70624e
JA
1263#if defined (__MSDOS__)
1264 putc ('\r', rl_outstream);
1265#else
28ef6c31 1266 tputs (_rl_term_cr, 1, _rl_output_character_function);
bb70624e 1267#endif
726f6388
JA
1268 _rl_last_c_pos = 0;
1269 }
1270 else
1271 { /* delta < 0 */
28ef6c31 1272 if (_rl_term_up && *_rl_term_up)
726f6388 1273 for (i = 0; i < -delta; i++)
28ef6c31 1274 tputs (_rl_term_up, 1, _rl_output_character_function);
726f6388 1275 }
bb70624e 1276
726f6388
JA
1277 _rl_last_v_pos = to; /* Now TO is here */
1278}
1279
1280/* Physically print C on rl_outstream. This is for functions which know
1281 how to optimize the display. Return the number of characters output. */
ccc6cda3 1282int
726f6388
JA
1283rl_show_char (c)
1284 int c;
1285{
1286 int n = 1;
1287 if (META_CHAR (c) && (_rl_output_meta_chars == 0))
1288 {
1289 fprintf (rl_outstream, "M-");
1290 n += 2;
1291 c = UNMETA (c);
1292 }
1293
1294#if defined (DISPLAY_TABS)
ccc6cda3 1295 if ((CTRL_CHAR (c) && c != '\t') || c == RUBOUT)
726f6388 1296#else
ccc6cda3 1297 if (CTRL_CHAR (c) || c == RUBOUT)
726f6388
JA
1298#endif /* !DISPLAY_TABS */
1299 {
1300 fprintf (rl_outstream, "C-");
1301 n += 2;
ccc6cda3 1302 c = CTRL_CHAR (c) ? UNCTRL (c) : '?';
726f6388
JA
1303 }
1304
1305 putc (c, rl_outstream);
1306 fflush (rl_outstream);
1307 return n;
1308}
1309
1310int
1311rl_character_len (c, pos)
1312 register int c, pos;
1313{
1314 unsigned char uc;
1315
1316 uc = (unsigned char)c;
1317
1318 if (META_CHAR (uc))
1319 return ((_rl_output_meta_chars == 0) ? 4 : 1);
1320
1321 if (uc == '\t')
1322 {
1323#if defined (DISPLAY_TABS)
1324 return (((pos | 7) + 1) - pos);
1325#else
1326 return (2);
1327#endif /* !DISPLAY_TABS */
1328 }
1329
ccc6cda3
JA
1330 if (CTRL_CHAR (c) || c == RUBOUT)
1331 return (2);
1332
726f6388
JA
1333 return ((isprint (uc)) ? 1 : 2);
1334}
1335
1336/* How to print things in the "echo-area". The prompt is treated as a
1337 mini-modeline. */
1338
ccc6cda3
JA
1339#if defined (USE_VARARGS)
1340int
1341#if defined (PREFER_STDARG)
1342rl_message (const char *format, ...)
1343#else
726f6388
JA
1344rl_message (va_alist)
1345 va_dcl
ccc6cda3 1346#endif
726f6388 1347{
726f6388 1348 va_list args;
ccc6cda3
JA
1349#if defined (PREFER_VARARGS)
1350 char *format;
1351#endif
726f6388 1352
ccc6cda3
JA
1353#if defined (PREFER_STDARG)
1354 va_start (args, format);
1355#else
726f6388
JA
1356 va_start (args);
1357 format = va_arg (args, char *);
ccc6cda3
JA
1358#endif
1359
726f6388
JA
1360 vsprintf (msg_buf, format, args);
1361 va_end (args);
1362
1363 rl_display_prompt = msg_buf;
ccc6cda3 1364 (*rl_redisplay_function) ();
726f6388
JA
1365 return 0;
1366}
ccc6cda3
JA
1367#else /* !USE_VARARGS */
1368int
726f6388
JA
1369rl_message (format, arg1, arg2)
1370 char *format;
1371{
1372 sprintf (msg_buf, format, arg1, arg2);
1373 rl_display_prompt = msg_buf;
ccc6cda3 1374 (*rl_redisplay_function) ();
726f6388
JA
1375 return 0;
1376}
ccc6cda3 1377#endif /* !USE_VARARGS */
726f6388
JA
1378
1379/* How to clear things from the "echo-area". */
ccc6cda3 1380int
726f6388
JA
1381rl_clear_message ()
1382{
1383 rl_display_prompt = rl_prompt;
ccc6cda3 1384 (*rl_redisplay_function) ();
726f6388
JA
1385 return 0;
1386}
1387
ccc6cda3 1388int
726f6388
JA
1389rl_reset_line_state ()
1390{
1391 rl_on_new_line ();
1392
1393 rl_display_prompt = rl_prompt ? rl_prompt : "";
1394 forced_display = 1;
1395 return 0;
1396}
1397
ccc6cda3
JA
1398static char *saved_local_prompt;
1399static char *saved_local_prefix;
1400static int saved_last_invisible;
1401static int saved_visible_length;
1402
1403void
b72432fd 1404rl_save_prompt ()
ccc6cda3
JA
1405{
1406 saved_local_prompt = local_prompt;
1407 saved_local_prefix = local_prompt_prefix;
28ef6c31
JA
1408 saved_last_invisible = prompt_last_invisible;
1409 saved_visible_length = prompt_visible_length;
ccc6cda3
JA
1410
1411 local_prompt = local_prompt_prefix = (char *)0;
28ef6c31 1412 prompt_last_invisible = prompt_visible_length = 0;
ccc6cda3
JA
1413}
1414
1415void
b72432fd 1416rl_restore_prompt ()
ccc6cda3 1417{
28ef6c31
JA
1418 FREE (local_prompt);
1419 FREE (local_prompt_prefix);
ccc6cda3
JA
1420
1421 local_prompt = saved_local_prompt;
1422 local_prompt_prefix = saved_local_prefix;
28ef6c31
JA
1423 prompt_last_invisible = saved_last_invisible;
1424 prompt_visible_length = saved_visible_length;
ccc6cda3
JA
1425}
1426
1427char *
1428_rl_make_prompt_for_search (pchar)
1429 int pchar;
1430{
1431 int len;
1432 char *pmt;
1433
b72432fd 1434 rl_save_prompt ();
ccc6cda3
JA
1435
1436 if (saved_local_prompt == 0)
1437 {
1438 len = (rl_prompt && *rl_prompt) ? strlen (rl_prompt) : 0;
1439 pmt = xmalloc (len + 2);
1440 if (len)
b72432fd 1441 strcpy (pmt, rl_prompt);
ccc6cda3
JA
1442 pmt[len] = pchar;
1443 pmt[len+1] = '\0';
1444 }
1445 else
1446 {
1447 len = *saved_local_prompt ? strlen (saved_local_prompt) : 0;
1448 pmt = xmalloc (len + 2);
1449 if (len)
b72432fd 1450 strcpy (pmt, saved_local_prompt);
ccc6cda3
JA
1451 pmt[len] = pchar;
1452 pmt[len+1] = '\0';
1453 local_prompt = savestring (pmt);
28ef6c31
JA
1454 prompt_last_invisible = saved_last_invisible;
1455 prompt_visible_length = saved_visible_length + 1;
ccc6cda3
JA
1456 }
1457 return pmt;
1458}
1459
726f6388
JA
1460/* Quick redisplay hack when erasing characters at the end of the line. */
1461void
1462_rl_erase_at_end_of_line (l)
1463 int l;
1464{
1465 register int i;
1466
ccc6cda3 1467 _rl_backspace (l);
726f6388
JA
1468 for (i = 0; i < l; i++)
1469 putc (' ', rl_outstream);
ccc6cda3 1470 _rl_backspace (l);
726f6388
JA
1471 for (i = 0; i < l; i++)
1472 visible_line[--_rl_last_c_pos] = '\0';
1473 rl_display_fixed++;
1474}
1475
1476/* Clear to the end of the line. COUNT is the minimum
1477 number of character spaces to clear, */
d166f048
JA
1478void
1479_rl_clear_to_eol (count)
726f6388
JA
1480 int count;
1481{
28ef6c31
JA
1482 if (_rl_term_clreol)
1483 tputs (_rl_term_clreol, 1, _rl_output_character_function);
d166f048 1484 else if (count)
726f6388
JA
1485 space_to_eol (count);
1486}
1487
1488/* Clear to the end of the line using spaces. COUNT is the minimum
1489 number of character spaces to clear, */
1490static void
1491space_to_eol (count)
1492 int count;
1493{
1494 register int i;
1495
1496 for (i = 0; i < count; i++)
1497 putc (' ', rl_outstream);
1498
1499 _rl_last_c_pos += count;
1500}
1501
d166f048
JA
1502void
1503_rl_clear_screen ()
1504{
28ef6c31
JA
1505 if (_rl_term_clrpag)
1506 tputs (_rl_term_clrpag, 1, _rl_output_character_function);
d166f048 1507 else
28ef6c31 1508 rl_crlf ();
d166f048
JA
1509}
1510
726f6388
JA
1511/* Insert COUNT characters from STRING to the output stream. */
1512static void
1513insert_some_chars (string, count)
1514 char *string;
1515 int count;
1516{
726f6388 1517 /* If IC is defined, then we do not have to "enter" insert mode. */
28ef6c31 1518 if (_rl_term_IC)
726f6388 1519 {
ccc6cda3 1520 char *buffer;
28ef6c31 1521 buffer = tgoto (_rl_term_IC, 0, count);
726f6388
JA
1522 tputs (buffer, 1, _rl_output_character_function);
1523 _rl_output_some_chars (string, count);
1524 }
1525 else
1526 {
1527 register int i;
1528
1529 /* If we have to turn on insert-mode, then do so. */
28ef6c31
JA
1530 if (_rl_term_im && *_rl_term_im)
1531 tputs (_rl_term_im, 1, _rl_output_character_function);
726f6388
JA
1532
1533 /* If there is a special command for inserting characters, then
1534 use that first to open up the space. */
28ef6c31 1535 if (_rl_term_ic && *_rl_term_ic)
726f6388
JA
1536 {
1537 for (i = count; i--; )
28ef6c31 1538 tputs (_rl_term_ic, 1, _rl_output_character_function);
726f6388
JA
1539 }
1540
1541 /* Print the text. */
1542 _rl_output_some_chars (string, count);
1543
1544 /* If there is a string to turn off insert mode, we had best use
1545 it now. */
28ef6c31
JA
1546 if (_rl_term_ei && *_rl_term_ei)
1547 tputs (_rl_term_ei, 1, _rl_output_character_function);
726f6388 1548 }
726f6388
JA
1549}
1550
1551/* Delete COUNT characters from the display line. */
1552static void
1553delete_chars (count)
1554 int count;
1555{
28ef6c31 1556 if (count > _rl_screenwidth) /* XXX */
726f6388
JA
1557 return;
1558
28ef6c31 1559 if (_rl_term_DC && *_rl_term_DC)
726f6388 1560 {
ccc6cda3 1561 char *buffer;
28ef6c31 1562 buffer = tgoto (_rl_term_DC, count, count);
726f6388
JA
1563 tputs (buffer, count, _rl_output_character_function);
1564 }
1565 else
1566 {
28ef6c31 1567 if (_rl_term_dc && *_rl_term_dc)
726f6388 1568 while (count--)
28ef6c31 1569 tputs (_rl_term_dc, 1, _rl_output_character_function);
726f6388 1570 }
726f6388
JA
1571}
1572
1573void
1574_rl_update_final ()
1575{
1576 int full_lines;
1577
1578 full_lines = 0;
ccc6cda3
JA
1579 /* If the cursor is the only thing on an otherwise-blank last line,
1580 compensate so we don't print an extra CRLF. */
1581 if (_rl_vis_botlin && _rl_last_c_pos == 0 &&
d166f048 1582 visible_line[vis_lbreaks[_rl_vis_botlin]] == 0)
726f6388
JA
1583 {
1584 _rl_vis_botlin--;
1585 full_lines = 1;
1586 }
1587 _rl_move_vert (_rl_vis_botlin);
ccc6cda3 1588 /* If we've wrapped lines, remove the final xterm line-wrap flag. */
28ef6c31 1589 if (full_lines && _rl_term_autowrap && (VIS_LLEN(_rl_vis_botlin) == _rl_screenwidth))
726f6388 1590 {
726f6388 1591 char *last_line;
bb70624e 1592#if 0
ccc6cda3 1593 last_line = &visible_line[inv_lbreaks[_rl_vis_botlin]];
bb70624e
JA
1594#else
1595 last_line = &visible_line[vis_lbreaks[_rl_vis_botlin]];
1596#endif
28ef6c31 1597 _rl_move_cursor_relative (_rl_screenwidth - 1, last_line);
d166f048 1598 _rl_clear_to_eol (0);
28ef6c31 1599 putc (last_line[_rl_screenwidth - 1], rl_outstream);
726f6388
JA
1600 }
1601 _rl_vis_botlin = 0;
28ef6c31 1602 rl_crlf ();
726f6388
JA
1603 fflush (rl_outstream);
1604 rl_display_fixed++;
1605}
1606
1607/* Move to the start of the current line. */
1608static void
1609cr ()
1610{
28ef6c31 1611 if (_rl_term_cr)
726f6388 1612 {
bb70624e
JA
1613#if defined (__MSDOS__)
1614 putc ('\r', rl_outstream);
1615#else
28ef6c31 1616 tputs (_rl_term_cr, 1, _rl_output_character_function);
bb70624e 1617#endif
726f6388
JA
1618 _rl_last_c_pos = 0;
1619 }
1620}
1621
bb70624e
JA
1622/* Redraw the last line of a multi-line prompt that may possibly contain
1623 terminal escape sequences. Called with the cursor at column 0 of the
1624 line to draw the prompt on. */
1625static void
1626redraw_prompt (t)
1627 char *t;
1628{
1629 char *oldp, *oldl, *oldlprefix;
28ef6c31 1630 int oldlen, oldlast, oldplen, oldninvis;
bb70624e
JA
1631
1632 /* Geez, I should make this a struct. */
1633 oldp = rl_display_prompt;
1634 oldl = local_prompt;
1635 oldlprefix = local_prompt_prefix;
28ef6c31
JA
1636 oldlen = prompt_visible_length;
1637 oldplen = prompt_prefix_length;
1638 oldlast = prompt_last_invisible;
1639 oldninvis = prompt_invis_chars_first_line;
bb70624e
JA
1640
1641 rl_display_prompt = t;
28ef6c31
JA
1642 local_prompt = expand_prompt (t, &prompt_visible_length,
1643 &prompt_last_invisible,
1644 &prompt_invis_chars_first_line);
bb70624e
JA
1645 local_prompt_prefix = (char *)NULL;
1646 rl_forced_update_display ();
1647
1648 rl_display_prompt = oldp;
1649 local_prompt = oldl;
1650 local_prompt_prefix = oldlprefix;
28ef6c31
JA
1651 prompt_visible_length = oldlen;
1652 prompt_prefix_length = oldplen;
1653 prompt_last_invisible = oldlast;
1654 prompt_invis_chars_first_line = oldninvis;
bb70624e
JA
1655}
1656
726f6388
JA
1657/* Redisplay the current line after a SIGWINCH is received. */
1658void
1659_rl_redisplay_after_sigwinch ()
1660{
bb70624e 1661 char *t;
726f6388
JA
1662
1663 /* Clear the current line and put the cursor at column 0. Make sure
1664 the right thing happens if we have wrapped to a new screen line. */
28ef6c31 1665 if (_rl_term_cr)
726f6388 1666 {
bb70624e
JA
1667#if defined (__MSDOS__)
1668 putc ('\r', rl_outstream);
1669#else
28ef6c31 1670 tputs (_rl_term_cr, 1, _rl_output_character_function);
bb70624e 1671#endif
726f6388 1672 _rl_last_c_pos = 0;
bb70624e 1673#if defined (__MSDOS__)
28ef6c31 1674 space_to_eol (_rl_screenwidth);
bb70624e
JA
1675 putc ('\r', rl_outstream);
1676#else
28ef6c31
JA
1677 if (_rl_term_clreol)
1678 tputs (_rl_term_clreol, 1, _rl_output_character_function);
726f6388
JA
1679 else
1680 {
28ef6c31
JA
1681 space_to_eol (_rl_screenwidth);
1682 tputs (_rl_term_cr, 1, _rl_output_character_function);
726f6388 1683 }
bb70624e 1684#endif
726f6388
JA
1685 if (_rl_last_v_pos > 0)
1686 _rl_move_vert (0);
1687 }
1688 else
28ef6c31 1689 rl_crlf ();
726f6388
JA
1690
1691 /* Redraw only the last line of a multi-line prompt. */
1692 t = strrchr (rl_display_prompt, '\n');
1693 if (t)
bb70624e 1694 redraw_prompt (++t);
726f6388
JA
1695 else
1696 rl_forced_update_display ();
1697}
ccc6cda3
JA
1698
1699void
1700_rl_clean_up_for_exit ()
1701{
1702 if (readline_echoing_p)
1703 {
1704 _rl_move_vert (_rl_vis_botlin);
1705 _rl_vis_botlin = 0;
1706 fflush (rl_outstream);
b72432fd 1707 rl_restart_output (1, 0);
ccc6cda3
JA
1708 }
1709}
b72432fd
JA
1710
1711void
1712_rl_erase_entire_line ()
1713{
1714 cr ();
1715 _rl_clear_to_eol (0);
1716 cr ();
1717 fflush (rl_outstream);
1718}
bb70624e
JA
1719
1720/* return the `current display line' of the cursor -- the number of lines to
1721 move up to get to the first screen line of the current readline line. */
1722int
1723_rl_current_display_line ()
1724{
1725 int ret, nleft;
1726
1727 /* Find out whether or not there might be invisible characters in the
1728 editing buffer. */
1729 if (rl_display_prompt == rl_prompt)
28ef6c31 1730 nleft = _rl_last_c_pos - _rl_screenwidth - rl_visible_prompt_length;
bb70624e 1731 else
28ef6c31 1732 nleft = _rl_last_c_pos - _rl_screenwidth;
bb70624e
JA
1733
1734 if (nleft > 0)
28ef6c31 1735 ret = 1 + nleft / _rl_screenwidth;
bb70624e
JA
1736 else
1737 ret = 0;
1738
1739 return ret;
1740}