/************************************************************************
* "Epsilon", "EEL" and "Lugaru" are trademarks of Lugaru Software, Ltd. *
*									*
*  Copyright (C) 1985, 1990 Lugaru Software Ltd.  All rights reserved.	*
*									*
* Limited permission is hereby granted to reproduce and modify this	*
* copyrighted material provided that the resulting code is used only in	*
* conjunction with Lugaru products and that this notice is retained in	*
* any such reproduction or modification.				*
*************************************************************************
*  The previous copyright and trademark notice applies to some of the	*
*  code herein. All other changes, enhancements and extension routines	*
*									*
*  Copyright (C) 1989, 1990 by Jonas H. Hedberg. All rights reserved.   *
*									*
*  These changes may be redistributed free allowed for any purpose	*
*  providing both the Lugaru and my copyrights remain intact.		*
*  I assume no liability for the use or inability to use		*
*  this software. Neither do I take responsibility for any damage	*
*  cased by this software extension.					*
*  This extension may NOT be redistributed for profit.			*
*************************************************************************
* File.......: CURSPOS.E                                                *
* Ver........: 1.04                                                     *
* Date.......: 1990-08-14                                               *
* īpsilon ver: 5.0                                                      *
* By.........: Jonas Hedberg                                            *
*     Title..: Software Engineer                                        *
*     Company: TEAMSTER AB                                              *
*     Address: P.O. Box 8321                                            *
*              S-402 79  GTEBORG                                       *
*     Country: SWEDEN                                                   *
*     Tel....: Int +46-31-22 22 65  (GMT + 1h)                          *
*     Fax....: Int +46-31-22 75 46                                      *
* Description: Show's current row and column at right bottom of the     *
*              screen, undepending if the screen is 25, 43 or 50 rows.  *
*              The function check if the screen is in color             *
*              or mono mode, and it's use the echo-line color.          *
*              The function is controled by the variable:               *
*              show-cursor-position. When it's "1" the current row      *
*              and column appears on the screen, and if it's "0",       *
*              nothing happends. The cursor-position-mode command can   *
*              be used to toggle between showing and not.               *
* Algorithm..: Remember where you were last time, start from there and  *
*              count your way forward or backword depending were you    *
*              move to.                                                 *
* Comments...:                                                          *
* Change log                                                            *
* Date      | Ver   | Change                                            *
* ----------+-------+-------------------------------------------------- *
* 15-Aug-90 | 1.01  | Added the "bazurk" test.                          *
* 15-Aug-90 | 1.02  | Command cursor-position-mode.                     *
* 11-Sep-90 | 1.03  | Add som more "AI" to the function current_row().  *
* 27-Sep-90 | 1.04  | Check the shortest way to count in current_row(). *
*           |       |                                                   *
*           |       |                                                   *
*           |       |                                                   *
************************************************************************/


#include "eel.h"
#include "lowlevel.h"

char show_cursor_position = 1;
buffer int _prev_point = 0;
buffer int _prev_row = 1;

command cursor_position_mode()
{
	if (show_cursor_position == 0) {
		show_cursor_position = 1;
	} else {
		show_cursor_position = 0;
	}
}

do_show_cursor_position()
{
	if (show_cursor_position == 1) {
		char cursor_position[25];
		sprintf(cursor_position, "   r%d c%d         ", current_row(), current_column() + 1);
		/* Check if screen is mono or color */
		if (screen_mode == 0 || screen_mode == 2 || screen_mode == 7)
			/* The screen is in monochrom mode */
			term_write(screen_cols-15, screen_lines-1,cursor_position,15,mcolors[1],1);
		else
			/* Screen is in color mode */
			term_write(screen_cols-15, screen_lines-1,cursor_position,15,colors[1],1);
	}
}

current_column()
{
	int start, column;

	start=point;
	if (nl_reverse())
		point++;
	column = horizontal(start);
	point = start;
	return column;
}

current_row()
{
	/* If current row has gone bazurk, fix it */
	if ((_prev_point > size()) || 
	   (_prev_row <= 0) || 
	   ((_prev_point == 0) && (_prev_row != 1))) {
		int row = 1, start = point;
		point = 0;
		while (nl_forward() && !user_abort && (point <= start)) {
			row++;
		}
		goto Ready;
	} else {
		int row = _prev_row, start = point;

		/* Check if point is top of buffer */
		if (point == 0) {
			row = 1;
			goto Ready;
		}

		point = _prev_point;
		
		/* Check witch is the shortest way to count the current row */
		if (point < (_prev_point - point)) {
			point = 0;
			while (nl_forward() && !user_abort && (point <= start)) {
				row++;
			}
			goto Ready;
		}

		/* The move has been forward since last time */
		if (start > _prev_point) {
			while (nl_forward() && !user_abort && (point <= start)) {
				row++;
			}
		/* Whe have moved backward in the buffer */ 	
		} else if (start < _prev_point) {
			while (nl_reverse() && !user_abort && (point >= start)) {
				row--;
			}
		}
Ready:
		point = start;
		check_abort();
		if (!row)
			row = row + 1;
		/* Remember where we are */
		_prev_point = point;
		_prev_row = row;
		return row;
	}
}

new_curspos_getkey()
{
	do_show_cursor_position();	/* Print row and col */

	return curspos_getkey();
}

when_loading()
{
	sayput("CURSOR POSITION-module  Ver 1.04  (C) 1990 by Jonas Hedberg. Loading...");

	/* Change Epsilon's getkey() */
	if (!find_index("curspos_getkey"))
		replace_name("getkey", "curspos_getkey");
	else
		drop_name("getkey");
	replace_name("new_curspos_getkey", "getkey");

	delay(300, COND_KEY);
}
            
