home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / AP / TERMNET / READLINE.0 / DISPLAY.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-20  |  35.1 KB  |  1,220 lines

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