[BACK]Return to search.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mg

Diff for /src/usr.bin/mg/search.c between version 1.1 and 1.2

version 1.1, 2000/02/25 19:08:50 version 1.2, 2000/04/13 06:12:17
Line 13 
Line 13 
 #include        "macro.h"  #include        "macro.h"
 #endif  #endif
   
 #define SRCH_BEGIN      (0)                     /* Search sub-codes.    */  #define SRCH_BEGIN      (0)     /* Search sub-codes.     */
 #define SRCH_FORW       (-1)  #define SRCH_FORW       (-1)
 #define SRCH_BACK       (-2)  #define SRCH_BACK       (-2)
 #define SRCH_NOPR       (-3)  #define SRCH_NOPR       (-3)
 #define SRCH_ACCM       (-4)  #define SRCH_ACCM       (-4)
 #define SRCH_MARK       (-5)  #define SRCH_MARK       (-5)
   
 typedef struct  {  typedef struct {
         int     s_code;          int             s_code;
         LINE    *s_dotp;          LINE           *s_dotp;
         int     s_doto;          int             s_doto;
 }       SRCHCOM;  }               SRCHCOM;
   
 static  SRCHCOM cmds[NSRCH];  static SRCHCOM  cmds[NSRCH];
 static  int     cip;  static int      cip;
   
 int     srch_lastdir = SRCH_NOPR;               /* Last search flags.   */  int             srch_lastdir = SRCH_NOPR;       /* Last search flags.    */
   
 static VOID     is_cpush();  static VOID     is_cpush();
 static VOID     is_lpush();  static VOID     is_lpush();
 static VOID     is_pop();  static VOID     is_pop();
 static int      is_peek();  static int      is_peek();
 static VOID     is_undo();  static VOID     is_undo();
 static int      is_find();  static int      is_find();
 static VOID     is_prompt();  static VOID     is_prompt();
 static VOID     is_dspl();  static VOID     is_dspl();
 static int      eq();  static int      eq();
   
 /*  /*
  * Search forward.   * Search forward.
Line 48 
Line 48 
  * matched characters, and display does all the hard stuff.   * matched characters, and display does all the hard stuff.
  * If not found, it just prints a message.   * If not found, it just prints a message.
  */   */
 /*ARGSUSED*/  /* ARGSUSED */
 forwsearch(f, n)  forwsearch(f, n)
 {  {
         register int    s;          register int    s;
   
         if ((s=readpattern("Search")) != TRUE)          if ((s = readpattern("Search")) != TRUE)
                 return s;                  return s;
         if (forwsrch() == FALSE) {          if (forwsrch() == FALSE) {
                 ewprintf("Search failed: \"%s\"", pat);                  ewprintf("Search failed: \"%s\"", pat);
Line 70 
Line 70 
  * pointing at the first character of the pattern [the last character that   * pointing at the first character of the pattern [the last character that
  * was matched].   * was matched].
  */   */
 /*ARGSUSED*/  /* ARGSUSED */
 backsearch(f, n)  backsearch(f, n)
 {  {
         register int    s;          register int    s;
   
         if ((s=readpattern("Search backward")) != TRUE)          if ((s = readpattern("Search backward")) != TRUE)
                 return (s);                  return (s);
         if (backsrch() == FALSE) {          if (backsrch() == FALSE) {
                 ewprintf("Search failed: \"%s\"", pat);                  ewprintf("Search failed: \"%s\"", pat);
Line 91 
Line 91 
  * has been saved in "srch_lastdir", so you know which way   * has been saved in "srch_lastdir", so you know which way
  * to go.   * to go.
  */   */
 /*ARGSUSED*/  /* ARGSUSED */
 searchagain(f, n)  searchagain(f, n)
 {  {
         if (srch_lastdir == SRCH_FORW) {          if (srch_lastdir == SRCH_FORW) {
Line 116 
Line 116 
  * Use incremental searching, initially in the forward direction.   * Use incremental searching, initially in the forward direction.
  * isearch ignores any explicit arguments.   * isearch ignores any explicit arguments.
  */   */
 /*ARGSUSED*/  /* ARGSUSED */
 forwisearch(f, n)  forwisearch(f, n)
 {  {
         return isearch(SRCH_FORW);          return isearch(SRCH_FORW);
Line 126 
Line 126 
  * Use incremental searching, initially in the reverse direction.   * Use incremental searching, initially in the reverse direction.
  * isearch ignores any explicit arguments.   * isearch ignores any explicit arguments.
  */   */
 /*ARGSUSED*/  /* ARGSUSED */
 backisearch(f, n)  backisearch(f, n)
 {  {
         return isearch(SRCH_BACK);          return isearch(SRCH_BACK);
Line 143 
Line 143 
  *      other ^ exit search, don't set mark   *      other ^ exit search, don't set mark
  *      else    accumulate into search string   *      else    accumulate into search string
  */   */
 isearch(dir) {  isearch(dir)
         register int    c;  {
         register LINE   *clp;          register int    c;
         register int    cbo;          register LINE  *clp;
         register int    success;          register int    cbo;
         int             pptr;          register int    success;
         char            opat[NPAT];          int             pptr;
         VOID            ungetkey();          char            opat[NPAT];
           VOID            ungetkey();
   
 #ifndef NO_MACRO  #ifndef NO_MACRO
         if(macrodef) {          if (macrodef) {
             ewprintf("Can't isearch in macro");                  ewprintf("Can't isearch in macro");
             return FALSE;                  return FALSE;
         }          }
 #endif  #endif
         for (cip=0; cip<NSRCH; cip++)          for (cip = 0; cip < NSRCH; cip++)
                 cmds[cip].s_code = SRCH_NOPR;                  cmds[cip].s_code = SRCH_NOPR;
         (VOID) strcpy(opat, pat);          (VOID) strcpy(opat, pat);
         cip = 0;          cip = 0;
Line 203 
Line 204 
                                 is_cpush(SRCH_FORW);                                  is_cpush(SRCH_FORW);
                                 success = TRUE;                                  success = TRUE;
                         }                          }
                         if (success==FALSE && dir==SRCH_FORW)                          if (success == FALSE && dir == SRCH_FORW)
                                 break;                                  break;
                         is_lpush();                          is_lpush();
                         pptr = strlen(pat);                          pptr = strlen(pat);
                         (VOID) forwchar(FFRAND, 1);                          (VOID) forwchar(FFRAND, 1);
                         if (is_find(SRCH_FORW) != FALSE) is_cpush(SRCH_MARK);                          if (is_find(SRCH_FORW) != FALSE)
                                   is_cpush(SRCH_MARK);
                         else {                          else {
                                 (VOID) backchar(FFRAND, 1);                                  (VOID) backchar(FFRAND, 1);
                                 ttbeep();                                  ttbeep();
Line 224 
Line 226 
                                 is_cpush(SRCH_BACK);                                  is_cpush(SRCH_BACK);
                                 success = TRUE;                                  success = TRUE;
                         }                          }
                         if (success==FALSE && dir==SRCH_BACK)                          if (success == FALSE && dir == SRCH_BACK)
                                 break;                                  break;
                         is_lpush();                          is_lpush();
                         pptr = strlen(pat);                          pptr = strlen(pat);
                         (VOID) backchar(FFRAND, 1);                          (VOID) backchar(FFRAND, 1);
                         if (is_find(SRCH_BACK) != FALSE) is_cpush(SRCH_MARK);                          if (is_find(SRCH_BACK) != FALSE)
                                   is_cpush(SRCH_MARK);
                         else {                          else {
                                 (VOID) forwchar(FFRAND, 1);                                  (VOID) forwchar(FFRAND, 1);
                                 ttbeep();                                  ttbeep();
Line 241 
Line 244 
                 case CCHR('H'):                  case CCHR('H'):
                 case CCHR('?'):                  case CCHR('?'):
                         is_undo(&pptr, &dir);                          is_undo(&pptr, &dir);
                         if (is_peek() != SRCH_ACCM) success = TRUE;                          if (is_peek() != SRCH_ACCM)
                                   success = TRUE;
                         is_prompt(dir, pptr < 0, success);                          is_prompt(dir, pptr < 0, success);
                         break;                          break;
   
                 case CCHR('\\'):                  case CCHR('\\'):
                 case CCHR('Q'):                  case CCHR('Q'):
                         c = (char) getkey(FALSE);                          c = (char) getkey(FALSE);
                         goto  addchar;                          goto addchar;
                 case CCHR('M'):                  case CCHR('M'):
                         c = CCHR('J');                          c = CCHR('J');
                         goto  addchar;                          goto addchar;
   
                 default:                  default:
                         if (ISCTRL(c)) {                          if (ISCTRL(c)) {
Line 260 
Line 264 
                                 curwp->w_marko = cbo;                                  curwp->w_marko = cbo;
                                 ewprintf("Mark set");                                  ewprintf("Mark set");
                                 curwp->w_flag |= WFMOVE;                                  curwp->w_flag |= WFMOVE;
                                 return  TRUE;                                  return TRUE;
                         }       /* and continue */                          }       /* and continue */
                 case CCHR('I'):                  case CCHR('I'):
                 case CCHR('J'):                  case CCHR('J'):
                 addchar:          addchar:
                         if (pptr == -1)                          if (pptr == -1)
                                 pptr = 0;                                  pptr = 0;
                         if (pptr == 0)                          if (pptr == 0)
Line 289 
Line 293 
                         is_prompt(dir, FALSE, success);                          is_prompt(dir, FALSE, success);
                 }                  }
         }          }
         /*NOTREACHED*/          /* NOTREACHED */
 }  }
   
 static VOID  static          VOID
 is_cpush(cmd) register int cmd; {  is_cpush(cmd)
           register int    cmd;
   {
         if (++cip >= NSRCH)          if (++cip >= NSRCH)
                 cip = 0;                  cip = 0;
         cmds[cip].s_code = cmd;          cmds[cip].s_code = cmd;
 }  }
   
 static VOID  static          VOID
 is_lpush() {  is_lpush()
         register int    ctp;  {
           register int    ctp;
   
         ctp = cip+1;          ctp = cip + 1;
         if (ctp >= NSRCH)          if (ctp >= NSRCH)
                 ctp = 0;                  ctp = 0;
         cmds[ctp].s_code = SRCH_NOPR;          cmds[ctp].s_code = SRCH_NOPR;
Line 311 
Line 318 
         cmds[ctp].s_dotp = curwp->w_dotp;          cmds[ctp].s_dotp = curwp->w_dotp;
 }  }
   
 static VOID  static          VOID
 is_pop() {  is_pop()
   {
         if (cmds[cip].s_code != SRCH_NOPR) {          if (cmds[cip].s_code != SRCH_NOPR) {
                 curwp->w_doto  = cmds[cip].s_doto;                  curwp->w_doto = cmds[cip].s_doto;
                 curwp->w_dotp  = cmds[cip].s_dotp;                  curwp->w_dotp = cmds[cip].s_dotp;
                 curwp->w_flag |= WFMOVE;                  curwp->w_flag |= WFMOVE;
                 cmds[cip].s_code = SRCH_NOPR;                  cmds[cip].s_code = SRCH_NOPR;
         }          }
         if (--cip <= 0)          if (--cip <= 0)
                 cip = NSRCH-1;                  cip = NSRCH - 1;
 }  }
   
 static int  static int
 is_peek() {  is_peek()
   {
         return cmds[cip].s_code;          return cmds[cip].s_code;
 }  }
   
 /* this used to always return TRUE (the return value was checked) */  /* this used to always return TRUE (the return value was checked) */
 static VOID  static          VOID
 is_undo(pptr, dir) register int *pptr; register int *dir; {  is_undo(pptr, dir)
         register int    redo = FALSE ;          register int   *pptr;
           register int   *dir;
   {
           register int    redo = FALSE;
         switch (cmds[cip].s_code) {          switch (cmds[cip].s_code) {
         case SRCH_BEGIN:          case SRCH_BEGIN:
         case SRCH_NOPR:          case SRCH_NOPR:
Line 358 
Line 370 
                 break;                  break;
         }          }
         is_pop();          is_pop();
         if (redo) is_undo(pptr, dir);          if (redo)
                   is_undo(pptr, dir);
 }  }
   
 static int  static int
 is_find(dir) register int dir; {  is_find(dir)
         register int    plen, odoto;          register int    dir;
         register LINE   *odotp ;  {
           register int    plen, odoto;
           register LINE  *odotp;
   
         odoto = curwp->w_doto;          odoto = curwp->w_doto;
         odotp = curwp->w_dotp;          odotp = curwp->w_dotp;
         plen = strlen(pat);          plen = strlen(pat);
         if (plen != 0) {          if (plen != 0) {
                 if (dir==SRCH_FORW) {                  if (dir == SRCH_FORW) {
                         (VOID) backchar(FFARG | FFRAND, plen);                          (VOID) backchar(FFARG | FFRAND, plen);
                         if (forwsrch() == FALSE) {                          if (forwsrch() == FALSE) {
                                 curwp->w_doto = odoto;                                  curwp->w_doto = odoto;
Line 379 
Line 394 
                         }                          }
                         return TRUE;                          return TRUE;
                 }                  }
                 if (dir==SRCH_BACK) {                  if (dir == SRCH_BACK) {
                         (VOID) forwchar(FFARG | FFRAND, plen);                          (VOID) forwchar(FFARG | FFRAND, plen);
                         if (backsrch() == FALSE) {                          if (backsrch() == FALSE) {
                                 curwp->w_doto = odoto;                                  curwp->w_doto = odoto;
Line 402 
Line 417 
  * of the callers looked at the status, so I just   * of the callers looked at the status, so I just
  * made the checking vanish.   * made the checking vanish.
  */   */
 static VOID  static          VOID
 is_prompt(dir, flag, success) {  is_prompt(dir, flag, success)
   {
         if (dir == SRCH_FORW) {          if (dir == SRCH_FORW) {
                 if (success != FALSE)                  if (success != FALSE)
                         is_dspl("I-search", flag);                          is_dspl("I-search", flag);
Line 414 
Line 430 
                         is_dspl("I-search backward", flag);                          is_dspl("I-search backward", flag);
                 else                  else
                         is_dspl("Failing I-search backward", flag);                          is_dspl("Failing I-search backward", flag);
         } else ewprintf("Broken call to is_prompt");          } else
                   ewprintf("Broken call to is_prompt");
 }  }
   
 /*  /*
Line 422 
Line 439 
  * The "prompt" is just a string. The "flag" determines   * The "prompt" is just a string. The "flag" determines
  * whether pat should be printed.   * whether pat should be printed.
  */   */
 static VOID  static          VOID
 is_dspl(prompt, flag) char *prompt; {  is_dspl(prompt, flag)
           char           *prompt;
   {
   
         if (flag != FALSE)          if (flag != FALSE)
                 ewprintf("%s: ", prompt);                  ewprintf("%s: ", prompt);
Line 435 
Line 454 
  * Query Replace.   * Query Replace.
  *      Replace strings selectively.  Does a search and replace operation.   *      Replace strings selectively.  Does a search and replace operation.
  */   */
 /*ARGSUSED*/  /* ARGSUSED */
 queryrepl(f, n)  queryrepl(f, n)
 {  {
         register int    s;          register int    s;
         register int    rcnt = 0;       /* Replacements made so far     */          register int    rcnt = 0;       /* Replacements made so far      */
         register int    plen;           /* length of found string       */          register int    plen;   /* length of found string        */
         char            news[NPAT];     /* replacement string           */          char            news[NPAT];     /* replacement string            */
   
 #ifndef NO_MACRO  #ifndef NO_MACRO
         if(macrodef) {          if (macrodef) {
             ewprintf("Can't query replace in macro");                  ewprintf("Can't query replace in macro");
             return FALSE;                  return FALSE;
         }          }
 #endif  #endif
         if ((s=readpattern("Query replace")) != TRUE)          if ((s = readpattern("Query replace")) != TRUE)
                 return (s);                  return (s);
         if ((s=ereply("Query replace %s with: ",news, NPAT, pat)) == ABORT)          if ((s = ereply("Query replace %s with: ", news, NPAT, pat)) == ABORT)
                 return (s);                  return (s);
         if (s == FALSE)          if (s == FALSE)
                 news[0] = '\0';                  news[0] = '\0';
Line 465 
Line 484 
          */           */
   
         while (forwsrch() == TRUE) {          while (forwsrch() == TRUE) {
         retry:  retry:
                 update();                  update();
                 switch (getkey(FALSE)) {                  switch (getkey(FALSE)) {
                 case ' ':                  case ' ':
Line 480 
Line 499 
                         rcnt++;                          rcnt++;
                         goto stopsearch;                          goto stopsearch;
   
                 case CCHR('G'): /* ^G or ESC */                  case CCHR('G'):/* ^G or ESC */
                         (VOID) ctrlg(FFRAND, 0);                          (VOID) ctrlg(FFRAND, 0);
                 case CCHR('['):                  case CCHR('['):
                         goto stopsearch;                          goto stopsearch;
Line 494 
Line 513 
                         goto stopsearch;                          goto stopsearch;
   
                 case CCHR('H'):                  case CCHR('H'):
                 case CCHR('?'):         /* To not replace */                  case CCHR('?'):/* To not replace */
                         break;                          break;
   
                 default:                  default:
 ewprintf("<SP> replace, [.] rep-end, <DEL> don't, [!] repl rest <ESC> quit");                          ewprintf("<SP> replace, [.] rep-end, <DEL> don't, [!] repl rest <ESC> quit");
                         goto retry;                          goto retry;
                 }                  }
         }          }
Line 521 
Line 540 
  * is notified of the change, and TRUE is returned. If the   * is notified of the change, and TRUE is returned. If the
  * string isn't found, FALSE is returned.   * string isn't found, FALSE is returned.
  */   */
 forwsrch() {  forwsrch()
         register LINE   *clp;  {
         register int    cbo;          register LINE  *clp;
         register LINE   *tlp;          register int    cbo;
         register int    tbo;          register LINE  *tlp;
         char            *pp;          register int    tbo;
         register int    c;          char           *pp;
           register int    c;
   
         clp = curwp->w_dotp;          clp = curwp->w_dotp;
         cbo = curwp->w_doto;          cbo = curwp->w_doto;
         for(;;) {          for (;;) {
                 if (cbo == llength(clp)) {                  if (cbo == llength(clp)) {
                         if((clp = lforw(clp)) == curbp->b_linep) break;                          if ((clp = lforw(clp)) == curbp->b_linep)
                                   break;
                         cbo = 0;                          cbo = 0;
                         c = CCHR('J');                          c = CCHR('J');
                 } else                  } else
Line 541 
Line 562 
                 if (eq(c, pat[0]) != FALSE) {                  if (eq(c, pat[0]) != FALSE) {
                         tlp = clp;                          tlp = clp;
                         tbo = cbo;                          tbo = cbo;
                         pp  = &pat[1];                          pp = &pat[1];
                         while (*pp != 0) {                          while (*pp != 0) {
                                 if (tbo == llength(tlp)) {                                  if (tbo == llength(tlp)) {
                                         tlp = lforw(tlp);                                          tlp = lforw(tlp);
Line 554 
Line 575 
                                 if (eq(c, *pp++) == FALSE)                                  if (eq(c, *pp++) == FALSE)
                                         goto fail;                                          goto fail;
                         }                          }
                         curwp->w_dotp  = tlp;                          curwp->w_dotp = tlp;
                         curwp->w_doto  = tbo;                          curwp->w_doto = tbo;
                         curwp->w_flag |= WFMOVE;                          curwp->w_flag |= WFMOVE;
                         return TRUE;                          return TRUE;
                 }                  }
         fail:   ;  fail:           ;
         }          }
         return FALSE;          return FALSE;
 }  }
Line 571 
Line 592 
  * is notified of the change, and TRUE is returned. If the   * is notified of the change, and TRUE is returned. If the
  * string isn't found, FALSE is returned.   * string isn't found, FALSE is returned.
  */   */
 backsrch() {  backsrch()
         register LINE   *clp;  {
         register int    cbo;          register LINE  *clp;
         register LINE   *tlp;          register int    cbo;
         register int    tbo;          register LINE  *tlp;
         register int    c;          register int    tbo;
         register char   *epp;          register int    c;
         register char   *pp;          register char  *epp;
           register char  *pp;
   
         for (epp = &pat[0]; epp[1] != 0; ++epp)          for (epp = &pat[0]; epp[1] != 0; ++epp);
                 ;  
         clp = curwp->w_dotp;          clp = curwp->w_dotp;
         cbo = curwp->w_doto;          cbo = curwp->w_doto;
         for (;;) {          for (;;) {
Line 589 
Line 610 
                         clp = lback(clp);                          clp = lback(clp);
                         if (clp == curbp->b_linep)                          if (clp == curbp->b_linep)
                                 return FALSE;                                  return FALSE;
                         cbo = llength(clp)+1;                          cbo = llength(clp) + 1;
                 }                  }
                 if (--cbo == llength(clp))                  if (--cbo == llength(clp))
                         c = CCHR('J');                          c = CCHR('J');
                 else                  else
                         c = lgetc(clp,cbo);                          c = lgetc(clp, cbo);
                 if (eq(c, *epp) != FALSE) {                  if (eq(c, *epp) != FALSE) {
                         tlp = clp;                          tlp = clp;
                         tbo = cbo;                          tbo = cbo;
                         pp  = epp;                          pp = epp;
                         while (pp != &pat[0]) {                          while (pp != &pat[0]) {
                                 if (tbo == 0) {                                  if (tbo == 0) {
                                         tlp = lback(tlp);                                          tlp = lback(tlp);
                                         if (tlp == curbp->b_linep)                                          if (tlp == curbp->b_linep)
                                                 goto fail;                                                  goto fail;
                                         tbo = llength(tlp)+1;                                          tbo = llength(tlp) + 1;
                                 }                                  }
                                 if (--tbo == llength(tlp))                                  if (--tbo == llength(tlp))
                                         c = CCHR('J');                                          c = CCHR('J');
                                 else                                  else
                                         c = lgetc(tlp,tbo);                                          c = lgetc(tlp, tbo);
                                 if (eq(c, *--pp) == FALSE)                                  if (eq(c, *--pp) == FALSE)
                                         goto fail;                                          goto fail;
                         }                          }
                         curwp->w_dotp  = tlp;                          curwp->w_dotp = tlp;
                         curwp->w_doto  = tbo;                          curwp->w_doto = tbo;
                         curwp->w_flag |= WFMOVE;                          curwp->w_flag |= WFMOVE;
                         return TRUE;                          return TRUE;
                 }                  }
         fail:   ;  fail:           ;
         }          }
         /*NOTREACHED*/          /* NOTREACHED */
 }  }
   
 /*  /*
Line 631 
Line 652 
  */   */
 static int  static int
 eq(bc, pc)  eq(bc, pc)
 register int bc, pc;          register int    bc, pc;
 {  {
         bc = CHARMASK(bc);          bc = CHARMASK(bc);
         pc = CHARMASK(pc);          pc = CHARMASK(pc);
         if (bc == pc) return TRUE;          if (bc == pc)
         if (ISUPPER(bc)) return TOLOWER(bc) == pc;                  return TRUE;
         if (ISUPPER(pc)) return bc == TOLOWER(pc);          if (ISUPPER(bc))
                   return TOLOWER(bc) == pc;
           if (ISUPPER(pc))
                   return bc == TOLOWER(pc);
         return FALSE;          return FALSE;
 }  }
   
Line 649 
Line 673 
  * Display the old pattern, in the style of Jeff Lomicka. There is   * Display the old pattern, in the style of Jeff Lomicka. There is
  * some do-it-yourself control expansion.   * some do-it-yourself control expansion.
  */   */
 readpattern(prompt) char *prompt; {  readpattern(prompt)
         register int    s;          char           *prompt;
         char            tpat[NPAT];  {
           register int    s;
           char            tpat[NPAT];
   
         if (tpat[0] == '\0') s = ereply("%s: ", tpat, NPAT, prompt);          if (tpat[0] == '\0')
         else s = ereply("%s: (default %s) ", tpat, NPAT, prompt, pat);                  s = ereply("%s: ", tpat, NPAT, prompt);
           else
                   s = ereply("%s: (default %s) ", tpat, NPAT, prompt, pat);
   
         if (s == TRUE)                          /* Specified            */          if (s == TRUE)          /* Specified             */
                 (VOID) strcpy(pat, tpat);                  (VOID) strcpy(pat, tpat);
         else if (s==FALSE && pat[0]!=0)         /* CR, but old one      */          else if (s == FALSE && pat[0] != 0)     /* CR, but old one       */
                 s = TRUE;                  s = TRUE;
         return s;          return s;
 }  }

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2