BASIC editor: Right copies one character, End the rest, Up recalls

After a bare line number, Right copies that program line one character at
a time and End copies the rest; Delete skips a character of it. Up brings
back the last line entered, cursor at the end. The last line is kept in a
256-byte buffer taken from the end of the program arena, so programs still
start at 8482h and have about 20.3 KB.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
brad ArantandClaude Opus 5.5 committed 2026-11-30 08:12:59 -08:00
1 parent eb72d85834
commit b629cbec00
7 files changed
+2033 -1978

No files matched your search

BIN
View File
Binary file not shown.
+88 -53
View File
@@ -221,7 +221,7 @@ static const char *const kw[] = {
};
/* ----------------------------------------------------------------- memory */
#define ARENA 21076 /* program text grows up, arrays grow down;
#define ARENA 20820 /* program text grows up, arrays grow down;
F000-F3F7 is left free for machine code */
#define STRMAX 80
#define NVARS (26 * 11)
@@ -229,6 +229,7 @@ static const char *const kw[] = {
#define GOSUBMAX 32
static u8 arena[ARENA];
static char ed_last[256]; /* the last line typed, for Up */
static u8 *prog_end; /* end-of-program marker (a zero length byte) */
static u8 *arr_low; /* lowest byte used by arrays */
static float vars[NVARS];
@@ -1039,14 +1040,18 @@ static void check_break(void)
/* ------------------------------------------------------------ line editor
The line being typed is linebuf[0..ed_n) with the cursor at ed_cur. Left
and Right move, Home and End (or Ctrl-A, Ctrl-E) jump, typing inserts,
Backspace and Delete take out, Ctrl-U clears. Right at the end of a line
that is only a line number brings that program line in to edit. The screen
is kept up to date with cursor moves only, so a line may wrap: ed_sc is the
column it starts in, ED_W the screen width, ed_row the screen row (from the
line's first) the terminal's cursor is on and ed_at the place it is at. */
Backspace and Delete take out, Ctrl-U clears, Up brings back the last line.
At the end of a line that is only a line number, Right copies that program
line one character at a time and End copies the rest of it; Delete there
skips a character of it. The screen is kept up to date with cursor moves
only, so a line may wrap: ed_sc is the column it starts in, ED_W the
screen width, ed_row the screen row (from the line's first) the
terminal's cursor is on and ed_at the place it is at. */
#define ED_W 80
static void esc_num(u8 n);
static u8 ed_sc, ed_row, ed_at, ed_pend, ed_n, ed_cur;
static u8 *ed_src; /* the program line being copied, or 0 */
static const char *ed_kw; /* the keyword being copied out of it */
static void ed_csi(u8 n, char f)
{
@@ -1082,45 +1087,69 @@ static void ed_show(u8 i, u8 gone)
ed_go(ed_cur);
}
static void ed_put(char c)
{
if (ed_n < 250) linebuf[ed_n++] = c;
}
/* Right at the end of a bare line number: bring that line in. */
static u8 ed_recall(void)
/* Start copying the program line whose number is all that has been typed. */
static void ed_start(void)
{
u8 i = 0;
u16 v = 0;
u8 *p;
if (ed_src || ed_cur < ed_n) return;
while (i < ed_n && linebuf[i] == ' ') i++;
if (i == ed_n) return 0;
if (i == ed_n) return;
for (; i < ed_n; i++) {
if (!is_digit(linebuf[i])) return 0;
if (!is_digit(linebuf[i])) return;
v = v * 10 + linebuf[i] - '0';
}
p = find_line(v);
if (!*p || line_no(p) != v) return 0;
i = ed_n;
ed_n = 0;
list_to(p, ed_put);
if (*p && line_no(p) == v) { ed_src = p + 3; ed_kw = " "; }
}
/* The next character of the line being copied, or 0. */
static char ed_next(void)
{
u8 c;
if (!ed_src) return 0;
if (*ed_kw) return *ed_kw++;
c = *ed_src;
if (!c) return 0;
ed_src++;
if (c >= T_LET && c < T_LAST) { ed_kw = kw[c - T_LET]; return *ed_kw++; }
return c;
}
/* Copy up to n characters of it to the end of the line. */
static void ed_copy(u8 n)
{
u8 i = ed_n;
char c;
ed_start();
for (; n && ed_n < 250 && (c = ed_next()); n--) linebuf[ed_n++] = c;
ed_cur = ed_n;
ed_show(0, ed_n < i ? i - ed_n : 0);
return 1;
ed_show(i, 0);
}
/* Take out the character at the cursor. */
static void ed_del(void)
{
memmove(linebuf + ed_cur, linebuf + ed_cur + 1, ed_n - ed_cur - 1);
ed_n--;
ed_show(ed_cur, 1);
}
/* Read a line with echo and editing; returns 0 on Ctrl-C. */
static u8 read_line(void)
{
u8 c, k;
ed_sc = col % 80;
ed_sc = col % ED_W;
ed_row = ed_at = ed_pend = ed_n = ed_cur = 0;
ed_src = 0;
for (;;) {
c = getkey();
if (c == '\r' || c == '\n') {
ed_cur = ed_n; ed_go(ed_n);
newline(); linebuf[ed_n] = 0; return 1;
newline(); linebuf[ed_n] = 0;
if (ed_n) strcpy(ed_last, linebuf);
return 1;
}
if (c == 3) { ed_cur = ed_n; ed_go(ed_n); puts_("^C"); newline(); return 0; }
if (c == 27) { /* cursor and editing keys */
@@ -1129,39 +1158,45 @@ static u8 read_line(void)
if (c == '[' || c == 'O')
do { c = getkey(); if (is_digit(c)) k = c; } while (c < 0x40 || c > 0x7E);
if (c == '~') c = k == '1' ? 'H' : k == '4' ? 'F' : k == '3' ? 'P' : 0;
if (c == 'C') {
if (ed_cur < ed_n) ed_cur++;
else ed_recall();
} else if (c == 'D') { if (ed_cur) ed_cur--; }
else if (c == 'H') ed_cur = 0;
else if (c == 'F') ed_cur = ed_n;
else if (c == 'P' && ed_cur < ed_n) {
memmove(linebuf + ed_cur, linebuf + ed_cur + 1, ed_n - ed_cur - 1);
ed_n--;
ed_show(ed_cur, 1);
}
ed_go(ed_cur);
continue;
}
if (c == 1) { ed_cur = 0; ed_go(0); }
else if (c == 5) { ed_cur = ed_n; ed_go(ed_n); }
else if (c == 8 || c == 127) {
if (ed_cur) {
ed_cur--;
memmove(linebuf + ed_cur, linebuf + ed_cur + 1, ed_n - ed_cur - 1);
ed_n--;
ed_show(ed_cur, 1);
}
} else if (c == 21) { /* Ctrl-U */
} else if (c == 1) c = 'H';
else if (c == 5) c = 'F';
else if (c == 21) { /* Ctrl-U */
k = ed_n;
ed_n = ed_cur = 0;
ed_src = 0;
ed_show(0, k);
} else if (c >= ' ' && c < 127 && ed_n < 250) {
memmove(linebuf + ed_cur + 1, linebuf + ed_cur, ed_n - ed_cur);
linebuf[ed_cur++] = c;
ed_n++;
ed_show(ed_cur - 1, 0);
continue;
} else if (c == 8 || c == 127) {
if (ed_cur) { ed_cur--; ed_del(); }
continue;
} else {
if (c >= ' ' && c < 127 && ed_n < 250) {
memmove(linebuf + ed_cur + 1, linebuf + ed_cur, ed_n - ed_cur);
linebuf[ed_cur++] = c;
ed_n++;
ed_show(ed_cur - 1, 0);
}
continue;
}
if (c == 'A') { /* Up: the last line again */
k = ed_n;
strcpy(linebuf, ed_last);
ed_n = ed_cur = strlen(linebuf);
ed_src = 0;
ed_show(0, k > ed_n ? k - ed_n : 0);
} else if (c == 'C') {
if (ed_cur < ed_n) ed_cur++;
else ed_copy(1);
} else if (c == 'D') { if (ed_cur) ed_cur--; }
else if (c == 'H') ed_cur = 0;
else if (c == 'F') {
if (ed_cur < ed_n) ed_cur = ed_n;
else ed_copy(250);
} else if (c == 'P') {
if (ed_cur < ed_n) ed_del();
else { ed_start(); ed_next(); }
}
ed_go(ed_cur);
}
}
+1906 -1906
View File
File diff suppressed because it is too large. Load diff
+22 -8
View File
@@ -14,7 +14,8 @@ HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(HERE, "..", "tools"))
from boardemu import Board # noqa: E402
RIGHT, LEFT, HOME, END, DEL = b"\x1b[C", b"\x1b[D", b"\x1b[1~", b"\x1b[4~", b"\x1b[3~"
RIGHT, LEFT, UP = b"\x1b[C", b"\x1b[D", b"\x1b[A"
HOME, END, DEL = b"\x1b[1~", b"\x1b[4~", b"\x1b[3~"
BS, CTRL_U, CR = b"\x7f", b"\x15", b"\r"
@@ -69,9 +70,15 @@ def main():
b.type(b"")
b.type(b'10 PRINT "HELLO"' + CR + b"20 GOTO 10" + CR)
b.type(b"PRINT PEEK(33923)" + CR) # line 10's number, low byte
check("the program still starts at 8482h", lines().rstrip().endswith(" 10\n\nREADY"))
y = cur()[0]
b.type(b"10" + RIGHT)
check("right after 10 brings line 10 in", row(y) == '10 PRINT "HELLO"' and cur() == (y, 16))
check("right after 10 copies one character of line 10", row(y) == "10" and cur() == (y, 3))
b.type(RIGHT * 2)
check("each right copies one more", row(y) == "10 PR" and cur() == (y, 5))
b.type(END)
check("End copies the rest", row(y) == '10 PRINT "HELLO"' and cur() == (y, 16))
b.type(LEFT * 6) # cursor onto the H
check("left moves the cursor back", cur() == (y, 10))
b.type(b"BIG ")
@@ -84,20 +91,24 @@ def main():
lst = lines()
check("the edited line is stored", '10 PRINT "BIG HELLO!"\n20 GOTO 10' in lst)
b.type(b"99" + RIGHT)
b.type(UP)
y = cur()[0]
check("right after a missing line number does nothing", row(y) == "99" and cur() == (y, 2))
check("up brings back the last line, cursor at the end", row(y) == "LIST" and cur() == (y, 4))
b.type(CTRL_U)
check("Ctrl-U clears the line", row(y) == "" and cur() == (y, 0))
b.type(b"20" + RIGHT + HOME + DEL * 2 + b"30" + CR)
b.type(b"99" + RIGHT)
check("right after a missing line number does nothing", row(y) == "99" and cur() == (y, 2))
b.type(CTRL_U + b"20" + RIGHT * 6 + DEL * 2 + b"30" + CR)
b.type(b"LIST" + CR)
lst = lines()
check("a recalled line can be stored as a copy", "20 GOTO 10\n30 GOTO 10" in lst)
lst = lines().split("LIST")[-1]
check("Delete skips old characters while copying", "20 GOTO 30" in lst and "20 GOTO 10" not in lst)
b.type(b"20" + END + HOME + DEL * 2 + b"40" + CR + b"LIST" + CR)
check("a copied line can be stored under a new number", "20 GOTO 30\n40 GOTO 30" in lines())
# a line that wraps onto three screen rows
text = "REM " + "".join(chr(65 + i % 26) for i in range(170))
b.type(b"40 " + text.encode() + CR)
b.type(b"40" + RIGHT)
b.type(b"40" + END)
y, x = cur()
full = "40 " + text
y0 = y - (len(full) // 80)
@@ -113,6 +124,9 @@ def main():
check("inserting and deleting across rows redraws them", shown.rstrip() == full and cur() == (y0, 79))
b.type(CR + b"LIST 40" + CR)
check("the long line is stored", full in "".join(screen().display))
b.type(UP + UP)
check("up again gives the same last line", lines().rstrip().endswith("LIST 40"))
b.type(CTRL_U)
b.type(b"NEW" + CR + b'INPUT A$: PRINT "GOT ";A$' + CR)
b.type(b"ABD" + LEFT + b"C" + CR)