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

+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);
}
}