	include	ed.inc

	.code

	Loc	Backward,word,1
	Loc	Dummy,word,1
	Loc	SaveOptions,word,1
	Entry	ReplaceText
	mov	ax,ds:BlockEnd.X
	mov	ds:TempPos2.X,ax
	mov	ax,ds:BlockEnd.Y
	mov	ds:TempPos2.Y,ax
	mov	ax,ds:BlockBeg.X
	mov	bx,ds:BlockBeg.Y
	xchg	ax,ds:TempPos.X
	xchg	bx,ds:TempPos.Y
	mov	cx,ds:CursorPos.Y
	mov	Backward,0
	mov	ds:BlockEnd.X,ax
	mov	ds:BlockEnd.Y,cx
	cmp	bx,cx
	mov	cx,ds:SearchPos
	jb	@@2
	ja	@@1
	cmp	cx,ax
	jle	@@2
@@1:	inc	Backward
	xchg	bx,ds:BlockEnd.Y
	xchg	cx,ds:BlockEnd.X
@@2:	mov	ds:BlockBeg.Y,bx
	mov	ds:BlockBeg.X,cx
	mov	ax,ds:Options
	mov	SaveOptions,ax
	and	ds:Options,not eoBlockHidden
	Invoke	DeleteBlockRaw
	Invoke	SetEditDS
	les	si,ds:LParam
	mov	cl,es:[si+2]
	mov	ch,0
	jcxz	@@4
	lea	si,[si+3]
@@3:	push	ds
	push	es
	pop	ds
	lodsb
	pop	ds
	mov	byte ptr ds:CParam,al
	push	cx es si
	invoke	InsertNoOver
	pop	si es cx
	loop	@@3
@@4:	invoke	SetBlockEndRaw
	invoke	SetEditDS
	cmp	Backward,0
	je	@@5
	invoke	MoveToBlockBegRaw
	invoke	SetEditDS
	mov	bx,ds:BlockBeg.X
	mov	ax,ds:BlockEnd.X
	mov	cx,ds:BlockEnd.Y
	jmp	@@6
@@5:	mov	bx,ds:BlockEnd.X
	mov	ax,ds:BlockBeg.X
	mov	cx,ds:BlockBeg.Y
@@6:	mov	ds:SearchPos,ax
	mov	ax,ds:TempPos.X
	mov	ds:BlockBeg.X,ax
	mov	ax,ds:TempPos.Y
	mov	ds:BlockBeg.Y,ax
	mov	ds:TempPos.X,bx
	mov	ds:TempPos.Y,cx
	mov	ax,ds:TempPos2.X
	mov	ds:BlockEnd.X,ax
	mov	ax,ds:TempPos2.Y
	mov	ds:BlockEnd.Y,ax
	mov	ax,SaveOptions
	mov	ds:Options,ax
	Exit	ReplaceText

	end
