retroforth/wip/edit.forth
crc e3ed438106 begin work on a text editor
FossilOrigin-Name: 256ad9238d592521d51a8470fa1c6fa829be517fb0732c5c0dc85c80a00b0d3f
2017-11-12 04:28:24 +00:00

65 lines
1.5 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
#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 n:dec #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 ;
:handler
getc
$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
~~~