/**********************************************************************/
/**                                                                  **/
/** Compiles a list of subroutines into a brief menu (ARGSFX compat.)**/
/**                                                                  **/
/**********************************************************************/

#include <dialog.h>

int	_r_line,_r_buf;
string PickBuff;

void r65816()
	{
	int old_buf_id,menu_buf,loc;
	int x,y,line,num_lines,num_cols,max_width = 0,loc2;
	int lastbuf=0,max_width2=0,firsttime;
	string routine,check,routine_name;
	string BuffExt,BuffName;

	save_position();

	_r_line = 0;
	old_buf_id = inq_buffer();
	menu_buf = create_buffer ("Routines", NULL, 1);

	message("Scanning for routines...");

	while (!lastbuf)
		{
		lastbuf = inq_buffer();
		top_of_buffer();
		firsttime = 1;
		while (search_fwd("<[~ \t;\n.*]"))
			{
			routine = trim(read());
			loc = strlen(routine);
			if (loc > 1)
				{
				inq_position(line);
				check = "";

				if (loc = search_string("[{ }|{\t}]",routine))
					{
					routine_name = trim(substr(routine,1,loc));

					if (x = search_string("[~ \t]",substr(routine,loc)))
						{
						if (loc2 = search_string("[{ }|{\t}]",substr(routine,loc+x)))
							check = substr(routine,loc+x-1,loc2);
						else check = substr(routine,loc+x-1);
						}
					}
				else routine_name = routine;

				if ((check != "EQU") && (check != "="))
					{
					inq_names(NULL,BuffExt,BuffName);

					if (strlen (BuffName) > max_width2)
						max_width2 = strlen (BuffName);

					if (strlen (routine_name)+strlen(BuffName)+3 > max_width)
						max_width = strlen (routine_name)+strlen(BuffName)+3;

					if (firsttime)
						{
						firsttime = 0;
						BuffName = upper(BuffName);
						sprintf(BuffName,"%s:",BuffName);
						}
					else BuffName = "";
	
					sprintf(routine_name,"\n%s\t%s",BuffName,routine_name);

					set_buffer (menu_buf);
					insert ("%s\t%d,%d;", routine_name,line,lastbuf);
					set_buffer (lastbuf);
					}
			
				}
			next_char();
			}
		set_buffer(lastbuf = next_buffer());

		if (lastbuf == old_buf_id)
			lastbuf = 1;
		else lastbuf = 0;
			
		}


	restore_position();

	set_buffer(menu_buf);
	inq_position(line);
	top_of_buffer();

	if (line>1)
		{
		delete_line();

		inq_screen_size (num_lines, num_cols);
		tabs (max_width2+3,num_cols);

		if ((line += 3) > (num_lines - 5))
			line = num_lines - 5;

		if ((max_width += 2) < 15)
			max_width = 15;

		if (max_width >= num_cols - 3)
			max_width = num_cols - 3;

		max_width = (max_width + 1) / 2;
		num_cols = (num_cols + 1 ) / 2;

		set_buffer (old_buf_id);
		_process_menu (num_cols - max_width, line, num_cols + max_width, 3, NULL, "", NULL, menu_buf, "_r_action", TRUE);

		if (_r_line)
			{
			edit_file(PickBuff);
			goto_line (_r_line);
			}

		
		}
	else
		{
		set_buffer (old_buf_id);
		delete_buffer (menu_buf);
		message ("No routines found.");
		}

	}


/*
**		_r_action:
**
**		This routine is called by the dialog manager when a routine is
**	selected.
*/

int _r_action (int event_type, ...)
{
	string button_text;

	get_parm (0, event_type);


	switch (event_type)
		{
		case DIALOG_PICK_MENU:
		case DIALOG_F10:
			{
			get_parm (2, button_text);
			_r_line = atoi (substr (button_text, rindex (button_text, "\t") + 1));
			_r_buf = set_buffer(atoi (substr(button_text,rindex (button_text, ",")+1)));
			inq_names(PickBuff,NULL,NULL);
			_dialog_esc ();
			}
		}
	returns (TRUE);
}

