


global int glob_x;
global int ced_onoff;

extern compile_it();

/* switch_it
 * this switches the ced compatible editting off or on, depending on the current mode
 */

void switch_it(...) {

	ced_onoff = !ced_onoff;

	if (ced_onoff) {
		assign_to_key("<Left>","left");
		assign_to_key("<Right>","right");
		assign_to_key("<Up>","up");
		assign_to_key("<Down>","down");
		}
	else {
		assign_to_key("<Left>","myleft");
		assign_to_key("<Right>","myright");
		assign_to_key("<Up>","_myup");
		assign_to_key("<Down>","_mydown");
		}

	return;
	}

/* myleft
 * this is to replace the standard left
 */

void myleft(...) {

	prev_char();

	return;
	}

void myright(...) {

	next_char();

	return;
	}

/* newdown
 * this is to detect continuation of down key
 */

void newdown() {
	assign_to_key("<Down>","_mydown");
	}

/* newup
 * this is to detect continuation of up key
 */

void newup() {
	assign_to_key("<Up>","_myup");
	}


/* _mydown
 * this is to replace the standard down
 */

void _mydown(...) {
	string fred,jim;
	int n,l,c;

	down();
	if (inq_position(l,c)) up();

	jim = inq_command();
	if (jim != "newdown" && jim != "newup") glob_x = c;
	assign_to_key("<Down>","newdown");
	push_back (key_to_int("<Down>"));

	fred = read(1);
	n = atoi(fred,0);

	if (n==13) {
		end_of_line();
		inq_position(l,c);
		if (c>glob_x) move_abs(l,glob_x);
		}
	else {
		if (glob_x==0) glob_x = c;
		else {
			move_abs(l,glob_x);
			fred = read(1);
			n = atoi(fred,0);
			if (n==13) end_of_line();
			if (n==9) {
				prev_char();
				next_char();
				}
			}
		}

	return;
	}



/* _myup
 * this is to replace the standard down
 */

void _myup(...) {
	string fred,jim;
	int n,l,c;

	up();
	inq_position(l,c);
	jim = inq_command();

	if (jim != "newup" && jim != "newdown") glob_x = c;
	assign_to_key("<Up>","newup");
	push_back (key_to_int("<Up>"));

	fred = read(1);
	n = atoi(fred,0);

	if (n==13) {
		end_of_line();
		inq_position(l,c);
		if (c>glob_x) move_abs(l,glob_x);
		}
	else {
		if (glob_x==0) glob_x = c;
		else {
			move_abs(l,glob_x);
			fred = read(1);
			n = atoi(fred,0);
			if (n==13) end_of_line();
			if (n==9) {
				prev_char();
				next_char();
				}
			}
		}

	return;
	}

/* mydel
 * this is to replace the standard backspace
 */

void mydel(...) {

	prev_char();
	delete_char();

	return;
	}




/*
** 	comment_block:
**
** 	This macro comments out a block of code.
**		by putting semi-colons in front of every line.
*/
void comment_block (void)
{
	int	start_line,
			start_col,
			end_line,
			mark_type;

	if (mark_type = inq_marked (start_line, start_col, end_line))
		{
		int	num_lines;

		save_position ();

		if (mark_type != COL_MARK)
			start_col = 1;

		move_abs (start_line, start_col);

		while (start_line <= end_line)
			{
			insert (";");
			move_abs (++start_line, start_col);
			}
		restore_position ();
		}
	else
		error ("No marked block.");
}


/*
** 	uncomment_block:
**
*/
void uncomment_block (void)
{
	int	start_line,
			start_col,
			end_line,
			curr_char,
			mark_type;

	if (mark_type = inq_marked (start_line, start_col, end_line))
		{
		int	num_lines;

		save_position ();

		if (mark_type != COL_MARK)
			start_col = 1;

		move_abs (start_line, start_col);

		while (start_line <= end_line)
			{
				if (read (1) ==";")
				{
				delete_char ();
				}
				move_abs (++start_line, start_col);
			}
		restore_position ();
		}
	else
		error ("No marked block.");
}

my_compile_it() {
	int curr_buff,next_buff;
	message ("Writing out all buffers...");

	curr_buff = inq_buffer();
		do {
			if (inq_modified()) write_buffer();
			next_buff = next_buffer();
			set_buffer(next_buff);
			} while (next_buff!=curr_buff);

	compile_it();
}

