	include	ed.inc

	.code

	Entry	GetOneChar
	mov	ax,ds:TempPos.Y
	Invoke	FindLine
	Invoke	UseCurPage
	sub	ax,ax
	cmp	bx,dx
	je	@@4
	mov	ax,ds:TempPos.X
	dec	ax
	cmp	ax,es:[bx].LineLength
	je	@@1
	ja	@@3
	add	bx,ax
	mov	al,es:[bx+size LineHeader]
	jmp	@@2
@@1:	mov	al,0dh
@@2:	inc	ds:TempPos.X
	jmp	@@4
@@3:	inc	ds:TempPos.Y
	mov	ds:TempPos.X,1
	mov	al,0ah
@@4:	mov	ah,0
	mov	ds:ReturnCode,ax
	Exit	GetOneChar

	Loc	SaveOptions,word,1
	Loc	PageLimit,word,1
	Loc	AddedLines,word,1
	Loc	BufPtr,word,1
	Loc	ReadBytes,word,1
	Loc	H,dword,1
	Loc	IgnoreLF,word,1
	Loc	IgnoreCR,word,1
	Loc	ReadStatus,word,1

ClosePage	proc	near
	inc	di
	and	di,0fffeh
	mov	bx,ds:CurPage
	mov	[bx].PageLength,di
	sub	ax,ax
	xchg	ax,AddedLines
	add	[bx].LinesInPage,ax
	ret
ClosePage	endp

_ReadTxt	proc	near
	sub	ax,ax
	mov	AddedLines,ax
	mov	ReadStatus,ax
	mov	IgnoreLF,ax
	mov	IgnoreCR,ax
	mov	ax,ds:PageSize
	mov	ReadBytes,ax
	mov	PageLimit,ax
	mov	ax,ds:Options
	mov	SaveOptions,ax
	or	ds:Options,eoUseTab+eoUndoDisabled+eoNoTruncate
	and	ds:Options,not (eoOverwrite+eoAutoIndent+eoOptimalFill)
	or	ds:RedrawFlags,rfFullScreen
	Invoke	GetNewPage
	Invoke	SetEditDS
	jnc	@@1
	jmp	@@26
@@1:	mov	H.Offs,ax
	mov	H.Segm,dx
	push	dx ax
	push	dx ax
	SetTurboDS
	Invoke	LockHandle
	Invoke	SetEditDS
	pop	bx es
	mov	ax,es:[bx]
	cmp	ax,VMemLimit
	jbe	@@3
	Invoke	FindCurLine
	Invoke	SetEditDS
	jnc	@@2
	jmp	@@25
@@2:	mov	ax,ds:PageSize
	cmp	ax,ReadBytes
	ja	@@5
	les	bx,H
	cmp	word ptr es:[bx],VMemLimit
	ja	@@4
@@3:	mov	bx,H.Offs
	mov	dx,H.Segm
	Invoke	FreePage
	InvokeJ	ErrorExit
@@4:	push	word ptr ds:LParam es bx ds:PageSize
	SetTurboDS
	Invoke	Read
	Invoke	SetEditDS
	mov	ReadBytes,ax
	test	ax,ax
	jz	@@5
	jg	@@7
	or	ReadStatus,erReadError
@@5:	Invoke	FindCurLine
	Invoke	UseCurPage
	jc	@@6
	mov	ax,es:[bx].LineLength
	test	ax,ax
	jle	@@6
	mov	si,bx
	add	si,ax
	cmp	byte ptr es:[si+size LineHeader-1],1ah
	jne	@@6
	dec	es:[bx].LineLength
	add	si,size LineHeader-1
	test	si,1
	jnz	@@6
	mov	bx,ds:CurPage
	sub	[bx].PageLength,2
@@6:	jmp	@@25
@@7:	mov	BufPtr,0
	cld
@@8:	Invoke	FindCurLine
	Invoke	UseCurPage
	jc	@@6
	lea	di,[bx+size LineHeader]
	add	di,es:[bx].LineLength
	mov	dx,PageLimit
	cmp	bx,size PageHeader
	ja	@@9
	sub	dx,size LineHeader+1
@@9:	push	ds
	lds	si,H
	cmp	word ptr [si],VMemLimit
	ja	@@10
	pop	ds
	jmp	@@3
@@10:	mov	ds,[si]
	mov	si,BufPtr
	mov	cx,ReadBytes
	sub	cx,si
	jbe	@@19
@@11:	lodsb
	cmp	al,0dh
	jne	@@13
	cmp	IgnoreCR,0
	je	@@12
	mov	IgnoreCR,0
	jmp	@@18
@@12:	mov	IgnoreLF,1
	jmp	@@20
@@13:	cmp	al,0ah
	jne	@@15
	cmp	IgnoreLF,0
	je	@@14
	mov	IgnoreLF,0
	jmp	@@18
@@14:	mov	IgnoreCR,1
	jmp	@@20
@@15:	cmp	es:[bx].LineLength,4000
	jl	@@16
	or	ReadStatus,erLineTooLong
	jmp	@@17
@@16:	cmp	di,dx
	jae	@@22
	stosb
	inc	es:[bx].LineLength
@@17:	sub	ax,ax
	mov	IgnoreLF,ax
	mov	IgnoreCR,ax
@@18:	loop	@@11
@@19:	pop	ds
	call	ClosePage
	jmp	@@2
@@20:	mov	ax,ds
	pop	ds
	cmp	ds:CursorPos.Y,32767
	jne	@@21
	call	ClosePage
	mov	ax,erTooManyLines
	or	ds:Errors,al
	mov	ds:ReturnCode,ax
	jmp	@@23
@@21:	push	ds
	mov	ds,ax
	inc	di
	and	di,0fffeh
	mov	bx,di
	lea	di,[bx+size LineHeader]
	mov	dx,PageLimit
	sub	ax,ax
	cmp	di,dx
	ja	@@24
	mov	es:[bx].LineLength,ax
	mov	es:[bx].LineNumber,ax
	mov	es:[bx].LineDummy,al
	inc	AddedLines
	mov	ax,ds
	pop	ds
	push	ds
	inc	ds:CursorPos.Y
	mov	ds,ax
	jmp	@@18
@@22:	cmp	bx,size PageHeader
	jbe	@@18
	dec	si
	mov	BufPtr,si
	pop	ds
	call	ClosePage
	Invoke	InsertPage
	jc	@@23
	Invoke	LineToNextPage
	jc	@@23
	Invoke	ROfLine
	jmp	@@8
@@23:	jmp	@@25
@@24:	mov	di,bx
	mov	BufPtr,si
	pop	ds
	call	ClosePage
	Invoke	InsertPage
	jc	@@25
	Invoke	GoNextPage
	jc	@@25
	Invoke	GetCurPage
	Invoke	UseCurPage
	jc	@@25
	mov	bx,size PageHeader
	lea	di,[bx+size LineHeader]
	inc	di
	and	di,0fffeh
	sub	ax,ax
	mov	es:[bx].LineLength,ax
	mov	es:[bx].LineNumber,ax
	inc	ds:CursorPos.Y
	mov	bx,ds:CurPage
	mov	[bx].LinesInPage,1
	mov	[bx].PageLength,di
	jmp	@@8
@@25:	mov	bx,H.Offs
	mov	dx,H.Segm
	Invoke	FreePage
	Invoke	SetEditDS
@@26:	mov	ax,SaveOptions
	mov	ds:Options,ax
	mov	ax,ReadStatus
	or	ds:Errors,al
	mov	ds:ReturnCode,ax
	ret
_ReadTxt	endp

	Entry	ReadBlk
	Invoke	CheckLine
	Invoke	UpdateUndo
	Invoke	SetEditDS
	push	ds:Options
	or	ds:Options,eoUseTab+eoUndoDisabled+eoNoTruncate
	and	ds:Options,not (eoOverwrite+eoAutoIndent+eoBlockHidden+eoOptimalFill)
	push	ds:ScreenPos.Y
	push	ds:CursorPos.X ds:CursorPos.Y
	Invoke	EndCursorRaw
	mov	byte ptr ds:CParam,0dh
	Invoke	InsertAllowUndo
	Invoke	SetBlockBegRaw
	Invoke	SetEditDS
	dec	ds:BlockBeg.Y
	call	_ReadTxt
	inc	ds:BlockBeg.Y
	Invoke	EndCursorRaw
	Invoke	SetBlockEndRaw
	Invoke	SetEditDS
	test	ds:Errors,erNoVirtMem+erHeapFailure
	jz	@@1
	Invoke	DeleteBlockRaw
	Invoke	SetEditDS
	pop	ds:CursorPos.Y ds:CursorPos.X
	jmp	@@4
@@1:	pop	ds:CursorPos.Y ds:CursorPos.X
	Invoke	BlockShown
	jnz	@@2
	Invoke	SetBlockBegRaw
	Invoke	SetBlockEndRaw
	jmp	@@3
@@2:	Invoke	MoveBlockRaw
@@3:	Invoke	EndCursorRaw
	Invoke	FoolBS
	Invoke	MoveToBlockBegRaw
	Invoke	SetEditDS
@@4:	pop	ds:ScreenPos.Y
	pop	ds:Options
	Invoke	CenterFixScreen
	Invoke	HighlightBlock
	Exit	ReadBlk,X

	Entry	ReadFile
	push	ds
	pop	es
	lea	di,ds:PrevPos
	mov	cx,(Marks+size Marks-PrevPos) shr 1
	sub	ax,ax
	cld
	rep	stosw
	dec	ax
	mov	ds:UndoLen,ax
	push	ds:Options
	or	ds:Options,eoUseTab+eoUndoDisabled+eoNoTruncate
	and	ds:Options,not (eoOverwrite+eoAutoIndent+eoOptimalFill)
	lea	di,ds:LineLens
	mov	cx,50
	mov	al,255
	rep	stosb
	Invoke	ClearText
	Invoke	SetEditDS
	mov	ax,1
	mov	ds:CursorPos.X,ax
	mov	ds:CursorPos.Y,ax
	mov	byte ptr ds:CParam,'x'
	Invoke	InsertAllowUndo
	Invoke	FoolBS
	Invoke	SetEditDS
	call	_ReadTxt
	test	ds:Errors,erHeapFailure
	jz	@@1
	InvokeJ	ErrorExit
@@1:	Invoke	HomeCursorRaw
	Invoke	SetEditDS
	sub	ax,ax
	cmp	al,ds:Errors
	je	@@2
	mov	al,emShow+emUpdate
@@2:	mov	ds:Modified,al
	pop	ds:Options
	and	ds:Options,not eoUndoDisabled
	Exit	ReadFile

	Loc	CurLine,word,1
	Loc	StartX,word,1
	Loc	StartY,word,1
	Loc	EndX,word,1
	Loc	EndY,word,1
	Loc	Cnt,word,1
	Loc	Status,word,1
	Loc	H,dword,1

_Write	proc	near
	cmp	word ptr ds:LParam,-16
	jne	@@1
@@1:	push	word ptr ds:LParam
	les	bx,H
	push	es bx
	push	Cnt
	SetTurboDS
	Invoke	Write
	Invoke	SetEditDS
	cmp	ax,Cnt
	mov	Cnt,0
	ret
_Write	endp

_WriteTxt	proc	near
	sub	ax,ax
	mov	Status,ax
	mov	H.Offs,ax
	mov	H.Segm,ax
	Invoke	GetNewPage
	Invoke	SetEditDS
	jnc	@@1
	jmp	@@10
@@1:	mov	H.Offs,ax
	mov	H.Segm,dx
	push	dx ax
	SetTurboDS
	Invoke	LockHandle
	mov	ax,StartY
	mov	CurLine,ax
	mov	Cnt,0
@@2:	mov	ax,CurLine
	Invoke	FindLine
	Invoke	UseCurPage
	jc	@@3
	cmp	bx,dx
	je	@@3
	mov	ax,es:[bx].LineLength
	inc	ax
	inc	ax
	add	ax,Cnt
	cmp	ax,ds:PageSize
	jb	@@4
	call	_Write
	jz	@@2
	jmp	@@10
@@3:	jmp	@@9
@@4:	lea	si,[bx+size LineHeader]
	push	ds
	push	es
	pop	ds
	mov	cx,[bx].LineLength
	mov	ax,CurLine
	cmp	ax,EndY
	jne	@@5
	mov	ax,EndX
	cmp	ax,cx
	jae	@@5
	mov	cx,ax
@@5:	les	di,H
	cmp	word ptr es:[di],VMemLimit
	ja	@@6
	mov	bx,H.Offs
	mov	dx,H.Segm
	Invoke	FreePage
	InvokeJ	ErrorExit
@@6:	mov	es,es:[di]
	mov	di,Cnt
	sub	ax,ax
	xchg	ax,StartX
	add	si,ax
	sub	cx,ax
	jae	@@7
	sub	cx,cx
@@7:	shr	cx,1
	rep	movsw
	rcl	cx,1
	rep	movsb
	mov	ax,CurLine
	cmp	ax,EndY
	je	@@8
	mov	ax,0a0dh
	stosw
@@8:	mov	Cnt,di
	pop	ds
	mov	ax,CurLine
	inc	CurLine
	cmp	ax,EndY
	jae	@@9
	jmp	@@2
@@9:	call	_Write
	jz	@@11
@@10:	mov	Status,-1
@@11:	mov	bx,H.Offs
	mov	dx,H.Segm
	mov	ax,bx
	or	ax,dx
	jz	@@12
	Invoke	FreePage
@@12:	Invoke	SetEditDS
	mov	ax,Status
	test	ax,ax
	jz	@@13
	mov	ds:ReturnCode,ax
	stc
@@13:	ret
_WriteTxt	endp

	Entry	WriteFile
	test	ds:Modified,emShow+emUpdate
	jz	@@1
	mov	ax,ds:CursorPos.Y
	cmp	ax,ds:UndoBeg.Y
	jb	@@1
	cmp	ax,ds:UndoEnd.Y
	ja	@@1
	Invoke	OptimalFill
	Invoke	TruncateWS
	Invoke	SetEditDS
@@1:	sub	ax,ax
	mov	StartX,ax
	mov	EndX,-1
	mov	StartY,1
	Invoke	TotalLines
	Invoke	SetEditDS
	mov	EndY,ax
	call	_WriteTxt
	jc	@@2
	and	ds:Modified,not (emShow+emUpdate)
@@2:	Exit	WriteFile,X

	Entry	WriteBlk
	test	ds:Modified,emShow+emUpdate
	jz	@@1
	mov	ax,ds:CursorPos.Y
	cmp	ax,ds:UndoBeg.Y
	jb	@@1
	cmp	ax,ds:UndoEnd.Y
	ja	@@1
	Invoke	OptimalFill
	Invoke	TruncateWS
@@1:	Invoke	BlockShown
	Invoke	SetEditDS
	jz	@@2
	mov	ax,ds:BlockBeg.X
	dec	ax
	mov	StartX,ax
	mov	ax,ds:BlockBeg.Y
	mov	StartY,ax
	mov	ax,ds:BlockEnd.X
	dec	ax
	mov	EndX,ax
	mov	ax,ds:BlockEnd.Y
	mov	EndY,ax
	call	_WriteTxt
@@2:	Exit	WriteBlk

_TotalLines	proc	near
	mov	dx,ds:LastPage
	lea	bx,ds:PageTbl
	sub	ax,ax
	jmp	@@2
@@1:	add	ax,[bx].LinesInPage
	jc	@@3
	add	bx,size PageInfo
@@2:	cmp	bx,dx
	jb	@@1
	jmp	@@4
@@3:	mov	ax,-1
@@4:	ret
_TotalLines	endp

	Loc	Dummy,word,2
	Entry	PushPos
	cmp	ds:VFilePoint.Y,0
	je	@@4
	mov	cx,4
	lea	di,ds:VFileStack
@@1:	cmp	[di].W0,0
	jne	@@2
	cmp	[di].W2,0
	je	@@3
@@2:	add	di,8
	loop	@@1
	mov	ds:ReturnCode,-1
	jmp	@@5
@@3:	push	cx
	push	ds
	pop	es
	mov	cx,4
	lea	si,ds:VFileOffset
	cld
	rep	movsw
	pop	cx
@@4:	sub	ax,ax
	mov	ds:VFileOffset.W0,ax
	mov	ds:VFileOffset.W2,ax
	mov	ds:VFilePoint.X,ax
	inc	ax
	mov	ds:VFilePoint.Y,ax
@@5:	Exit	PushPos

	Loc	Dummy,word,2
	Entry	PopPos
	cmp	ds:VFilePoint.Y,0
	je	@@3
	mov	cx,4
	lea	si,ds:VFileStack+3*8
@@1:	cmp	[si+4].Y,0
	jne	@@2
	sub	si,8
	loop	@@1
	push	ds
	pop	es
	sub	ax,ax
	mov	cx,4
	lea	di,ds:VFileOffset
	cld
	rep	stosw
	jmp	@@3
@@2:	push	ds
	pop	es
	lea	di,ds:VFileOffset
	mov	cx,4
	cld
	rep	movsw
	sub	ax,ax
	mov	cx,8
	mov	di,si
	sub	di,cx
	shr	cx,1
	cld
	rep	stosw
@@3:	Exit	PopPos

	Loc	PosLo,word,1
	Loc	PosHi,word,1
	Entry	SeekPos
	cmp	ds:VFilePoint.Y,0
	jne	@@1
	jmp	@@15
@@1:	cmp	ds:WParam,1
	je	@@4
	jl	@@2
	mov	ax,65535
	mov	dx,32767
	jmp	@@3
@@2:	mov	ax,ds:LParam.W0
	mov	dx,ds:LParam.W2
@@3:	mov	ds:VFileOffset.W0,ax
	mov	ds:VFileOffset.W2,dx
	sub	ax,ax
	mov	ds:VFilePoint.X,ax
	mov	PosLo,ax
	mov	PosHi,ax
	inc	ax
	mov	ds:VFilePoint.Y,ax
	jmp	@@5
@@4:	mov	ax,ds:VFileOffset.W0
	mov	dx,ds:VFileOffset.W2
	mov	PosLo,ax
	mov	PosHi,dx
	add	ax,ds:LParam.W0
	adc	dx,ds:LParam.W2
	mov	ds:VFileOffset.W0,ax
	mov	ds:VFileOffset.W2,dx
@@5:	mov	ax,PosHi
	cmp	ax,ds:VFileOffset.W2
	jg	@@9
	jl	@@6
	mov	ax,PosLo
	cmp	ax,ds:VFileOffset.W0
	jae	@@9
@@6:	mov	ax,ds:VFilePoint.Y
	Invoke	FindLine
	Invoke	UseCurPage
	jc	@@11
	cmp	dx,bx
	je	@@12
	mov	ax,es:[bx].LineLength
	inc	ax
	inc	ax
	sub	ax,ds:VFilePoint.X
	sub	dx,dx
	mov	ds:VFilePoint.X,dx
	add	ax,PosLo
	adc	dx,PosHi
	cmp	dx,ds:VFileOffset.W2
	jb	@@7
	ja	@@8
	cmp	ax,ds:VFileOffset.W0
	ja	@@8
	je	@@10
@@7:	mov	PosLo,ax
	mov	PosHi,dx
	inc	ds:VFilePoint.Y
	jmp	@@6
@@8:	mov	ax,ds:VFileOffset.W0
	sub	ax,PosLo
	mov	ds:VFilePoint.X,ax
@@9:	jmp	@@13
@@10:	mov	PosLo,ax
	mov	PosHi,dx
	inc	ds:VFilePoint.Y
	jmp	@@14
@@11:	sub	ax,ax
	mov	ds:VFilePoint.X,ax
	mov	PosLo,ax
	mov	PosHi,ax
	inc	ax
	mov	ds:VFilePoint.Y,ax
	jmp	@@13
@@12:	mov	ds:VFilePoint.X,0
	call	_TotalLines
	inc	ax
	mov	ds:VFilePoint.Y,ax
@@13:	mov	ax,PosLo
	mov	dx,PosHi
@@14:	mov	ds:VFileOffset.W0,ax
	mov	ds:VFileOffset.W2,dx
@@15:	Exit	SeekPos

	Loc	Buf,word,1
	Loc	Cnt,word,1
	Loc	Len,word,1
	Loc	Dummy,word,1
	Entry	GetText
	cmp	ds:VFilePoint.Y,0
	jne	@@1
	mov	ds:ReturnCode,-1
	jmp	@@13
@@1:	mov	ax,ds:WParam
	mov	Cnt,ax
	mov	ax,word ptr ds:LParam
	mov	Buf,ax
@@2:	mov	cx,Cnt
	jcxz	@@3
	jmp	@@5
@@3:	jmp	@@11
@@4:	jmp	@@12
@@5:	mov	ax,ds:VFilePoint.Y
	push	cx
	Invoke	FindLine
	Invoke	UseCurPage
	pop	cx
	jc	@@4
	cmp	dx,bx
	je	@@4
	lea	si,[bx+size LineHeader]
	add	si,ds:VFilePoint.X
	mov	ax,es:[bx].LineLength
	mov	Len,ax
	mov	dx,es
	les	di,ds:LParam
	mov	di,Buf
	sub	ax,ds:VFilePoint.X
	jbe	@@7
	cmp	cx,ax
	jbe	@@6
	mov	cx,ax
@@6:	sub	Cnt,cx
	add	ds:VFilePoint.X,cx
	push	ds
	mov	ds,dx
	cld
	shr	cx,1
	rep	movsw
	rcl	cx,1
	rep	movsb
	pop	ds
@@7:	mov	cx,Cnt
	jcxz	@@12
	mov	ax,ds:VFilePoint.X
	cmp	ax,Len
	je	@@8
	dec	ax
	cmp	ax,Len
	je	@@9
	jmp	@@10
@@8:	mov	al,0dh
	inc	ds:VFilePoint.X
	stosb
	loop	@@9
	jmp	@@11
@@9:	mov	al,0ah
	stosb
	dec	cx
@@10:	inc	ds:VFilePoint.Y
	mov	ds:VFilePoint.X,0
	mov	Cnt,cx
	mov	Buf,di
	jmp	@@2
@@11:	mov	Cnt,cx
@@12:	mov	ax,ds:WParam
	sub	ax,Cnt
	mov	ds:ReturnCode,ax
	add	ds:VFileOffset.W0,ax
	adc	ds:VFileOffset.W2,0
@@13:	Exit	GetText

	Entry	TotalLines
	call	_TotalLines
	Exit	TotalLines

	Entry	GetTotalLines
	call	_TotalLines
	mov	ds:ReturnCode,ax
	Exit	GetTotalLines

	end
