home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 1 / ARM_CLUB_CD.iso / contents / apps / program / d / elvis / Source / c / move2 < prev    next >
Encoding:
Text File  |  1989-12-31  |  2.8 KB  |  173 lines

  1. /* move2.c */
  2.  
  3. /* Author:
  4.  *    Steve Kirkendall
  5.  *    16820 SW Tallac Way
  6.  *    Beaverton, OR 97006
  7.  *    kirkenda@jove.cs.pdx.edu, or ...uunet!tektronix!psueea!jove!kirkenda
  8.  */
  9.  
  10.  
  11. /* This function contains the mvoement functions that perform RE searching */
  12.  
  13. #include "vi.h"
  14. #include "regexp.h"
  15.  
  16. static regexp    *re;    /* compiled version of the pattern to search for */
  17. static        prevsf;    /* boolean: previous search direction was forward? */
  18.  
  19. MARK    movensrch(m)
  20.     MARK    m;    /* where to start searching */
  21. {
  22.     if (prevsf)
  23.     {
  24.         m = movefsrch(m, "");
  25.         prevsf = TRUE;
  26.     }
  27.     else
  28.     {
  29.         m = movebsrch(m, "");
  30.         prevsf = FALSE;
  31.     }
  32.     return m;
  33. }
  34.  
  35. MARK    moveNsrch(m)
  36.     MARK    m;    /* where to start searching */
  37. {
  38.     if (prevsf)
  39.     {
  40.         m = movebsrch(m, "");
  41.         prevsf = TRUE;
  42.     }
  43.     else
  44.     {
  45.         m = movefsrch(m, "");
  46.         prevsf = FALSE;
  47.     }
  48.     return m;
  49. }
  50.  
  51. MARK    movefsrch(m, ptrn)
  52.     MARK    m;    /* where to start searching */
  53.     char    *ptrn;    /* pattern to search for */
  54. {
  55.     long    l;    /* line# of line to be searched */
  56.     char    *line;    /* text of line to be searched */
  57.  
  58.     /* remember: "previous search was forward" */
  59.     prevsf = TRUE;
  60.  
  61.     if (ptrn && *ptrn)
  62.     {
  63.         /* free the previous pattern */
  64.         if (re) free(re);
  65.  
  66.         /* compile the pattern */
  67.         re = regcomp(ptrn);
  68.         if (!re)
  69.         {
  70.             return MARK_UNSET;
  71.         }
  72.     }
  73.     else if (!re)
  74.     {
  75.         msg("No previous expression");
  76.         return MARK_UNSET;
  77.     }
  78.  
  79.     /* search forward for the pattern */
  80.     for (l = markline(m) + 1; l != markline(m); l++)
  81.     {
  82.         /* wrap search */
  83.         if (l > nlines)
  84.         {
  85.             if (*o_wrapscan)
  86.             {
  87.                 l = 0;
  88.                 continue;
  89.             }
  90.             else
  91.             {
  92.                 break;
  93.             }
  94.         }
  95.  
  96.         /* get this line */
  97.         line = fetchline(l);
  98.  
  99.         /* check this line */
  100.         if (regexec(re, line, 1))
  101.         {
  102.             /* match! */
  103.             return MARK_AT_LINE(l) + (re->startp[0] - line);
  104.         }
  105.     }
  106.  
  107.     /* not found */
  108.     msg(*o_wrapscan ? "Not found" : "Hit bottom without finding RE");
  109.     return MARK_UNSET;
  110. }
  111.  
  112. MARK    movebsrch(m, ptrn)
  113.     MARK    m;    /* where to start searching */
  114.     char    *ptrn;    /* pattern to search for */
  115. {
  116.     long    l;    /* line# of line to be searched */
  117.     char    *line;    /* text of line to be searched */
  118.  
  119.     /* remember: "previous search was not forward" */
  120.     prevsf = FALSE;
  121.  
  122.     if (ptrn && *ptrn)
  123.     {
  124.         /* free the previous pattern, if any */
  125.         if (re) free(re);
  126.  
  127.         /* compile the pattern */
  128.         re = regcomp(ptrn);
  129.         if (!re)
  130.         {
  131.             return MARK_UNSET;
  132.         }
  133.     }
  134.     else if (!re)
  135.     {
  136.         msg("No previous expression");
  137.         return MARK_UNSET;
  138.     }
  139.  
  140.     /* search backward for the pattern */
  141.     for (l = markline(m) - 1; l != markline(m); l--)
  142.     {
  143.         /* wrap search */
  144.         if (l < 1)
  145.         {
  146.             if (*o_wrapscan)
  147.             {
  148.                 l = nlines + 1;
  149.                 continue;
  150.             }
  151.             else
  152.             {
  153.                 break;
  154.             }
  155.         }
  156.  
  157.         /* get this line */
  158.         line = fetchline(l);
  159.  
  160.         /* check this line */
  161.         if (regexec(re, line, 1))
  162.         {
  163.             /* match! */
  164.             return MARK_AT_LINE(l) + (re->startp[0] - line);
  165.         }
  166.     }
  167.  
  168.     /* not found */
  169.     msg(*o_wrapscan ? "Not found" : "Hit top without finding RE");
  170.     return MARK_UNSET;
  171. }
  172.  
  173.