; Invect's Inverse Pattern

; HIDEDEBUG
; DEBUGOFF

; Init
SETACTIVESCREEN PATTERN
ACTIVATE
UPDATEPATTERN
setstring NumRows ROWS
setstring NumTracks TRACKS

; Make Pattern 2x
setstring TwiceNumRows NumRows
MULTIPLY 2 TwiceNumRows
vbkey ^{BS}
vbkey {TAB}
vbkey TwiceNumRows
vbkey {ENTER}

; Do It
setstring TickNum 0
LABEL BeginReverse

; Move to Top
vbkey {HOME}{HOME}{HOME}{HOME}

; Move Down to Cut Position
COMPARE TickNum 0
ISEQUAL SkipCutPosition
setstring count 0
LABEL CutPosition
ADD 1 count
key {DOWN}
COMPARE TickNum count
ISGREATER CutPosition
LABEL SkipCutPosition

; Cut the Line
UNSELECT
BLOCKSTART
BLOCKSTART
BLOCKSTART
CUT

; Move to Bottom
; setstring count 0
; LABEL ToBottom
; ADD 1 count
; vbkey {DOWN}
; COMPARE TwiceNumRows count
; ISGREATER ToBottom
vbkey {PGDN}{PGDN}{PGDN}{PGDN}{PGDN}{PGDN}{PGDN}{PGDN}{PGDN}{PGDN}{PGDN}{PGDN}{PGDN}{PGDN}{PGDN}{PGDN}{PGDN}{PGDN}{PGDN}{PGDN}{PGDN}{PGDN}{PGDN}

; Move Up to Paste Position
compare TickNum 0
ISEQUAL SkipPastePosition
setstring count 0
LABEL PastePosition
ADD 1 count
key {UP}
COMPARE TickNum count
ISGREATER PastePosition
LABEL SkipPastePosition

; Paste the Line
PASTE

; Paste Loop
ADD 1 TickNum
COMPARE NumRows TickNum
ISGREATER BeginReverse

; Back to Top
UNSELECT
vbkey {HOME}{HOME}{HOME}{HOME}

; Move Reversed Rows Up
setstring TrackCount 0
LABEL TrackLoop
ADD 1 TrackCount

; Delete Rows 
setstring count 0
LABEL MoveRowsUp
ADD 1 count
key {DEL}
COMPARE NumRows count
ISGREATER MoveRowsUp

; Track Loop
vbkey {TAB}
COMPARE NumTracks TrackCount
ISGREATER TrackLoop

; Restore Pattern Size
vbkey ^{BS}
vbkey {TAB}
vbkey NumRows
vbkey {ENTER}

; Back to Top
vbkey {HOME}{HOME}{HOME}{HOME}

info NumTracks

ENDSCRIPT