From f4ff04897ec56145befe2fc1865a452bb7ffa4dc Mon Sep 17 00:00:00 2001 From: bincxz <16399091+binaricat@users.noreply.github.com> Date: Mon, 17 Aug 2026 20:28:11 +0800 Subject: [PATCH] fix(terminal): treat history preview as a selection for Ctrl+C #3026 Urgent interrupt still asked only xterm for a selection. With history preview text selected, Ctrl+C therefore sent SIGINT into vim before the copy path ran. Use the same copyable-selection flag for interrupt, shortcut passthrough, and Kitty broadcast. --- components/terminal/runtime/createXTermRuntime.test.ts | 3 ++- components/terminal/runtime/createXTermRuntime.ts | 9 +++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/components/terminal/runtime/createXTermRuntime.test.ts b/components/terminal/runtime/createXTermRuntime.test.ts index c75ff122f..ddb4dfbcd 100644 --- a/components/terminal/runtime/createXTermRuntime.test.ts +++ b/components/terminal/runtime/createXTermRuntime.test.ts @@ -947,5 +947,6 @@ test("alternate-screen history preview stays selectable and copyable", async () assert.match(source, /shouldHideHistoryPreviewOnMouseDown/); assert.match(source, /shouldKeepHistoryPreviewOnKey/); assert.match(source, /previewSelection \|\| getTerminalSelectionForClipboard/); - assert.match(source, /term\.hasSelection\(\) \|\| Boolean\(previewSelection\)/); + assert.match(source, /hasCopyableSelection = term\.hasSelection\(\) \|\| Boolean\(previewSelection\)/); + assert.match(source, /shouldUseUrgentTerminalInterrupt\(e, \{ hasSelection: hasCopyableSelection \}\)/); }); diff --git a/components/terminal/runtime/createXTermRuntime.ts b/components/terminal/runtime/createXTermRuntime.ts index e4eb068ce..b81942b46 100644 --- a/components/terminal/runtime/createXTermRuntime.ts +++ b/components/terminal/runtime/createXTermRuntime.ts @@ -1458,6 +1458,7 @@ export const createXTermRuntime = (ctx: CreateXTermRuntimeContext): XTermRuntime } const previewSelection = getHistoryPreviewSelectionFromRoot(ctx.container); + const hasCopyableSelection = term.hasSelection() || Boolean(previewSelection); const forcedHistoryScrollPages = forcedHistoryScrollPagesForKey(e); if (forcedHistoryScrollPages !== null) { e.preventDefault(); @@ -1541,7 +1542,7 @@ export const createXTermRuntime = (ctx: CreateXTermRuntimeContext): XTermRuntime // the interrupt path hard-aborts instead (#2191). if ( e.key.length === 1 - && !shouldUseUrgentTerminalInterrupt(e, { hasSelection: term.hasSelection() }) + && !shouldUseUrgentTerminalInterrupt(e, { hasSelection: hasCopyableSelection }) ) { sudoAutofill.cancelHint(); // fall through: key becomes the first char of the manually typed password @@ -1574,7 +1575,7 @@ export const createXTermRuntime = (ctx: CreateXTermRuntimeContext): XTermRuntime : null; if ( (!kittySequenceForKeyDown || kittySequenceForKeyDown === "\x03") && - shouldUseUrgentTerminalInterrupt(e, { hasSelection: term.hasSelection() }) + shouldUseUrgentTerminalInterrupt(e, { hasSelection: hasCopyableSelection }) ) { const id = ctx.sessionRef.current; if (id && ctx.statusRef.current === "connected") { @@ -1704,7 +1705,7 @@ export const createXTermRuntime = (ctx: CreateXTermRuntimeContext): XTermRuntime const shouldForwardCopyToTerminal = shouldPassThroughCopyShortcut( action, - term.hasSelection() || Boolean(previewSelection), + hasCopyableSelection, e, ); if (shouldForwardCopyToTerminal && !kittySequenceForKeyDown) return true; @@ -1857,7 +1858,7 @@ export const createXTermRuntime = (ctx: CreateXTermRuntimeContext): XTermRuntime event: kittyEvent, fallbackToLegacy: true, urgentInterrupt: shouldUseUrgentTerminalInterrupt(e, { - hasSelection: term.hasSelection(), + hasSelection: hasCopyableSelection, }), }); if (forwarded) {