	include	ed.inc

	.code

MergeLines	proc	near
	cmp	ds:CursorPos.Y,1
	ja	@@1
	clc
	jmp	@@2
@@1:	Invoke	Up
	Invoke	ROfLine
	Invoke	DeleteChar
	Invoke	SetEditDS
@@2:	ret
MergeLines	endp

	Loc	OutdentPos,word,1
	Entry	BackspaceDelete
	cmp	ds:CursorPos.X,1
	jbe	@@2
	test	ds:Options,eoAutoOutdent
	jz	@@1
	push	ds:CursorPos.X
	Invoke	SkipWS
	Invoke	SetEditDS
	pop	ds:CursorPos.X
	jc	@@1
	jcxz	@@3
	inc	dx
	cmp	dx,ds:CursorPos.X
	je	@@4
@@1:	jmp	@@9
@@2:	call	MergeLines
	jmp	@@11
@@3:	mov	dx,ds:CursorPos.X
@@4:	mov	OutdentPos,dx
	push	ds:CursorPos.Y
@@5:	sub	ds:CursorPos.Y,1
	jbe	@@8
	Invoke	SkipWS
	Invoke	SetEditDS
	jc	@@5
	jcxz	@@5
	inc	dx
	cmp	dx,OutdentPos
	jae	@@5
	pop	ds:CursorPos.Y
	mov	ax,OutdentPos
	mov	ds:CursorPos.X,ax
	mov	OutdentPos,dx
@@6:	mov	ax,ds:CursorPos.X
	cmp	ax,OutdentPos
	je	@@10
	jb	@@7
	Invoke	FoolBS
	Invoke	SetEditDS
	jmp	@@6
@@7:	mov	ax,ds:CursorPos.X
	cmp	ax,OutdentPos
	jae	@@10
	mov	byte ptr ds:CParam,' '
	Invoke	InsertNoOver
	Invoke	SetEditDS
	jnc	@@7
	jmp	@@11
@@8:	mov	ax,OutdentPos
	mov	ds:CursorPos.X,ax
	pop	ds:CursorPos.Y
@@9:	Invoke	FoolBS
	jmp	@@11
@@10:	clc
@@11:	Exit	BackSpaceDelete

DelLineFromPage	proc	near
	sub	cx,si
	jae	@@1
	sub	cx,cx
@@1:	shr	cx,1
	rep	movsw
	mov	ds:CurLineNum,-1
	mov	ds,dx
	mov	bx,ds:CurPage
	mov	[bx].PageLength,di
	dec	[bx].LinesInPage
	or	ds:RedrawFLags,rfBelowCurLine+rfPageChanged
	or	ds:Modified,emShow+emUpdate
	clc
	ret
DelLineFromPage	endp

	Entry	DeleteChar
@@1:	Invoke	GoCurPos
	test	di,di
	jz	@@2
	cmp	di,bx
	jne	@@3
@@2:	jmp	@@15
@@3:	push	ax
	Invoke	UpdateUndo
	Invoke	GoCurPos
	Invoke	UseCurPage
	pop	ax
	test	ax,ax
	jz	@@6
	mov	cx,es:[di].LineLength
	mov	dx,bx
	sub	dx,di
	sub	dx,size LineHeader
	cmp	dx,cx
	jb	@@4
	mov	byte ptr ds:CParam,'x'
	Invoke	InsertNoOver
	Invoke	FoolBS
	jmp	@@5
@@4:	test	ds:Options,eoRoamingCursor
	jz	@@6
	cmp	byte ptr es:[bx+1],9
	je	@@6
	dec	ds:CursorPos.X
	mov	byte ptr ds:CParam,' '
	Invoke	InsertNoOver
@@5:	Invoke	GoCurPos
	Invoke	UseCurPage
@@6:	mov	si,bx
	mov	cx,es:[di].LineLength
	mov	dx,bx
	sub	dx,di
	sub	dx,size LineHeader
	cmp	dx,cx
	jae	@@9
	Invoke	ShiftLeft
	Invoke	UseCurPage
	sub	cx,dx
	dec	cx
	mov	bx,ds:CurPage
	Invoke	StoreLinesInfo
	mov	bx,[bx].PageLength
	mov	dx,ds
	mov	ax,es
	mov	ds,ax
	dec	[di].LineLength
	mov	di,si
	inc	si
	shr	cx,1
	rep	movsw
	rcl	cx,1
	rep	movsb
	test	di,1
	jnz	@@8
	inc	si
	mov	cx,bx
	sub	cx,si
	jbe	@@7
	shr	cx,1
	rep	movsw
@@7:	mov	ds,dx
	mov	bx,ds:CurPage
	mov	[bx].PageLength,di
@@8:	mov	ds,dx
	or	ds:RedrawFLags,rfCurLine+rfPageChanged
	or	ds:Modified,emShow+emUpdate
	Invoke	FixScreen
	clc
	jmp	@@16
@@9:	inc	bx
	and	bl,0feh
	push	si
	mov	si,ds:CurPage
	cmp	bx,[si].PageLength
	pop	si
	jae	@@12
	mov	dx,es:[bx].LineLength
	add	dx,cx
	cmp	dx,4000
	jle	@@10
	jmp	@@14
@@10:	Invoke	ShiftMerge
	Invoke	UseCurPage
	push	bx dx
	mov	bx,ds:CurPage
	Invoke	StoreLinesInfo
	pop	dx bx
	mov	es:[di].LineLength,dx
	mov	ax,es:[di].LineNumber
	test	ax,ax
	jnz	@@11
	mov	ax,es:[bx].LineNumber
	mov	es:[di].LineNumber,ax
@@11:	lea	di,[di+size LineHeader]
	add	di,cx
	sub	dx,cx
	mov	cx,dx
	mov	si,ds:CurPage
	mov	ax,[si].PageLength
	lea	si,[bx+size LineHeader]
	mov	dx,ds
	mov	bx,es
	mov	ds,bx
	shr	cx,1
	rep	movsw
	rcl	cx,1
	rep	movsb
	inc	di
	and	di,0fffeh
	inc	si
	and	si,0fffeh
	mov	cx,ax
	call	DelLineFromPage
	jmp	@@16
@@12:	Invoke	LineFromNextPage
	jnc	@@13
	jcxz	@@15
	Invoke	LineToNextPage
	jnc	@@13
	Invoke	CompactPages
	test	ax,ax
	jnz	@@13
	Invoke	FindCurLine
	Invoke	InsertPage
	jc	@@15
	Invoke	LineToNextPage
	Invoke	FindCurLine
	Invoke	LineFromNextPage
	jc	@@14
@@13:	jmp	@@1
@@14:	Invoke	SetEditDS
	mov	ax,erLineTooLong
	or	ds:Errors,al
	mov	ds:ReturnCode,ax
@@15:	stc
@@16:	Exit	DeleteChar

	Entry	FoolBS
	cmp	ds:CursorPos.X,1
	jbe	@@1
	Invoke	CursorSwitchedLeft
	Invoke	GoLine
	Invoke	SetEditDS
	inc	ax
	cmp	ds:CursorPos.X,ax
	jae	@@2
	Invoke	DeleteChar
	jmp	@@3
@@1:	call	MergeLines
	jmp	@@3
@@2:	clc
@@3:	Exit	FoolBS

	Entry	DeleteToEOL
	Invoke	GoCurPos
	Invoke	UseCurPage
	test	di,di
	jz	@@1
	sub	ds:CursorPos.X,ax
	mov	si,bx
	cmp	di,bx
@@1:	je	@@3
	mov	dx,es:[di].LineLength
	test	dx,dx
	jz	@@3
	mov	cx,si
	sub	cx,di
	sub	cx,size LineHeader
	cmp	cx,dx
	jae	@@3
	push	dx cx
	Invoke	UpdateUndo
	Invoke	SetEditDS
	pop	cx dx
	or	ds:RedrawFLags,rfCurLine+rfPageChanged
	or	ds:Modified,emShow+emUpdate
	Invoke	ShiftEOL
	Invoke	UseCurPage
	mov	es:[di].LineLength,cx
	inc	si
	and	si,0fffeh
	add	dx,size LineHeader
	inc	dx
	and	dl,0feh
	add	di,dx
	xchg	si,di
	cmp	si,di
	je	@@3
	mov	bx,ds:CurPage
	mov	cx,[bx].PageLength
	sub	cx,si
	jbe	@@2
	push	ds
	push	es
	pop	ds
	shr	cx,1
	cld
	rep	movsw
	pop	ds
@@2:	mov	[bx].PageLength,di
@@3:	clc
	Exit	DeleteToEOL

	Entry	TruncateWS
	test	ds:Options,eoNoTruncate
	jnz	@@4
	Invoke	FindCurLine
	Invoke	UseCurPage
	jc	@@4
	cmp	bx,dx
	je	@@4
	mov	di,bx
	mov	bx,es:[di].LineLength
	test	bx,bx
	jz	@@4
	mov	cx,bx
	lea	bx,[di+size LineHeader+bx-1]
	Invoke	CheckWS
	Invoke	SetEditDS
	jnz	@@4
	jmp	@@2
@@1:	Invoke	CheckWS
	Invoke	SetEditDS
	jnz	@@3
@@2:	dec	bx
	loop	@@1
@@3:	inc	bx
	push	ds:CursorPos.X
	Invoke	CalcColumn
	Invoke	SetEditDS
	mov	ds:CursorPos.X,ax
	Invoke	DeleteToEOL
	Invoke	SetEditDS
	pop	ds:CursorPos.X
@@4:	Exit	TruncateWS

	Entry	OptimalFill
	test	ds:Options,eoOptimalFill
	jz	@@2
	test	ds:Options,eoUseTab
	jz	@@2
	push	ds:CursorPos.X
	Invoke	SkipWS
	Invoke	GoCurPos
	Invoke	SetEditDS
	jc	@@1
	cmp	di,bx
	je	@@1
	mov	ax,ds:CursorPos.X
	dec	ax
	jle	@@1
	sub	dx,dx
	mov	cx,ds:TabSize
	cmp	cx,1
	jg	@@3
	mov	dx,ax
	sub	ax,ax
	jmp	@@4
@@1:	jmp	@@5
@@2:	jmp	@@6
@@3:	div	cx
@@4:	mov	si,di
	add	di,size LineHeader
	mov	cx,ax
	add	ax,di
	add	ax,dx
	cmp	ax,bx
	je	@@5
	push	dx
	push	cx
	Invoke	UpdateUndo
	Invoke	UseCurPage
	pop	cx
	or	ds:RedrawFLags,rfCurLine+rfPageChanged
	or	ds:Modified,emShow+emUpdate
	mov	al,9
	cld
	rep	stosb
	mov	al,' '
	pop	cx
	rep	stosb
	cmp	di,bx
	je	@@5
	lea	cx,[si+size LineHeader]
	add	cx,es:[si].LineLength
	sub	cx,bx
	mov	ax,bx
	sub	ax,di
	push	ax si
	mov	si,bx
	push	es
	pop	ds
	rep	movsb
	mov	bx,di
	pop	di si
	add	si,size LineHeader-1
	add	si,di
	push	bx
	Invoke	ShiftCompact
	pop	bx
	Invoke	CalcColumn
	Invoke	SetEditDS
	mov	ds:CursorPos.X,ax
	Invoke	DeleteToEOL
	Invoke	SetEditDS
@@5:	pop	ds:CursorPos.X
@@6:	Exit	OptimalFill

	Entry	DeleteLineX
	Invoke	GoCurPos
	jc	@@1
	Invoke	CheckForLastLine
	Invoke	SetEditDS
	jnc	@@2
	mov	ds:CursorPos.X,1
	Invoke	DeleteToEOL
@@1:	jmp	@@3
@@2:	Invoke	UpdateUndo
	Invoke	FindCurLine
	mov	di,bx
	Invoke	ShiftUp
	Invoke	UseCurPage
	mov	bx,ds:CurPage
	Invoke	StoreLinesInfo
	mov	bx,es:[di].LineLength
	add	bx,size LineHeader+1
	and	bl,0feh
	lea	si,[bx+di]
	mov	cx,dx
	mov	dx,ds
	mov	ax,es
	mov	ds,ax
	call	DelLineFromPage
@@3:	Exit	DeleteLineX

	Entry	DeleteWord
	Invoke	GoCurPos
	Invoke	CheckDelim
	jnz	@@1
	Invoke	DeleteChar
	jmp	@@2
@@1:	Invoke	DeleteChar
	Invoke	GoCurPos
	Invoke	CheckDelim
	jnz	@@1
@@2:	Invoke	GoCurPos
	Invoke	CheckWS
	jnz	@@3
	Invoke	DeleteChar
	jmp	@@2
@@3:	Exit	DeleteWord

	end
