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