/*
 * STEVIE - Simply Try this Editor for VI Enthusiasts
 *
 * Code Contributions By : Tim Thompson           twitch!tjt
 *                         Tony Andrews           onecom!wldrdg!tony 
 *                         G. R. (Fred) Walter    watmath!watcgl!grwalter 
 */

#include "stevie.h"

/*
 * updateRealscreen 
 *
 * Transfer the contents of Nextscreen to the screen, using Realscreen to avoid
 * unnecessary output. 
 */
void
updateRealscreen()
{
    register char  *np = Nextscreen;
    register char  *rp = Realscreen;
    register char  *endscreen;
    int             row = 0, col = 0;
    int             gorow = -1, gocol = -1;

    if (RedrawingDisabled)
	return;

    if (!MustRedrawScreen && !MustRedrawLine)
	return;

    if (MustRedrawLine) {
	msg("STEVIE internal error: updateRealscreen called");
	sleep(5);
    }
    endscreen = &np[(Rows - 1) * Columns];

    outstr(T_CI);		/* disable cursor */

    for (; np < endscreen; np++, rp++) {
	/* If desired screen (contents of Nextscreen) does not */
	/* match what's really there, put it there. */
	if (*np != *rp) {
	    /* if we are positioned at the right place, */
	    /* we don't have to use windgoto(). */
	    if (gocol != col || gorow != row) {
		/*
		 * If we're just off by one, don't send an entire esc. seq.
		 * (this happens a lot!) 
		 */
		if (gorow == row && gocol + 1 == col) {
		    outchar(*(np - 1));
		    gocol++;
		} else
		    windgoto(gorow = row, gocol = col);
	    }
	    outchar(*rp = *np);
	    gocol++;
	}
	if (++col >= Columns) {
	    col = 0;
	    row++;
	}
    }
    outstr(T_CV);		/* enable cursor again */

    MustRedrawLine = FALSE;
    MustRedrawScreen = FALSE;
}
