/*
	  ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
	  º															  º
    º    SCHEDULE: Schedule editting mode on/off	  º
	  º															  º
	  ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ¼
*/


#define MONDAY 1
#define TUESDAY 2
#define WEDNESDAY 3
#define THURSDAY 4
#define FRIDAY 5
#define SATURDAY 6
#define SUNDAY 7

#define LEFTMOST 4
#define RIGHTMOST 51
#define RIGHTMOST2 53
#define LEFTMOST2 54
#define RIGHTMOST3 55

int skiptoday(int);
int next_para(int);
int sch_down();
int sch_up();
void sch_home(int);
void sch_end();
void sch_closecomment();

/*
  ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
  ³  The main routine.. assign your hotkey to this  ³
  ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
*/

void schedule()
	{
	int old_insert_mode;
	string filename;
	string mydate,bflags;

	global int whichmode;

/* copy the filename (minus extension) into the file */


	inq_names(NULL,NULL,filename);

	bflags = inq_environment("BFLAGS");

	if (!("%d",get_parm(NULL,mydate,"Date for Schedule (dd-mm-yy): ",8)))
		return;

	if (mydate != "")
		sprintf(filename,"%s.%s",mydate,substr(bflags,index(bflags,"-m")+2,3));

	edit_file(filename);

	top_of_buffer();

	if (!search_fwd("week ending"))
		{
		read_file("template");
			top_of_buffer();
		if (!search_fwd("week ending"))
			{
			error("Invalid template file.");
			return;
			}
		}

	if (index(filename,"."))
		filename = substr(filename,1,index(filename,".")-1);

	old_insert_mode = insert_mode(0);

	move_rel(0,12);
	insert(filename);
	delete_char(strlen(filename));

	message("<Esc> to exit schedule update mode");

	keyboard_push();

	keyboard_typeables();

	register_macro(0,"check_pos");

	assign_to_key("<Esc>","escape");
	assign_to_key("<Home>","sch_home");
	assign_to_key("<End>","sch_end");
	assign_to_key("<Up>","sch_up");
	assign_to_key("<Down>","sch_down");
	assign_to_key("<Left>","sch_left");
	assign_to_key("<Right>","sch_right");
	assign_to_key("<Enter>","ins_newline");
	assign_to_key("<Keypad-Enter>","ins_newline");
	assign_to_key("<Del>","sch_delchar");
	assign_to_key("<Backspace>","sch_backspace");
	assign_to_key("<Keypad-Minus>","sch_delline");
	assign_to_key("<Alt-d>","sch_delline");
	assign_to_key("<F4>","sch_closecommentall");
	assign_to_key("<F3>","sch_newcomment");
	assign_to_key("<Tab>","toggle_mode");

	assign_to_key("<Alt-1>","sch_day1");
	assign_to_key("<Alt-2>","sch_day2");
	assign_to_key("<Alt-3>","sch_day3");
	assign_to_key("<Alt-4>","sch_day4");
	assign_to_key("<Alt-5>","sch_day5");
	assign_to_key("<Alt-6>","sch_day6");
	assign_to_key("<Alt-7>","sch_day7");

	if (skiptoday(MONDAY))
		{
		refresh();
		process();
		}

	message(" ");

	unregister_macro(0,"check_pos");
	insert_mode(old_insert_mode);

	keyboard_pop();

	}

void toggle_mode()
	{
	int line;
	if (whichmode == 0)
		{
		whichmode = 1;
		save_position();
		inq_position(line);
		move_abs(line,LEFTMOST2);
		}
	else
		{
		whichmode = 0;
		restore_position();
		}
	}

string getchar(int line,int col)
	{
	string c;
	save_position();
	move_abs(line,col);
	c = read(1);
	restore_position();

	return c;
	}

int valid_line(int line)
	{
	string c;

	save_position();

	move_abs(line,RIGHTMOST2);

	c = read(1);

	restore_position();

	return (c == "³");
	}

/*
  ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
  ³  Insert new comment line    ³
  ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
*/


void ins_newline()
	{
	if (whichmode) return;

	move_rel(1,0);
	beginning_of_line();
	
	insert("º                                                   ³  º\n");

	move_rel(-1,LEFTMOST-1);

	}


/*
  ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
  ³  Keep in check the cursor position  ³
  ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
*/

void check_pos()
	{
	int line,col;

	inq_position(line,col);

	if (whichmode)
		{
		if (col<LEFTMOST2)
			{
			move_abs(line,LEFTMOST2);
			}
		if (col>RIGHTMOST3)
			{
			move_abs(line,LEFTMOST2);
			}
		}
	else
		{

		if (col<LEFTMOST)
			{
			move_abs(line,LEFTMOST);
			sch_up();
			}
		else
			{

			if (col>RIGHTMOST)
				{
				move_abs(line+1,LEFTMOST);
				if (read(1) == "Ä")
					{
					move_abs(line,col);
					ins_newline();
					}
				else
					{
					move_abs(line,col);
					sch_home(0);
					sch_down();
					}
				}
			}
		}
	}



/*
  ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
  ³  Move the cursor up a line  ³
  ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
*/

int sch_up()
	{
	int line,col,oldline;

	inq_position(line,col);
	oldline = line;

	while (line>1)
		{
		if (valid_line(--line))
			{
			move_abs(line,col);
			return 1;
			}
		}
	move_abs(oldline,col);
	return 0;
	}


/*
  ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
  ³  Move the cursor down a line  ³
  ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
*/

int sch_down()
	{
	int line,col,oldline,lastline;

	inq_position(line,col);
	oldline = line;

	end_of_buffer();
	inq_position(lastline);

	while (line<lastline)
		{
		if (valid_line(++line))
			{
			move_abs(line,col);
			return 1;
			}
		}
	move_abs(oldline,col);
	return 0;
	}


/*
  ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
  ³  Move the cursor right 1 char  ³
  ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
*/

void sch_right()
	{
	int line,col;

	if (whichmode) return;

	move_rel(0,1);

	inq_position(line,col);

	if (col>RIGHTMOST)
		{
		sch_home(0);
		sch_down();
		}
	}

/*
  ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
  ³  Move the cursor left 1 char   ³
  ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
*/


void sch_left()
	{
	int line,col;

	if (whichmode) return;

	move_rel(0,-1);

	inq_position(line,col);

	if (col<LEFTMOST)
		{
		sch_end();
		sch_up();
		}
	}


/*
  ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
  ³  Delete the character under the cursor       ³
  ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
*/

void sch_delchar()
	{
	int line,col;

	if (whichmode) return;

	inq_position(line,col);

	delete_char();

	move_abs(line,RIGHTMOST);
	insert(" ");

	move_abs(line,col);
	
	}


/*
  ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
  ³  Delete the character left of the cursor     ³
  ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
*/

void sch_backspace()
	{

	sch_left();
	delete_char();
	insert(" ");
	sch_left();

	}


/*
  ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
  ³  Delete line at current cursor position      ³
  ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
*/

int sch_delline()
	{
	int line,col,n;

	if (whichmode) return 0;

	inq_position(line);

	if (valid_line(line))
		{
		if (valid_line(line+1) || valid_line(line-1))
			{
			delete_line();
			if (!valid_line(line)) sch_up();
			message("Line deleted.");
			return 1;
			}
		else
			{
			move_abs(line,RIGHTMOST+1);
			for (n=RIGHTMOST;n>=LEFTMOST;n--)
				sch_backspace();
			sch_closecomment();
			return 0;
			}
		}

	return 0;
	}


/*
  ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
  ³  Close comment at the current position       ³
  ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
*/

void sch_closecommentall()
	{
	while (sch_delline()) ;

	}

void sch_closecomment()
	{
	int line,col;

	inq_position(line,col);

	if (!valid_line(line))
		{
		message("Cannot close this comment.");
		return;
		}

	if (getchar(line+2,LEFTMOST) != "Í")
		{
		delete_line();
		delete_line();
		}
	else
		{
		if (getchar(line-1,RIGHTMOST2) != "Â")
			{
			up();
			delete_line();
			delete_line();
			sch_up();
			}
		else
			{
			message("Cannot close the last comment.");
			return;
			}
		}

	}

/*
  ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
  ³  Insert new comment at current position      ³
  ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
*/

void sch_newcomment()
	{
	if (whichmode) return;

	move_rel(1,0);
	beginning_of_line();
	
	insert("ÇÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄ¶\n");

	move_rel(-1,LEFTMOST-1);

	ins_newline();

	}


/*
  ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
  ³  Home the cursor into the left-most column  ³
  ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
*/

void sch_home(int noprev)
	{
	int line,col;

	if (whichmode) return;

	inq_position(line,col);

	if (col==LEFTMOST && noprev==0) next_para(-1);
	else move_abs(line,LEFTMOST);
	}


/*
  ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
  ³  Move the cursor to the right-most column   ³
  ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
*/

void sch_end()
	{
	int line,col,oldline,lastline;

	if (whichmode) return;

	inq_position(line,col);

	if (col==RIGHTMOST)
		{
		oldline = line;
		end_of_buffer();
		inq_position(lastline);

		while (line++<lastline)
			{
			move_abs(line,RIGHTMOST);
			if (read(1) == "Ä")
				{
				move_abs(line-1,RIGHTMOST);
				if (line-1 == oldline)
					next_para(0);
				return;
				}
			}
		move_abs(oldline,RIGHTMOST);
		
		}
	else move_abs(line,RIGHTMOST);
	}


/*
  ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
  ³  Move the cursor down to the next paragraph  ³
  ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
*/

int next_para(int whichway)
	{
	int line,col;
	int lastline,oldline;

	if (whichmode) return 0;

	inq_position(line,col);

	if (!whichway) whichway = 1;
	else whichway = -1;
	oldline = line;

	end_of_buffer();

	inq_position(lastline);

	while (line<lastline && line >=1)
		{
		move_abs(line,1);
		if (read(1) == "Ç")
			{
			move_abs(line+whichway,RIGHTMOST2);
			if (read(1) == "³")
				{
				move_abs(line+whichway,col);
				return 1;
				}
			}
		line += whichway;
		}
	move_abs(oldline,col);
	return 0;
	}




/*
  ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
  ³  Code to jump to different days of the week  ³
  ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
*/

void sch_day1()
	{
	skiptoday(MONDAY);
	}

void sch_day2()
	{
	skiptoday(TUESDAY);
	}

void sch_day3()
	{
	skiptoday(WEDNESDAY);
	}

void sch_day4()
	{
	skiptoday(THURSDAY);
	}

void sch_day5()
	{
	skiptoday(FRIDAY);
	}

void sch_day6()
	{
	skiptoday(SATURDAY);
	}

void sch_day7()
	{
	skiptoday(SUNDAY);
	}



/*
  ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
  ³  Find the box for a particular day, returns FALSE if doesn't exist  ³
  ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
*/

int skiptoday(int whichday)
	{
	int line,col;
	string daystring = "µ Monday    µ Tuesday   µ Wednesday µ Thursday  µ Friday    µ Saturday  µ Sunday    ";

	if (whichmode)
		{
		whichmode = 0;
		restore_position();
		}

	inq_position(line,col);
	top_of_buffer();
	if (search_fwd(trim(substr(daystring,(whichday-1)*12+1,12))))
		{
		sch_down();
		inq_position(line,col);
		move_abs(line,LEFTMOST);
		return 1;
		}
	else
		{
		move_abs(line,col);
		message("Cannot find %s",substr(daystring,(whichday-1)*12+3,10));
		return 0;
		}
	}


escape()
	{
	exit();
	}


