commit aa1704927fffa465a29f3924cdec8e5192547a54
parent 1732447f7ef6f92426631bdddc12f6029436e54a
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Tue, 17 Jan 2023 20:52:02 -0500
text/ed: implement "edsave"
Diffstat:
3 files changed, 28 insertions(+), 5 deletions(-)
diff --git a/fs/fs/blob.fs b/fs/fs/blob.fs
@@ -33,7 +33,7 @@ extends File struct[ BlobFile
: :new ( fs -- hdl )
here
0 ( putback ) , ['] _readbuf , ['] _writebuf , ['] _flush , ['] drop ,
- 0 ( pos ) , -1 ( size ) , ['] _seek , ['] abort ( truncate ) ,
+ 0 ( pos ) , -1 ( size ) , ['] _seek , ['] drop ( truncate ) ,
swap ( fs ) , -1 ( sec ) , 0 ( dirty? ) , dup :bufsz allot ;
]struct
diff --git a/fs/tests/text/ed.fs b/fs/tests/text/ed.fs
@@ -1,4 +1,6 @@
?f<< /tests/harness.fs
+?f<< /drv/ramdrive.fs
+?f<< /fs/blob.fs
?f<< /text/ed.fs
testbegin
\ Ed tests
@@ -26,5 +28,14 @@ capture O inserted line
S" inserted line\n ^\n4 / 17\n" #s=
0 g capture O at the beginning of the buf
S" at the beginning of the buf\n ^\n0 / 18\n" #s=
-p
+
+\ Now let's save this to a BlobFile
+2 const TOTSEC
+512 TOTSEC RAMDrive :new value mydrv
+mydrv BlobFS :mount value myfs
+0 myfs Filesystem :open >file
+edsave
+0 file :seek
+file :readline S" at the beginning of the buf" #s=
+file$
testend
diff --git a/fs/text/ed.fs b/fs/text/ed.fs
@@ -18,8 +18,6 @@ extends IO struct[ Edbuf
sfield lpos
sfield cpos
- : _readbuf ( a self -- a? read-n ) abort" TODO" ;
-
: _newline ( self -- line )
Line SZ swap buf Arena :allot dup Line SZ 0 fill ;
@@ -34,6 +32,17 @@ extends IO struct[ Edbuf
else nip dup lines then ( self line )
swap _sel! ;
+ : _eol? ( self -- f ) dup cpos swap sel Line cnt = ;
+ : _eof? ( self -- f ) dup sel llnext not swap _eol? and ;
+ create _lf LF c,
+ : _readbuf ( n self -- a? read-n ) >r ( n ) \ V1=self
+ r@ _eof? if rdrop drop 0 exit then
+ r@ _eol? if
+ drop 1 r@ :godown 0 r> to cpos _lf 1 exit then
+ r@ sel Line cnt r@ cpos - ( n1 n2 )
+ r@ sel Line ptr r@ cpos + ( n1 n2 a )
+ rot> min dup r> to+ cpos ;
+
: :appendline ( self -- )
dup _newline over sel llinsert 1 swap :godown ;
@@ -114,7 +123,6 @@ Edbuf :new structbind Edbuf edbuf
0 file :seek edbuf :empty
edbuf :self file :spit
edbuf lines to edbuf sel ;
-
: s ( -- ) edbuf :cprint edbuf :linecnt edbuf lpos .f" %d / %d\n" ;
: ?s print? if s then ;
@@ -131,3 +139,7 @@ Edbuf :new structbind Edbuf edbuf
: i console :readline edbuf :puts ?s ;
: o edbuf :appendline i ;
: O edbuf :insertline i ;
+: edsave ( -- )
+ 0 g H 0 file :seek
+ file :self edbuf :spit
+ file :truncate file :flush 0 file :seek ;