/******************************************************************************

	GroEBf()

	This function makes room in the edit buffer so that text can be
inserted.  HowMuc is how much room is required.

*****************************************************************************/

#include "ZPort.h"		/* define portability identifiers */
#include "DefTeco.h"		/* define general identifiers */
#include "DefError.h"		/* define identifiers for error messages */

extern	VOID	ErrMsg();	/* display an error message */
extern	VOID	ZCpyBl();	/* copy a block of memory */
extern	char	*ZRaloc();	/* re-allocate memory*/

EXTERN	char	*EBfBeg;	/* beginning of edit buffer */
EXTERN	char	*EBfEnd;	/* end of edit buffer */
EXTERN	char	*GapBeg;	/* beginning of edit buffer gap */
EXTERN	char	*GapEnd;	/* end of edit buffer gap */
EXTERN	char	*IBfEnd;	/* end of input buffer area */


DEFAULT GroEBf(HowMuc)		/* expand the edit buffer */
LONG HowMuc;
{
	LOCAL	LONG	NewSiz;
	LOCAL	char	*NewBeg;

#if DEBUGGING
DbgInd+=2;if(DbgLvl>=3)
{DbgMsg();DbgDBf("GroEBf: called with ");
MakDBf(HowMuc, 10);ZDspBf(DBfBeg, DBfPtr-DBfBeg);DbgDBf("\015\012");DbgROf();}
#endif

/*
  expand the edit buffer.  Expand by more than HowMuc, because if we
  expand by HowMuc,  then there will only be HowMuc free space when we
  return.  It is likely that the next insertion command would require
  another call to this routine.
*/
	NewSiz = (GapBeg-EBfBeg) + HowMuc + EBFEXP + (IBfEnd-GapEnd);
	NewBeg = ZRaloc(EBfBeg, NewSiz);
	if (NewBeg == NULL)
		{
		ErrMsg(ERR_MEM);		/* memory overflow */
#if DEBUGGING
if(DbgLvl>=3)
{DbgMsg();DbgDBf("GroEBf: returning FAILURE.\015\012");DbgROf();}DbgInd-=2;
#endif
		return(FAILURE);
		}
	if (NewBeg != EBfBeg)
		{
		GapBeg = NewBeg + (GapBeg - EBfBeg);
		GapEnd = NewBeg + (GapEnd - EBfBeg);
		EBfEnd = NewBeg + (EBfEnd - EBfBeg);
		EBfBeg = NewBeg;
		}
	IBfEnd = (NewBeg + NewSiz) - 1L;

	HowMuc += EBFEXP;
	ZCpyBl(HowMuc, GapEnd+1L, GapEnd+1L+HowMuc);
	GapEnd += HowMuc;
	EBfEnd += HowMuc;
#if DEBUGGING
if(DbgLvl>=3)
{DbgMsg();DbgDBf("GroEBf: returning SUCCESS.\015\012");DbgROf();}DbgInd-=2;
#endif
	return(SUCCESS);
}
