home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / vile-src.zip / vile-8.1 / csrch.c < prev    next >
C/C++ Source or Header  |  1998-04-28  |  3KB  |  203 lines

  1. /* These functions perform vi's on-this-line character scanning functions.
  2.  * written for vile: Copyright (c) 1990, 1995 by Paul Fox
  3.  *
  4.  * $Header: /usr/build/vile/vile/RCS/csrch.c,v 1.28 1998/04/28 10:16:08 tom Exp $
  5.  *
  6. */
  7.  
  8. #include "estruct.h"
  9. #include "edef.h"
  10.  
  11. static USHORT lstscan;
  12. static int   lstchar;
  13. #define BACK 0
  14. #define FORW 1
  15. #define DIREC 1
  16.  
  17. #define F 0
  18. #define T 2
  19. #define TYPE 2
  20.  
  21. static int
  22. fscan(int f, int n, int c)
  23. {
  24.     int i;
  25.     int doto;
  26.  
  27.     if (!f || n <= 0)
  28.         n = 1;
  29.  
  30.     lstchar = c;
  31.     lstscan = FORW;
  32.  
  33.     doto = DOT.o;
  34.  
  35.     i = doto+1;
  36.     while(i < llength(DOT.l)) {
  37.         if ( c == lgetc(DOT.l,i)) {
  38.             doto = i;
  39.             n--;
  40.             if (!n) break;
  41.         }
  42.         i++;
  43.     }
  44.  
  45.     if ( i == llength(DOT.l)) {
  46.         return(FALSE);
  47.     }
  48.     if (doingopcmd && !doingsweep)
  49.         doto++;
  50.     else if (doingsweep)
  51.         sweephack = TRUE;
  52.  
  53.     DOT.o = doto;
  54.     curwp->w_flag |= WFMOVE;
  55.     return(TRUE);
  56.  
  57. }
  58.  
  59. static int
  60. bscan(int f, int n, int c)
  61. {
  62.     int i;
  63.     int doto;
  64.  
  65.     if (!f || n <= 0)
  66.         n = 1;
  67.  
  68.     lstchar = c;
  69.     lstscan = BACK;
  70.  
  71.     doto = DOT.o;
  72.  
  73.     i = doto-1;
  74.     while(i >= w_left_margin(curwp)) {
  75.         if ( c == lgetc(DOT.l,i)) {
  76.             doto = i;
  77.             n--;
  78.             if (!n) break;
  79.         }
  80.         i--;
  81.     }
  82.  
  83.     if ( i < w_left_margin(curwp) ) {
  84.         return(FALSE);
  85.     }
  86.  
  87.     DOT.o = doto;
  88.     curwp->w_flag |= WFMOVE;
  89.     return(TRUE);
  90.  
  91. }
  92.  
  93. static int
  94. get_csrch_char(int *cp)
  95. {
  96.     int c;
  97.  
  98.     if (clexec || isnamedcmd) {
  99.         int status;
  100.         static char cbuf[2];
  101.         if ((status=mlreply("Scan for: ", cbuf, 2)) != TRUE)
  102.             return status;
  103.         c = cbuf[0];
  104.     } else {
  105.         c = keystroke();
  106.         if (c == quotec)
  107.             c = keystroke_raw8();
  108.         else if (ABORTED(c))
  109.             return FALSE;
  110.     }
  111.  
  112.     *cp = c;
  113.     return TRUE;
  114. }
  115.  
  116. /* f */
  117. int
  118. fcsrch(int f, int n)
  119. {
  120.     int c, s;
  121.  
  122.     s = get_csrch_char(&c);
  123.     if (s != TRUE)
  124.         return s;
  125.  
  126.     return(fscan(f,n,c));
  127. }
  128.  
  129. /* F */
  130. int
  131. bcsrch(int f, int n)
  132. {
  133.     int c, s;
  134.  
  135.     s = get_csrch_char(&c);
  136.     if (s != TRUE)
  137.         return s;
  138.  
  139.     return(bscan(f,n,c));
  140. }
  141.  
  142. /* t */
  143. int
  144. fcsrch_to(int f, int n)
  145. {
  146.     int s;
  147.     s = fcsrch(f,n);
  148.     if (s == TRUE)
  149.         s = backchar(FALSE,1);
  150.     lstscan |= T;
  151.     return(s);
  152. }
  153.  
  154. /* T */
  155. int
  156. bcsrch_to(int f, int n)
  157. {
  158.     int s;
  159.     s = bcsrch(f,n);
  160.     if (s == TRUE)
  161.         s = forwchar(FALSE,1);
  162.     lstscan |= T;
  163.     return(s);
  164. }
  165.  
  166. /* ; */
  167. int
  168. rep_csrch(int f, int n)
  169. {
  170.     int s;
  171.     USHORT ls = lstscan;
  172.  
  173.     if ((ls & DIREC) == FORW) {
  174.         s = fscan(f,n,lstchar);
  175.         if ((ls & TYPE) == T) {
  176.             if (s == TRUE)
  177.                 s = backchar(FALSE,1);
  178.             lstscan |= T;
  179.         }
  180.         return(s);
  181.     } else {
  182.         s = bscan(f,n,lstchar);
  183.         if ((ls & TYPE) == T) {
  184.             if (s == TRUE)
  185.                 s = forwchar(FALSE,1);
  186.             lstscan |= T;
  187.         }
  188.         return(s);
  189.     }
  190. }
  191.  
  192. /* , */
  193. int
  194. rev_csrch(int f, int n)
  195. {
  196.     int s;
  197.  
  198.     lstscan ^= DIREC;
  199.     s = rep_csrch(f,n);
  200.     lstscan ^= DIREC;
  201.     return(s);
  202. }
  203.