#       update $cmds so that we just load once
$cmds{"search_menu"} = '&search_menu';

%search_menu_cmds = (
	'?',	'$arg = "s"; eval $cmds{"help"}',

	'g',	'&search_globally',
	'p',	'&search_package',

	'A',	'eval $cmds{"add_menu"}',
	'E',	'&record_actions',
	'M',	'&record_actions',
	'C',	'&reset_variables',
	'V',	'&record_actions',
	'P',	'&record_actions',
	'R',	'0',
	'!',	'&quick_shell',
	' ',	'&pop_level',
	'@',	'eval $cmds{"eval_perl_expression"}',
	'-',	'&push_previous_menu',
);

#	'l',	'&search_location',	'c',	'&search_contact',

$search_menu_cmds{pack("c", 27)} = '&main_menu';	# ESC

#	search_menu - entry point for searches
sub search_menu {

    #	ay yi yi... we need to have references.pl loaded so that
    #	we can register callbacks to find_references.  Going through
    #	$cmds and trying to call a callback wont work because you
    #	get a very strange looking function call that bombs
    if (! defined ($refs_init)) {
	&load_routine ("references.pl", "references_init");
    }
    push (@menu_stack, "&search_menu");
    while ($menu_stack[$#menu_stack] =~ "&search_menu") {
	&do_callbacks("search");
	print TTYOUT <<"+++";
$clear_str
		Search/Edit Menu $init_message

        $num_data_files data files total${last_found_str}${reference_hdr_str}

	?       help

        Search by:              Actions:

        p       package         A       add
        c       contact         C       clear patterns
        l       location        E       edit
        g       globally        M       mail
                                P       print
                                V       view
				$reference_str

        Current Search Pattern: "$search_pattern"

+++
        print TTYOUT "\tyour choice >> "; &flush;
        read (TTYIN, $ans, 1);

        eval ($search_menu_cmds{$ans}) ||
            print TTYOUT "no such option: $ans\n";

    }
    1;
}


#	record_actions - manipulate the found records
#
# this will do any number of things to the found files, depending on
# what $ans is set to...
sub record_actions {
    local ($pushd_arg[0]) = $DIRS{'DATA'};
    
    # first, build a list of filenames...
    @found_files = ();
    # note: hey! this loop could be a one liner, but would it be any faster?
    foreach $file (@found_records) {
	push (@found_files, $file);
    }

    &pushd (*main_dir_stack, *pushd_arg);
    eval $record_actions{$ans};
    &popd (*main_dir_stack);
    1;
}


#	search_package - search by name
sub search_package {
    print TTYOUT "\n\n";
    @found_records = ();
    &add_callback("eval $cmds{'find_references'}");
    @command_history = @package_history;
    &get_input("\tenter package search pattern >> ");
    $search_pattern = $command_history[$#command_history];
    @key_records = grep(/$search_pattern/, keys %all_packages);
    foreach $package_key (@key_records) {
	push (@found_records, $all_packages{$package_key});
    }
    @package_history = @command_history;
    $last_found_str=", last search found ".($#found_records + 1);
    1;
}


sub make_data_search_routine {
    local($the_keystroke, $the_routine_name) = @_;

    # register with our search menu
    $search_menu_cmds{"$the_keystroke"} = "&search_SR_$the_routine_name";

    eval <<EOS;
sub search_SR_$the_routine_name {
    \&add_callback("eval \$cmds{'find_references'}");
    print TTYOUT "\n\n";
    \@command_history = \@${the_routine_name}_history;
    \&get_input("\tenter $the_routine_name search pattern >> ");
    \$search_pattern =
    "${the_routine_name}:.*".\$command_history[\$#command_history];
					       \&file_search(\$search_pattern);
    \$last_found_str=", last search found ".(\$#found_records + 1);
    \@${the_routine_name}_history = \@command_history;
    1;
}
EOS
    1;
}


#	search_globally - grep for pattern in all current data files
sub search_globally {
    &add_callback("eval $cmds{'find_references'}");
    print TTYOUT "\n\n";
    
    # picking the @package_history is a good shot, but it's also bogus.
    # what should happen here is to refer to a global history (all of the
    # lines typed in throughout the script...
    
    @command_history = @package_history;
    &get_input("\tenter global search pattern >> ");
    print TTYOUT "..."; &flush;
    $search_pattern = $command_history[$#command_history];
    &file_search($search_pattern);
    $last_found_str=", last search found ".($#found_records + 1);
    @package_history = @command_history;
    1;
}


#	file_search - do an actual search through data files
sub file_search {
    local ($pattern) = @_;
    ($pattern = $pattern) =~ s/!/\\!/g;
    local ($pushd_arg[0]) = $DIRS{'DATA'};
    @found_records = ();
    
    &pushd (*main_dir_stack, *pushd_arg);
    foreach $file (values %all_packages) {
	open (CUR_FILE, $file);
	while (<CUR_FILE>) {
	    if (m!$pattern!) {
		push (@found_records, $file);
		last;
	    }
	}
	close(CUR_FILE);
    }
    &popd (*main_dir_stack);
    1;
}
1;
