	include	ed.inc

A	macro	Name
	.data
	global	Name:far
	.code
	dd	Name
	endm

	.code

CTable	label	dword
	A	FoolNop
	A	Glop
	A	InsertChar
	A	SetReturnCode
	A	ReadFile
	A	WriteFile
	A	ReadBlk
	A	WriteBlk
	A	InvalidateScreen
	A	FullPaintScreen
	A	SmartRefreshScreen
	A	SearchText
	A	StorePagesInfo
	A	Compact
	A	ClearText
	A	FindOldLine
	A	GetNewLineNumber
	A	CursorRight
	A	CursorLeft
	A	CursorCharRight
	A	CursorCharLeft
	A	CursorUp
	A	CursorDown
	A	WordLeft
	A	WordRight
	A	ScrollScreenUp
	A	ScrollScreenDown
	A	PageScreenUp
	A	PageScreenDown
	A	LeftOfLine
	A	RightOfLine
	A	TopOfScreenRaw
	A	BottomOfScreenRaw
	A	HomeCursorRaw
	A	EndCursorRaw
	A	ToggleInsert
	A	SetInsertMode
	A	DeleteLineX
	A	DeleteToEOL
	A	DeleteWord
	A	DeleteChar
	A	BackspaceDelete
	A	RestoreLine
	A	SetBlockBegRaw
	A	SetBlockEndRaw
	A	HighlightBlock
	A	ToggleHideBlock
	A	MoveToBlockBegRaw
	A	MoveToBlockEndRaw
	A	CopyBlockRaw
	A	MoveBlockRaw
	A	DeleteBlockRaw
	A	SetPrevPos
	A	SwapPrevPos
	A	MoveToPrevPos
	A	MoveToMark
	A	SetMark
	A	InsertText
	A	LiteralChar
	A	SeekPos
	A	GetText
	A	GetTotalLines
	A	FixCursorPos
	A	FixScreenPos
	A	CenterFixScreenPos
	A	ReplaceText
	A	SetTempPos
	A	MoveToTempPos
	A	GetOneChar
	A	InsertBuf
	A	IndentBlock
	A	OutdentBlock
	A	ToggleAutoIndent
	A	SetAutoIndent
	A	ToggleAutoOutdent
	A	SetAutoOutdent
	A	RightOfWord
	A	MatchPairForward
	A	MatchPairBackward
	A	HideBlock
	A	SetTabbingMode
	A	ToggleTabbingMode
	A	SetOptimalFillMode
	A	ToggleOptimalFillMode
	A	SmartTab
	A	CursorRight
	A	CursorSwitchedLeft
	A	PageUpRaw
	A	PageDownRaw
	A	GetPosOffset
	A	ToggleRoamingCursorMode
	A	SetRoamingCursorMode
	A	PushPos
	A	PopPos
CTableL	equ	($-CTable) shr 2

	Entry	ProcessKey
	sub	ax,ax
	mov	ds:ReturnCode,ax
	mov	ds:Errors,al
	les	bx,ds:CommandTable
	mov	al,ds:CParam.B0
	sub	bx,bx
	xchg	bl,ds:PendingChar
	mov	ds:Perverted,bl
	shl	bl,1
	jnz	@@3
	jnc	@@1
	jmp	@@9
@@1:	test	al,al
	jnz	@@2
	mov	al,ds:CParam.B1
	mov	bx,2
	jmp	@@3
@@2:	mov	ah,0
	mov	si,ax
	mov	cl,3
	shr	si,cl
	mov	ah,80h
	mov	cl,al
	and	cl,7
	shr	ah,cl
	and	ah,es:[si].ControlChars
	jz	@@8
@@3:	shl	bx,1
	mov	cx,es:[bx].Macros+2
	mov	bx,es:[bx].Macros
	jcxz	@@7
	mov	ah,al
	cmp	al,1
	jb	@@5
	cmp	al,1fh
	jbe	@@4
	cmp	al,'a'
	jb	@@5
	cmp	al,'z'
	ja	@@5
@@4:	and	ah,1fh
	add	ah,40h
@@5:	test	byte ptr es:[bx+1],1
	jnz	@@6
	cmp	al,es:[bx]
	je	@@11
	add	bx,4
	loop	@@5
	jmp	@@7
@@6:	cmp	ah,es:[bx]
	je	@@11
	add	bx,4
	loop	@@5
@@7:	Invoke	QuickCompactPages
	Invoke	SetEditDS
	mov	ax,-1
	mov	ds:ReturnCode,ax
	jmp	@@18
@@8:	cmp	ds:Perverted,0
	jne	@@7
	Invoke	TypeChar
	jc	@@16
	jmp	@@10
@@9:	Invoke	InsertAllowWS
	jc	@@16
@@10:	Invoke	CenterFixScreen
	Invoke	QuickCompactPages
	Invoke	SmartestRefreshScreen
	Invoke	SetEditDS
	jmp	@@16
@@11:	mov	al,es:[bx+1]
	mov	ds:SecondByte,al
	mov	ax,es:[bx+2]
	mov	ds:SubTblPtr,ax
@@12:	les	bx,ds:CommandTable
	mov	bx,ds:SubTblPtr
	mov	al,es:[bx]
	inc	ds:SubTblPtr
	mov	ah,0
	cmp	al,1
	jbe	@@13
	test	ds:PendingChar,80h
	jz	@@13
	and	ds:PendingChar,7fh
	mov	byte ptr ds:CParam,al
	Invoke	InsertAllowWS
	jmp	@@15
@@13:	cmp	ax,CTableL
	jb	@@14
	jmp	@@12
@@14:	shl	ax,1
	shl	ax,1
	mov	bx,ax
	cld
	call	CTable[bx]
@@15:	SetTurboDS
	lds	bx,EdHandle
	mov	ds,[bx]
	mov	ax,ds:ReturnCode
	test	ax,ax
	jz	@@12
@@16:	sub	ax,ax
	mov	al,ds:Errors
	test	al,al
	jz	@@17
	push	ax
	Invoke	CenterFixScreen
	Invoke	QuickCompactPages
	Invoke	InvalidateScreen
	Invoke	SetEditDS
	pop	ax
	mov	ds:ReturnCode,ax
	mov	ds:Errors,al
	mov	ax,-2
	jmp	@@18
@@17:	mov	ax,ds:ReturnCode
	cmp	ax,-2
	jne	@@18
	sub	ax,ax
@@18:	Exit	ProcessKey

	Entry	SetReturnCode
	Invoke	GetParam
	mov	ah,al
	Invoke	GetParam
	Invoke	SetEditDS
	xchg	ah,al
	mov	ds:ReturnCode,ax
	Exit	SetReturnCode

	Entry	Glop
	Invoke	QuickCompactPages
	Invoke	SmartestRefreshScreen
	Invoke	SetEditDS
	mov	ds:ReturnCode,-2
	Exit	Glop

	Entry	DoCommand
	sub	ax,ax
	mov	ds:ReturnCode,ax
	mov	ds:Errors,al
	mov	al,byte ptr ds:CParam
	mov	ah,0
	cmp	ax,CTableL
	jb	@@1
	mov	ax,-1
	jmp	@@3
@@1:	shl	ax,1
	shl	ax,1
	mov	bx,ax
	cld
	call	CTable[bx]
	SetTurboDS
	lds	bx,EdHandle
	mov	ds,[bx]
	sub	ax,ax
	mov	al,ds:Errors
	test	al,al
	jz	@@2
	push	ax
	Invoke	QuickCompactPages
	Invoke	InvalidateScreen
	Invoke	SetEditDS
	pop	ax
	mov	ds:ReturnCode,ax
	mov	ds:Errors,al
	mov	ax,-2
	jmp	@@3
@@2:	push	ds:ReturnCode
	Invoke	QuickCompactPages
	pop	ax
	cmp	ax,-2
	jne	@@3
	sub	ax,ax
@@3:	Exit	DoCommand

	Entry	InsertChar
	Invoke	GetParam
	Invoke	SetEditDS
	mov	ds:PendingChar,al
	Exit	InsertChar

	Entry	LiteralChar
	mov	ds:PendingChar,80h
	Exit	LiteralChar

	Entry	FoolNop
	Exit	FoolNop

	Entry	GetParam
	push	es
	les	bx,ds:CommandTable
	mov	bx,ds:SubTblPtr
	mov	al,es:[bx]
	pop	es
	inc	ds:SubTblPtr
	Exit	GetParam

	.data

	extrn	EdHandle:dword

	end
