/*---------------05-18-93 09:45am-------------------
 * The macro rbfilter will filter a file using a regular
 * expression string.  The output of the filter is put in
 * a file named 'rbfilter.out'.  The filter starts from the start of the file.
 *
 * This file also contains a macro 'get_str' which prompts the
 * user for the regular expression string to be used in the
 * search.  The macro get_str was just cut & pasted from example.s,
 * removing all the stuff I didn't need.
 *
 * Author: Ron Bondy,
 * Warranty: None, Freeware, use at your own risk, not to be included in
 *                           any commercial products
 * Revision History:
 * May 18, 1993 - morning - initial version
 *                afternoon - saved string in history list
 *                          - avoid getting multiple hits on the same line
 *--------------------------------------------------*/

macro_file rbfilter;

macro rbfilter DUMP
{
   refresh = false;          // turn of screen refresh to speed things up
   int orig_id = window_id;  // save original window id
   int t_line = c_line;      // save original line number
   int t_col  = c_col;       // save original column number

   int reg_exp_t = reg_exp_stat;  // save regular expression status
   reg_exp_stat  = 1;             // enable regular expressions

   int insert_mode_t = insert_mode;   // save original insert mode status
   insert_mode       = 1;             // enable insert mode

   switch_window(window_count);       // switch to last window
   create_window;                     // create a new window for the search output
   if ( Error_Level != 0 )
      {                             /* If an error occurred then */
      RM('MEERROR');                /* Run the ME-ERROR macro */
      Goto EXIT;                    /* and exit */
      }

   int new_id = window_id;          // save the new window id
   file_name = "rbfilter.out";      // set the file name
   window_name = "Srch";            // set the window name

   switch_win_id(orig_id);          // switch back to the original file to begin the search
   tof;                             // start the search from the top of the original file

   int kount = 0;                   // count how many search strings were found

//   str srch_str = "%?*ary[ |9]*$";   // testing - search for all words ending in 'ary'
   str srch_str = "";

   rm('rbfilter^get_str');           // prompt for the regular expression string, returned in return_str
   srch_str = return_str;

   while( search_fwd(srch_str,0) )            // do the search
      {
      kount++;
      str t1 = get_line;                      // copy the current line to string t1
      switch_win_id(new_id);                  // switch to the output window
      text( t1 );                             // write t1 to the output window
      cr;                                     // need to add a carriage return
      switch_win_id(orig_id);                 // back to the original window to continue the search
      eol;                                    // go to the end of line, search pattern may include %
      }

   make_message(srch_str + " was found " + str(kount) +  " times");

   insert_mode = insert_mode_t;  // restore original mode info
   reg_exp_stat= reg_exp_t;

   switch_win_id(orig_id);   // return to the original line and column numbers
   goto_line(t_line);
   goto_col(t_col);

   switch_win_id(new_id);    // leave cursor at the top of the new file
   tof;

exit:
}

macro get_str DUMP {
/*---------------05-18-93 10:18am-------------------
 * this is a cut down version of example^data_in_example
 * --------------------------------------------------*/

   int original_window = cur_window,    /* remember where we were */
       T_Int,                           /* temp integer variable  */
       menu = menu_create               /* create menu data structure */
      ;

   refresh = FALSE;
   switch_window( window_count );
   create_window;

   str last_str = GLOBAL_STR('!RBFILTER_STR');

   menu_set_item(menu,1,"Reg Exp Search String",last_str,"/QK=1/L=1/C=1/W=15/ML=50/HISTORY=RBFILTER",0,0,0);

   Return_Int = menu;
   RM("USERIN^DATA_IN /HN=1/PRE=EX/#=1/T=Filter Dialog Box/H=DATABOX");

   return_str = menu_item_str(menu,1,2);

   SET_GLOBAL_STR('!RBFILTER_STR',return_str);

//   make_message( return_str );   // testing...
	delete_window;                      /* kill our list window  */
   switch_window( original_window );   /* Go back to where we were */
	menu_delete(menu);            /* get rid of the menu data structure */
}

