home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / source / ncsat.cpt / Telnet2.5 final / vs / vsintern.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-14  |  35.3 KB  |  1,412 lines

  1. #ifdef lint
  2. static char *SCCSid = "%W%    (NCSA)    %G%";
  3. #endif
  4.  
  5. /*
  6.  *
  7.  *      Virtual Screen Kernel Internal Routines
  8.  *                      (vsintern.c)
  9.  *  National Center for Supercomputing Applications
  10.  *
  11.  *      by Gaige B. Paulsen
  12.  *
  13.  *    This file contains the private internal calls for the NCSA
  14.  *  Virtual Screen Kernel.
  15.  *
  16.  *        Version Date    Notes
  17.  *        ------- ------  ---------------------------------------------------
  18.  *        0.01    861102  Initial coding -GBP
  19.  *        0.50    861113  First compiled edition -GBP
  20.  *        0.70    861114  Internal operation confirmed -GBP
  21.  *        2.1        871130    NCSA Telnet 2.1 -GBP
  22.  *        2.2     880715    NCSA Telnet 2.2 -GBP
  23.  */
  24.  
  25. #include <String.h>
  26. #include <Quickdraw.h>
  27. #include <Controls.h>
  28. #include <Memory.h>
  29. #include <OSUtils.h>            /* BYU 2.4.12 */
  30. #include "vsdata.h"
  31. #include "vskeys.h"
  32. #include "vsinterf.h"
  33. #include "rsmac.h"
  34.  
  35. #define ScrollbackQuantum 100
  36.  
  37. #define VSIclrattrib 0
  38.  
  39. extern SysEnvRec theWorld;        /* BYU 2.4.12 - System Environment record */
  40. extern int isEightBit();        /* BYU 2.4.12 */
  41.  
  42. int VSIclip
  43.   (
  44.      int *x1, /* starting column */
  45.      int *y1, /* line on which to draw (assumed to lie within visible region) */
  46.      int *x2, /* ending column (inclusive) (output if *n >= 0) */
  47.      int *y2, /* ending line (inclusive) (output if *n >= 0) */
  48.      int *n, /* length of text to draw (input and output) */
  49.      int *offset /* length of initial part of text to skip (output) */
  50.   )
  51.   /* clips a text string to the visible region, given the starting
  52.     line and column in screen coordinates at which it is to be drawn.
  53.     If the length of the string is given, will also compute the ending
  54.     line and column. On return, these coordinates will be normalized
  55.     to the current visible region. Returns a nonzero function result
  56.     iff the string is completely invisible. */
  57.   {
  58.     if (*n >= 0)
  59.       {
  60.       /* compute ending line and column (inclusive) */
  61.         *x2 = *x1 + *n - 1;
  62.         *y2 = *y1;
  63.       }
  64.   /* else take these as given */
  65.  
  66.     if ((*x1 > VSIw->Rright) || (*y2 < VSIw->Rtop))
  67.         return (-1); /* nothing to draw */
  68.  
  69.     if (*x2 > VSIw->Rright)
  70.         *x2 = VSIw->Rright;
  71.     if (*y2 > VSIw->Rbottom)
  72.         *y2 = VSIw->Rbottom;
  73.   /* normalize x1, x2, y1, y2 to be relative to current visible region */
  74.     *x1 -= VSIw->Rleft;
  75.     *x2 -= VSIw->Rleft;
  76.     *y1 -= VSIw->Rtop;
  77.     *y2 -= VSIw->Rtop;
  78.   /* clip part of text string lying outside region, if any */
  79.     *offset = - *x1;
  80.     if (*offset < 0)
  81.         *offset = 0; /* text string starts within region--nothing to clip */
  82.   /* don't draw anything outside region */
  83.     if (*x1 < 0)
  84.         *x1 = 0;
  85.     if (*y1 < 0)
  86.         *y1 = 0;
  87.  
  88.     *n = *x2 - *x1  + 1 ; /* length of string to draw (assuming it's all on one line) */
  89.     if ((*n <= 0) || (*y2 - *y1 < 0))
  90.         return (-1); /* nothing to draw */
  91.     return (0);
  92.   } /* VSIclip */
  93.  
  94. VSIcdellines(w, top, bottom, n, scrolled)
  95.     int
  96.         w, /* affected window */
  97.         top, /* where to start deleting from */
  98.         bottom, /* bottom line of region to scroll */
  99.         n, /* number of lines to delete */
  100.         scrolled;
  101.           /*
  102.             -ve => cancel current selection, if any;
  103.             +ve => selection has moved up one line;
  104.             0 => don't touch selection
  105.           */
  106.   /* updates the display to indicate deletion of the specified
  107.     number of lines from the top of the specified region.
  108.     Returns 0 iff any part of the change is visible. */
  109.   {
  110.     int
  111.         x1 = 0,
  112.         x2 = VSIw->maxwidth,
  113.         tn = -1,
  114.         offset;
  115.  
  116.     if (VSIclip(&x1, &top, &x2, &bottom, &tn, &offset))
  117.         return(-1); /* affected region is invisible */
  118.     tn = bottom - top;
  119.     if (tn < n)
  120.         n = tn; /* don't bother scrolling more lines than scrolling region holds */
  121.     RSdellines(w, top, bottom, n, scrolled);
  122.     return(0);                /* I delete the whole thing! */
  123.   } /* VSIcdellines */
  124.  
  125. VSIcinslines(w, top, bottom, n,scrolled)
  126.     int
  127.         w, /* affected window */
  128.         top, /* where to insert blank lines */
  129.         bottom, /* bottom of area to scroll */
  130.         n, /* number of lines to insert */
  131.         scrolled; /* -ve <=> cancel current selection, if any */
  132.   /* updates the display to indicate insertion of the specified
  133.     number of blank lines at the top of the specified region.
  134.     Returns 0 iff any part of the change is visible. */
  135.   {
  136.     int
  137.         x1 = 0,
  138.         x2 = VSIw->maxwidth,
  139.         tn = -1,
  140.         offset;
  141.  
  142.     if (VSIclip(&x1, &top, &x2, &bottom, &tn, &offset))
  143.         return -1; /* affected region is invisible */
  144.     tn = bottom - top;
  145.     if (tn < n)
  146.         n = tn; /* don't bother scrolling more lines than scrolling region holds */
  147.     RSinslines(w, top, bottom, n, scrolled);
  148.     return 0;
  149.   } /* VSIcinslines */
  150.  
  151. void VSIcurson
  152.   (
  153.     int w,
  154.     int x,
  155.     int y,
  156.     int ForceMove
  157.   )
  158.   /* displays the text cursor at the specified position. If
  159.     ForceMove is true, I am to do any appropriate scrolling of
  160.     the display to ensure the cursor is within the visible region.
  161.     Assumes cursor isn't currently being shown. */
  162.   {
  163.     int
  164.         x2,
  165.         y2,
  166.         n = 1,
  167.         offset;
  168.  
  169.     if (!VSIclip(&x, &y, &x2, &y2, &n, &offset))
  170.       /* cursor already lies within visible region */
  171.         RScurson(w, x, y); /* just make it visible */
  172.     else if (ForceMove)
  173.       {
  174.       /* scroll to make cursor visible */
  175.         x2 = VSIw->Rbottom - VSIw->Rtop;
  176.         if (x2 >= VSIw->lines)
  177.           /* visible region is big enough to show entire screen--
  178.             make sure I don't scroll off the bottom of the screen.
  179.             This call will also do any appropriate scrolling and
  180.             redisplaying of the cursor. */
  181.             VSsetrgn(VSIwn, VSIw->Rleft, VSIw->lines - x2,
  182.                 VSIw->Rright, VSIw->lines);
  183.         else
  184.           {
  185.           /* x & y have been normalized relative to left & top
  186.             of current visible region. Just call the appropriate scroll
  187.             routine, which will also redisplay the cursor. */
  188.             if (y > 0)
  189.                 VSscrolforward(VSIwn, y);
  190.             else
  191.                 VSscrolback(VSIwn, -y);
  192.           } /* if */
  193.       } /* if */
  194.   } /* VSIcurson */
  195.  
  196. void VSIcuroff
  197.   (
  198.     int w
  199.   )
  200.   /* hides the cursor for the specified screen. Assumes it
  201.     is currently being shown (or that it's on an invisible
  202.     part of the screen). */
  203.   {
  204.     int
  205.         x = VSIw->x,
  206.         y = VSIw->y,
  207.         x2,
  208.         y2,
  209.         n = 1,
  210.         offset;
  211.  
  212.     if (!VSIclip(&x, &y, &x2, &y2, &n, &offset))
  213.       /* cursor is on visible part of screen */
  214.         RScursoff(w);
  215.   } /* VSIcuroff */
  216.  
  217. int VSIcursorvisible        /* BYU 2.4.12 */
  218.   (                            /* BYU 2.4.12 */
  219.     void                    /* BYU 2.4.12 */
  220.   )                            /* BYU 2.4.12 */
  221.   {                            /* BYU 2.4.12 */
  222.     int                        /* BYU 2.4.12 */
  223.         x = VSIw->x,        /* BYU 2.4.12 */
  224.         y = VSIw->y,        /* BYU 2.4.12 */
  225.         x2,                    /* BYU 2.4.12 */
  226.         y2,                    /* BYU 2.4.12 */
  227.         n = 1,                /* BYU 2.4.12 */
  228.         offset;                /* BYU 2.4.12 */
  229.                                                             /* BYU 2.4.12 */
  230.     if (!VSIclip(&x, &y, &x2, &y2, &n, &offset))            /* BYU 2.4.12 */
  231.         return 1;                                            /* BYU 2.4.12 */
  232.     else                    /* BYU 2.4.12 */
  233.         return 0;            /* BYU 2.4.12 */
  234. }                            /* BYU 2.4.12 */
  235.  
  236. VSlineArray VSInewlinearray
  237.   (
  238.     int nrlines
  239.   )
  240.   /* allocates an array to hold the specified number of pointers
  241.     to line elements. */
  242.   {
  243.     return
  244.         (VSlineArray) NewPtr(sizeof(VSlinePtr) * nrlines);
  245.   } /* VSInewlinearray */
  246.  
  247. VSlinePtr VSInewlines
  248.   (
  249.     int nlines
  250.   )
  251.   /* allocates a doubly-linked list of the specified number of
  252.     line elements, and returns a pointer to the head of the list,
  253.     or nil if ran out of memory. The maximum number of characters
  254.     each line can hold is controlled by VSIw->allwidth. */
  255.   {
  256.     VSlinePtr t2;
  257.     char *t;
  258.     register int i;
  259.     
  260.     if (!RSokmem(30000))                /* don't use up end of mem for scrollback */
  261.         return((VSlinePtr) 0L);
  262.   /* allocate one block for the line list elements, and another
  263.     block for the line contents. These blocks will be divided up
  264.     and appropriate flags set so I will be able to call DisposPtr
  265.     the right number of times, with the right arguments. */
  266.     if ((t = NewPtr(nlines * (VSIw->allwidth + 1))) != 0L)
  267.       {
  268.         if ((t2 = (VSlinePtr) NewPtr(nlines * sizeof(VSline))) != 0L)
  269.             t2->text = t;
  270.         else
  271.           {
  272.           /* clean up gracefully before giving up */
  273.             DisposPtr(t);
  274.             return(0L);
  275.           } /* if */
  276.       }
  277.     else
  278.       /* sorree no memoree */
  279.         return((VSlinePtr) 0L);
  280.  
  281. /*
  282. *  indicate to the free routine that the first record is the one to free.
  283. */
  284.     t2->mem = 1;                        /* call DisposPtr on this one */
  285.     t2->next = t2 + 1;                    /* point to next one */
  286. /*
  287. *  Take our allocation for multiple lines and fill in the structures to 
  288. *  point to the right text fields and connect the doubly-linked chain.
  289. *
  290. */
  291.     for (i = 1; i < nlines; i++)
  292.       {
  293.         t += (VSIw->allwidth + 1);        /* inc to next text space for a line */
  294.         t2[i].mem = 0;                    /* don't DisposPtr any of these */
  295.         t2[i].text = t;
  296.         t2[i].prev = t2 + i - 1;        /* point back one */
  297.         t2[i].next = t2 + i + 1;        /* point forward one */
  298.       } /* for */
  299.     
  300.     t2[0].prev = 0L;                    /* first one has no prev yet */
  301.     t2[nlines - 1].next = 0L;            /* last one has no next yet */
  302.  
  303.     return(t2);
  304.   } /* VSInewlines */
  305.  
  306. void VSIlistndx
  307.   (
  308.     register VSlinePtr ts,
  309.     register VSlinePtr as
  310.   )
  311.   /* sets up the screen arrays for the current screen to point
  312.     at the given lists of attribute and text lines. */
  313.   {
  314.     register int i;
  315.     for (i = 0; i <= VSIw->lines; i++)
  316.       {
  317.         VSIw->attrst[i] = as;
  318.         VSIw->linest[i] = ts;
  319.         ts = ts->next;
  320.         as = as->next;
  321.       } /* for */
  322.   } /* VSIlistndx */
  323.  
  324. void VSIscroff
  325.   (
  326.     void
  327.   )
  328.   /* called to save current screen contents in scrollback buffer,
  329.     if it is ordained that I should do so. This is called by VSIes
  330.     (below) just before the entire screen is cleared. */
  331.   {
  332.     VSlinePtr tmp;
  333.     register int i, j;
  334.  
  335.     if
  336.       (
  337.             (!VSIw->savelines) /* not saving lines */
  338.         ||
  339.             (VSIw->top != 0) || (VSIw->bottom != VSIw->lines)
  340.               /* scrolling region isn't exactly the entire screen */
  341.       )
  342.         return; /* do nothing */
  343.  
  344.     tmp = VSIw->linest[VSIw->lines]; /* need VSIw->lines + 1 more lines */
  345.     for (i = 0; i <= VSIw->lines; i++)
  346.       {
  347.       /* count off the lines in the part of the scrollback buffer
  348.         below the screen (if any), to see if there's enough to hold
  349.         a screenful. If the scrollback list isn't circular, then
  350.         this part contains lines that have been allocated, but not
  351.         yet used. If the list is circular (meaning it has reached
  352.         its full size), then this is the part that is next in line
  353.         for reuse. */
  354.         if (!tmp->next) 
  355.           { /* not enough */
  356.             j = VSIw->maxlines - VSIw->numlines - i; /* potential unallocated scrollback */
  357.             if (j > ScrollbackQuantum)
  358.                 j = ScrollbackQuantum; /* but don't bother allocating more than this */
  359.             if (j <= 0)
  360.               {
  361.               /* already reached user-specified scrollback limit-- */
  362.               /* make the list circular to indicate no more extension. */
  363.                 tmp->next = VSIw->buftop;
  364.                 VSIw->buftop->prev = tmp;        /* connect it up */
  365.               }
  366.             else
  367.               {
  368.               /* extend the scrollback buffer to make room for
  369.                 another screenful */
  370.                 if (j < VSIw->lines - i + 1)
  371.                     j = VSIw->lines - i + 1; /* need at least this many */
  372.                 if ((tmp->next = VSInewlines(j)) != 0L) 
  373.                     tmp->next->prev = tmp;        /* got some space--link it up */
  374.                 else
  375.                   {
  376.                   /* out of memory--no more extensions */
  377.                     tmp->next = VSIw->buftop;
  378.                     VSIw->buftop->prev = tmp;    
  379.                   } /* if */                
  380.               } /* if */
  381.             break;                                /* only allocate once is enough */
  382.           } /* if */
  383.         tmp = tmp->next; /* keep counting */
  384.       } /* for */
  385.         
  386. /*
  387. *  at this point, we know we have enough memory for the whole scroll.
  388. *  It might be wraparound (reuse of some line elements), might not.
  389. */
  390.         
  391.     for (i = 0; i <= VSIw->lines; i++)
  392.       {
  393.       /* push another screen line into the scrollback area */
  394.         if (VSIw->linest[VSIw->lines]->next == VSIw->buftop)
  395.             VSIw->buftop = VSIw->buftop->next;    /* reusing old space */
  396.         else
  397.             VSIw->numlines++;                /* using some new space */
  398.         VSIw->scrntop = VSIw->scrntop->next; /* move another line into the scrollback buffer */
  399.         VSIlistndx(VSIw->scrntop, VSIw->attrst[1]); /* and update screen arrays */
  400.       /* note that it's up to the caller to clear out the new screen text
  401.         and attribute lines */
  402.       } /* for */
  403.  
  404.     VSIw->vistop = VSIw->scrntop;
  405.     RSbufinfo(VSIwn, VSIw->numlines, VSIw->Rtop, VSIw->Rbottom); /* update vertical scroll bar */
  406.   } /* VSIscroff */
  407.  
  408. void VSIelo
  409.   (
  410.     int s /* line to erase, -ve => line containing cursor */
  411.   )
  412.   /* blanks out the specified line in the screen buffer.
  413.     Doesn't do anything to the display. */
  414.   {
  415.     char *tt, *ta;
  416.     int i;
  417.  
  418.     if (s < 0)
  419.         s = VSIw->y;
  420.  
  421.     ta = &VSIw->attrst[s]->text[0];
  422.     tt = &VSIw->linest[s]->text[0];
  423.     for (i = 0; i <= VSIw->allwidth; i++)
  424.       {
  425.         *ta++ = VSIclrattrib;
  426.         *tt++ = ' ';
  427.       } /* for */
  428.   } /* VSIelo */
  429.  
  430. void VSIes
  431.   (
  432.     void
  433.   )
  434.   /* clears the screen, first saving its contents in the
  435.     scrollback buffer if appropriate. Also updates the display. */
  436.   {
  437.     int
  438.         i;
  439.     int
  440.         x1 = 0,
  441.         y1 = 0,
  442.         x2 = VSIw->maxwidth,
  443.         y2 = VSIw->lines,
  444.         n = -1,
  445.         offset;
  446.  
  447.   /* save screen contents in scrollback buffer, if appropriate */
  448.     if (VSIw->ESscroll)
  449.         VSIscroff();
  450.   /* clear out screen buffer */
  451.     for (i = 0; i <= VSIw->lines; i++)
  452.         VSIelo(i);
  453.   /* update display to show what I've done */
  454.     if (!VSIclip(&x1, &y1, &x2, &y2, &n, &offset)) 
  455.         RSerase(VSIwn, x1, y1, x2, y2);
  456.     VSIw->vistop = VSIw->scrntop;
  457.   } /* VSIes */
  458.  
  459. void VSItabclear
  460.   (
  461.     void
  462.   )
  463.   /* clears all current tab settings. */
  464.   {
  465.     int
  466.         x = 0;
  467.  
  468.     while (x <= VSIw->allwidth)
  469.       {
  470.         VSIw->tabs[x] = ' ';
  471.         x++;
  472.       } /* while */
  473.   } /* VSItabclear */
  474.  
  475. void VSItabinit
  476.   (
  477.     void
  478.   )
  479.   /* sets tab settings to default (every 8 columns). */
  480.   {
  481.     int
  482.         x = 0;
  483.  
  484.     VSItabclear();
  485.     while (x <= VSIw->allwidth)
  486.       {
  487.         VSIw->tabs[x] = 'x';
  488.         x += 8;
  489.       }
  490.     VSIw->tabs[VSIw->allwidth] = 'x';
  491.   } /* VSItabinit */
  492.  
  493. void VSIreset
  494.   (
  495.     void
  496.   )
  497.   /* restores terminal mode settings to defaults and clears screen. */
  498.   {
  499.     VSIw->top = 0;
  500.     VSIw->bottom = VSIw->lines;
  501.     VSIw->parmptr = 0;
  502.     VSIw->escflg = 0;
  503.     VSIw->DECAWM = 0;
  504.     VSIw->DECCKM = 0;
  505.     VSIw->DECPAM = 0;
  506.     VSIw->DECORG = 0;
  507.     VSIw->IRM = 0;
  508.     VSIw->attrib = 0;
  509.     VSIw->Pattrib = -1;
  510.     VSIw->x = 0;
  511.     VSIw->y = 0;
  512.     VSIw->charset = 0;
  513.     VSIes();
  514.     VSItabinit();
  515.   } /* VSIreset */
  516.  
  517. VSIlistmove(TD, BD, TI, BI)
  518.     register VSlinePtr TD, BD, TI, BI;
  519.   /* moves the lines from TD to BD inclusive from their
  520.     present position to between TI and BI. Either of the
  521.     latter may be nil. */
  522.   {
  523.   /* unlink the lines from TD to BD */
  524.     if (TD->prev != 0L)
  525.         TD->prev->next = BD->next;    /* Maintain circularity */
  526.     if (BD->next != 0L)
  527.         BD->next->prev = TD->prev;
  528.   /* relink them between TI and BI */
  529.     TD->prev = TI;                                /* Place the node in its new home */
  530.     BD->next = BI;
  531.     if (TI != 0L)
  532.         TI->next = TD;                    /* Ditto prev->prev */
  533.     if (BI != 0L)
  534.         BI->prev = BD;
  535.   } /* VSIlistmove */
  536.  
  537. void VSIdellines
  538.   (
  539.     int n, /* nr lines to delete */
  540.     int s /* starting line to delete, -ve => line containing cursor */
  541.   )
  542.   /* deletes lines from the screen, scrolling up the remainder and
  543.     inserting new blank lines at the bottom of the scrolling region. */
  544.   {
  545.     int i, j;
  546.     char *ta, *tt;
  547.     VSlinePtr as, ts, TD, BD, TI, BI, itt, ita;
  548.  
  549.     if (s < 0)
  550.         s = VSIw->y;
  551.     if (s + n - 1 > VSIw->bottom)
  552.         n = VSIw->bottom - s + 1; /* don't bother deleting more than scrolling region will hold */
  553.  
  554.   /* find new tops of screen arrays */
  555.     if (s == 0 && n <= VSIw->lines)
  556.       {
  557.       /* element for line after last one being deleted */
  558.         ts = VSIw->linest[n];
  559.         as = VSIw->attrst[n];
  560.       }
  561.     else
  562.       {
  563.       /* top line unaffected, or entire screen is being wiped */
  564.         ts = VSIw->linest[0];
  565.         as = VSIw->attrst[0];
  566.       } /* if */
  567.  
  568.     TD = VSIw->linest[s]; /* topmost line to delete */
  569.     BD = VSIw->linest[s + n - 1]; /* bottommost line to delete */
  570.     TI = VSIw->linest[VSIw->bottom]; /* insert replacement blank lines after this line */
  571.     BI = TI->next; /* insert them before this line (might be nil) */
  572.     itt = TD; /* start of text lines to be blanked out */
  573.   /* the space taken by the deleted lines will be reused for
  574.     the inserted blank lines */
  575.     if (TD != BI && TI != BD)
  576.       /* insertion and deletion areas not adjacent -- move the lines to
  577.         their new position */
  578.         VSIlistmove(TD, BD, TI, BI);
  579.  
  580.     TD = VSIw->attrst[s]; /* topmost line to delete */
  581.     BD = VSIw->attrst[s + n - 1]; /* bottommost line to delete */
  582.     TI = VSIw->attrst[VSIw->bottom]; /* insert new lines after this one */
  583.     BI = TI->next; /* insert them before this line */
  584.   /* perform same rearrangement on attribute lines as on text lines */
  585.     if (TD != BI && TI != BD)
  586.       /* insertion and deletion areas not adjacent -- move the lines to
  587.         their new position */
  588.         VSIlistmove(TD, BD, TI, BI);
  589.  
  590.   /* blank out the newly-created replacement lines */
  591.     ita = TD; /* start of attribute lines to be blanked out */
  592.     for (i = 0; i < n; i++)
  593.       {
  594.         ta = ita->text;
  595.         tt = itt->text;
  596.         for (j = 0; j <= VSIw->allwidth; j++)
  597.           {
  598.             *tt++ = ' ';
  599.             *ta++ = VSIclrattrib;
  600.           } /* for */
  601.         ita = ita->next;
  602.         itt = itt->next;
  603.       } /* for */
  604.  
  605.     VSIw->scrntop = ts; /* new topmost line (if it's changed) */
  606.   /* re-sync screen arrays */
  607.     VSIlistndx(ts, as);
  608.     if (VSIw->Rtop >= 0)
  609.       /* make sure vistop still points to same line position
  610.         on screen that it did before */
  611.         VSIw->vistop = VSIw->linest[VSIw->Rtop];
  612.   /* and actually display the change on-screen */
  613.     VSIcdellines(VSIwn, s, VSIw->bottom, n, -1); /* Cancel current selection */
  614.   } /* VSIdellines */
  615.  
  616. void VSIinslines
  617.   (
  618.     int n, /* how many to insert */
  619.     int s /* where to insert them, -ve => line containing cursor */
  620.   )
  621.   /* inserts the specified number of blank lines, scrolling the
  622.     remaining ones down, and dropping off any that fall off the
  623.     end of the scrolling region. */
  624.   {
  625.     int i, j;
  626.     char *ta, *tt;
  627.     VSlinePtr as, ts, TD, BD, TI, BI, itt, ita;
  628.  
  629.     if (s < 0)
  630.         s = VSIw->y;
  631.     if (s + n - 1 > VSIw->bottom)
  632.       /* don't bother inserting more than scrolling region can hold */
  633.         n = VSIw->bottom - s + 1;
  634.  
  635.   /* find new tops of screen arrays */
  636.     if (s == 0 && n <= VSIw->lines)
  637.       {
  638.       /* element for first blank line being inserted */
  639.         ts = VSIw->linest[VSIw->bottom - n + 1];
  640.         as = VSIw->attrst[VSIw->bottom - n + 1];
  641.       }
  642.     else
  643.       {
  644.       /* top line unaffected, or entire screen is being wiped */
  645.         ts = VSIw->linest[0];
  646.         as = VSIw->attrst[0];
  647.       } /* if */
  648.  
  649.     BI = VSIw->linest[s]; /* insert blank lines before this one */
  650.     TI = BI->prev; /* insert them after this one */
  651.     TD = VSIw->linest[VSIw->bottom - n + 1]; /* topmost line to delete */
  652.     BD = VSIw->linest[VSIw->bottom]; /* bottommost line to delete */
  653.     itt = TD; /* start of text lines to be blanked out */
  654.   /* the space occupied by the deleted lines will be reused for
  655.     the new blank lines */
  656.     if (TD != BI && TI != BD)
  657.       /* new and deleted lines not contiguous -- move the space
  658.         to its new position */
  659.         VSIlistmove(TD, BD, TI, BI);
  660.  
  661.     BI = VSIw->attrst[s]; /* insert new lines before this one */
  662.     TI = BI->prev; /* insert them after this one */
  663.     TD = VSIw->attrst[VSIw->bottom - n + 1]; /* topmost line to delete */
  664.     BD = VSIw->attrst[VSIw->bottom]; /* bottommost line to delete */
  665.   /* do the same rearrangement on the attribute lines */
  666.     if (TD != BI && TI != BD)
  667.       /* new and deleted lines not contiguous -- move the space
  668.         to its new position */
  669.         VSIlistmove(TD, BD, TI, BI);
  670.  
  671.   /* blank out the newly-inserted lines */
  672.     ita = TD; /* start of attribute lines to be blanked out */
  673.     for (i = 0; i < n; i++)
  674.       {
  675.         tt = itt->text;
  676.         ta = ita->text;
  677.         for (j = 0; j <= VSIw->allwidth; j++)
  678.           {
  679.             *tt++ = ' ';
  680.             *ta++ = VSIclrattrib;
  681.           }
  682.         itt = itt->next;
  683.         ita = ita->next;
  684.       } /* for */
  685.  
  686.     VSIw->scrntop = ts;
  687.     VSIlistndx(ts, as); /* re-sync screen arrays */
  688.     if (VSIw->Rtop >= 0)
  689.       /* make sure vistop still points to same line position
  690.         on screen that it did before */
  691.         VSIw->vistop = VSIw->linest[VSIw->Rtop];
  692.   /* update display to match reality */
  693.     VSIcinslines(VSIwn, s, VSIw->bottom, n, -1);  /* Destroy selection area if this is called tooo */
  694.   } /* VSIinslines */
  695.  
  696. void VSIscroll
  697.   (
  698.     void
  699.   )
  700.   /* scrolls scrolling region up one line. */
  701.   {
  702.     register char *temp, *tempa;
  703.     VSlinePtr tmp;
  704.     register int i;
  705.     int tx1, tx2, ty1, ty2, tn, offset;
  706.     short theBottom;                /* NCSA 2.5: the correct screen bottom */
  707.  
  708.     if (VSIw->y > VSIw->lines)        /* BYU - replaces BYU modification below */
  709.         return;                        /* BYU */
  710.  
  711.     tx1 = ty1 = 0;
  712.     tn = 132;
  713.     if (!VSIclip(&tx1, &ty1, &tx2, &ty2, &tn, &offset))
  714.       /* top line of screen is visible */
  715.         RSdrawsep(VSIwn, ty1, 1);                    /* Draw Separator */
  716.  
  717.  
  718.     if ((!VSIw->savelines) /* no scrollback */    ||    (VSIw->top != 0)     /* NCSA 2.5 */
  719.             || ((VSIw->bottom != VSIw->lines) && !VSIw->forcesave))    /* NCSA 2.5 */
  720.               /* region being scrolled is not entire screen */        
  721.       
  722.       /* no saving of lines */
  723.         VSIdellines(1, VSIw->top);
  724.     else
  725.       {
  726.       /* scrolling region is entire screen, and lines are being saved off top */
  727.         if (VSIw->linest[VSIw->lines]->next == 0L)
  728.           {
  729.           /* all currently-allocated scrollback lines have been used, but
  730.             scrollback buffer isn't at its full size -- allocate some more
  731.             space */
  732.             i = VSIw->maxlines - VSIw->numlines; /* number of lines that can be allocated */
  733.             if (i > ScrollbackQuantum)
  734.                 i = ScrollbackQuantum; /* don't bother allocating more than this at once */
  735.             if ((i > 0) && (tmp = VSInewlines(i)) != 0L)
  736.               {
  737.               /* link newly-allocated lines into the list */
  738.                 VSIw->linest[VSIw->lines]->next = tmp;
  739.                 tmp->prev = VSIw->linest[VSIw->lines];
  740.                 VSIw->numlines++; /* use one of the newly-allocated scrollback lines */
  741.                 RSbufinfo(VSIwn, VSIw->numlines, VSIw->Rtop, VSIw->Rbottom); /* update vertical scroll bar accordingly */
  742.               }
  743.             else
  744.               {
  745.               /* not enough memory to extend scrollback buffer--reuse
  746.                 oldest line and give up on future extensions */
  747.                 VSIw->linest[VSIw->lines]->next = VSIw->buftop;        /* Make it circular */
  748.                 VSIw->buftop->prev = VSIw->linest[VSIw->lines];
  749.                 VSIw->buftop = VSIw->buftop->next;    /* step one forward */
  750.               } /* if */
  751.           }    
  752.         else
  753.           {
  754.           /* either there's allocated, but not yet used, space at
  755.             VSIw->linest[VSIw->lines]->next, or the text line list
  756.             is circular. Either way, don't do any new scrollback
  757.             allocation. */
  758.             if (VSIw->linest[VSIw->lines]->next == VSIw->buftop)
  759.               /* scrollback buffer is at full size--reuse oldest line */
  760.                 VSIw->buftop = VSIw->buftop->next;
  761.             else
  762.               {
  763.               /* haven't used up all the space I allocated last time */
  764.                 VSIw->numlines++;                    /* count another line */
  765.                 RSbufinfo(VSIwn, VSIw->numlines, VSIw->Rtop, VSIw->Rbottom); /* update vertical scroll bar accordingly */
  766.               } /* if */
  767.           } /* if */
  768.  
  769.         VSIw->scrntop = VSIw->scrntop->next; /* scroll the screen buffer */
  770.         VSIlistndx(VSIw->scrntop, VSIw->attrst[1]); /* update screen arrays */
  771.       /* reflect the change in the display by scrolling up the visible
  772.         part of the on-screen area, if any */
  773.         
  774.         if (VSIw->forcesave) theBottom = VSIw->bottom;    /* NCSA 2.5: get the correct scroll rgn */
  775.         else theBottom = VSIw->Rbottom;                /* NCSA 2.5: just use whole screen */
  776.         
  777.         if (VSIcdellines(VSIwn, VSIw->Rtop, theBottom, 1, 1))    /* NCSA 2.5 */
  778.           {
  779.           /* no part of on-screen area is visible */
  780.             if (VSIw->Rtop > -VSIw->numlines)
  781.               /* update bounds of visible region to be consistent
  782.                 with portion of scrollback buffer still being displayed */
  783.               {
  784.                 VSIw->Rtop--;
  785.                 VSIw->Rbottom--;
  786.               }
  787.             else
  788.               {
  789.               /* displaying right from top of scrollback buffer. Topmost
  790.                 line being shown has in fact vanished. Update the display
  791.                 to show this fact. */
  792.                 VSIw->vistop = VSIw->vistop->next;
  793.                 RSdellines(VSIwn, 0, VSIw->Rbottom - VSIw->Rtop, 1, 1);
  794.               } /* if */
  795.           }
  796.         else
  797.             VSIw->vistop = VSIw->vistop->next; /* consistent with changed display */
  798.       /* blank out newly-revealed bottom line */
  799.         tempa = VSIw->attrst[VSIw->lines]->text;
  800.         temp = VSIw->linest[VSIw->lines]->text;
  801.         for (i = 0; i <= VSIw->allwidth; i++)
  802.           {
  803.             *temp++ = ' ';
  804.             *tempa++ = 0;
  805.           } /* for */
  806.       } /* if */
  807.     tx1 = ty1 = 0;
  808.     tn = 132;
  809.     if (!VSIclip(&tx1, &ty1, &tx2, &ty2, &tn, &offset))
  810.         RSdrawsep(VSIwn, ty1, 1);                    /* Draw Separator */
  811.   } /* VSIscroll */
  812.  
  813. void VSIindex
  814.   (
  815.     void
  816.   )
  817.   /* moves cursor down one line, unless it's at the bottom of
  818.     the scrolling region, in which case scrolls up one. */
  819.   {
  820.     if (VSIw->y == VSIw->bottom)    /* BYU - changed "==" to ">=" and back again */
  821.         VSIscroll();
  822.     else if (VSIw->y < VSIw->lines)     /* BYU  - added "if ... " */
  823.         VSIw->y++;
  824.   } /* VSIindex */
  825.  
  826. VSIwrapnow(xp, yp)
  827.     int *xp, *yp;
  828.   /* checks current cursor position for VSIw to see if
  829.     it's within bounds, wrapping to next line if not.
  830.     Returns correct cursor position in either case in *xp
  831.     and *yp. */
  832.   {
  833.     if (VSIw->x > VSIw->maxwidth) 
  834.       {
  835.         VSIw->x = 0;
  836.         VSIindex();
  837.       } /* if */
  838.     *xp = VSIw->x;
  839.     *yp = VSIw->y;
  840.   } /* VSIwrapnow */
  841.  
  842. void VSIeeol
  843.   (
  844.     void
  845.   )
  846.   /* erases characters to the end of the current line. */
  847.   {
  848.     char
  849.         *tt,
  850.         *ta;
  851.     int
  852.         x1 = VSIw->x,
  853.         y1 = VSIw->y,
  854.         x2 = VSIw->maxwidth,
  855.         y2 = VSIw->y,
  856.         n = -1,
  857.         offset;
  858.     int
  859.         i;
  860.  
  861.     VSIwrapnow(&x1, &y1);
  862.     y2 = y1;
  863.   /* clear out screen line */
  864.     ta = &VSIw->attrst[y1]->text[x1];
  865.     tt = &VSIw->linest[y1]->text[x1];
  866.     for (i = VSIw->allwidth - x1 + 1; i > 0; i--)
  867.       {
  868.         *ta++ = VSIclrattrib;
  869.         *tt++ = ' ';
  870.       }
  871.   /* update display */
  872.     if (!VSIclip(&x1, &y1, &x2, &y2, &n, &offset)) 
  873.         RSerase(VSIwn, x1, y1, x2, y2);
  874.   } /* VSIeeol */
  875.  
  876. void VSIdelchars
  877.   (
  878.     int x /* how many characters to delete */
  879.   )
  880.   /* deletes characters at the current cursor position onwards,
  881.     moving the remainder of the line to the left. */
  882.   {
  883.     int
  884.         i;
  885.     int
  886.         x1 = VSIw->x,
  887.         y1 = VSIw->y,
  888.         x2 = VSIw->maxwidth,
  889.         y2 = VSIw->y,
  890.         n = -1,
  891.         offset;
  892.     char
  893.         *tempa,
  894.         *temp;
  895.  
  896.     VSIwrapnow(&x1, &y1);
  897.     y2 = y1;
  898.  
  899.     if (x > VSIw->maxwidth)
  900.         x = VSIw->maxwidth;
  901.     tempa = VSIw->attrst[y1]->text;
  902.     temp = VSIw->linest[y1]->text;
  903.     for (i = x1; i <= VSIw->maxwidth - x; i++)
  904.       {
  905.       /* move remainder of line to the left */
  906.         temp[i] = temp[x + i];
  907.         tempa[i] = tempa[x + i];
  908.       }
  909.     for (i = VSIw->maxwidth - x + 1; i <= VSIw->allwidth; i++)
  910.       {
  911.       /* insert blank characters after end of line */
  912.         temp[i] = ' ';
  913.         tempa[i] = VSIclrattrib;
  914.       }
  915.   /* update display */
  916.     if (!VSIclip(&x1, &y1, &x2, &y2, &n, &offset))
  917.       {
  918.         if (VSIw->VSIDC)
  919.             RSdelchars(VSIwn, x1, y1, x);
  920.         else
  921.           /* redraw from cursor position to end of line.
  922.             Trouble is, this isn't going to preserve the
  923.             right attributes. */
  924.             RSdraw
  925.               (
  926.                 VSIwn, x1, y1, VSIw->attrib, n,
  927.                 &VSIw->linest[y1]->text[x1]
  928.               );
  929.       } /* if */
  930.   } /* VSIdelchars */
  931.  
  932. void VSIfreelinelist
  933.   (
  934.     VSlinePtr listhead
  935.   )
  936.   /* frees up the list of line elements pointed to by listhead. */
  937.   {
  938.     register VSlinePtr
  939.         ThisElt, NextElt, ToFree;
  940.  
  941.     ThisElt = listhead;
  942.     ToFree = nil;
  943.     while (true)
  944.       {
  945.         if (ThisElt == nil)
  946.             break;
  947.         NextElt = ThisElt->next;
  948.         if (ThisElt->mem)
  949.           {
  950.             ThisElt->next = ToFree;
  951.             ToFree = ThisElt;
  952.           } /* if */
  953.         ThisElt = NextElt;
  954.         if (ThisElt == listhead)
  955.             break;
  956.       } /* while */
  957.     while (ToFree)
  958.       {
  959.         NextElt = ToFree->next;
  960.         DisposPtr(ToFree->text);
  961.         DisposPtr((Ptr) ToFree);
  962.         ToFree = NextElt;
  963.       } /* while */
  964.   } /* VSIfreelinelist */
  965.  
  966. void VSIfreelines
  967.   (
  968.     void
  969.   )
  970.   /* frees up all the memory allocated for screen and scrollback
  971.     text lines for the current screen. */
  972.   {
  973.     VSIfreelinelist(VSIw->buftop);
  974.   } /* VSIfreelines */
  975.  
  976. void VSIrindex
  977.   (
  978.     void
  979.   )
  980.   /* moves cursor up one line, unless it's at the top of
  981.     the scrolling region, in which case scrolls down one. */
  982.   {
  983.     if (VSIw->y == VSIw->top)
  984.         VSIinslines(1, VSIw->top);
  985.     else
  986.         VSIw->y--;
  987.   } /* VSIrindex */
  988.  
  989. void VSIebol
  990.   (
  991.     void
  992.   )
  993.   /* erases characters from beginning of line to cursor. */
  994.   {
  995.     char
  996.         *tt,
  997.         *ta;
  998.     int
  999.         x1 = 0,
  1000.         y1 = VSIw->y,
  1001.         x2 = VSIw->x,
  1002.         y2 = VSIw->y,
  1003.         n = -1,
  1004.         offset;
  1005.     int
  1006.         i;
  1007.  
  1008.     VSIwrapnow(&x2, &y1);
  1009.     y2 = y1;
  1010.   /* clear from beginning of line to cursor */
  1011.     ta = &VSIw->attrst[y1]->text[0];
  1012.     tt = &VSIw->linest[y1]->text[0];
  1013.     for (i = 0; i <= x2; i++)
  1014.       {
  1015.         *ta++ = VSIclrattrib;
  1016.         *tt++ = ' ';
  1017.       }
  1018.   /* update display */
  1019.     if (!VSIclip(&x1, &y1, &x2, &y2, &n, &offset)) 
  1020.         RSerase(VSIwn, x1, y1, x2, y2);
  1021.   } /* VSIebol */
  1022.  
  1023. void VSIel
  1024.   (
  1025.     int s /* line to clear, -ve => line containing cursor */
  1026.   )
  1027.   /* erases the specified line. */
  1028.   {
  1029.     char
  1030.         *tt,
  1031.         *ta;
  1032.     int
  1033.         x1 = 0,
  1034.         y1 = s,
  1035.         x2 = VSIw->maxwidth,
  1036.         y2 = s,
  1037.         n = -1,
  1038.         offset;
  1039.     int
  1040.         i;
  1041.  
  1042.     if (s < 0)
  1043.       {
  1044.         VSIwrapnow(&x1, &y1);
  1045.         s = y2 = y1;
  1046.         x1 = 0;
  1047.       } /* if */
  1048.   /* clear out line */
  1049.     ta = &VSIw->attrst[s]->text[0];
  1050.     tt = &VSIw->linest[s]->text[0];
  1051.     for(i = 0; i <= VSIw->allwidth; i++)
  1052.       {
  1053.         *ta++ = VSIclrattrib;
  1054.         *tt++ = ' ';
  1055.       }
  1056.   /* update display */
  1057.     if (!VSIclip(&x1, &y1, &x2, &y2, &n, &offset)) 
  1058.         RSerase(VSIwn, x1, y1, x2, y2);
  1059.   } /* VSIel */
  1060.  
  1061. void VSIeeos
  1062.   (
  1063.     void
  1064.   )
  1065.   /* erases characters from cursor to end of screen. */
  1066.   {
  1067.     int
  1068.         i;
  1069.     int
  1070.         x1 = 0,
  1071.         y1 = VSIw->y + 1,
  1072.         x2 = VSIw->maxwidth,
  1073.         y2 = VSIw->lines,
  1074.         n = -1,
  1075.         offset;
  1076.  
  1077.     VSIwrapnow(&x1, &y1);
  1078.     y1++;
  1079.     x1 = 0;
  1080.  
  1081.     i = y1;
  1082.  
  1083.     if (VSIw->forcesave)        /* NCSA 2.5 */
  1084.         VSIscroff();            /* NCSA 2.5 */
  1085.  
  1086.  
  1087.   /* erase complete lines from screen */
  1088.     if (!VSIclip(&x1, &y1, &x2, &y2, &n, &offset)) 
  1089.         RSerase(VSIwn, x1, y1, x2, y2);
  1090.   /* blank out current line from cursor to end */
  1091.     VSIeeol(); /* this also erases the partial line on-screen */
  1092.   /* blank out remaining lines to end of screen */
  1093.     while (i <= VSIw->lines)
  1094.       {
  1095.         VSIelo(i);
  1096.         i++;
  1097.       } /* while */
  1098.     if (VSIw->y < VSIw->lines && (VSIw->x <= VSIw->maxwidth))
  1099.       /* erase the partial line (what--again??) */
  1100.         if (!VSIclip(&x1, &y1, &x2, &y2, &n, &offset)) 
  1101.             RSerase(VSIwn, x1, y1, x2, y2);
  1102.   } /* VSIeeos */
  1103.  
  1104. void VSIebos
  1105.   (
  1106.     void
  1107.   )
  1108.   /* erases characters from beginning of screen to cursor. */
  1109.   {
  1110.     int
  1111.         i;
  1112.     int
  1113.         x1,
  1114.         y1,
  1115.         x2 = VSIw->maxwidth,
  1116.         y2,
  1117.         n = -1,
  1118.         offset;
  1119.  
  1120.     VSIwrapnow(&x1, &y1);
  1121.     y2 = y1 - 1;
  1122.     x1 = 0;
  1123.     y1 = 0;
  1124.   /* blank out current line from beginning to cursor */
  1125.     VSIebol(); /* this also erases the partial line on-screen */
  1126.     i = 0;
  1127.   /* blank out remaining lines from beginning of screen to previous line */
  1128.     while (i < (y2 + 1))
  1129.       {
  1130.         VSIelo(i);
  1131.         i++;
  1132.       } /* while */
  1133.     if (y2 >= 0)
  1134.       /* erase the partial line (what--again??) */
  1135.         if (!VSIclip(&x1, &y1, &x2, &y2, &n, &offset)) 
  1136.             RSerase(VSIwn, x1, y1, x2, y2);
  1137.   } /* VSIebos */
  1138.  
  1139. void VSIrange
  1140.   (
  1141.     void
  1142.   )
  1143.   /* constrains cursor position to valid range (somewhere on the screen). */
  1144.   {
  1145.     int
  1146.         wrap = 0;
  1147.     if (VSIw->DECAWM)
  1148.         wrap = 1;
  1149.     if (VSIw->x < 0)
  1150.         VSIw->x = 0;
  1151.     if (VSIw->x > (VSIw->maxwidth + wrap))
  1152.         VSIw->x = VSIw->maxwidth + wrap;
  1153.     if (VSIw->y < 0)
  1154.         VSIw->y = 0;
  1155.     if (VSIw->y > VSIw->lines)
  1156.         VSIw->y = VSIw->lines;
  1157.   } /* VSIrange */
  1158.  
  1159. void VTsendpos
  1160.   (
  1161.     void
  1162.   )
  1163.   /* sends an escape sequence representing the current cursor position. */
  1164.   {
  1165.     char
  1166.         tempbuf[19];
  1167.     int
  1168.         x = VSIw->x,
  1169.         y = VSIw->y;
  1170.  
  1171.     if (x > VSIw->maxwidth)
  1172.       {
  1173.       /* autowrap pending */
  1174.         x = 0;
  1175.         y++;
  1176.       }
  1177.     if (y > VSIw->lines)
  1178.       /* scroll pending (because of the autowrap) */
  1179.         y = VSIw->lines;
  1180.  
  1181.     sprintf(tempbuf, "\033[%d;%dR", y + 1, x + 1);
  1182.     RSsendstring(VSIwn, tempbuf, strlen(tempbuf));
  1183.   } /* VTsendpos */
  1184.  
  1185. void VTsendstat
  1186.   (
  1187.     void
  1188.   )
  1189.   /* sends the terminal status string. */
  1190.   {
  1191.     RSsendstring(VSIwn, "\033[0n", 4);
  1192.   } /* VTsendstat */
  1193.  
  1194. void VTsendident
  1195.   (
  1196.     void
  1197.   )
  1198.   /* sends an appropriate terminal identification sequence. */
  1199.   {
  1200. #ifdef VT100RESP
  1201.     if (VSIw->allwidth > 80)
  1202.         RSsendstring(VSIwn, "\033[?4;6c", 7);
  1203.     else
  1204.         RSsendstring(VSIwn, "\033[?1;6c", 7);
  1205. #endif VT100RESP
  1206.     if (isEightBit(VSIwn) &&                                 /* BYU 2.4.12 */
  1207.         ((theWorld.keyBoardType == envAExtendKbd) ||        /* BYU 2.4.12 */
  1208.          (theWorld.keyBoardType == envExtISOADBKbd)))        /* BYU 2.4.12 */
  1209.         RSsendstring(VSIwn, "\033[?62;1;6c", 10);            /* BYU 2.4.12 - VT200-series*/
  1210.     else                                                    /* BYU 2.4.12 */
  1211.         RSsendstring(VSIwn, "\033[?6c", 5);                    /* BYU 2.4.12 - VT102 */
  1212.   } /* VTsendident */
  1213.  
  1214. void VTalign
  1215.   (
  1216.     void
  1217.   )
  1218.   /* fills screen with uppercase "E"s, for checking screen alignment. */
  1219.   /* Yeah, right. */
  1220.   {
  1221.     char *tt;
  1222.     int i, j;
  1223.  
  1224.     VSIes();        /* erase the screen */
  1225.     for (j = 0; j < VSIw->lines; j++)
  1226.       {
  1227.         tt = &VSIw->linest[j]->text[0];
  1228.         for (i = 0; i <= VSIw->maxwidth; i++)
  1229.             *tt++ = 'E';
  1230.       } /* for */
  1231.   /* update the display */
  1232.     VSredraw(VSIwn, 0, 0,
  1233.         (VSIw->Rright - VSIw->Rleft), (VSIw->Rbottom - VSIw->Rtop));
  1234.   } /* VTalign */
  1235.  
  1236. void VSIapclear
  1237.   (
  1238.     void
  1239.   )
  1240.   /* initializes all the parameters for the current control
  1241.     sequence, and the current param index, to zero. */
  1242.   {
  1243.     int
  1244.         parmptr = maxparms;
  1245.     while (--parmptr >= 0)
  1246.         VSIw->parms[parmptr] = -1;
  1247.     VSIw->parmptr = 0;
  1248.   } /* VSIapclear */
  1249.  
  1250. void VSIsetoption
  1251.   (
  1252.     int toggle /* 1 => set, 0 => reset */
  1253.   )
  1254.   /* sets/resets various options, as specified by the parms in
  1255.     the current control sequence. Note that this implementation
  1256.     will not set/reset more than one option at a time! */
  1257.   {
  1258.     int
  1259.         WindWidth = VSIw->Rright - VSIw->Rleft;
  1260.  
  1261.     switch (VSIw->parms[0])
  1262.       {
  1263.         case -2: /* DEC-private control sequence */
  1264.             switch (VSIw->parms[1])
  1265.               {
  1266.                 case 1: /* cursor-key mode */
  1267.                     VSIw->DECCKM = toggle;
  1268.                     break;
  1269.                 case 3: /* 80/132 columns */
  1270.                     VSIw->x = VSIw->y = 0; /* home cursor */
  1271.                     VSIes(); /* and clear screen */
  1272.                     if (toggle)        /* 132 column mode */
  1273.                         VSIw->maxwidth = VSIw->allwidth;
  1274.                     else
  1275.                         VSIw->maxwidth = 79;
  1276.                   /* update scroll bars */
  1277.                     RSmargininfo(VSIwn, VSIw->maxwidth - WindWidth, VSIw->Rleft);
  1278.                     break;
  1279.                 case 6: /* origin mode */
  1280.                     VSIw->DECORG = toggle;
  1281.                     break;
  1282.                 case 7: /* autowrap mode */
  1283.                     VSIw->DECAWM = toggle;
  1284.                     break;
  1285.                 default:
  1286.                     break;
  1287.               } /* switch */
  1288.             break;
  1289.         case  4: /* insert/replace character writing mode */
  1290.             VSIw->IRM = toggle;
  1291.             break;
  1292.         default:
  1293.             break;
  1294.       } /* switch */
  1295.   } /* VSIsetoption */
  1296.  
  1297. int VSItab
  1298.   (
  1299.     void
  1300.   )
  1301.   /* advances VSIw->x to the next tab position. */
  1302.   {
  1303.     if (VSIw->x >= VSIw->maxwidth)
  1304.       {
  1305.       /* already at right margin */
  1306.         VSIw->x = VSIw->maxwidth;
  1307.         return(0);
  1308.       } /* if */
  1309.     VSIw->x++; /* advance at least one position */
  1310.     while ((VSIw->tabs[VSIw->x] != 'x') && (VSIw->x < VSIw->maxwidth))
  1311.         VSIw->x++;
  1312.     return(0);
  1313.   } /* VSItab */
  1314.  
  1315. void VSIinschar
  1316.   (
  1317.     int x /* number of blanks to insert */
  1318.   )
  1319.   /* inserts the specified number of blank characters at the
  1320.     current cursor position, moving the rest of the line along,
  1321.     losing any characters that fall off the right margin.
  1322.     Does not update the display. */
  1323.   {
  1324.     int i, j;
  1325.     char *tempa, *temp;
  1326.  
  1327.     VSIwrapnow(&i, &j);
  1328.  
  1329.     tempa = VSIw->attrst[VSIw->y]->text;
  1330.     temp = VSIw->linest[VSIw->y]->text;
  1331.     for (i = VSIw->maxwidth - x; i >= VSIw->x; i--)
  1332.       {
  1333.       /* move along remaining characters on line */
  1334.         temp[x + i] =temp[i];
  1335.         tempa[x + i] = tempa[i];
  1336.       } /* for */
  1337.     for (i = VSIw->x; i < VSIw->x + x; i++)
  1338.       {
  1339.       /* insert appropriate number of blanks */
  1340.         temp[i] = ' ';
  1341.         tempa[i] = VSIclrattrib;
  1342.       } /* for */
  1343.   } /* VSIinschar */
  1344.  
  1345. void VSIinsstring
  1346.   (
  1347.     int len,
  1348.     char *start
  1349.   )
  1350.   /* updates the screen to show insertion of a string of characters
  1351.     at the current cursor position. The text has already been
  1352.     inserted into the screen buffer. Also, the cursor position has
  1353.     already been updated, so the part needing redrawing begins at column
  1354.     (VSIw->x - len). */
  1355.   {
  1356.     if (VSIw->VSIDC)
  1357.         RSinsstring(VSIwn, VSIw->x - len, VSIw->y,
  1358.             VSIw->attrib, len, start);
  1359.     else
  1360.       /* redraw from beginning of text insertion to end of line.
  1361.         Trouble is, this isn't going to preserve the right attributes. */
  1362.         RSdraw(VSIwn, VSIw->x - len, VSIw->y, VSIw->attrib,
  1363.             VSIw->maxwidth - VSIw->x + len + 1, start);
  1364.   } /* VSIinsstring */
  1365.  
  1366. void VSIsave
  1367.   (
  1368.     void
  1369.   )
  1370.   /* saves the current cursor position and attribute settings. */
  1371.   {
  1372.     VSIw->Px = VSIw->x;
  1373.     VSIw->Py = VSIw->y;
  1374.     VSIw->Pattrib = VSIw->attrib;
  1375.   } /* VSIsave */
  1376.  
  1377. void VSIrestore
  1378.   (
  1379.     void
  1380.   )
  1381.   /* restores the last-saved cursor position and attribute settings. */
  1382.   {
  1383.     if (VSIw->Pattrib < 0)
  1384.       /* no previous save */
  1385.         return;
  1386.         
  1387.     VSIw->x = VSIw->Px;
  1388.     VSIw->y = VSIw->Py;
  1389.     VSIrange();
  1390.     VSIw->attrib = VSinattr(VSIw->Pattrib); /* hmm, this will clear the graphics character set selection */
  1391.   } /* VSIrestore */
  1392.  
  1393. void VSIdraw
  1394.   (
  1395.     int VSIwn, /* window number */
  1396.     int x, /* starting column */
  1397.     int y, /* line on which to draw */
  1398.     int a, /* text attributes */
  1399.     int len, /* length of text to draw */
  1400.     char *c /* pointer to text */
  1401.   )
  1402.   /* displays a piece of text (assumed to fit on a single line) on a
  1403.     screen, using the specified attributes, and clipping to the
  1404.     current visible region. Highlights any part of the text lying
  1405.     within the current selection. */
  1406.   {
  1407.     int x2, y2, offset;
  1408.  
  1409.     if (!VSIclip(&x, &y, &x2, &y2, &len, &offset))
  1410.         RSdraw(VSIwn, x, y, a, len, c + offset);
  1411.   } /* VSIdraw */
  1412.