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

	ExeD()

	This function executes a D command.

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

#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	DEFAULT	ExeK();		/* execute a K command */
extern	DEFAULT	GetNmA();	/* get numeric argument */
extern	DEFAULT	PushEx();	/* push onto expression stack */
extern	VOID	UMinus();	/* handle unary minus */

EXTERN	BYTE	CmdMod;		/* command modifiers flags for @, :, etc. */
EXTERN	char	*EBfBeg;	/* beginning of edit buffer */
EXTERN	char	*EBfEnd;	/* end of edit buffer */
EXTERN	WORD	EStBot;		/* expression stack bottom */
EXTERN	WORD	EStTop;		/* expression stack top */
EXTERN	char	*GapBeg;	/* beginning of edit buffer gap */
EXTERN	char	*GapEnd;	/* end of edit buffer gap */
EXTERN	LONG	NArgmt;		/* numeric argument */


DEFAULT ExeD()			/* execute a D command */
{
	LOCAL	LONG	Status;

#if DEBUGGING
DbgInd+=2;if(DbgLvl>=1){DbgMsg();DbgDBf("ExeD: called.\015\012");DbgROf();}
#endif

	if (CmdMod & MARGIS)			/* if it's m,nD */
#if DEBUGGING
{if(DbgLvl>=1)
{DbgMsg();DbgDBf("ExeD: returning ExeK().\015\012");DbgROf();}DbgInd-=2;
#endif
		return(ExeK());
#if DEBUGGING
}
#endif

	if (EStTop == EStBot)			/* if no numeric argument */
		NArgmt = 1L;			/* default is 1D */
	else
		{
		UMinus();			/* if it's -D, make it -1D */
		if (GetNmA() == FAILURE)	/* get numeric argument */
#if DEBUGGING
{if(DbgLvl>=1)
{DbgMsg();DbgDBf("ExeD: returning FAILURE.\015\012");DbgROf();}DbgInd-=2;
#endif
			return(FAILURE);
#if DEBUGGING
}
#endif
		}

	Status = -1L;					/* -1 means success */
	if (NArgmt > 0L)
		if ((GapEnd+NArgmt) > EBfEnd)		/* if out of range */
			Status = 0L;			/* 0 means failure */
		else
			GapEnd += NArgmt;		/* delete */
	else
		if ((GapBeg+NArgmt) < EBfBeg)		/* if out of range */
			Status = 0L;			/* 0 means failure */
		else
			GapBeg += NArgmt;		/* delete */

	if (CmdMod & COLON)				/* if it's :D */
		{
		CmdMod = '\0';			/* clear modifiers flags */
#if DEBUGGING
if(DbgLvl>=1)
{DbgMsg();DbgDBf("ExeD: returning PushEx().\015\012");DbgROf();}DbgInd-=2;
#endif
		return(PushEx(Status, OPERAND));
		}

	if (Status == 0L)
		{
		ErrMsg(ERR_DTB);		/* DTB = "delete too big" */
#if DEBUGGING
if(DbgLvl>=1)
{DbgMsg();DbgDBf("ExeD: returning FAILURE.\015\012");DbgROf();}DbgInd-=2;
#endif
		return(FAILURE);
		}

	CmdMod = '\0';				/* clear modifiers flags */
#if DEBUGGING
if(DbgLvl>=1)
{DbgMsg();DbgDBf("ExeD: returning SUCCESS.\015\012");DbgROf();}DbgInd-=2;
#endif
	return(SUCCESS);
}
