Artifact b7fff2b1677ade5cce4822c3c0fc07d617e3de657c815fff54029685754b073c:
- File
src/tui/editor.c
— part of check-in
[e6887db767]
at
2026-11-19 22:44:19
on branch tui-parity-2
— TUI: closer to Pi, round 2
!cmd runs a command and adds its output to the next message; !!cmd runs it without. Editor: a kill ring (Ctrl+K, Ctrl+U, Ctrl+W, Alt+D into it; Ctrl+Y, Alt+Y out), undo (Ctrl+-), Ctrl+] to jump to a character, Ctrl+G to edit in $VISUAL or $EDITOR, Ctrl+Z to suspend. Markdown: tables, ordered and nested lists, quotes, italics, strikethrough, links (OSC 8 where the terminal draws them), code-fence labels; user messages on a background. Alt+Up restores queued messages. Startup shows Pi's key hints and the loaded context files, skills and prompt templates, and sets the terminal title. Double Esc opens /tree, Shift+Ctrl+P cycles models backwards, Ctrl+X copies the last reply. (user: dmestas, size: 21331) [annotate] [blame] [check-ins using] [more...]
/* The message editor; see editor.h. * * The text is one byte buffer with "\n" between lines and a byte-offset * cursor. A collapsed paste is stored in the text as its marker, and in a * table as marker -> original text; take expands markers that match the * table exactly. Paste numbering restarts after each submit/clear. */ #include "tui/editor.h" #include <stdio.h> #include <stdlib.h> #include <string.h> #define HIST_MAX 200 #define PASTE_LINES 10 #define PASTE_CHARS 1000 /* Zero-width sentinel (variation selector U+E01EF) placed at the cursor so * the wrapper tells us where it landed. */ #define CURSOR_MARK "\xF3\xA0\x87\xAF" typedef struct { char *marker, *content; size_t marker_len; } paste; typedef struct { char *text; size_t cur; } snap; /* What the last key did, for kill accumulation, yank-pop and typing undo units. */ enum { ACT_NONE, ACT_KILL, ACT_YANK, ACT_TYPE }; #define UNDO_MAX 200 #define RING_MAX 60 struct tui_editor { char *buf; size_t len, cap, cur; char **hist; size_t nhist; size_t hpos; /* nhist: editing the unsent text */ char *saved; /* the unsent text while browsing history */ paste *pastes; size_t npaste; int next_paste; long want_col; /* column kept across UP/DOWN; -1: none */ char **ring; /* kill ring, newest last */ size_t nring; int last; /* what the previous key did: ACT_* */ int jump; /* waiting for the character to jump to: 1 forward, -1 backward, 0 none */ snap *undo; /* undo snapshots, newest last */ size_t nundo; }; tui_editor *tui_editor_new(void) { tui_editor *e = calloc(1, sizeof *e); if (e) e->next_paste = 1, e->want_col = -1; return e; } static void pastes_clear(tui_editor *e) { for (size_t i = 0; i < e->npaste; i++) { free(e->pastes[i].marker); free(e->pastes[i].content); } free(e->pastes); e->pastes = NULL; e->npaste = 0; e->next_paste = 1; } static void undo_clear(tui_editor *e) { for (size_t i = 0; i < e->nundo; i++) free(e->undo[i].text); free(e->undo); e->undo = NULL; e->nundo = 0; } void tui_editor_free(tui_editor *e) { if (!e) return; for (size_t i = 0; i < e->nhist; i++) free(e->hist[i]); free(e->hist); free(e->saved); pastes_clear(e); undo_clear(e); for (size_t i = 0; i < e->nring; i++) free(e->ring[i]); free(e->ring); free(e->buf); free(e); } /* ---- buffer primitives ------------------------------------------------- */ static int ins(tui_editor *e, size_t pos, const char *s, size_t n) { if (e->len + n + 1 > e->cap) { size_t cap = e->cap ? e->cap : 128; while (cap < e->len + n + 1) cap *= 2; char *p = realloc(e->buf, cap); if (!p) return -1; e->buf = p, e->cap = cap; } memmove(e->buf + pos + n, e->buf + pos, e->len - pos); memcpy(e->buf + pos, s, n); e->len += n; e->buf[e->len] = 0; return 0; } static void del(tui_editor *e, size_t from, size_t to) { memmove(e->buf + from, e->buf + to, e->len - to); e->len -= to - from; e->buf[e->len] = 0; if (e->cur > to) e->cur -= to - from; else if (e->cur > from) e->cur = from; } static size_t prev_char(const tui_editor *e, size_t pos) { if (pos == 0) return 0; pos--; while (pos > 0 && ((unsigned char)e->buf[pos] & 0xC0) == 0x80) pos--; return pos; } static size_t next_char(const tui_editor *e, size_t pos) { if (pos >= e->len) return e->len; uint32_t cp; return pos + tui_utf8_decode(e->buf + pos, e->len - pos, &cp); } static size_t line_start(const tui_editor *e, size_t pos) { while (pos > 0 && e->buf[pos - 1] != '\n') pos--; return pos; } static size_t line_end(const tui_editor *e, size_t pos) { while (pos < e->len && e->buf[pos] != '\n') pos++; return pos; } static int is_space(char c) { return c == ' ' || c == '\t' || c == '\n'; } static size_t word_left(const tui_editor *e, size_t pos) { while (pos > 0 && is_space(e->buf[pos - 1])) pos--; while (pos > 0 && !is_space(e->buf[pos - 1])) pos--; return pos; } static size_t word_right(const tui_editor *e, size_t pos) { while (pos < e->len && is_space(e->buf[pos])) pos++; while (pos < e->len && !is_space(e->buf[pos])) pos++; return pos; } /* ---- paste markers ----------------------------------------------------- */ /* Length of the known paste marker that starts at pos (0: none). */ static size_t marker_at(const tui_editor *e, size_t pos, const paste **found) { if (pos >= e->len || e->buf[pos] != '[') return 0; for (size_t i = 0; i < e->npaste; i++) { const paste *p = &e->pastes[i]; if (pos + p->marker_len <= e->len && memcmp(e->buf + pos, p->marker, p->marker_len) == 0) { if (found) *found = p; return p->marker_len; } } return 0; } /* Start of a known marker ending exactly at pos, or pos itself. */ static size_t marker_before(const tui_editor *e, size_t pos) { if (pos == 0 || e->buf[pos - 1] != ']') return pos; for (size_t i = 0; i < e->npaste; i++) { size_t m = e->pastes[i].marker_len; if (m <= pos && marker_at(e, pos - m, NULL) == m) return pos - m; } return pos; } /* A cursor strictly inside a known marker moves out of it: dir < 0 to its * start, dir > 0 to its end, 0 to the nearer side. */ static void snap_out(tui_editor *e, int dir) { size_t lo = e->cur > 64 ? e->cur - 64 : 0; for (size_t s = e->cur; s-- > lo;) { size_t m = marker_at(e, s, NULL); if (m && s < e->cur && e->cur < s + m) { if (dir < 0 || (dir == 0 && e->cur - s <= m / 2)) e->cur = s; else e->cur = s + m; return; } } } static int insert_paste(tui_editor *e, const char *s, size_t n) { size_t lines = 1; for (size_t i = 0; i < n; i++) lines += s[i] == '\n'; if (lines <= PASTE_LINES && n <= PASTE_CHARS) return ins(e, e->cur, s, n) == 0 ? (e->cur += n, 0) : -1; char marker[64]; int id = e->next_paste; int ml = lines > PASTE_LINES ? snprintf(marker, sizeof marker, "[paste #%d +%zu lines]", id, lines) : snprintf(marker, sizeof marker, "[paste #%d %zu chars]", id, n); paste *np = realloc(e->pastes, (e->npaste + 1) * sizeof *np); if (!np) return -1; e->pastes = np; paste *p = &np[e->npaste]; p->marker = malloc((size_t)ml + 1); p->content = malloc(n + 1); if (!p->marker || !p->content) { free(p->marker); free(p->content); return -1; } memcpy(p->marker, marker, (size_t)ml + 1); p->marker_len = (size_t)ml; memcpy(p->content, s, n); p->content[n] = 0; if (ins(e, e->cur, marker, (size_t)ml) != 0) { free(p->marker); free(p->content); return -1; } e->npaste++; e->next_paste++; e->cur += (size_t)ml; return 0; } /* ---- text replacement and history -------------------------------------- */ static void set_text(tui_editor *e, const char *text) { size_t n = strlen(text); e->len = 0; if (e->buf) e->buf[0] = 0; if (ins(e, 0, text, n) == 0) e->cur = n; else e->cur = 0; } void tui_editor_set(tui_editor *e, const char *text) { pastes_clear(e); undo_clear(e); e->last = ACT_NONE; e->jump = 0; set_text(e, text); e->hpos = e->nhist; e->want_col = -1; } void tui_editor_clear(tui_editor *e) { tui_editor_set(e, ""); free(e->saved); e->saved = NULL; } int tui_editor_empty(const tui_editor *e) { return e->len == 0; } static void history_add(tui_editor *e, const char *text) { if (e->nhist > 0 && strcmp(e->hist[e->nhist - 1], text) == 0) return; char *c = strdup(text); if (!c) return; if (e->nhist == HIST_MAX) { free(e->hist[0]); memmove(e->hist, e->hist + 1, (HIST_MAX - 1) * sizeof *e->hist); e->nhist--; } else if (!e->hist || e->nhist % 32 == 0) { char **h = realloc(e->hist, (e->nhist + 32) * sizeof *h); if (!h) { free(c); return; } e->hist = h; } e->hist[e->nhist++] = c; } void tui_editor_history_add(tui_editor *e, const char *text) { history_add(e, text); e->hpos = e->nhist; } char *tui_editor_expanded(const tui_editor *e) { char *out = malloc(e->len + 1); size_t cap = e->len + 1, n = 0; if (!out) return NULL; for (size_t i = 0; i < e->len;) { const paste *p = NULL; size_t m = marker_at(e, i, &p); const char *src = m ? p->content : e->buf + i; size_t add = m ? strlen(p->content) : 1; if (n + add + 1 > cap) { cap = (n + add + 1) * 2; char *q = realloc(out, cap); if (!q) { free(out); return NULL; } out = q; } memcpy(out + n, src, add); n += add; i += m ? m : 1; } out[n] = 0; return out; } char *tui_editor_take(tui_editor *e) { char *out = tui_editor_expanded(e); if (!out) return NULL; history_add(e, out); tui_editor_clear(e); return out; } /* Replaces the text with a history entry (or the saved unsent text). */ static void show_history(tui_editor *e, size_t pos) { e->hpos = pos; set_text(e, pos == e->nhist ? (e->saved ? e->saved : "") : e->hist[pos]); } /* ---- cursor movement --------------------------------------------------- */ static size_t col_of(const tui_editor *e, size_t pos) { size_t s = line_start(e, pos); return tui_width(e->buf + s, pos - s); } /* Byte offset on the line starting at s whose display column is the * largest not past col. */ static size_t pos_at_col(const tui_editor *e, size_t s, size_t col) { size_t end = line_end(e, s), pos = s, w = 0; while (pos < end) { size_t nx = next_char(e, pos); size_t cw = tui_width(e->buf + pos, nx - pos); if (w + cw > col) break; w += cw; pos = nx; } return pos; } /* UP/DOWN; 1 if the cursor moved, 0 when already on the first/last line. */ static int move_vertical(tui_editor *e, int up) { size_t s = line_start(e, e->cur); if (e->want_col < 0) e->want_col = (long)col_of(e, e->cur); size_t target; if (up) { if (s == 0) return 0; target = line_start(e, s - 1); } else { size_t end = line_end(e, e->cur); if (end >= e->len) return 0; target = end + 1; } e->cur = pos_at_col(e, target, (size_t)e->want_col); return 1; } static int on_first_line(const tui_editor *e) { return line_start(e, e->cur) == 0; } static int on_last_line(const tui_editor *e) { return line_end(e, e->cur) >= e->len; } const char *tui_editor_text(const tui_editor *e) { return e->buf ? e->buf : ""; } size_t tui_editor_cursor(const tui_editor *e) { return e->cur; } /* ---- undo, kill ring, jump --------------------------------------------- */ /* Saves the text and cursor so a later undo returns to them. */ static void snapshot(tui_editor *e) { char *t = strdup(e->buf ? e->buf : ""); snap *u; if (!t) return; if (e->nundo == UNDO_MAX) { free(e->undo[0].text); memmove(e->undo, e->undo + 1, (UNDO_MAX - 1) * sizeof *e->undo); e->nundo--; } if (!(u = realloc(e->undo, (e->nundo + 1) * sizeof *u))) { free(t); return; } e->undo = u; e->undo[e->nundo].text = t; e->undo[e->nundo++].cur = e->cur; } static void undo(tui_editor *e) { if (e->nundo == 0) return; snap *u = &e->undo[--e->nundo]; set_text(e, u->text); e->cur = u->cur <= e->len ? u->cur : e->len; free(u->text); e->hpos = e->nhist; } /* Adds killed text to the ring; consecutive kills merge into one entry, * prepended for backward deletion and appended for forward (Pi's KillRing). */ static void ring_push(tui_editor *e, const char *s, size_t n, int prepend, int accumulate) { if (n == 0) return; if (accumulate && e->nring > 0) { char *last = e->ring[e->nring - 1], *m = malloc(strlen(last) + n + 1); if (!m) return; if (prepend) { memcpy(m, s, n); strcpy(m + n, last); } else { strcpy(m, last); memcpy(m + strlen(last), s, n); m[strlen(last) + n] = 0; } free(last); e->ring[e->nring - 1] = m; return; } char *c = malloc(n + 1); char **r; if (!c) return; memcpy(c, s, n); c[n] = 0; if (e->nring == RING_MAX) { free(e->ring[0]); memmove(e->ring, e->ring + 1, (RING_MAX - 1) * sizeof *e->ring); e->nring--; } if (!(r = realloc(e->ring, (e->nring + 1) * sizeof *r))) { free(c); return; } e->ring = r; e->ring[e->nring++] = c; } /* Deletes [from, to), keeping it in the kill ring. */ static void kill_range(tui_editor *e, size_t from, size_t to, int prepend) { if (to <= from) return; snapshot(e); ring_push(e, e->buf + from, to - from, prepend, e->last == ACT_KILL); del(e, from, to); } static void yank_insert(tui_editor *e) { const char *t = e->ring[e->nring - 1]; size_t n = strlen(t); if (ins(e, e->cur, t, n) == 0) e->cur += n; } /* Moves to the first c after the cursor (forward) or before it (backward), * searching across lines. */ static void jump_to(tui_editor *e, const char *c, int dir) { size_t n = strlen(c); if (dir > 0) { for (size_t i = next_char(e, e->cur); i + n <= e->len && i < e->len; i = next_char(e, i)) if (memcmp(e->buf + i, c, n) == 0 && (e->buf[i] & 0xC0) != 0x80) { e->cur = i; return; } } else { for (size_t i = e->cur; i > 0;) { i = prev_char(e, i); if (i + n <= e->len && memcmp(e->buf + i, c, n) == 0) { e->cur = i; return; } } } } /* ---- keys -------------------------------------------------------------- */ static int only_space(const tui_editor *e) { for (size_t i = 0; i < e->len; i++) if (!is_space(e->buf[i])) return 0; return 1; } int tui_editor_key(tui_editor *e, const tui_key *k) { size_t old_cur = e->cur, old_len = e->len; int keep_col = 0, snap_dir = 0, act = ACT_NONE; if (e->jump) { int dir = e->jump; e->jump = 0; if (k->kind == TUI_KEY_JUMP || k->kind == TUI_KEY_JUMP_BACK) return TUI_EDIT_NONE; /* the key again cancels */ if (k->kind == TUI_KEY_CHAR) { jump_to(e, k->text, dir); e->last = ACT_NONE; e->want_col = -1; return e->cur != old_cur ? TUI_EDIT_CHANGED : TUI_EDIT_NONE; } } switch (k->kind) { case TUI_KEY_CHAR: { size_t n = strlen(k->text); /* Typing: a run of word characters is one undo unit, and each space * starts a new one (Pi's fish-style coalescing). */ if (k->text[0] == ' ' || k->text[0] == '\t' || e->last != ACT_TYPE) snapshot(e); act = ACT_TYPE; if (ins(e, e->cur, k->text, n) == 0) e->cur += n; break; } case TUI_KEY_NEWLINE: snapshot(e); if (ins(e, e->cur, "\n", 1) == 0) e->cur++; break; case TUI_KEY_ENTER: return only_space(e) ? TUI_EDIT_NONE : TUI_EDIT_SUBMIT; case TUI_KEY_PASTE: if (k->paste_len) { snapshot(e); (void)insert_paste(e, k->paste, k->paste_len); } break; case TUI_KEY_BACKSPACE: if (e->cur > 0) { size_t from = marker_before(e, e->cur); snapshot(e); del(e, from != e->cur ? from : prev_char(e, e->cur), e->cur); } break; case TUI_KEY_DELETE: if (e->cur < e->len) { size_t m = marker_at(e, e->cur, NULL); snapshot(e); del(e, e->cur, m ? e->cur + m : next_char(e, e->cur)); } break; case TUI_KEY_LEFT: e->cur = prev_char(e, e->cur); snap_dir = -1; break; case TUI_KEY_RIGHT: e->cur = next_char(e, e->cur); snap_dir = 1; break; case TUI_KEY_HOME: e->cur = line_start(e, e->cur); break; case TUI_KEY_END: e->cur = line_end(e, e->cur); break; case TUI_KEY_WORD_LEFT: e->cur = word_left(e, e->cur); snap_dir = -1; break; case TUI_KEY_WORD_RIGHT: e->cur = word_right(e, e->cur); snap_dir = 1; break; case TUI_KEY_DELETE_WORD: kill_range(e, word_left(e, e->cur), e->cur, 1); act = ACT_KILL; break; case TUI_KEY_DELETE_WORD_FWD: { size_t end = line_end(e, e->cur), to = word_right(e, e->cur); if (e->cur == end && end < e->len) to = end + 1; /* at the end of a line: join the next one */ else if (to > end) to = end; kill_range(e, e->cur, to, 0); act = ACT_KILL; break; } case TUI_KEY_KILL_LINE: { size_t end = line_end(e, e->cur); if (end == e->cur && end < e->len) end++; /* at the end of a line: join the next one */ kill_range(e, e->cur, end, 0); act = ACT_KILL; break; } case TUI_KEY_KILL_START: { size_t s = line_start(e, e->cur); if (s == e->cur && s > 0) s--; /* at the start of a line: join it to the previous one */ kill_range(e, s, e->cur, 1); act = ACT_KILL; break; } case TUI_KEY_YANK: if (e->nring) { snapshot(e); yank_insert(e); act = ACT_YANK; } break; case TUI_KEY_YANK_POP: /* Only right after a yank: swap the yanked text for the previous entry. */ if (e->last == ACT_YANK && e->nring > 1) { size_t n = strlen(e->ring[e->nring - 1]); char *first; snapshot(e); if (n <= e->cur) del(e, e->cur - n, e->cur); first = e->ring[e->nring - 1]; memmove(e->ring + 1, e->ring, (e->nring - 1) * sizeof *e->ring); e->ring[0] = first; yank_insert(e); act = ACT_YANK; } break; case TUI_KEY_UNDO: undo(e); break; case TUI_KEY_JUMP: e->jump = 1; return TUI_EDIT_NONE; case TUI_KEY_JUMP_BACK: e->jump = -1; return TUI_EDIT_NONE; case TUI_KEY_UP: case TUI_KEY_DOWN: { int up = k->kind == TUI_KEY_UP; keep_col = 1; if (up ? !on_first_line(e) : !on_last_line(e)) { move_vertical(e, up); snap_out(e, 0); } else if (up && e->hpos > 0) { if (e->hpos == e->nhist) { free(e->saved); e->saved = strdup(e->buf ? e->buf : ""); } show_history(e, e->hpos - 1); e->last = ACT_NONE; return TUI_EDIT_CHANGED; } else if (!up && e->hpos < e->nhist) { show_history(e, e->hpos + 1); e->last = ACT_NONE; return TUI_EDIT_CHANGED; } break; } default: return TUI_EDIT_NONE; } e->last = act; if (snap_dir) snap_out(e, snap_dir); if (!keep_col) e->want_col = -1; return e->cur != old_cur || e->len != old_len || k->kind == TUI_KEY_UNDO ? TUI_EDIT_CHANGED : TUI_EDIT_NONE; } /* ---- rendering --------------------------------------------------------- */ int tui_editor_render(const tui_editor *e, size_t cols, tui_lines *out, int *crow, int *ccol) { /* Wrap to cols - 3: two for the "> " / " " prefix and one spare, so a * cursor after a full line still has a column on that row. */ size_t wrap_cols = cols > 3 ? cols - 3 : 1; size_t mark = sizeof CURSOR_MARK - 1; char *tmp = malloc(e->len + mark + 1); if (!tmp) return -1; memcpy(tmp, e->buf ? e->buf : "", e->cur); memcpy(tmp + e->cur, CURSOR_MARK, mark); memcpy(tmp + e->cur + mark, e->buf ? e->buf + e->cur : "", e->len - e->cur); tui_lines w = {0}; int rc = tui_wrap(tmp, e->len + mark, wrap_cols, &w); free(tmp); *crow = 0; *ccol = 2; for (size_t i = 0; rc == 0 && i < w.n; i++) { char *line = w.lines[i]; char *at = strstr(line, CURSOR_MARK); if (at) { *crow = (int)i; *ccol = 2 + (int)tui_width(line, (size_t)(at - line)); memmove(at, at + mark, strlen(at + mark) + 1); } size_t n = strlen(line); char *full = malloc(n + 3); if (!full) { rc = -1; break; } memcpy(full, i == 0 ? "> " : " ", 2); memcpy(full + 2, line, n + 1); rc = tui_lines_add(out, full, n + 2); free(full); } tui_lines_clear(&w); return rc; }