home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1995 April / Internet Tools.iso / applic / ncsa / Mac / Telnet2.6 / prerelease / d5 / Telnet 2.6.1d5.src.sit.hqx / Telnet 2.6.1d5 src / source / vs / vsintern.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-22  |  35.8 KB  |  1,416 lines

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