retroforth/wip/edit.forth
crc 6f3044c43e editor now has a replace line command. basic editing is now functional.
FossilOrigin-Name: 04bac77371cfe463998332e48764778352cd96c0b31fbf0a51f98186594c99ef
2017-11-12 04:44:52 +00:00

84 lines
2.3 KiB
Forth
Executable file

#!/usr/bin/env rre
This will (hopefully) be a functional text editor written in RETRO.
Interface:
<filename> : <line-count> : <current-line>
---------------------------------------------------------------
* 99:
100: :n:square dup * ;
101:
102: This is the current line
103:
---------------------------------------------------------------
As with my old block editor, this will be primarily line oriented.
And it's visual, with some simple key bindings.
~~~
'SourceFile var
'FID var
'FOUT var
#0 sys:argv s:keep !SourceFile
:count-lines #0 @SourceFile [ drop n:inc ] file:for-each-line ;
#12 'MAX-LINES const
'CurrentLine var
:pad (n-n)
dup #0 #9 n:between? [ '___ puts ] if
dup #10 #99 n:between? [ '__ puts ] if
dup #100 #999 n:between? [ '_ puts ] if ;
:current (n-n)
dup @CurrentLine eq? [ '*_ puts ] [ '__ puts ] choose ;
:skip-to
@CurrentLine #0 n:max [ @FID file:read-line drop ] times ;
:display
@SourceFile file:R file:open !FID
skip-to
@CurrentLine count-lines MAX-LINES n:min [ dup current pad putn n:inc ':_ puts @FID file:read-line puts nl ] times drop
@FID file:close ;
:delete-line
'/tmp/rre.edit file:W file:open !FID
#0 @SourceFile [ over @CurrentLine eq? [ drop ] [ [ @FID file:write ] s:for-each ] choose ASCII:LF @FID file:write n:inc ] file:for-each-line drop
@FID file:close
@SourceFile 'mv_/tmp/rre.edit_%s s:with-format unix:system ;
:gets
s:empty [ buffer:set [ repeat getc dup ASCII:LF -eq? 0; drop buffer:add again ] call drop ] sip ;
:replace-line
'/tmp/rre.edit file:W file:open !FID
#0 @SourceFile [ over @CurrentLine eq? [ drop gets ] if [ @FID file:write ] s:for-each ASCII:LF @FID file:write n:inc ] file:for-each-line drop
@FID file:close
@SourceFile 'mv_/tmp/rre.edit_%s s:with-format unix:system ;
:handler
getc
$i [ replace-line ] case
$d [ delete-line ] case
$j [ &CurrentLine v:inc ] case
$k [ &CurrentLine v:dec ] case
$q [ 'stty_-cbreak unix:system #0 unix:exit ] case
drop ;
:loop
'stty_cbreak unix:system
repeat
ASCII:ESC '%c[2J s:with-format puts nl
@CurrentLine count-lines @SourceFile '%s_:_%n_:_%n\n s:with-format puts
display
#72 [ $- putc ] times nl
handler
again ;
loop
~~~