	include	ed.inc

	.code

	db	'$*$*$*',1,65,0,1,'TEDIT',0

	Argm	AEditor,dword,1
	Argm	Command,word,1
	Argm	LP,dword,1
	Argm	WP,word,1
	Entry	EditFunc,X
	mov	ax,_Data
	mov	ds,ax
	push	SaveBP CurEd EdHandle
	push	si di
	les	si,AEditor
	mov	CurEd.Segm,es
	mov	CurEd.Offs,si
	les	bx,es:[si].Handle
	mov	dx,es
	test	dx,dx
	jz	@@1
	jmp	@@5
@@1:	mov	ax,VMemPageSize
	push	ax
	Invoke	GetVMem
	mov	EdHandle.Offs,ax
	mov	EdHandle.Segm,dx
	mov	bx,ax
	or	ax,dx
	jnz	@@4
@@2:	les	si,CurEd
	mov	ax,-2
	push	ax
	cmp	es:[si].ErrorProc.Segm,0
	je	@@3
	mov	ax,erHeapFailure
	push	ax
	call	es:[si].ErrorProc
@@3:	jmp	@@12
@@4:	push	dx bx
	push	dx bx
	Invoke	LockHandle
	pop	bx dx
	mov	es,dx
	mov	ax,es:[bx]
	cmp	ax,VMemLimit
	jbe	@@2
	mov	es,ax
	xor	di,di
	push	di
	xor	ax,ax
	mov	cx,VMemPageSize shr 1
	cld
	rep	stosw
	pop	di
	mov	es:[di].PageSize,VMemPageSize
	mov	es:[di].PageTblSize,VMemPageSize
	lea	ax,es:PageTbl
	mov	es:[di].LastPage,ax
	mov	es:[di].CurPage,ax
	mov	es:[di].CPFirstLine,1
	mov	es:[di].SearchPos,-1
	mov	ax,bx
	mov	bx,dx
	les	si,CurEd
	mov	es:[si].Handle.Offs,ax
	mov	es:[si].Handle.Segm,bx
	jmp	@@6
@@5:	mov	EdHandle.Offs,bx
	mov	EdHandle.Segm,dx
	push	dx bx
	Invoke	LockHandle
@@6:	les	bx,EdHandle
	mov	ax,es:[bx]
	cmp	ax,VMemLimit
	ja	@@7
	jmp	@@2
@@7:	mov	es,ax
	sub	di,di
	lds	si,CurEd
	mov	cx,size TEditor shr 1
	cld
	rep	movsw
	lea	si,AEditor
	lea	di,es:EParam
	mov	cx,8
	push	ss
	pop	ds
	cld
	movsw
	movsw
	lea	si,Command
	movsw
	lea	si,LP
	movsw
	movsw
	lea	si,WP
	movsw
	SetTurboDS
	lea	ax,@@9
	push	cs ax bp
	mov	bp,sp
	mov	SaveBP,bp
	push	es
	pop	ds
	cmp	ds:TabSize,1
	adc	ds:TabSize,0
	Invoke	CheckLine
	Invoke	SetEditDS
	mov	ax,ds:CursorPos.y
	mov	ds:RedrawLine,ax
	mov	ax,ds:CParam
	cmp	ah,0ffh
	je	@@8
	Invoke	ProcessKey
	jmp	@@10
@@8:	Invoke	DoCommand
	jmp	@@10
@@9:	Invoke	SetEditDS
	or	ds:Modified,emShow+emUpdate
	Invoke	FullPaintScreen
	Invoke	SetEditDS
	mov	ax,erHeapFailure
	mov	ds:ReturnCode,ax
	or	ds:Errors,al
	mov	ax,-2
	jmp	@@11
@@10:	mov	sp,bp
	pop	bp di di
@@11:	SetTurboDS
	push	ax
	Invoke	UnlockPage
	SetTurboDS
	les	di,CurEd
	lds	si,EdHandle
	mov	ds,[si]
	xor	si,si
	mov	ax,ds:RedrawFlags
	and	ds:RedrawFlags,not rfAny
	mov	cx,size TEditor shr 1
	cld
	rep	movsw
	mov	ds:RedrawFlags,ax
	Invoke	RefreshChain
	Invoke	SetEditDS
	cmp	ds:Errors,0
	je	@@12
	cmp	ds:ErrorProc.Segm,0
	je	@@12
	push	ds
	pop	es
	mov	ax,ds:ReturnCode
	push	ax
	SetTurboDS
	call	es:ErrorProc
@@12:	SetTurboDS
	les	di,EdHandle
	mov	ax,es
	test	ax,ax
	jz	@@13
	push	es di
	Invoke	UnlockHandle
@@13:	pop	ax
	pop	di si
	SetTurboDS
	pop	EdHandle CurEd SaveBP
	Exit	EditFunc

	.data

	extrn	CurEd:dword,EdHandle:dword,SaveBP:word

	end
