home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / edit / point20 / auxcom.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-04  |  14.1 KB  |  572 lines

  1. #include "pt.h"
  2. #include "io.h"
  3. #include "string.h"
  4. #include "stdlib.h"
  5. #include "direct.h"
  6.  
  7. int pascal
  8. /* XTAG:quitPoint */
  9. quitPoint(fn, noSave)
  10.     int fn, noSave;
  11. {
  12.     extern unsigned char msgBuffer[];
  13.     extern struct window *windowList;
  14.     extern struct window *hiddenList;
  15.     extern struct openFile *files;
  16.     extern int i43lines;
  17. #ifdef OVERLAYS
  18.     extern int overlayState;
  19. #endif
  20.     extern unsigned char startDirectory[];
  21.     extern unsigned char startDrive;
  22.     extern struct openFile *files;
  23.  
  24.     register struct window *w2;
  25.     register int j;
  26.     unsigned char *fileName;
  27.     int i, n;
  28.     int fid;
  29.  
  30.     /* create (or truncate) the last state file */
  31.     setDefaultDrive(startDrive);
  32.     chdir(startDirectory);
  33.     fid = creatls("pt.las", 0);
  34.     if( fid >= 0 ) {
  35.         sprintf(msgBuffer, "%d \r\n", i43lines);
  36.         writels(fid, msgBuffer, strlen(msgBuffer));
  37.     }
  38.     if( !noSave )
  39.         goto noChanges;
  40.     n = 0;
  41.     w2 = windowList;
  42.     while( 1 ) {
  43.         if( w2 == NULL ) {
  44.             /* gone through both windowList and hiddenList? */
  45.             if( n == 1 )
  46.                 break;    /* ifm we are done */
  47.             /* if not do hiddenList */
  48.             n = 1;    /* remember that we already did windowList */
  49.             w2 = hiddenList;
  50.             continue;
  51.         }
  52.         /* see if the file has been edited */
  53.         if( files[w2->fileId].isChanged )
  54.             goto areChanges;
  55.         w2 = w2->nextWindow;
  56.     }
  57.     goto noChanges;
  58. areChanges:
  59.     fileName = getInput(
  60.         "Are you sure you want to discard all changes?  ", "y", 1);
  61.     if( tolower(fileName[0]) != 'y' ) {
  62.         msg("Quit-Discard Edits cancelled", 1);
  63.         return 0;
  64.     }
  65. noChanges:
  66.     n = 0;
  67.     w2 = windowList;
  68.     while( 1 ) {
  69.         if( w2 == NULL ) {
  70.             /* gone through both windowList and hiddenList? */
  71.             if( n == 1 )
  72.                 break;    /* ifm we are done */
  73.             /* if not do hiddenList */
  74.             n = 1;    /* remember that we already did windowList */
  75.             w2 = hiddenList;
  76.             continue;
  77.         }
  78.         if( fn == FQUITASK )
  79.             j = 1;    /* ask about saving */
  80.         else
  81.             j = 0;    /* save without asking */
  82.         if( fn == FQUITNOSAVE )
  83.             j = 2;    /* do not ask or save */
  84.         i = closeWindow(w2, j, 0);
  85.         if( i == -1 ) {
  86.             msg("Window close failed, quit cancelled", 3);
  87.             goto noQuit;
  88.         }
  89.         /* record the state in the last state file */
  90.         if( fid >= 0 ) {
  91.             sprintf(msgBuffer, "%d %d %d %d %d %s \r\n",
  92.                 w2->row1, w2->col1, w2->row2, w2->col2,
  93.                 w2->numTopline, files[w2->fileId].origName);
  94.             writels(fid, msgBuffer, strlen(msgBuffer));
  95.         }
  96.         w2 = w2->nextWindow;
  97.     }
  98.     /* reset the mouse */
  99.     termMouse();
  100.     n = 1;
  101.     goto ret;
  102. noQuit:
  103.     n = 0;
  104. ret:
  105.     if( fid >= 0 )
  106.         closels(fid);
  107.     return n;
  108. }
  109.  
  110. unsigned char * pascal
  111. /* XTAG:getSelection */
  112. getSelection(s)
  113.     unsigned char *s;
  114. {
  115.     extern long selBegin, selEnd;
  116.     extern struct window *selWindow;
  117.  
  118.     register unsigned char *p;
  119.     long cp;
  120.     register int fid;
  121.     
  122.     p = s;
  123.     cp = selBegin;
  124.     fid = selWindow->fileId;
  125.     while( cp <= selEnd ) {
  126.         *p++ =  readChar(fid, cp++);
  127.         if( p-s >= 63 )
  128.             break;
  129.     }
  130.     *p = '\0';
  131.     return s;
  132. }
  133.  
  134. void pascal
  135. /* XTAG:matchChar */
  136. matchChar()
  137. {
  138.     extern struct window *selWindow;
  139.     extern long selBegin, selEnd;
  140.  
  141.     register unsigned char ch;
  142.     long cp, fSize;
  143.     unsigned char ch1, ch2;
  144.     int n, matchDirection, nLines;
  145.  
  146.     cp = selBegin;
  147.     ch = readChar(selWindow->fileId, cp);
  148.     switch( ch ) {
  149.     case '(': ch1 = '('; ch2 = ')'; matchDirection =  1; break;
  150.     case ')': ch1 = ')'; ch2 = '('; matchDirection = -1; break;
  151.     case '[': ch1 = '['; ch2 = ']'; matchDirection =  1; break;
  152.     case ']': ch1 = ']'; ch2 = '['; matchDirection = -1; break;
  153.     case '{': ch1 = '{'; ch2 = '}'; matchDirection =  1; break;
  154.     case '}': ch1 = '}'; ch2 = '{'; matchDirection = -1; break;
  155.     default:  ch1 = ' '; ch2 = ' '; matchDirection = -1; break;
  156.     }
  157.     if( ch1 == ' ' ) {
  158.         msg("Matching for (, ), [, ], {, and } only", 1);
  159.         return;
  160.     }
  161.     n = 1;    /* n==0 ==> we found the matching character */
  162.     nLines = 0;
  163.     fSize = fileSize(selWindow->fileId);
  164.     while( 1 ) {
  165.         cp += matchDirection;
  166.         ch = readChar(selWindow->fileId, cp);
  167.         if( ch == ch1 )
  168.             ++n;
  169.         else if( ch == ch2 )
  170.             --n;
  171.         else if( ch == '\n' )
  172.             ++nLines;
  173.         if( n == 0 || cp == 0 || cp >= fSize )
  174.             break;
  175.     }
  176.     if( n == 0 ) {
  177.         selBegin = selEnd = cp;
  178.         /* put the selection on the third line */
  179.         if( selBegin >= selWindow->posBotline 
  180.          || selBegin < selWindow->posTopline ) {
  181.             /* remember where we came from */
  182.             selWindow->rowLastline = selWindow->numTopline;
  183.             n = 3;
  184.             cp = prevLine(selWindow->fileId, selBegin, &n);
  185.             selWindow->posTopline = cp;
  186.             /* recalculate the line number by letting */
  187.             /* prevLine count as far back as it can */
  188.             n = 30000;
  189.             prevLine(selWindow->fileId, cp, &n);
  190.             selWindow->numTopline = n + 1;
  191.         }
  192.         redrawBox(selWindow->row1, selWindow->col1,
  193.             selWindow->row2, selWindow->col2);
  194.         updateScreen(selWindow->row1, selWindow->row2);
  195.     } else
  196.         msg("No matching character was found.", 1);
  197. }
  198.  
  199. int pascal
  200. /* XTAG:getUnixState */
  201. getUnixState(fileId)
  202.     int fileId;
  203. {
  204.     extern unsigned char msgBuffer[];
  205.     extern unsigned char textBuffer[];
  206.     extern int unixMode;
  207.  
  208.     long cp, limit;
  209.     int fileUnixMode;
  210.  
  211.     /* if unixMode is 0 or 1 then that+1 is the window's unixMode */
  212.     if( unixMode == 0 )
  213.         return 1;
  214.     else if( unixMode == 1 )
  215.         return 3;
  216.     /* otherwise, sample the first 300 characters */
  217.     /* if no CRs are found then assume it is a UNIX file */
  218.     /* if a CR is found, then it is a DOS file */
  219.     cp = 0L;
  220.     limit = fileSize(fileId) - 1;
  221.     if( limit > 300 )
  222.         limit = 300;
  223.     fileUnixMode = 3;
  224.     while( cp < limit ) {
  225.         if( readChar(fileId, cp) == '\r' ) {
  226.             fileUnixMode = 1;
  227.             break;
  228.         }
  229.         ++cp;
  230.     }
  231.     return fileUnixMode;
  232. }
  233.  
  234. int pascal
  235. /* XTAG:indentToShowSelection */
  236. /* CPC 4-26-88 */
  237. indentToShowSelection(selCol)
  238.     int selCol;    /* the column where the first character of */
  239.             /* the selection is */
  240. /* CPC 4-26-88 */
  241. {
  242.     extern unsigned char msgBuffer[];
  243.     extern struct window *selWindow;
  244.     extern long selBegin, selEnd;
  245.  
  246.     register struct window *rselWindow;
  247.     long longIndent;
  248.     int dummy, col, windowWidth, indent;
  249.     int originalIndent;
  250.  
  251.     rselWindow = selWindow;
  252.     originalIndent = rselWindow->indent;
  253.  
  254.     /* find the column the selection starts in */
  255. /* CPC 4-26-88 */
  256.     if( selCol == -1 )
  257.         (void)posToxy(rselWindow, selBegin, &dummy, &col);
  258.     else
  259.         col = selCol;
  260. /* CPC 4-26-88 */
  261.  
  262.     /* posToxy subtracts off the current indent so add it back in */
  263.     /* posToxy adds in rselWindow->col1 so subtract it off */
  264.     col += rselWindow->indent - rselWindow->col1;
  265.     windowWidth = rselWindow->col2 - rselWindow->col1 - 1;
  266.  
  267.     /* if the selection is right of the window, change the indent */
  268.     if( (col > (windowWidth+rselWindow->indent)) ) {
  269.         indent = col - (windowWidth>>1);
  270.         /* figure the column of the end of the selection */
  271.         /*     add window width / 2 to put it in the  */
  272.         /*    middle of the window */
  273.         longIndent = (selEnd - selBegin) + (long)indent;
  274.         if( longIndent < (long)col )
  275.             rselWindow->indent = (int)longIndent;
  276.         else
  277.             rselWindow->indent = indent;
  278.     } else if( (col < rselWindow->indent) ) {
  279.         /* if the selection is left of the window change the indent */
  280.         /* change the indent back to zero if this will still leave */
  281.         /* the beginning of the selection in the left half of the */
  282.         /* window */
  283.         indent = col - (windowWidth>>1);
  284.         if( indent < 0 )
  285.             indent = 0;
  286.         rselWindow->indent = indent;
  287.     }
  288.     /* indicate on return whether you actually changed the indent */
  289.     return (originalIndent != rselWindow->indent);
  290. }
  291.  
  292. void pascal
  293. /* XTAG:doGoSel */
  294. doGoSel(w)
  295.     struct window *w;
  296. {
  297.     extern struct window *selWindow;
  298.     extern long selBegin, selEnd;
  299.     extern int linesOverFind;
  300.     extern int debug;
  301.  
  302.     register struct window *rselWindow;
  303.     register int n;
  304.     int i, fid;
  305.     long cp, toCp;
  306.  
  307.     rselWindow = selWindow;
  308.  
  309.     /* remember where we came from */
  310.     w->rowLastline = w->numTopline;
  311.  
  312.     /* n is the number of lines to move */
  313.     fid = rselWindow->fileId;
  314.     cp = rselWindow->posTopline;
  315.     i = linesOverFind;
  316.     /* find the number of lines in the window */
  317.     i = rselWindow->row2 - rselWindow->row1 - 2;
  318.     if( linesOverFind > i )
  319.         /* if linesOverFind would place it outside the */
  320.         /* window then put it in the middle of the window */
  321.         i >>= 1;
  322.     else
  323.         /* otherwise put it linesOverFind lines down */
  324.         i = linesOverFind;
  325.     toCp = prevLine(fid, selBegin, &i);
  326.     n = rselWindow->numTopline;
  327.     if( cp <= toCp ) {
  328.         while( cp < toCp ) {
  329.             i = 1;
  330.             cp = nextLine(fid, cp, &i);
  331.             ++n;
  332.         }
  333.     } else {    /* cp > toCp */
  334.         while( cp > toCp ) {
  335.             i = 1;
  336.             cp = prevLine(fid, cp, &i);
  337.             --n;
  338.         }
  339.     }
  340.     rselWindow->posTopline = toCp;
  341.     rselWindow->numTopline = n;
  342.     (void)indentToShowSelection(-1);
  343.     doTopWindow(rselWindow, 0);
  344.     redrawWindow(rselWindow);
  345. }
  346.  
  347. void pascal
  348. /* XTAG:doInform */
  349. doInform()
  350. {
  351.     extern unsigned char msgBuffer[];
  352.     extern long selBegin, selEnd;
  353.     extern struct diskBuffer *buffers;
  354.     extern struct openFile *files;
  355.     extern struct changeItem *change;
  356.     extern struct window *windowList;
  357.     extern int nextChange;
  358.     extern struct changeItem scrapBuffer;
  359.     extern struct piece *freePList;
  360.     extern unsigned int piecesLeft;
  361.     extern int nBuffers;
  362.     extern unsigned int bytesLeft;
  363.     extern int nextSpace;
  364.     extern int addHandle;
  365. #ifdef OVERLAYS
  366.     extern unsigned char primaryOverlay[];
  367.     extern unsigned char secondaryOverlay[];
  368.     extern int overlayState;
  369. #endif
  370.     extern int maxFiles;
  371.  
  372.     register int i;
  373.     int j;
  374.     long fs;
  375.     char ch;
  376.     struct piece *pp;
  377.  
  378.     msg("f(ree space), s(election)", 2);
  379.     ch = incon();
  380.  
  381.     switch( ch ) {
  382.     
  383.     case 'f':    /* free space */
  384.         sprintf(msgBuffer,
  385. "Free bytes = %d   Free pieces = %d  Free menu bytes = %d",
  386.             bytesLeft, piecesLeft, MENUSPACE - nextSpace);
  387.         msg(msgBuffer, 1);
  388.         break;
  389.  
  390.     case 's':    /* selection */
  391.         sprintf(msgBuffer,
  392.             "Selection: first char=%ld  last char=%ld",
  393.             selBegin, selEnd);
  394.         msg(msgBuffer, 1);
  395.         break;
  396.  
  397. #ifdef DOINFORMS
  398. #ifdef OVERLAYS
  399.     case 'o':    /* overlays */
  400.         sprintf(msgBuffer, "state=0x%X primary <%s> secondary <%s>",
  401.             overlayState, primaryOverlay, secondaryOverlay);
  402.         msg(msgBuffer, 1); incon();
  403.         break;
  404. #endif
  405.  
  406.     case 'p':
  407.         for(i = 0; i < maxFiles; i++) {
  408.             if( files[i].origHandle == -1 )
  409.                 continue;
  410.             sprintf(msgBuffer,
  411. "«%s»(%d): handle=%d piece=%x lo=%ld hi=%ld size=%ld",
  412.               files[i].origName, i, files[i].origHandle,
  413.               files[i].logPiece, files[i].loLogPiece,
  414.               files[i].hiLogPiece, files[i].fileSize );
  415.             msg(msgBuffer, 1); incon();
  416.             sprintf(msgBuffer,
  417. "«%s»(%d): buffer cache: lo=%ld hi=%ld addr=%X %X",
  418.               files[i].origName, i, files[i].loLogBuffer,
  419.               files[i].hiLogBuffer, files[i].logBufOffset,
  420.               files[i].logBufSegment);
  421.             msg(msgBuffer, 1); incon();
  422.             fs = 0;
  423.             j = 0;
  424.             for(pp=files[i].pieceList;pp!=NULL;pp=pp->nextPiece){
  425.                 sprintf(msgBuffer,
  426.          "        piece[%d] @%x  position=%ld  length=%ld  file=%d",
  427.                   j++, pp, pp->position, pp->length, pp->file);
  428.                 msg(msgBuffer, 1); incon();
  429.                 fs += pp->length;
  430.             }
  431.             sprintf(msgBuffer,
  432.             "    #pieces=%d  fileSize=%ld  pieceTotals=%ld",
  433.             j, files[i].fileSize, fs);
  434.             msg(msgBuffer, 1);
  435.             if( incon() == 'z' )
  436.                 break;
  437.         }
  438.         break;
  439.  
  440.     case 'i':    /* information */
  441.         sprintf(msgBuffer, "buffers = %d", nBuffers);
  442.         msg(msgBuffer, 1);
  443.         break;
  444.  
  445.     case 'h':
  446.         for(i = 0; i <= nextChange; i++) {
  447.             sprintf(msgBuffer,
  448.         "change[%d]: type=%d fileId=%d position=%ld length=%ld",
  449.                 i, change[i].type, change[i].fileId,
  450.                 change[i].position, change[i].length);
  451.             msg(msgBuffer, 1); incon();
  452.         }
  453.         break;
  454.  
  455.     case 'b':
  456.         msg("***********IN BUFFERS************", 1);
  457.         sprintf(msgBuffer, "addHandle=%d", addHandle);
  458.         msg(msgBuffer, 1); incon();
  459.         for(i = 0; i < nBuffers; i++) {
  460.             if( buffers[i].handle == -1 )
  461.                 continue;
  462.             sprintf(msgBuffer,
  463. "buffer %d: header=%x  address=%lx  handle=%d  block=%ld",
  464.               i, &buffers[i], buffers[i].bufferAddress,
  465.               buffers[i].handle, buffers[i].blockNumber);
  466.             msg(msgBuffer, 1); incon();
  467.         }
  468.         break;
  469.  
  470.     case 'c':
  471.         msg("********** CHANGES ***********", 1); incon();
  472.         for(i = 0; i < NHISTORY; i++) {
  473.             pp = change[i].firstPiece;
  474.             if( pp == NULL )
  475.                 continue;
  476.             sprintf(msgBuffer, "change[%d]:", i);
  477.             msg(msgBuffer, 1); incon();
  478.             for( ; pp != NULL; pp = pp->nextPiece ) {
  479.                 sprintf(msgBuffer,
  480.          "        piece[%d] @%x  position=%ld  length=%ld  file=%d",
  481.                   j++, pp, pp->position, pp->length, pp->file);
  482.                 msg(msgBuffer, 1); incon();
  483.             }
  484.             sprintf(msgBuffer,"#pieces=%d", j);
  485.             msg(msgBuffer, 1); incon();
  486.         }
  487.         msg("********* SCRAPBUFFER *********", 1); incon();
  488.         sprintf(msgBuffer, "    type=%d", scrapBuffer.type);
  489.         msg(msgBuffer, 1); incon();
  490.         j = 0;
  491.         for(pp=scrapBuffer.firstPiece;pp!=NULL;pp=pp->nextPiece){
  492.             sprintf(msgBuffer,
  493. "        piece[%d] @%x  position=%ld  length=%ld  file=%d",
  494.               j++, pp, pp->position, pp->length, pp->file);
  495.             msg(msgBuffer, 1); incon();
  496.         }
  497.         j = 0;
  498.         pp = freePList;
  499.         while( pp != NULL ) {
  500.             ++j;
  501.             pp = pp->nextPiece;
  502.         }
  503.         sprintf(msgBuffer,"Number of free pieces = %d", j);
  504.         msg(msgBuffer, 1); incon();
  505.         break;
  506. #endif
  507.     default:
  508.         break;
  509.     }
  510. }
  511.  
  512. void pascal
  513. /* XTAG:doGoto */
  514. doGoto(w, i)
  515.     register struct window *w;
  516.     int i;
  517. {
  518.     extern unsigned char msgBuffer[];
  519.  
  520.     int n, fid;
  521.     unsigned char ch, *fileName;
  522.  
  523.     if( i == -1 ) {
  524.         fileName = getInput("Line number to go to: ", "", 0);
  525.         if( fileName == NULL ) {
  526.             msg("Go to line number cancelled", 1);
  527.             return;
  528.         }
  529.         /* skip past any non-numeric characters at the front */
  530.         while( 1 ) {
  531.             ch = *fileName;
  532.             if( ch == '\0' || ('0'<=ch && ch<='9') )
  533.                 break;
  534.             ++fileName;
  535.         }
  536.         i = atoi(fileName);
  537.     }
  538.  
  539.     /* remember where we came from */
  540.     w->rowLastline = w->numTopline;
  541.  
  542.     /* n is the number of lines to move */
  543.     n = i - w->numTopline;
  544.     fid = w->fileId;
  545.     if( n > 0 ) {
  546.         w->posTopline = nextLine(fid, w->posTopline, &n);
  547.         w->numTopline += n;
  548.         w->posBotline = nextLine(fid, w->posBotline, &n);
  549.         w->numBotline += n;
  550.     } else {    /* n < 0 */
  551.         n = -n;
  552.         w->posTopline = prevLine(fid, w->posTopline, &n);
  553.         w->numTopline -= n;
  554.         w->posBotline = prevLine(fid, w->posBotline, &n);
  555.         w->numBotline -= n;
  556.     }
  557.     w->indent = 0;
  558.     redrawWindow(w);
  559. }
  560.  
  561. void pascal
  562. /* XTAG:initKeymaps */
  563. initKeymaps()
  564. {
  565.     extern int asciiMap[];
  566.     register int i;
  567.  
  568.     for(i = 0; i < 132; i++)
  569.         asciiMap[i] = FCHARACTER;
  570.     asciiMap[27] = FCANCEL;    /* ESCape */
  571. }
  572.