home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 133_01 / e4 < prev    next >
Text File  |  1985-03-10  |  4KB  |  213 lines

  1. /*
  2.     e screen editor
  3.  
  4.     (C) G. Nigel Gilbert, MICROLOGY, 1981
  5.  
  6.     August-December 1981
  7.  
  8.     FILE: e4
  9.  
  10.     FUNCTIONS: findorrep,dofindrep,find
  11.  
  12.     PURPOSE: perform find, alter and repeat commands
  13.  
  14. */
  15.  
  16. #include "e.h"
  17.  
  18. findorrep()
  19. {
  20.     int  toend, global, i, count;
  21.     char c, *p;
  22.  
  23.     putmess("Find? ");
  24.     global=nocheck=toend=nocase=NO;
  25.     findir=1;
  26.     count=0;
  27.     c=scans(patt,FLIM);
  28.     if (replace) {
  29.         if (c == ESCKEY) return;
  30.         putmess("Alter to? ");
  31.         c=scans(changeto,FLIM);
  32.         }
  33.     else if (!patt[0]) return;
  34.     if (c == CR) {
  35.         if (replace)
  36.             putmess("B|ackwards/|G|lobally/|T|o end (start)/|I|gnore case/|W|ithout asking/number|? ");
  37.         else putmess("B|ackwards/|I|gnore case/number of times|? ");
  38.         if (scans(opts,5) == ESCKEY) return;
  39.         for (i=0; (c=opts[i]); i++) {
  40.             switch(tolower(c)) {
  41.             case 'g' :
  42.                 global=YES;
  43.             case 't' :
  44.                 toend=YES;
  45.                 break;
  46.             case 'b' :
  47.                 findir=-1;
  48.                 break;
  49.             case 'w' :
  50.                 nocheck=YES;
  51.                 break;
  52.             case 'i' :
  53.                 nocase=YES;
  54.                 for (p=patt; *p; p++)
  55.                     *p=tolower(*p);
  56.                 break;
  57.                 }
  58.             if (c >= '0' && c <= '9') count=count*10+c-'0';
  59.             }
  60.         if (!replace) {
  61.             global=NO;
  62.             toend=NO;
  63.             }
  64.         if (count == 0) {
  65.             if (toend) count=MAXINT;
  66.             else count=1;
  67.             }
  68.         if (global) {
  69.             findir=1;
  70.             moveline(1-cline);
  71.             sync(0);
  72.             }
  73.         }
  74.     else count=1;
  75.     dofindrep(count);
  76. }
  77.  
  78. dofindrep(count)
  79. int count;
  80. {
  81.     int cp, i, len;
  82.     char c, testlow();
  83.  
  84.     puttext();
  85.     do {
  86.         count--;
  87.         if (find() == FAIL) count=0;
  88.         else if (replace) {
  89.             if (nocheck) c='y';
  90.             else {
  91.                 gotoxy(EMPOS,0);
  92.                 puts("     Replace |{|Y|/|N|}|? ");
  93.                 do {
  94.                     gotoxy(REPPOS,0);
  95.                     for (i=0; i < CURSORWAIT; i++);
  96.                     resetcursor();
  97.                     for (i=0; i < CURSORWAIT; i++);
  98.                     }
  99.                 while ((c=testlow()) != 'y' && c != 'n'
  100.                 && c != ESCKEY);
  101.                 }
  102.             deleteline(EMPOS,0);
  103.             switch(c) {
  104.             case 'y' :
  105.                 if (strlen(text)+(len=strlen(patt)) >= LLIM) {
  106.                     error("Line would be too long"); 
  107.                     return;
  108.                     }
  109.                 for (cp=charn; (text[cp]=text[cp+len]); cp++);
  110.                 for (cp=strlen(text), len=strlen(changeto); 
  111.                     cp >= charn; cp--)
  112.                         text[cp+len]=text[cp];
  113.                 for (i=0; (c=changeto[i]); i++) text[charn++]=c;
  114.                 altered=YES; 
  115.                 puttext();
  116.                 rewrite(++cp,cursorx);
  117.                 sync(charn);
  118.                 break;
  119.             case ESCKEY:
  120.                 count=0;
  121.                 error("Search stopped");
  122.             case 'n' :
  123.                 movechar(findir); 
  124.                 break;
  125.                 }
  126.             }
  127.         } 
  128.     while(count);
  129.     inbufp=0;
  130. }
  131.  
  132. find()    /*find 'patt', searching back (findir==-1) or forwards (1) from
  133.       current line.  Return FAIL or YES, and set current line to
  134.       that containing pattern*/
  135. {
  136.     int fline, oldcharn, newcharn, interupt, linecount, pos;
  137.     char *s, pattch1, *p, *t, *getline(), testkey(), lcline[LLIM];
  138.  
  139.     if (!replace || repeat) movechar(findir);
  140.     fline=cline;  
  141.     oldcharn=charn; 
  142.     interupt=NO;
  143.     linecount= cline%100;
  144.     pattch1=patt[0];
  145.     gotoxy(WAITPOS,0);
  146.     if (findir == 1)
  147.         while (fline <= lastl) {
  148.             if (linecount++ == 100) {
  149.                 linecount=1; 
  150.                 putlineno(fline);
  151.                 gotoxy(WAITPOS,0);
  152.                 if (testkey() == ESCKEY) {
  153.                     interupt=YES;
  154.                     goto interrupted;
  155.                     }
  156.                 }
  157.             s=getline(fline)+charn;
  158.             if (nocase) {
  159.                 for (t=lcline; (*t=tolower(*s & NOPARITY));
  160.                             s++, t++);
  161.                 s=lcline;
  162.                 }
  163.             if ( (pos=index(s,patt)) != FAIL) {
  164.                 charn+=pos;
  165.                 goto foundit;
  166.                 }
  167.             fline++; 
  168.             charn=0;
  169.             }
  170.     else
  171.     while (fline >= 1) {
  172.         if (linecount-- == 0) {
  173.             linecount=99; 
  174.             putlineno(fline);
  175.             gotoxy(WAITPOS,0);
  176.             if (testkey() == ESCKEY) {
  177.                 interupt=YES;
  178.                 goto interrupted;
  179.                 }
  180.             }
  181.         s=getline(fline);
  182.         if (nocase) {
  183.             for (t=lcline; (*t=tolower(*s)); s++, t++);
  184.             s=lcline;
  185.             }
  186.         for (; charn >= 0; charn--)
  187.             if (*(p=&s[charn]) == pattch1) {
  188.                 for (t=patt+1,p++; *t && *p == *t; p++, t++);
  189.                 if (!*t) goto foundit;
  190.                 }
  191.         charn=strlen(getline(--fline))-1;
  192.         }
  193. interrupted:
  194.     charn=oldcharn; 
  195.     if (!replace || repeat) movechar(-findir);
  196.     if (interupt) {
  197.         error("Search aborted");
  198.         putlineno(cline);
  199.         }
  200.     else error("       Search fails");
  201.     return FAIL;
  202.  
  203. foundit:
  204.     newcharn=charn; 
  205.     moveline(fline-cline); 
  206.     sync(charn=newcharn);
  207.     return YES;
  208. }
  209. ; (c=changeto[i]); i++) text[charn++]=c;
  210.                 altered=YES; 
  211.                 puttext();
  212.                 rewrite(++cp,cursorx);
  213.                 sync(