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

  1. /*
  2. Header:          CUG199;
  3. Title:           Module 4 of ged editor;
  4. Last Updated:    12/02/87;
  5.  
  6. Description:    "PURPOSE: perform find, alter and repeat commands";
  7.  
  8. Keywords:        e, editor, qed, ged, DeSmet, MSDOS;
  9. Filename:        ged4.c;
  10. Warnings:       "O file must be present during link for ged";
  11.  
  12. Authors:         G. Nigel Gilbert, James W. Haefner, and Mel Tearle;
  13. Compilers:       DeSmet 2.51;
  14.  
  15. References:
  16. Endref
  17. */
  18.  
  19. /*
  20. e/qed/ged  screen editor
  21.  
  22. (C) G. Nigel Gilbert, MICROLOGY, 1981 - August-December 1981
  23.  
  24. Modified:  Aug-Dec   1984:  BDS-C 'e'(vers 4.6a) to 'qe' (J.W. Haefner)
  25.            March     1985:  BDS-C 'qe' to DeSmet-C 'qed' (J.W. Haefner)
  26.  
  27.            May       1986:  qed converted to ged         (Mel Tearle)
  28.            August    1987:  ged converted to MSC 4.0     (Mel Tearle)
  29.  
  30. File:      ged4.c
  31.  
  32. Functions: info, findorrep, dofindrep, find
  33.  
  34. */
  35.  
  36. #ifndef  TC
  37. #include "ged.h"
  38. #else
  39. #include "ged.t"
  40. #endif
  41.  
  42. /* quick key services
  43.  */
  44. void info()
  45. {
  46. int  line, xcursor;
  47. char c;
  48.  
  49. xcursor = getcursor();
  50. putmess( "F|ind, |A|lter, |R|eform, |Y|(DEL-EOL), |S|ervices" );
  51.  
  52. while ( (c = getlow()) != 'f'  &&  c != 'a'  &&
  53.                      c != 'r'  &&  c != 'y'  &&
  54.                      c != 's'  &&  c != ESCKEY );
  55.  
  56. if ( c == ESCKEY )  return;
  57.  
  58. switch (c)  {
  59.   case 'f':
  60.     replace = NO;
  61.     findorrep();
  62.     break;
  63.   case 'a':
  64.     replace = YES;
  65.     findorrep();
  66.     break;
  67.   case 'y':
  68.     scr_delete( ( cursorx > SWIDTH ) ? xcursor : cursorx, cursory );
  69.     altered = YES;
  70.     text[charn] = '\0';
  71.     changed = YES;
  72.     break;
  73.   case 's':
  74.     envir();
  75.     show_fkeys();
  76.     break;
  77.   case 'r':
  78.     puttext();
  79.     if ( !text[0] )       /* empty line - skip it */
  80.       moveline(1);
  81.     else  {               /* make from -> to */
  82.       from = to = line = cline;
  83.       if ( cline != lastl )
  84.       do  {
  85.          gettext( ++line );
  86.          to++;
  87.       }  while ( cline <= lastl  &&  text[0] != '\0' );
  88.       else to++;
  89.  
  90.     /* "to" must be 1 more than text to reform
  91.      */
  92.     reform( from, to );
  93.     changed = YES;
  94.     putpage();
  95.     }
  96.   break;
  97.   }
  98. }
  99.  
  100.  
  101. /* find and replace
  102.  */
  103. void findorrep()
  104. {
  105. int  toend, global, i, count;
  106. char c;
  107.  
  108. putmess( "Find|(^]=esc)|? " );
  109.  
  110. global = toend = NO;  nocheck = NO;
  111.  
  112. findir = 1;
  113. count  = 0;
  114.  
  115. c = ( char )( scans( patt, FLIM ) );
  116.  
  117. if ( replace )  {
  118.      if ( c == ESCKEY )  return;
  119.      putmess( "Alter to|(^]=esc)|? " );
  120.      c = ( char ) ( scans( changeto, FLIM ) );
  121. }
  122. else if ( !patt[0] )  return;
  123.  
  124. if ( c == CR )  {
  125.      if ( replace )
  126.           putmess
  127.           ( "B|ack/|G|lobal/|T|o end (start)/|W|ithout asking/number |? " );
  128.      else putmess
  129.           ( "|Search |B|ackwards/number|? - ESC to halt " );
  130.      if ( scans( opts, 5 ) == ESCKEY )   return;
  131.  
  132.      for ( i = 0; ( c = opts[i]); i++ )  {
  133.        switch(tolower(c)) {
  134.          case 'g':
  135.            global = YES;
  136.          case 't':
  137.            toend = YES;
  138.            break;
  139.          case 'b':
  140.            findir = -1;
  141.            break;
  142.          case 'w':
  143.            nocheck = YES;
  144.            break;
  145.          }
  146.        if ( c >= '0' && c <= '9' )  count = count*10+c-'0';
  147.        }
  148.     if ( !replace )  {
  149.          global = NO;
  150.          toend  = NO;
  151.     }
  152.     if ( count == 0 )  {
  153.          if (toend)  count = MAXINT;
  154.          else count = 1;
  155.     }
  156.     if ( global )  {
  157.          findir = 1;
  158.          moveline(1-cline);
  159.          sync(0);
  160.     }
  161.     }
  162.     else count = 1;
  163.     dofindrep(count);
  164. }
  165.  
  166.  
  167. void dofindrep(count)
  168. int count;
  169. {
  170. int cp, i, len;
  171. char c;
  172.  
  173. puttext();
  174. do  {
  175.   count--;
  176.   if ( find() == FAIL )  count = 0;
  177.        else if ( replace )  {
  178.        if ( nocheck ) c = 'y';
  179.       else  {
  180.           scr_curson();
  181.           putmess( "|Replace |{|Y|/|N|}|? " );
  182.           do  {
  183.             scr_xy( REPPOS, 0 );
  184.             for (i = 0; i < 1250; i++);
  185.             resetcursor();
  186.             for (i = 0; i < 1250; i++);
  187.           }
  188.           while ( ( c = testlow() ) != 'y' &&
  189.                     c != 'n' && c != ESCKEY );
  190.       }
  191.       scr_delete( EMPOS, 0 );
  192.       switch(c)  {
  193.         case 'y' :
  194.           if ( strlen(text)+(len=strlen(patt)) >= LLIM )  {
  195.            error( "Line would be too long " );
  196.            return;
  197.           }
  198.           for ( cp = charn; (text[cp] = text[cp+len]); cp++ );
  199.  
  200.           for ( cp = strlen(text), len = strlen(changeto);
  201.                 cp >= charn;
  202.                 cp-- )
  203.                 text[cp+len]=text[cp];
  204.           for ( i = 0; (c=changeto[i]); i++ )  text[charn++] = c;
  205.           altered = YES;
  206.           puttext();
  207.           rewrite( ++cp, cursorx );
  208.           sync(charn);
  209.           changed = YES;
  210.         break;
  211.        case ESCKEY:
  212.          count = 0;
  213.          error( "Search stopped " );
  214.        case 'n' :
  215.          movechar(findir);
  216.          break;
  217.        }
  218.      }
  219.   }
  220.   while(count);
  221.   inbufp = 0;
  222. }
  223.  
  224.  
  225. /* find 'patt', searching backwords ( findir==-1)
  226.  * or forwards (findir==1) from current line
  227.  * return FAIL or YES, (set current line to line containing pattern)
  228.  * no wild cards
  229.  */
  230. int find()
  231. {
  232. int   i, fline, oldcharn, newcharn, interupt;
  233. char  *s, pattch1, *p, *t, *getline();
  234.  
  235. if ( !replace || repeat )  movechar(findir);
  236.  
  237. fline       = cline;
  238. oldcharn    = charn;
  239. interupt    = NO;
  240. pattch1     = patt[0];
  241.  
  242. /* searches forward beginning with current char if not tof
  243.  */
  244. if ( findir == 1 )
  245.      while ( fline <= lastl )  {
  246.        for ( s = getline(fline)+charn; *( p = s ); charn++, s++ )
  247.          if ( *s == pattch1)  {
  248.            for ( t = patt+1, p++; *t && *p == *t; p++, t++ );
  249.            if ( !*t )  goto  foundit;
  250.          }
  251.        chkbuf();
  252.        if ( !inbufp )  {
  253.              fline++;
  254.              charn = 0;
  255.        }
  256.        else
  257.        if ( testkey() == ESCKEY )  {
  258.             inbufp = 0;
  259.             interupt = YES;
  260.             goto  interrupted;
  261.        }
  262.     }
  263. /* searches backwards
  264.  */
  265.     else
  266.     while ( fline >= 1 )  {
  267.       for ( s = getline(fline); charn >= 0; charn-- )
  268.         if ( *( p = &s[charn]) == pattch1 )  {
  269.          for ( t = patt+1, p++; *t && *p == *t; p++, t++ );
  270.          if (!*t)  goto  foundit;
  271.         }
  272.      chkbuf();
  273.      if ( !inbufp )
  274.       charn = strlen( getline(--fline) )-1;
  275.      else
  276.        if ( testkey() == ESCKEY )  {
  277.         inbufp = 0;
  278.         interupt = YES;
  279.         goto  interrupted;
  280.        }
  281.     }
  282. interrupted:
  283.   charn = oldcharn;
  284.   if ( !replace || repeat )
  285.        movechar( -findir );
  286.   if ( interupt )
  287.        error( "Search aborted " );
  288.   else error( "Search fails " );
  289.        return  FAIL;
  290.  
  291. foundit:
  292.   newcharn = charn;
  293.   moveline( fline - cline );
  294.   sync( charn = newcharn );
  295.   return  YES;
  296. }
  297.  
  298.  
  299.  
  300.  
  301.  
  302.