/* W1.C		Main edit loop 					01 Nov 1987 */

#include <ws.h>

FLAG edit_text()		/* edits text in memory			*/
				/* returns TRUE iff text was altered	*/
				/*  and/or is to be saved on exit	*/
{
	extern INDEX	t_begin, t_end;	/* text begin, end		*/
	extern FLAG	alt_flag;	/* TRUE iff text is changed	*/

	void		init_scr();	/* inits screen for display	*/
	void		exit_scr();	/* cleans up screen before exit	*/
	void		init_ext();	/* inits external variables	*/
	FLAG		to_t_begin();	/* goes to first text page	*/
	void		show_status();	/* shows status line		*/
	unsigned int	GetKey();	/* gets char without echo	*/
	unsigned int	pmpt_key();	/* prompts then GetKey		*/
	FLAG		ctrl_K();	/* processes Ctrl-K commands	*/
	FLAG		ctrl_Q();	/* processes Ctrl-Q commands	*/
	FLAG		non_QK();	/* processes other input	*/

	FLAG		finis;		/* TRUE iff finished w/ editing	*/

	int	ch;			/* console input char		*/

	init_scr();
	init_ext();
	finis = alt_flag = to_t_begin();/* = FALSE since no text change	*/

	do {				/* process input until finis	*/
		show_status();
		switch (ch=GetKey()) {
			case 0x0B:				/* Ctrl-K */
				finis = ctrl_K(ch=pmpt_key(ch));
				break;
			case 0x11:				/* Ctrl-Q */
				alt_flag |= ctrl_Q(ch=pmpt_key(ch));
				break;
			default:
				alt_flag |= non_QK(ch);
				break;
		}
	} while (!finis);
	exit_scr();
	return alt_flag;
}


void init_ext()			/* inits external variables		*/
{
	extern INDEX	t_begin, t_end;		/* text begin, end	*/
	extern unsigned	n_lines;		/* no. of text lines	*/
	extern INDEX	old_index;		/* previous pos in text	*/
	extern INDEX	b_begin, b_end;		/* block begin, end pos	*/
	extern FLAG	blk_flag;		/* TRUE iff block marked */
	extern FLAG	ins_flag;		/* TRUE iff in insert mode */

	static INDEX	i;

	n_lines = 0;
	for (i=t_begin; i<t_end; i++)
		if (*i=='\0') n_lines++;
	if (n_lines==0) n_lines++;
	b_begin = t_begin; 
	b_end = b_begin-1;
	blk_flag = FALSE;
	ins_flag = TRUE;
	old_index = t_begin;
	return;
}


FLAG	ctrl_K(ch)		/* processes Ctrl-K + ch command	*/
int ch;				/* returns TRUE iff to end editing	*/
				/* sets alt_flag iff text gets changed	*/
				/* resets it iff to quit without save	*/
{
	extern FLAG	alt_flag;	/* TRUE iff text has been ch'd	*/
	FLAG		alter;		/* TRUE iff text gets changed	*/

	FLAG		quit_edit();	/* confirm abort command	*/
/*	FLAG		read_block();	/* read text block from disk	*/
	FLAG		writ_block();	/* write text block to disk	*/
	FLAG		mark_word();	/* mark word as block		*/
	FLAG		beg_block();	/* set block begin		*/
	FLAG		end_block();	/* set block end		*/
	FLAG		hide_block();	/* toggle block active/unactive	*/
	FLAG		copy_block();	/* copy block in text		*/
	FLAG		move_block();	/* move block in text		*/
	FLAG		del_block();	/* delete block			*/
*/
	switch (ch) {
		case 0x04: return TRUE;				/* Ctrl-D */
		case 0x11: return quit_edit();			/* Ctrl-Q */
/*		case 0x12: alter=read_block();	break;		/* Ctrl-R */
		case 0x13: alter=writ_block();	break;		/* Ctrl-S */
		case 0x14: alter=mark_word();	break;		/* Ctrl-T */
		case 0x02: alter=beg_block();	break;		/* Ctrl-B */
		case 0x05: alter=end_block();	break;		/* Ctrl-E */
		case 0x08: alter=hide_block(); 	break;		/* Ctrl-H */
		case 0x03: alter=copy_block();	break;		/* Ctrl-C */
		case 0x16: alter=move_block();	break;		/* Ctrl-M */
		case 0x19: alter=del_block();	break;		/* Ctrl-Y */
*/		default:   alter=FALSE;		break;
	}
	alt_flag |= alter;
	return FALSE;
}


FLAG ctrl_Q(ch)			/* processes Ctrl-Q commands		*/
int ch;				/* returns TRUE iff text gets changed	*/
{
	extern INDEX	index;		/* current position in text	*/
	extern INDEX	old_index;	/* previous position in text	*/

/*	FLAG		find_page();	/* flips to page for new index	*/
	FLAG		find();		/* finds text pattern		*/
	FLAG		find_replace();	/* finds and replaces pattern	*/
	FLAG		to_b_begin();	/* goes to begin of block	*/
	FLAG		to_b_end();	/* goes to end of block		*/
*/	FLAG		to_t_begin();	/* goes to begin of text	*/
	FLAG		to_t_end();	/* goes to end of text		*/
	FLAG		to_p_top();	/* goes to top of page		*/
	FLAG		to_p_bottom();	/* goes to bottom of page	*/
	FLAG		to_l_left();	/* goes to begin of line	*/
	FLAG		to_l_right();	/* goes to end of line		*/
	FLAG		del_eol();	/* deletes to end of line	*/
	FLAG		del_bol();	/* deletes to begin of line	*/

	if (ch == 0x10) {				/* Ctrl-P */
		index=old_index;
		return find_page(index);
	}
	old_index=index;
	switch (ch) {
/*		case 0x06: return find();		/* Ctrl-F */
		case 0x01: return find_replace();	/* Ctrl-A */
		case 0x02: return to_b_begin();		/* Ctrl-B */
		case 0x0B: return to_b_end();		/* Ctrl-K */
*/		case 0x12: return to_t_begin();		/* Ctrl-R */
		case 0x03: return to_t_end();		/* Ctrl-C */
		case 0x05: return to_p_top();		/* Ctrl-E */
		case 0x18: return to_p_bottom();	/* Ctrl-X */
		case 0x13: return to_l_left();		/* Ctrl-S */
		case 0x04: return to_l_right();		/* Ctrl-D */
		case 0x19: return del_eol();		/* Ctrl-Y */
		case 0x7F: return del_bol();		/* Delete */
	}
	return FALSE;
}


FLAG non_QK(ch)			/* processes all other inputs		*/
int ch;				/* returns TRUE iff text gets altered	*/
{
	extern INDEX	index, old_index;/* current,previous text index	*/

	FLAG		ins_mode();	/* toggles insert/overwrite mode */
	FLAG		page_up();	/* goes to preceding page	*/
	FLAG		page_down();	/* goes to next page		*/
	FLAG		scr_up();	/* scrolls page up one line	*/
	FLAG		scr_down();	/* scrolls page down one line	*/
	FLAG		line_up();	/* goes one line up		*/
	FLAG		line_down();	/* goes one line down		*/
	FLAG		word_left();	/* goes backward to word begin	*/
	FLAG		word_right();	/* goes forward to next word	*/
	FLAG		char_left();	/* goes to preceding character	*/
	FLAG		char_right();	/* goes to next character	*/
	FLAG		undelete();	/* undeletes last string	*/
	FLAG		del_line();	/* deletes entire line		*/
	FLAG		del_lw();	/* deletes word	to the left	*/
	FLAG		del_rw();	/* deletes word to the right	*/
	FLAG		del_cc();	/* deletes current char		*/
	FLAG		del_lc();	/* deletes char to the left	*/
	FLAG		ins_line();	/* inserts a new line code	*/
	FLAG		new_line();	/* appends or goes to new line	*/
	FLAG		text_char();	/* inserts or overwrites a char	*/
	FLAG		isprint();	/* TRUE iff char is printable	*/

	switch (ch) {
		case 0x16:	return ins_mode();	/* Ctrl-V */
		case 0x0E:	return ins_line();	/* Ctrl-N */
		case 0x15:	return undelete();	/* Ctrl-U */
		case 0x19:	return del_line();	/* Ctrl-Y */
		case 0x1F:	return del_lw();	/* Ctrl-_,Ctrl-Del */
		case 0x14:	return del_rw();	/* Ctrl-T */
		case 0x07:	return del_cc();	/* Ctrl-G */
		case 0x7F:	return del_lc();	/* Delete */
	}
	old_index = index;
	switch (ch) {
		case 0x12:	return page_up();	/* Ctrl-R */
		case 0x03:	return page_down();	/* Ctrl-C */
		case 0x1A:	return scr_up();	/* Ctrl-Z */
		case 0x17:	return scr_down();	/* Ctrl-W */
		case 0x05:	return line_up();	/* Ctrl-E */
		case 0x18:	return line_down();	/* Ctrl-X */
		case 0x01:	return word_left();	/* Ctrl-A */
		case 0x06:	return word_right();	/* Ctrl-F */
		case 0x08:				/* Ctrl-H,Backspace */
		case 0x13:	return char_left();	/* Ctrl-S */
		case 0x04:	return char_right();	/* Ctrl-D */

		case 0x0D:	return new_line();	/* Carriage return */
		case 0x09:	return text_char(ch);	/* Tabulator */
		default:	if (isprint(ch))	/* other */
					return text_char(ch);
				return FALSE;
	}
}


FLAG quit_edit()	/* confirm abort command if text was changed	*/
			/* returns TRUE, resets alt_flag iff confirmed	*/
{
	extern FLAG	alt_flag;	/* TRUE iff text has been changed  */
	FLAG		pmptYN();	/* prompts for TRUE(Yes)/FALSE(No) */

	if (alt_flag) {
		if (pmptYN("Quit without saving",FALSE))
			alt_flag = FALSE;
		else 
			return FALSE;
	}
	return TRUE;
}
