reading windows line endings works

This commit is contained in:
~d6 2022-04-06 20:39:02 -04:00
parent ab336cdc00
commit 8229c0db14
1 changed files with 78 additions and 37 deletions

109
femto.tal
View File

@ -75,12 +75,14 @@
%emit-3 { LIT2 '3 18 DEO } %emit-3 { LIT2 '3 18 DEO }
%emit-6 { LIT2 '6 18 DEO } %emit-6 { LIT2 '6 18 DEO }
%emit-7 { LIT2 '7 18 DEO } %emit-7 { LIT2 '7 18 DEO }
%emit-: { LIT2 ': 18 DEO }
%emit-; { LIT2 '; 18 DEO } %emit-; { LIT2 '; 18 DEO }
%emit-C { LIT2 'C 18 DEO } %emit-C { LIT2 'C 18 DEO }
%emit-H { LIT2 'H 18 DEO } %emit-H { LIT2 'H 18 DEO }
%emit-J { LIT2 'J 18 DEO } %emit-J { LIT2 'J 18 DEO }
%emit-K { LIT2 'K 18 DEO } %emit-K { LIT2 'K 18 DEO }
%emit-[ { LIT2 '[ 18 DEO } %emit-[ { LIT2 '[ 18 DEO }
%emit-] { LIT2 '] 18 DEO }
%emit-m { LIT2 'm 18 DEO } %emit-m { LIT2 'm 18 DEO }
%emit-n { LIT2 'n 18 DEO } %emit-n { LIT2 'n 18 DEO }
%emit-~ { LIT2 '~ 18 DEO } %emit-~ { LIT2 '~ 18 DEO }
@ -103,6 +105,7 @@
&tab-width $2 ( how many spaces to display tab chars ) &tab-width $2 ( how many spaces to display tab chars )
&insert-tabs $1 ( tab key inserts tabs when true ) &insert-tabs $1 ( tab key inserts tabs when true )
&color $2 ( digits of highlight color in reverse order ) &color $2 ( digits of highlight color in reverse order )
&line-endings $1 ( 00 = unix, 01 = windows, 80 = mixed )
] ]
( tracks information related to the buffer's view of data ) ( tracks information related to the buffer's view of data )
@ -193,10 +196,9 @@
( the interpreter to halt. ) ( the interpreter to halt. )
@error! ( msg* -> ) @error! ( msg* -> )
emit-! sp emit-! sp
&loop LDAk ,&continue JCN ,&done JMP &loop LDAk #00 EQU ,&done JMP
&continue LDAk emit INC2 ,&loop JMP LDAk emit INC2 ,&loop JMP
&done POP2 nl &done POP2 nl dbg BRK
dbg BRK
( open the given file at editor start up ) ( open the given file at editor start up )
( ) ( )
@ -242,43 +244,68 @@
( ) ( )
( called by ;receive-terminal-size after complete message received ) ( called by ;receive-terminal-size after complete message received )
@parse-terminal-size ( -> ) @parse-terminal-size ( -> )
LIT2r 0000 LIT2r 0000 #0000 ,&acc STR2
.tmp/data LDZk #1b NEQ ,&parse-error JCN ( i ) INC .tmp/data LDZk #1b NEQ ,&parse-error JCN ( i ) INC
LDZk LIT '[ NEQ ,&parse-error JCN ( i ) INC LDZk LIT '[ NEQ ,&parse-error JCN ( i ) INC
&loop &loop
LDZk LIT '; EQU ,&parse-col JCN LDZk LIT '; EQU ,&parse-col JCN
LIT2r 000a MUL2r LIT2r :&loop ,&read JMP
LDZk LIT '0 SUB #00 SWP STH2 ADD2r
INC ,&loop JMP
&parse-col &parse-col
INC STH2r #0002 SUB2 .term/rows STZ2 INC ,&acc LDR2 #0002 SUB2 .term/rows STZ2
#0000 ,&acc STR2
&loop2 &loop2
LDZk LIT 'R EQU ,&done JCN LDZk LIT 'R EQU ,&done JCN
LIT2r 000a MUL2r LIT2r :&loop2 ,&read JMP
LDZk LIT '0 SUB #00 SWP STH2 ADD2r &read
INC ,&loop2 JMP LDZk LIT '0 SUB #00 SWP
,&acc LDR2 #000a MUL2 ADD2 ,&acc STR2
INC JMP2r
&done &done
STH2r .term/cols STZ2 POP ,&acc LDR2 .term/cols STZ2 POP
;on-key .Console/vector DEO2 ;on-key .Console/vector DEO2
;draw-all JSR2 ;draw-all JSR2
BRK BRK
[ &acc $2 ]
&parse-error POP .tmp/data LDZ2 &parse-error POP .tmp/data LDZ2
;messages/term-size-parse-error ;error! JMP2 ;messages/term-size-parse-error ;error! JMP2
@count-c ( c^ -> n* )
#0000 ,&count STR2
STH ;data
&loop ( DUP2 .buffer/limit LDZ2 EQU2 ,&done JCN )
LDAk #00 EQU ,&done JCN
LDAk STHkr NEQ ,&next JCN
,&count LDR2 INC2 ,&count STR2
&next INC2 ,&loop JMP
&done POP2 POPr ,&count LDR2 JMP2r [ &count $2 ]
( save count of number of lines in input file ) ( save count of number of lines in input file )
( ) ( )
( this method also detects whether \t characters are used, ) ( this method also detects whether \t characters are used, )
( and uses this to initialize config/insert-tabs. ) ( and uses this to initialize config/insert-tabs. )
@setup-linecount ( -> ) @setup-linecount ( -> )
;data LIT2r 0001 #0a ;count-c JSR2 INC2 .buffer/line-count STZ2
&loop DUP2 .buffer/limit LDZ2 EQU2 ,&done JCN #09 ;count-c JSR2 #0000 GTH2 .config/insert-tabs STZ
LDAk #00 EQU ,&done JCN #0d ;count-c JSR2 DUP2 INC2 .buffer/line-count LDZ2 EQU2 STH
LDAk #09 NEQ ,&next JCN #01 .config/insert-tabs STZ #0000 NEQ2 STHkr #00 EQU AND #80 MUL
&next LDAk #0a NEQ JMP INC2r STHr ADD DUP .config/line-endings STZ
INC2 ,&loop JMP ;strip-cr JCN2 JMP2r
&done POP2
STH2r .buffer/line-count STZ2 ( strip our carriage returns \r )
JMP2r ( )
( a few imporant notes: )
( 1. we need "re-add" these \r bytes when displaying the buffer size )
( 2. we need to "zero out" all the trailing bytes )
@strip-cr ( -> )
;data LIT2r :data ( readpos [writepos] )
&loop LDAk #00 EQU ,&done JCN
LDAk #0d NEQ ,&next JCN INC2 ,&loop JMP
&next LDAk STH2kr STA
INC2 INC2r ,&loop JMP
&done STH2kr .buffer/limit STZ2
&fill DUP2 STH2kr EQU2 ,&exit JCN
#00 STH2kr STA INC2r ,&fill JMP
&exit POP2 POP2r JMP2r
( reads filename from the program's argv ) ( reads filename from the program's argv )
( ) ( )
@ -315,6 +342,7 @@
( move forward by one character ) ( move forward by one character )
@forward ( -> ) @forward ( -> )
;redraw-statusbar-and-cursor JSR2
;cur-pos JSR2 ;last-pos JSR2 GTH2 ;return JCN2 ;cur-pos JSR2 ;last-pos JSR2 GTH2 ;return JCN2
;cur-col JSR2 ;cur-len JSR2 LTH2 ,&normal JCN ;cur-col JSR2 ;cur-len JSR2 LTH2 ,&normal JCN
#0000 .cursor/col STZ2 #0000 .cursor/col STZ2
@ -323,7 +351,6 @@
;return JMP2 ;return JMP2
&normal &normal
;cur-col JSR2 INC2 .cursor/col STZ2 ;cur-col JSR2 INC2 .cursor/col STZ2
;redraw-statusbar-and-cursor JSR2
;return JMP2 ;return JMP2
( move backward by one character ) ( move backward by one character )
@ -692,6 +719,8 @@
;redraw-prompt-and-cursor JSR2 ;redraw-prompt-and-cursor JSR2
;return JMP2 ;return JMP2
( TODO: need to add \r back in for windows )
( save the file with the filename found in tmp/data ) ( save the file with the filename found in tmp/data )
@do-save ( -> ) @do-save ( -> )
.buffer/limit LDZ2 ;data SUB2 STH2 ( [size] ) .buffer/limit LDZ2 ;data SUB2 STH2 ( [size] )
@ -772,7 +801,15 @@
( when opening a file, this defaults to 01 if existing tab ) ( when opening a file, this defaults to 01 if existing tab )
( characters are found, and 00 otherwise. ) ( characters are found, and 00 otherwise. )
@toggle-tabs ( -> ) @toggle-tabs ( -> )
.config/insert-tabs LDZk #01 EOR SWP STZ .config/insert-tabs LDZk #00 EQU SWP STZ
;redraw-statusbar-and-cursor JSR2 ;return JMP2
( toggle whether to use literal tab characters )
( )
( when opening a file, this defaults to 01 if existing tab )
( characters are found, and 00 otherwise. )
@toggle-line-endings ( -> )
.config/line-endings LDZk #00 EQU SWP STZ
;redraw-statusbar-and-cursor JSR2 ;return JMP2 ;redraw-statusbar-and-cursor JSR2 ;return JMP2
( interpret user input as an escaped sequence ) ( interpret user input as an escaped sequence )
@ -788,6 +825,7 @@
.state/key LDZ LIT '> EQU ( M-> ) ;goto-end JCN2 .state/key LDZ LIT '> EQU ( M-> ) ;goto-end JCN2
.state/key LDZ LIT 'c EQU ( M-c ) ;toggle-color JCN2 .state/key LDZ LIT 'c EQU ( M-c ) ;toggle-color JCN2
.state/key LDZ LIT 'g EQU ( M-g ) ;goto-line JCN2 .state/key LDZ LIT 'g EQU ( M-g ) ;goto-line JCN2
.state/key LDZ LIT 'l EQU ( M-l ) ;toggle-line-endings JCN2
.state/key LDZ LIT 's EQU ( M-s ) ;regex-search JCN2 .state/key LDZ LIT 's EQU ( M-s ) ;regex-search JCN2
.state/key LDZ LIT 't EQU ( M-t ) ;toggle-tabs JCN2 .state/key LDZ LIT 't EQU ( M-t ) ;toggle-tabs JCN2
.state/key LDZ LIT 'v EQU ( M-v ) ;page-up JCN2 .state/key LDZ LIT 'v EQU ( M-v ) ;page-up JCN2
@ -1145,15 +1183,16 @@
;messages/unsaved ;messages/saved ;messages/unsaved ;messages/saved
.state/modified LDZ JMP SWP2 POP2 JMP2r .state/modified LDZ JMP SWP2 POP2 JMP2r
( display [t] if the file uses tabs, [s] otherwise )
@get-tab-status
;messages/st-tabs ;messages/st-spaces
.config/insert-tabs LDZ JMP SWP2 POP2 JMP2r
( move the terminal cursor to the statusbar line ) ( move the terminal cursor to the statusbar line )
@move-to-statusbar ( -> ) @move-to-statusbar ( -> )
#0000 .term/rows LDZ2 ;term-move-cursor JMP2 #0000 .term/rows LDZ2 ;term-move-cursor JMP2
@buffer-size
.buffer/limit LDZ2 ;data SUB2
.config/line-endings LDZ #00 EQU ,&done JCN
.buffer/line-count LDZ2 #0001 SUB2 ADD2
&done JMP2r
( draw the full statusbar ) ( draw the full statusbar )
@draw-statusbar ( -> ) @draw-statusbar ( -> )
;move-to-statusbar JSR2 ;move-to-statusbar JSR2
@ -1167,7 +1206,8 @@
;get-save-status JSR2 ;print JSR2 ;get-save-status JSR2 ;print JSR2
;filename ;print JSR2 ;filename ;print JSR2
sp emit-[ sp emit-[
.buffer/limit LDZ2 ;data SUB2 ;emit-dec2 JSR2 ( .buffer/limit LDZ2 ;data SUB2 ;emit-dec2 JSR2 )
;buffer-size JSR2 ;emit-dec2 JSR2
;messages/bytes ;print JSR2 ;messages/bytes ;print JSR2
sp sp
.buffer/line-count LDZ2 ;emit-dec2 JSR2 .buffer/line-count LDZ2 ;emit-dec2 JSR2
@ -1176,8 +1216,11 @@
;cur-col JSR2 INC2 ;emit-dec2 JSR2 ;cur-col JSR2 INC2 ;emit-dec2 JSR2
emit-, emit-,
.cursor/row LDZ2 INC2 ;emit-dec2 JSR2 .cursor/row LDZ2 INC2 ;emit-dec2 JSR2
emit-) sp emit-) sp emit-[
;get-tab-status JSR2 ;print JSR2 LIT 's .config/insert-tabs LDZ ADD emit
emit-:
LIT 'u .config/line-endings LDZ #00 NEQ #02 MUL ADD emit ( "u" or "w" )
emit-]
;emit-reset JMP2 ;emit-reset JMP2
@draw-prompt ( -> ) @draw-prompt ( -> )
@ -1544,8 +1587,6 @@
&unsaved "** 20 00 &unsaved "** 20 00
&term-size-parse-error "error 20 "parsing 20 "term 20 "size 00 &term-size-parse-error "error 20 "parsing 20 "term 20 "size 00
&rel-line-error "invalid 20 "relative 20 "line 20 "number 00 &rel-line-error "invalid 20 "relative 20 "line 20 "number 00
&st-tabs "[t] 00
&st-spaces "[s] 00
] ]
( path to file being edited ) ( path to file being edited )