daf736ca1cd98af262c4936563f7c6db2561cae0
[platform/upstream/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 1, 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    675 Mass Ave, Cambridge, MA 02139, USA. */
22 #define READLINE_LIBRARY
23
24 #if defined (HAVE_CONFIG_H)
25 #  include "config.h"
26 #endif
27
28 #include <stdio.h>
29 #include <sys/types.h>
30
31 #if defined (HAVE_UNISTD_H)
32 #  include <unistd.h>
33 #endif /* HAVE_UNISTD_H */
34
35 #if defined (HAVE_STDLIB_H)
36 #  include <stdlib.h>
37 #else
38 #  include "ansi_stdlib.h"
39 #endif /* HAVE_STDLIB_H */
40
41 #include "posixstat.h"
42
43 /* System-specific feature definitions and include files. */
44 #include "rldefs.h"
45
46 /* Some standard library routines. */
47 #include "readline.h"
48 #include "history.h"
49
50 #if !defined (strchr) && !defined (__STDC__)
51 extern char *strchr (), *strrchr ();
52 #endif /* !strchr && !__STDC__ */
53
54 /* Global and pseudo-global variables and functions
55    imported from readline.c. */
56 extern char *rl_prompt;
57 extern int readline_echoing_p;
58 extern char *term_clreol, *term_im, *term_ic,  *term_ei, *term_DC;
59 /* Termcap variables. */
60 extern char *term_up, *term_dc, *term_cr, *term_IC;
61 extern int screenheight, screenwidth, screenchars;
62 extern int terminal_can_insert, term_xn;
63
64 extern void _rl_output_some_chars ();
65 extern int _rl_output_character_function ();
66
67 extern int _rl_output_meta_chars;
68 extern int _rl_horizontal_scroll_mode;
69 extern int _rl_mark_modified_lines;
70 extern int _rl_prefer_visible_bell;
71
72 /* Pseudo-global functions (local to the readline library) exported
73    by this file. */
74 void _rl_move_cursor_relative (), _rl_output_some_chars ();
75 void _rl_move_vert ();
76
77 static void update_line (), clear_to_eol (), space_to_eol ();
78 static void delete_chars (), insert_some_chars ();
79
80 extern char *xmalloc (), *xrealloc ();
81
82 /* Heuristic used to decide whether it is faster to move from CUR to NEW
83    by backing up or outputting a carriage return and moving forward. */
84 #define CR_FASTER(new, cur) (((new) + 1) < ((cur) - (new)))
85
86 /* **************************************************************** */
87 /*                                                                  */
88 /*                      Display stuff                               */
89 /*                                                                  */
90 /* **************************************************************** */
91
92 /* This is the stuff that is hard for me.  I never seem to write good
93    display routines in C.  Let's see how I do this time. */
94
95 /* (PWP) Well... Good for a simple line updater, but totally ignores
96    the problems of input lines longer than the screen width.
97
98    update_line and the code that calls it makes a multiple line,
99    automatically wrapping line update.  Careful attention needs
100    to be paid to the vertical position variables. */
101
102 /* Keep two buffers; one which reflects the current contents of the
103    screen, and the other to draw what we think the new contents should
104    be.  Then compare the buffers, and make whatever changes to the
105    screen itself that we should.  Finally, make the buffer that we
106    just drew into be the one which reflects the current contents of the
107    screen, and place the cursor where it belongs.
108
109    Commands that want to can fix the display themselves, and then let
110    this function know that the display has been fixed by setting the
111    RL_DISPLAY_FIXED variable.  This is good for efficiency. */
112
113 /* Global variables declared here. */
114 /* What YOU turn on when you have handled all redisplay yourself. */
115 int rl_display_fixed = 0;
116
117 /* The stuff that gets printed out before the actual text of the line.
118    This is usually pointing to rl_prompt. */
119 char *rl_display_prompt = (char *)NULL;
120
121 /* Pseudo-global variables declared here. */
122 /* The visible cursor position.  If you print some text, adjust this. */
123 int _rl_last_c_pos = 0;
124 int _rl_last_v_pos = 0;
125
126 /* Number of lines currently on screen minus 1. */
127 int _rl_vis_botlin = 0;
128
129 /* Variables used only in this file. */
130 /* The last left edge of text that was displayed.  This is used when
131    doing horizontal scrolling.  It shifts in thirds of a screenwidth. */
132 static int last_lmargin = 0;
133
134 /* The line display buffers.  One is the line currently displayed on
135    the screen.  The other is the line about to be displayed. */
136 static char *visible_line = (char *)NULL;
137 static char *invisible_line = (char *)NULL;
138
139 /* A buffer for `modeline' messages. */
140 static char msg_buf[128];
141
142 /* Non-zero forces the redisplay even if we thought it was unnecessary. */
143 static int forced_display = 0;
144
145 /* Default and initial buffer size.  Can grow. */
146 static int line_size = 1024;
147
148 static char *last_prompt_string = (char *)NULL;
149 static char *local_prompt, *local_prompt_prefix;
150 static int visible_length, prefix_length;
151
152 /* The number of invisible characters in the line currently being
153    displayed on the screen. */
154 static int visible_wrap_offset = 0;
155
156 /* The length (buffer offset) of the first line of the last (possibly
157    multi-line) buffer displayed on the screen. */
158 static int visible_first_line_len = 0;
159
160 /* Expand the prompt string S and return the number of visible
161    characters in *LP, if LP is not null.  This is currently more-or-less
162    a placeholder for expansion. */
163
164 /* Current implementation:
165         \001 (^A) start non-visible characters
166         \002 (^B) end non-visible characters
167    all characters except \001 and \002 (following a \001) are copied to
168    the returned string; all characters except those between \001 and
169    \002 are assumed to be `visible'. */ 
170
171 static char *
172 expand_prompt (pmt, lp)
173      char *pmt;
174      int *lp;
175 {
176   char *r, *ret, *p;
177   int l, rl, ignoring;
178
179   /* Short-circuit if we can. */
180   if (strchr (pmt, RL_PROMPT_START_IGNORE) == 0)
181     {
182       r = savestring (pmt);
183       if (lp)
184         *lp = strlen (r);
185       return r;
186     }
187
188   l = pmt ? strlen (pmt) : 0;
189   r = ret = xmalloc (l + 1);
190   
191   for (rl = ignoring = 0, p = pmt; p && *p; p++)
192     {
193       /* This code strips the invisible character string markers
194          RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE */
195       if (*p == RL_PROMPT_START_IGNORE)
196         {
197           ignoring++;
198           continue;
199         }
200       else if (ignoring && *p == RL_PROMPT_END_IGNORE)
201         {
202           ignoring = 0;
203           continue;
204         }
205       else
206         {
207           *r++ = *p;
208           if (!ignoring)
209             rl++;
210         }
211     }
212
213   *r = '\0';
214   if (lp)
215     *lp = rl;
216   return ret;
217 }
218
219 /*
220  * Expand the prompt string into the various display components, if
221  * necessary.
222  *
223  * local_prompt = expanded last line of string in rl_display_prompt
224  *                (portion after the final newline)
225  * local_prompt_prefix = portion before last newline of rl_display_prompt,
226  *                       expanded via expand_prompt
227  * visible_length = number of visible characters in local_prompt
228  * prefix_length = number of visible characters in local_prompt_prefix
229  *
230  * This function is called once per call to readline().  It may also be
231  * called arbitrarily to expand the primary prompt.
232  *
233  * The return value is the number of visible characters on the last line
234  * of the (possibly multi-line) prompt.
235  */
236 int
237 rl_expand_prompt (prompt)
238      char *prompt;
239 {
240   char *p, *t;
241   int c;
242
243   /* Clear out any saved values. */
244   if (local_prompt)
245     free (local_prompt);
246   if (local_prompt_prefix)
247     free (local_prompt_prefix);
248   local_prompt = local_prompt_prefix = (char *)0;
249
250   if (prompt == 0 || *prompt == '\0')
251     return (0);
252
253   p = strrchr (prompt, '\n');
254   if (!p)
255     {
256       /* The prompt is only one line. */
257       local_prompt = expand_prompt (prompt, &visible_length);
258       local_prompt_prefix = (char *)0;
259       return (visible_length);
260     }
261   else
262     {
263       /* The prompt spans multiple lines. */
264       t = ++p;
265       local_prompt = expand_prompt (p, &visible_length);
266       c = *t; *t = '\0';
267       /* The portion of the prompt string up to and including the
268          final newline is now null-terminated. */
269       local_prompt_prefix = expand_prompt (prompt, &prefix_length);
270       *t = c;
271       return (prefix_length);
272     }
273 }
274
275 /* Basic redisplay algorithm. */
276 void
277 rl_redisplay ()
278 {
279   register int in, out, c, linenum;
280   register char *line = invisible_line;
281   int c_pos = 0, inv_botlin = 0, wrap_offset, wrap_column;
282   char *prompt_this_line;
283
284   if (!readline_echoing_p)
285     return;
286
287   if (!rl_display_prompt)
288     rl_display_prompt = "";
289
290   if (!invisible_line)
291     {
292       visible_line = xmalloc (line_size);
293       invisible_line = xmalloc (line_size);
294       line = invisible_line;
295       for (in = 0; in < line_size; in++)
296         {
297           visible_line[in] = 0;
298           invisible_line[in] = 1;
299         }
300       rl_on_new_line ();
301     }
302
303   /* Draw the line into the buffer. */
304   c_pos = -1;
305
306   /* Mark the line as modified or not.  We only do this for history
307      lines. */
308   out = 0;
309   if (_rl_mark_modified_lines && current_history () && rl_undo_list)
310     {
311       line[out++] = '*';
312       line[out] = '\0';
313     }
314
315   /* If someone thought that the redisplay was handled, but the currently
316      visible line has a different modification state than the one about
317      to become visible, then correct the caller's misconception. */
318   if (visible_line[0] != invisible_line[0])
319     rl_display_fixed = 0;
320
321   /* If the prompt to be displayed is the `primary' readline prompt (the
322      one passed to readline()), use the values we have already expanded.
323      If not, use what's already in rl_display_prompt.  WRAP_OFFSET is the
324      number of non-visible characters in the prompt string. */
325   if (rl_display_prompt == rl_prompt)
326     {
327       int local_len = local_prompt ? strlen (local_prompt) : 0;
328       if (local_prompt_prefix && forced_display)
329         _rl_output_some_chars (local_prompt_prefix, strlen (local_prompt_prefix));
330
331       if (local_len > 0)
332         strncpy (line + out, local_prompt, local_len);
333       out += local_len;
334       line[out] = '\0';
335       wrap_offset = local_len - visible_length;
336     }
337   else
338     {
339       int pmtlen;
340       prompt_this_line = strrchr (rl_display_prompt, '\n');
341       if (!prompt_this_line)
342         prompt_this_line = rl_display_prompt;
343       else
344         {
345           prompt_this_line++;
346           if (forced_display)
347             _rl_output_some_chars (rl_display_prompt, prompt_this_line - rl_display_prompt);
348         }
349
350       pmtlen = strlen (prompt_this_line);
351       strncpy (line + out,  prompt_this_line, pmtlen);
352       out += pmtlen;
353       line[out] = '\0';
354       wrap_offset = 0;
355     }
356
357   for (in = 0; in < rl_end; in++)
358     {
359       c = (unsigned char)rl_line_buffer[in];
360
361       if (out + 8 >= line_size)         /* XXX - 8 for \t */
362         {
363           line_size *= 2;
364           visible_line = xrealloc (visible_line, line_size);
365           invisible_line = xrealloc (invisible_line, line_size);
366           line = invisible_line;
367         }
368
369       if (in == rl_point)
370         c_pos = out;
371
372       if (META_CHAR (c))
373         {
374           if (_rl_output_meta_chars == 0)
375             {
376               sprintf (line + out, "\\%o", c);
377               out += 4;
378             }
379           else
380             line[out++] = c;      
381         }
382 #if defined (DISPLAY_TABS)
383       else if (c == '\t')
384         {
385           register int newout = (out | (int)7) + 1;
386           while (out < newout)
387             line[out++] = ' ';
388         }
389 #endif
390       else if (c < ' ')
391         {
392           line[out++] = '^';
393           line[out++] = UNCTRL (c);     /* XXX was c ^ 0x40 */
394         }
395       else if (c == 127)
396         {
397           line[out++] = '^';
398           line[out++] = '?';
399         }
400       else
401         line[out++] = c;
402     }
403   line[out] = '\0';
404   if (c_pos < 0)
405     c_pos = out;
406
407   /* C_POS == position in buffer where cursor should be placed. */
408
409   /* PWP: now is when things get a bit hairy.  The visible and invisible
410      line buffers are really multiple lines, which would wrap every
411      (screenwidth - 1) characters.  Go through each in turn, finding
412      the changed region and updating it.  The line order is top to bottom. */
413
414   /* If we can move the cursor up and down, then use multiple lines,
415      otherwise, let long lines display in a single terminal line, and
416      horizontally scroll it. */
417
418   if (!_rl_horizontal_scroll_mode && term_up && *term_up)
419     {
420       int total_screen_chars = screenchars;
421       int nleft, cursor_linenum, pos, changed_screen_line;
422
423       if (!rl_display_fixed || forced_display)
424         {
425           forced_display = 0;
426
427           /* If we have more than a screenful of material to display, then
428              only display a screenful.  We should display the last screen,
429              not the first.  I'll fix this in a minute. */
430           if (out >= total_screen_chars)
431             out = total_screen_chars - 1;
432
433           /* Number of screen lines to display.  The first line wraps at
434              (screenwidth + wrap_offset) chars, the rest of the lines have
435              screenwidth chars. */
436           nleft = out - wrap_offset + term_xn - 1;
437           inv_botlin = (nleft > 0) ? nleft / screenwidth : 0;
438
439           /* The first line is at character position 0 in the buffer.  The
440              second and subsequent lines start at N * screenwidth, offset by
441              OFFSET.  OFFSET is wrap_offset for the invisible line and
442              visible_wrap_offset for the line currently displayed. */
443
444 #define W_OFFSET(line, offset) ((line) == 0 ? offset : 0)
445 #define L_OFFSET(n, offset) ((n) > 0 ? ((n) * screenwidth) + (offset) : 0)
446 #define VIS_CHARS(line) &visible_line[L_OFFSET((line), visible_wrap_offset)]
447 #define VIS_LINE(line) ((line) > _rl_vis_botlin) ? "" : VIS_CHARS(line)
448 #define INV_LINE(line) &invisible_line[L_OFFSET((line), wrap_offset)]
449
450           /* For each line in the buffer, do the updating display. */
451           for (linenum = 0; linenum <= inv_botlin; linenum++)
452             {
453               update_line (VIS_LINE(linenum), INV_LINE(linenum), linenum,
454                            screenwidth + W_OFFSET(linenum, visible_wrap_offset),
455                            screenwidth + W_OFFSET(linenum, wrap_offset),
456                            inv_botlin);
457
458               /* If this is the line with the prompt, we might need to
459                  compensate for invisible characters in the new line. Do
460                  this only if there is not more than one new line (which
461                  implies that we completely overwrite the old visible line)
462                  and the new line is shorter than the old. */
463               if (linenum == 0 &&
464                   inv_botlin == 0 &&
465                   (wrap_offset > visible_wrap_offset) &&
466                   (_rl_last_c_pos < visible_first_line_len))
467                 {
468                   nleft = screenwidth + wrap_offset - _rl_last_c_pos;
469                   if (nleft)
470                     clear_to_eol (nleft);
471                 }
472
473               /* Since the new first line is now visible, save its length. */
474               if (linenum == 0)
475                 visible_first_line_len = (inv_botlin > 0) ? screenwidth : out - wrap_offset;
476             }
477
478           /* We may have deleted some lines.  If so, clear the left over
479              blank ones at the bottom out. */
480           if (_rl_vis_botlin > inv_botlin)
481             {
482               char *tt;
483               for (; linenum <= _rl_vis_botlin; linenum++)
484                 {
485                   tt = VIS_CHARS (linenum);
486                   _rl_move_vert (linenum);
487                   _rl_move_cursor_relative (0, tt);
488                   clear_to_eol
489                     ((linenum == _rl_vis_botlin) ? strlen (tt) : screenwidth);
490                 }
491             }
492           _rl_vis_botlin = inv_botlin;
493
494           /* Move the cursor where it should be. */
495           /* Which line? */
496           nleft = c_pos - wrap_offset + term_xn - 1;
497           cursor_linenum = (nleft > 0) ? nleft / screenwidth : 0;
498
499           /* CHANGED_SCREEN_LINE is set to 1 if we have moved to a
500              different screen line during this redisplay. */
501           changed_screen_line = _rl_last_v_pos != cursor_linenum;
502           if (changed_screen_line)
503             {
504               _rl_move_vert (cursor_linenum);
505               /* If we moved up to the line with the prompt using term_up,
506                  the physical cursor position on the screen stays the same,
507                  but the buffer position needs to be adjusted to account
508                  for invisible characters. */
509               if (cursor_linenum == 0 && wrap_offset)
510                 _rl_last_c_pos += wrap_offset;
511             }
512
513           /* We have to reprint the prompt if it contains invisible
514              characters, since it's not generally OK to just reprint
515              the characters from the current cursor position. */
516           nleft = visible_length + wrap_offset;
517           if (cursor_linenum == 0 && wrap_offset > 0 && _rl_last_c_pos > 0 &&
518               _rl_last_c_pos <= nleft && local_prompt)
519             {
520               if (term_cr)
521                 tputs (term_cr, 1, _rl_output_character_function);
522               _rl_output_some_chars (local_prompt, nleft);
523               _rl_last_c_pos = nleft;
524             }
525
526           /* Where on that line?  And where does that line start
527              in the buffer? */
528           pos = L_OFFSET(cursor_linenum, wrap_offset);
529           /* nleft == number of characters in the line buffer between the
530              start of the line and the cursor position. */
531           nleft = c_pos - pos;
532
533           /* Since backspace() doesn't know about invisible characters in the
534              prompt, and there's no good way to tell it, we compensate for
535              those characters here and call backspace() directly. */
536           if (wrap_offset && cursor_linenum == 0 && nleft < _rl_last_c_pos)
537             {
538               backspace (_rl_last_c_pos - nleft);
539               _rl_last_c_pos = nleft;
540             }
541
542           if (nleft != _rl_last_c_pos)
543             _rl_move_cursor_relative (nleft, &invisible_line[pos]);
544         }
545     }
546   else                          /* Do horizontal scrolling. */
547     {
548 #define M_OFFSET(margin, offset) ((margin) == 0 ? offset : 0)
549       int lmargin, ndisp, nleft, phys_c_pos, t;
550
551       /* Always at top line. */
552       _rl_last_v_pos = 0;
553
554       /* Compute where in the buffer the displayed line should start.  This
555          will be LMARGIN. */
556
557       /* The number of characters that will be displayed before the cursor. */
558       ndisp = c_pos - wrap_offset;
559       nleft  = visible_length + wrap_offset;
560       /* Where the new cursor position will be on the screen.  This can be
561          longer than SCREENWIDTH; if it is, lmargin will be adjusted. */
562       phys_c_pos = c_pos - (last_lmargin ? last_lmargin : wrap_offset);
563       t = screenwidth / 3;
564
565       /* If the number of characters had already exceeded the screenwidth,
566          last_lmargin will be > 0. */
567
568       /* If the number of characters to be displayed is more than the screen
569          width, compute the starting offset so that the cursor is about
570          two-thirds of the way across the screen. */
571       if (phys_c_pos > screenwidth - 2)
572         {
573           lmargin = c_pos - (2 * t);
574           if (lmargin < 0)
575             lmargin = 0;
576           /* If the left margin would be in the middle of a prompt with
577              invisible characters, don't display the prompt at all. */
578           if (wrap_offset && lmargin > 0 && lmargin < nleft)
579             lmargin = nleft;
580         }
581       else if (ndisp < screenwidth - 2)         /* XXX - was -1 */
582         lmargin = 0;
583       else if (phys_c_pos < 1)
584         {
585           /* If we are moving back towards the beginning of the line and
586              the last margin is no longer correct, compute a new one. */
587           lmargin = ((c_pos - 1) / t) * t;      /* XXX */
588           if (wrap_offset && lmargin > 0 && lmargin < nleft)
589             lmargin = nleft;
590         }
591       else
592         lmargin = last_lmargin;
593
594       /* If the first character on the screen isn't the first character
595          in the display line, indicate this with a special character. */
596       if (lmargin > 0)
597         line[lmargin] = '<';
598
599       /* If SCREENWIDTH characters starting at LMARGIN do not encompass
600          the whole line, indicate that with a special characters at the
601          right edge of the screen.  If LMARGIN is 0, we need to take the
602          wrap offset into account. */
603       t = lmargin + M_OFFSET (lmargin, wrap_offset) + screenwidth;
604       if (t < out)
605         line[t - 1] = '>';
606
607       if (!rl_display_fixed || forced_display || lmargin != last_lmargin)
608         {
609           forced_display = 0;
610           update_line (&visible_line[last_lmargin],
611                        &invisible_line[lmargin],
612                        0,
613                        screenwidth + visible_wrap_offset,
614                        screenwidth + (lmargin ? 0 : wrap_offset),
615                        0);
616
617           /* If the visible new line is shorter than the old, but the number
618              of invisible characters is greater, and we are at the end of
619              the new line, we need to clear to eol. */
620           t = _rl_last_c_pos - M_OFFSET (lmargin, wrap_offset);
621           if ((M_OFFSET (lmargin, wrap_offset) > visible_wrap_offset) &&
622               (_rl_last_c_pos == out) &&
623               t < visible_first_line_len)
624             {
625               nleft = screenwidth - t;
626               clear_to_eol (nleft);
627             }
628           visible_first_line_len = out - lmargin - M_OFFSET (lmargin, wrap_offset);
629           if (visible_first_line_len > screenwidth)
630             visible_first_line_len = screenwidth;
631
632           _rl_move_cursor_relative (c_pos - lmargin, &invisible_line[lmargin]);
633           last_lmargin = lmargin;
634         }
635     }
636   fflush (rl_outstream);
637
638   /* Swap visible and non-visible lines. */
639   {
640     char *temp = visible_line;
641     visible_line = invisible_line;
642     invisible_line = temp;
643     rl_display_fixed = 0;
644     /* If we are displaying on a single line, and last_lmargin is > 0, we
645        are not displaying any invisible characters, so set visible_wrap_offset
646        to 0. */
647     if (_rl_horizontal_scroll_mode && last_lmargin)
648       visible_wrap_offset = 0;
649     else
650       visible_wrap_offset = wrap_offset;
651   }
652 }
653
654 /* PWP: update_line() is based on finding the middle difference of each
655    line on the screen; vis:
656
657                              /old first difference
658         /beginning of line   |        /old last same       /old EOL
659         v                    v        v             v
660 old:    eddie> Oh, my little gruntle-buggy is to me, as lurgid as
661 new:    eddie> Oh, my little buggy says to me, as lurgid as
662         ^                    ^  ^                          ^
663         \beginning of line   |  \new last same     \new end of line
664                              \new first difference
665
666    All are character pointers for the sake of speed.  Special cases for
667    no differences, as well as for end of line additions must be handeled.
668
669    Could be made even smarter, but this works well enough */
670 static void
671 update_line (old, new, current_line, omax, nmax, inv_botlin)
672      register char *old, *new;
673      int current_line, omax, nmax;
674 {
675   register char *ofd, *ols, *oe, *nfd, *nls, *ne;
676   int temp, lendiff, wsatend, od, nd;
677
678   /* If we're at the right edge of a terminal that supports xn, we're
679      ready to wrap around, so do so.  This fixes problems with knowing
680      the exact cursor position and cut-and-paste with certain terminal
681      emulators.  In this calculation, TEMP is the physical screen
682      position of the cursor. */
683   temp = _rl_last_c_pos - W_OFFSET(_rl_last_v_pos, visible_wrap_offset);
684   if (temp == screenwidth && term_xn && !_rl_horizontal_scroll_mode
685       && _rl_last_v_pos == current_line - 1)
686     {
687       if (new[0])
688         putc (new[0], rl_outstream);
689       else
690         putc (' ', rl_outstream);
691       _rl_last_c_pos = 1;               /* XXX */
692       _rl_last_v_pos++;
693       if (old[0] && new[0])
694         old[0] = new[0];
695     }
696       
697   /* Find first difference. */
698   for (ofd = old, nfd = new;
699        (ofd - old < omax) && *ofd && (*ofd == *nfd);
700        ofd++, nfd++)
701     ;
702
703   /* Move to the end of the screen line.  ND and OD are used to keep track
704      of the distance between ne and new and oe and old, respectively, to
705      move a subtraction out of each loop. */
706   for (od = ofd - old, oe = ofd; od < omax && *oe; oe++, od++);
707   for (nd = nfd - new, ne = nfd; nd < nmax && *ne; ne++, nd++);
708
709   /* If no difference, continue to next line. */
710   if (ofd == oe && nfd == ne)
711     return;
712
713   wsatend = 1;                  /* flag for trailing whitespace */
714   ols = oe - 1;                 /* find last same */
715   nls = ne - 1;
716   while ((ols > ofd) && (nls > nfd) && (*ols == *nls))
717     {
718       if (*ols != ' ')
719         wsatend = 0;
720       ols--;
721       nls--;
722     }
723
724   if (wsatend)
725     {
726       ols = oe;
727       nls = ne;
728     }
729   else if (*ols != *nls)
730     {
731       if (*ols)                 /* don't step past the NUL */
732         ols++;
733       if (*nls)
734         nls++;
735     }
736
737   _rl_move_vert (current_line);
738
739   /* If this is the first line and there are invisible characters in the
740      prompt string, and the prompt string has not changed, then redraw
741      the entire prompt string.  We can only do this reliably if the
742      terminal supports a `cr' capability.
743
744      This is more than just an efficiency hack -- there is a problem with
745      redrawing portions of the prompt string if they contain terminal
746      escape sequences (like drawing the `unbold' sequence without a
747      corresponding `bold') that manifests itself on certain terminals. */
748
749   lendiff = local_prompt ? strlen (local_prompt) : 0;
750   if (current_line == 0 && !_rl_horizontal_scroll_mode &&
751       lendiff > visible_length &&
752       _rl_last_c_pos > 0 && (ofd - old) >= lendiff && term_cr)
753     {
754       tputs (term_cr, 1, _rl_output_character_function);
755       _rl_output_some_chars (local_prompt, lendiff);
756       _rl_last_c_pos = lendiff;
757     }
758
759   _rl_move_cursor_relative (ofd - old, old);
760
761   /* if (len (new) > len (old)) */
762   lendiff = (nls - nfd) - (ols - ofd);
763
764   /* Insert (diff (len (old), len (new)) ch. */
765   temp = ne - nfd;
766   if (lendiff > 0)
767     {
768       /* Non-zero if we're increasing the number of lines. */
769       int gl = current_line >= _rl_vis_botlin && inv_botlin > _rl_vis_botlin;
770       /* Sometimes it is cheaper to print the characters rather than
771          use the terminal's capabilities.  If we're growing the number
772          of lines, make sure we actually cause the new line to wrap
773          around on auto-wrapping terminals. */
774       if (terminal_can_insert && ((2 * temp) >= lendiff || term_IC) && (!term_xn || !gl))
775         {
776           /* If lendiff > visible_length and _rl_last_c_pos == 0 and
777              _rl_horizontal_scroll_mode == 1, inserting the characters with
778              term_IC or term_ic will screw up the screen because of the
779              invisible characters.  We need to just draw them. */
780           if (*ols && (!_rl_horizontal_scroll_mode || _rl_last_c_pos > 0 ||
781                         lendiff <= visible_length))
782             {
783               insert_some_chars (nfd, lendiff);
784               _rl_last_c_pos += lendiff;
785             }
786           else
787             {
788               /* At the end of a line the characters do not have to
789                  be "inserted".  They can just be placed on the screen. */
790               _rl_output_some_chars (nfd, lendiff);
791               _rl_last_c_pos += lendiff;
792             }
793           /* Copy (new) chars to screen from first diff to last match. */
794           temp = nls - nfd;
795           if ((temp - lendiff) > 0)
796             {
797               _rl_output_some_chars (nfd + lendiff, temp - lendiff);
798               _rl_last_c_pos += temp - lendiff;
799             }
800         }
801       else
802         {
803           /* cannot insert chars, write to EOL */
804           _rl_output_some_chars (nfd, temp);
805           _rl_last_c_pos += temp;
806         }
807     }
808   else                          /* Delete characters from line. */
809     {
810       /* If possible and inexpensive to use terminal deletion, then do so. */
811       if (term_dc && (2 * temp) >= -lendiff)
812         {
813           /* If all we're doing is erasing the invisible characters in the
814              prompt string, don't bother.  It screws up the assumptions
815              about what's on the screen. */
816           if (_rl_horizontal_scroll_mode && _rl_last_c_pos == 0 &&
817               -lendiff == visible_wrap_offset)
818             lendiff = 0;
819
820           if (lendiff)
821             delete_chars (-lendiff); /* delete (diff) characters */
822
823           /* Copy (new) chars to screen from first diff to last match */
824           temp = nls - nfd;
825           if (temp > 0)
826             {
827               _rl_output_some_chars (nfd, temp);
828               _rl_last_c_pos += temp;
829             }
830         }
831       /* Otherwise, print over the existing material. */
832       else
833         {
834           if (temp > 0)
835             {
836               _rl_output_some_chars (nfd, temp);
837               _rl_last_c_pos += temp;
838             }
839           lendiff = (oe - old) - (ne - new);
840           if (term_xn && current_line < inv_botlin)
841             space_to_eol (lendiff);
842           else
843             clear_to_eol (lendiff);
844         }
845     }
846 }
847
848 /* Tell the update routines that we have moved onto a new (empty) line. */
849 rl_on_new_line ()
850 {
851   if (visible_line)
852     visible_line[0] = '\0';
853
854   _rl_last_c_pos = _rl_last_v_pos = 0;
855   _rl_vis_botlin = last_lmargin = 0;
856   return 0;
857 }
858
859 /* Actually update the display, period. */
860 rl_forced_update_display ()
861 {
862   if (visible_line)
863     {
864       register char *temp = visible_line;
865
866       while (*temp) *temp++ = '\0';
867     }
868   rl_on_new_line ();
869   forced_display++;
870   rl_redisplay ();
871   return 0;
872 }
873
874 /* Move the cursor from _rl_last_c_pos to NEW, which are buffer indices.
875    DATA is the contents of the screen line of interest; i.e., where
876    the movement is being done. */
877 void
878 _rl_move_cursor_relative (new, data)
879      int new;
880      char *data;
881 {
882   register int i;
883
884   /* If we don't have to do anything, then return. */
885   if (_rl_last_c_pos == new) return;
886
887   /* It may be faster to output a CR, and then move forwards instead
888      of moving backwards. */
889   /* i == current physical cursor position. */
890   i = _rl_last_c_pos - W_OFFSET(_rl_last_v_pos, visible_wrap_offset);
891   if (CR_FASTER (new, _rl_last_c_pos) || (term_xn && i == screenwidth))
892     {
893 #if defined (__MSDOS__)
894       putc ('\r', rl_outstream);
895 #else
896       tputs (term_cr, 1, _rl_output_character_function);
897 #endif /* !__MSDOS__ */
898       _rl_last_c_pos = 0;
899     }
900
901   if (_rl_last_c_pos < new)
902     {
903       /* Move the cursor forward.  We do it by printing the command
904          to move the cursor forward if there is one, else print that
905          portion of the output buffer again.  Which is cheaper? */
906
907       /* The above comment is left here for posterity.  It is faster
908          to print one character (non-control) than to print a control
909          sequence telling the terminal to move forward one character.
910          That kind of control is for people who don't know what the
911          data is underneath the cursor. */
912 #if defined (HACK_TERMCAP_MOTION)
913       extern char *term_forward_char;
914
915       if (term_forward_char)
916         for (i = _rl_last_c_pos; i < new; i++)
917           tputs (term_forward_char, 1, _rl_output_character_function);
918       else
919         for (i = _rl_last_c_pos; i < new; i++)
920           putc (data[i], rl_outstream);
921 #else
922       for (i = _rl_last_c_pos; i < new; i++)
923         putc (data[i], rl_outstream);
924 #endif /* HACK_TERMCAP_MOTION */
925     }
926   else if (_rl_last_c_pos != new)
927     backspace (_rl_last_c_pos - new);
928   _rl_last_c_pos = new;
929 }
930
931 /* PWP: move the cursor up or down. */
932 void
933 _rl_move_vert (to)
934      int to;
935 {
936   register int delta, i;
937
938   if (_rl_last_v_pos == to || to > screenheight)
939     return;
940
941 #if defined (__GO32__)
942   {
943     int row, col;
944
945     ScreenGetCursor (&row, &col);
946     ScreenSetCursor ((row + to - _rl_last_v_pos), col);
947   }
948 #else /* !__GO32__ */
949
950   if ((delta = to - _rl_last_v_pos) > 0)
951     {
952       for (i = 0; i < delta; i++)
953         putc ('\n', rl_outstream);
954       tputs (term_cr, 1, _rl_output_character_function);
955       _rl_last_c_pos = 0;
956     }
957   else
958     {                   /* delta < 0 */
959       if (term_up && *term_up)
960         for (i = 0; i < -delta; i++)
961           tputs (term_up, 1, _rl_output_character_function);
962     }
963 #endif /* !__GO32__ */
964   _rl_last_v_pos = to;          /* Now TO is here */
965 }
966
967 /* Physically print C on rl_outstream.  This is for functions which know
968    how to optimize the display.  Return the number of characters output. */
969 rl_show_char (c)
970      int c;
971 {
972   int n = 1;
973   if (META_CHAR (c) && (_rl_output_meta_chars == 0))
974     {
975       fprintf (rl_outstream, "M-");
976       n += 2;
977       c = UNMETA (c);
978     }
979
980 #if defined (DISPLAY_TABS)
981   if (c < 32 && c != '\t')
982 #else
983   if (c < 32)
984 #endif /* !DISPLAY_TABS */
985     {
986       fprintf (rl_outstream, "C-");
987       n += 2;
988       c += 64;
989     }
990
991   putc (c, rl_outstream);
992   fflush (rl_outstream);
993   return n;
994 }
995
996 int
997 rl_character_len (c, pos)
998      register int c, pos;
999 {
1000   unsigned char uc;
1001
1002   uc = (unsigned char)c;
1003
1004   if (META_CHAR (uc))
1005     return ((_rl_output_meta_chars == 0) ? 4 : 1);
1006
1007   if (uc == '\t')
1008     {
1009 #if defined (DISPLAY_TABS)
1010       return (((pos | 7) + 1) - pos);
1011 #else
1012       return (2);
1013 #endif /* !DISPLAY_TABS */
1014     }
1015
1016   return ((isprint (uc)) ? 1 : 2);
1017 }
1018
1019 /* How to print things in the "echo-area".  The prompt is treated as a
1020    mini-modeline. */
1021
1022 #if defined (HAVE_VARARGS_H)
1023 rl_message (va_alist)
1024      va_dcl
1025 {
1026   char *format;
1027   va_list args;
1028
1029   va_start (args);
1030   format = va_arg (args, char *);
1031   vsprintf (msg_buf, format, args);
1032   va_end (args);
1033
1034   rl_display_prompt = msg_buf;
1035   rl_redisplay ();
1036   return 0;
1037 }
1038 #else /* !HAVE_VARARGS_H */
1039 rl_message (format, arg1, arg2)
1040      char *format;
1041 {
1042   sprintf (msg_buf, format, arg1, arg2);
1043   rl_display_prompt = msg_buf;
1044   rl_redisplay ();
1045   return 0;
1046 }
1047 #endif /* !HAVE_VARARGS_H */
1048
1049 /* How to clear things from the "echo-area". */
1050 rl_clear_message ()
1051 {
1052   rl_display_prompt = rl_prompt;
1053   rl_redisplay ();
1054   return 0;
1055 }
1056
1057 rl_reset_line_state ()
1058 {
1059   rl_on_new_line ();
1060
1061   rl_display_prompt = rl_prompt ? rl_prompt : "";
1062   forced_display = 1;
1063   return 0;
1064 }
1065
1066 /* Quick redisplay hack when erasing characters at the end of the line. */
1067 void
1068 _rl_erase_at_end_of_line (l)
1069      int l;
1070 {
1071   register int i;
1072
1073   backspace (l);
1074   for (i = 0; i < l; i++)
1075     putc (' ', rl_outstream);
1076   backspace (l);
1077   for (i = 0; i < l; i++)
1078     visible_line[--_rl_last_c_pos] = '\0';
1079   rl_display_fixed++;
1080 }
1081
1082 /* Clear to the end of the line.  COUNT is the minimum
1083    number of character spaces to clear, */
1084 static void
1085 clear_to_eol (count)
1086      int count;
1087 {
1088 #if !defined (__GO32__)
1089   if (term_clreol)
1090     {
1091       tputs (term_clreol, 1, _rl_output_character_function);
1092     }
1093   else
1094 #endif /* !__GO32__ */
1095     space_to_eol (count);
1096 }
1097
1098 /* Clear to the end of the line using spaces.  COUNT is the minimum
1099    number of character spaces to clear, */
1100 static void
1101 space_to_eol (count)
1102      int count;
1103 {
1104   register int i;
1105
1106   for (i = 0; i < count; i++)
1107    putc (' ', rl_outstream);
1108
1109   _rl_last_c_pos += count;
1110 }
1111
1112 /* Insert COUNT characters from STRING to the output stream. */
1113 static void
1114 insert_some_chars (string, count)
1115      char *string;
1116      int count;
1117 {
1118 #if defined (__GO32__)
1119   int row, col, width;
1120   char *row_start;
1121
1122   ScreenGetCursor (&row, &col);
1123   width = ScreenCols ();
1124   row_start = ScreenPrimary + (row * width);
1125
1126   memcpy (row_start + col + count, row_start + col, width - col - count);
1127
1128   /* Place the text on the screen. */
1129   _rl_output_some_chars (string, count);
1130 #else /* !_GO32 */
1131
1132   /* If IC is defined, then we do not have to "enter" insert mode. */
1133   if (term_IC)
1134     {
1135       char *tgoto (), *buffer;
1136       buffer = tgoto (term_IC, 0, count);
1137       tputs (buffer, 1, _rl_output_character_function);
1138       _rl_output_some_chars (string, count);
1139     }
1140   else
1141     {
1142       register int i;
1143
1144       /* If we have to turn on insert-mode, then do so. */
1145       if (term_im && *term_im)
1146         tputs (term_im, 1, _rl_output_character_function);
1147
1148       /* If there is a special command for inserting characters, then
1149          use that first to open up the space. */
1150       if (term_ic && *term_ic)
1151         {
1152           for (i = count; i--; )
1153             tputs (term_ic, 1, _rl_output_character_function);
1154         }
1155
1156       /* Print the text. */
1157       _rl_output_some_chars (string, count);
1158
1159       /* If there is a string to turn off insert mode, we had best use
1160          it now. */
1161       if (term_ei && *term_ei)
1162         tputs (term_ei, 1, _rl_output_character_function);
1163     }
1164 #endif /* !__GO32__ */
1165 }
1166
1167 /* Delete COUNT characters from the display line. */
1168 static void
1169 delete_chars (count)
1170      int count;
1171 {
1172 #if defined (__GO32__)
1173   int row, col, width;
1174   char *row_start;
1175
1176   ScreenGetCursor (&row, &col);
1177   width = ScreenCols ();
1178   row_start = ScreenPrimary + (row * width);
1179
1180   memcpy (row_start + col, row_start + col + count, width - col - count);
1181   memset (row_start + width - count, 0, count * 2);
1182 #else /* !_GO32 */
1183
1184   if (count > screenwidth)      /* XXX */
1185     return;
1186
1187   if (term_DC && *term_DC)
1188     {
1189       char *tgoto (), *buffer;
1190       buffer = tgoto (term_DC, count, count);
1191       tputs (buffer, count, _rl_output_character_function);
1192     }
1193   else
1194     {
1195       if (term_dc && *term_dc)
1196         while (count--)
1197           tputs (term_dc, 1, _rl_output_character_function);
1198     }
1199 #endif /* !__GO32__ */
1200 }
1201
1202 void
1203 _rl_update_final ()
1204 {
1205   int full_lines;
1206
1207   full_lines = 0;
1208   if (_rl_vis_botlin && visible_line[screenwidth * _rl_vis_botlin] == 0)
1209     {
1210       _rl_vis_botlin--;
1211       full_lines = 1;
1212     }
1213   _rl_move_vert (_rl_vis_botlin);
1214   if (full_lines && term_xn)
1215     {
1216       /* Remove final line-wrap flag in xterm. */
1217       char *last_line;
1218       last_line = &visible_line[screenwidth * _rl_vis_botlin];
1219       _rl_move_cursor_relative (screenwidth - 1, last_line);
1220       clear_to_eol (0);
1221       putc (last_line[screenwidth - 1], rl_outstream);
1222     }
1223   _rl_vis_botlin = 0;
1224   crlf ();
1225   fflush (rl_outstream);
1226   rl_display_fixed++;
1227 }
1228
1229 /* Move to the start of the current line. */
1230 static void
1231 cr ()
1232 {
1233   if (term_cr)
1234     {
1235       tputs (term_cr, 1, _rl_output_character_function);
1236       _rl_last_c_pos = 0;
1237     }
1238 }
1239
1240 /* Redisplay the current line after a SIGWINCH is received. */
1241 void
1242 _rl_redisplay_after_sigwinch ()
1243 {
1244   char *t, *oldp;
1245
1246   /* Clear the current line and put the cursor at column 0.  Make sure
1247      the right thing happens if we have wrapped to a new screen line. */
1248   if (term_cr)
1249     {
1250       tputs (term_cr, 1, _rl_output_character_function);
1251       _rl_last_c_pos = 0;
1252       if (term_clreol)
1253         tputs (term_clreol, 1, _rl_output_character_function);
1254       else
1255         {
1256           space_to_eol (screenwidth);
1257           tputs (term_cr, 1, _rl_output_character_function);
1258         }
1259       if (_rl_last_v_pos > 0)
1260         _rl_move_vert (0);
1261     }
1262   else
1263     crlf ();
1264
1265   /* Redraw only the last line of a multi-line prompt. */
1266   t = strrchr (rl_display_prompt, '\n');
1267   if (t)
1268     {
1269       oldp = rl_display_prompt;
1270       rl_display_prompt = ++t;
1271       rl_forced_update_display ();
1272       rl_display_prompt = oldp;
1273     }
1274   else
1275     rl_forced_update_display ();
1276 }