
		LOAD		'Macros.dump'
		INCLUDE	'M16.Profile'

		INCLUDE	'SS.equ'

		INCLUDE	'Driver.equ'
		INCLUDE	'Heap.aii.i'

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

		IMPORT	D_GrowHandle
		IMPORT	D_MemoryError
		IMPORT	D_NeedHand
		IMPORT	D_SetFileChanged
		IMPORT	D_SetUndoStr

		IMPORT	S_AcceptCell
		IMPORT	S_ActivateScrlBars
		IMPORT	S_ActiveWindow
		IMPORT	S_AddCellToChangedList
		IMPORT	S_AdjustSheetColumns
		IMPORT	S_CalculateSheet
		IMPORT	S_CellWidth
		IMPORT	S_ChangeColWidth
		IMPORT	S_ChangeDef
		IMPORT	S_CurBRSelect
		IMPORT	S_CurDefColWd
		IMPORT	S_CurEditFlag
		IMPORT	S_CurTLSelect
		IMPORT	S_CurWindow
		IMPORT	S_DeltaMove
		IMPORT	S_DoMove
		IMPORT	S_DoTranspose
		IMPORT	S_DrawMove
		IMPORT	S_GetCellIndex
		IMPORT	S_GetDestCellRanges
		IMPORT	S_GetSSScrap
		IMPORT	S_InRange
		IMPORT	S_MoveDestBR
		IMPORT	S_MoveDestTL
		IMPORT	S_MoveSrcBR
		IMPORT	S_MoveSrcTL
		IMPORT	S_NewLineEdit
		IMPORT	S_PutSSScrap
		IMPORT	S_RedrawCellRange
		IMPORT	S_RefreshScreen
		IMPORT	S_ScrapBR
		IMPORT	S_ScrapBottom
		IMPORT	S_ScrapBuffer
		IMPORT	S_ScrapLeft
		IMPORT	S_ScrapPasteType
		IMPORT	S_ScrapRight
		IMPORT	S_ScrapTL
		IMPORT	S_ScrapTop
		IMPORT	S_SwapColumns
		IMPORT	S_SwapIn
		IMPORT	S_SwapRows
		IMPORT	S_TraverseRange

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

		ENTRY	S_BuildColWdUndo
		ENTRY	S_BuildSimpleUndo
		ENTRY	S_DeleteUndo
		ENTRY	S_FixLE
		ENTRY	S_SetUndoOff
		ENTRY	S_UndoClearStr
		ENTRY	S_UndoColWdStr
		ENTRY	S_UndoCutStr
		ENTRY	S_UndoDeleteStr
		ENTRY	S_UndoEntryStr
		ENTRY	S_UndoFillStr
		ENTRY	S_UndoImportStr
		ENTRY	S_UndoInsertStr
		ENTRY	S_UndoMoveStr
		ENTRY	S_UndoPasteFormatStr
		ENTRY	S_UndoPasteStr
		ENTRY	S_UndoPasteValuesStr
		ENTRY	S_UndoSortStr
		ENTRY	S_UndoTransposeStr
		ENTRY	S_UndoTrashCells

;-----------------------------------------------------------------------------
;
;

S_UndoData	PROC		EXPORT

		EXPORT	S_UndoInProgress
		EXPORT	S_NewUndoHandle
		EXPORT	S_NewUndoLocation1
		EXPORT	S_NewUndoLocation2
		EXPORT	S_UndoType
		EXPORT	S_UndoHandle
		EXPORT	S_UndoLocation1
		EXPORT	S_UndoLocation2
		EXPORT	S_UndoNames
		EXPORT	S_UndoColWdStr
		EXPORT	S_UndoEntryStr
		EXPORT	S_UndoClearStr
		EXPORT	S_UndoCutStr
		EXPORT	S_UndoPasteStr
		EXPORT	S_UndoPasteValuesStr
		EXPORT	S_UndoPasteFormatStr
		EXPORT	S_UndoFillStr
		EXPORT	S_UndoMoveStr
		EXPORT	S_UndoDeleteStr
		EXPORT	S_UndoInsertStr
		EXPORT	S_UndoTransposeStr
		EXPORT	S_UndoSortStr
		EXPORT	S_UndoImportStr

S_UndoInProgress	DC.W		0

S_NewUndoHandle	DS.B 	4
S_NewUndoLocation1	DS.B 	4
S_NewUndoLocation2	DS.B 	4

S_UndoType		DS.B 	2
S_UndoHandle		DS.B 	4
S_UndoLocation1	DS.B 	4
S_UndoLocation2	DS.B 	4

S_UndoNames
				DC.L		0
				DC.L		S_UndoColWdStr
				DC.L		S_UndoEntryStr
				DC.L		S_UndoClearStr
				DC.L		S_UndoCutStr
				DC.L		S_UndoPasteStr
				DC.L		S_UndoPasteValuesStr
				DC.L		S_UndoPasteFormatStr
				DC.L		S_UndoFillStr
				DC.L		S_UndoMoveStr
				DC.L		S_UndoDeleteStr
				DC.L		S_UndoInsertStr
				DC.L		S_UndoTransposeStr
				DC.L		S_UndoSortStr
				DC.L		S_UndoImportStr

S_UndoColWdStr		str		'Column Width Change'
S_UndoEntryStr		str		'Entry'
S_UndoClearStr		str		'Clear'
S_UndoCutStr		str		'Cut'
S_UndoPasteStr		str		'Paste'
S_UndoPasteValuesStr	str	'Paste Values'
S_UndoPasteFormatStr	str	'Paste Format'
S_UndoFillStr		str		'Fill'
S_UndoMoveStr		str		'Move'
S_UndoDeleteStr	str		'Delete'
S_UndoInsertStr	str		'Insert'
S_UndoTransposeStr	str		'Transpose'
S_UndoSortStr		str		'Sort'
S_UndoImportStr	str		'Import'

			ENDP

;-----------------------------------------------------------------------------
;
;

S_SetUndoOn	PROC		EXPORT
			;Using	S_CurrentData
			;Using	S_UndoData

			input	Type:w
			BEGIN

			Call		S_SetUndoOff

			lda		S_NewUndoHandle
			ora		S_NewUndoHandle+2
			beq		Exit

			MoveWord	Type,S_UndoType
			asl		a
			asl		a
			tax
			PushLong	S_UndoNames:x

			lda		Type
			bpl		highBitOK

			lda		3,s
			ora		#$8000
			sta		3,s

highBitOK
			Call		D_SetUndoStr,in=(:l,S_CurWindow:l)
;			Call		D_EnableMItems,in=(#UndoBit:w,#3:w)

			MoveLong	S_NewUndoHandle,S_UndoHandle
			stzl		S_NewUndoHandle

			MoveLong	S_NewUndoLocation1,S_UndoLocation1
			stzl		S_NewUndoLocation1

			MoveLong	S_NewUndoLocation2,S_UndoLocation2
			stzl		S_NewUndoLocation2

Exit
			RETURN
			ENDP

;-----------------------------------------------------------------------------
;
;

S_SetUndoOff	PROC		EXPORT
			;Using	S_CurrentData
			;Using	S_UndoData

		EXPORT	S_SetUndoOff2

			ldx		#0
			bra		SetItOff

S_SetUndoOff2
			ldx		#1

SetItOff
			BEGIN	+b

			txa
			bne		skip

			Call		D_SetFileChanged,in=(S_CurWindow:l)

skip
			lda		S_UndoType
			beq		Exit

			Call		S_DeleteUndo,in=(a:w,S_UndoHandle:l)

			stz		S_UndoType
			stzl		S_UndoHandle
			stzl		S_UndoLocation1
			stzl		S_UndoLocation2

			Call		D_SetUndoStr,in=(#0:l,S_CurWindow:l)

Exit
			RETURN
			ENDP

;-----------------------------------------------------------------------------
;
;

S_DeleteUndo	PROC		EXPORT

			input	Type:w,Handle:l
			local	Ptr:l
			BEGIN

			lda		Handle
			ora		Handle+2
			beq		Exit

			lda		Type
			dec		a
			asl		a
			adc		#0
			asl		a
			tax
			jsr		(DisposeTable,x)

Exit
			RETURN

DisposeTable
			DC.W		DisposeHandle		; undo Col Wd
			DC.W		DisposeHandle		; redo Col Wd
			DC.W		DisposeHandle		; undo Entry
			DC.W		DisposeHandle		; redo Entry
			DC.W		DisposeHandle		; undo Clear
			DC.W		DisposeHandle		; redo Clear
			DC.W		DisposeHandle		; undo Cut
			DC.W		DisposeHandle		; redo Cut
			DC.W		DisposeHandle		; undo Paste
			DC.W		DisposeHandle		; redo Paste
			DC.W		DisposeHandle		; undo Paste Values
			DC.W		DisposeHandle		; redo Paste Values
			DC.W		DisposeHandle		; undo Paste Format
			DC.W		DisposeHandle		; redo Paste Format
			DC.W		DisposeHandle		; undo Fill
			DC.W		DisposeHandle		; redo Fill
			DC.W		DisposeMove		; undo Move
			DC.W		DisposeMove		; redo Move
			DC.W		DisposeMove		; undo Delete
			DC.W		DisposeMove		; redo Delete
			DC.W		DisposeMove		; undo Insert
			DC.W		DisposeMove		; redo Insert
			DC.W		DisposeMove		; undo Transpose
			DC.W		DisposeMove		; redo Transpose
			DC.W		DisposeHandle		; undo Sort
			DC.W		DisposeHandle		; redo Sort
			DC.W		DisposeHandle		; undo Import
			DC.W		DisposeHandle		; redo Import


DisposeHandle
			Tool		_DisposeHandle,in=(Handle:l)
			rts

;-----------------------

DisposeMove

MoveOffset		equ	0
TL1				equ	4
Range1Handle		equ	8
TL2				equ	12
Range2Handle		equ	16
TrashCellHandle	equ	20

			MoveLong	[Handle],Ptr

			Tool		_DisposeHandle,in=([Ptr]:#Range1Handle:l)
			MoveWord	[Ptr]:#TL1,a
			beq		chkTrash

			Tool		_DisposeHandle,in=([Ptr]:#Range2Handle:l)

chkTrash
			ldy		#TrashCellHandle
			lda		[Ptr],y
			ldy		#TrashCellHandle+2
			ora		[Ptr],y
			beq		finishMove

			Tool		_DisposeHandle,in=([Ptr]:#TrashCellHandle:l)

finishMove
			Tool		_DisposeHandle,in=(Handle:l)
			rts

			ENDP


;-----------------------------------------------------------------------------
;
;

S_Undo		PROC		EXPORT
			;Using	S_CurrentData
			;Using	S_UndoData
			;Using	S_ScrapData
			;Using	S_MoveData

			local	Handle:l,Ptr:l,Size:l,Offset:w
			local	Handle2:l,Ptr2:l,Offset2:w,Type:w
			error	ErrFlag
			BEGIN	+b

			stz		ErrFlag

			Tool		_SetPort,in=(S_ActiveWindow:l)
			Call		S_SwapIn,in=(S_ActiveWindow:l)

			lda		S_UndoType
			beq		Exit

			MoveWord	#1,S_UndoInProgress

			lda		S_UndoType
			dec		a
			asl		a
			adc		#0
			asl		a
			tax
			jsr		(UndoTable,x)

			lda		S_UndoType
			eor		#$8000

			Call		S_SetUndoOn,in=(a:w)

			stz		S_UndoInProgress

Exit
			RETURN

UndoTable
			DC.W		UColWd		; undo Col Wd
			DC.W		UColWd		; redo Col Wd
			DC.W		USimple		; undo Entry
			DC.W		USimple		; redo Entry
			DC.W		USimple		; undo Clear
			DC.W		USimple		; redo Clear
			DC.W		USimple		; undo Cut
			DC.W		USimple		; redo Cut
			DC.W		USimple		; undo Paste
			DC.W		USimple		; redo Paste
			DC.W		USimple		; undo Paste Values
			DC.W		USimple		; redo Paste Values
			DC.W		USimple		; undo Paste Format
			DC.W		USimple		; redo Paste Format
			DC.W		USimple		; undo Fill
			DC.W		USimple		; redo Fill
			DC.W		UMove		; undo Move
			DC.W		UMove		; redo Move
			DC.W		UMove		; undo Delete
			DC.W		UMove		; redo Delete
			DC.W		UMove		; undo Insert
			DC.W		UMove		; redo Insert
			DC.W		UTranspose	; undo Transpose
			DC.W		UTranspose	; redo Transpose
			DC.W		USort		; undo Sort
			DC.W		USort		; redo Sort
			DC.W		USimple		; undo Import
			DC.W		USimple		; redo Import


UColWd
			MoveLong	S_UndoHandle,Handle
			MoveLong	[Handle],Ptr

			MoveWord	[Ptr]:#4,a
			bne		notDefault

			Call		S_BuildColWdUndo,in=(#0:w),err=ErrFlag
			bcs		memError1

			MoveWord	[Ptr]:#2,S_CurDefColWd

			in		#1:w,#1:w,#702:w,#1:w
			in		#S_ChangeDef:l
			XCall	S_TraverseRange,err=ErrFlag
			bcc		adjustColumns

memError1
			Call		D_MemoryError
			bra		adjustColumns

notDefault
			SubWord	[Ptr],#2,Size

loop
			MoveLong	[Handle],Ptr
			PushWord	[Ptr]:Size
			dey
			dey
			PushWord	[Ptr]:y
			dey
			dey
			sty		Size

			Call		S_ChangeColWidth,in=(:w,:w),err=ErrFlag
			bcs		memError1

			lda		Size
			bne		loop

adjustColumns
			Call		S_AdjustSheetColumns
			Call		S_ActivateScrlBars

			Call		S_RefreshScreen,in=(#1:w)
			rts

;-----------------------

USimple
			MoveLong	S_UndoLocation1,S_ScrapTL
			MoveLong	S_UndoLocation2,S_ScrapBR

			in		S_UndoLocation1:l,S_UndoLocation2:l
			XCall	S_BuildSimpleUndo,err=ErrFlag
			bcs		memError2

			MoveWord	#0,S_ScrapPasteType
			MoveLong	S_UndoHandle,S_ScrapBuffer
			Call		S_PutSSScrap,err=ErrFlag
			bcc		redrawSimple

memError2
			Call		D_MemoryError

redrawSimple
			Call		S_FixLE

			in		S_ScrapTL:l,S_ScrapBR:l
			XCall	S_RedrawCellRange

			lda		S_CurEditFlag
			and		#S_ManCalcBit
			bne		doneSimple

			Call		S_CalculateSheet

doneSimple
			rts

;-----------------------

UMove

MoveOffset		equ	0
TL1				equ	4
Range1Handle		equ	8
TL2				equ	12
Range2Handle		equ	16
TrashCellHandle	equ	20

			MoveLong	S_UndoHandle,Handle
			MoveLong	[Handle],Ptr

			MoveLong	[Ptr],S_DeltaMove

			MoveLong	S_UndoLocation1,S_MoveSrcTL
			MoveLong	S_UndoLocation2,S_MoveSrcBR

			Call		S_DoMove,err=ErrFlag
			bcs		redrawMove

			MoveLong	[Handle],Ptr

			MoveWord	#0,S_ScrapPasteType
			MoveLong	[Ptr]:#TL1,S_ScrapTL
			MoveLong	[Ptr]:#Range1Handle,S_ScrapBuffer
			Call		S_PutSSScrap,err=ErrFlag

			CmpWord	S_ScrapLeft,S_MoveSrcTL+2
			bge		chkRightM

			sta		S_MoveSrcTL+2

chkRightM
			CmpWord	S_ScrapRight,S_MoveSrcBR+2
			blt		chkBottomM

			sta		S_MoveSrcBR+2

chkBottomM
			CmpWord	S_ScrapBottom,S_MoveSrcBR
			blt		chkTopM

			sta		S_MoveSrcBR

chkTopM
			CmpWord	S_ScrapTop,S_MoveSrcTL
			blt		redrawMove

			sta		S_MoveSrcTL

redrawMove
			Call		S_DrawMove

			lda		ErrFlag
			bne		memErrorMove

			MoveLong	[Handle],Ptr
			MoveLong	[Ptr]:#TL2,S_ScrapTL
			beq		fixMove

			MoveLong	[Ptr]:#Range2Handle,S_ScrapBuffer
			Call		S_PutSSScrap,err=ErrFlag

			in		S_ScrapTL:l,S_ScrapBR:l
			XCall	S_RedrawCellRange

	;	This is also the last part of UTranspose	;

fixTranspose
			lda		ErrFlag
			bne		memErrorMove

fixMove
			MoveLong	[Handle],Ptr

			in		[Ptr]:#TrashCellHandle:l
			XCall	S_UndoTrashCells,err=ErrFlag
			bcc		fixLE3

memErrorMove
			Call		S_DeleteUndo,in=(S_UndoType:w,S_NewUndoHandle:l)
			stzl		S_NewUndoHandle

			Call		D_MemoryError

fixLE3
			Call		S_FixLE

			lda		S_CurEditFlag
			and		#S_ManCalcBit
			bne		doneMove

			Call		S_CalculateSheet

doneMove
			rts

;-----------------------

UTranspose
			MoveLong	S_UndoLocation1,S_MoveSrcTL
			MoveLong	S_UndoLocation2,S_MoveSrcBR

			Call		S_DoTranspose,err=ErrFlag
			bcs		redrawTrans

			MoveLong	S_UndoHandle,Handle
			MoveLong	[Handle],Ptr

			MoveWord	#0,S_ScrapPasteType
			MoveLong	[Ptr]:#TL1,S_ScrapTL
			MoveLong	[Ptr]:#Range1Handle,S_ScrapBuffer
			Call		S_PutSSScrap,err=ErrFlag

			CmpWord	S_ScrapLeft,S_MoveSrcTL+2
			bge		chkRightT

			sta		S_MoveSrcTL+2

chkRightT
			CmpWord	S_ScrapRight,S_MoveSrcBR+2
			blt		chkBottomT

			sta		S_MoveSrcBR+2

chkBottomT
			CmpWord	S_ScrapBottom,S_MoveSrcBR
			blt		chkTopT

			sta		S_MoveSrcBR

chkTopT
			CmpWord	S_ScrapTop,S_MoveSrcTL
			blt		redrawTrans

			sta		S_MoveSrcTL

redrawTrans
			in		S_MoveSrcTL:l,S_MoveSrcBR:l
			XCall	S_RedrawCellRange

			in		S_MoveDestTL:l,S_MoveDestBR:l
			XCall	S_RedrawCellRange

			brl		fixTranspose

;-----------------------

USort
			Tool		_GetHandleSize,in=(S_UndoHandle:l),out=(Size:l)
			Call		D_NeedHand,in=(Size:l),out=(ax:l),err=ErrFlag
			jcs		memErrorSort2

			MoveLong	ax,Handle2
			MoveLong	ax,S_NewUndoHandle

			MoveLong	S_UndoHandle,Handle

			MoveLong	[Handle],Ptr
			MoveLong	[Handle2],Ptr2

			MoveWord	#2,Offset
			AddWord	Size,#4,Offset2

			MoveWord	[Ptr],[Ptr2]
			sta		Type

undoLoop1
			SubWord	Offset2,#12,Offset2

			MoveLong	[Ptr]:Offset,[Ptr2]:Offset2
			AddWord	Offset,#4,Offset
			AddWord	Offset2,#4,Offset2
			MoveLong	[Ptr]:Offset,[Ptr2]:Offset2

			AddWord	Offset,#4,Offset

			cmp		Size
			blt		undoLoop1

	;	New Undo handle built	;

			MoveWord	S_UndoLocation1,S_NewUndoLocation1
			sta		S_MoveDestTL
			sta		S_MoveSrcTL
			MoveWord	S_UndoLocation1+2,S_NewUndoLocation1+2
			sta		S_MoveDestTL+2
			sta		S_MoveSrcTL+2

			MoveWord	S_UndoLocation2,S_NewUndoLocation2
			sta		S_MoveDestBR
			sta		S_MoveSrcBR
			MoveWord	S_UndoLocation2+2,S_NewUndoLocation2+2
			sta		S_MoveDestBR+2
			sta		S_MoveSrcBR+2

			MoveWord	#2,Offset

undoLoop2
			MoveLong	[Handle],Ptr
			PushLong	[Ptr]:Offset

			AddWord	y,#6,y
			PushWord	[Ptr]:y
			dey
			dey
			PushWord	[Ptr]:y

			AddWord	y,#4,Offset

			lda		Type
			bne		doCol

			Call		S_SwapRows,in=(:l,:l),err=ErrFlag
			bra		chkUndoEnd

doCol
			Call		S_SwapColumns,in=(:l,:l),err=ErrFlag

chkUndoEnd
			bcs		memErrorSort

			CmpWord	Offset,Size
			blt		undoLoop2
			bra		redrawSort

memErrorSort
			Call		S_DeleteUndo,in=(S_UndoType:w,S_NewUndoHandle:l)
			stzl		S_NewUndoHandle

memErrorSort2
			Call		D_MemoryError

redrawSort
			CmpWord	S_MoveSrcBR+2,S_MoveDestBR+2
			blt		chkLeftSort

			sta		S_MoveDestBR+2

chkLeftSort
			CmpWord	S_MoveSrcTL+2,S_MoveDestTL+2
			bge		doDrawSort

			sta		S_MoveDestTL+2

doDrawSort
			in		S_MoveDestTL:l,S_MoveDestBR:l
			XCall	S_RedrawCellRange

			Call		S_FixLE

			lda		S_CurEditFlag
			and		#S_ManCalcBit
			bne		doneSort

			Call		S_CalculateSheet

doneSort
			rts

			ENDP

;-----------------------------------------------------------------------------
;
;

S_BuildColWdUndo	PROC		EXPORT
				;Using	S_CurrentData
				;Using	S_UndoData

			input	Col:w
			local	Width:w,Handle:l,Ptr:l,Size:w
			error	ErrFlag
			BEGIN

			stz		ErrFlag
			stz		Width

			lda		S_NewUndoHandle
			ora		S_NewUndoHandle+2
			bne		exists

			Call		D_NeedHand,in=(#6:l),out=(ax:l),err=ErrFlag
			bcs		Exit

			MoveLong	ax,S_NewUndoHandle
			MoveLong	ax,Handle
			MoveWord	#6,Size
			bra		addCol

exists
			MoveLong	S_NewUndoHandle,Handle
			MoveLong	[Handle],Ptr
			AddWord	[Ptr],#4,Size

			Call		D_GrowHandle,in=(#0:w,a:w,Handle:l),err=ErrFlag
			bcs		Exit

addCol
			lda		Col
			bne		notDefault

			lda		S_CurDefColWd
			bra		storeWidth

notDefault
			Call		S_CellWidth,in=(Col:w),out=(a:w)
			bcs		haveWidth

storeWidth
			sta		Width

haveWidth
			MoveLong	[Handle],Ptr
			MoveWord	Size,[Ptr]
			dec		a
			dec		a
			tay

			MoveWord	Col,[Ptr]:y
			dey
			dey
			MoveWord	Width,[Ptr]:y

Exit
			RETURN
			ENDP

;---------------------------------------------------------------------------
;
;

S_BuildSimpleUndo	PROC		EXPORT
				;Using	S_CurrentData
				;Using	S_UndoData

			input	TLCell:l,BRCell:l
			error	ErrFlag
			BEGIN

			stz		ErrFlag

			MoveLong	TLCell,S_NewUndoLocation1
			MoveLong	BRCell,S_NewUndoLocation2

			in		TLCell:l,BRCell:l
			out		S_NewUndoHandle:l
			XCall	S_GetSSScrap,err=ErrFlag
			bcc		Exit

			stzl		S_NewUndoHandle

Exit
			RETURN
			ENDP

;---------------------------------------------------------------------------
;
;

S_BuildMoveUndo	PROC		EXPORT
				;Using	S_CurrentData
				;Using	S_UndoData
				;Using	S_MoveData

			local	UndoHandle:l,UndoPtr:l
			local	BRCell2:l,TLCell2:l,BRCell1:l,TLCell1:l
			local	MoveHandle:l
			error	ErrFlag
			BEGIN

	ProfileIn		0

			stz		ErrFlag

MoveOffset		equ	0
TL1				equ	4
Range1Handle		equ	8
TL2				equ	12
Range2Handle		equ	16
TrashCellHandle	equ	20

			Call		D_NeedHand,in=(#24:l),out=(ax:l),err=ErrFlag
			jcs		Exit

			MoveLong	ax,S_NewUndoHandle
			MoveLong	ax,UndoHandle
			MoveLong	[UndoHandle],UndoPtr

			MoveLong	S_MoveDestTL,S_NewUndoLocation1
			MoveLong	S_MoveDestBR,S_NewUndoLocation2

			lda		S_DeltaMove
			eor		#-1
			inc		a
			sta		[UndoPtr]

			ldy		#MoveOffset+2
			lda		S_DeltaMove+2
			eor		#-1
			inc		a
			sta		[UndoPtr],y

			in		S_MoveSrcTL:l,S_MoveSrcBR:l
			in		S_MoveDestTL:l,S_MoveDestBR:l
			out		TLCell1:l,BRCell1:l,TLCell2:l,BRCell2:l
			XCall	S_GetDestCellRanges

			in		TLCell1:l,BRCell1:l
			out		MoveHandle:l
			XCall	S_GetSSScrap,err=ErrFlag
			jcs		memError1

			MoveLong	[UndoHandle],UndoPtr
			MoveLong	TLCell1,[UndoPtr]:#TL1
			MoveLong	MoveHandle,[UndoPtr]:#Range1Handle

			MoveWord	#0,[UndoPtr]:#TrashCellHandle
			MoveWord	a,[UndoPtr]:#TrashCellHandle+2

			MoveLong	TLCell2,[UndoPtr]:#TL2
			beq		Exit

			in		TLCell2:l,BRCell2:l
			out		MoveHandle:l
			XCall	S_GetSSScrap,err=ErrFlag

			MoveLong	[UndoHandle],UndoPtr
			bcs		memError2

			MoveLong	MoveHandle,[UndoPtr]:#Range2Handle
			bra		Exit

memError2
			Tool		_DisposeHandle,in=([UndoPtr]:#Range1Handle:l)

memError1
			Tool		_DisposeHandle,in=(UndoHandle:l)
			stz		S_NewUndoHandle
			stz		S_NewUndoHandle+2

Exit

	ProfileOut	0

			RETURN
			ENDP

;-----------------------------------------------------------------------------
;
;

S_BuildTrashUndo	PROC		EXPORT
				;Using	S_CurrentData
				;Using	S_UndoData

			input	Cell:l,FormulaIndex:l
			local	UndoHandle:l,UndoPtr:l
			local	TrashHandle:l,TrashPtr:l,ContentSize:l
			local	OldSize:l,NewSize:l,TempSize:l
			error	ErrFlag
			BEGIN

TrashCellHandle	equ	20				; also defined in BuildMoveUndo

TrashStructSize	equ	0
TrashStructStart	equ	4

TrashCellSize		equ	0
TrashCell			equ	2
TrashContent		equ	6

			stz		ErrFlag

			MoveLong	S_NewUndoHandle,UndoHandle

			lda		UndoHandle+2
			ora		UndoHandle
			jeq		Exit

			H_GetBlockSize	FormulaIndex,ContentSize

			MoveLong	[UndoHandle],UndoPtr
			MoveLong	[UndoPtr]:#TrashCellHandle,TrashHandle

			lda		TrashHandle+2
			ora		TrashHandle
			bne		exists

			AddLong	#TrashContent+TrashStructStart,ContentSize,NewSize

			Call		D_NeedHand,in=(NewSize:l),out=(TrashHandle:l),err=ErrFlag
			jcs		Exit

			MoveLong	[UndoHandle],UndoPtr
			MoveLong	TrashHandle,[UndoPtr]:#TrashCellHandle

			MoveLong	[TrashHandle],TrashPtr
			brl		addFormula

exists
			MoveLong	[TrashHandle],TrashPtr
			MoveLong	[TrashPtr],OldSize

			AddLong	#TrashContent,ContentSize,TempSize
			AddLong	TempSize,OldSize,NewSize

			Call		D_GrowHandle,in=(NewSize:l,TrashHandle:l),err=ErrFlag
			jcs		Exit

			MoveLong	[TrashHandle],TrashPtr

			AddLong	#TrashStructStart,TrashPtr,s

;			AddLong	1:s,TempSize,s
			lda		1,s
			clc
			adc		TempSize
			pha
			pha
			lda		7,s
			adc		TempSize+2
			sta		3,s

			SubLong	OldSize,#TrashStructStart,s
			Tool		_BlockMove,in=(:l,:l,:l)

addFormula
			MoveLong	NewSize,[TrashPtr]

			AddWord	#TrashContent,ContentSize,a
			MoveWord	a,[TrashPtr]:#TrashCellSize+TrashStructStart

			MoveLong	Cell,[TrashPtr]:#TrashCell+TrashStructStart

			H_GetBlockPtr	FormulaIndex,s
			AddLong		TrashPtr,#TrashContent+TrashStructStart,s
			PushLong		ContentSize
			Tool			_BlockMove,in=(:l,:l,:l)

Exit
			RETURN
			ENDP

;----------------------------------------------------------------------------
;
;

S_UndoTrashCells	PROC		EXPORT
				;Using	S_CurrentData
				;Using	S_UndoData


			input	TrashHandle:l
			local	TrashPtr:l,TrashLoc:l,TrashLimit:l
			local	CellIndex:l,CellPtr:l,CellSize:w,CellFormat:l
			local	Cell:l,ContentSize:l,FormulaIndex:l,FormulaPtr:l
			error	ErrFlag
			BEGIN

TrashCellHandle	equ	20				; also defined in BuildMoveUndo

TrashStructSize	equ	0
TrashStructStart	equ	4

TrashCellSize		equ	0
TrashCell			equ	2
TrashContent		equ	6

			stz		ErrFlag

			lda		TrashHandle
			ora		TrashHandle+2
			jeq		Exit

			MoveLong	#TrashStructStart,TrashLoc

			MoveLong	[TrashHandle],TrashPtr
			MoveLong	[TrashPtr],TrashLimit

			stz		ContentSize+2

UntrashLoop
			AddLong	[TrashHandle],TrashLoc,TrashPtr

			MoveWord	[TrashPtr]:#TrashCellSize,CellSize
			MoveLong	[TrashPtr]:#TrashCell,Cell

			SubWord	CellSize,#TrashContent,ContentSize

			H_NewBlock	ContentSize,FormulaIndex,FormulaPtr,err=ErrFlag
			jcs			Exit

	;	Memory might move	;

			AddLong	[TrashHandle],TrashLoc,TrashPtr

			AddLong	TrashPtr,#TrashContent,s
			Tool		_BlockMove,in=(:l,FormulaPtr:l,ContentSize:l)

			in		Cell:l
			out		CellIndex:l
			XCall	S_GetCellIndex

			H_GetBlockPtr	CellIndex,CellPtr

	;	We want any formulas (all cells) stored to show a current value of 0
	;	so that we won't dispose of what should be the calculated string and
	;	dispose of a 0 index.

			MoveWord	[CellPtr]:#S_CellFormat,a
			ora		#S_CellTypeFormula
			sta		CellFormat
			MoveWord	[CellPtr]:#S_CellFormat+2,CellFormat+2


			in		Cell:l,CellFormat:l,FormulaIndex:l
			out		ax:l,ax:l
			XCall	S_AcceptCell,err=ErrFlag
			bcs		Exit

			Call		S_AddCellToChangedList,in=(Cell:l)

			lda		CellSize
			AddLong	a,TrashLoc,TrashLoc

			CmpLong	TrashLoc,TrashLimit
			bge		Exit

			brl		UntrashLoop

Exit
			RETURN
			ENDP

;-----------------------------------------------------------------------------
;
;

S_StartSortUndo	PROC		EXPORT
				;Using	S_UndoData

			input	Type:w,TLCell:l,BRCell:l
			local	Handle:l,Ptr:l
			error	ErrFlag
			BEGIN

			stz		ErrFlag

			Call		D_NeedHand,in=(#2:l),out=(ax:l),err=ErrFlag
			bcs		Exit

			MoveLong	ax,S_NewUndoHandle
			MoveLong	ax,Handle
			MoveLong	[Handle],Ptr

			MoveWord	Type,[Ptr]

			MoveLong	TLCell,S_NewUndoLocation1
			MoveLong	BRCell,S_NewUndoLocation2

Exit
			RETURN
			ENDP

;-----------------------------------------------------------------------------
;
;

S_BuildSortUndo	PROC		EXPORT
				;Using	S_UndoData

			input	TLCell:l,BRCell:l
			local	Handle:l,Ptr:l,NewSize:l
			error	ErrFlag
			BEGIN

			stz		ErrFlag

			MoveLong	S_NewUndoHandle,Handle
			ora		Handle
			jeq		Exit

			Tool		_GetHandleSize,in=(Handle:l),out=(:l)
			AddLong	s,#8,NewSize

			Call		D_GrowHandle,in=(NewSize:l,Handle:l),err=ErrFlag
			bcs		Exit

			MoveLong	[Handle],Ptr

			AddLong	#2,Ptr,s
			AddLong	#10,Ptr,s
			SubLong	NewSize,#10,s
			Tool		_BlockMove,in=(:l,:l,:l)

			MoveLong	TLCell,[Ptr]:#2
			MoveLong	BRCell,[Ptr]:#6

Exit
			RETURN
			ENDP

;-----------------------------------------------------------------------------
;
;

S_FixLE		PROC		EXPORT
			;Using	S_CurrentData

			BEGIN

			CmpLong	S_CurTLSelect,S_CurBRSelect
			bne		setEmptyLE

			in		S_CurTLSelect:l
			out		:l
			XCall	S_GetCellIndex

			bra		setLE

setEmptyLE
			lda		#0
			pha
			pha

setLE
			Call		S_NewLineEdit,in=(:l)

			RETURN
			ENDP

;------------------------------------------------------------------------
;
;

S_ProtectData PROC	EXPORT

		EXPORT	S_ProtectedCell
		EXPORT	S_ExtraData

S_ProtectedCell	DS.B 	2
S_ExtraData		DS.B 	2

			ENDP

;------------------------------------------------------------------------
;
;

S_ChkCellProtect	PROC		EXPORT
				;Using	S_CurrentData
				;Using	S_ProtectData

			input	Cell:l,CellIndex:l
			local	CellPtr:l
			error	ErrorFlag
			BEGIN

			stz		ErrorFlag

	;	Since this routine is called with TraverseDest, (i.e. S_QTraverseTable)
	;	some calls may not be real cells.

			lda		CellIndex+2
			and		#S_CellTableFlags
			bne		Exit

			H_GetBlockPtr	CellIndex,CellPtr

			MoveWord	[CellPtr]:#S_CellFormat,a
			bmi		Exit

			MoveWord	[CellPtr]:#S_CellFormat+2,a
			bmi		protected

			in		S_CurTLSelect:l,S_CurBRSelect:l,Cell:l
			out		a:w
			XCall	S_InRange

			bne		Exit

			inc		S_ExtraData
			bra		Exit

protected
			inc		S_ProtectedCell
			inc		ErrorFlag

Exit
			RETURN
			ENDP
			END

