diff --git a/gtm b/gtm index b1da9c1..177e9a9 100755 --- a/gtm +++ b/gtm @@ -461,11 +461,25 @@ def perform_search(state: AppState) -> AppState: new_state = replace(state, search_matches=matches) - # If we have matches, select the first one + # If we have matches, select the first one that's in view or below current scroll if matches: - new_state = replace(new_state, current_match_idx=0) - # Scroll to the first match - new_state = replace(new_state, right_scroll_offset=max(0, matches[0] - 3)) + current_scroll = state.right_scroll_offset + visible_height = state.height - state.status_bar_height + + # Find the first match that's in the current view or below it + first_visible_match_idx = 0 + for idx, match_line in enumerate(matches): + if match_line >= current_scroll: + first_visible_match_idx = idx + break + + # If all matches are above current view, wrap around to the first match + new_state = replace(new_state, current_match_idx=first_visible_match_idx) + + # Scroll to the match if it's not already visible + match_line = matches[first_visible_match_idx] + if match_line < current_scroll or match_line >= current_scroll + visible_height: + new_state = replace(new_state, right_scroll_offset=max(0, match_line - 3)) else: new_state = replace(new_state, current_match_idx=-1)