@@ -15,6 +15,12 @@ import {
1515 getSubagentControlAction,
1616 getTurnControlAction,
1717 } from "./input-control.ts";
18+import {
19+ deleteWordBackward as deleteWordBackwardInBuffer,
20+ insertAtCursor,
21+ moveCursorWordLeft,
22+ moveCursorWordRight,
23+} from "./input-editing.ts";
1824 import { tryExtractImageFromPaste } from "./input-images.ts";
1925 import { terminal } from "./terminal-io.ts";
2026
@@ -207,15 +213,15 @@ export async function readline(opts: { cwd?: string }): Promise<InputResult> {
207213 }
208214
209215 function insertText(text: string): void {
210- buf = buf.slice(0, cursor) + text + buf.slice(cursor);
211- cursor += text.length;
216+ const updated = insertAtCursor(buf, cursor, text);
217+ buf = updated.buf;
218+ cursor = updated.cursor;
212219 }
213220
214221 function deleteWordBackward(): void {
215- const end = cursor;
216- while (cursor > 0 && buf[cursor - 1] === " ") cursor--;
217- while (cursor > 0 && buf[cursor - 1] !== " ") cursor--;
218- buf = buf.slice(0, cursor) + buf.slice(end);
222+ const updated = deleteWordBackwardInBuffer(buf, cursor);
223+ buf = updated.buf;
224+ cursor = updated.cursor;
219225 prunePasteTokens();
220226 renderPrompt();
221227 }
@@ -370,15 +376,13 @@ export async function readline(opts: { cwd?: string }): Promise<InputResult> {
370376 }
371377 // Alt+Left (word left): ESC b or ESC [1;3D
372378 if (raw === `${ESC}b` || raw === `${ESC}[1;3D`) {
373- while (cursor > 0 && buf[cursor - 1] === " ") cursor--;
374- while (cursor > 0 && buf[cursor - 1] !== " ") cursor--;
379+ cursor = moveCursorWordLeft(buf, cursor);
375380 renderPrompt();
376381 continue;
377382 }
378383 // Alt+Right (word right): ESC f or ESC [1;3C
379384 if (raw === `${ESC}f` || raw === `${ESC}[1;3C`) {
380- while (cursor < buf.length && buf[cursor] === " ") cursor++;
381- while (cursor < buf.length && buf[cursor] !== " ") cursor++;
385+ cursor = moveCursorWordRight(buf, cursor);
382386 renderPrompt();
383387 continue;
384388 }