	include	ed.inc

	.code

CheckBpts	proc	near
	mov	ax,es
	or	ax,si
	jz	@@3
	mov	cx,es:[si].Count
	inc	si
	inc	si
	jcxz	@@3
	mov	di,size TEBpt
@@1:	cmp	es:[si].Pos.X,0
	jle	@@2
	cmp	bx,es:[si].Pos.Y
	je	@@4
@@2:	add	si,di
	loop	@@1
@@3:	clc
	ret
@@4:	mov	al,byte ptr es:[si].Color
	mov	ah,al
	stc
	ret
CheckBpts	endp

	Entry	InvalidateScreen
	push	ds
	pop	es
	lea	di,ds:LineLens
	mov	cx,50
	mov	al,-1
	cld
	rep	stosb
	Invoke	FullPaintScreen
	Exit	InvalidateScreen

	Entry	FullPaintScreen
	or	ds:RedrawFlags,rfFullScreen
	Invoke	SmartRefreshScreen
	Exit	FullPaintScreen

	Loc	CurLine,word,1
	Loc	HiliteStart,word,1
	Loc	HiliteEnd,word,1
	Loc	DrawColor,word,1
	Loc	SaveEditViewOffs,word,1
	Loc	SaveEditViewSegm,word,1
	Loc	HiliteStartX,word,1
	Loc	HiliteStartY,word,1
	Loc	HiliteEndX,word,1
	Loc	HiliteEndY,word,1
	Loc	SaveCurEdOffs,word,1
	Loc	SaveCurEdSegm,word,1
	Loc	SaveRF,word,1
	Loc	NoDrawSearchHilite,word,1

CheckSearchHilite	proc	near
	cmp	NoDrawSearchHilite,0
	jne	@@3
	mov	bx,ds:SearchPos
	test	bx,bx
	jle	@@3
	mov	ax,ds:TempPos.Y
	cmp	ax,ds:CursorPos.Y
	jb	@@2
	ja	@@1
	cmp	bx,ds:TempPos.X
	je	@@3
	jb	@@2
@@1:	dec	bx
	mov	HiliteEndX,bx
	mov	HiliteEndY,ax
	mov	ax,ds:CursorPos.Y
	mov	bx,ds:TempPos.X
	dec	bx
	js	@@3
	mov	HiliteStartX,bx
	mov	HiliteStartY,ax
	jmp	@@4
@@2:	dec	bx
	mov	HiliteStartX,bx
	mov	HiliteStartY,ax
	mov	ax,ds:CursorPos.Y
	mov	bx,ds:TempPos.X
	dec	bx
	js	@@3
	mov	HiliteEndX,bx
	mov	HiliteEndY,ax
	jmp	@@4
@@3:	sub	ax,ax
	mov	HiliteStartY,ax
	mov	HiliteEndY,ax
	dec	ax
	mov	HiliteStartX,ax
	mov	HiliteEndX,ax
@@4:	ret
CheckSearchHilite	endp

RefreshLine	proc	near
	mov	CurLine,ax
	mov	ax,word ptr ds:Colors[0]
	mov	DrawColor,ax
	mov	ax,-1
	mov	HiliteStart,ax
	mov	HiliteEnd,ax
	push	si di
	cld
	sub	bx,bx
	mov	ax,CurLine
	add	ax,ds:ScreenPos.Y
	cmp	ax,HiliteStartY
	jb	@@3
	ja	@@1
	mov	bx,HiliteStartX
@@1:	cmp	ax,HiliteEndY
	ja	@@3
	mov	HiliteStart,bx
	mov	bx,HiliteEndX
	jne	@@2
	mov	HiliteEnd,bx
@@2:	jmp	@@8
@@3:	cmp	ax,ds:ExecBar
	jne	@@4
	mov	al,ds:Colors[2]
	mov	ah,al
	mov	DrawColor,ax
	mov	HiliteStart,0
	jmp	@@8
@@4:	mov	bx,ax
	les	si,ds:BreakPts
	call	CheckBpts
	jnc	@@5
	mov	DrawColor,ax
	mov	HiliteStart,0
	jmp	@@8
@@5:	mov	ax,-1
	mov	HiliteStart,ax
	Invoke	BlockShown
	Invoke	SetEditDS
	jz	@@8
	mov	ax,ds:BlockBeg.X
	mov	bx,ds:BlockBeg.Y
	mov	cx,ds:BlockEnd.X
	mov	dx,ds:BlockEnd.Y
	dec	ax
	dec	cx
	mov	si,ds:ScreenPos.Y
	add	si,CurLine
	cmp	si,bx
	jb	@@8
	jne	@@6
	mov	HiliteStart,ax
@@6:	cmp	si,dx
	ja	@@8
	jne	@@7
	mov	HiliteEnd,cx
@@7:	cmp	si,bx
	je	@@8
	mov	HiliteStart,0
@@8:	mov	ax,HiliteStart
	sub	HiliteEnd,ax
	mov	ax,ds:ScreenPos.Y
	add	ax,CurLine
	Invoke	FindLine
	Invoke	UseCurPage
	jc	@@15
	cmp	bx,dx
	jae	@@15
	mov	cx,es:[bx].LineLength
	lea	si,[bx+size LineHeader]
	sub	dx,dx
	mov	di,ds:ScreenPos.X
	dec	di
	mov	bx,HiliteStart
@@9:	inc	bx
@@10:	dec	bx
	jz	@@11
	jcxz	@@15
	cmp	dx,di
	jae	@@16
	seges	lodsb
	cmp	al,9
	je	@@12
	inc	dx
	loop	@@10
	jmp	@@15
@@11:	mov	ax,DrawColor
	xchg	ah,al
	mov	DrawColor,ax
	mov	bx,HiliteEnd
	mov	HiliteEnd,-1
	jmp	@@9
@@12:	mov	ax,dx
	push	cx
	mov	cx,ds:TabSize
	cmp	cx,1
	jg	@@13
	inc	ax
	jmp	@@14
@@13:	add	ax,cx
	sub	dx,dx
	push	ax
	div	cx
	pop	ax
	sub	ax,dx
@@14:	pop	cx
	mov	dx,ax
	loop	@@10
@@15:	push	ss
	pop	es
	mov	cx,ds:WindowWidth
	lea	di,[bp-@lp]
	mov	bx,di
	sub	di,cx
	sub	di,cx
	jmp	@@25
@@16:	inc	bx
	mov	HiliteStart,bx
	mov	ax,dx
	sub	ax,di
	lea	di,[bp-@lp]
	mov	bx,ds:WindowWidth
	sub	di,bx
	sub	di,bx
	cmp	ax,bx
	jbe	@@17
	mov	ax,bx
@@17:	shl	bx,1
	add	bx,di
	mov	ah,DrawColor.B0
	push	ds
	push	ss es
	pop	ds es
	inc	cx
	cmp	al,0
	je	@@19
	push	cx
	mov	cl,al
	mov	ch,0
	mov	al,' '
	rep	stosw
	pop	cx
	jmp	@@19
@@18:	cmp	di,bx
	jae	@@24
	lodsb
	cmp	al,9
	je	@@20
	stosw
	inc	dx
@@19:	dec	HiliteStart
	jz	@@23
	loop	@@18
	jmp	@@24
@@20:	pop	es
	push	es
	push	cx
	push	ax
	mov	ax,dx
	sub	dx,dx
	mov	cx,es:TabSize
	push	ss
	pop	es
	cmp	cx,1
	jle	@@21
	div	cx
	sub	cx,dx
@@21:	mov	ax,bx
	sub	ax,di
	shr	ax,1
	cmp	ax,cx
	jae	@@22
	mov	cx,ax
@@22:	pop	ax
	mov	al,' '
	rep	stosw
	pop	cx
	sub	dx,dx
	jmp	@@19
@@23:	mov	ax,-1
	xchg	ax,HiliteEnd
	mov	HiliteStart,ax
	mov	ax,DrawColor
	xchg	ah,al
	mov	DrawColor,ax
	xchg	ah,al
	loop	@@18
@@24:	pop	ds
	mov	cx,bx
	sub	cx,di
	shr	cx,1
@@25:	mov	ax,DrawColor
	cmp	al,ds:Colors[0]
	je	@@26
	mov	ah,al
	mov	al,' '
	rep	stosw
@@26:	lea	ax,[di+@lp]
	sub	ax,bp
	add	ax,ds:WindowWidth
	add	ax,ds:WindowWidth
	shr	ax,1
	mov	si,CurLine
	xchg	al,ds:LineLens[si]
	sub	al,ds:LineLens[si]
	jb	@@28
	mov	cx,bx
	sub	cx,di
	shr	cx,1
	cmp	ax,cx
	ja	@@27
	mov	cx,ax
@@27:	mov	ah,ds:Colors[0]
	mov	al,' '
	rep	stosw
@@28:	lea	bx,[bp-@lp]
	sub	bx,ds:WindowWidth
	sub	bx,ds:WindowWidth
	sub	di,bx
	shr	di,1
	cmp	di,ds:WindowWidth
	jbe	@@29
	mov	di,ds:WindowWidth
@@29:	mov	ax,CurLine
	inc	ax
	push	di es bx
	SetTurboDS
	Invoke	WriteOneLine
	Invoke	SetEditDS
	pop	di si
	ret
RefreshLine	endp

Refresh	proc	near
	push	ds:EditView
	SetTurboDS
	Invoke	GetCurEditView
	mov	SaveEditViewOffs,ax
	mov	SaveEditViewSegm,dx
	SetTurboDS
	Invoke	SetCurEditView
	Invoke	SetEditDS
	call	CheckSearchHilite
	mov	ax,HiliteStartX
	test	ax,ax
	js	@@1
	or	ds:RedrawFlags,rfFullScreen
@@1:	sub	bx,bx
	mov	cx,ds:WindowHeight
	mov	ax,ds:ScreenPos.Y
	cmp	ds:PrevScreenRow,ax
	jne	@@8
	mov	ax,ds:ScreenPos.X
	test	ax,ax
	jg	@@2
	mov	ax,1
	mov	ds:ScreenPos.X,ax
@@2:	cmp	ds:PrevScreenCol,ax
	jne	@@8
	mov	ax,ds:RedrawFlags
	test	ax,rfFullScreen
	jnz	@@8
	mov	bx,ds:RedrawLine
	sub	bx,ds:ScreenPos.Y
	jz	@@3
	dec	bx
@@3:	test	ax,rfBelowPrevLine
	jnz	@@5
	mov	bx,ds:RedrawLine
	sub	bx,ds:ScreenPos.Y
	test	ax,rfBelowCurLine
	jnz	@@5
	mov	cx,bx
	inc	cx
	test	ax,rfCurLine
	jnz	@@5
	mov	ax,word ptr ds:SmartBounds
	test	ax,ax
	jnz	@@4
	jmp	@@16
@@4:	mov	cl,ah
	mov	bl,al
	mov	ch,0
	mov	bh,0
@@5:	mov	ax,word ptr ds:SmartBounds
	test	ax,ax
	jz	@@7
	cmp	al,bl
	jae	@@6
	mov	bl,al
@@6:	cmp	ah,cl
	jbe	@@7
	mov	cl,ah
@@7:	cmp	cx,ds:WindowHeight
	jbe	@@8
	mov	cx,ds:WindowHeight
@@8:	mov	ds:SmartBounds[0],bl
	mov	ds:SmartBounds[1],cl
	cmp	bl,cl
	jb	@@9
	jmp	@@15
@@9:	mov	ax,ds:RedrawLine
	sub	ax,ds:ScreenPos.Y
	cmp	al,bl
	jbe	@@10
	cmp	al,cl
	jae	@@12
	sub	cx,bx
	shr	cl,1
	sub	al,bl
	cmp	al,cl
	ja	@@12
@@10:	mov	al,ds:SmartBounds[0]
	sub	ah,ah
	inc	ax
	push	ax
	mov	al,1
	push	ax
	SetTurboDS
	Invoke	SetLine
	Invoke	SetEditDS
@@11:	mov	al,ds:SmartBounds[0]
	sub	ah,ah
	call	RefreshLine
	inc	ds:SmartBounds[0]
	mov	al,ds:SmartBounds[0]
	cmp	al,ds:SmartBounds[1]
	jb	@@11
	jmp	@@14
@@12:	mov	al,ds:SmartBounds[1]
	sub	ah,ah
	push	ax
	sub	ax,ax
	push	ax
	SetTurboDS
	Invoke	SetLine
	Invoke	SetEditDS
@@13:	dec	ds:SmartBounds[1]
	js	@@14
	mov	al,ds:SmartBounds[1]
	mov	ah,0
	call	RefreshLine
	mov	al,ds:SmartBounds[0]
	cmp	al,ds:SmartBounds[1]
	jbe	@@13
@@14:	SetTurboDS
	Invoke	FlushScreen
	Invoke	SetEditDS
@@15:	sub	ax,ax
	mov	word ptr ds:SmartBounds,ax
@@16:	mov	ax,ds:CursorPos.X
	sub	ax,ds:ScreenPos.X
	js	@@18
	inc	ax
	cmp	ax,ds:WindowWidth
	ja	@@18
	push	ax
	mov	ax,ds:CursorPos.Y
	sub	ax,ds:ScreenPos.Y
	js	@@17
	inc	ax
	cmp	ax,ds:WindowHeight
	jbe	@@19
@@17:	pop	ax
@@18:	sub	ax,ax
	push	ax
@@19:	push	ax
	SetTurboDS
	Invoke	SetEditorCursor
	push	SaveEditViewSegm
	push	SaveEditViewOffs
	SetTurboDS
	Invoke	SetCurEditView
	Invoke	SetEditDS
	mov	ax,ds:ScreenPos.Y
	mov	ds:PrevScreenRow,ax
	mov	ax,ds:ScreenPos.X
	mov	ds:PrevScreenCol,ax
	ret
Refresh	endp

	Entry	SmartestRefreshScreen
	and	ds:Modified,not emLocked
	SetTurboDS
	Invoke	RedrawLocked
	Invoke	SetEditDS
	test	ax,ax
	jz	@@1
	or	ds:Modified,emLocked
	jmp	@@2
@@1:	mov	ax,ds:WindowWidth
	shl	ax,1
	sub	sp,ax
	push	ax
	mov	NoDrawSearchHilite,0
	call	Refresh
	pop	ax
	add	sp,ax
@@2:	Exit	SmartestRefreshScreen,X

	Entry	SmartRefreshScreen
	mov	ax,ds:WindowWidth
	shl	ax,1
	sub	sp,ax
	push	ax
	and	ds:Modified,not emLocked
	mov	NoDrawSearchHilite,0
	call	Refresh
	pop	ax
	add	sp,ax
	Exit	SmartRefreshScreen,X

	Entry	RefreshChain
	mov	ax,ds:RedrawFlags
	test	ax,rfAny
	jz	@@4
	mov	SaveRF,ax
	mov	NoDrawSearchHilite,1
	mov	ax,ds:CursorPos.Y
	mov	ds:RedrawLine,ax
	push	ds
	SetTurboDS
	les	bx,CurEd
	mov	SaveCurEdOffs,bx
	mov	SaveCurEdSegm,es
	les	bx,es:[bx].FileRec
	les	si,es:[bx].Editor
	pop	ds
	test	ds:Modified,emLocked
	jnz	@@4
@@1:	mov	dx,es
	mov	ax,dx
	or	ax,si
	jz	@@4
	cmp	si,SaveCurEdOffs
	jne	@@2
	cmp	dx,SaveCurEdSegm
	je	@@3
@@2:	mov	ax,es:[si].Handle.Offs
	cmp	ax,ds:Handle.Offs
	jne	@@3
	mov	ax,es:[si].Handle.Segm
	cmp	ax,ds:Handle.Segm
	jne	@@3
	mov	ax,SaveRF
	mov	es:[si].RedrawFlags,ax
	jmp	@@5
@@3:	les	si,es:[si].Next
	jmp	@@1
@@4:	jmp	@@7
@@5:	mov	ax,es:[si].WindowWidth
	shl	ax,1
	sub	sp,ax
	push	ax
	push	si
	push	es si
	sub	di,di
	mov	ax,ds
	mov	bx,es
	mov	es,ax
	mov	ds,bx
	mov	cx,size TEditor shr 1
	cld
	rep	movsw
	mov	ds,ax
	call	Refresh
	SetTurboDS
	lds	si,EdHandle
	mov	ds,[si]
	and	ds:RedrawFlags,not rfAny
	pop	di es
	sub	si,si
	mov	cx,size TEditor shr 1
	cld
	rep	movsw
	pop	si
	jmp	@@3
@@6:	pop	ax
	add	sp,ax
@@7:	Exit	RefreshChain

	.data

	extrn	CurEd:dword,EdHandle:dword

	end
