home *** CD-ROM | disk | FTP | other *** search
/ Hot Shareware 35 / hot35.iso / ficheros / 9TXT / ZE32V270.ZIP / ISEARCH.ZM_ / ISEARCH.ZM
Text File  |  1997-10-04  |  5KB  |  166 lines

  1. /*************************************************************************
  2.  
  3.         Name: IncrementalSearch
  4.  
  5.  Description: This macro provides an incremental search  feature.  To
  6.               use this macro either run it directly or  install it to 
  7.               the Macros menu using the Options Macros menu item. The
  8.               following keys are also supported:
  9.  
  10.                 ASCII Char - add the char to the search string
  11.                 BackSpace  - remove last char from the search string
  12.                 ESC        - cancel the search
  13.                 Enter      - complete the search
  14.                 ArrowUp    - search for previous occurrence
  15.                 ArrowDown  - search for next occurrence
  16.                 ArrowLeft  - same as backspace
  17.                 ArrowRight - add the next char to the search string
  18.  
  19.       Author: Brander, Bertel K.
  20.  
  21.      Contact: bertel@post4.tele.dk
  22.               
  23. **************************************************************************/
  24.  
  25. int IncrementalSearch()
  26. {
  27.   int  ox;
  28.   int  oy;
  29.   int  length;
  30.   int  end = 0;
  31.   char ch;
  32.   string sstring = "";
  33.  
  34.   // make sure it is a document window
  35.   if (is_document() == 0)
  36.   {
  37.       message("This macro only works for document files!");
  38.       beep();
  39.       return;
  40.   }
  41.  
  42.   // save the current cursor porition
  43.   cursor_save();
  44.   
  45.   // save the current search engine setting
  46.   search_save();
  47.  
  48.   set_find_text(sstring);
  49.   
  50.   // extended codes found by printing 'getch' values to debug_output
  51.   key_left      = 37;
  52.   key_up        = 38;
  53.   key_right     = 39;
  54.   key_down      = 40;
  55.   key_escape    = 27;
  56.   key_backspace = 8;
  57.   key_enter     = 13;
  58.  
  59.   ch = 0;
  60.  
  61.   message("Enter the search text. Backspace, ESC/Enter and Arrow keys control searching.");
  62.  
  63.   // do the searching till an escape/enter key is hit
  64.   while (end == 0)
  65.   {
  66.     // gets ascii keys and extended keys
  67.     ch = getch(sExtended);
  68.     
  69.     if ((ch != -1) && (ch != key_escape) && (ch != key_enter))
  70.     {
  71.       if ((ch == 0) && (sExtended == key_down))
  72.       {
  73.         // line down key pressed
  74.         found = SearchNext();
  75.       }
  76.       else if ((ch == 0) && (sExtended == key_up))
  77.       {
  78.         // line up key pressed
  79.         found = SearchPrevious();
  80.       }
  81.       else if ((ch == 0) && (sExtended == key_right))
  82.       {
  83.         // get the current column position
  84.         get_column_pos(column);
  85.     
  86.         // get the length of the current highlighted word and a bit more
  87.         sstring = "$W";
  88.         wordlength = strlen(sstring) + 1;
  89.     
  90.         // get a part of the line starting at column of wordlength
  91.         get_line(sstring, column, wordlength);
  92.     
  93.         // search for the new word
  94.         set_find_text(sstring);
  95.         found = SearchNext();
  96.       }
  97.       else if (((ch == 0) && (sExtended == key_left)) || (ch == key_backspace))
  98.       {
  99.         // remove last char from sstring
  100.         length = strlen(sstring);
  101.    
  102.         if (length > 1)
  103.         {
  104.           strleft(sstring, sstring, length -1);
  105.           set_find_text(sstring);
  106.           found = SearchPrevious();
  107.         }
  108.         else
  109.         {
  110.           // nothing left to search for
  111.           sstring = "";
  112.           MarkHide();
  113.           found = 1;
  114.         }
  115.       }
  116.       else if (ch != 0)
  117.       {
  118.         // an ascii key was pressed
  119.         get_column_pos(ox);
  120.         get_line_pos(oy);
  121.         strcat(sstring, ch);
  122.         set_find_text(sstring);
  123.         MoveLineLeftEx();
  124.  
  125.         found = 1;  // assume success
  126.  
  127.         if (SearchNext() == 0)
  128.         {
  129.           // no match were found so try search backwards with no sound
  130.           beep_enable(0);
  131.           if (SearchPrevious() == 0)
  132.           {
  133.             // No match were found, remove last char from sstring
  134.             length = strlen(sstring);
  135.             strleft(sstring, sstring, length - 1);
  136.             set_find_text(sstring);
  137.             set_column_pos(ox);
  138.             set_line_pos(oy);
  139.             found = 0;  // no success
  140.           }
  141.           beep_enable(1);
  142.         }
  143.       }
  144.  
  145.       if (found == 1) message("Search for: %s", sstring);
  146.     }
  147.     else if (ch == key_escape)
  148.     {
  149.       // restore the cursor porition
  150.       cursor_restore();
  151.       message("Incremental search canceled.");
  152.       end = -1;
  153.     }
  154.     else if (ch == key_enter)
  155.     {
  156.       end = 1;
  157.     }
  158.   }
  159.   if (end == 1) message("Incremental search complete.");
  160.   MarkHide();
  161.  
  162.   // restore the search settings
  163.   search_restore();
  164. }
  165.  
  166.