
#include	"sysdef.h"

/*								*/
/*	Copyright Notice					*/
/*								*/
/*								*/
/*	C version Copyright (C) 1984 Change Software, Inc.	*/
/*	Copyright (C) 1978 1979 1980 1981 1982 1983 1984 by	*/
/*		      Change Software, Inc.			*/
/*								*/
/*								*/
/*	This software is furnished under a license and may	*/
/*	be  used  and  copied  only in accordance with the	*/
/*	terms of such license and with  the  inclusion  of	*/
/*	the  above copyright notice.  This software or any	*/
/*	other copies thereof may not be provided or other-	*/
/*	wise made available to any other person.  No title	*/
/*	to and ownership of the software is hereby  trans-	*/
/*	ferred.							*/
/*								*/


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

#define	then
#define	CR	015
#define	LF	012


vtcenter()
{
	extern int rightmargin,worklength,read_only ;
	extern char workbuffer[] ;
	register int i,nbl ;

	if ( read_only ) then return(0) ;
	creset() ;
	if ( (worklength = cvtstring(workbuffer,worklength,0210))==0 )
	  then	{
		workbuffer[0] = CR ;
		workbuffer[1] = LF ;
		worklength = 2 ;
		}
	  else	{
		nbl = (rightmargin - worklength) / 2 ;
		if ( nbl < 0 ) then return(0) ;
		for (i=0; i<nbl; i++) svinsert(0," ",1) ;
		scmove() ;
		lkill() ;
		dumpworkbuffer() ;
		scmove() ;
		}
	return(1) ;
}

vtfill()
{
	extern int rightmargin,worklength ;
	extern char workbuffer[] ;
	register int i,nbl ;

	creset() ;
	txtmod() ;
	if ( (worklength = cvtstring(workbuffer,worklength,0210))==0 )
	  then	{
		workbuffer[0] = CR ;
		workbuffer[1] = LF ;
		worklength = 2 ;
		}
	  else	{
		worklength = lnfill(workbuffer,worklength,rightmargin) ;
		workbuffer[worklength++] = CR ;
		workbuffer[worklength++] = LF ;
		scmove() ;
		lkill() ;
		dumpworkbuffer() ;
		scmove() ;
		}
	return(1) ;
}





#define	then
#define	SPACE	040
#define	TAB	011

/*	LNFILL	Justify a line						*/
/*									*/
/*	Originally from K&P Software Tools, then into Pascal, then to	*/
/*	a Macro-11 version and now into C.				*/
/*									*/
/*	18-Sep-84  10:26:04  Brian Nelson				*/

lnfill(s,len,fill)
char *s ;
int len,fill ;

{

	register char *cp,*op ;
	register int  i ;
	char lastch ;
	int fillcount,j,nbl,nholes,wordcount ;
	extern int fildir ;			/* dir could be static	*/
  
	lastch = ' ' ;
	if ( len == 0 ) then len = strlen(s) ;
	if ( (i = len) == 0 ) then return(0) ;
	cp = &s[i] ;
	while ( *(--cp) <= ' ' && i > 0 ) i-- ;
	if ( (len = i) == 0 ) then return(0) ;
	i = 0 ;
	cp = s ;
	op = s ;
	for ( j=0; j<len; j++ ) {
		if ( *cp == TAB ) then *cp = SPACE ;
		if ( !( *cp == SPACE && lastch == SPACE ) )
		  then	{
			i++ ;
			lastch = *cp ;
			*op++ = *cp++ ;
			}
		  else	lastch = *cp++ ;
		}
	len = i ;
	wordcount = 1 ;
	cp = s ;
	for (i=0; i<len; i++ ) if (*cp++ == ' ') then wordcount++ ;
	fillcount = fill - len ;
	nholes = wordcount-1 ;
	if ( fillcount <= 0 || wordcount <= 1 ) then return(len) ;

	i = len-1 ;
	j = i+fillcount ;
	fildir = (fildir) ? 0:1 ;
	while ( i<j && j >= 0 && i >= 0 ) {

		s[j] = s[i] ;
		if ( s[j] == ' ' )
		  then	{
			nbl = (fildir)?(fillcount-1)/nholes+1:fillcount/nholes;
			fillcount -= nbl ;
			nholes-- ;
			while ( (nbl--) > 0 && j >=0 ) s[--j] = ' ' ;
			}
		i-- ;
		j-- ;
	}
	return( fill ) ;
}
