BASIC line editor: cursor keys, Home/End, Delete, and Right to recall a line
Typing a line number and pressing Right brings that program line up to edit. Left/Right move, Home/End (Ctrl-A/Ctrl-E) jump, typing inserts, Backspace and Delete remove, Enter takes the whole line; long lines wrap. To fit the ROM, the date code is smaller and SDCC gets more allocator effort; build.sh now fails if the code runs past 8000h. basic/test-edit.py checks the editor in the emulator. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
073db68754
commit
eb72d85834
8 files changed
+6473
-6227
No files matched your search
+144
-46
@@ -352,24 +352,21 @@ static u8 month_len(u16 y, u8 m) { return mdays[m - 1] + (m == 2 && leap(y)); }
|
||||
/* "YYYY-MM-DD HH:MM" (or with ":SS" when sec is set) for t; "" when t is 0. */
|
||||
static void fmt_date(u32 t, char *out, u8 sec)
|
||||
{
|
||||
u16 y = 2000, days = t / 86400UL;
|
||||
u8 m = 1, i;
|
||||
u32 r = t % 86400UL;
|
||||
u16 v[6];
|
||||
static const char sep[] = "-- ::";
|
||||
u16 y = 2000, days, mins;
|
||||
u8 m = 1, i, v[6];
|
||||
if (!t) { *out = 0; return; }
|
||||
days = t / 86400UL;
|
||||
mins = (u16)(t % 86400UL / 60);
|
||||
while (days >= 365 + leap(y)) days -= 365 + leap(y++);
|
||||
while (days >= month_len(y, m)) days -= month_len(y, m++);
|
||||
v[0] = y; v[1] = m; v[2] = days + 1;
|
||||
v[3] = r / 3600; v[4] = r / 60 % 60; v[5] = r % 60;
|
||||
for (i = 0; i < (sec ? 6 : 5); i++) {
|
||||
if (i == 0) {
|
||||
*out++ = '0' + y / 1000; *out++ = '0' + y / 100 % 10;
|
||||
}
|
||||
*out++ = '0' + v[i] / 10 % 10;
|
||||
v[0] = y - 2000; v[1] = m; v[2] = days + 1;
|
||||
v[3] = mins / 60; v[4] = mins % 60; v[5] = t % 60;
|
||||
*out++ = '2'; *out++ = '0';
|
||||
for (i = 0; i < 5 + sec; i++) {
|
||||
if (i) *out++ = sep[i - 1];
|
||||
*out++ = '0' + v[i] / 10;
|
||||
*out++ = '0' + v[i] % 10;
|
||||
if (i < 2) *out++ = '-';
|
||||
else if (i == 2) *out++ = ' ';
|
||||
else if (i < (sec ? 5 : 4)) *out++ = ':';
|
||||
}
|
||||
*out = 0;
|
||||
}
|
||||
@@ -388,29 +385,26 @@ static u16 date_field(const char **p, u8 n, u16 lo, u16 hi)
|
||||
/* Set the clock from "YYYY-MM-DD[ HH:MM[:SS]]". */
|
||||
static void set_date(const char *s)
|
||||
{
|
||||
u16 y, m, d, hh = 0, mm = 0, ss = 0, i;
|
||||
static const char sep[] = "-- ::";
|
||||
static const u8 hi[] = { 99, 12, 31, 23, 59, 59 };
|
||||
u8 v[6], i;
|
||||
u32 days;
|
||||
y = date_field(&s, 4, 2000, 2099);
|
||||
if (*s++ != '-') error(E_FUNC);
|
||||
m = date_field(&s, 2, 1, 12);
|
||||
if (*s++ != '-') error(E_FUNC);
|
||||
d = date_field(&s, 2, 1, month_len(y, m));
|
||||
if (*s) {
|
||||
if (*s++ != ' ') error(E_FUNC);
|
||||
hh = date_field(&s, 2, 0, 23);
|
||||
if (*s++ != ':') error(E_FUNC);
|
||||
mm = date_field(&s, 2, 0, 59);
|
||||
if (*s) {
|
||||
if (*s++ != ':') error(E_FUNC);
|
||||
ss = date_field(&s, 2, 0, 59);
|
||||
memset(v, 0, sizeof v);
|
||||
if (*s++ != '2' || *s++ != '0') error(E_FUNC);
|
||||
for (i = 0; i < 6; i++) {
|
||||
if (i) {
|
||||
if (!*s && (i == 3 || i == 5)) break;
|
||||
if (*s++ != sep[i - 1]) error(E_FUNC);
|
||||
}
|
||||
if (*s) error(E_FUNC);
|
||||
v[i] = date_field(&s, 2, i == 1 || i == 2,
|
||||
i == 2 ? month_len(2000 + v[0], v[1]) : hi[i]);
|
||||
}
|
||||
days = d - 1;
|
||||
for (i = 2000; i < y; i++) days += 365 + leap(i);
|
||||
for (i = 1; i < m; i++) days += month_len(y, i);
|
||||
if (*s) error(E_FUNC);
|
||||
days = v[2] - 1;
|
||||
for (i = 0; i < v[0]; i++) days += 365 + leap(2000 + i);
|
||||
for (i = 1; i < v[1]; i++) days += month_len(2000 + v[0], i);
|
||||
clk_ms = millis();
|
||||
clk_sec = days * 86400UL + hh * 3600UL + mm * 60 + ss;
|
||||
clk_sec = days * 86400UL + (u16)(v[3] * 60 + v[4]) * 60UL + v[5];
|
||||
if (!clk_sec) clk_sec = 1;
|
||||
}
|
||||
|
||||
@@ -1042,27 +1036,131 @@ static void check_break(void)
|
||||
if (c >= 0) held = c;
|
||||
}
|
||||
|
||||
/* Read a line with echo and simple editing; returns 0 on Ctrl-C. */
|
||||
/* ------------------------------------------------------------ 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. */
|
||||
#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 void ed_csi(u8 n, char f)
|
||||
{
|
||||
out_c(27); out_c('['); esc_num(n); out_c(f);
|
||||
}
|
||||
|
||||
/* Put the terminal's cursor at place i of the line. */
|
||||
static void ed_go(u8 i)
|
||||
{
|
||||
u16 p = ed_sc + i;
|
||||
u8 r = p / ED_W, c = p % ED_W;
|
||||
if (i == ed_at && !ed_pend) return;
|
||||
out_c('\r');
|
||||
if (r < ed_row) ed_csi(ed_row - r, 'A');
|
||||
for (; ed_row < r; ed_row++) out_c('\n');
|
||||
if (c) ed_csi(c, 'C');
|
||||
ed_row = r; ed_at = i; ed_pend = 0;
|
||||
}
|
||||
|
||||
/* Write the line from place i on, blank `gone` cells after it, then put the
|
||||
cursor back. */
|
||||
static void ed_show(u8 i, u8 gone)
|
||||
{
|
||||
u8 k = i;
|
||||
ed_go(i);
|
||||
for (; k < ed_n; k++) out_c(linebuf[k]);
|
||||
for (; gone; gone--, k++) out_c(' ');
|
||||
if (k > i) {
|
||||
ed_row = (ed_sc + k - 1) / ED_W;
|
||||
ed_at = k;
|
||||
ed_pend = (ed_sc + k) % ED_W == 0;
|
||||
}
|
||||
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)
|
||||
{
|
||||
u8 i = 0;
|
||||
u16 v = 0;
|
||||
u8 *p;
|
||||
while (i < ed_n && linebuf[i] == ' ') i++;
|
||||
if (i == ed_n) return 0;
|
||||
for (; i < ed_n; i++) {
|
||||
if (!is_digit(linebuf[i])) return 0;
|
||||
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);
|
||||
ed_cur = ed_n;
|
||||
ed_show(0, ed_n < i ? i - ed_n : 0);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Read a line with echo and editing; returns 0 on Ctrl-C. */
|
||||
static u8 read_line(void)
|
||||
{
|
||||
u8 n = 0, c;
|
||||
u8 c, k;
|
||||
ed_sc = col % 80;
|
||||
ed_row = ed_at = ed_pend = ed_n = ed_cur = 0;
|
||||
for (;;) {
|
||||
c = getkey();
|
||||
if (c == '\r' || c == '\n') { newline(); linebuf[n] = 0; return 1; }
|
||||
if (c == 3) { puts_("^C"); newline(); return 0; }
|
||||
if (c == 27) { /* drop cursor-key escape sequences */
|
||||
if (c == '\r' || c == '\n') {
|
||||
ed_cur = ed_n; ed_go(ed_n);
|
||||
newline(); linebuf[ed_n] = 0; 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 */
|
||||
c = getkey();
|
||||
k = 0;
|
||||
if (c == '[' || c == 'O')
|
||||
do c = getkey(); while (c < 0x40 || c > 0x7E);
|
||||
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 == 8 || c == 127) {
|
||||
if (n) { n--; putch('\b'); putch(' '); putch('\b'); }
|
||||
} else if (c == 21) { /* Ctrl-U */
|
||||
while (n) { n--; putch('\b'); putch(' '); putch('\b'); }
|
||||
} else if (c >= ' ' && c < 127 && n < 250) {
|
||||
linebuf[n++] = c;
|
||||
putch(c);
|
||||
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 */
|
||||
k = ed_n;
|
||||
ed_n = ed_cur = 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user