	include	ed.inc

	.code

	Entry	RestoreLine
	mov	ax,ds:BlockBeg.X
	mov	ds:TempPos.X,ax
	mov	ax,ds:BlockBeg.Y
	mov	ds:TempPos.Y,ax
	mov	ax,ds:BlockEnd.X
	mov	ds:TempPos2.X,ax
	mov	ax,ds:BlockEnd.Y
	mov	ds:TempPos2.Y,ax
	mov	ax,ds:UndoLen
	test	ax,ax
	jl	@@1
	mov	ax,ds:UndoBeg.X
	test	ax,ax
	jle	@@1
	mov	ds:BlockBeg.X,ax
	mov	ax,ds:UndoBeg.Y
	test	ax,ax
	jnz	@@2
@@1:	jmp	@@5
@@2:	mov	ds:BlockBeg.Y,ax
	mov	ax,ds:UndoEnd.X
	mov	ds:BlockEnd.X,ax
	mov	ax,ds:UndoEnd.Y
	mov	ds:BlockEnd.Y,ax
	Invoke	MoveToBlockBegRaw
	Invoke	SetEditDS
	push	ds:Options
	or	ds:Options,eoUndoDisabled
	and	ds:Options,not eoBlockHidden
	Invoke	DeleteBlockRaw
	Invoke	SetEditDS
	mov	cx,ds:UndoLen
	jcxz	@@4
	push	cx
	Invoke	LockCurPage
	Invoke	SetEditDS
	SetTurboDS
	Invoke	ToggleMAlloc
	Invoke	SetEditDS
	les	si,ds:UndoPtr
	pop	cx
@@3:	les	bx,ds:UndoPtr
	mov	al,es:[si]
	inc	si
	mov	byte ptr ds:CParam,al
	push	si cx
	Invoke	InsertNoOver
	Invoke	SetEditDS
	pop	cx si
	loop	@@3
	Invoke	UnlockPage
@@4:	Invoke	MoveToBlockBegRaw
	Invoke	FixScreen
	Invoke	SetEditDS
	and	ds:Options,not eoUndoDisabled
	Invoke	InvalidateUndo
	Invoke	SetEditDS
	pop	ds:Options
@@5:	mov	ax,ds:TempPos.X
	mov	ds:BlockBeg.X,ax
	mov	ax,ds:TempPos.Y
	mov	ds:BlockBeg.Y,ax
	mov	ax,ds:TempPos2.X
	mov	ds:BlockEnd.X,ax
	mov	ax,ds:TempPos2.Y
	mov	ds:BlockEnd.Y,ax
	Exit	RestoreLine

	Entry	InvalidateUndo
	test	ds:Options,eoUndoDisabled
	jnz	@@2
	mov	bx,ds:UndoPtr.Offs
	mov	dx,ds:UndoPtr.Segm
	mov	ax,bx
	or	ax,dx
	jz	@@1
	push	dx bx ds:UndoBufLen
	SetTurboDS
	Invoke	MFree
	Invoke	SetEditDS
	xor	ax,ax
	mov	ds:UndoPtr.Offs,ax
	mov	ds:UndoPtr.Segm,ax
	mov	ds:UndoBufLen,ax
@@1:	mov	ds:UndoBeg.Y,ax
	mov	ds:UndoEnd.Y,ax
	dec	ax
	mov	ds:UndoBeg.X,ax
	mov	ds:UndoEnd.X,ax
	mov	ds:UndoLen,ax
@@2:	Exit	InvalidateUndo

	Entry	UpdateUndo
	test	ds:Options,eoUndoDisabled
	jz	@@1
	jmp	@@14
@@1:	push	ax bx cx dx di si
	mov	ax,ds:CursorPos.Y
	mov	bx,ds:CursorPos.X
	cmp	ax,ds:UndoBeg.Y
	jb	@@5
	ja	@@2
	cmp	bx,ds:UndoBeg.X
	jb	@@5
@@2:	cmp	ax,ds:UndoEnd.Y
	ja	@@5
	jb	@@3
	cmp	bx,ds:UndoEnd.X
	jae	@@5
@@3:	jmp	@@13
@@4:	jmp	@@12
@@5:	Invoke	InvalidateUndo
	Invoke	FindCurLine
	Invoke	UseCurPage
	jc	@@7
	cmp	bx,dx
	je	@@7
	mov	cx,es:[bx].LineLength
	push	cx
	mov	ax,ds:CursorPos.Y
	inc	ax
	Invoke	FindLine
	Invoke	SetEditDS
	pop	cx
	jc	@@6
	cmp	bx,dx
	je	@@6
	inc	cx
@@6:	test	cx,cx
	jl	@@4
	mov	ds:UndoLen,cx
	push	cx
	mov	ax,ds:CursorPos.Y
	Invoke	FindLine
	Invoke	LockCurPage
	Invoke	SetEditDS
	pop	cx
	test	cx,cx
	jnz	@@8
@@7:	sub	ax,ax
	mov	ds:UndoLen,ax
	Invoke	UnlockPage
	Invoke	SetEditDS
	jmp	@@10
@@8:	push	cx
	push	cx
	SetTurboDS
	Invoke	MAlloc
	mov	bx,ax
	or	bx,dx
	jnz	@@9
	pop	cx
	Invoke	UnlockPage
	jmp	@@12
@@9:	SetTurboDS
	Invoke	ToggleMAlloc
	Invoke	SetEditDS
	pop	cx
	mov	ds:UndoPtr.Offs,ax
	mov	ds:UndoPtr.Segm,dx
	mov	ds:UndoBufLen,cx
@@10:	mov	ax,1
	mov	ds:UndoBeg.X,ax
	mov	ds:UndoEnd.X,ax
	mov	ax,ds:CursorPos.Y
	mov	ds:UndoBeg.Y,ax
	inc	ax
	mov	ds:UndoEnd.Y,ax
	dec	ax
	Invoke	FindLine
	Invoke	UseCurPage
	jc	@@13
	mov	cx,es:[bx].LineLength
	mov	ax,ds:UndoLen
	test	ax,ax
	jz	@@13
	sub	ax,cx
	push	ds
	push	ax
	push	es
	lea	si,[bx+size LineHeader]
	les	di,ds:UndoPtr
	pop	ds
	shr	cx,1
	rep	movsw
	rcl	cx,1
	rep	movsb
	pop	ax
	test	ax,ax
	jz	@@11
	mov	al,0dh
	stosb
@@11:	pop	ds
	SetTurboDS
	Invoke	ToggleMAlloc
	Invoke	UnlockPage
	jmp	@@13
@@12:	Invoke	InvalidateUndo
@@13:	pop	si di dx cx bx ax
@@14:	Exit	UpdateUndo

	end
