	include	ed.inc

	.code

GoColumn	proc	near
	Invoke	UseCurPage
	push	si
	mov	cx,es:[bx].LineLength
	mov	di,bx
	lea	bx,[bx+size LineHeader]
	mov	dx,ax
	sub	ax,ax
	jcxz	@@5
@@1:	cmp	ax,dx
	jae	@@5
	cmp	byte ptr es:[bx],9
	je	@@2
	inc	bx
	inc	ax
	loop	@@1
	jmp	@@5
@@2:	mov	si,ax
	push	dx cx
	mov	cx,ds:TabSize
	cmp	cx,1
	jg	@@3
	inc	ax
	jmp	@@4
@@3:	add	ax,cx
	push	ax
	sub	dx,dx
	div	cx
	pop	ax
	sub	ax,dx
@@4:	pop	cx dx
	inc	bx
	loop	@@1
@@5:	xchg	ax,dx
	sub	ax,dx
	jae	@@6
	dec	bx
	add	ax,dx
	sub	ax,si
@@6:	pop	si
	ret
GoColumn	endp

	Entry	GoLine
	push	di
	Invoke	FindCurLine
	Invoke	UseCurPage
	mov	ax,0
	jc	@@1
	cmp	bx,dx
	jae	@@1
	dec	ax
	call	GoColumn
	not	ax
@@1:	pop	di
	Exit	GoLine

	Entry	GoCurPos
	Invoke	FindCurLine
	Invoke	UseCurPage
	jc	@@2
	mov	di,bx
	cmp	bx,dx
	mov	ax,ds:CursorPos.X
	jae	@@1
	dec	ax
	call	GoColumn
	clc
	jmp	@@3
@@1:	dec	ax
	jmp	@@3
@@2:	sub	ax,ax
	mov	es,ax
	mov	di,ax
	mov	bx,ax
	stc
@@3:	Exit	GoCurPos

	Entry	CalcColumn
	Invoke	UseCurPage
	sub	ax,ax
	cmp	di,bx
	je	@@5
	mov	cx,bx
	lea	bx,[di+size LineHeader]
	sub	cx,bx
	jcxz	@@5
@@1:	cmp	byte ptr es:[bx],9
	je	@@2
	inc	bx
	inc	ax
	loop	@@1
	inc	ax
	jmp	@@6
@@2:	push	cx
	mov	cx,ds:TabSize
	cmp	cx,1
	jg	@@3
	inc	ax
	jmp	@@4
@@3:	add	ax,cx
	sub	dx,dx
	push	ax
	div	cx
	pop	ax
	sub	ax,dx
@@4:	pop	cx
	inc	bx
	loop	@@1
@@5:	inc	ax
@@6:	Exit	CalcColumn

_FindLine	proc	near
	Invoke	FindPage
	Invoke	SetEditDS
	jc	@@5
	cmp	cx,[bx].LinesInPage
	je	@@5
	push	cx
	Invoke	GetCurPage
	Invoke	UseCurPage
	pop	cx
	jc	@@5
	sub	si,si
	mov	dx,[bx].PageLength
	mov	bx,size PageHeader
	mov	ax,cx
	cmp	ax,es:[si].CurLineNum
	jb	@@1
	mov	bx,es:[si].CurLineOfs
	sub	cx,es:[si].CurLineNum
	jz	@@4
@@1:	mov	es:CurLineNum,ax
	jcxz	@@3
@@2:	cmp	bx,dx
	jae	@@6
	add	bx,es:[bx].LineLength
	add	bx,size LineHeader+1
	and	bl,0feh
	loop	@@2
@@3:	mov	es:CurLineOfs,bx
@@4:	clc
	jmp	@@8
@@5:	mov	bx,dx
	jmp	@@7
@@6:	mov	es:CurLineNum,-1
@@7:	stc
@@8:	ret
_FindLine	endp

	Entry	FindCurLine
	Invoke	CheckLine
	Invoke	SetEditDS
	mov	ax,ds:CursorPos.Y
	call	_FindLine
	Exit	FindCurLine

	Entry	FindLine
	call	_FindLine
	Exit	FindLine

	Entry	FindPage
	cmp	ax,1
	adc	al,0
	mov	cx,ax
	mov	bx,ds:CurPage
	mov	dx,ds:CPFirstLine
	cmp	bx,ds:LastPage
	jb	@@1
	cmp	bx,offset PageTbl
	ja	@@2
	stc
	jmp	@@6
@@1:	cmp	cx,dx
	jae	@@3
	cmp	bx,offset PageTbl
	jbe	@@2
	sub	bx,size PageInfo
	sub	dx,[bx].LinesInPage
	jnc	@@1
@@2:	lea	bx,ds:PageTbl
	mov	dx,1
	jmp	@@1
@@3:	sub	cx,dx
	cmp	cx,[bx].LinesInPage
	jb	@@5
	add	cx,dx
	mov	ax,bx
	add	ax,size PageInfo
	cmp	ax,ds:LastPage
	jae	@@4
	add	dx,[bx].LinesInPage
	mov	bx,ax
	jmp	@@3
@@4:	mov	cx,[bx].LinesInPage
@@5:	mov	ds:CurPage,bx
	mov	ds:CPFirstLine,dx
	clc
@@6:	Exit	FindPage

	end
