diff --git a/kitty/screen.c b/kitty/screen.c index 2b5513d5b..d8e8eadf7 100644 --- a/kitty/screen.c +++ b/kitty/screen.c @@ -3789,9 +3789,10 @@ screen_history_scroll_to_prompt(Screen *self, int num_of_prompts_to_jump, int sc } else { int delta = num_of_prompts_to_jump < 0 ? -1 : 1; num_of_prompts_to_jump = num_of_prompts_to_jump < 0 ? -num_of_prompts_to_jump : num_of_prompts_to_jump; - int y = -self->scrolled_by; + const int oldest_y = -(int)self->historybuf->count; + int y = -(int)self->scrolled_by; #define ensure_y_ok \ - if (y >= (int)self->lines || -y > (int)self->historybuf->count) return false; + if (y >= (int)self->lines || y < oldest_y) return false; ensure_y_ok; y += scroll_offset; while (num_of_prompts_to_jump) { @@ -3799,10 +3800,12 @@ screen_history_scroll_to_prompt(Screen *self, int num_of_prompts_to_jump, int sc ensure_y_ok; if (range_line_(self, y)->attrs.prompt_kind == PROMPT_START) { num_of_prompts_to_jump--; } } - y -= scroll_offset; #undef ensure_y_ok - self->scrolled_by = y >= 0 ? 0 : -y; - screen_set_last_visited_prompt(self, 0); + const int viewport_top = MAX(oldest_y, MIN(0, y - scroll_offset)); + self->scrolled_by = -viewport_top; + const int prompt_y = y - viewport_top; + // The context above the prompt can include another command's output. + screen_set_last_visited_prompt(self, prompt_y >= 0 && prompt_y < (int)self->lines ? prompt_y : 0); } if (old != self->scrolled_by) { reset_pixel_scroll(self, 0); diff --git a/kitty_tests/screen.py b/kitty_tests/screen.py index 3d770609a..454e771b8 100644 --- a/kitty_tests/screen.py +++ b/kitty_tests/screen.py @@ -2353,6 +2353,28 @@ class TestScreen(BaseTest): s.draw('a foo') ae(s, 'foo', 2) + def test_scroll_to_prompt_with_short_scrollback(self): + for lines_before_prompt in (0, 1): + with self.subTest(lines_before_prompt=lines_before_prompt): + s = self.create_screen(cols=12, lines=4, scrollback=10) + if lines_before_prompt: + parse_bytes(s, b'\x1b]133;C\x07older\r\n') + parse_bytes(s, b'\x1b]133;A\x07$ a\r\n') + parse_bytes(s, b'\x1b]133;C\x07a0\r\n\r\n\r\n') + parse_bytes(s, b'\x1b]133;A\x07$ live') + self.ae(s.historybuf.count, lines_before_prompt + 1) + + self.assertTrue(s.scroll_to_prompt(-1, 3)) + self.ae(s.scrolled_by, s.historybuf.count) + s.scroll_to_absolute(0) + + output = [] + s.cmd_output(2, output.append) + self.ae(''.join(output).rstrip(), 'a0') + self.assertTrue(s.scroll_to_prompt(0)) + self.ae(s.scrolled_by, s.historybuf.count) + self.ae(str(s.visual_line(lines_before_prompt)), '$ a') + def test_prompt_marking(self): # ]]]]]]]]]]]]]]]]}}}}}}}}}}}}}}}}))))))))))))))))))))))