display.c
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:44k
源码类别:

MySQL数据库

开发平台:

Visual C++

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