From 8229c0db1444ed1b75c98eb53dd1c953018381b7 Mon Sep 17 00:00:00 2001 From: d6 Date: Wed, 6 Apr 2022 20:39:02 -0400 Subject: [PATCH] reading windows line endings works --- femto.tal | 115 ++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 78 insertions(+), 37 deletions(-) diff --git a/femto.tal b/femto.tal index 2d063dc..44f6abb 100644 --- a/femto.tal +++ b/femto.tal @@ -75,12 +75,14 @@ %emit-3 { LIT2 '3 18 DEO } %emit-6 { LIT2 '6 18 DEO } %emit-7 { LIT2 '7 18 DEO } +%emit-: { LIT2 ': 18 DEO } %emit-; { LIT2 '; 18 DEO } %emit-C { LIT2 'C 18 DEO } %emit-H { LIT2 'H 18 DEO } %emit-J { LIT2 'J 18 DEO } %emit-K { LIT2 'K 18 DEO } %emit-[ { LIT2 '[ 18 DEO } +%emit-] { LIT2 '] 18 DEO } %emit-m { LIT2 'm 18 DEO } %emit-n { LIT2 'n 18 DEO } %emit-~ { LIT2 '~ 18 DEO } @@ -100,9 +102,10 @@ ( configuration settings used when editing ) @config [ - &tab-width $2 ( how many spaces to display tab chars ) - &insert-tabs $1 ( tab key inserts tabs when true ) - &color $2 ( digits of highlight color in reverse order ) + &tab-width $2 ( how many spaces to display tab chars ) + &insert-tabs $1 ( tab key inserts tabs when true ) + &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 ) @@ -193,10 +196,9 @@ ( the interpreter to halt. ) @error! ( msg* -> ) emit-! sp - &loop LDAk ,&continue JCN ,&done JMP - &continue LDAk emit INC2 ,&loop JMP - &done POP2 nl - dbg BRK + &loop LDAk #00 EQU ,&done JMP + LDAk emit INC2 ,&loop JMP + &done POP2 nl dbg BRK ( open the given file at editor start up ) ( ) @@ -242,43 +244,68 @@ ( ) ( called by ;receive-terminal-size after complete message received ) @parse-terminal-size ( -> ) - LIT2r 0000 LIT2r 0000 + #0000 ,&acc STR2 .tmp/data LDZk #1b NEQ ,&parse-error JCN ( i ) INC LDZk LIT '[ NEQ ,&parse-error JCN ( i ) INC &loop LDZk LIT '; EQU ,&parse-col JCN - LIT2r 000a MUL2r - LDZk LIT '0 SUB #00 SWP STH2 ADD2r - INC ,&loop JMP + LIT2r :&loop ,&read JMP &parse-col - INC STH2r #0002 SUB2 .term/rows STZ2 + INC ,&acc LDR2 #0002 SUB2 .term/rows STZ2 + #0000 ,&acc STR2 &loop2 LDZk LIT 'R EQU ,&done JCN - LIT2r 000a MUL2r - LDZk LIT '0 SUB #00 SWP STH2 ADD2r - INC ,&loop2 JMP + LIT2r :&loop2 ,&read JMP + &read + LDZk LIT '0 SUB #00 SWP + ,&acc LDR2 #000a MUL2 ADD2 ,&acc STR2 + INC JMP2r &done - STH2r .term/cols STZ2 POP + ,&acc LDR2 .term/cols STZ2 POP ;on-key .Console/vector DEO2 ;draw-all JSR2 BRK + [ &acc $2 ] &parse-error POP .tmp/data LDZ2 ;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 ) ( ) ( this method also detects whether \t characters are used, ) ( and uses this to initialize config/insert-tabs. ) @setup-linecount ( -> ) - ;data LIT2r 0001 - &loop DUP2 .buffer/limit LDZ2 EQU2 ,&done JCN - LDAk #00 EQU ,&done JCN - LDAk #09 NEQ ,&next JCN #01 .config/insert-tabs STZ - &next LDAk #0a NEQ JMP INC2r - INC2 ,&loop JMP - &done POP2 - STH2r .buffer/line-count STZ2 - JMP2r + #0a ;count-c JSR2 INC2 .buffer/line-count STZ2 + #09 ;count-c JSR2 #0000 GTH2 .config/insert-tabs STZ + #0d ;count-c JSR2 DUP2 INC2 .buffer/line-count LDZ2 EQU2 STH + #0000 NEQ2 STHkr #00 EQU AND #80 MUL + STHr ADD DUP .config/line-endings STZ + ;strip-cr JCN2 JMP2r + +( strip our carriage returns \r ) +( ) +( 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 ) ( ) @@ -315,6 +342,7 @@ ( move forward by one character ) @forward ( -> ) + ;redraw-statusbar-and-cursor JSR2 ;cur-pos JSR2 ;last-pos JSR2 GTH2 ;return JCN2 ;cur-col JSR2 ;cur-len JSR2 LTH2 ,&normal JCN #0000 .cursor/col STZ2 @@ -323,7 +351,6 @@ ;return JMP2 &normal ;cur-col JSR2 INC2 .cursor/col STZ2 - ;redraw-statusbar-and-cursor JSR2 ;return JMP2 ( move backward by one character ) @@ -692,6 +719,8 @@ ;redraw-prompt-and-cursor JSR2 ;return JMP2 +( TODO: need to add \r back in for windows ) + ( save the file with the filename found in tmp/data ) @do-save ( -> ) .buffer/limit LDZ2 ;data SUB2 STH2 ( [size] ) @@ -772,7 +801,15 @@ ( when opening a file, this defaults to 01 if existing tab ) ( characters are found, and 00 otherwise. ) @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 ( interpret user input as an escaped sequence ) @@ -788,6 +825,7 @@ .state/key LDZ LIT '> EQU ( M-> ) ;goto-end 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 'l EQU ( M-l ) ;toggle-line-endings 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 'v EQU ( M-v ) ;page-up JCN2 @@ -1145,15 +1183,16 @@ ;messages/unsaved ;messages/saved .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-to-statusbar ( -> ) #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-statusbar ( -> ) ;move-to-statusbar JSR2 @@ -1167,7 +1206,8 @@ ;get-save-status JSR2 ;print JSR2 ;filename ;print JSR2 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 sp .buffer/line-count LDZ2 ;emit-dec2 JSR2 @@ -1176,8 +1216,11 @@ ;cur-col JSR2 INC2 ;emit-dec2 JSR2 emit-, .cursor/row LDZ2 INC2 ;emit-dec2 JSR2 - emit-) sp - ;get-tab-status JSR2 ;print JSR2 + emit-) sp emit-[ + 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 @draw-prompt ( -> ) @@ -1544,8 +1587,6 @@ &unsaved "** 20 00 &term-size-parse-error "error 20 "parsing 20 "term 20 "size 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 )