home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD1.iso / Editor / DVD!FX17.LHA / FrexxEd / fpl / IncSearch.FPL < prev    next >
Encoding:
Text File  |  1995-03-08  |  3.7 KB  |  120 lines

  1. // $VER: IncSearch.FPL 1.2 (08.03.95) © Jesper Skov
  2.  
  3. // dir - Initial direction of search
  4. //   -1: Backward
  5. //    1: Forward
  6. //
  7. string lastsearch="";                        // holds string from previous search session
  8.  
  9. void export IncSearch2(int dir)
  10. {
  11.   int cont=1;
  12.   int searchLen = 0;
  13.   int prevLine, prevByte;
  14.   int lenHistory[96];
  15.   int byteHistory[96];
  16.   int lineHistory[96];
  17.   int count = 0;
  18.   int searchflags=ReadInfo("search_flags"); // Keep old search flags
  19.  
  20.   string searchString, key;
  21.  
  22.   if (1==dir)                                // Set inital search direction
  23.     SearchSet("=f+");
  24.   else
  25.     SearchSet("=f-");
  26.  
  27.   byteHistory[0]=ReadInfo("byte_position");    // Get initial position
  28.   lineHistory[0]=ReadInfo("line");
  29.   lenHistory[0]=0;
  30.  
  31.   while (cont){
  32.     Status(0, joinstr("Search:»",searchString,"«"));
  33.     key = GetKey();
  34.     if (!strcmp(key,"\x07") || !strcmp(key, "\x1b")){    // ESC or C-g -> Cancel search
  35.       GotoLine(lineHistory[0], byteHistory[0]);
  36.       cont = 0;
  37.  
  38.     } else if (!strcmp(key, "\b")){         // Backspace
  39.       if (searchLen && count){
  40.         count--;
  41.         searchLen=lenHistory[count];
  42.         searchString=substr(searchString, 0, searchLen);
  43.         GotoLine(lineHistory[count], byteHistory[count]);
  44.       }
  45.  
  46.     } else if (count<96){                    // Only proceed if enough table space!
  47.  
  48.       if (!strcmp(key, "\x13")){            // C-s -> search forward
  49.         dir = 1;
  50.         if (!searchLen && ReadInfo("search_forward")){
  51.           searchString=lastsearch;            // Get old string if C-r/s C-s
  52.           searchLen=strlen(searchString);
  53.         }
  54.         SearchSet("f+");
  55.         if (searchLen)
  56.           if (Search(searchString)){
  57.             DisplayBeep();
  58.           } else {
  59.             count++;                            // Inc one for successfull continued search
  60.             lenHistory[count]=searchLen;
  61.             byteHistory[count]=ReadInfo("byte_position");
  62.             lineHistory[count]=ReadInfo("line");
  63.           }
  64.       } else if (!strcmp(key, "\x12")){     // C-r -> search backwards
  65.         dir = -1;
  66.         if (!searchLen && !ReadInfo("search_forward")) {
  67.           searchString=lastsearch;            // Get old string if C-r/s C-r
  68.           searchLen=strlen(searchString);
  69.         }
  70.         SearchSet("f-");
  71.         if (searchLen) {
  72.           if (Search(searchString)){
  73.             DisplayBeep();
  74.           } else {
  75.             count++;                            // Inc one for successfull continued search
  76.             lenHistory[count]=searchLen;
  77.             byteHistory[count]=ReadInfo("byte_position");
  78.             lineHistory[count]=ReadInfo("line");
  79.           }
  80.         }
  81.       } else { // Check if we're dealing with a printable [spc-z]
  82.         if ((0<=strcmp(key, " "))&&(0>=strcmp(key, "z"))&&(searchLen<33)){
  83.           searchLen++;
  84.           searchString+=key;
  85.           prevLine=ReadInfo("line");
  86.           prevByte=ReadInfo("byte_position");
  87.           if (1==dir){
  88.             CursorLeft();                    // Prepare forward search
  89.           } else {
  90.             CursorRight(searchLen);            // Prepare backwards search
  91.           }
  92.           count++;
  93.           lenHistory[count]=searchLen;
  94.           if (Search(searchString)){        // If no luck re-position cursor
  95.             if (1==dir){
  96.               CursorRight();
  97.             } else {
  98.               CursorLeft(searchLen);
  99.             }
  100.             DisplayBeep();
  101.           }
  102.           byteHistory[count]=ReadInfo("byte_position"); // Get new position
  103.           lineHistory[count]=ReadInfo("line");
  104.         } else {                            // Non-printable -> quit
  105.           cont=0;
  106.         }
  107.       }
  108.     }
  109.   }
  110.   if (searchLen)
  111.     lastsearch=searchString;                // Remember searchstring
  112.   SetInfo(0, "search_flags", searchflags);    // Restore search flags
  113. }
  114.  
  115. AssignKey("IncSearch2(-1);","control r");
  116. AssignKey("IncSearch2(1);","control s");
  117.  
  118.  
  119. // Ideas:        Wrapped search (C-s after fail -> wrap buffer)
  120.