diff --git a/src/ui.c b/src/ui.c index caac866..5e4f112 100644 --- a/src/ui.c +++ b/src/ui.c @@ -751,6 +751,55 @@ entry_prefix(const struct entry *e, char *buf, size_t len) #define MD_ITALIC 0x02 #define MD_CODE 0x04 +/* + * Is the '*' run at p (len 1 or 2) emphasis, or just an asterisk? The rules of + * CommonMark, short: an opener is followed by text and preceded by a space or + * punctuation, a closer is preceded by text, and an opener needs its closer on + * the same line. Without this "void *ctx, const char *name" in a code answer + * lost its pointers and came out italic. + */ +static int +emph_ok(const char *start, const char *p, int len, int open_now, int in_code) +{ + char prev = p > start ? p[-1] : ' '; + char next = p[len]; + int prev_space = prev == ' ' || prev == '\t' || prev == '\n'; + int next_space = next == '\0' || next == ' ' || next == '\t' || + next == '\n'; + const char *q; + + if (in_code) + return 0; + if (open_now) /* a closer: it must follow text */ + return !prev_space; + if (next_space) /* an opener: it must precede text */ + return 0; + if (!(prev_space || strchr("([{\"'-,.;:/", prev) != NULL)) + return 0; + /* + * ... and a closer must follow on this line before another opener does: + * the nearest opener takes the closer, the earlier ones stay asterisks. + */ + for (q = p + len; *q != '\0' && *q != '\n'; q++) { + int rl = 0; + + if (*q != '*') + continue; + while (q[rl] == '*') + rl++; + if (rl == len) { + char pq = q[-1], nq = q[rl]; + + if (pq != ' ' && pq != '\t') + return 1; /* a closer */ + if (nq != '\0' && nq != ' ' && nq != '\t' && nq != '\n') + return 0; /* a nearer opener */ + } + q += rl - 1; + } + return 0; +} + static size_t md_strip_n(const char *in, char *out, unsigned char *attr) { @@ -782,7 +831,8 @@ md_strip_n(const char *in, char *out, unsigned char *attr) bold = 1; continue; } - if (!fence && p[0] == '*' && p[1] == '*') { + if (!fence && p[0] == '*' && p[1] == '*' && + emph_ok(in, p, 2, bold, code)) { bold = !bold; p += 2; continue; @@ -797,7 +847,7 @@ md_strip_n(const char *in, char *out, unsigned char *attr) * Qwen3.6-35B-A3B-UD-Q8_K_XL came out as Qwen3.6-35B-A3B-UD-Q8KX, * which is a different model as far as a reader is concerned. */ - if (!fence && *p == '*') { + if (!fence && *p == '*' && emph_ok(in, p, 1, italic, code)) { italic = !italic; p++; continue; diff --git a/tests/fake_openai_tool.py b/tests/fake_openai_tool.py index 5a6a5c9..1a2a1cb 100644 --- a/tests/fake_openai_tool.py +++ b/tests/fake_openai_tool.py @@ -20,7 +20,10 @@ class H(http.server.BaseHTTPRequestHandler): self.send_header("Content-Type", "text/event-stream") self.end_headers() w = self.wfile.write - if last["role"] == "tool": + if last["role"] == "user" and "pointers" in str(last.get("content")): + w(ev({"choices": [{"delta": {"content": + "void *ctx, const char *name -- **bold** and *it*"}}]})) + elif last["role"] == "tool": w(ev({"choices": [{"delta": {"content": "obs: " + str(last.get("content", ""))[:300]}}]})) else: diff --git a/tests/test_agents_ui.sh b/tests/test_agents_ui.sh index 2d2953f..acdb56a 100644 --- a/tests/test_agents_ui.sh +++ b/tests/test_agents_ui.sh @@ -71,6 +71,11 @@ tmux new-session -d -s $S -x 120 -y 30 -c "$D/proj" \ SPLINTER_COOLDOWN_DIR=$D/c $PWD/splinter --no-config \ --endpoint http://127.0.0.1:$P/v1/chat/completions --model m --chat 2>/dev/null" check "German UI from LANG=de_DE" shows 'bereit' +# an asterisk in code is an asterisk; real emphasis loses its markers +tmux send-keys -t $S -l 'pointers bitte' +tmux send-keys -t $S Enter +check "pointers keep their asterisks" shows 'void \*ctx, const char \*name' +check "bold loses its markers" shows -- '-- bold and it' check "no raw catalogue keys on the screen" sh -c \ "! tmux capture-pane -t $S -p | grep -Eq '(^|[ >])(ui|sidebar|status|widget|agent|usage|role)\.[a-z_]+'"