
#include	"sysdef.h"

#include	"scerrd.h"
#define	then
#define	CTRLZ	032
#define	CR	015
#define	FF	014
#define	LF	012

	struct cutblock {
			int	cpnt	;
			int	cblk	;
			} ;

	extern struct cutblock *pnt_cur,*pnt_eof ;
	extern struct cutblock cut_cur[],cut_eof[] ;
	extern char cutbuffer[] ;
	extern int  cut_lines ;
	extern int  bufnumber ;
	extern int *curline,cut_st,*loclow,*lochigh ;
	extern int worklength ;
	extern char workbuffer[] ;




setpaste(n)
int n ;
{
	flucut() ;
	pnt_cur = &cut_cur[0] ;
	pnt_eof = &cut_eof[0] ;
	bufnumber = n ;
	rewcut() ;
}

vtsetbuffer()
{
	extern int rept ;
	int len ;
	char buf[65] ;

	if ( rept == 0 )
	  then	{
		movel24() ;
		ceol24() ;
		if (len = trimjunk(buf,lgetbin(buf,"Buffer number (0..2) ? ")))
		  then
			if ( len != 1 || isdigit(buf[0]) == 0 )
			  then rept = 999 ;
			  else rept = buf[0] - '0' ;
		goback() ;
		}
	if ( rept < 0 || rept > 2 )
	  then	scwarning(w_badbuf) ;
	  else	setpaste(rept) ;
	rept = 0 ;
	sctopline() ;
	goback() ;
}




/*+	VTMARK()	start a select range, or save a select range	*/
/*			if a select range is active			*/

vtmark()
{
	int nlines,oldcurline ;
	register int i,j ;
	register char *cp ;

	oldcurline = *curline ;
	if ( cut_st )
	  then	{
		inicut() ;
		scunloadline() ;
		if ( ( nlines = *curline-cut_st ) > 0 )
		  then	{
			*curline = cut_st ;
			for (i=nlines; i>0 ;i--) {
			  scloadline() ;
			  cp = workbuffer ;
			  for (j=0; j<worklength; j++)
				if (i==1 && ( *cp == FF || *cp == CR ) )
				  then	{
					putcut(CTRLZ) ;
					break ;
					}
				  else	putcut(*cp++) ;
			  if ( *curline >= *loclow )
			    then {
				scmove() ;
				retype() ;
				 }
			  (*curline)++ ;
			  }
			flucut() ;
			}
		ledoff(l_select) ;
		cut_st = 0 ;
		*curline = oldcurline ;
		scloadline() ;
		scmove() ;
		}
	  else	{
		cut_st = *curline ;
		ledon(l_select) ;
		}

}


/*+	VTPASTE		insert the cut text into the file, saving it	*/
/*			if a select range is still active.		*/

vtpaste()
{
	char temp ;
	register int ch,i ;

	if ( cut_st ) then vtmark() ;
	rewcut() ;
	if ( getcut() < 0 )
	  then	{
		scwarning(w_nopaste) ;
		return(0) ;
		}
	rewcut() ;
	sclinsert(CR) ;
	creset() ;
	i = 0 ;
	while ( (ch = getcut()) >= 0 ) {
		temp = ch ;
		if ( ch == CTRLZ )
		  then	retype() ;
		  else
			if ( ch == CR || ch == FF )
			  then	{
				retype() ;
				i = 0 ;
				mvdown() ;
				sclinsert(ch) ;
				}
			  else	svinsert(i++,&temp,1) ;
	}
	creset() ;
	scmove() ;
	mvdown() ;
}

vtapp()
{
	register int i ;
	register char *cp ;

	cp = workbuffer ;
	appcut() ;
	putcut(CR) ;
	putcut(LF) ;
	for ( i=0;i<worklength && isdelimiter(*cp) == 0;i++ ) putcut(*cp++) ;
	putcut(CTRLZ) ;
	flucut() ;
}


vtcutzap()
{
	inicut() ;
	cut_st = 0 ;
	ledoff( l_select ) ;
	return(1) ;
}


	



#include	"scerrd.h"
#define	BUFSIZ	01000
#define	then


/*	INICUT	clear all text saved in virtual paste buffer		*/

static inicut()
{
	pnt_cur->cpnt = 0 ;		/* init next free byte pointer	*/
	pnt_cur->cblk = 0 ;		/* init current virtual block	*/
	pnt_eof->cpnt = 0 ;		/* clear first free in last vbn	*/
	pnt_eof->cblk = 0 ;		/* clear eof block number	*/
	cut_lines = 0 ;
	zapcut() ;			/* clear the block buffer	*/
	return(1) ;
}

static zapcut()
{
	register int i ;
	for (i=BUFSIZ-1; i>=0 ; i--) cutbuffer[i] = 0 ;
}


static wricut()
{
	register int status ;
	status = wricbuffer(cutbuffer,++pnt_cur->cblk,bufnumber) ;
	if ( status == 0 ) then erp() ;
	zapcut() ;
	pnt_cur->cpnt = 0 ;		/* next byte is zero		*/
	return( status ) ;
}


static reacut()
{
	register int status ;
	status = reacbuffer(cutbuffer,++pnt_cur->cblk,bufnumber) ;
	pnt_cur->cpnt = 0 ;		/* next byte is zero		*/
	return( status ) ;
}


static flucut()
{
	register int status ;
	status = wricut() ;
	pnt_eof->cblk = pnt_cur->cblk ;	/* update last block pointer	*/
	return( status ) ;
}




static rewcut()
{
	register int status ;
	pnt_cur->cblk = 0 ;		/* start at the first block	*/
	status = reacut() ;		/* load the correct block	*/
	pnt_cur->cpnt = 0 ;		/* next free byte is zero	*/
	return( status ) ;
}

static appcut()
{
	register int status ;
	if ( (pnt_cur->cblk = pnt_eof->cblk) != 0 || pnt_eof->cpnt != 0 )
	  then	{
		pnt_cur->cblk-- ;
		status = reacut() ;
		pnt_cur->cblk-- ;
		pnt_cur->cpnt = pnt_eof->cpnt ;
		return( status ) ;
		}
	  else	return( inicut() ) ;
}


static putcut(ch)
char ch ;
{
	register int status ;

	status = 1 ;
	if ( pnt_cur->cpnt >= BUFSIZ ) then status = wricut() ;
	cutbuffer[pnt_cur->cpnt++] = ch ;
	pnt_eof->cpnt = pnt_cur->cpnt ;
	pnt_eof->cblk = pnt_cur->cblk ;
	cut_lines++ ;
	return( status ) ;
}
	

static getcut()
{
	register int ch ;
	while ( ( ch = xgetcut() ) == LF ) ;
	return(ch) ;
}


static xgetcut()
{
	register int status ;

	status = sys_normal ;
	if ( eofcut() ) then return(-1) ;
	if ( pnt_cur->cblk == 0 || pnt_cur->cpnt >= BUFSIZ )
	   then status=reacut();
	if ( status == sys_normal)
	   then return( cutbuffer[pnt_cur->cpnt++] ) ;
	   else return(-2) ;
}
	
static eofcut()
{
	if ( pnt_cur->cblk > pnt_eof->cblk )
	  then	return(1) ;
	  else
		if (pnt_cur->cblk == pnt_eof->cblk &&
		    pnt_cur->cpnt >= pnt_eof->cpnt   )
		  then	return(1) ;
		  else	return(0) ;
}


static erp()
{
	char s[80] ;
	scwarning(w_cuterr) ;
	movel24() ;
	ceol24() ;
	geterror(s) ;
	putbin(s,0) ;
	goback() ;
}

