/* W6.C		Cursor movement					01 Nov 1987 */

#include <ws.h>


FLAG char_left()		/* goes to the preceding character	*/
				/* returns FALSE since no text change	*/
{
	extern INDEX	t_begin;	/* text begin			*/
	extern INDEX	index;		/* current text position	*/
	extern unsigned	cx;		/* current cursor x position	*/
	extern unsigned	pt_line;	/* page top line		*/

	unsigned	line();		/* current text line		*/
	void		down_scroll();	/* scrolls text page down	*/
	unsigned	set_xy();	/* sets cursor coordinates	*/

	if (index<=t_begin) return FALSE;
	if (*--index)
		cx -= dx(index);
	else {
		if (line(index)<pt_line)
			down_scroll(1);
		set_xy();
	}
	return FALSE;
}


FLAG char_right()		/* goes to the next character		*/
				/* returns FALSE since no text change	*/
{
	extern INDEX	t_end;		/* text end			*/
	extern INDEX	index;		/* current text position	*/
	extern unsigned	cx;		/* current cursor x position	*/
	extern unsigned	pb_line;	/* page bottom line		*/

	unsigned	line();		/* current text line		*/
	void		up_scroll();	/* scrolls text page up		*/
	unsigned	set_xy();	/* sets cursor coordinates	*/

	if (index>=t_end) return FALSE;
	if (*index)
		cx += dx(index++);
	else {
		if (line(index++)==pb_line)
			up_scroll(1);
		set_xy();
	}
	return FALSE;
}


FLAG word_left()	/* goes backward to begin of word		*/
			/* returns FALSE since no text change		*/
{
	extern INDEX	index;		/* current text position	*/
	extern INDEX	t_begin;	/* text begin			*/

	void		find_page();	/* finds page for index		*/
	void		set_xy();	/* sets cursor coordinates	*/

	if (index<=t_begin) return FALSE;
	if (!isalnum(*index) || !isalnum(*--index))
		while (--index>t_begin && !isalnum(*index)) ;
	while (--index>=t_begin && isalnum(*index)) ;
	find_page(++index);
	set_xy();
	return FALSE;
}


FLAG word_right()	/* goes forward to begin of word		*/
			/* returns FALSE since no text change		*/
{
	extern INDEX	index;		/* current text position	*/
	extern INDEX	t_end;		/* text end			*/

	void		find_page();	/* finds page for index		*/
	void		set_xy();	/* sets cursor coordinates	*/

	while (index<t_end && isalnum(*index++)) ;
	while (index<t_end && !isalnum(*index++)) ;
	find_page(index);
	set_xy();
	return FALSE;
}


FLAG to_l_left()	/* goes to begin of line			*/
			/* returns FALSE since no text change		*/
{
	extern INDEX	index;		/* current text position	*/

	INDEX		bol_index();	/* index at begin of line	*/
	void		set_x();	/* sets cursor x coordinate	*/

	index = bol_index(index);
	set_x();
	return FALSE;
}


FLAG to_l_right()	/* goes to end of line			*/
			/* returns FALSE since no text change		*/
{
	extern INDEX	index;		/* current text position	*/

	INDEX		eol_index();	/* index at end of line		*/
	void		set_x();	/* sets cursor x coordinate	*/

	index = eol_index(index);
	set_x();
	return FALSE;
}


FLAG line_up()		/* goes to char one line above current char	*/
			/* returns FALSE since no text change		*/
{
	extern INDEX	index;		/* current text position	*/
	extern unsigned	pt_line;	/* page top line		*/

	unsigned	line();		/* current text line		*/
	void		down_scroll();	/* scroll text page down	*/
	INDEX		above();	/* index of char above current 	*/
	void		set_xy();	/* sets cursor coordinates	*/

	if (line(index)==pt_line) {
		if (pt_line==1) return FALSE;
		down_scroll(1);
	}
	index=above(index,1);
	set_xy();
	return FALSE;
}


FLAG line_down()	/* goes to char one line below current char	*/
			/* returns FALSE since no text change		*/
{
	extern INDEX	index;		/* current text position	*/
	extern unsigned	pb_line;	/* page bottom line		*/
	extern unsigned	n_lines;	/* no. of text lines		*/

	unsigned	line();		/* current text line		*/
	void		up_scroll();	/* scroll text page up		*/
	INDEX		below();	/* index of char below current 	*/
	void		set_xy();	/* sets cursor coordinates	*/

	if (line(index)==pb_line) {
		if (pb_line>n_lines) return FALSE;
		up_scroll(1);
	}
	index=below(index,1);
	set_xy();
	return FALSE;
}


FLAG to_p_top()		/* goes to top of screen page			*/
			/* returns FALSE since no text change		*/
{
	extern INDEX	index;		/* current text position	*/
	extern unsigned pt_line;	/* page top line		*/

	INDEX		above();	/* index above current index	*/
	unsigned	line();		/* current text line		*/
	void		set_xy();	/* sets cursor coordinates	*/

	index = above(index, line(index)-pt_line);
	set_xy();
	return FALSE;
}


FLAG to_p_bottom()	/* goes to bottom of screen page		*/
			/* returns FALSE since no text change		*/
{
	extern INDEX	index;		/* current text position	*/
	extern unsigned pb_line;	/* page bottom line		*/

	INDEX		below();	/* index below current index	*/
	unsigned	line();		/* current text line		*/
	void		set_xy();	/* sets cursor coordinates	*/

	index = below(index, pb_line-line(index));
	set_xy();
	return FALSE;
}
