diff --git a/src/cascadia/TerminalControl/ControlCore.cpp b/src/cascadia/TerminalControl/ControlCore.cpp index cffc4e91a8..644bb2c0bc 100644 --- a/src/cascadia/TerminalControl/ControlCore.cpp +++ b/src/cascadia/TerminalControl/ControlCore.cpp @@ -1787,6 +1787,15 @@ namespace winrt::Microsoft::Terminal::Control::implementation void ControlCore::ClearSearch() { const auto lock = _terminal->LockForWriting(); + + // GH #19358: select the focused search result before clearing search + if (const auto focusedSearchResult = _terminal->GetSearchHighlightFocused()) + { + _terminal->SetSelectionAnchor(focusedSearchResult->start); + _terminal->SetSelectionEnd(focusedSearchResult->end); + _renderer->TriggerSelection(); + } + _terminal->SetSearchHighlights({}); _terminal->SetSearchHighlightFocused(0); _renderer->TriggerSearchHighlight(_searcher.Results()); diff --git a/src/cascadia/TerminalCore/TerminalSelection.cpp b/src/cascadia/TerminalCore/TerminalSelection.cpp index f316cc782a..6942fb9ea5 100644 --- a/src/cascadia/TerminalCore/TerminalSelection.cpp +++ b/src/cascadia/TerminalCore/TerminalSelection.cpp @@ -362,10 +362,30 @@ void Terminal::ToggleMarkMode() { // Enter Mark Mode // NOTE: directly set cursor state. We already should have locked before calling this function. - if (!IsSelectionActive()) + if (IsSelectionActive()) + { + // Selection already existed --> just target "end" + if (WI_AreAllFlagsClear(_selectionEndpoint, SelectionEndpoint::Start | SelectionEndpoint::End)) + { + WI_SetFlag(_selectionEndpoint, SelectionEndpoint::End); + } + } + else if (const auto focusedSearchResult = GetSearchHighlightFocused()) + { + // We have a focused search result --> treat it as selection + *_selection.write() = SelectionInfo{ + .start = focusedSearchResult->start, + .end = focusedSearchResult->end, + .pivot = focusedSearchResult->start, + .blockSelection = false, + .active = true, + }; + WI_SetFlag(_selectionEndpoint, SelectionEndpoint::End); + } + else { // If we're scrolled up, use the viewport origin as the selection start. - // Otherwise, use the cursor position. + // Otherwise, just use the cursor position. const auto cursorPos = _scrollOffset != 0 ? _GetVisibleViewport().Origin() : _activeBuffer().GetCursor().GetPosition(); *_selection.write() = SelectionInfo{ @@ -377,11 +397,6 @@ void Terminal::ToggleMarkMode() }; WI_SetAllFlags(_selectionEndpoint, SelectionEndpoint::Start | SelectionEndpoint::End); } - else if (WI_AreAllFlagsClear(_selectionEndpoint, SelectionEndpoint::Start | SelectionEndpoint::End)) - { - // Selection already existed - WI_SetFlag(_selectionEndpoint, SelectionEndpoint::End); - } _ScrollToPoint(_selection->start); _selectionMode = SelectionInteractionMode::Mark; _selectionIsTargetingUrl = false;