@@ -191,8 +191,19 @@ function findFocusedTIInTree(ln: LayoutNode): LayoutNode | null {
191191
192192 // --- Input handling ---
193193
194-// Regex for a single SGR mouse event (non-anchored, for scanning batched input)
195-const SGR_MOUSE_RE = /\x1b\[<(\d+);(\d+);(\d+)([Mm])/g;
194+const BRACKETED_PASTE_START = "\x1b[200~";
195+const BRACKETED_PASTE_END = "\x1b[201~";
196+
197+// Regex for a single SGR mouse event, anchored to the current parse position.
198+const SGR_MOUSE_RE = /^\x1b\[<(\d+);(\d+);(\d+)([Mm])/;
199+
200+// Trailing keyboard data that ended with an incomplete CSI sequence.
201+let pendingKeyData = "";
202+
203+// Bracketed paste state across stdin chunks.
204+let inBracketedPaste = false;
205+let bracketedPasteData = "";
206+let bracketedPasteSuffix = "";
196207
197208 // Tracks accumulated scroll offsets during a batch of mouse events.
198209 // Controlled scroll reads scrollOffset from props, which doesn't update until
@@ -206,32 +217,61 @@ let batchScrollOffsets: Map<object, number> | null = null;
206217 let batchTextInputEdits: Map<TextInputProps, EditState> | null = null;
207218
208219 function handleInput(data: string): void {
209- SGR_MOUSE_RE.lastIndex = 0;
210220 batchTextInputEdits = new Map();
211221
212- let lastIndex = 0;
213-
214222 try {
215- let match = SGR_MOUSE_RE.exec(data);
216- while (match) {
217- if (match.index > lastIndex) {
218- handleKeyChunk(data.slice(lastIndex, match.index));
219- }
223+ let remaining = data;
224+
225+ if (inBracketedPaste) {
226+ remaining = consumeBracketedPasteData(remaining);
227+ if (remaining.length === 0) return;
228+ }
229+
230+ let chunk = pendingKeyData + remaining;
231+ pendingKeyData = "";
232+
233+ let keyChunkStart = 0;
234+ let index = 0;
235+
236+ while (index < chunk.length) {
237+ if (chunk.startsWith(BRACKETED_PASTE_START, index)) {
238+ handleKeyChunk(chunk.slice(keyChunkStart, index));
239+
240+ index += BRACKETED_PASTE_START.length;
241+ keyChunkStart = index;
242+ inBracketedPaste = true;
243+
244+ const afterPaste = consumeBracketedPasteData(chunk.slice(index));
245+ if (afterPaste.length === 0) return;
220246
221- if (batchScrollOffsets === null) {
222- batchScrollOffsets = new Map();
247+ chunk = afterPaste;
248+ index = 0;
249+ keyChunkStart = 0;
250+ continue;
223251 }
224252
225- const mouse = parseSgrMatch(match);
226- if (mouse) handleMouseEvent(mouse);
253+ const mouse = readSgrMouseEvent(chunk, index);
254+ if (mouse) {
255+ handleKeyChunk(chunk.slice(keyChunkStart, index));
227256
228- lastIndex = match.index + match[0].length;
229- match = SGR_MOUSE_RE.exec(data);
230- }
257+ if (batchScrollOffsets === null) {
258+ batchScrollOffsets = new Map();
259+ }
260+ if (mouse.event) handleMouseEvent(mouse.event);
261+
262+ index = mouse.nextIndex;
263+ keyChunkStart = index;
264+ continue;
265+ }
231266
232- if (lastIndex < data.length) {
233- handleKeyChunk(data.slice(lastIndex));
267+ index++;
234268 }
269+
270+ const { complete, pending } = splitIncompleteCsiSuffix(
271+ chunk.slice(keyChunkStart),
272+ );
273+ handleKeyChunk(complete);
274+ pendingKeyData = pending;
235275 } finally {
236276 batchScrollOffsets = null;
237277 batchTextInputEdits = null;
@@ -245,12 +285,84 @@ function handleKeyChunk(data: string): void {
245285 }
246286 }
247287
288+function consumeBracketedPasteData(data: string): string {
289+ const chunk = bracketedPasteSuffix + data;
290+ bracketedPasteSuffix = "";
291+
292+ const endIndex = chunk.indexOf(BRACKETED_PASTE_END);
293+ if (endIndex === -1) {
294+ const { complete, pending } = splitTrailingMarkerPrefix(
295+ chunk,
296+ BRACKETED_PASTE_END,
297+ );
298+ bracketedPasteData += complete;
299+ bracketedPasteSuffix = pending;
300+ return "";
301+ }
302+
303+ bracketedPasteData += chunk.slice(0, endIndex);
304+ const pastedText = bracketedPasteData;
305+
306+ inBracketedPaste = false;
307+ bracketedPasteData = "";
308+ bracketedPasteSuffix = "";
309+ handleBracketedPaste(pastedText);
310+
311+ return chunk.slice(endIndex + BRACKETED_PASTE_END.length);
312+}
313+
314+function splitTrailingMarkerPrefix(
315+ data: string,
316+ marker: string,
317+): { complete: string; pending: string } {
318+ const maxPrefixLength = Math.min(data.length, marker.length - 1);
319+ for (let length = maxPrefixLength; length > 0; length--) {
320+ if (data.endsWith(marker.slice(0, length))) {
321+ return {
322+ complete: data.slice(0, data.length - length),
323+ pending: data.slice(data.length - length),
324+ };
325+ }
326+ }
327+ return { complete: data, pending: "" };
328+}
329+
330+function splitIncompleteCsiSuffix(data: string): {
331+ complete: string;
332+ pending: string;
333+} {
334+ const csiStart = data.lastIndexOf("\x1b[");
335+ if (csiStart === -1) return { complete: data, pending: "" };
336+
337+ const suffix = data.slice(csiStart);
338+ for (let i = 2; i < suffix.length; i++) {
339+ const code = suffix.charCodeAt(i);
340+ if (code >= 0x40 && code <= 0x7e) {
341+ return { complete: data, pending: "" };
342+ }
343+ }
344+
345+ return { complete: data.slice(0, csiStart), pending: suffix };
346+}
347+
248348 interface MouseEvent {
249349 type: "click" | "scroll-up" | "scroll-down";
250350 x: number;
251351 y: number;
252352 }
253353
354+function readSgrMouseEvent(
355+ data: string,
356+ index: number,
357+): { event: MouseEvent | null; nextIndex: number } | null {
358+ const match = SGR_MOUSE_RE.exec(data.slice(index));
359+ if (!match) return null;
360+ return {
361+ event: parseSgrMatch(match),
362+ nextIndex: index + match[0].length,
363+ };
364+}
365+
254366 /**
255367 * Parse a single SGR mouse event from a RegExp match.
256368 * Returns a MouseEvent for scroll and click events, null for unhandled buttons.
@@ -610,6 +722,31 @@ function getTextInputEditState(props: TextInputProps): EditState {
610722 };
611723 }
612724
725+function commitTextInputEdit(
726+ props: TextInputProps,
727+ previousState: EditState,
728+ nextState: EditState,
729+): void {
730+ batchTextInputEdits?.set(props, nextState);
731+ setTextInputCursor(props, nextState.cursor);
732+ if (nextState.value !== previousState.value) {
733+ props.onChange(nextState.value);
734+ }
735+ cel.render();
736+}
737+
738+function handleBracketedPaste(text: string): void {
739+ if (text.length === 0) return;
740+
741+ const focusedInput = findFocusedTextInput();
742+ if (!focusedInput) return;
743+
744+ const props = focusedInput.node.props as TextInputProps;
745+ const editState = getTextInputEditState(props);
746+ const nextState = insertChar(editState, text);
747+ commitTextInputEdit(props, editState, nextState);
748+}
749+
613750 function handleKeyEvent(event: KeyInput): void {
614751 const { key, text } = event;
615752
@@ -750,12 +887,7 @@ function handleKeyEvent(event: KeyInput): void {
750887 }
751888
752889 if (newState && newState !== editState) {
753- batchTextInputEdits?.set(props, newState);
754- setTextInputCursor(props, newState.cursor);
755- if (newState.value !== editState.value) {
756- props.onChange(newState.value);
757- }
758- cel.render();
890+ commitTextInputEdit(props, editState, newState);
759891 return;
760892 }
761893 }
@@ -874,8 +1006,8 @@ export const cel = {
8741006 * Initialize the framework with a terminal implementation.
8751007 * Must be called before {@link cel.viewport}.
8761008 *
877- * Enables the Kitty keyboard protocol (level 1) via the terminal,
878- * enters raw mode, and starts mouse tracking.
1009+ * Enables the Kitty keyboard protocol (level 1) and bracketed paste mode via
1010+ * the terminal, enters raw mode, and starts mouse tracking.
8791011 *
8801012 * @param term - Terminal to render to (ProcessTerminal or MockTerminal).
8811013 * @param options - Optional configuration.
@@ -914,8 +1046,8 @@ export const cel = {
9141046 /**
9151047 * Stop the framework and restore terminal state.
9161048 *
917- * Pops the Kitty keyboard protocol mode, disables mouse tracking,
918- * and restores the terminal to its previous state.
1049+ * Pops the Kitty keyboard protocol mode, disables bracketed paste and mouse
1050+ * tracking, and restores the terminal to its previous state.
9191051 */
9201052 stop(): void {
9211053 terminal?.stop();
@@ -930,6 +1062,10 @@ export const cel = {
9301062 stampedNode = null;
9311063 uncontrolledScrollOffsets.clear();
9321064 stampedScrollNodes = [];
1065+ pendingKeyData = "";
1066+ inBracketedPaste = false;
1067+ bracketedPasteData = "";
1068+ bracketedPasteSuffix = "";
9331069 lastTerminalCursor = { visible: false };
9341070 activeTheme = defaultTheme;
9351071 },