home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / simtel / emacs / src / bsearch.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-07  |  3.0 KB  |  142 lines

  1. #include    "stdio.h"
  2. #include    "ed.h"
  3.  
  4. /*
  5.  * Reverse search.
  6.  * Get a search string from the
  7.  * user, and search, starting at "."
  8.  * and proceeding toward the front of
  9.  * the buffer. If found "." is left
  10.  * pointing at the first character of
  11.  * the pattern [the last character that
  12.  * was matched]. Bound to "M-R".
  13.  */
  14. ovmain(x, f, n)
  15. {
  16.     register LINE    *clp;
  17.     register int    cbo;
  18.     register LINE    *tlp;
  19.     register int    tbo;
  20.     register int    c;
  21.     register char    *epp;
  22.     register char    *pp;
  23.     register int    s;
  24.  
  25.     if ((s=readpattern("Reverse search")) != TRUE)
  26.         return (s);
  27.     for (epp = &pat[0]; epp[1] != 0; ++epp)
  28.         ;
  29.     clp = curwp->w_dotp;
  30.     cbo = curwp->w_doto;
  31.     for (;;) {
  32.         if (cbo == 0) {
  33.             clp = lback(clp);
  34.             if (clp == curbp->b_linep) {
  35.                 mlwrite("Not found");
  36.                 return (FALSE);
  37.             }
  38.             cbo = llength(clp)+1;
  39.         }
  40.         if (--cbo == llength(clp))
  41.             c = '\n';
  42.         else
  43.             c = lgetc(clp, cbo);
  44.         if (eq(c, *epp) != FALSE) {
  45.             tlp = clp;
  46.             tbo = cbo;
  47.             pp  = epp;
  48.             while (pp != &pat[0]) {
  49.                 if (tbo == 0) {
  50.                     tlp = lback(tlp);
  51.                     if (tlp == curbp->b_linep)
  52.                         goto fail;
  53.                     tbo = llength(tlp)+1;
  54.                 }
  55.                 if (--tbo == llength(tlp))
  56.                     c = '\n';
  57.                 else
  58.                     c = lgetc(tlp, tbo);
  59.                 if (eq(c, *--pp) == FALSE)
  60.                     goto fail;
  61.             }
  62.             curwp->w_dotp  = tlp;
  63.             curwp->w_doto  = tbo;
  64.             curwp->w_flag |= WFMOVE;
  65.             return (TRUE);
  66.         }
  67.     fail:    ;
  68.     }
  69. }
  70.  
  71. /*
  72.  * Compare two characters.
  73.  * The "bc" comes from the buffer.
  74.  * It has it's case folded out. The
  75.  * "pc" is from the pattern.
  76.  */
  77. eq(bc, pc)
  78. register int    bc;
  79. register int    pc;
  80. {
  81.     if (bc>='a' && bc<='z')
  82.         bc -= 0x20;
  83.     if (pc>='a' && pc<='z')
  84.         pc -= 0x20;
  85.     if (bc == pc)
  86.         return (TRUE);
  87.     return (FALSE);
  88. }
  89.  
  90. /*
  91.  * Read a pattern.
  92.  * Stash it in the external
  93.  * variable "pat". The "pat" is
  94.  * not updated if the user types in
  95.  * an empty line. If the user typed
  96.  * an empty line, and there is no
  97.  * old pattern, it is an error.
  98.  * Display the old pattern, in the
  99.  * style of Jeff Lomicka. There is
  100.  * some do-it-yourself control
  101.  * expansion.
  102.  */
  103. readpattern(prompt)
  104. char    *prompt;
  105. {
  106.     register char    *cp1;
  107.     register char    *cp2;
  108.     register int    c;
  109.     register int    s;
  110.     char        tpat[NPAT+20];
  111.  
  112.     cp1 = &tpat[0];                /* Copy prompt        */
  113.     cp2 = prompt;
  114.     while ((c = *cp2++) != '\0')
  115.         *cp1++ = c;
  116.     if (pat[0] != '\0') {            /* Old pattern        */
  117.         *cp1++ = ' ';
  118.         *cp1++ = '[';
  119.         cp2 = &pat[0];
  120.         while ((c = *cp2++) != 0) {
  121.             if (cp1 < &tpat[NPAT+20-6]) {    /* "??]: \0"    */
  122.                 if (c<0x20 || c==0x7F) {
  123.                     *cp1++ = '^';
  124.                     c ^= 0x40;
  125.                 } else if (c == '%')    /* Map "%" to    */
  126.                     *cp1++ = c;    /* "%%".    */
  127.                 *cp1++ = c;
  128.             }
  129.         }
  130.         *cp1++ = ']';
  131.     }
  132.     *cp1++ = ':';                /* Finish prompt    */
  133.     *cp1++ = ' ';
  134.     *cp1++ = '\0';
  135.     s = mlreply(tpat, tpat, NPAT);        /* Read pattern        */
  136.     if (s == TRUE)                /* Specified        */
  137.         strcpy(pat, tpat);
  138.     else if (s==FALSE && pat[0]!=0)        /* CR, but old one    */
  139.         s = TRUE;
  140.     return (s);
  141. }
  142.