⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 screen.c

📁 STEVIE文本文件编缉器的C 语言源程序
💻 C
📖 第 1 页 / 共 2 页
字号:
/* $Header: /nw2/tony/src/stevie/src/RCS/screen.c,v 1.9 89/08/31 09:59:24 tony Exp $
 *
 * Routines to manipulate the screen representations.
 */

#include "stevie.h"

/*
 * This gets set if we ignored an update request while input was pending.
 * We check this when the input is drained to see if the screen should be
 * updated.
 */
bool_t	need_redraw = FALSE;

/*
 * The following variable is set (in filetonext) to the number of physical
 * lines taken by the line the cursor is on. We use this to avoid extra
 * calls to plines(). The optimized routines lfiletonext() and lnexttoscreen()
 * make sure that the size of the cursor line hasn't changed. If so, lines
 * below the cursor will move up or down and we need to call the routines
 * filetonext() and nexttoscreen() to examine the entire screen.
 */
static	int	Cline_size;	/* size (in rows) of the cursor line */
static	int	Cline_row;	/* starting row of the cursor line */

static	char	*mkline();	/* calculate line string for "number" mode */

/*
 * filetonext()
 *
 * Based on the current value of Topchar, transfer a screenfull of
 * stuff from Filemem to Nextscreen, and update Botchar.
 */

static void
filetonext()
{
	register int	row, col;
	register char	*screenp = Nextscreen;
	LPTR	memp;
	LPTR	save;			/* save pos. in case line won't fit */
	register char	*endscreen;
	register char	*nextrow;
	char	extra[16];
	int	nextra = 0;
	register int	c;
	int	n;
	bool_t	done;		/* if TRUE, we hit the end of the file */
	bool_t	didline;	/* if TRUE, we finished the last line */
	int	srow;		/* starting row of the current line */
	int	lno;		/* number of the line we're doing */
	int	coff;		/* column offset */

	coff = P(P_NU) ? 8 : 0;

	save = memp = *Topchar;

	if (P(P_NU))
		lno = cntllines(Filemem, Topchar);

	/*
	 * The number of rows shown is Rows-1.
	 * The last line is the status/command line.
	 */
	endscreen = &screenp[(Rows-1)*Columns];

	done = didline = FALSE;
	srow = row = col = 0;
	/*
	 * We go one past the end of the screen so we can find out if the
	 * last line fit on the screen or not.
	 */
	while ( screenp <= endscreen && !done) {


		if (P(P_NU) && col == 0 && memp.index == 0) {
			strcpy(extra, mkline(lno++));
			nextra = 8;
		}

		/* Get the next character to put on the screen. */

		/* The 'extra' array contains the extra stuff that is */
		/* inserted to represent special characters (tabs, and */
		/* other non-printable stuff.  The order in the 'extra' */
		/* array is reversed. */

		if ( nextra > 0 )
			c = extra[--nextra];
		else {
			c = (unsigned)(0xff & gchar(&memp));
			if (inc(&memp) == -1)
				done = 1;
			/* when getting a character from the file, we */
			/* may have to turn it into something else on */
			/* the way to putting it into 'Nextscreen'. */
			if ( c == TAB && !P(P_LS) ) {
				strcpy(extra,"        ");
				/* tab amount depends on current column */
				nextra = ((P(P_TS)-1) - (col - coff)%P(P_TS));
				c = ' ';
			}
			else if ( c == NUL && P(P_LS) ) {
				extra[0] = NUL;
				nextra = 1;
				c = '$';
			} else if ( (n = chars[c].ch_size) > 1 ) {
				char *p;
				nextra = 0;
				p = chars[c].ch_str;
				/* copy 'ch-str'ing into 'extra' in reverse */
				while ( n > 1 )
					extra[nextra++] = p[--n];
				c = p[0];
			}
		}

		if (screenp == endscreen) {
			/*
			 * We're one past the end of the screen. If the
			 * current character is null, then we really did
			 * finish, so set didline = TRUE. In either case,
			 * break out because we're done.
			 */
			dec(&memp);
			if (memp.index != 0 && c == NUL) {
				didline = TRUE;
				inc(&memp);
			}
			break;
		}

		if ( c == NUL ) {
			srow = ++row;
			/*
			 * Save this position in case the next line won't
			 * fit on the screen completely.
			 */
			save = memp;
			/* get pointer to start of next row */
			nextrow = &Nextscreen[row*Columns];
			/* blank out the rest of this row */
			while ( screenp != nextrow )
				*screenp++ = ' ';
			col = 0;
			continue;
		}
		if ( col >= Columns ) {
			row++;
			col = 0;
		}
		/* store the character in Nextscreen */
		*screenp++ = c;
		col++;
	}
	/*
	 * If we didn't hit the end of the file, and we didn't finish
	 * the last line we were working on, then the line didn't fit.
	 */
	if (!done && !didline) {
		/*
		 * Clear the rest of the screen and mark the unused lines.
		 */
		screenp = &Nextscreen[srow * Columns];
		while (screenp < endscreen)
			*screenp++ = ' ';
		for (; srow < (Rows-1) ;srow++)
			Nextscreen[srow * Columns] = '@';
		*Botchar = save;
		return;
	}
	/* make sure the rest of the screen is blank */
	while ( screenp < endscreen )
		*screenp++ = ' ';
	/* put '~'s on rows that aren't part of the file. */
	if ( col != 0 )
		row++;
	while ( row < Rows ) {
		Nextscreen[row*Columns] = '~';
		row++;
	}
	if (done)	/* we hit the end of the file */
		*Botchar = *Fileend;
	else
		*Botchar = memp;	/* FIX - prev? */
}

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

	if (anyinput()) {
		need_redraw = TRUE;
		return;
	}

	endscreen = &np[(Rows-1)*Columns];

	outcstr(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++;
		}
	}
	outcstr(T_CV);		/* enable cursor again */
}

/*
 * lfiletonext() - like filetonext() but only for cursor line
 *
 * Returns true if the size of the cursor line (in rows) hasn't changed.
 * This determines whether or not we need to call filetonext() to examine
 * the entire screen for changes.
 */
static bool_t
lfiletonext()
{
	register int	row, col;
	register char	*screenp;
	LPTR	memp;
	register char	*nextrow;
	char	extra[16];
	int	nextra = 0;
	register int	c;
	int	n;
	bool_t	eof;
	int	lno;		/* number of the line we're doing */
	int	coff;		/* column offset */

	coff = P(P_NU) ? 8 : 0;

	/*
	 * This should be done more efficiently.
	 */
	if (P(P_NU))
		lno = cntllines(Filemem, Curschar);

	screenp = Nextscreen + (Cline_row * Columns);

	memp = *Curschar;
	memp.index = 0;

	eof = FALSE;
	col = 0;
	row = Cline_row;

	while (!eof) {

		if (P(P_NU) && col == 0 && memp.index == 0) {
			strcpy(extra, mkline(lno));
			nextra = 8;
		}

		/* Get the next character to put on the screen. */

		/* The 'extra' array contains the extra stuff that is */
		/* inserted to represent special characters (tabs, and */
		/* other non-printable stuff.  The order in the 'extra' */
		/* array is reversed. */

		if ( nextra > 0 )
			c = extra[--nextra];
		else {
			c = (unsigned)(0xff & gchar(&memp));
			if (inc(&memp) == -1)
				eof = TRUE;
			/* when getting a character from the file, we */
			/* may have to turn it into something else on */
			/* the way to putting it into 'Nextscreen'. */
			if ( c == TAB && !P(P_LS) ) {
				strcpy(extra,"        ");
				/* tab amount depends on current column */
				nextra = ((P(P_TS)-1) - (col - coff)%P(P_TS));
				c = ' ';
			} else if ( c == NUL && P(P_LS) ) {
				extra[0] = NUL;
				nextra = 1;
				c = '$';
			} else if ( c != NUL && (n=chars[c].ch_size) > 1 ) {
				char *p;
				nextra = 0;
				p = chars[c].ch_str;
				/* copy 'ch-str'ing into 'extra' in reverse */
				while ( n > 1 )
					extra[nextra++] = p[--n];
				c = p[0];
			}
		}

		if ( c == NUL ) {
			row++;
			/* get pointer to start of next row */
			nextrow = &Nextscreen[row*Columns];
			/* blank out the rest of this row */
			while ( screenp != nextrow )
				*screenp++ = ' ';
			col = 0;
			break;
		}

		if ( col >= Columns ) {
			row++;
			col = 0;
		}
		/* store the character in Nextscreen */
		*screenp++ = c;
		col++;
	}
	return ((row - Cline_row) == Cline_size);
}

/*
 * lnexttoscreen
 *
 * Like nexttoscreen() but only for the cursor line.
 */
static void
lnexttoscreen()
{
	register char	*np = Nextscreen + (Cline_row * Columns);
	register char	*rp = Realscreen + (Cline_row * Columns);
	register char	*endline;
	register int	row, col;
	int	gorow = -1, gocol = -1;

	if (anyinput()) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -