]> git.ipfire.org Git - thirdparty/bash.git/blob - lib/readline/display.c
Imported from ../bash-2.05.tar.gz.
[thirdparty/bash.git] / lib / readline / display.c
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
10 as published by the Free Software Foundation; either version 2, or
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,
21 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
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
44 /* System-specific feature definitions and include files. */
45 #include "rldefs.h"
46
47 /* Termcap library stuff. */
48 #include "tcap.h"
49
50 /* Some standard library routines. */
51 #include "readline.h"
52 #include "history.h"
53
54 #include "rlprivate.h"
55 #include "xmalloc.h"
56
57 #if !defined (strchr) && !defined (__STDC__)
58 extern char *strchr (), *strrchr ();
59 #endif /* !strchr && !__STDC__ */
60
61 #if defined (HACK_TERMCAP_MOTION)
62 extern char *_rl_term_forward_char;
63 #endif
64
65 static void update_line __P((char *, char *, int, int, int, int));
66 static void space_to_eol __P((int));
67 static void delete_chars __P((int));
68 static void insert_some_chars __P((char *, int));
69 static void cr __P((void));
70
71 static int *inv_lbreaks, *vis_lbreaks;
72 static int inv_lbsize, vis_lbsize;
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
105 /* Application-specific redisplay function. */
106 rl_voidfunc_t *rl_redisplay_function = rl_redisplay;
107
108 /* Global variables declared here. */
109 /* What YOU turn on when you have handled all redisplay yourself. */
110 int rl_display_fixed = 0;
111
112 int _rl_suppress_redisplay = 0;
113
114 /* The stuff that gets printed out before the actual text of the line.
115 This is usually pointing to rl_prompt. */
116 char *rl_display_prompt = (char *)NULL;
117
118 /* Pseudo-global variables declared here. */
119 /* The visible cursor position. If you print some text, adjust this. */
120 int _rl_last_c_pos = 0;
121 int _rl_last_v_pos = 0;
122
123 /* Number of lines currently on screen minus 1. */
124 int _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. */
129 static int last_lmargin;
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. */
133 static char *visible_line = (char *)NULL;
134 static char *invisible_line = (char *)NULL;
135
136 /* A buffer for `modeline' messages. */
137 static char msg_buf[128];
138
139 /* Non-zero forces the redisplay even if we thought it was unnecessary. */
140 static int forced_display;
141
142 /* Default and initial buffer size. Can grow. */
143 static int line_size = 1024;
144
145 /* Variables to keep track of the expanded prompt string, which may
146 include invisible characters. */
147
148 static char *local_prompt, *local_prompt_prefix;
149 static int prompt_visible_length, prompt_prefix_length;
150
151 /* The number of invisible characters in the line currently being
152 displayed on the screen. */
153 static int visible_wrap_offset;
154
155 /* The number of invisible characters in the prompt string. Static so it
156 can be shared between rl_redisplay and update_line */
157 static int wrap_offset;
158
159 /* The index of the last invisible character in the prompt string. */
160 static int prompt_last_invisible;
161
162 /* The length (buffer offset) of the first line of the last (possibly
163 multi-line) buffer displayed on the screen. */
164 static int visible_first_line_len;
165
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. */
169 static int prompt_invis_chars_first_line;
170
171 static int prompt_last_screen_line;
172
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
175 a placeholder for expansion. LIP, if non-null is a place to store the
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. */
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
187 static char *
188 expand_prompt (pmt, lp, lip, niflp)
189 char *pmt;
190 int *lp, *lip, *niflp;
191 {
192 char *r, *ret, *p;
193 int l, rl, last, ignoring, ninvis, invfl;
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
204 l = strlen (pmt);
205 r = ret = xmalloc (l + 1);
206
207 invfl = 0; /* invisible chars in first line of prompt */
208
209 for (rl = ignoring = last = ninvis = 0, p = pmt; p && *p; p++)
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;
221 last = r - ret - 1;
222 continue;
223 }
224 else
225 {
226 *r++ = *p;
227 if (!ignoring)
228 rl++;
229 else
230 ninvis++;
231 if (rl == _rl_screenwidth)
232 invfl = ninvis;
233 }
234 }
235
236 if (rl < _rl_screenwidth)
237 invfl = ninvis;
238
239 *r = '\0';
240 if (lp)
241 *lp = rl;
242 if (lip)
243 *lip = last;
244 if (niflp)
245 *niflp = invfl;
246 return ret;
247 }
248
249 /* Just strip out RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE from
250 PMT and return the rest of PMT. */
251 char *
252 _rl_strip_prompt (pmt)
253 char *pmt;
254 {
255 char *ret;
256
257 ret = expand_prompt (pmt, (int *)NULL, (int *)NULL, (int *)NULL);
258 return ret;
259 }
260
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
269 * prompt_visible_length = number of visible characters in local_prompt
270 * prompt_prefix_length = number of visible characters in local_prompt_prefix
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 */
278 int
279 rl_expand_prompt (prompt)
280 char *prompt;
281 {
282 char *p, *t;
283 int c;
284
285 /* Clear out any saved values. */
286 FREE (local_prompt);
287 FREE (local_prompt_prefix);
288
289 local_prompt = local_prompt_prefix = (char *)0;
290 prompt_last_invisible = prompt_visible_length = 0;
291
292 if (prompt == 0 || *prompt == 0)
293 return (0);
294
295 p = strrchr (prompt, '\n');
296 if (!p)
297 {
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);
302 local_prompt_prefix = (char *)0;
303 return (prompt_visible_length);
304 }
305 else
306 {
307 /* The prompt spans multiple lines. */
308 t = ++p;
309 local_prompt = expand_prompt (p, &prompt_visible_length,
310 &prompt_last_invisible,
311 &prompt_invis_chars_first_line);
312 c = *t; *t = '\0';
313 /* The portion of the prompt string up to and including the
314 final newline is now null-terminated. */
315 local_prompt_prefix = expand_prompt (prompt, &prompt_prefix_length,
316 (int *)NULL,
317 &prompt_invis_chars_first_line);
318 *t = c;
319 return (prompt_prefix_length);
320 }
321 }
322
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. */
328 static void
329 init_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
366 /* Basic redisplay algorithm. */
367 void
368 rl_redisplay ()
369 {
370 register int in, out, c, linenum, cursor_linenum;
371 register char *line;
372 int c_pos, inv_botlin, lb_botlin, lb_linenum;
373 int newlines, lpos, temp;
374 char *prompt_this_line;
375
376 if (!readline_echoing_p)
377 return;
378
379 if (!rl_display_prompt)
380 rl_display_prompt = "";
381
382 if (invisible_line == 0)
383 {
384 init_line_structures (0);
385 rl_on_new_line ();
386 }
387
388 /* Draw the line into the buffer. */
389 c_pos = -1;
390
391 line = invisible_line;
392 out = inv_botlin = 0;
393
394 /* Mark the line as modified or not. We only do this for history
395 lines. */
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. */
412 if (rl_display_prompt == rl_prompt || local_prompt)
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)
419 {
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 }
427 strncpy (line + out, local_prompt, local_len);
428 out += local_len;
429 }
430 line[out] = '\0';
431 wrap_offset = local_len - prompt_visible_length;
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++;
442 pmtlen = prompt_this_line - rl_display_prompt; /* temp var */
443 if (forced_display)
444 {
445 _rl_output_some_chars (rl_display_prompt, pmtlen);
446 /* Make sure we are at column zero even after a newline,
447 regardless of the state of terminal output processing. */
448 if (pmtlen < 2 || prompt_this_line[-2] != '\r')
449 cr ();
450 }
451 }
452
453 pmtlen = strlen (prompt_this_line);
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 }
461 strncpy (line + out, prompt_this_line, pmtlen);
462 out += pmtlen;
463 line[out] = '\0';
464 wrap_offset = prompt_invis_chars_first_line = 0;
465 }
466
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
476 #define CHECK_LPOS() \
477 do { \
478 lpos++; \
479 if (lpos >= _rl_screenwidth) \
480 { \
481 if (newlines >= (inv_lbsize - 2)) \
482 { \
483 inv_lbsize *= 2; \
484 inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
485 } \
486 inv_lbreaks[++newlines] = out; \
487 lpos = 0; \
488 } \
489 } while (0)
490
491 /* inv_lbreaks[i] is where line i starts in the buffer. */
492 inv_lbreaks[newlines = 0] = 0;
493 lpos = out - wrap_offset;
494
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
501 contents of the command line? */
502 while (lpos >= _rl_screenwidth)
503 {
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);
513
514 inv_lbreaks[++newlines] = temp;
515 lpos -= _rl_screenwidth;
516 }
517
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. */
525 lb_linenum = 0;
526 for (in = 0; in < rl_end; in++)
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)
539 {
540 c_pos = out;
541 lb_linenum = newlines;
542 }
543
544 if (META_CHAR (c))
545 {
546 if (_rl_output_meta_chars == 0)
547 {
548 sprintf (line + out, "\\%o", c);
549
550 if (lpos + 4 >= _rl_screenwidth)
551 {
552 temp = _rl_screenwidth - lpos;
553 CHECK_INV_LBREAKS ();
554 inv_lbreaks[++newlines] = out + temp;
555 lpos = 4 - temp;
556 }
557 else
558 lpos += 4;
559
560 out += 4;
561 }
562 else
563 {
564 line[out++] = c;
565 CHECK_LPOS();
566 }
567 }
568 #if defined (DISPLAY_TABS)
569 else if (c == '\t')
570 {
571 register int newout;
572
573 #if 0
574 newout = (out | (int)7) + 1;
575 #else
576 newout = out + 8 - lpos % 8;
577 #endif
578 temp = newout - out;
579 if (lpos + temp >= _rl_screenwidth)
580 {
581 register int temp2;
582 temp2 = _rl_screenwidth - lpos;
583 CHECK_INV_LBREAKS ();
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 }
595 }
596 #endif
597 else if (c == '\n' && _rl_horizontal_scroll_mode == 0 && _rl_term_up && *_rl_term_up)
598 {
599 line[out++] = '\0'; /* XXX - sentinel */
600 CHECK_INV_LBREAKS ();
601 inv_lbreaks[++newlines] = out;
602 lpos = 0;
603 }
604 else if (CTRL_CHAR (c) || c == RUBOUT)
605 {
606 line[out++] = '^';
607 CHECK_LPOS();
608 line[out++] = CTRL_CHAR (c) ? UNCTRL (c) : '?';
609 CHECK_LPOS();
610 }
611 else
612 {
613 line[out++] = c;
614 CHECK_LPOS();
615 }
616 }
617 line[out] = '\0';
618 if (c_pos < 0)
619 {
620 c_pos = out;
621 lb_linenum = newlines;
622 }
623
624 inv_botlin = lb_botlin = newlines;
625 CHECK_INV_LBREAKS ();
626 inv_lbreaks[newlines+1] = out;
627 cursor_linenum = lb_linenum;
628
629 /* C_POS == position in buffer where cursor should be placed.
630 CURSOR_LINENUM == line number where the cursor should be placed. */
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
641 if (_rl_horizontal_scroll_mode == 0 && _rl_term_up && *_rl_term_up)
642 {
643 int nleft, pos, changed_screen_line;
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,
651 not the first. */
652 if (out >= _rl_screenchars)
653 out = _rl_screenchars - 1;
654
655 /* The first line is at character position 0 in the buffer. The
656 second and subsequent lines start at inv_lbreaks[N], offset by
657 OFFSET (which has already been calculated above). */
658
659 #define W_OFFSET(line, offset) ((line) == 0 ? offset : 0)
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])
663 #define VIS_LINE(line) ((line) > _rl_vis_botlin) ? "" : VIS_CHARS(line)
664 #define INV_LINE(line) (invisible_line + inv_lbreaks[line])
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,
670 VIS_LLEN(linenum), INV_LLEN(linenum), inv_botlin);
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)
676 and the new line is shorter than the old. Make sure we are
677 at the end of the new line before clearing. */
678 if (linenum == 0 &&
679 inv_botlin == 0 && _rl_last_c_pos == out &&
680 (wrap_offset > visible_wrap_offset) &&
681 (_rl_last_c_pos < visible_first_line_len))
682 {
683 nleft = _rl_screenwidth + wrap_offset - _rl_last_c_pos;
684 if (nleft)
685 _rl_clear_to_eol (nleft);
686 }
687
688 /* Since the new first line is now visible, save its length. */
689 if (linenum == 0)
690 visible_first_line_len = (inv_botlin > 0) ? inv_lbreaks[1] : out - wrap_offset;
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);
703 _rl_clear_to_eol
704 ((linenum == _rl_vis_botlin) ? strlen (tt) : _rl_screenwidth);
705 }
706 }
707 _rl_vis_botlin = inv_botlin;
708
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);
715 /* If we moved up to the line with the prompt using _rl_term_up,
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. */
719 if (cursor_linenum == 0 && wrap_offset)
720 _rl_last_c_pos += wrap_offset;
721 }
722
723 /* We have to reprint the prompt if it contains invisible
724 characters, since it's not generally OK to just reprint
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. */
728 nleft = prompt_visible_length + wrap_offset;
729 if (cursor_linenum == 0 && wrap_offset > 0 && _rl_last_c_pos > 0 &&
730 _rl_last_c_pos <= prompt_last_invisible && local_prompt)
731 {
732 #if defined (__MSDOS__)
733 putc ('\r', rl_outstream);
734 #else
735 if (_rl_term_cr)
736 tputs (_rl_term_cr, 1, _rl_output_character_function);
737 #endif
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? */
744 pos = inv_lbreaks[cursor_linenum];
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
749 /* Since _rl_backspace() doesn't know about invisible characters in the
750 prompt, and there's no good way to tell it, we compensate for
751 those characters here and call _rl_backspace() directly. */
752 if (wrap_offset && cursor_linenum == 0 && nleft < _rl_last_c_pos)
753 {
754 _rl_backspace (_rl_last_c_pos - nleft);
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;
775 nleft = prompt_visible_length + wrap_offset;
776 /* Where the new cursor position will be on the screen. This can be
777 longer than SCREENWIDTH; if it is, lmargin will be adjusted. */
778 phys_c_pos = c_pos - (last_lmargin ? last_lmargin : wrap_offset);
779 t = _rl_screenwidth / 3;
780
781 /* If the number of characters had already exceeded the screenwidth,
782 last_lmargin will be > 0. */
783
784 /* If the number of characters to be displayed is more than the screen
785 width, compute the starting offset so that the cursor is about
786 two-thirds of the way across the screen. */
787 if (phys_c_pos > _rl_screenwidth - 2)
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 }
797 else if (ndisp < _rl_screenwidth - 2) /* XXX - was -1 */
798 lmargin = 0;
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
808 lmargin = last_lmargin;
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
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. */
819 t = lmargin + M_OFFSET (lmargin, wrap_offset) + _rl_screenwidth;
820 if (t < out)
821 line[t - 1] = '>';
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,
829 _rl_screenwidth + visible_wrap_offset,
830 _rl_screenwidth + (lmargin ? 0 : wrap_offset),
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 {
841 nleft = _rl_screenwidth - t;
842 _rl_clear_to_eol (nleft);
843 }
844 visible_first_line_len = out - lmargin - M_OFFSET (lmargin, wrap_offset);
845 if (visible_first_line_len > _rl_screenwidth)
846 visible_first_line_len = _rl_screenwidth;
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 {
856 char *vtemp = visible_line;
857 int *itemp = vis_lbreaks, ntemp = vis_lbsize;
858
859 visible_line = invisible_line;
860 invisible_line = vtemp;
861
862 vis_lbreaks = inv_lbreaks;
863 inv_lbreaks = itemp;
864
865 vis_lbsize = inv_lbsize;
866 inv_lbsize = ntemp;
867
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
885 old: eddie> Oh, my little gruntle-buggy is to me, as lurgid as
886 new: 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
892 no differences, as well as for end of line additions must be handled.
893
894 Could be made even smarter, but this works well enough */
895 static void
896 update_line (old, new, current_line, omax, nmax, inv_botlin)
897 register char *old, *new;
898 int current_line, omax, nmax, inv_botlin;
899 {
900 register char *ofd, *ols, *oe, *nfd, *nls, *ne;
901 int temp, lendiff, wsatend, od, nd;
902 int current_invis_chars;
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);
910 if (temp == _rl_screenwidth && _rl_term_autowrap && !_rl_horizontal_scroll_mode
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])
920 old[0] = new[0];
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
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 }
971
972 /* If this is the first line and there are invisible characters in the
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.
978
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. */
983
984 lendiff = local_prompt ? strlen (local_prompt) : 0;
985 od = ofd - old; /* index of first difference in visible line */
986 if (current_line == 0 && !_rl_horizontal_scroll_mode &&
987 _rl_term_cr && lendiff > prompt_visible_length && _rl_last_c_pos > 0 &&
988 od >= lendiff && _rl_last_c_pos <= prompt_last_invisible)
989 {
990 #if defined (__MSDOS__)
991 putc ('\r', rl_outstream);
992 #else
993 tputs (_rl_term_cr, 1, _rl_output_character_function);
994 #endif
995 _rl_output_some_chars (local_prompt, lendiff);
996 _rl_last_c_pos = lendiff;
997 }
998
999 _rl_move_cursor_relative (od, old);
1000
1001 /* if (len (new) > len (old)) */
1002 lendiff = (nls - nfd) - (ols - ofd);
1003
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)
1009 lendiff += visible_wrap_offset - current_invis_chars;
1010
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. */
1021 if (_rl_terminal_can_insert && ((2 * temp) >= lendiff || _rl_term_IC) && (!_rl_term_autowrap || !gl))
1022 {
1023 /* If lendiff > prompt_visible_length and _rl_last_c_pos == 0 and
1024 _rl_horizontal_scroll_mode == 1, inserting the characters with
1025 _rl_term_IC or _rl_term_ic will screw up the screen because of the
1026 invisible characters. We need to just draw them. */
1027 if (*ols && (!_rl_horizontal_scroll_mode || _rl_last_c_pos > 0 ||
1028 lendiff <= prompt_visible_length || !current_invis_chars))
1029 {
1030 insert_some_chars (nfd, lendiff);
1031 _rl_last_c_pos += lendiff;
1032 }
1033 else if (*ols == 0)
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. */
1037 /* However, this screws up the rest of this block, which
1038 assumes you've done the insert because you can. */
1039 _rl_output_some_chars (nfd, lendiff);
1040 _rl_last_c_pos += lendiff;
1041 }
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 }
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. */
1069 if (_rl_term_dc && (2 * temp) >= -lendiff)
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);
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 }
1105 }
1106 }
1107 }
1108
1109 /* Tell the update routines that we have moved onto a new (empty) line. */
1110 int
1111 rl_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;
1118 if (vis_lbreaks)
1119 vis_lbreaks[0] = vis_lbreaks[1] = 0;
1120 visible_wrap_offset = 0;
1121 return 0;
1122 }
1123
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. */
1127 int
1128 rl_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. */
1154 real_screenwidth = _rl_screenwidth + (_rl_term_autowrap ? 0 : 1);
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
1176 /* Actually update the display, period. */
1177 int
1178 rl_forced_update_display ()
1179 {
1180 if (visible_line)
1181 {
1182 register char *temp = visible_line;
1183
1184 while (*temp)
1185 *temp++ = '\0';
1186 }
1187 rl_on_new_line ();
1188 forced_display++;
1189 (*rl_redisplay_function) ();
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. */
1196 void
1197 _rl_move_cursor_relative (new, data)
1198 int new;
1199 const char *data;
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);
1210 if (new == 0 || CR_FASTER (new, _rl_last_c_pos) ||
1211 (_rl_term_autowrap && i == _rl_screenwidth))
1212 {
1213 #if defined (__MSDOS__)
1214 putc ('\r', rl_outstream);
1215 #else
1216 tputs (_rl_term_cr, 1, _rl_output_character_function);
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)
1233 if (_rl_term_forward_char)
1234 for (i = _rl_last_c_pos; i < new; i++)
1235 tputs (_rl_term_forward_char, 1, _rl_output_character_function);
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 }
1244 else if (_rl_last_c_pos > new)
1245 _rl_backspace (_rl_last_c_pos - new);
1246 _rl_last_c_pos = new;
1247 }
1248
1249 /* PWP: move the cursor up or down. */
1250 void
1251 _rl_move_vert (to)
1252 int to;
1253 {
1254 register int delta, i;
1255
1256 if (_rl_last_v_pos == to || to > _rl_screenheight)
1257 return;
1258
1259 if ((delta = to - _rl_last_v_pos) > 0)
1260 {
1261 for (i = 0; i < delta; i++)
1262 putc ('\n', rl_outstream);
1263 #if defined (__MSDOS__)
1264 putc ('\r', rl_outstream);
1265 #else
1266 tputs (_rl_term_cr, 1, _rl_output_character_function);
1267 #endif
1268 _rl_last_c_pos = 0;
1269 }
1270 else
1271 { /* delta < 0 */
1272 if (_rl_term_up && *_rl_term_up)
1273 for (i = 0; i < -delta; i++)
1274 tputs (_rl_term_up, 1, _rl_output_character_function);
1275 }
1276
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. */
1282 int
1283 rl_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)
1295 if ((CTRL_CHAR (c) && c != '\t') || c == RUBOUT)
1296 #else
1297 if (CTRL_CHAR (c) || c == RUBOUT)
1298 #endif /* !DISPLAY_TABS */
1299 {
1300 fprintf (rl_outstream, "C-");
1301 n += 2;
1302 c = CTRL_CHAR (c) ? UNCTRL (c) : '?';
1303 }
1304
1305 putc (c, rl_outstream);
1306 fflush (rl_outstream);
1307 return n;
1308 }
1309
1310 int
1311 rl_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
1330 if (CTRL_CHAR (c) || c == RUBOUT)
1331 return (2);
1332
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
1339 #if defined (USE_VARARGS)
1340 int
1341 #if defined (PREFER_STDARG)
1342 rl_message (const char *format, ...)
1343 #else
1344 rl_message (va_alist)
1345 va_dcl
1346 #endif
1347 {
1348 va_list args;
1349 #if defined (PREFER_VARARGS)
1350 char *format;
1351 #endif
1352
1353 #if defined (PREFER_STDARG)
1354 va_start (args, format);
1355 #else
1356 va_start (args);
1357 format = va_arg (args, char *);
1358 #endif
1359
1360 vsprintf (msg_buf, format, args);
1361 va_end (args);
1362
1363 rl_display_prompt = msg_buf;
1364 (*rl_redisplay_function) ();
1365 return 0;
1366 }
1367 #else /* !USE_VARARGS */
1368 int
1369 rl_message (format, arg1, arg2)
1370 char *format;
1371 {
1372 sprintf (msg_buf, format, arg1, arg2);
1373 rl_display_prompt = msg_buf;
1374 (*rl_redisplay_function) ();
1375 return 0;
1376 }
1377 #endif /* !USE_VARARGS */
1378
1379 /* How to clear things from the "echo-area". */
1380 int
1381 rl_clear_message ()
1382 {
1383 rl_display_prompt = rl_prompt;
1384 (*rl_redisplay_function) ();
1385 return 0;
1386 }
1387
1388 int
1389 rl_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
1398 static char *saved_local_prompt;
1399 static char *saved_local_prefix;
1400 static int saved_last_invisible;
1401 static int saved_visible_length;
1402
1403 void
1404 rl_save_prompt ()
1405 {
1406 saved_local_prompt = local_prompt;
1407 saved_local_prefix = local_prompt_prefix;
1408 saved_last_invisible = prompt_last_invisible;
1409 saved_visible_length = prompt_visible_length;
1410
1411 local_prompt = local_prompt_prefix = (char *)0;
1412 prompt_last_invisible = prompt_visible_length = 0;
1413 }
1414
1415 void
1416 rl_restore_prompt ()
1417 {
1418 FREE (local_prompt);
1419 FREE (local_prompt_prefix);
1420
1421 local_prompt = saved_local_prompt;
1422 local_prompt_prefix = saved_local_prefix;
1423 prompt_last_invisible = saved_last_invisible;
1424 prompt_visible_length = saved_visible_length;
1425 }
1426
1427 char *
1428 _rl_make_prompt_for_search (pchar)
1429 int pchar;
1430 {
1431 int len;
1432 char *pmt;
1433
1434 rl_save_prompt ();
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)
1441 strcpy (pmt, rl_prompt);
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)
1450 strcpy (pmt, saved_local_prompt);
1451 pmt[len] = pchar;
1452 pmt[len+1] = '\0';
1453 local_prompt = savestring (pmt);
1454 prompt_last_invisible = saved_last_invisible;
1455 prompt_visible_length = saved_visible_length + 1;
1456 }
1457 return pmt;
1458 }
1459
1460 /* Quick redisplay hack when erasing characters at the end of the line. */
1461 void
1462 _rl_erase_at_end_of_line (l)
1463 int l;
1464 {
1465 register int i;
1466
1467 _rl_backspace (l);
1468 for (i = 0; i < l; i++)
1469 putc (' ', rl_outstream);
1470 _rl_backspace (l);
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, */
1478 void
1479 _rl_clear_to_eol (count)
1480 int count;
1481 {
1482 if (_rl_term_clreol)
1483 tputs (_rl_term_clreol, 1, _rl_output_character_function);
1484 else if (count)
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, */
1490 static void
1491 space_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
1502 void
1503 _rl_clear_screen ()
1504 {
1505 if (_rl_term_clrpag)
1506 tputs (_rl_term_clrpag, 1, _rl_output_character_function);
1507 else
1508 rl_crlf ();
1509 }
1510
1511 /* Insert COUNT characters from STRING to the output stream. */
1512 static void
1513 insert_some_chars (string, count)
1514 char *string;
1515 int count;
1516 {
1517 /* If IC is defined, then we do not have to "enter" insert mode. */
1518 if (_rl_term_IC)
1519 {
1520 char *buffer;
1521 buffer = tgoto (_rl_term_IC, 0, count);
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. */
1530 if (_rl_term_im && *_rl_term_im)
1531 tputs (_rl_term_im, 1, _rl_output_character_function);
1532
1533 /* If there is a special command for inserting characters, then
1534 use that first to open up the space. */
1535 if (_rl_term_ic && *_rl_term_ic)
1536 {
1537 for (i = count; i--; )
1538 tputs (_rl_term_ic, 1, _rl_output_character_function);
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. */
1546 if (_rl_term_ei && *_rl_term_ei)
1547 tputs (_rl_term_ei, 1, _rl_output_character_function);
1548 }
1549 }
1550
1551 /* Delete COUNT characters from the display line. */
1552 static void
1553 delete_chars (count)
1554 int count;
1555 {
1556 if (count > _rl_screenwidth) /* XXX */
1557 return;
1558
1559 if (_rl_term_DC && *_rl_term_DC)
1560 {
1561 char *buffer;
1562 buffer = tgoto (_rl_term_DC, count, count);
1563 tputs (buffer, count, _rl_output_character_function);
1564 }
1565 else
1566 {
1567 if (_rl_term_dc && *_rl_term_dc)
1568 while (count--)
1569 tputs (_rl_term_dc, 1, _rl_output_character_function);
1570 }
1571 }
1572
1573 void
1574 _rl_update_final ()
1575 {
1576 int full_lines;
1577
1578 full_lines = 0;
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 &&
1582 visible_line[vis_lbreaks[_rl_vis_botlin]] == 0)
1583 {
1584 _rl_vis_botlin--;
1585 full_lines = 1;
1586 }
1587 _rl_move_vert (_rl_vis_botlin);
1588 /* If we've wrapped lines, remove the final xterm line-wrap flag. */
1589 if (full_lines && _rl_term_autowrap && (VIS_LLEN(_rl_vis_botlin) == _rl_screenwidth))
1590 {
1591 char *last_line;
1592 #if 0
1593 last_line = &visible_line[inv_lbreaks[_rl_vis_botlin]];
1594 #else
1595 last_line = &visible_line[vis_lbreaks[_rl_vis_botlin]];
1596 #endif
1597 _rl_move_cursor_relative (_rl_screenwidth - 1, last_line);
1598 _rl_clear_to_eol (0);
1599 putc (last_line[_rl_screenwidth - 1], rl_outstream);
1600 }
1601 _rl_vis_botlin = 0;
1602 rl_crlf ();
1603 fflush (rl_outstream);
1604 rl_display_fixed++;
1605 }
1606
1607 /* Move to the start of the current line. */
1608 static void
1609 cr ()
1610 {
1611 if (_rl_term_cr)
1612 {
1613 #if defined (__MSDOS__)
1614 putc ('\r', rl_outstream);
1615 #else
1616 tputs (_rl_term_cr, 1, _rl_output_character_function);
1617 #endif
1618 _rl_last_c_pos = 0;
1619 }
1620 }
1621
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. */
1625 static void
1626 redraw_prompt (t)
1627 char *t;
1628 {
1629 char *oldp, *oldl, *oldlprefix;
1630 int oldlen, oldlast, oldplen, oldninvis;
1631
1632 /* Geez, I should make this a struct. */
1633 oldp = rl_display_prompt;
1634 oldl = local_prompt;
1635 oldlprefix = local_prompt_prefix;
1636 oldlen = prompt_visible_length;
1637 oldplen = prompt_prefix_length;
1638 oldlast = prompt_last_invisible;
1639 oldninvis = prompt_invis_chars_first_line;
1640
1641 rl_display_prompt = t;
1642 local_prompt = expand_prompt (t, &prompt_visible_length,
1643 &prompt_last_invisible,
1644 &prompt_invis_chars_first_line);
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;
1651 prompt_visible_length = oldlen;
1652 prompt_prefix_length = oldplen;
1653 prompt_last_invisible = oldlast;
1654 prompt_invis_chars_first_line = oldninvis;
1655 }
1656
1657 /* Redisplay the current line after a SIGWINCH is received. */
1658 void
1659 _rl_redisplay_after_sigwinch ()
1660 {
1661 char *t;
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. */
1665 if (_rl_term_cr)
1666 {
1667 #if defined (__MSDOS__)
1668 putc ('\r', rl_outstream);
1669 #else
1670 tputs (_rl_term_cr, 1, _rl_output_character_function);
1671 #endif
1672 _rl_last_c_pos = 0;
1673 #if defined (__MSDOS__)
1674 space_to_eol (_rl_screenwidth);
1675 putc ('\r', rl_outstream);
1676 #else
1677 if (_rl_term_clreol)
1678 tputs (_rl_term_clreol, 1, _rl_output_character_function);
1679 else
1680 {
1681 space_to_eol (_rl_screenwidth);
1682 tputs (_rl_term_cr, 1, _rl_output_character_function);
1683 }
1684 #endif
1685 if (_rl_last_v_pos > 0)
1686 _rl_move_vert (0);
1687 }
1688 else
1689 rl_crlf ();
1690
1691 /* Redraw only the last line of a multi-line prompt. */
1692 t = strrchr (rl_display_prompt, '\n');
1693 if (t)
1694 redraw_prompt (++t);
1695 else
1696 rl_forced_update_display ();
1697 }
1698
1699 void
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);
1707 rl_restart_output (1, 0);
1708 }
1709 }
1710
1711 void
1712 _rl_erase_entire_line ()
1713 {
1714 cr ();
1715 _rl_clear_to_eol (0);
1716 cr ();
1717 fflush (rl_outstream);
1718 }
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. */
1722 int
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)
1730 nleft = _rl_last_c_pos - _rl_screenwidth - rl_visible_prompt_length;
1731 else
1732 nleft = _rl_last_c_pos - _rl_screenwidth;
1733
1734 if (nleft > 0)
1735 ret = 1 + nleft / _rl_screenwidth;
1736 else
1737 ret = 0;
1738
1739 return ret;
1740 }