	include	ed.inc

	.code

	Loc	Direction,word,1
	Loc	OrigX,word,1
	Loc	OrigY,word,1
	Loc	Column,word,1
	Loc	Rest,word,1
	Loc	RealDir,word,1
	Loc	Nested,word,1
	Loc	Level,word,1
	Loc	Token1,word,1
	Loc	Token2,word,1

GetToken	proc	near
	sub	ax,ax
	mov	cx,Rest
	jcxz	@@1
	mov	al,es:[bx]
	dec	cx
	jz	@@1
	mov	ah,es:[bx+1]
@@1:	clc
	ret
GetToken	endp

Init	proc	near
	Invoke	GoCurPos
	Invoke	UseCurPage
	jc	@@1
	mov	ax,bx
	sub	ax,di
	sub	ax,size LineHeader
	mov	Column,ax
	neg	ax
	add	ax,es:[di].LineLength
	mov	Rest,ax
	call	GetToken
@@1:	ret
Init	endp

NextLine	proc	near
	inc	ds:CursorPos.y
	mov	ax,ds:CursorPos.Y
	push	ax
	Invoke	FindCurLine
	Invoke	UseCurPage
	pop	ax
	jc	@@2
	cmp	ax,ds:CursorPos.Y
	je	@@1
	mov	ax,-1
	and	ax,ax
	jmp	@@2
@@1:	mov	Column,0
	mov	ax,es:[bx].LineLength
	mov	Rest,ax
	add	bx,size LineHeader
	sub	ax,ax
@@2:	ret
NextLine	endp

NextToken	proc	near
	inc	bx
	inc	Column
	dec	Rest
	jns	@@1
	call	NextLine
	jc	@@2
	js	@@2
@@1:	call	GetToken
@@2:	ret
NextToken	endp

PrevLine	proc	near
	dec	ds:CursorPos.Y
	jnz	@@1
	mov	ax,-1
	and	ax,ax
	jmp	@@2
@@1:	Invoke	FindCurLine
	Invoke	UseCurPage
	jc	@@2
	mov	Rest,0
	mov	ax,es:[bx].LineLength
	mov	Column,ax
	add	bx,size LineHeader
	add	bx,ax
	sub	ax,ax
@@2:	ret
PrevLine	endp

CompareToken	proc	near
	cmp	cl,al
	jne	@@2
	test	ch,ch
	jz	@@1
	cmp	ch,ah
	jne	@@2
@@1:	ret
@@2:	stc
	ret
CompareToken	endp

PrevToken	proc	near
	dec	bx
	inc	Rest
	dec	Column
	jns	@@1
	call	PrevLine
	jc	@@2
	js	@@2
@@1:	call	GetToken
@@2:	ret
PrevToken	endp

MatchPair	proc	near
	mov	Direction,ax
	mov	ax,ds:CursorPos.X
	mov	OrigX,ax
	mov	ax,ds:CursorPos.Y
	mov	OrigY,ax
	sub	ax,ax
	mov	RealDir,ax
	mov	Nested,ax
@@1:	call	Init
	jc	@@4
	les	bx,ds:CommandTable
	mov	bx,es:Matches
@@2:	test	byte ptr es:[bx],2
	jz	@@3
	jmp	@@13
@@3:	mov	cx,es:[bx+1]
	mov	dx,es:[bx+3]
	call	CompareToken
	jnc	@@6
	xchg	cx,dx
	call	CompareToken
	jnc	@@5
	add	bx,5
	jmp	@@2
@@4:	jmp	@@14
@@5:	inc	RealDir
@@6:	mov	al,es:[bx]
	and	ax,1
	mov	Nested,ax
	mov	Token1,dx
	mov	Token2,cx
	cmp	cx,dx
	jne	@@7
	mov	ax,Direction
	mov	RealDir,ax
	mov	Nested,0
@@7:	call	Init
	cmp	Nested,0
	jne	@@17
	mov	cx,Token2
	test	ch,ch
	jz	@@9
	cmp	RealDir,0
	jne	@@8
	call	NextToken
	jmp	@@9
@@8:	call	PrevToken
@@9:	cmp	RealDir,0
	jne	@@10
	call	NextToken
	jmp	@@11
@@10:	call	PrevToken
@@11:	jc	@@14
	cmp	ax,-1
	je	@@13
	mov	cx,Token1
	call	CompareToken
	jc	@@9
@@12:	push	bx
	Invoke	GoCurPos
	Invoke	UseCurPage
	pop	bx
	Invoke	CalcColumn
	Invoke	SetEditDS
	mov	ds:CursorPos.X,ax
	Invoke	CenterFixScreen
	Invoke	SetEditDS
	clc
	jmp	@@16
@@13:	clc
	jmp	@@15
@@14:	stc
@@15:	mov	ax,OrigX
	mov	ds:CursorPos.X,ax
	mov	ax,OrigY
	mov	ds:CursorPos.Y,ax
@@16:	ret
@@17:	mov	Level,1
	mov	cx,Token2
@@18:	test	ch,ch
	jz	@@20
	cmp	RealDir,0
	jne	@@19
	call	NextToken
	jmp	@@20
@@19:	call	PrevToken
@@20:	cmp	RealDir,0
	jne	@@21
	call	NextToken
	jmp	@@22
@@21:	call	PrevToken
@@22:	jc	@@14
	cmp	ax,-1
	je	@@13
	mov	cx,Token1
	call	CompareToken
	jc	@@23
	dec	Level
	jz	@@12
	jmp	@@18
@@23:	mov	cx,Token2
	call	CompareToken
	jc	@@20
	inc	Level
	jmp	@@18
MatchPair	endp

	Entry	MatchPairForward
	sub	ax,ax
	call	MatchPair
	Exit	MatchPairForward,X

	Entry	MatchPairBackward
	mov	ax,1
	call	MatchPair
	Exit	MatchPairBackward

	end
