home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 157_01 / qed4 < prev    next >
Text File  |  1987-10-12  |  7KB  |  328 lines

  1. /*  VERSION 0004  (DATE: 31/03/87)  (TIME: 17:20)  */
  2. /*
  3.     e (qed) screen editor
  4.  
  5.     (C) G. Nigel Gilbert, MICROLOGY, 1981
  6.  
  7.     August-December 1981
  8.  
  9.     Modified: Aug-Dec    1984:    BDS-C 'e'(vers 4.6a) to 'qe' (J.W. Haefner)
  10.               March        1985:    BDS-C 'qe' to DeSmet-C 'qed' (J.W. Haefner)
  11.  
  12.     FILE: qed4
  13.  
  14.     FUNCTIONS: info,findorrep,dofindrep,find
  15.  
  16.     PURPOSE: perform find, alter and repeat commands
  17.  
  18. */
  19.  
  20. #include "qed.h"
  21.  
  22. info()
  23. {
  24.     char c;
  25.     int from,to,line;    /*non-empty lines to reform*/
  26.     char bakname[FILELEN];
  27.     
  28. #if (WWRAP)
  29.     if (expert) xprtmess("F/A/P/Y/K/L/O/B ? ");
  30.     else
  31.        putmess("F|ind |A|lter |P|aragraph |Y|(DEL-EOL) |K|ontext |L|ine:col pos |O|nscrn help |B|ackup");
  32.     while ((c = getlow()) != 'a' && c != 'f' && c != 'p' && c != 'k' &&
  33.             c!='l' && c!='o' && c != 'y' && c!='b' && c != ESCKEY);
  34. #else    /*no word wrap*/
  35.     if (expert) xprtmess("F/A/Y/K/L/O/B ? ");
  36.     else
  37.        putmess("F|ind |A|lter |Y|(DEL-EOL) |K|ontext |L|ine:col pos |O|nscrn help |B|ackup");
  38.     while ((c = getlow()) != 'a' && c != 'f' && c != 'k' &&
  39.           c!='l' && c!='o' && c != 'y' && c!='b' && c != ESCKEY);
  40. #endif   /*wrap*/
  41.  
  42. if (c == ESCKEY) goto clear;
  43. switch (c)  {
  44.     case 'f':
  45.         replace = NO;
  46.         findorrep();
  47.         break;
  48.     case 'a':
  49.         replace = YES;
  50.         findorrep();
  51.         break;
  52.     case 'b':
  53.         strcpy(bakname,bakpath);
  54.         strcat(bakname,&justfile[2]);            /*skip drive and colon*/
  55.           /*force textfile read */
  56.         if((!goteof) || (lastl==UNKNOWN)) loc(UNKNOWN,0);
  57.                 /*leave error message if write fail*/
  58.         if (writefile(1,lastl,bakname,bakname,YES)==FAIL) return NO;
  59.         break;
  60. #if WWRAP
  61.     case 'p':
  62.         puttext();
  63.         if (!text[0]) moveline(1);
  64.         else  {
  65.             from=to=line=cline;
  66.             if (cline!=lastl)
  67.                 do {
  68.                     gettext(++line);
  69.                     to++;
  70.                 } while     ((cline<=lastl) &&
  71.                     (c=text[0]) && (!((c==' ')||(c=='\t'))) );
  72.             else to++;            /*"to" must be 1 more than text to refrm*/
  73.             reform(from,to);
  74.             changed=YES;
  75.             putpage();            /* this could be smarter */
  76.             }
  77.         break;
  78. #endif   /*word wrap*/
  79.     case 'y':
  80.         strcpy(textb,&text[charn]);
  81.         deleteline((cursorx-offset+(offset ? 1 : 0)),cursory);
  82.         altered=changed=YES;
  83.         text[charn] = '\0';
  84.         break;
  85.     case 'k':
  86.         envir();
  87.         break;
  88.     case 'l':
  89.         displaypos = !displaypos;
  90.         deleteline(LNPOS,0);
  91.         putlineno(cline);
  92.         return;
  93.     case 'o':
  94.         if (helpon) {
  95.             helpon = NO;
  96.             unmess();
  97.             }
  98.         else {
  99.             helpon=YES;
  100.             dohelp();
  101.             }
  102.         break;
  103.     }
  104. clear:
  105.     if (expert) xmessoff();
  106. }
  107.  
  108. findorrep()
  109. {
  110.     int  toend, global, i, count, showalt;
  111.     char c;
  112.  
  113.     if (expert) {
  114.         deleteline(0,0);
  115.         putstr("Find|:(^]=esc)|? ");
  116.         }
  117.     else putmess("Find|(^]=ESC)|? ");
  118.     global=nocheck=toend=showalt=NO;
  119.     findir=1; 
  120.     count=0;
  121.     c=scans(patt,FLIM);
  122.     if (replace) {
  123.         if (expert) {
  124.             if (c==ESCKEY) goto clrfind;
  125.             deleteline(0,0);
  126.             putstr("Alter|:(^]=esc)|? ");
  127.             }
  128.         else putmess("Alter to|(^]=ESC)|? ");
  129.         c=scans(changeto,FLIM);
  130.         }
  131.     else if (!patt[0]) goto clrfind;
  132.     if (c == CR) {
  133.         if (replace) {
  134.             if (expert) xprtmess("B/G/T/W/V/n ? ");
  135.             else
  136.               putmess("B|ack/|G|lobal/|T|o end (start)/|W|ithout asking/|V|erbose/number |? ");
  137.             }
  138.         else
  139.             if (expert) xprtmess("B|kwrds/n|? ");
  140.             else putmess("|Search |B|ackwards/number|? ");
  141.         if (scans(opts,5) == ESCKEY) goto clrfind;
  142.         for (i=0; (c=opts[i]); i++) {
  143.             switch(tolower(c)) {
  144.             case 'g' : 
  145.                 global=YES;
  146.             case 't' : 
  147.                 toend=YES;
  148.                 break;
  149.             case 'b' : 
  150.                 findir=-1; 
  151.                 break;
  152.             case 'w' : 
  153.                 nocheck=YES; 
  154.                 break;
  155.             case 'v' :
  156.                 showalt=YES;
  157.                 break;
  158.                 }
  159.             if (c >= '0' && c <= '9') count=count*10+c-'0';
  160.             }
  161.         if (!replace) {
  162.             global=NO; 
  163.             toend=NO;
  164.             }
  165.         if (count == 0) {
  166.             if (toend) count=MAXINT;
  167.             else count=1;
  168.             }
  169.         if (global) {
  170.             findir=1;
  171.             moveline(1-cline);
  172.             sync(0);
  173.             }
  174.         if (!nocheck && !showalt) showalt=YES;  /*check user input*/
  175.         }
  176.     else count=1;
  177.     dofindrep(count,showalt);
  178. clrfind:
  179.     if (expert) {
  180.         deleteline(0,0);
  181.         putstatusline();
  182.         resetcursor();
  183.     }
  184. }
  185.  
  186. dofindrep(count,verbose)
  187. int count,verbose;
  188. {
  189.     int cp, i, len;
  190.     char c;
  191.  
  192.     puttext();
  193.     if (count==1)verbose=YES;        /*force verbose for repeat*/
  194.     do {
  195.         count--;
  196.         if (find(verbose) == FAIL) count=0;
  197.         else if (replace) {
  198.             if (nocheck) {
  199.                 c='y';
  200.                 {
  201.                 if (!verbose)  gotoxy(EMPOS,0);
  202.                 }
  203.             }
  204.             else {
  205.                 gotoxy(EMPOS,0);
  206.                 putstr("     Replace |{|Y|/|N|}|? ");
  207.                 do {
  208.                     gotoxy(REPPOS,0);
  209.                     for (i=0; i < 850; i++);
  210.                     resetcursor();
  211.                     for (i=0; i < 850; i++);
  212.                     } 
  213.                 while ((c=testlow()) != 'y' && c != 'n'
  214.                 && c != ESCKEY);
  215.                 }
  216.             deleteline(EMPOS,0);
  217.             switch(c) {
  218.             case 'y' :    
  219.                 if (strlen(text)+(len=strlen(patt)) >= LLIM) {
  220.                     error("Line would be too long"); 
  221.                     return;
  222.                     }
  223.                 for (cp=charn; (text[cp]=text[cp+len]); cp++);
  224.                 for (cp=strlen(text), len=strlen(changeto); 
  225.                     cp >= charn; cp--)
  226.                         text[cp+len]=text[cp];
  227.                 for (i=0; (c=changeto[i]); i++) text[charn++]=c;
  228.                 altered=YES; 
  229.                 puttext();
  230.                 if (verbose) {
  231.                     rewrite(++cp,cursorx);
  232.                     sync(charn);
  233.                 }
  234.                 changed=YES;
  235.                 break;
  236.             case ESCKEY:
  237.                 count=0;
  238.                 error("Search stopped");
  239.             case 'n' :
  240.                 movechar(findir); 
  241.                 break;
  242.                 }
  243.             }
  244.         } 
  245.     while(count);
  246.     if (!verbose) putpage();
  247.     inbufp=0;
  248. }
  249.  
  250. find(verbose)    /*find 'patt', searching back (findir==-1) or forwards (1) from
  251. int verbose;      current line.  Return FAIL or YES, and set current line to
  252.                     that containing pattern*/
  253. {
  254.     int fline, oldcharn, newcharn, interupt, linecount, pos;
  255.     char *s, pattch1, *p, *t, *getline();
  256.  
  257.     if (!replace || repeat) movechar(findir);
  258.     fline=cline;  
  259.     oldcharn=charn; 
  260.     interupt=NO;
  261.     linecount= cline%100;
  262.     pattch1=patt[0];
  263.     gotoxy(WAITPOS,0);
  264.     if (findir == 1)
  265.         while (fline <= lastl) {
  266.             if (linecount++ == 100) {
  267.                 linecount=1; 
  268.                 putlineno(fline);
  269.                 gotoxy(WAITPOS,0);
  270.                 if (testkey() == ESCKEY) {
  271.                     interupt=YES;
  272.                     goto interrupted;
  273.                     }
  274.                 }
  275.             for (s=getline(fline)+charn; *(p=s); charn++,s++)
  276.                 if (*s == pattch1) {
  277.                     for (t=patt+1, p++; *t && *p == *t; p++, t++);
  278.                     if (!*t) goto foundit;
  279.                     }
  280. /*            if ( (pos=index(getline(fline)+charn,patt)) != NO) {
  281.                 charn+=pos;
  282.                 goto foundit;
  283.                 }
  284. */
  285.             fline++; 
  286.             charn=0;
  287.             }
  288.     else
  289.     while (fline >= 1) {
  290.         if (linecount-- == 0) {
  291.             linecount=99; 
  292.             putlineno(fline);
  293.             gotoxy(WAITPOS,0);
  294.             if (testkey() == ESCKEY) {
  295.                 interupt=YES;
  296.                 goto interrupted;
  297.                 }
  298.             }
  299.         for (s=getline(fline); charn >= 0; charn--)
  300.             if (*(p=&s[charn]) == pattch1) {
  301.                 for (t=patt+1,p++; *t && *p == *t; p++, t++);
  302.                 if (!*t) goto foundit;
  303.                 }
  304.         charn=strlen(getline(--fline))-1;
  305.         }
  306. interrupted:
  307.     charn=oldcharn; 
  308.     if (!replace || repeat) movechar(-findir);
  309.     if (interupt) {
  310.         error("Search aborted");
  311.         putlineno(cline);
  312.         }
  313.     else error("       Search fails");
  314.     return FAIL;
  315.  
  316. foundit:
  317.     newcharn=charn;
  318.     if (verbose) {
  319.         moveline(fline-cline);
  320.         sync(charn=newcharn);
  321.     }
  322.     else {
  323.         puttext();
  324.         gettext(fline);
  325.     }
  326.     return YES;
  327. }
  328.