This commit was manufactured by cvs2svn to create tag 'FreeBSD-release/1.0'.
[unix-history] / usr.bin / elvis / move2.c
index b664153..2c768cd 100644 (file)
 
 #include "config.h"
 #include "vi.h"
 
 #include "config.h"
 #include "vi.h"
-#include "regexp.h"
+#ifdef REGEX
+# include <regex.h>
+#else
+# include "regexp.h"
+#endif
 
 extern long    atol();
 
 
 extern long    atol();
 
+#ifdef REGEX
+static regex_t *re = NULL;     /* compiled version of the pattern to search for */
+#else
 static regexp  *re;    /* compiled version of the pattern to search for */
 static regexp  *re;    /* compiled version of the pattern to search for */
+#endif
 static         prevsf; /* boolean: previous search direction was forward? */
 
 #ifndef NO_EXTENSIONS
 static         prevsf; /* boolean: previous search direction was forward? */
 
 #ifndef NO_EXTENSIONS
@@ -44,38 +52,51 @@ MARK m_wsrch(word, m, cnt)
 }
 #endif
 
 }
 #endif
 
-MARK   m_nsrch(m)
+MARK   m_nsrch(m, cnt, cmd)
        MARK    m;      /* where to start searching */
        MARK    m;      /* where to start searching */
+       long    cnt;    /* number of searches to do */
+       int     cmd;    /* command character -- 'n' or 'N' */
 {
 {
-       if (prevsf)
-       {
-               m = m_fsrch(m, (char *)0);
-               prevsf = TRUE;
-       }
-       else
-       {
-               m = m_bsrch(m, (char *)0);
-               prevsf = FALSE;
-       }
-       return m;
-}
+       int     oldprevsf; /* original value of prevsf, so we can fix any changes */
 
 
-MARK   m_Nsrch(m)
-       MARK    m;      /* where to start searching */
-{
-       if (prevsf)
+       DEFAULT(1L);
+
+       /* clear the bottom line.  In particular, we want to loose any
+        * "(wrapped)" notice.
+        */
+       move(LINES - 1, 0);
+       clrtoeol();
+
+       /* if 'N' command, then invert the "prevsf" variable */
+       oldprevsf = prevsf;
+       if (cmd == 'N')
        {
        {
-               m = m_bsrch(m, (char *)0);
-               prevsf = TRUE;
+               prevsf = !prevsf;
        }
        }
-       else
+
+       /* search forward if prevsf -- i.e., if previous search was forward */
+       while (--cnt >= 0L && m != MARK_UNSET)
        {
        {
-               m = m_fsrch(m, (char *)0);
-               prevsf = FALSE;
+               if (prevsf)
+               {
+                       m = m_fsrch(m, (char *)0);
+               }
+               else
+               {
+                       m = m_bsrch(m, (char *)0);
+               }
        }
        }
+
+       /* restore the old value of prevsf -- if cmd=='N' then it was inverted,
+        * and the m_fsrch() and m_bsrch() functions force it to a (possibly
+        * incorrect) value.  The value of prevsf isn't supposed to be changed
+        * at all here!
+        */
+       prevsf = oldprevsf;
        return m;
 }
 
        return m;
 }
 
+
 MARK   m_fsrch(m, ptrn)
        MARK    m;      /* where to start searching */
        char    *ptrn;  /* pattern to search for */
 MARK   m_fsrch(m, ptrn)
        MARK    m;      /* where to start searching */
        char    *ptrn;  /* pattern to search for */
@@ -84,6 +105,11 @@ MARK        m_fsrch(m, ptrn)
        char    *line;  /* text of line to be searched */
        int     wrapped;/* boolean: has our search wrapped yet? */
        int     pos;    /* where we are in the line */
        char    *line;  /* text of line to be searched */
        int     wrapped;/* boolean: has our search wrapped yet? */
        int     pos;    /* where we are in the line */
+#ifdef REGEX
+       regex_t *optpat();
+       regmatch_t rm[SE_MAX];
+       int     n;
+#endif
 #ifndef CRUNCH
        long    delta = INFINITY;/* line offset, for things like "/foo/+1" */
 #endif
 #ifndef CRUNCH
        long    delta = INFINITY;/* line offset, for things like "/foo/+1" */
 #endif
@@ -103,11 +129,17 @@ MARK      m_fsrch(m, ptrn)
 #endif
                ptrn++;
 
 #endif
                ptrn++;
 
+
+#ifdef REGEX
+               /* XXX where to free re? */
+               re = optpat(ptrn);
+#else
                /* free the previous pattern */
                /* free the previous pattern */
-               if (re) free(re);
+               if (re) _free_(re);
 
                /* compile the pattern */
                re = regcomp(ptrn);
 
                /* compile the pattern */
                re = regcomp(ptrn);
+#endif
                if (!re)
                {
                        return MARK_UNSET;
                if (!re)
                {
                        return MARK_UNSET;
@@ -156,7 +188,11 @@ MARK       m_fsrch(m, ptrn)
                line = fetchline(l);
 
                /* check this line */
                line = fetchline(l);
 
                /* check this line */
+#ifdef REGEX
+               if (!regexec(re, &line[pos], SE_MAX, rm, (pos == 0) ? 0 : REG_NOTBOL))
+#else
                if (regexec(re, &line[pos], (pos == 0)))
                if (regexec(re, &line[pos], (pos == 0)))
+#endif
                {
                        /* match! */
                        if (wrapped && *o_warn)
                {
                        /* match! */
                        if (wrapped && *o_warn)
@@ -174,7 +210,11 @@ MARK       m_fsrch(m, ptrn)
                                return MARK_AT_LINE(l);
                        }
 #endif
                                return MARK_AT_LINE(l);
                        }
 #endif
+#ifdef REGEX
+                       return MARK_AT_LINE(l) + pos + rm[0].rm_so;
+#else
                        return MARK_AT_LINE(l) + (int)(re->startp[0] - line);
                        return MARK_AT_LINE(l) + (int)(re->startp[0] - line);
+#endif
                }
                pos = 0;
        }
                }
                pos = 0;
        }
@@ -194,6 +234,11 @@ MARK       m_bsrch(m, ptrn)
        int     pos;    /* last acceptable idx for a match on this line */
        int     last;   /* remembered idx of the last acceptable match on this line */
        int     try;    /* an idx at which we strat searching for another match */
        int     pos;    /* last acceptable idx for a match on this line */
        int     last;   /* remembered idx of the last acceptable match on this line */
        int     try;    /* an idx at which we strat searching for another match */
+#ifdef REGEX
+       regex_t *optpat();
+       regmatch_t rm[SE_MAX];
+       int     n;
+#endif
 #ifndef CRUNCH
        long    delta = INFINITY;/* line offset, for things like "/foo/+1" */
 #endif
 #ifndef CRUNCH
        long    delta = INFINITY;/* line offset, for things like "/foo/+1" */
 #endif
@@ -213,11 +258,16 @@ MARK      m_bsrch(m, ptrn)
 #endif
                ptrn++;
 
 #endif
                ptrn++;
 
+#ifdef REGEX
+               /* XXX where to free re? */
+               re = optpat(ptrn);
+#else
                /* free the previous pattern, if any */
                /* free the previous pattern, if any */
-               if (re) free(re);
+               if (re) _free_(re);
 
                /* compile the pattern */
                re = regcomp(ptrn);
 
                /* compile the pattern */
                re = regcomp(ptrn);
+#endif
                if (!re)
                {
                        return MARK_UNSET;
                if (!re)
                {
                        return MARK_UNSET;
@@ -253,16 +303,31 @@ MARK      m_bsrch(m, ptrn)
                line = fetchline(l);
 
                /* check this line */
                line = fetchline(l);
 
                /* check this line */
+#ifdef REGEX
+               if (!regexec(re, line, SE_MAX, rm, 0) && rm[0].rm_so < pos)
+#else
                if (regexec(re, line, 1) && (int)(re->startp[0] - line) < pos)
                if (regexec(re, line, 1) && (int)(re->startp[0] - line) < pos)
+#endif
                {
                {
+                       try = 0;
                        /* match!  now find the last acceptable one in this line */
                        do
                        {
                        /* match!  now find the last acceptable one in this line */
                        do
                        {
+#ifdef REGEX
+                               last = try + rm[0].rm_so;
+                               try += rm[0].rm_eo;
+#else
                                last = (int)(re->startp[0] - line);
                                try = (int)(re->endp[0] - line);
                                last = (int)(re->startp[0] - line);
                                try = (int)(re->endp[0] - line);
+#endif
                        } while (try > 0
                        } while (try > 0
+#ifdef REGEX
+                                && !regexec(re, &line[try], SE_MAX, rm, REG_NOTBOL)
+                                && try + rm[0].rm_so < pos);
+#else
                                 && regexec(re, &line[try], FALSE)
                                 && (int)(re->startp[0] - line) < pos);
                                 && regexec(re, &line[try], FALSE)
                                 && (int)(re->startp[0] - line) < pos);
+#endif
 
                        if (wrapped && *o_warn)
                                msg("(wrapped)");
 
                        if (wrapped && *o_warn)
                                msg("(wrapped)");