#	add menu routines

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


%add_menu_cmds = (
		  '?',    '$arg = "a"; eval $cmds{"help"}',
		  'B',    'eval $cmds{"batch_menu"}',
		  'R',    '&reset_variables',
		  'U',    '&update',
		  '!',    '&quick_shell',
		  ' ',    '&pop_level',
		  '@',    'eval $cmds{"eval_perl_expression"}',
		  '-',    '&push_previous_menu',
		  );

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

#	add_menu - add new records interactively
sub add_menu {
    push(@menu_stack, "&add_menu");
    while ($menu_stack[$#menu_stack] =~ "&add_menu") {
	print TTYOUT <<"+++";
$clear_str
                Add Menu $init_message 

        current values: $saved_stat, $num_data_files data files $dir_menu_msg

        ?       help

$list_syms{"ADD_MENU_STRS"}

	B	batch input
        R       reset variables
        U       update

+++
        print TTYOUT "\tyour choice >> "; &flush;
	read(TTYIN, $ans, 1);
	eval($add_menu_cmds{$ans}) || 
	    print TTYOUT "hey, no such option: $ans\n";

    }
    1;
}



#	make_data_input_routine
#
#	this generates a subroutine on the fly to be used for data
#	input in the add menu.  Whatever %list_data template is
#	active is the one we'll generate routines for.  We will usually
#	be called by &parse_primary or &parse_data_field.  As a check
#	to see if your template/routines "made it", you can try pushing
#	the CLI, use the "dumpv" alias, and search for "add_menu_cmds".
sub make_data_input_routine {
    local($the_keystroke, $the_routine_name, $the_value_var) = @_;
    
    # register with our add menu
    $add_menu_cmds{"$the_keystroke"}  = "&GS_add_$the_routine_name";

    eval <<EOS;
sub GS_add_$the_routine_name {
    print TTYOUT "\n\n";
    \@command_history = \@${the_routine_name}_history;
    \&get_input("\tenter $the_routine_name >> ");
    \$list_syms{"$the_value_var"} = \$command_history[\$#command_history];
     \@${the_routine_name}_history = \@command_history;
     if (\$list_syms{"$the_value_var"} =~ /^\$/) {
    	\$list_syms{"$the_value_var"} = "unknown";
     }
     \&make_add_menu_strs;
     \$saved_stat = "not saved";
     1;
}
EOS

    eval <<EOS;
sub GS_reset_$the_routine_name {
    \$list_syms{"$the_value_var"} = '';
    1;
}
EOS

    # add to our reset list for later...
    $list_syms{"RESET_LIST"} .=  "&GS_reset_$the_routine_name;";
    1;
}


sub add_reset {
    eval $list_syms{"RESET_LIST"}; # you can see this via dumpv...
    &make_add_menu_strs;
    1;
}


#	init_add_menu_strs - called once when list is started
#
#	we basically want to get a handle on how many custom items there
#	will be in the menu
sub init_add_menu_strs {
    @field_stack = (sort grep(/DATA_[1-9]/, keys %list_syms));
    &make_add_menu_strs;
    1;
}

#	make_add_menu_strs - generate custom menu entries
sub make_add_menu_strs {
    local($which_field) = 0;
    $howmany = $#field_stack;
    if ($#field_stack == -1) { return; }	# sanity check...

    # main key first, then the rest of the data keys...
    $list_syms{"ADD_MENU_STRS"} = sprintf("\t%-8s%-20s\"%s\"\n",
					   $list_syms{"MAIN_KS"},
					   $list_syms{"MAIN"},
					   $list_syms{"MAIN_VAL"});
    
    while ($howmany-- >= 0) {
        $which_field++;
	$list_syms{"ADD_MENU_STRS"} .= sprintf("\t%-8s%-20s\"%s\"\n",
		$list_syms{"DATA_KS$which_field"},
		$list_syms{"DATA_$which_field"},
		$list_syms{"DATA_VAL$which_field"});

    }
    $list_syms{"ADD_MENU_STRS"} .= sprintf("\t%-8s%-20s\"%s\"\n",
			       	   $list_syms{"DESCRIPTION_KS"},
					   $list_syms{"DESCRIPTION"},
					   $list_syms{"DESCRIPTION_STAT"});
    1;
}

#	make_description_input_routine - set up menu entry
sub make_description_input_routine {
    local($the_keystroke) = @_;
    $add_menu_cmds{"$the_keystroke"} = "&description";
    1;
}


#	description - enter package description in $ENV{'EDITOR'}
sub description {
    	local($desc_str) = @_;

	print TTYOUT "$list_syms{'DESCRIPTION'}...";
	&flush;
	open(DESC_FILE, "> $DIRS{'CATALOG'}/description") ||
	    warn "can't open description file...\n";

		print DESC_FILE <<"Instructions";
##      [start your description at the end of this file]
##
##      Note that all lines starting with "##" (such as this one) will
##      *not* be inserted into the record
##
##	if you want to dynamically reference other files at search time
##	specify them at the beginning of a line as:
##
##      $list_syms{"DYNAMIC_REF"}: <dynamic_path>/some_file_specification
##
##	The references can be commands to execute, or files to browse through.
##      You would use this to point at online documentation, file lists, etc.
##
##	%dynamic_paths should be defined in your ~/.SoftListrc.  A sample
##	reference would look like "dynamic: _X11_SRC/GumbyDraw/README"
##
##      uncomment these lines if needed (multiple $list_syms{'DYNAMIC_REF'}: lines are okay!)
##$list_syms{'DYNAMIC_REF'}: _SRC/
##$list_syms{'DYNAMIC_REF'}: _X11_SRC/
##$list_syms{'DYNAMIC_REF'}: _BIN/
##
##      type in whatever is relevant to this record... 
$list_syms{'DESCRIPTION'}:
Instructions
    
    close(DESC_FILE);
    system("$ENV{'EDITOR'} $DIRS{'CATALOG'}/description");
    $descstat = "edited";
    $saved_stat = "not saved";
    $desc_str = `grep -v "^##" $DIRS{'CATALOG'}/description`;
    1;
}
1;



