16-bit row/col for cursor

This commit is contained in:
~d6 2022-02-18 00:20:09 -05:00
parent cdc13dc609
commit 895c305cbe
1 changed files with 79 additions and 94 deletions

173
femto.tal
View File

@ -67,11 +67,6 @@
%quit! { #01 .System/halt DEO } %quit! { #01 .System/halt DEO }
%height { ;term/rows LDA2 NIP }
%last-line { ;term/rows LDA2 #0001 SUB2 NIP }
%pen-line { ;term/rows LDA2 #0002 SUB2 NIP }
%pen-col { ;term/cols LDA2 #0002 SUB2 NIP }
( startup ) ( startup )
|0100 ;startup JMP2 |0100 ;startup JMP2
@ -104,15 +99,15 @@
&ok .File/success DEI2 ;buffer/data ADD2 ;buffer/limit STA2 &ok .File/success DEI2 ;buffer/data ADD2 ;buffer/limit STA2
JMP2r JMP2r
@setup-terminal-size @setup-terminal-size ( -> )
( ;setup-80x24 JSR2 ) ( ;setup-80x24 JSR2 )
#fe #fe ;term-move-cursor JSR2 #03e7 #03e7 ;term-move-cursor JSR2
;term-get-cursor-position JSR2 ;term-get-cursor-position JSR2
;tmp/data ;tmp/pos STA2 ;tmp/data ;tmp/pos STA2
;receive-terminal-size .Console/vector DEO2 ;receive-terminal-size .Console/vector DEO2
JMP2r JMP2r
@receive-terminal-size @receive-terminal-size ( -> )
.Console/read DEI ;tmp/pos LDA2 STA .Console/read DEI ;tmp/pos LDA2 STA
;tmp/pos LDA2 INC2 ;tmp/pos STA2 ;tmp/pos LDA2 INC2 ;tmp/pos STA2
.Console/read DEI LIT 'R EQU ;parse-terminal-size JCN2 .Console/read DEI LIT 'R EQU ;parse-terminal-size JCN2
@ -175,42 +170,42 @@
( ;setup-80x24 JSR2 ( hardcoded terminal size ) ) ( ;setup-80x24 JSR2 ( hardcoded terminal size ) )
BRK BRK
@bol @bol ( -> )
#00 ;cursor/col STA #0000 ;cursor/col STA2
;draw-statusbar JSR2 ;draw-statusbar JSR2
;draw-cursor JSR2 BRK ;draw-cursor JSR2 BRK
( FIXME: handle long lines ) ( FIXME: handle long lines )
@eol @eol ( -> )
;cur-line JSR2 ;line-len JSR2 NIP ;cursor/col STA ;cur-line JSR2 ;line-len JSR2 ;cursor/col STA2
;draw-statusbar JSR2 ;draw-statusbar JSR2
;draw-cursor JSR2 BRK ;draw-cursor JSR2 BRK
( FIXME: handle long lines ) ( FIXME: handle long lines )
@forward @forward ( -> )
;cur-pos JSR2 ;last-pos JSR2 GTH2 ,&skip JCN ;cur-pos JSR2 ;last-pos JSR2 GTH2 ,&skip JCN
;cur-col JSR2 ;cur-last JSR2 GTH ,&next-line JCN ;cur-col JSR2 ;cur-last JSR2 GTH2 ,&next-line JCN
;cur-col JSR2 INC ;cursor/col STA ;cur-col JSR2 INC2 ;cursor/col STA2
;draw-statusbar JSR2 ;draw-cursor JSR2 ;draw-statusbar JSR2 ;draw-cursor JSR2
,&skip JMP ,&skip JMP
&next-line #00 ;cursor/col STA ( TODO: need to ensure cursor is visible ) &next-line #0000 ;cursor/col STA2
;cursor/row LDA INC ;cursor/row STA ;cursor/row LDA2 INC2 ;cursor/row STA2
;ensure-visible-cursor JSR2 ;ensure-visible-cursor JSR2
;draw-cursor JSR2 ;draw-cursor JSR2
&skip BRK &skip BRK
( FIXME: handle long lines ) ( FIXME: handle long lines )
@back @back ( -> )
;cur-col JSR2 #01 LTH ,&skip JCN ;cur-col JSR2 #01 LTH ,&skip JCN
;cur-col JSR2 #01 SUB ;cursor/col STA ;cur-col JSR2 #0001 SUB2 ;cursor/col STA2
;draw-statusbar JSR2 ;draw-statusbar JSR2
;draw-cursor JSR2 ;draw-cursor JSR2
&skip BRK &skip BRK
@up @up ( -> )
;cur-line-num JSR2 #0000 EQU2 ,&done JCN ;cur-line-num JSR2 #0000 EQU2 ,&done JCN
;cursor/row LDA #01 LTH ,&screen-up JCN ;cursor/row LDA2 #0001 LTH2 ,&screen-up JCN
;cursor/row LDA #01 SUB ;cursor/row STA ;cursor/row LDA2 #0001 SUB2 ;cursor/row STA2
;draw-statusbar JSR2 ;draw-statusbar JSR2
;draw-cursor JSR2 BRK ;draw-cursor JSR2 BRK
&screen-up &screen-up
@ -219,15 +214,15 @@
;draw-all JSR2 ;draw-all JSR2
&done BRK &done BRK
@down @down ( -> )
;cur-abs-row JSR2 ;last-abs-row JSR2 EQU2 ,&done JCN ;cur-abs-row JSR2 ;last-abs-row JSR2 EQU2 ,&done JCN
;cursor/row LDA INC ;cursor/row STA ;cursor/row LDA2 INC2 ;cursor/row STA2
;ensure-visible-cursor JSR2 ;ensure-visible-cursor JSR2
;draw-statusbar JSR2 ;draw-statusbar JSR2
;draw-cursor JSR2 BRK ;draw-cursor JSR2 BRK
&done BRK &done BRK
@page-up @page-up ( -> )
;bof-is-visible JSR2 ,&near-eof JCN ;bof-is-visible JSR2 ,&near-eof JCN
;buffer/line-offset LDA2 ;buffer/line-offset LDA2
;term/rows LDA2 SUB2 #0003 ADD2 ;term/rows LDA2 SUB2 #0003 ADD2
@ -236,8 +231,8 @@
;ensure-visible-cursor JSR2 ;ensure-visible-cursor JSR2
;draw-all JSR2 BRK ;draw-all JSR2 BRK
&near-eof &near-eof
#00 ;cursor/row STA #0000 ;cursor/row STA2
#00 ;cursor/col STA #0000 ;cursor/col STA2
;draw-cursor JSR2 BRK ;draw-cursor JSR2 BRK
@page-down @page-down
@ -251,8 +246,8 @@
&near-eof &near-eof
;buffer/line-count LDA2 ;buffer/line-count LDA2
;buffer/line-offset LDA2 INC2 ;buffer/line-offset LDA2 INC2
SUB2 NIP ;cursor/row STA SUB2 ;cursor/row STA2
;cur-len JSR2 ;cursor/col STA ;cur-len JSR2 ;cursor/col STA2
;draw-cursor JSR2 BRK ;draw-cursor JSR2 BRK
@quit quit! @quit quit!
@ -261,15 +256,15 @@
@insert ( c^ -> ) @insert ( c^ -> )
;cur-pos JSR2 ;shift-right JSR2 ;cur-pos JSR2 ;shift-right JSR2
;cur-col JSR2 INC ;cursor/col STA ;cur-col JSR2 INC2 ;cursor/col STA2
;draw-all JSR2 ;draw-all JSR2
BRK BRK
( TODO: handle last line ) ( TODO: handle last line )
@newline ( c^ -> ) @newline ( c^ -> )
#0a ;cur-pos JSR2 ;shift-right JSR2 #0a ;cur-pos JSR2 ;shift-right JSR2
#00 ;cursor/col STA #0000 ;cursor/col STA2
;cursor/row LDA INC ;cursor/row STA ;cursor/row LDA2 INC2 ;cursor/row STA2
;buffer/line-count LDA2k INC2 SWP2 STA2 ;buffer/line-count LDA2k INC2 SWP2 STA2
;ensure-visible-cursor JSR2 ;ensure-visible-cursor JSR2
;draw-all JSR2 ;draw-all JSR2
@ -279,7 +274,7 @@
;cur-pos JSR2 ;buffer/data EQU2 JMP2r ;cur-pos JSR2 ;buffer/data EQU2 JMP2r
@at-line-start ( -> bool^ ) @at-line-start ( -> bool^ )
;cursor/col LDA #00 EQU JMP2r ;cursor/col LDA2 #0000 EQU2 JMP2r
@bof-is-visible ( -> bool^ ) @bof-is-visible ( -> bool^ )
;buffer/line-offset LDA2 #0000 EQU2 JMP2r ;buffer/line-offset LDA2 #0000 EQU2 JMP2r
@ -292,12 +287,12 @@
@backspace ( -> ) @backspace ( -> )
;at-buffer-start JSR2 ,&skip JCN ;at-buffer-start JSR2 ,&skip JCN
;at-line-start JSR2 ,&prev-line JCN ;at-line-start JSR2 ,&prev-line JCN
;cur-col JSR2 #01 SUB ;cursor/col STA ;cur-col JSR2 #0001 SUB2 ;cursor/col STA2
,&finish JMP ,&finish JMP
&prev-line &prev-line
;cur-line-num JSR2 #0001 SUB2 ;cur-line-num JSR2 #0001 SUB2
;jump-to-line JSR2 ( TODO: fix weird eof behavior ) ;jump-to-line JSR2 ( TODO: fix weird eof behavior )
;cur-len JSR2 NIP ;cursor/col STA ;cur-len JSR2 ;cursor/col STA2
;buffer/line-count LDA2k #0001 SUB2 SWP2 STA2 ;buffer/line-count LDA2k #0001 SUB2 SWP2 STA2
&finish &finish
;cur-pos JSR2 ;shift-left JSR2 ;cur-pos JSR2 ;shift-left JSR2
@ -319,22 +314,22 @@
@goto-end ( -> ) @goto-end ( -> )
;more-than-one-screen JSR2 ,&large JCN ;more-than-one-screen JSR2 ,&large JCN
;buffer/line-count LDA2 NIP #01 SUB ;cursor/row STA ;buffer/line-count LDA2 #0001 SUB2 ;cursor/row STA2
#0000 ,&continue JMP #0000 ,&continue JMP
&large &large
height #01 SUB ;cursor/row STA ;term/rows LDA2 #0001 SUB2 ;cursor/row STA2
;buffer/line-count LDA2 ;term/rows LDA2 SUB2 ;buffer/line-count LDA2 ;term/rows LDA2 SUB2
&continue &continue
DUP2 ;buffer/line-offset STA2 DUP2 ;buffer/line-offset STA2
;abs-line JSR2 ;buffer/offset STA2 ;abs-line JSR2 ;buffer/offset STA2
;cur-len JSR2 NIP ;cursor/col STA ;cur-len JSR2 ;cursor/col STA2
;draw-all JSR2 BRK ;draw-all JSR2 BRK
@goto-start ( -> ) @goto-start ( -> )
;buffer/data ;buffer/offset STA2 ;buffer/data ;buffer/offset STA2
#0000 ;buffer/line-offset STA2 #0000 ;buffer/line-offset STA2
#00 ;cursor/col STA #0000 ;cursor/col STA2
#00 ;cursor/row STA #0000 ;cursor/row STA2
;draw-all JSR2 ;draw-all JSR2
BRK BRK
@ -358,12 +353,12 @@
SUB2k STH2 DUP2 ( n o o [n-o] ) SUB2k STH2 DUP2 ( n o o [n-o] )
;buffer/line-offset STA2 ( n o [n-o] ) ;buffer/line-offset STA2 ( n o [n-o] )
;abs-line JSR2 ;buffer/offset STA2 ( n [n-o] ) ;abs-line JSR2 ;buffer/offset STA2 ( n [n-o] )
#00 ;cursor/col STA ( n [n-o] ) #0000 ;cursor/col STA2 ( n [n-o] )
POP2 STH2r NIP ;cursor/row STA ( ) POP2 STH2r ;cursor/row STA2 ( )
JMP2r JMP2r
@ensure-visible-cursor @ensure-visible-cursor
;cursor/row LDA height LTH ,&noop JCN ;cursor/row LDA2 ;term/rows LDA2 LTH2 ,&noop JCN
;cur-line-num JSR2 ;jump-to-line JSR2 ;cur-line-num JSR2 ;jump-to-line JSR2
;draw-all JSR2 ;draw-all JSR2
&noop JMP2r &noop JMP2r
@ -373,12 +368,11 @@
@debug @debug
;rel-line-error ;error! JMP2 ;rel-line-error ;error! JMP2
( #00 #00 DIV BRK )
( TODO: M-f and M-b for next/previous word ) ( TODO: M-f and M-b for next/previous word )
( M-n and M-p for next/previous paragraph ) ( M-n and M-p for next/previous paragraph )
( maybe M-% for search&replace ) ( maybe M-% for search&replace )
@on-key-escaped @on-key-escaped ( -> )
#00 ;saw-esc STA #00 ;saw-esc STA
.Console/read DEI LIT '< EQU ( M-< ) ;goto-start JCN2 .Console/read DEI LIT '< EQU ( M-< ) ;goto-start JCN2
.Console/read DEI LIT '> EQU ( M-> ) ;goto-end JCN2 .Console/read DEI LIT '> EQU ( M-> ) ;goto-end JCN2
@ -387,7 +381,7 @@
BRK BRK
@move-to-message-line ( -> ) @move-to-message-line ( -> )
#02 height #02 ADD ;term-move-cursor JSR2 JMP2r #0002 ;term/rows LDA2 #0002 ADD2 ;term-move-cursor JSR2 JMP2r
( TODO: filename prmopt ) ( TODO: filename prmopt )
@save ( -> ) @save ( -> )
@ -429,29 +423,26 @@
.Console/read DEI ( printable ASCII ) ;insert JMP2 .Console/read DEI ( printable ASCII ) ;insert JMP2
BRK BRK
@min ( x^ y^ -> min^ )
LTHk JMP SWP POP JMP2r
@min2 ( x* y* -> min* ) @min2 ( x* y* -> min* )
LTH2k JMP SWP2 POP2 JMP2r LTH2k JMP SWP2 POP2 JMP2r
@term-move-cursor ( col^ row^ -> ) @term-move-cursor ( col* row* -> )
ansi INC ( row+1 ) ;emit-dec JSR2 ansi INC2 ( row+1 ) ;emit-dec2 JSR2
LIT '; emit INC ( col+1 ) ;emit-dec JSR2 LIT '; emit INC2 ( col+1 ) ;emit-dec2 JSR2
LIT 'H emit JMP2r LIT 'H emit JMP2r
@term-get-cursor-position @term-get-cursor-position ( -> )
ansi LIT '6 emit LIT 'n emit JMP2r ansi LIT '6 emit LIT 'n emit JMP2r
@term-erase-all @term-erase-all ( -> )
ansi LIT '2 emit LIT 'J emit JMP2r ansi LIT '2 emit LIT 'J emit JMP2r
@draw-cursor @draw-cursor ( -> )
;cur-col JSR2 ;cur-row JSR2 ;cur-col JSR2 ;cur-row JSR2
;term-move-cursor JSR2 JMP2r ;term-move-cursor JSR2 JMP2r
@draw-statusbar @draw-statusbar ( -> )
#00 height ;term-move-cursor JSR2 #0000 ;term/rows LDA2 ;term-move-cursor JSR2
ansi LIT '7 emit LIT 'm emit ansi LIT '7 emit LIT 'm emit
LIT2r 2018 LIT2r 2018
;term/cols LDA2 #0000 ;term/cols LDA2 #0000
@ -459,7 +450,7 @@
&continue DEOkr INC2 ,&loop JMP &continue DEOkr INC2 ,&loop JMP
&done POP2 POP2 POP2r &done POP2 POP2 POP2r
#00 height ;term-move-cursor JSR2 #0000 ;term/rows LDA2 ;term-move-cursor JSR2
;messages/saved ;print JSR2 ;messages/saved ;print JSR2
;filename ;print JSR2 ;filename ;print JSR2
#20 emit #20 emit
@ -479,31 +470,25 @@
ansi LIT '0 emit LIT 'm emit ansi LIT '0 emit LIT 'm emit
JMP2r JMP2r
( @draw-line ( s* -> ) @draw-all ( -> )
&loop LDAk #00 EQU ,&done JCN
LDAk #0a EQU ,&done JCN
LDAk emit INC2 ,&loop JMP
&done POP2 JMP2r )
@draw-all
;term-erase-all JSR2 ;term-erase-all JSR2
#00 #00 ;term-move-cursor JSR2 #0000 #0000 ;term-move-cursor JSR2
#01 STH #0001 STH2
;buffer/offset LDA2 ;buffer/offset LDA2
&loop &loop
LDAk #00 EQU ,&eof JCN LDAk #00 EQU ,&eof JCN
LDAk #0a EQU ,&eol JCN LDAk #0a EQU ,&eol JCN
LDAk emit INC2 ,&loop JMP LDAk emit INC2 ,&loop JMP
&eol INCr STHkr height GTH ,&done JCN &eol INC2r STH2kr ;term/rows LDA2 GTH2 ,&done JCN
cr nl INC2 ,&loop JMP cr nl INC2 ,&loop JMP
&eof &eof
ansi LIT '3 emit LIT '1 emit LIT 'm emit ansi LIT '3 emit LIT '1 emit LIT 'm emit
&eof-loop &eof-loop
STHkr height GTH ,&done JCN STH2kr ;term/rows LDA2 GTH2 ,&done JCN
cr nl cr nl
LIT '~ emit INCr LIT '~ emit INC2r
,&eof-loop JMP ,&eof-loop JMP
&done POP2 POPr &done POP2 POP2r
ansi LIT '0 emit LIT 'm emit ansi LIT '0 emit LIT 'm emit
;draw-statusbar JSR2 ;draw-statusbar JSR2
;draw-cursor JSR2 ;draw-cursor JSR2
@ -526,7 +511,7 @@
;cur-line JSR2 ;line-len JSR2 JMP2r ;cur-line JSR2 ;line-len JSR2 JMP2r
@cur-last ( -> n* ) @cur-last ( -> n* )
;cur-line JSR2 ;line-len JSR2 #0001 SUB2 NIP JMP2r ;cur-line JSR2 ;line-len JSR2 #0001 SUB2 JMP2r
@line-len ( s* -> n* ) @line-len ( s* -> n* )
#0000 STH2 #0000 STH2
@ -549,31 +534,31 @@
&not-found POP2 POP2r #0000 JMP2r &not-found POP2 POP2r #0000 JMP2r
( line number relative to the offset, starting at 0 ) ( line number relative to the offset, starting at 0 )
@rel-line ( y^ -> s* ) @rel-line ( y* -> s* )
#00 SWP SUB STH ( [-y] ) #0000 SWP2 SUB2 STH2 ( [-y] )
;buffer/offset LDA2 ( addr* ) ;buffer/offset LDA2 ( addr* )
STHkr #00 EQU ,&done JCN ( addr [-y] ) STH2kr #0000 EQU2 ,&done JCN ( addr [-y] )
&newline ( addr [-y] ) &newline ( addr [-y] )
STHkr ,&loop JCN ,&done JMP STH2kr ORA ,&loop JCN ,&done JMP
&loop ( addr [-y] ) &loop ( addr [-y] )
LDAk #00 EQU ,&not-found JCN ( addr [-y] ) LDAk #00 EQU ,&not-found JCN ( addr [-y] )
LDAk #0a EQU ,&found JCN ( addr [-y] ) LDAk #0a EQU ,&found JCN ( addr [-y] )
INC2 ,&loop JMP ( addr+1 [-y] ) INC2 ,&loop JMP ( addr+1 [-y] )
&found INC2 INCr ( addr+1 [-y+1] ) ,&newline JMP &found INC2 INC2r ( addr+1 [-y+1] ) ,&newline JMP
&done POPr JMP2r &done POP2r JMP2r
&not-found ;rel-line-error ;error! JMP2 &not-found ;rel-line-error ;error! JMP2
@cur-line ( -> s* ) @cur-line ( -> s* )
;cursor/row LDA ;rel-line JSR2 JMP2r ;cursor/row LDA2 ;rel-line JSR2 JMP2r
@cur-line-num ( -> n* ) @cur-line-num ( -> n* )
#00 ;cursor/row LDA ;buffer/line-offset LDA2 ADD2 JMP2r ;cursor/row LDA2 ;buffer/line-offset LDA2 ADD2 JMP2r
@cur-pos ( -> s* ) @cur-pos ( -> s* )
;cur-line JSR2 #00 ;cur-col JSR2 ADD2 JMP2r ;cur-line JSR2 ;cur-col JSR2 ADD2 JMP2r
@cur-abs-row ( -> n* ) @cur-abs-row ( -> n* )
;buffer/line-offset LDA2 #00 ;cursor/row LDA ADD2 JMP2r ;buffer/line-offset LDA2 ;cursor/row LDA2 ADD2 JMP2r
@last-abs-row ( -> n* ) @last-abs-row ( -> n* )
;buffer/line-count LDA2 #0001 SUB2 JMP2r ;buffer/line-count LDA2 #0001 SUB2 JMP2r
@ -609,13 +594,13 @@
( TODO: should be using cur-col and cur-row almost everywhere ) ( TODO: should be using cur-col and cur-row almost everywhere )
( otherwise, bugs! ) ( otherwise, bugs! )
@cur-col @cur-col ( -> col* )
;cursor/col LDA ;cur-len JSR2 NIP ;min JSR2 JMP2r ;cursor/col LDA2 ;cur-len JSR2 ;min2 JSR2 JMP2r
@cur-row @cur-row ( -> row* )
;cursor/row LDA JMP2r ;cursor/row LDA2 JMP2r
@last-pos @last-pos ( -> addr* )
;buffer/limit LDA2 #0001 SUB2 JMP2r ;buffer/limit LDA2 #0001 SUB2 JMP2r
@more-than-one-screen ( -> bool^ ) @more-than-one-screen ( -> bool^ )
@ -630,12 +615,11 @@
@mod-div2 ( x^ y^ -> x%d x/y ) @mod-div2 ( x^ y^ -> x%d x/y )
DIV2k STH2k MUL2 SUB2 STH2r JMP2r DIV2k STH2k MUL2 SUB2 STH2r JMP2r
@emit ( @emit
( &long SWP2 ,&short JSR ) &short ( s* -> ) SWP ,&byte JSR
&short SWP ,&byte JSR &byte ( b^ -> ) DUP #04 SFT ,&nib JSR
&byte DUP #04 SFT ,&char JSR &nib ( n^ -> ) #0f AND DUP #09 GTH #27 MUL ADD #30 ADD #18 DEO
&char #0f AND DUP #09 GTH #27 MUL ADD #30 ADD #18 DEO JMP2r )
JMP2r
@emit-digit ( n^ -> ) @emit-digit ( n^ -> )
LIT '0 ADD emit JMP2r LIT '0 ADD emit JMP2r
@ -674,7 +658,8 @@
@term [ &cols 0050 @term [ &cols 0050
&rows 0018 ] &rows 0018 ]
@cursor [ &col 00 &row 00 ] ( relative cursor positions, e.g. 0 to cols-1 )
@cursor [ &col 0000 &row 0000 ]
( did we just see ESC? ) ( did we just see ESC? )
@saw-esc 00 @saw-esc 00