	load 'macros.dump'
	include 'driver.equ'
	include 'wp.equ'

;-----------------------------------------------
;
;   Imported addresses
;
;-----------------------------------------------

	IMPORT	D_BitMapPtr
	IMPORT	D_MemoryError
	IMPORT	D_ClearBitMap
	IMPORT	D_RectZap
	import	D_UndoMemError
	
	IMPORT	W_BackChar
	IMPORT	W_CalcDocRect
	IMPORT	W_CalcFBytes
	IMPORT	W_CalcLineHeight
	IMPORT	W_CalcPages
	IMPORT	W_CaretBot
	IMPORT	W_CaretTop
	IMPORT	W_CheckCol
	IMPORT	W_CheckDocPos
	IMPORT	W_CurDoc
	IMPORT	W_CutLine2
	IMPORT	W_DelSelect
	IMPORT	W_DeleteRange
	IMPORT	W_DeleteRangeD
	IMPORT	W_Descent
	IMPORT	W_DoDownArrow
	IMPORT	W_DoRoom
	IMPORT	W_DoUpArrow
	IMPORT	W_DrawLine
	IMPORT	W_DrawPartLine
	IMPORT	W_EndLine
	IMPORT	W_EndOffset
	IMPORT	W_EndPar
	IMPORT	W_EndPtr
	IMPORT	W_FindFont
	IMPORT	W_FindLine
	IMPORT	W_ForChar
	IMPORT	W_GetAddr
	IMPORT	W_GetBegLine
	IMPORT	W_GetEndLine
	IMPORT	W_GetFirstChar
	IMPORT	W_GetHPos
	IMPORT	W_GetLRecPtr
	IMPORT	W_GetLineAddr
	IMPORT	W_GetLineRec
	IMPORT	W_GetParRec
	IMPORT	W_GotApNum
	IMPORT	W_Height

	IMPORT	W_IsJunk
	IMPORT	W_JunkLeft
	IMPORT	W_JunkRight
	IMPORT	W_JustBits
	IMPORT	W_KillKey
	IMPORT	W_KillSelect
	IMPORT	W_LineToPixel
	IMPORT	W_LineToTopPixel
	IMPORT	W_MakeCaret
	IMPORT	W_MakeCr
	IMPORT	W_MakeFontChange
	IMPORT	W_MakeLines
	IMPORT	W_MakeRoom
	IMPORT	W_MaxAscent
	IMPORT	W_MaxDescent
	IMPORT	W_MyClear
	IMPORT	W_NextCharBack
	IMPORT	W_NextLine
	IMPORT	W_OptionConvert
	IMPORT	W_OSUpdateText
	IMPORT	W_PaperSize

	IMPORT	W_PutNewHeight
	IMPORT	W_PutOnScreen


	IMPORT	W_ReadFont
	IMPORT	W_ReadRuler
	IMPORT	W_ReadRulerP
	IMPORT	W_RightMargin
	IMPORT	W_RulBits
	IMPORT	W_SetDocRect
	IMPORT	W_SetFullREct
	IMPORT	W_StartLine
	IMPORT	W_StartOffset
	IMPORT	W_StartPar

	IMPORT	W_TopPgPixel
	IMPORT	W_UndoEndLine
	IMPORT	W_UndoEndOffset
	IMPORT	W_UndoEndPar
	IMPORT	W_UndoHandle
	IMPORT	W_UndoOff
	IMPORT	W_UndoOn
	IMPORT	W_UndoOnND
	IMPORT	W_UndoStartLine
	IMPORT	W_UndoStartOffset
	IMPORT	W_UndoStartPar
	IMPORT	W_UpdateLines
	IMPORT	W_UpdateText
	IMPORT	W_ValidLines
	IMPORT	W_WPColor
	IMPORT	W_WpFont
	IMPORT	W_WPFontFlag
	IMPORT	W_WPLeftArrow
	IMPORT	W_WPRightArrow
	IMPORT	W_WPUndoType
	IMPORT	W_WaitTime
	IMPORT	W_WpCopy
	IMPORT	W_WriteFBytes2
	IMPORT	W_LastP
	IMPORT	W_Selected
	IMPORT	W_View

;-----------------------------------------------
;
;   Forward addresses and entries
;
;-----------------------------------------------

	ENTRY	W_CtrlX
	ENTRY	W_DoDelete
	ENTRY	W_DoUpDate
	ENTRY	W_DoneScreen
	ENTRY	W_DumpBuffer
	ENTRY	W_EdMakeCaret
	ENTRY	W_GotCr
	ENTRY	W_NoFC
	ENTRY	W_NoRapUp
	ENTRY	W_NotAtBegD
	ENTRY	W_Skip

********************************************************************
*	Includes...
*
* Monday, May 1, 1989 12:11:37 AM
********************************************************************





W_EditData	PROC	EXPORT
	EXPORT	W_KeyBuffer
	EXPORT	W_EditEvent
	EXPORT	W_Key
	EXPORT	W_KeyWhen
	EXPORT	W_ModFlags
	EXPORT	W_EditEnd
	EXPORT	W_LastKeyWhen
	EXPORT	W_Char
	EXPORT	W_EditPar
	EXPORT	W_eBlockHand
	EXPORT	W_eOffset
	EXPORT	W_eAttr
	EXPORT	W_eRulerHand
	EXPORT	W_ePixels
	EXPORT	W_eLineHand
	EXPORT	W_eLastLine
	EXPORT	W_EditLineRec
	EXPORT	W_elOffset
	EXPORT	W_elFont
	EXPORT	W_elSize
	EXPORT	W_elStyle
	EXPORT	W_elColor
	EXPORT	W_elAscent
	EXPORT	W_elDescent
	EXPORT	W_elHeight
	EXPORT	W_elLeading
	Export  W_EditEventSize
W_KeyBuffer	DS.B $100

W_EditEvent
	DS.B 2
W_Key	DS.B 4
W_KeyWhen	DS.B 4
	DS.B 4
W_ModFlags	DS.B 2
W_EditEnd
W_EditEventSize	equ W_EditEnd-W_EditEvent

W_LastKeyWhen	DS.B 4

W_Char	DS.B 2

W_EditPar
W_eBlockHand	DS.B 4
W_eOffset	DS.B 2
W_eAttr	DS.B 2
W_eRulerHand	DS.B 4
W_ePixels	DS.B 2
W_eLineHand	DS.B 4
W_eLastLine	DS.B 2

W_EditLineRec
W_elOffset	DS.B 2
W_elFont	DS.B 2
W_elSize	DS.B 1
W_elStyle	DS.B 1
W_elColor	DS.B 2
W_elAscent	DS.B 2
W_elDescent	DS.B 2
W_elHeight	DS.B 2
W_elLeading	DS.B 2

	ENDP


****************************************************************
* W_InsertKeys
W_InsertKeys	PROC	EXPORT
	;Using wpglobals
	;Using W_EditData
	;Using D_GlobalData
	EXPORT	W_DumpBuffer
	EXPORT	W_NoFC
	EXPORT	W_Skip
	EXPORT	W_NoRapUp
	EXPORT	W_DoUpDate
	EXPORT	W_DoneScreen
	EXPORT	W_InsertExit
	EXPORT	W_GotCr
	EXPORT	W_DoDelete
	EXPORT	W_NotAtBegD
	EXPORT	W_CtrlX
	EXPORT	W_EdMakeCaret

	local TempVal:l,KeyPtr:l,NumKeys:w,rect:r,oldoffset:w,DOffset1:w
	local NewH:w,LineRecPtr:l,BaseLine:w,W_Descent:w,DOffset2:w
	local W_Height:w,Command:w,TopPixel:w,Src:l,HtChange:w
	local DFont1:l,DCol1:w,DFont2:l,DCol2:w,DFontFl:w,DPtr:l
	local ChangeBytes:w,DidDel:w,method:w,UpdateLine:w,rect2:r
	local DonePar:w,DoneLine:w,UpdatePar:w,OldH:w,NewPar:w,NewLine:w
	begin
W_WaitTime	equ 10	;1/3 second

	stz DonePar
	stz DoneLine

	_ObscureCursor

StartEdit
	stz HtChange
	stz DidDel

	movelong #W_KeyBuffer,KeyPtr

	pushword W_StartPar
	jsl W_ValidLines

	lda W_StartPar
	jsl W_GetParRec
	movelong ax,TempVal

	pushlong TempVal
	pushlong #W_EditPar
	pushlong #W_pBytes
	_BlockMove

	Cmpw	W_eAttr,#W_PgBrk
	beq	pagebreak

	movelong W_eLineHand,LineRecPtr

	ldy #2
	lda [LineRecPtr],y
	tax
	lda [LineRecPtr]
	ldy W_StartLine
	jsl W_GetLineRec
	phx
	pha
	pushlong #W_EditLineRec
	pushlong #W_lBytes
	_BlockMove

pagebreak
	jsr IsChar
	sta W_Char
	bcs NotRegK

	pushword #0
	pushlong W_eLineHand
	_SetPurge

;	  pushlong W_eBlockHand
;	  _HLock

	jsr EditDelSel
	jcs	KeyExit

	pushword W_StartPar
	pushword W_StartLine
	pushlong #0
	jsl W_PutOnScreen
	jcc GotOne

	moveword #1,diddel
	jsl W_EdMakeCaret
	stz diddel
	bra GotOne


NotRegK
	lda W_Key
	cmp #$7f
	jeq W_DoDelete

	lda W_ModFlags
	and #$100	;apple W_Key
	beq NotNum

	lda W_Key
	cmp #'1'
	bcc NotNum
	cmp #'9'+1
	bcs NotNum
	sec
	sbc #'1'
	pha	  ;used in W_GotApNum
	jsl W_KillSelect
	jsl W_GotApNum

	brl KeyExit

NotNum	lda W_Key
	cmp #sp
	jcs KeyExit
	asl a
	tax
	jmp (ChrTable,x)

BufferLoop
	pha
	pushword #%101000	;W_Key down and auto
	pushlong #W_EditEvent
	_EventAvail
	pla
	sta Command
	jeq W_DumpBuffer

	jsr IsChar
	jcs W_DumpBuffer
	sta W_Char

	stz Command

	pha
	pushword #%101000	;W_Key down and auto
	pushlong #W_EditEvent
	_GetNextEvent
	pla
	jeq W_DumpBuffer

GotOne
	moveword W_Char,[KeyPtr]
	inc KeyPtr
	lda KeyPtr
	sec
	sbc #W_KeyBuffer
	cmp #$f0
	bcc BufferLoop

W_DumpBuffer	;
	movelong W_KeyWhen,W_LastKeyWhen
	subword KeyPtr,#W_KeyBuffer,NumKeys

	moveword #W_KeyBuffer,KeyPtr

	jsr UndoInserting

	pha
	pushword W_StartPar
	pushword W_StartLine
	pushword W_StartOffset
	jsl W_JunkLeft
	pullword W_StartOffset

	stz ChangeBytes
	lda W_WPFontFlag
	beq W_NoFC

	pha
	jsl W_MakeFontChange
	pullword Changebytes
	inc HtChange

; Make room in text block to insert the text from the buffer. If memory error warn the user and don't
; do the text insertion!

W_NoFC	call	W_MakeRoom,in=(W_StartPar:w,W_StartOffset:w,NumKeys:w)
	bcc	@insertkeys
	jsl	D_MemoryError
	bra	@doupdate

@insertkeys	pushlong #W_KeyBuffer
	lda W_StartPar
	ldx W_StartOffset
	jsl W_GetAddr
	phx
	pha
	pushword #0	  ;high word of Amt
	pushword NumKeys
	_BlockMove

@doupdate	pushword W_StartPar
	pushword W_StartLine
	addword NumKeys,ChangeBytes,s
	jsl W_UpdateLines

	moveword W_StartOffset,OldOffset
	addword NumKeys,W_StartOffset,W_StartOffset

	moveword W_StartOffset,W_UndoEndOffset

	pushword W_StartPar
	pushword W_StartLine
	jsl W_CutLine2

;now check for a W_Height change in the Q_Line
	cmpw W_elAscent,W_MaxAscent
	bne HtDidChange
	cmpw W_elDescent,W_MaxDescent
	beq NoHtChange

HtDidChange

	jsl W_PutNewHeight

	pha
	pha
	pushword W_StartPar
	pushword W_StartLine
	pushword #0
	jsl W_OSUpdateText
	pullword DoneLine
	pullword DonePar

	brl DoneLineUpdate

NoHtChange
;	  lda W_RulBits
;	  and #W_r_full
;	  beq NotFull
;
;	  lda W_StartLine
;	  inc a
;	  cmp W_eLastLine
;	  beq DoQuick

NotFull
	lda W_RulBits
	and #W_r_left
	jeq DrawOffScreen

DoQuick

	moveword #1,method

	pha
	pha
	pushword W_StartPar
	lda W_StartLine
	inc a
	pha
	jsl W_GetLRecPtr
	pullLong TempVal

	pha
	pushword OldOffset
	pushword [TempVal]:#W_lOffset
	jsl W_IsJunk
	pla
	jne DrawOffScreen2

;on screen updating
	pha
	pushword W_StartPar
	pushword W_StartLine
	pushword W_StartOffset
	jsl W_GetHPos
	pla
	sta NewH

	pushlong !rect
	jsl W_CalcDocRect

	movelong W_CaretTop,rect
	lda W_CaretBot
	inc a
	sta rect+4

	jsl W_SetDocRect

	cmpw W_StartOffset,[TempVal]:#W_lOffset
	bcs NoScroll

	pha
	pushword W_StartPar
	pushword W_StartLine
	pushword [TempVal]:#W_lOffset
	jsl W_GetHPos
	pla
	sta rect+6
	cmp rect+2
	beq NoScroll

	jsl W_SetDocRect

	pushlong !rect
	lda NewH
	sec
	sbc W_CaretTop+2
	pha
	pushword #0
	pushlong #0
	_ScrollRect

NoScroll
	pushword W_StartPar
	pushword W_StartLine
	pushlong W_CaretBot
	pushword OldOffset
	pushword W_StartOffset
	jsl W_DrawPartLine

	brl DoneLineUpDate

DrawOffScreen
	stz method
DrawOffScreen2

	jsr DOSRoutine
	brl DoneLineUpdate

DOSRoutine
	movelong W_CurDoc,tempval

	pushlong [tempval]:#2

	movelong >D_BitMapPtr,[tempval]:#2

	pha
	pushword W_StartPar
	pushword W_StartLine
	jsl W_LineToPixel
	pla
	sta BaseLine

	spacelong
	pushword W_StartPar
	pushword W_StartLine
	jsl W_GetLRecPtr
	pullLong LineRecPtr

	lda W_StartPar
	jsl W_GetParRec
	jsl W_ReadRulerP

	pushlong !rect
	jsl W_CalcDocRect

	movelong LineRecPtr,ax
	jsl W_CalcLineHeight
	stx W_Descent
	sta W_Height
	lda BaseLine
	sec
	sbc W_Height
	clc
	adc W_Descent
	sta rect
	clc
	adc W_Height
	sta rect+4

	movelong rect,rect2
	movelong rect+4,rect2+4

	pushlong !rect2
	_LocalToGlobal

	pushlong !rect2+4
	_LocalToGlobal

	pushlong !rect2
	jsl D_ClearBitMap

	pushword W_StartPar
	pushword W_StartLine
	pushlong LineRecPtr
	pushword BaseLine
	jsl W_DrawLine

	pulllong [tempval]:#2

	jsl W_SetDocRect

	pushlong !rect2
	jsl D_RectZap

	rts

DoneLineUpDate
	pushword W_StartPar
	pushword W_StartLine
	jsl W_CutLine2

	lda W_StartPar
	ldx #0
	jsl W_GetAddr
	sta TempVal
	subword W_EndPtr,TempVal,a
	cmpw a,W_StartOffset
	bcc UpdateScreen

	jsl W_EdMakeCaret	  ;?????

	lda command
	bne UpdateScreen

WaitLoop
	pha
	pushword #%101000	;W_Key down and auto
	pushlong #W_EditEvent
	_EventAvail
	pla
	beq NoLookAtKey

LookAtKey
	jsr IsChar
	bcs UpdateScreen
	brl BufferLoop

NoLookAtKey
	jsr EraseCheck

	sublong W_KeyWhen,W_LastKeyWhen,TempVal
	cmpl TempVal,#W_WaitTime
	bcc WaitLoop

UpDateScreen
	jsr EraseCheck
	bra W_Skip

EraseCheck
	jsl W_SetDocRect

	subword W_RightMargin,W_JustBits,TopPixel
	lda method
	beq ECExit
	cmpw TopPixel,rect+6
	beq ECExit

	moveword TopPixel,rect+2
	pushlong !rect
;	   jsl W_MyClearRect
	_EraseRect
ECExit	rts

W_Skip	;

	pha
	pha
	pushword W_StartPar
	pushword W_StartLine
	jsl W_NextLine
	pullword UpdateLine
	pullword UpdatePar

	lda W_StartLine
	jeq W_NoRapUp

	pushword W_StartPar
	lda W_StartLine
	dec a
	pha
	jsl W_CutLine2

	spacelong
	pushword W_StartPar
	lda W_StartLine
	pha
	jsl W_GetLineAddr
	Pulllong TempVal

	cmpl TempVal,W_EndPtr
	bne YesRapUp
	lda W_StartPar
	ldx W_StartOffset
	jsl W_GetAddr
	movelong ax,TempVal

	cmpl TempVal,W_EndPtr
	jne W_NoRapUp

;there is a rap up
YesRapUp	lda W_StartLine
	dec a
	sta UpdateLine

	lda W_RulBits
	and #W_r_left
	jeq GotUpdate

	pha
	pha
	pushword W_StartPar
	lda W_StartLine
	dec a
	pha
	jsl W_GetLRecPtr
	pullLong TempVal

	cmpw [TempVal]:#W_lAscent,W_MaxAscent
	bne HtDidChange2
	cmpw [TempVal]:#W_lDescent,W_MaxDescent
	beq NoHtChange2
HtDidChange2
	lda W_StartLine
	dec a
	sta UpdateLine
	brl GotUpdate

NoHtChange2
	moveword W_StartLine,UpDateLine

	lda W_StartPar
	ldx #0
	jsl W_GetAddr
	sta TempVal

	subword W_EndPtr,TempVal,DOffset2

	pha
	pushword W_StartPar
	lda W_StartLine
	dec a
	pha
	jsl W_LineToPixel
	pullword TempVal

	pha
	pushword W_StartPar
	lda W_StartLine
	dec a
	pha
	pushword W_elOffset
	jsl W_GetHPos
	pullword TempVal+2

	spacelong
	pushword W_StartPar
	lda W_StartLine
	dec a
	pha
	jsl W_GetLRecPtr
	pullLong LineRecPtr

	addword Tempval,[LineRecPtr]:#W_lDescent,TempVal

	pushword W_StartPar
	lda W_StartLine
	dec a
	pha
	pushlong TempVal
	pushword W_elOffset
	pushword DOffset2
	jsl W_DrawPartLine

	dec W_StartLine

	brl W_DoUpDate

W_NoRapUp	;

	pushword W_StartPar
	lda W_StartLine
	pha
	jsl W_CutLine2

	spacelong
	pushword W_StartPar
	lda W_StartLine
	inc a
	pha
	jsl W_GetLineAddr
	Pulllong TempVal

	cmpl TempVal,W_EndPtr
	jeq NoLineChange

	pushlong !rect
	jsl W_CalcDocRect

	cmpw W_StartPar,W_LastP
	bcc NotLast

	lda W_StartLine
	inc a
	cmp W_eLastLine
	bcc NotLast

	pushword W_StartPar
	jsl W_MakeLines

	moveword W_CaretBot,rect

	pushlong !rect
;	   jsl W_MyClearRect
	_EraseRect

	pushlong !rect
	_ClipRect

	jsl W_UpdateText

	brl W_DoneScreen

NotLast

W_DoUpDate	;
	pushword W_StartPar
	jsl W_MakeLines

	pha
	pha
	pushword W_StartPar
	pushword W_StartLine
	jsl W_NextLine
	pla
	plx
	sta UpdateLine
	stx UpdatePar

	bra SkipMake

GotUpdate
	pushword W_StartPar
	jsl W_MakeLines

SkipMake
	pushlong !rect
	jsl W_CalcDocRect

	moveword W_StartPar,UpdatePar

	pha
	pha
	pushword UpDatePar
	pushword UpdateLine
	pushword #0
	jsl W_OSUpdateText
	pullword DoneLine
	pullword DonePar

W_DoneScreen	;
	pha
	pushword W_StartPar
	pushword W_StartOffset
	jsl W_FindLine
	pullword W_StartLine
	sta W_UndoEndLine

	jsl W_CalcPages

	jsl W_CheckDocPos

	pha
	pushword #%101000	;W_Key down and auto
	pushlong #W_EditEvent
	_GetNextEvent
	pla
	beq GetOut

	jsl W_EdMakeCaret

	jsr IsChar
	jcc StartEdit

	lda W_Key
	cmp #del
	jeq W_DoDelete

GetOut

NoLineChange

W_InsertExit	;

	jsl W_EdMakeCaret

	lda DonePar
	beq ScreenOk

	pha
	pha
	pushword DonePar
	pushword DoneLine
	pushword #1
	jsl W_OSUpdateText
	pla
	pla

ScreenOK

	pushword #3
	pushlong W_eLineHand
	_SetPurge

	pushlong W_eBlockHand
	_HUnLock

KeyExit
	jsl W_SetFullREct

	stz DidDel

	return


;----------------
GotTab
	stz DidDel

	pushword #0
	pushlong W_eLineHand
	_SetPurge

;	  pushlong W_eBlockHand
;	  _HLock

	jsr EditDelSel
	jcs	KeyExit

	lda #tab
	sta W_Char
	brl GotOne



GotOneNon
	lda W_View
	jeq KeyExit

	stz DidDel

	pushword #0
	pushlong W_eLineHand
	_SetPurge

;	  pushlong W_eBlockHand
;	  _HLock

	brl GotOne


W_GotCr	;
	moveword #1,diddel

	jsr EditDelSel
	jcs	KeyExit

	jsr UndoInserting

	pushword #0
	pushlong W_eLineHand
	_SetPurge

;	  pushlong W_eBlockHand
;	  _HLock

	pushword W_StartPar
	pushword W_StartLine
	pushlong #0
	jsl W_PutOnScreen

	pushword #cr
	jsl W_KillKey

	pha
	pha
	pushword W_StartPar
	lda W_StartLine
	beq GotA
	dec a
GotA	pha
	jsl W_LineToTopPixel
	pla
	pla
	sta TopPixel

	pushword W_StartPar
	pushword W_StartLine
	pushword W_StartOffset
	jsl W_MakeCr

; now do the screen updating
	pushlong !rect
	jsl W_CalcDocRect

	moveword W_StartLine,UpDateline

	cmpw W_StartOffset,#W_TextHeader
	jne NotAtBeg

	lda W_StartPar
	inc a
	cmp W_LastP
	bcc NoTAtEndCr

	lda W_StartPar
	inc a
	ldx #W_TextHeader
	jsl W_GetAddr
	movelong ax,TempVal

	lda [TempVal]
	and #$ff
	cmp #cr
	jeq DoneCRSC

NotAtEndCR
	subword rect+4,rect,a
	addword a,W_TopPgPixel,a
	cmp W_PaperSize
	beq NotMuch
	bcc NotMuch

	addword TopPixel,W_TopPgPixel,a
	cmp W_PaperSize
	bcs NotMuch
	brl DoOUpdate

NotMuch
	moveword TopPixel,rect

	lda W_StartPar
	jsl W_GetParRec
	movelong ax,TempVal

	pushlong !rect
	pushword #0
	pushword [TempVal]:#W_pPixels
	pushlong #0
	_ScrollRect

	brl DoneCrSc

NotAtBeg
	lda W_StartLine
	beq NotFirstL

	dec UpdateLine

NotFirstL

DoOUpdate

	pha
	pha
	pushword W_StartPar
	pushword UpDateLine
	pushword #0
	jsl W_OSUpdateText
	pullword DoneLine
	pullword DonePar

DoneCrSc
	inc W_StartPar
	moveword W_StartPar,W_UndoEndPar
	stz W_StartLine
	stz W_UndoEndLine
	moveword #W_TextHeader,W_StartOffset
	sta W_UndoEndOffset

	brl W_DoneScreen



; the arrow keys
DoRArr

	jsl W_UndoOff
	jsl W_KillSelect
	pushlong #W_EditEvent
	jsl W_WPRightArrow
	brl KeyExit


DoLArr

	jsl W_UndoOff
	jsl W_KillSelect
	pushlong #W_EditEvent
	jsl W_WPLeftArrow
	brl KeyExit

DoUArr

	jsl W_UndoOff
	jsl W_KillSelect
	pushlong #W_EditEvent
	jsl W_DoUpArrow
	brl KeyExit

DoDArr

	jsl W_UndoOff
	jsl W_KillSelect
	pushlong #W_EditEvent
	jsl W_DoDownArrow
	brl KeyExit

;------------------------------------------------------
; Process a delete
W_DoDelete	;
	moveword #1,diddel

	lda W_Selected
	beq NoDelsel

	jsr EditDelSel
	brl Dexit

NoDelSel
	jsl W_UndoOff

	pushword #0
	pushlong W_eLineHand
	_SetPurge

;	  pushlong W_eBlockHand
;	  _HLock

DelLoop
	lda W_StartPar
	jsl W_GetParRec
	movelong ax,TempVal

	ldy #W_pRulerHand+2
	lda [TempVal],y
	tax
	ldy #W_pRulerHand
	lda [TempVal],y
	jsl W_ReadRuler

NoCalcHs
	pushword #del
	jsl W_KillKey

	movelong W_KeyWhen,W_LastKeyWhen

	moveword W_StartOffset,oldoffset

	pha
	pushword W_StartPar
	pha
	pushword W_StartPar
	pushword W_StartLine
	jsl W_GetBegLine
	jsl W_GetFirstChar
	pla
	cmp W_StartOffset
	bcc W_NotAtBegD

	cmpw #1,W_StartPar
	bne NotTop
	lda W_StartLine
	jeq NoDR

NotTop
	pha   ;space for W_DeleteRange
	pha
	pha
	pha   ;space for W_BackChar
	pha
	pha
	pushword W_StartPar
	lda W_StartLine
	beq GotStD
	dec a
GotSTD	pha
	pushword W_StartOffset
	jsl W_BackChar
	lda 5,s	  ;par
	pha
	lda 5,s	  ;Q_Line from W_BackChar
	pha
	pushlong #0
	jsl W_PutOnScreen
	pushword W_StartPar
	pushword W_StartLine
	pushword W_StartOffset
	jsl W_DeleteRange
	pullword W_StartOffset
	pullword W_StartLine
	pullword W_StartPar

NODR
	pushword W_StartPar
	jsl W_MakeLines

	pha
	pha
	pushword W_StartPar
	lda W_StartLine
	beq CheapHac
	dec a
CheapHac	pha
	pushword #0
	jsl W_OSUpdateText
	pullword DoneLine
	pullword DonePar

	stz method

	brl KeepCheckIn


W_NotAtBegD	;
	pha
	pushword W_StartPar
	pushword W_StartLine
	pushword W_StartOffset
	jsl W_JunkRight
	pullword DOffset2

	pha
	pha
	pushword W_StartPar
	pushword W_StartLine
	pushword W_StartOffset
	jsl W_NextCharBack
	pla
	pullword W_StartOffset

	pha
	pushword W_StartPar
	pushword W_StartLine
	pushword W_StartOffset
	jsl W_JunkLeft
	pullword DOffset1

	spacelong
	pha
	pushword W_StartPar
	pushword W_StartLine
	lda W_StartOffset
	inc a
	pha
	jsl W_FindFont
	pullword W_WPColor
	pullLong W_WpFont

	pha	  ;output for calc
	spacelong	;output for next findf
	pha	  ;' '
	pushword W_StartPar
	pushword W_StartLine
	pushword DOffset1
	jsl W_FindFont
	moveword 1:s,DCol1
	movelong 3:s,DFont1
	spacelong	;output for D_Read
	pha	  ;"
	pushlong DFont1
	pushword DCol1
	lda W_StartPar
	ldx DOffset1
	jsl W_GetAddr
	phx
	pha
	lda W_StartPar
	ldx DOffset2
	jsl W_GetAddr
	phx
	pha
	jsl W_ReadFont
	moveword 1:s,DCol2
	movelong 3:s,DFont2
	jsl W_CalcFBytes
	ply
	sty W_WPFontFlag

	pushword W_StartPar
	pushword DOffset1
	addword y,DOffset1,a
	subword a,DOffset2,s
	sta ChangeBytes
	cmp #-1
	beq NoFt
	sta W_WPFontFlag
NoFt
	jsl W_DoRoom

	spaceword
	lda W_StartPar
	pha
	ldx DOffset1
	phx
	pushlong DFont1
	pushword DCol1
	pushlong DFont2
	pushword DCol2
	jsl W_WriteFBytes2
	subword ChangeBytes,s,ChangeBytes

	moveword DOffset1,W_StartOffset

DoneData
	pushword W_StartPar
	pushword W_StartLine
	pushword ChangeBytes
	jsl W_UpdateLines

	pushword W_StartPar
	pushword W_StartLine
	jsl W_CutLine2

;now check for a W_Height change in the Q_Line
	cmpw W_elAscent,W_MaxAscent
	bne HtDidChange3
	cmpw W_elDescent,W_MaxDescent
	beq NoHtChange3

HtDidChange3

	jsl W_PutNewHeight

DoFullOsUp
	pha
	pha
	pushword W_StartPar
	pushword W_StartLine
	pushword #0
	jsl W_OSUpdateText
	pullword DoneLine
	pullword DonePar

	brl DoneLineUpdate

NoHtChange3
	moveword #1,method

;	  lda W_RulBits
;	  and #W_r_left
;	  bne DoShift
;
;	  lda W_RulBits
;	  and #W_r_full
;	  beq DoOS
;
;	  lda W_StartLine
;	  beq DoShift

DoOS
	pha
	jsl W_CheckCol
	pla
	beq NoFullOS

	pushword W_StartPar
	jsl W_MakeLines
	brl DoFullOSUp

NoFullOs
	jsr DOSRoutine
	brl KeepCheckIn

DoShift
	moveword OldH,rect+6

	pha
	pha
	pushword W_StartPar
	lda W_StartLine
	pha
	jsl W_LineToTopPixel
	pla
	clc
	adc 1,s
	sta rect+4
	pla
	sta rect

	pha	  ;get current hpos
	pushword W_StartPar
	pushword W_StartLine
	pushword W_StartOffset
	jsl W_GetHPos
	pla
	sta rect+2

	pushlong !rect
	subword rect+2,NewH,s
	pushword #0
	pushlong #0
	_ScrollRect

KeepCheckIn

	pha
	pushword #%101000	;W_Key down and auto
	pushlong #W_EditEvent
	_EventAvail
	pla
	beq NoLookAtKey2

	lda W_Key
	cmp #$7f
	bne NoTime

	pha
	pushword #%101000	;W_Key down and auto
	pushlong #W_EditEvent
	_GetNextEvent
	pla

	brl DelLoop

NoLookAtKey2

	sublong W_KeyWhen,W_LastKeyWhen,TempVal
	cmpl TempVal,#W_WaitTime
	bcc KeepCheckIn

NoTime
	lda method
	jne W_Skip
	brl W_DoneScreen

DExit
	pushword W_StartPar
	jsl W_MakeLines
	brl W_InsertExit


;---------------------------
CtrlF
	lda W_ModFlags
	and #$1000
	jeq GotOneNon

	jsl W_DelSelect

	pha
	pha
	pha
	pushword W_StartPar
	pushword W_StartLine
	pushword W_StartOffset
	jsl W_ForChar
	pullword OldOffset
	pullword NewLine
	pullword NewPar

	cmpw W_StartPar,NewPar
	bne UseNew
	cmpw W_StartOffset,OldOffset
	jeq KeyExit

UseNew
	moveword NewPar,W_StartPar
	moveword NewLine,W_StartLine
	moveword OldOffset,W_StartOffset
	jsl W_MakeCaret

	brl W_DoDelete


;-------------------------------------------
W_CtrlX	;
	jsl W_MyClear

	lda W_ModFlags
	and #$2000	   ;must be a clear from keyboard
	jne KeyExit

	jsl W_UndoOff

	lda W_StartPar
	cmp W_LastP
	beq NoChk
	inc a
	jsl W_GetParRec
	movelong ax,TempVal

	moveword [TempVal]:#W_pAttr,a
	and #W_PgBrk
	jne KeyExit

NoChk
	lda W_StartPar
	jsl W_GetParRec
	movelong ax,TempVal

	pha
	pha
	pha
	pushword W_StartPar
	pushword W_StartLine
	pha
	pushword W_StartPar
	pushword W_StartLine
	jsl W_GetBegLine	   ;!!!!!!Dont' change without looking x

	cmpw [TempVal]:#W_pLastLine,#1
	bne NoDelPar

	cmpw W_StartPar,W_LastP
	beq NoDelPar

	addword W_StartPar,#1,s
	pushword #0
	pushword #W_TextHeader
	bra DoDelPar

NoDelPar
	pushword W_StartPar
	pushword W_StartLine
	pha
	pushword W_StartPar
	pushword W_StartLine
	jsl W_GetEndLine
DoDelPar	jsl W_DeleteRangeD
	pullword W_StartOffset
	pullword W_StartLine
	pullword W_StartPar

	jsl W_MakeCaret

	brl KeyExit

;------------------------------------------
CtrlY
	jsl W_DelSelect

	lda W_StartPar
	jsl W_GetParRec
	movelong ax,TempVal

	pha
	pha
	pha
	pushword W_StartPar
	pushword W_StartLine
	pushword W_StartOffset

	brl NoDelPar




;-------------------------------------------------------
ChrTable
	DC.W	EditIgnore	;0
	DC.W	EditIgnore	;1
	DC.W	EditIgnore	;2
	DC.W	EditIgnore	;3
	DC.W	EditIgnore	;4
	DC.W	GotOneNon	;5
	DC.W	CtrlF	;6
	DC.W	GotOneNon	;7
	DC.W	DoLArr	;8
	DC.W	GotTab	;9
	DC.W	DoDArr	;a
	DC.W	DoUArr	;b
	DC.W	EditIgnore	;c
	DC.W	W_GotCr	;d
	DC.W	EditIgnore	;e
	DC.W	EditIgnore	;f
	DC.W	EditIgnore	;10
	DC.W	EditIgnore	;11
	DC.W	EditIgnore	;12
	DC.W	EditIgnore	;13
	DC.W	EditIgnore	;14
	DC.W	DoRArr	;15
	DC.W	EditIgnore	;16
	DC.W	EditIgnore	;17
	DC.W	W_CtrlX	;18
	DC.W	CtrlY	;19
	DC.W	EditIgnore	;1a
	DC.W	EditIgnore	;1b
	DC.W	EditIgnore	;1c
	DC.W	EditIgnore	;1d
	DC.W	EditIgnore	;1e
	DC.W	EditIgnore	;1f



;-------------------------------
EditIgnore	brl W_InsertExit

IsChar
	lda W_Key
	and #$ff
	cmp #sp
	bcc NotChar
	cmp #del
	beq NotChar

	lda W_ModFlags
	and #$100	;apple W_Key
	beq NoApple

	lda W_Key
	and #$ff
	cmp #sp
	bne NotChar
	jsl W_OptionConvert
	bra OPtExit

NoApple
	lda W_ModFlags
	and #$800	;option W_Key
	beq RegKey

	lda W_Key
	jsl W_OptionConvert
	bra OptExit

RegKey	lda W_Key
	and #$ff
OptExit	clc
	rts

NotChar	sec
	rts



;----------------
W_EdMakeCaret	;
	lda DidDel
	beq NoSave
	pushword W_WPFontFlag
	pushlong W_WpFont
	pushword W_WPColor
NoSave
	jsl W_MakeCaret
	lda DidDel
	beq NoSave2
	pullword W_WPColor
	pulllong W_WpFont
	pullword W_WPFontFlag
NoSave2
	lda W_StartPar
	jsl W_GetParRec
	jsl W_ReadRulerP
	rtl




UndoInserting
	lda W_WPUndoType
	cmp #W_InsertUndo
	beq MoInsert
	cmp #W_RTUndo
	beq MoInsert
	cmp #W_RangeUndo
	bne UndoInit

	pushword #W_RTUndo
	jsl W_UndoOnND

	bra StartIU

UndoInit
	pushword #W_InsertUndo
	jsl W_UndoOn

StartIU
	moveword W_StartPar,W_UndoStartPar
	sta W_UndoEndPar
	moveword W_StartLine,W_UndoStartLine
	sta W_UndoEndLine
	moveword W_StartOffset,W_UndoStartOffset
	sta W_UndoEndOffset

MoInsert
	rts


EditDelSel	clc
	lda W_Selected
	beq NoSelD

	jsl W_UndoOff

	spacelong
	pushword W_StartPar
	pushword W_StartLine
	pushword W_StartOffset
	pushword W_EndPar
	pushword W_EndLine
	pushword W_EndOffset
	jsl W_WpCopy
	pulllong W_UndoHandle
	bcc D_DoUndo

	call	D_UndoMemError,out=(a:w)
	cmp	#OK
	beq	oktodo
	sec
	bra	NoSelD
	
oktodo	jsl	W_UndoOff
	clc
	bra	dodelete

D_DoUndo
	pushword #W_RangeUndo
	jsl W_UndoOn

dodelete	jsl W_DelSelect
	clc

NoSelD
	rts


	ENDP
	END

