/* Code was created by Amras of Hexahedron
 * hexahedron.genesismuds.com port 1414
 * Purpose of this code is to format the show command
 * on mprogs, to be more like a design enviroment.
 * Copy code to bottem of olc_mprog.c and replace
 * line in show command of mpedit_show with a call
 * to the function:

format_code_output(ch, pMcode->code);

 * This code is free to use as long as I get some kind of credit,
 * and this header is included in the code. :)
 */

void print_tabs(CHAR_DATA * ch, int tabs);
char *str_replace(char *seed, char *apple, char *tree);
extern const char * fn_keyword[];
extern const char * fn_evals[];
void format_code_output(CHAR_DATA * ch, const char * code)
{
	int tabs = 0, x = 0, placeholder = 0;
	char buf[MSL];
	char buf2[MIL];
	char buf3[20];

	while(code[placeholder] != '\0')
	{
		int i;
		//Reset the buffer to all NULL bits
		for(i = 0; i < MSL; i++) 
			buf[i] = '\0';

		//Copy the string until we hit a new line
		for(i = 0; code[placeholder] != '\n' 
			&& code[placeholder] != '\r'  
			&& code[placeholder] != '\0'; i++)
		{
			buf[i] = code[placeholder];
			placeholder++;
		}
		//Re-Add the end-line to the string we copied
		buf[i] = '\n';
		buf[i+1] = '\r';

		//Clear any whitespace
		while(code[placeholder] == '\n' 
			|| code[placeholder] == '\r' 
			|| code[placeholder] == ' '
			|| code[placeholder] == '\t')
			placeholder++;

		//Start the formatting and coloring
		if(!str_prefix("if", buf))
		{
			str_replace("if", "{Cif{x", buf);
			print_tabs(ch, tabs);
			for(x = 0; fn_keyword[x][0] != '\n'; x++)
			{
				if(str_infix(fn_keyword[x], buf))
					continue;
				{
					char buf3[20];
					sprintf(buf3, " %s ", fn_keyword[x]);
					sprintf(buf2, " {Y%s{x ", fn_keyword[x]);
					str_replace(buf3, buf2, buf);
				}
			}
			for(x = 0; fn_evals[x][0] != '\n'; x++)
			{
				if(str_infix(fn_evals[x], buf))
					continue;
				sprintf(buf3, "%s", fn_evals[x]);
				sprintf(buf2, "{R%s{x", fn_evals[x]);
				str_replace(buf3, buf2, buf);
			}
			send_to_char(buf, ch);
			tabs++;
		}
		else if(!str_prefix("end if", buf))
		{
			tabs--;
			str_replace("end if", "{Cend if{x", buf);
			print_tabs(ch, tabs);
			send_to_char(buf, ch);

		}
		else if(!str_prefix("endif", buf))
		{
			tabs--;
			str_replace("endif", "{Cendif{x", buf);
			print_tabs(ch, tabs);
			send_to_char(buf, ch);

		}
		else if(!str_prefix("else", buf))
		{
			tabs--;
			str_replace("else", "{Celse{x", buf);
			print_tabs(ch, tabs);
			send_to_char(buf, ch);
			tabs++;
		}
		else
		{
			print_tabs(ch, tabs);
			for(x = 0; fn_keyword[x][0] != '\n'; x++)
			{
				if(str_infix(fn_keyword[x], buf))
					continue;

				sprintf(buf3, " %s ", fn_keyword[x]);
				sprintf(buf2, " {Y%s{x ", fn_keyword[x]);
				str_replace(buf3, buf2, buf);
			}
			for(x = 0; fn_evals[x][0] != '\n'; x++)
			{
				if(str_infix(fn_evals[x], buf))
					continue;

				sprintf(buf3, "%s", fn_evals[x]);
				sprintf(buf2, "{R%s{x", fn_evals[x]);
				str_replace(buf3, buf2, buf);
			}
			send_to_char(buf, ch);
		}
		//Lets double check our math
		if(tabs < 0) 
			tabs = 0;
	}
}
void print_tabs(CHAR_DATA * ch, int tabs)
{
	while(tabs > 0)
	{
		tabs--;
		send_to_char("   ", ch);
	}
}