home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / jove414s.zip / screen.c < prev    next >
C/C++ Source or Header  |  1991-07-06  |  9KB  |  404 lines

  1. /***************************************************************************
  2.  * This program is Copyright (C) 1986, 1987, 1988 by Jonathan Payne.  JOVE *
  3.  * is provided to you without charge, and with no warranty.  You may give  *
  4.  * away copies of JOVE, including sources, provided that this notice is    *
  5.  * included in all the files.                                              *
  6.  ***************************************************************************/
  7. #define INCL_BASE
  8. #include <os2.h>
  9. #include "jove.h"
  10. #include "fp.h"
  11. #include "ctype.h"
  12. #include "termcap.h"
  13. #include "disp.h"
  14.  
  15.  
  16. int AbortCnt, CanScroll = 0, tabstop = 8;
  17.  
  18. struct scrimage
  19. *DesiredScreen = 0, *PhysScreen = 0;
  20.  
  21. struct screenline *Screen = 0,    /* the screen (a bunch of screenline) */
  22. *Curline = 0;            /* current line */
  23.  
  24. static struct screenline *Savelines = 0;    /* another bunch (LI of them) */
  25.  
  26.  
  27. static char *cursor;        /* offset into current Line */
  28.  
  29. char *cursend;
  30.  
  31. int CapCol, CapLine, i_line, i_col;
  32. extern unsigned char char_per_line;
  33. extern void _near _fastcall normfun(),
  34.  _near _fastcall scr_win(),
  35.  _near _fastcall clr_page(),
  36.  _near _fastcall clr_eoln();
  37.  
  38. /****************/
  39. void make_scr()
  40. /****************/
  41. {
  42.     /* register */ int i;
  43.     /* register */ struct screenline *ns;
  44.     /* register */ char *nsp;
  45.     DesiredScreen = (struct scrimage *) malloc((unsigned) LI * sizeof(struct scrimage));
  46.     PhysScreen = (struct scrimage *) malloc((unsigned) LI * sizeof(struct scrimage));
  47.  
  48.     Savelines = (struct screenline *)
  49.     malloc((unsigned) LI * sizeof(struct screenline));
  50.     ns = Screen = (struct screenline *)
  51.     malloc((unsigned) LI * sizeof(struct screenline));
  52.  
  53.     nsp = (char *) malloc((unsigned) CO * LI);
  54.     if (nsp == 0) {
  55.     writef("\n\rCannot malloc screen!\n");
  56.     finish(1);
  57.     }
  58.     for (i = 0; i < LI; i++) {
  59.     ns->s_line = nsp;
  60.     nsp += CO;
  61.     ns->s_length = nsp - 1;    /* End of Line */
  62.     ns += 1;
  63.     }
  64.     cl_scr(0);
  65. }
  66.  
  67. /****************************************************/
  68. void clrline(/* register */ char *cp1, /* register */ char *cp2)
  69. /****************************************************/
  70. {
  71.     while (cp1 <= cp2)
  72.     *cp1++ = ' ';
  73. }
  74. int force = 0;
  75.  
  76. /******************/
  77. void cl_eol(void)
  78. /******************/
  79. {
  80.     void _fastcall Placur(int line, int col);
  81.  
  82.     if (cursor > cursend)
  83.     return;
  84.  
  85.     if (cursor < Curline->s_length) {
  86.  
  87.     Placur(i_line, i_col);
  88.     clr_eoln();
  89.  
  90.     clrline(cursor, Curline->s_length);
  91.  
  92.     Curline->s_length = cursor;
  93.     }
  94. }
  95.  
  96. /*******************/
  97. void cl_scr(int doit)
  98. /*******************/
  99. {
  100.     /* register */ int i;
  101.     /* register */ struct screenline *sp = Screen;
  102.  
  103.     for (i = 0; i < LI; i++, sp++) {
  104.     clrline(sp->s_line, sp->s_length);
  105.     sp->s_length = sp->s_line;
  106.     PhysScreen[i].s_id = 0;
  107.     }
  108.     if (doit) {
  109.     clr_page();
  110.  
  111.     CapCol = CapLine = 0;
  112.     UpdMesg = 1;
  113.     }
  114. }
  115.  
  116. /* Output one character (if necessary) at the current position */
  117.  
  118. /******************************************************/
  119. int /* only for lints sake */ dosputc(/* register */ int c)
  120. /******************************************************/
  121. {
  122.     void _fastcall Placur(int line, int col);
  123.  
  124.     if ((force) || (*cursor != c)) {
  125.     if (i_line != CapLine || i_col != CapCol)
  126.         Placur(i_line, i_col);
  127.     *cursor++ = c;
  128.     normfun((char) c);
  129.  
  130.     AbortCnt -= 1;
  131.     CapCol += 1;
  132.     i_col += 1;
  133.     } else {
  134.     cursor += 1;
  135.     i_col += 1;
  136.     }
  137.     return (0);            /* useless result */
  138. }
  139.  
  140. /*
  141.  * Write `line' at the current position of `cursor'.  Stop when we reach the
  142.  * end of the screen.  Aborts if there is a character waiting.
  143.  */
  144. int swrite(line, inversep, abortable)
  145. /* register */ char *line;
  146. int inversep;
  147. /* register */ int abortable;
  148. {
  149.     /* register */ int c;
  150.     int col = i_col, aborted = 0;
  151.     /* register */ int n = cursend - cursor;
  152.     int thebyte;
  153.     force = inversep ? 1 : 0;    /* to force a redraw of the modeline */
  154.  
  155.     if (n <= 0)
  156.     return 1;
  157.     while ((c = *line++) != '\0') {
  158.     if (abortable && AbortCnt < 0) {
  159.         AbortCnt = BufSize;
  160.         if ((InputPending = charp()) != '\0') {
  161.         aborted = 1;
  162.         break;
  163.         }
  164.     }
  165.     if (c == '\t') {
  166.         int nchars;
  167.  
  168.         nchars = (tabstop - (col % tabstop));
  169.         col += nchars;
  170.         while (nchars--) {
  171.         if (--n <= 0)
  172.             break;
  173.         else
  174.             dosputc(((' ')));
  175.         };
  176.  
  177.         if (n <= 0)
  178.         break;
  179.     } else if (((CharTable[0][c & (0400 - 1)]) & 020)) {
  180.         {
  181.         if (--n <= 0)
  182.             break;
  183.         else
  184.             dosputc((('^')));
  185.         };
  186.  
  187.         c = ((c == '\177') ? '?' : c + '@');
  188.         {
  189.         if (--n <= 0)
  190.             break;
  191.         else
  192.             dosputc(((c)));
  193.         };
  194.  
  195.         col += 2;
  196.     } else {
  197.         if (c == 255)
  198.         c = 1;
  199.         if (c == ' ' && inversep)
  200.         c = 255;
  201.         {
  202.         if (--n <= 0)
  203.             break;
  204.         else
  205.             dosputc(((c)));
  206.         };
  207.  
  208.         col += 1;
  209.     }
  210.     }
  211.     if (n <= 0) {
  212.     if ((*line == '\0') && (c != '\t') && !((CharTable[0][c & (0400 - 1)]) & 020))
  213.         dosputc((c));
  214.     else
  215.         dosputc(('!'));
  216.     }
  217.     if (cursor > Curline->s_length)
  218.     Curline->s_length = cursor;
  219.  
  220.     force = 0;
  221.     return !aborted;
  222. }
  223.  
  224.  
  225. /*
  226.  * This is for writing a buffer line to the screen.  This is to minimize the
  227.  * amount of copying from one buffer to another buffer. This gets the info
  228.  * directly from the disk buffers.
  229.  */
  230.  
  231. int BufSwrite(linenum)
  232. int linenum;
  233. {
  234.     void screen_buffer_flush (void);
  235.     /* register */ int n = cursend - cursor, col = 0, c = -1;
  236.     /* register */ char *bp;
  237.     int StartCol = DesiredScreen[linenum].s_offset, visspace = DesiredScreen[linenum].s_window->w_flags & 04,
  238.      aborted = 0;
  239.  
  240.     bp = lcontents(DesiredScreen[linenum].s_lp);
  241.     if (*bp)
  242.     for (;;) {
  243.         if (col >= StartCol) {
  244.         DesiredScreen[linenum].s_offset = col;
  245.         break;
  246.         }
  247.         c = *bp++ & (0400 - 1);
  248.         if (c == '\0')
  249.         break;
  250.         if (c == '\t')
  251.         col += (tabstop - (col % tabstop));
  252.         else if (((CharTable[0][c & (0400 - 1)]) & 020))
  253.         col += 2;
  254.         else
  255.         col += 1;
  256.     }
  257.     if (c != '\0')
  258.     while ((c = *bp++) != '\0') {
  259.         if (AbortCnt < 0) {
  260.         AbortCnt = BufSize;
  261.         if ((InputPending = charp()) != '\0') {
  262.             aborted = 1;
  263.             break;
  264.         }
  265.         }
  266.         if (c == '\t') {
  267.         int nchars = (tabstop - (col % tabstop));
  268.  
  269.         col += nchars;
  270.         if (visspace) {
  271.             {
  272.             if (--n <= 0)
  273.                 break;
  274.             else
  275.                 dosputc((('>')));
  276.             };
  277.             nchars -= 1;
  278.         }
  279.         while (--nchars >= 0) {
  280.             if (--n <= 0)
  281.             break;
  282.             else
  283.             dosputc(((' ')));
  284.         };
  285.         if (n <= 0)
  286.             break;
  287.         } else if (((CharTable[0][c & (0400 - 1)]) & 020)) {
  288.         {
  289.             if (--n <= 0)
  290.             break;
  291.             else
  292.             dosputc((('^')));
  293.         };
  294.         {
  295.             if (--n <= 0)
  296.             break;
  297.             else
  298.             dosputc((((c == '\177') ? '?' : c + '@')));
  299.         };
  300.         col += 2;
  301.         } else {
  302.         if (c == ' ' && visspace)
  303.             c = '_';
  304.  
  305.         if (c == 255)
  306.             c = 1;
  307.         {
  308.             if (--n <= 0)
  309.             break;
  310.             else
  311.             dosputc(((c)));
  312.         };
  313.         col += 1;
  314.         }
  315.     }
  316.     if (n <= 0) {
  317.     if ((*bp == '\0') && (c != '\t') && !((CharTable[0][c & (0400 - 1)]) & 020))
  318.         dosputc((c));
  319.     else
  320.         dosputc(('!'));
  321.     }
  322.     if (cursor > Curline->s_length)
  323.     Curline->s_length = cursor;
  324.     //screen_buffer_flush ();    /* DB */
  325.     return !aborted;        /* Didn't abort */
  326. }
  327.  
  328. void i_set(nline, ncol)
  329. /* register */ int nline, ncol;
  330. {
  331.     Curline = &Screen[nline];
  332.     cursor = Curline->s_line + ncol;
  333.     cursend = &Curline->s_line[CO - 1];
  334.     i_line = nline;
  335.     i_col = ncol;
  336. }
  337.  
  338. /*
  339.  * Insert `num' lines a top, but leave all the lines BELOW `bottom' alone (at
  340.  * least they won't look any different when we are done). This changes the
  341.  * screen array AND does the physical changes.
  342.  */
  343.  
  344. void v_ins_line (num, top, bottom)
  345. int num, top, bottom;
  346. {
  347.     /* register */ int i;
  348.  
  349.     /* Save the screen pointers. */
  350.  
  351.     for (i = 0; i < num && top + i <= bottom; i++)
  352.     Savelines[i] = Screen[bottom - i];
  353.  
  354.     /*
  355.      * Num number of bottom lines will be lost. Copy everything down num
  356.      * number of times.
  357.      */
  358.  
  359.     for (i = bottom; i > top && i - num >= 0; i--)
  360.     Screen[i] = Screen[i - num];
  361.  
  362.     /* Restore the saved ones, making them blank. */
  363.  
  364.     for (i = 0; i < num; i++) {
  365.     Screen[top + i] = Savelines[i];
  366.     clrline(Screen[top + i].s_line, Screen[top + i].s_length);
  367.     Screen[top + i].s_length = Screen[top + i].s_line;
  368.     }
  369.     scr_win((int) -num, (unsigned char) top, 0, (unsigned char) bottom, char_per_line - 1);
  370. }
  371.  
  372. /*
  373.  * Delete `num' lines starting at `top' leaving the lines below `bottom'
  374.  * alone.  This updates the internal image as well as the physical image.
  375.  */
  376.  
  377. void v_del_line(num, top, bottom)
  378. int num, top, bottom;
  379. {
  380.     /* register */ int i, bot;
  381.  
  382.     bot = bottom;
  383.  
  384.     /* Save the lost lines. */
  385.  
  386.     for (i = 0; i < num && top + i <= bottom; i++)
  387.     Savelines[i] = Screen[top + i];
  388.  
  389.     /* Copy everything up num number of lines. */
  390.  
  391.     for (i = top; num + i <= bottom; i++)
  392.     Screen[i] = Screen[i + num];
  393.  
  394.     /* Restore the lost ones, clearing them. */
  395.  
  396.     for (i = 0; i < num; i++) {
  397.     Screen[bottom - i] = Savelines[i];
  398.     clrline(Screen[bot].s_line, Screen[bot].s_length);
  399.     Screen[bot].s_length = Screen[bot].s_line;
  400.     bot -= 1;
  401.     }
  402.     scr_win(num, (unsigned char) top, 0, (unsigned char) bottom, char_per_line - 1);
  403. }
  404.