
#include	"sysdef.h"


#include	"scerrd.h"
#include	"tempio.h"

#define	then
#define	CR	015
#define	FF	014
#define	LF	012
#define	SPACE	040
#define	TAB	011

extern	int	verbose ;

findplace(tp,pos)
struct textline *tp ;
int pos ;
{
	
	register char *wp ;
	register int temp ;
	register int i ;
	int chpos ;

	temp = 1 ;
	chpos = 1 ;
	wp = tp->c ;
	for (i=tp->len; i>0 && temp < pos && isdelimiter(*wp)==0 ;i--) {
		chpos++ ;
		if ( *wp++ == TAB && (temp&07) != 0 )
		  then temp += 8 - (temp&07) ;
		temp++ ;
		}
	return( (i==0) ? 0:chpos ) ;
}

cplace()
{
	extern int cmdlun,kblun,lowlim,highlim ;
	extern struct textline *textpointer() ;
	register struct textline *tp ;
	extern struct textline inline ;
	register char *cp,*sp ;
	int lineno,newsize,pos,size,where ;
	char s[160] ;

	sp = s ;
	tp = textpointer(lowlim) ;
	if ( cmdlun == kblun) then echoline(kblun,tp,lowlim) ;
	cp = inline.c ;
	if ( ( newsize = getprm(s,"Insert >",160) ) != 0 )
	  then	{
		pos = 1 ;
		size = detab(cp,s,(newsize=cvtstring(s,newsize,04))) ;
		for ( ;size > 0 && *cp == SPACE; size-- ) {cp++ ;pos++ ; }
		for ( ;newsize > 0 && (*sp==SPACE||*sp==TAB); newsize--) sp++ ;
		if ( newsize && *sp == '~' )
		  then	{
			sp++ ;
			newsize -- ;
			}
		if ( size && newsize )
		  then
		    for (lineno=lowlim; lineno<=highlim; lineno++ ) {
			tp = textpointer(lineno) ;
			if ( (where=findplace(tp,pos)) != 0 )
			  then	{
				where-- ;
				insert(&tp->c[where],tp->len-where,sp,newsize) ;
				tp->len += newsize ;
				if (verbose) then echoline(kblun,tp,lineno) ;
				}
			}
		}
}



cxchange()
{
	extern int cmdlun,kblun,lowlim,highlim ;
	extern struct textline *textpointer() ;
	register struct textline *tp ;
	extern struct textline inline ;
	register char *cp ;
	register int lineno ;
	int pos,size,where ;
	char s[160] ;

	tp = textpointer(lowlim) ;
	if ( cmdlun == kblun) then echoline(kblun,tp,lowlim) ;
	cp = inline.c ;
	if ( ( size = getprm(s,"Replace>",160) ) != 0 )
	  then	{
		pos = 1 ;
		size = detab(cp,s,cvtstring(s,size,04)) ;
		for ( ;size > 0 && *cp == SPACE; size-- ) {cp++ ;pos++ ; }
		if ( size )
		  then
		    for (lineno=lowlim; lineno<=highlim; lineno++ ) {
			tp = textpointer(lineno) ;
			if ( (where=findplace(tp,pos)) != 0 )
			  then	{
				where-- ;
				replace(&tp->c[where],tp->len-where,cp,size) ;
				if (verbose) then echoline(kblun,tp,lineno) ;
				}
			}
		}
}




cchardel()
{
	extern int cmdlun,kblun,lowlim,highlim ;
	extern struct textline *textpointer() ;
	register struct textline *tp ;
	extern struct textline inline ;
	register char *cp ;
	register int lineno ;
	int pos,size,where ;
	char s[160] ;

	tp = textpointer(lowlim) ;
	if ( cmdlun == kblun) then echoline(kblun,tp,lowlim) ;
	cp = inline.c ;
	if ( ( size = getprm(s,"Delete >",160) ) != 0 )
	  then	{
		pos = 1 ;
		size = detab(cp,s,cvtstring(s,size,0204)) ;
		for ( ;size > 0 && *cp == SPACE; size-- ) {cp++ ;pos++ ; }
		if ( size )
		  then
		    for (lineno=lowlim; lineno<=highlim; lineno++ ) {
			tp = textpointer(lineno) ;
			if ( (where=findplace(tp,pos)) != 0 )
			  then	{
				where-- ;
				if ( where+size < tp->len )
				  then	{
				    remove(&tp->c[where],tp->len-where,size) ;
				    tp->len -= size ;
				    } ;
				if (verbose) then echoline(kblun,tp,lineno) ;
				}
			}
		}
}







static insert(s,size,new,newsize)
char *s,*new ;
int size,newsize ;
{
	register char *cp,*dp ;
	register int i ;

	cp = &s[size] ;
	dp = &s[size+newsize] ;
	for ( i=size; i > 0 ; i-- ) *(--dp) = *(--cp) ;
	cp = new ;
	dp = s ;
	for ( i=newsize; i > 0 ; i-- ) *dp++ = *cp++ ;
	txtmod() ;
	return(new+newsize) ;
}


static replace(s,size,new,newsize)
register char *s,*new ;
int size,newsize ;
{
	register int i ;

	for ( i=newsize; i > 0 && size-- > 0 && isdelimiter(*s) == 0; i--)
		*s++ = *new++ ;
	txtmod() ;
	return(size) ;
}


static remove(s,size,amount)
char *s ;
int size,amount ;
{
	register char *cp, *dp ;
	register int i ;

	cp = s ;
	dp = &s[amount] ;
	for ( i=size-amount; i>0; i-- ) *cp++ = *dp++ ;
	txtmod() ;
	return(size-amount) ;
}


static echoline(lun,tp,lineno)
struct textline *tp ;
int lineno,lun ;
{
	extern int p_lines,kblun ;

	if (p_lines && lun == kblun) then plinenumber(lineno) ;
	putline(lun,tp->c,tp->len) ;

}


static scopy(d,s,len)
int len ;
register char *d,*s ;
{
	register int i ;
	for (i=len; i>0 ;i--) *d++ = *s++ ;
}
