	include	ed.inc

	.code

	Entry	HideBlock
	test	ds:Options,eoBlockHidden
	jnz	@@1
	or	ds:Options,eoBlockHidden
	Invoke	BlockPresent
	Invoke	SetEditDS
	jz	@@1
	or	ds:RedrawFlags,rfFullScreen
@@1:	Exit	HideBlock

	Entry	ToggleHideBlock
	xor	ds:Options,eoBlockHidden
	Invoke	BlockPresent
	Invoke	SetEditDS
	jz	@@1
	or	ds:RedrawFlags,rfFullScreen
@@1:	Exit	ToggleHideBlock

	Entry	HighlightBlock
	test	ds:Options,eoBlockHidden
	jz	@@1
	and	ds:Options,not eoBlockHidden
	Invoke	BlockPresent
	Invoke	SetEditDS
	jz	@@1
	or	ds:RedrawFlags,rfFullScreen
@@1:	Exit	HighlightBlock

SetPos	proc	near
	push	di
	Invoke	GoCurPos
	Invoke	SetEditDS
	mov	ax,bx
	sub	ax,di
	jz	@@1
	sub	ax,size LineHeader-1
@@1:	test	di,di
	pop	di
	jz	@@2
	mov	[di].X,ax
	mov	ax,ds:CursorPos.Y
	mov	[di].Y,ax
@@2:	ret
SetPos	endp

	Entry	SetBlockBegRaw
	lea	di,ds:BlockBeg
	call	SetPos
	Invoke	BlockPresent
	Invoke	SetEditDS
	jz	@@1
	or	ds:RedrawFlags,rfFullScreen
@@1:	Exit	SetBlockBegRaw

	Entry	SetBlockEndRaw
	lea	di,ds:BlockEnd
	call	SetPos
	Invoke	BlockPresent
	Invoke	SetEditDS
	jz	@@1
	or	ds:RedrawFlags,rfFullScreen
@@1:	Exit	SetBlockEndRaw

	Entry	SetPrevPos
	lea	di,ds:PrevPos
	call	SetPos
	Exit	SetPrevPos

	Entry	SetTempPos
	lea	di,ds:TempPos
	call	SetPos
	Exit	SetTempPos

MarkNumber	proc	near
	Invoke	GetParam
	Invoke	SetEditDS
	cmp	al,10
	jae	@@1
	mov	ah,0
	shl	ax,1
	shl	ax,1
	add	ax,offset Marks
	mov	si,ax
	clc
	ret
@@1:	stc
	ret
MarkNumber	endp

	Entry	SetMark
	call	MarkNumber
	jc	@@1
	mov	di,si
	call	SetPos
@@1:	Exit	SetMark

MoveToPos	proc	near
	mov	ax,[si].X
	test	ax,ax
	jle	@@4
	mov	ax,[si].Y
	test	ax,ax
	jz	@@4
	push	si
	cmp	ax,ds:CursorPos.Y
	je	@@1
	push	ax
	Invoke	PackLine
	Invoke	SetEditDS
	pop	ax
@@1:	mov	ds:CursorPos.Y,ax
	mov	ds:RedrawLine,ax
	Invoke	FindCurLine
	Invoke	UseCurPage
	pop	si
	mov	ax,1
	jc	@@3
	cmp	bx,dx
	jae	@@3
	mov	di,bx
	mov	bx,es:[di].LineLength
	inc	bx
	cmp	bx,[si].X
	ja	@@2
	mov	[si].X,bx
@@2:	lea	bx,[di+size LineHeader-1]
	add	bx,[si].X
	Invoke	CalcColumn
	Invoke	SetEditDS
@@3:	mov	ds:CursorPos.X,ax
@@4:	ret
MoveToPos	endp

	Entry	SwapPrevPos
	lea	di,ds:PrevPos
	mov	ax,[di].X
	inc	ax
	jz	@@1
	mov	ax,[di].Y
	test	ax,ax
	jz	@@1
	push	ax
	push	[di].X
	call	SetPos
	lea	si,ds:CursorPos
	pop	dword ptr [si]
	call	MoveToPos
@@1:	Exit	SwapPrevPos

	Entry	MoveToBlockBegRaw
	lea	si,ds:BlockBeg
	call	MoveToPos
	Exit	MoveToBlockBegRaw

	Entry	MoveToBlockEndRaw
	lea	si,ds:BlockEnd
	call	MoveToPos
	Exit	MoveToBlockEndRaw

	Entry	MoveToPrevPos
	lea	si,ds:PrevPos
	call	MoveToPos
	Exit	MoveToPrevPos

	Entry	MoveToTempPos
	lea	si,ds:TempPos
	call	MoveToPos
	Exit	MoveToTempPos

	Entry	MoveToMark
	call	MarkNumber
	jc	@@1
	call	MoveToPos
@@1:	Exit	MoveToMark

	Argm	Action,word,1
	Loc	CurEditorOffs,word,1
	Loc	CurEditorSegm,word,1
	Loc	HandleOffs,word,1
	Loc	HandleSegm,word,1
	Loc	OrigEditor,word,1
	Loc	Position,word,1
	Entry	Shift,X
	push	ax bx cx dx
	push	si di
	lea	bx,[di+size LineHeader-1]
@@1:	inc	bx
	Invoke	CheckWS
	jz	@@1
	sub	bx,di
	sub	bx,size LineHeader-2
	mov	Position,bx
	Invoke	SetEditDS
	mov	OrigEditor,ds
	mov	bx,si
	sub	bx,di
	sub	bx,size LineHeader-1
	mov	dx,ds:CursorPos.Y
	les	si,ds:BreakPts
	mov	ax,es
	or	ax,si
	jz	@@5
	push	es
	pop	ds
	cld
	lodsw
	mov	cx,ax
	jcxz	@@4
	push	si cx
	mov	di,size TEBpt
	mov	ax,Position
@@2:	cmp	dx,[si].Y
	jne	@@3
	cmp	[si].X,-2
	je	@@3
	mov	[si].X,ax
@@3:	add	si,di
	loop	@@2
	pop	cx si
	stc
	call	Action
@@4:	mov	ds,OrigEditor
@@5:	lea	si,ds:PrevPos
	mov	cx,(Marks+size Marks-PrevPos) shr 2
	mov	di,4
	stc
	call	Action
	push	bx dx
	SetTurboDS
	mov	ax,EdHandle.Offs
	mov	HandleOffs,ax
	mov	ax,EdHandle.Segm
	mov	HandleSegm,ax
	lds	si,CurEd
	mov	CurEditorOffs,si
	mov	CurEditorSegm,ds
	lds	si,[si].FileRec
	lds	si,[si].Editor
	pop	dx bx
@@6:	cmp	si,CurEditorOffs
	jne	@@7
	mov	ax,ds
	cmp	ax,CurEditorSegm
	je	@@8
@@7:	mov	ax,ds
	or	ax,si
	jz	@@9
	mov	ax,HandleOffs
	cmp	ax,[si].Handle.Offs
	jne	@@8
	mov	ax,HandleSegm
	cmp	ax,[si].Handle.Segm
	jne	@@8
	push	si
	add	si,offset PrevPos
	mov	cx,(Marks+size Marks-PrevPos) shr 2
	mov	di,4
	clc
	call	Action
	pop	si
@@8:	lds	si,[si].Next
	jmp	@@6
@@9:	mov	ds,OrigEditor
	pop	bx ax
	sub	ax,bx
	push	ax
	mov	ax,ds:CursorPos.Y
	Invoke	FindLine
	Invoke	UseCurPage
	mov	di,bx
	pop	si
	add	si,bx
	pop	dx cx bx ax
	Exit	Shift

_ShiftDown	proc	near
	jc	@@1
	cmp	dx,[si-4].Y
	jae	@@1
	inc	[si-4].Y
	cmp	dx,[si-8].Y
	jae	@@1
	inc	[si-8].Y
@@1:	mov	ax,[si].Y
	cmp	ax,dx
	jb	@@3
	je	@@2
	inc	ax
	jz	@@3
	mov	[si].Y,ax
	add	si,di
	loop	@@1
	jmp	@@4
@@2:	cmp	[si].X,bx
	jle	@@3
	inc	ax
	jz	@@3
	sub	[si].X,bx
	inc	[si].X
	mov	[si].Y,ax
@@3:	add	si,di
	loop	@@1
@@4:	ret
_ShiftDown	endp

HiliteDown	proc	near
	cmp	ds:ExecBar,0
	je	@@2
	push	ax
	mov	ax,ds:CursorPos.Y
	cmp	ax,ds:ExecBar
	jae	@@1
	inc	ds:ExecBar
@@1:	pop	ax
@@2:	ret
HiliteDown	endp

HiliteUp	proc	near
	cmp	ds:ExecBar,0
	je	@@2
	push	ax
	mov	ax,ds:CursorPos.Y
	cmp	ax,ds:ExecBar
	jae	@@1
	dec	ds:ExecBar
@@1:	pop	ax
@@2:	ret
HiliteUp	endp

	Entry	ShiftDown
	call	HiliteDown
	push	ax
	lea	ax,_ShiftDown
	xchg	ax,[bp-2]
	Invoke	Shift
	Exit	ShiftDown

_ShiftMerge	proc	near
	jc	@@1
	cmp	dx,[si-4].Y
	jae	@@1
	dec	[si-4].Y
	cmp	dx,[si-8].Y
	jae	@@1
	dec	[si-8].Y
@@1:	mov	ax,[si].Y
	cmp	ax,dx
	jbe	@@3
	inc	ax
	jz	@@3
	dec	ax
	dec	ax
	cmp	ax,dx
	jne	@@2
	add	[si].X,bx
	dec	[si].X
@@2:	mov	[si].Y,ax
@@3:	add	si,di
	loop	@@1
	ret
_ShiftMerge	endp

	Entry	ShiftMerge
	call	HiliteUp
	push	ax
	lea	ax,_ShiftMerge
	xchg	ax,[bp-2]
	Invoke	Shift
	Exit	ShiftMerge

_ShiftRight	proc	near
@@1:	cmp	[si].Y,dx
	je	@@3
@@2:	add	si,di
	loop	@@1
	ret
@@3:	mov	ax,[si].X
	cmp	ax,bx
	jle	@@2
	inc	ax
	jz	@@2
	mov	[si].X,ax
	jmp	@@2
_ShiftRight	endp

	Entry	ShiftRight
	push	ax
	lea	ax,_ShiftRight
	xchg	ax,[bp-2]
	Invoke	Shift
	Exit	ShiftRight

_ShiftLeft	proc	near
@@1:	cmp	[si].Y,dx
	je	@@3
@@2:	add	si,di
	loop	@@1
	ret
@@3:	cmp	[si].X,bx
	jle	@@2
	dec	[si].X
	jmp	@@2
_ShiftLeft	endp

	Entry	ShiftLeft
	push	ax
	lea	ax,_ShiftLeft
	xchg	ax,[bp-2]
	Invoke	Shift
	Exit	ShiftLeft

_ShiftCompact	proc	near
@@1:	cmp	[si].Y,dx
	je	@@3
@@2:	add	si,di
	loop	@@1
	ret
@@3:	sub	[si].X,bx
	jg	@@2
	mov	[si].X,1
	jmp	@@2
_ShiftCompact	endp

	Entry	ShiftCompact
	push	ax
	lea	ax,_ShiftCompact
	xchg	ax,[bp-2]
	Invoke	Shift
	Exit	ShiftCompact

_ShiftUp	proc	near
	jc	@@1
	cmp	dx,[si-4].Y
	jae	@@1
	dec	word ptr [si-4].Y
	cmp	dx,[si-8].Y
	jae	@@1
	dec	word ptr [si-8].Y
@@1:	cmp	[si].Y,dx
	jae	@@3
@@2:	add	si,di
	loop	@@1
	jmp	@@4
@@3:	je	@@5
	dec	[si].Y
	add	si,di
	loop	@@1
@@4:	ret
@@5:	mov	[si].X,1
	cmp	di,4
	je	@@2
	mov	[si].X,-2
	jmp	@@2
_ShiftUp	endp

	Entry	ShiftUp
	push	ax
	call	HiliteUp
	lea	ax,_ShiftUp
	xchg	ax,[bp-2]
	Invoke	Shift
	Exit	ShiftUp

_ShiftEOL	proc	near
	mov	ax,bx
@@1:	cmp	[si].Y,dx
	jne	@@2
	cmp	[si].X,bx
	jl	@@2
	mov	[si].X,bx
@@2:	add	si,di
	loop	@@1
	ret
_ShiftEOL	endp

	Entry	ShiftEOL
	push	ax
	lea	ax,_ShiftEOL
	xchg	ax,[bp-2]
	Invoke	Shift
	Exit	ShiftEOL

_ShiftMoveBlock	proc	near
@@1:	mov	ax,[si].Y
	cmp	ax,dx
	jb	@@2
	cmp	ax,bx
	jae	@@3
	sub	ax,dx
	add	ax,di
	jmp	@@8
@@2:	cmp	ax,bx
	jb	@@4
@@3:	cmp	ax,di
	jae	@@5
	add	ax,dx
	sub	ax,di
	jmp	@@8
@@4:	cmp	ax,di
	jb	@@9
@@5:	cmp	ax,dx
	jae	@@9
	cmp	bx,di
	jae	@@6
	sub	ax,di
	jmp	@@7
@@6:	sub	ax,dx
@@7:	add	ax,bx
@@8:	mov	[si].Y,ax
@@9:	add	si,bp
	loop	@@1
	ret
_ShiftMoveBlock	endp

	Entry	ShiftMoveBlock
	mov	bx,ds:TempPos.Y
	mov	di,ds:BlockBeg.Y
	mov	dx,ds:BlockEnd.Y
	lea	si,ds:ExecBar-2
	mov	cx,1
	push	bp
	call	_ShiftMoveBlock
	pop	bp
	les	si,ds:BreakPts
	mov	ax,es
	or	ax,si
	jz	@@2
	push	ds
	push	es
	pop	ds
	cld
	lodsw
	mov	cx,ax
	jcxz	@@1
	push	bp
	mov	bp,size TEBpt
	clc
	call	_ShiftMoveBlock
	pop	bp
@@1:	pop	ds
@@2:	lea	si,ds:PrevPos
	mov	cx,(Marks+size Marks-PrevPos) shr 2
	push	bp
	mov	bp,4
	stc
	call	_ShiftMoveBlock
	pop	bp
	mov	ax,_Data
	mov	ds,ax
	push	CurEd
	push	EdHandle
	push	bx di dx
	lds	si,CurEd
	lds	si,[si].FileRec
	lds	si,[si].Editor
	pop	dx di bx
@@3:	push	bp
	mov	bp,sp
	cmp	si,[bp+6]
	jne	@@4
	mov	ax,ds
	cmp	ax,[bp+8]
@@4:	pop	bp
	je	@@5
	mov	ax,ds
	or	ax,si
	jz	@@6
	pop	ax cx
	push	cx ax
	cmp	ax,[si].Handle.Offs
	jne	@@5
	cmp	cx,[si].Handle.Segm
	jne	@@5
	or	[si].RedrawFlags,rfFullScreen
	push	si
	add	si,offset ScreenPos
	mov	cx,(Marks+size Marks-ScreenPos) shr 2
	push	bp
	mov	bp,4
	clc
	call	_ShiftMoveBlock
	pop	bp
	pop	si
@@5:	lds	si,[si].Next
	jmp	@@3
@@6:	pop	bx ds
	mov	ds,[bx]
	pop	bx ax
	mov	ax,dx
	sub	ax,di
	Exit	ShiftMoveBlock

_ShiftDeleteBlock	proc	near
@@1:	mov	ax,[si].Y
	cmp	ax,dx
	jb	@@2
	sub	ax,dx
	add	ax,di
	jmp	@@3
@@2:	cmp	ax,di
	jb	@@3
	mov	ax,di
	mov	[si].X,1
@@3:	mov	[si].Y,ax
	add	si,bp
	loop	@@1
	ret
_ShiftDeleteBlock	endp

	Entry	ShiftDeleteBlock
	mov	di,ds:BlockBeg.Y
	mov	dx,ds:BlockEnd.Y
	lea	si,ds:ExecBar-2
	mov	cx,1
	push	bp
	call	_ShiftDeleteBlock
	pop	bp
	les	si,ds:BreakPts
	mov	ax,es
	or	ax,si
	jz	@@2
	push	ds
	push	es
	pop	ds
	cld
	lodsw
	mov	cx,ax
	jcxz	@@1
	push	bp
	mov	bp,size TEBpt
	clc
	call	_ShiftDeleteBlock
	pop	bp
@@1:	pop	ds
@@2:	lea	si,ds:PrevPos
	mov	cx,(Marks+size Marks-PrevPos) shr 2
	push	bp
	mov	bp,4
	stc
	call	_ShiftDeleteBlock
	pop	bp
	mov	ax,_Data
	mov	ds,ax
	push	CurEd
	push	EdHandle
	push	di dx
	lds	si,CurEd
	lds	si,[si].FileRec
	lds	si,[si].Editor
	pop	dx di
@@3:	push	bp
	mov	bp,sp
	cmp	si,[bp+6]
	jne	@@4
	mov	ax,ds
	cmp	ax,[bp+8]
@@4:	pop	bp
	je	@@5
	mov	ax,ds
	or	ax,si
	jz	@@6
	pop	ax cx
	push	cx ax
	cmp	ax,[si].Handle.Offs
	jne	@@5
	cmp	cx,[si].Handle.Segm
	jne	@@5
	or	[si].RedrawFlags,rfFullScreen
	push	si
	add	si,offset ScreenPos
	mov	cx,(Marks+size Marks-ScreenPos) shr 2
	push	bp
	mov	bp,4
	clc
	call	_ShiftDeleteBlock
	pop	bp
	pop	si
@@5:	lds	si,[si].Next
	jmp	@@3
@@6:	pop	bx ds
	mov	ds,[bx]
	pop	bx ax
	mov	ax,dx
	sub	ax,di
	Exit	ShiftDeleteBlock

	.data

	extrn	CurEd:dword,EdHandle:dword

	end
