From f386614292b55e1a26d41ccc82f799e667f4a9aa Mon Sep 17 00:00:00 2001 From: d6 Date: Wed, 23 Mar 2022 22:55:03 -0400 Subject: [PATCH] saving works pretty well --- femto.tal | 74 ++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 60 insertions(+), 14 deletions(-) diff --git a/femto.tal b/femto.tal index 97281c1..ed7d705 100644 --- a/femto.tal +++ b/femto.tal @@ -96,6 +96,8 @@ @state [ &saw-esc $1 ( did we just see ESC? ) + &modified $1 ( has the buffer been modified? ) + &quitting $1 ( are we in the process of quitting? ) ] ( prompt uses .tmp/pos and .tmp/data to track user input ) @@ -269,20 +271,18 @@ ;cur-rel-row JSR2 #0001 LTH2 ,&screen-up JCN ;dec-row JSR2 ;draw-statusbar JSR2 - ;draw-cursor JSR2 BRK + &done ;draw-cursor JSR2 BRK &screen-up ;cur-abs-row JSR2 #0001 SUB2 ;jump-to-line JSR2 - ;draw-all JSR2 - &done BRK + ;draw-all JSR2 BRK @down ( -> ) ;cur-abs-row JSR2 ;last-abs-row JSR2 EQU2 ,&done JCN ;inc-row JSR2 ;ensure-visible-cursor JSR2 ;draw-statusbar JSR2 - ;draw-cursor JSR2 BRK - &done BRK + &done ;draw-cursor JSR2 BRK @page-up ( -> ) ;bof-is-visible JSR2 ,&at-bof JCN @@ -315,11 +315,33 @@ ;cur-len JSR2 .cursor/col STZ2 ;draw-cursor JSR2 BRK -@quit quit! +@quit + #01 .state/quitting STZ + .state/modified LDZ ,&is-modified JCN + quit! + &is-modified + ;messages/quit-prompt ;messages/null ;do-quit ;start-prompt JSR2 + ;draw-prompt JSR2 + ;draw-cursor JSR2 + BRK -@ignore BRK +@do-quit + .tmp/data LDZ LIT 'n EQU ;quit-now JCN2 + .tmp/data LDZ LIT 'y EQU ;save JCN2 + #00 .state/quitting STZ + ;move-to-message-line JSR2 + ;messages/unknown-input ;print JSR2 + ;tmp/data ;print JSR2 + BRK + +@quit-now quit! + +@ignore + ;draw-cursor JSR2 + BRK @insert ( c^ -> ) + #01 .state/modified STZ ;cur-pos JSR2 ;shift-right JSR2 ;cur-col JSR2 INC2 .cursor/col STZ2 ;draw-all JSR2 @@ -334,6 +356,7 @@ BRK @insert-tab ( -> ) + #01 .state/modified STZ .config/insert-tabs LDZ ,&use-tabs JCN #0000 .config/tab-width LDZ2 SUB2 &loop @@ -348,6 +371,7 @@ ( TODO: handle last line ) @newline ( c^ -> ) + #01 .state/modified STZ #0a ;cur-pos JSR2 ;shift-right JSR2 #0000 .cursor/col STZ2 ;inc-row JSR2 @@ -371,6 +395,7 @@ GTH2 JMP2r @backspace ( -> ) + #01 .state/modified STZ ;at-buffer-start JSR2 ,&skip JCN ;at-line-start JSR2 ,&prev-line JCN ;cur-col JSR2 #0001 SUB2 .cursor/col STZ2 @@ -394,6 +419,7 @@ BRK @delete ( -> ) + #01 .state/modified STZ ;last-pos JSR2 #0001 SUB2 ( lst-1 ) ;cur-pos JSR2 LTH2 ,&skip JCN ;cur-pos JSR2 LDAk STH ( cur [c] ) @@ -515,8 +541,12 @@ ;move-to-message-line JSR2 .File/success DEI2 STH2r EQU2 ( ok? ) ,&ok JCN ;messages/save-failed ,&finish JMP - &ok ;messages/save-ok - &finish ;print JSR2 ;tmp/data ;print JSR2 BRK + &ok #00 .state/modified STZ ;messages/save-ok + &finish + ;print JSR2 ;tmp/data ;print JSR2 + ;draw-all JSR2 + .state/quitting LDZ ;quit-now JCN2 + BRK @tmp-file-name "file.tmp 00 @@ -531,6 +561,14 @@ .Console/read DEI LIT 'v EQU ( M-v ) ;page-up JCN2 BRK +@clear-line ( -> ) + ansi LIT '2 emit LIT 'K emit JMP2r + +@clear-message-line + ;move-to-message-line JSR2 + ;clear-line JSR2 + JMP2r + @on-key-prompt .Console/read DEI #07 EQU ( C-g ) ;cancel-prompt JCN2 .Console/read DEI #0d EQU ( \r ) ;finish-prompt JCN2 @@ -540,10 +578,11 @@ .Console/read DEI ( printable ASCII ) ;insert-prompt JMP2 BRK -( TODO: C-g or C-h for help ) +( TODO: C-h for help ) ( TODO: C-s for search ) ( TODO: tab input? ) @on-key + ;clear-message-line JSR2 .prompt/active LDZ ;on-key-prompt JCN2 .state/saw-esc LDZ ;on-key-escaped JCN2 .Console/read DEI #01 EQU ( C-a ) ;bol JCN2 @@ -608,6 +647,11 @@ &on-prompt JMP2r +@get-save-status + .state/modified LDZ ,&is-modified JCN + ;messages/saved JMP2r + &is-modified ;messages/unsaved JMP2r + @draw-statusbar ( -> ) #0000 .term/rows LDZ2 ;term-move-cursor JSR2 ;emit-color-reverse JSR2 @@ -618,7 +662,7 @@ &done POP2 POP2 POP2r #0000 .term/rows LDZ2 ;term-move-cursor JSR2 - ;messages/saved ;print JSR2 + ;get-save-status JSR2 ;print JSR2 ;filename ;print JSR2 #20 emit LIT '[ emit @@ -638,8 +682,7 @@ JMP2r @draw-prompt ( -> ) - #0000 .term/rows LDZ2 INC2 ;term-move-cursor JSR2 - ansi LIT '2 emit LIT 'K emit + ;clear-message-line JSR2 .prompt/active LDZ ,&is-active JCN JMP2r &is-active @@ -929,12 +972,15 @@ DEOr JMP2r ( various string constants used as messages for the user ) -@messages [ &input-error "input 20 "error: 20 00 +@messages [ &null 00 + &input-error "input 20 "error: 20 00 &bytes 20 "bytes, 00 &save-ok "Successfully 20 "saved 20 00 &save-failed "Failed 20 "to 20 "save 20 00 &lines 20 "lines] 00 &save-prompt "File 20 "Name 20 "to 20 "Write: 20 00 + &quit-prompt "Save 20 "modified 20 "file 20 "(y/n)? 20 00 + &unknown-input "Unknown 20 "input: 20 00 &saved "-- 20 00 &unsaved "** 20 00 ]