Files
LithosAnanake/v4/capsule/editor.fth
T
rajamesandClaude Opus 5.5 a1afb44598 feat(v4.0.0): the editor, redone in FORTH; COLD WARM PAGE VERSION DEFER
- capsule/editor.fth: the block editor as FORTH source, which the node
  compiles itself.  It is a vocabulary, EDITOR, used at the ordinary
  prompt: n EDIT, then L N B T P E D S H R I WIPE DONE.  v3's EDIT, a
  shell of its own, is not carried over (ruled 2026-10-05); v3's L S and
  SHOW are kept in FORTH as they were, with COPY.
- system.v4: COLD (the system as the loader left it), WARM, PAGE,
  VERSION, 79-STANDARD (FORTH-79: silent), and DEFER IS DEFER@ as v3.
- input.v4: where words are split at blanks a zero byte reads as a blank,
  so a block never written, or filled a line at a time, loads cleanly.
- tests/test_host_quit.c: the editor's source fed to the prompt line by
  line, every command on empty and full screens, what reaches storage;
  the system words; deferred words.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-05 10:25:20 -04:00

69 lines
2.6 KiB
Forth

\ editor.fth -- the block editor, in FORTH.
\
\ This file is FORTH source: the node compiles it itself, a line at a time,
\ as if it were typed. No line is longer than 79 characters.
\
\ A block is a screen of 16 lines of 64 characters. SCR is the screen being
\ edited. The editor is a vocabulary, EDITOR: its words are ordinary FORTH
\ words used at the ordinary prompt, so everything else is there as well.
\
\ n EDIT edit screen n: list it and make EDITOR the CONTEXT vocabulary
\ L list the screen N B the next, the one before
\ n T type line n
\ n P text put the rest of the input line on line n
\ n E erase line n WIPE blank the whole screen
\ n D delete line n; the lines below move up; it is held in PAD
\ n S spread: a blank line at n; the lines from n move down
\ n H hold line n in PAD n R replace line n from PAD
\ n I insert PAD's line at n
\ DONE write what has changed and go back to FORTH
\ a b COPY copy screen a to screen b (in FORTH)
\
\ Every change is UPDATEd; nothing reaches storage until FLUSH or DONE, or
\ until the buffer is wanted for another block.
FORTH DEFINITIONS
\ ( baddr -- ) 64 characters; a zero shows as a blank, others as a dot
: (.LINE)
64 0 DO DUP I + C@
DUP 0= IF DROP 32 THEN DUP 32 < OVER 126 > OR IF DROP 46 THEN EMIT
LOOP DROP ;
\ ( n -- baddr ) where line n of screen SCR is
: (LINE)
DUP 0< OVER 15 > OR ABORT" Line out of range" 64 * SCR @ BLOCK + ;
\ v3's three: a line shown, a line set from a string, the screen shown
: L ( n -- ) (LINE) (.LINE) CR ;
: S ( baddr u n -- )
(LINE) DUP 64 BLANK SWAP 0 MAX 64 MIN CMOVE UPDATE ;
: SHOW ( -- )
." Screen " SCR @ 0 .R ." :" CR
16 0 DO I 2 .R ." : " I (LINE) (.LINE) CR LOOP ;
: COPY ( from to -- ) SWAP BLOCK SWAP BUFFER 1024 CMOVE UPDATE ;
VOCABULARY EDITOR EDITOR DEFINITIONS
: T ( n -- ) DUP (LINE) SWAP 2 .R ." : " (.LINE) CR ;
: L ( -- ) SHOW ;
: E ( n -- ) (LINE) 64 BLANK UPDATE ;
: P ( n -- ) (LINE) DUP 64 BLANK 0 WORD COUNT 64 MIN ROT SWAP CMOVE UPDATE ;
: H ( n -- ) (LINE) PAD 64 CMOVE ;
: R ( n -- ) PAD SWAP (LINE) 64 CMOVE UPDATE ;
: D ( n -- )
DUP H DUP 15 < IF
DUP 1+ (LINE) OVER (LINE) ROT 15 SWAP - 64 * CMOVE
ELSE DROP THEN 15 E ;
: S ( n -- )
DUP 15 < IF DUP (LINE) OVER 1+ (LINE) 3 PICK 15 SWAP - 64 * CMOVE> THEN E ;
: I ( n -- ) DUP S R ;
: WIPE ( -- ) SCR @ BLOCK 1024 BLANK UPDATE ;
: N ( -- ) 1 SCR +! L ;
: B ( -- ) -1 SCR +! L ;
: DONE ( -- ) FLUSH [COMPILE] FORTH ;
FORTH DEFINITIONS
: EDIT ( n -- ) DUP BLOCK DROP SCR ! EDITOR [ EDITOR ] L [ FORTH ] ;