diff --git a/kitty/graphics.c b/kitty/graphics.c index 4711f3f12..59922b662 100644 --- a/kitty/graphics.c +++ b/kitty/graphics.c @@ -2309,8 +2309,8 @@ static bool scroll_filter_func(ImageRef *ref, Image UNUSED *img, const void *data, CellPixelSize cell UNUSED) { if (ref->is_virtual_ref) return false; ScrollData *d = (ScrollData *)data; - // A reverse scroll moves screen lines down but leaves the history in place. - if (d->amt > 0 && is_cell_image(ref) && ref->start_row + (int32_t)ref->effective_num_rows <= 0) return false; + // A reverse scroll that does not pull lines back from the history leaves the history in place + if (d->amt > 0 && !d->scroll_history && ref->start_row + (int32_t)ref->effective_num_rows <= 0) return false; ref->start_row += d->amt; // A cell image is tied to the placeholders on its line, which are lost when // the line is scrolled off the bottom, so the ref must not come back on a @@ -2347,9 +2347,10 @@ static bool scroll_filter_margins_func(ImageRef *ref, Image *img, const void *data, CellPixelSize cell) { if (ref->is_virtual_ref) return false; ScrollData *d = (ScrollData *)data; - // A line scrolled out of a region at the top of the screen still enters the history, - // so a cell image whose line is already in the history moves with it. - if (d->add_to_history && is_cell_image(ref) && ref->start_row + (int32_t)ref->effective_num_rows <= (int32_t)d->margin_top) { + // Lines scrolled out of a region at the top of the screen still enter the history and + // lines popped from the history move it down, so a cell image whose line is in the + // history moves with it. + if (d->scroll_history && is_cell_image(ref) && ref->start_row + (int32_t)ref->effective_num_rows <= 0) { ref->start_row += d->amt; return ref->start_row + (int32_t)ref->effective_num_rows <= d->limit; } diff --git a/kitty/graphics.h b/kitty/graphics.h index f40cdf89b..405c8325d 100644 --- a/kitty/graphics.h +++ b/kitty/graphics.h @@ -180,7 +180,8 @@ typedef struct { int32_t amt, limit; index_type margin_top, margin_bottom; bool has_margins; - bool add_to_history; + // The history scrolls with the screen: lines move into it (amt < 0) or are popped back from it (amt > 0) + bool scroll_history; } ScrollData; diff --git a/kitty/screen.c b/kitty/screen.c index c99cc4b81..2c8215458 100644 --- a/kitty/screen.c +++ b/kitty/screen.c @@ -433,7 +433,7 @@ index_selection(const Screen *self, Selections *selections, bool up, index_type bool is_main = self->linebuf == self->main_linebuf; \ static ScrollData s; \ s.amt = amtv; \ - s.add_to_history = histv; \ + s.scroll_history = histv; \ s.limit = is_main ? -self->historybuf->ynum : 0; \ s.has_margins = self->margin_top != 0 || self->margin_bottom != self->lines - 1; \ s.margin_top = top; \ @@ -443,7 +443,7 @@ index_selection(const Screen *self, Selections *selections, bool up, index_type } -#define INDEX_DOWN \ +#define INDEX_DOWN(scroll_history) \ linebuf_reverse_index(self->linebuf, top, bottom); \ linebuf_clear_line(self->linebuf, top, true); \ if (self->linebuf == self->main_linebuf && self->last_visited_prompt.is_set) { \ @@ -451,7 +451,7 @@ index_selection(const Screen *self, Selections *selections, bool up, index_type else if (self->last_visited_prompt.y < self->lines - 1) self->last_visited_prompt.y++; \ else self->last_visited_prompt.is_set = false; \ } \ - INDEX_GRAPHICS(1, false) \ + INDEX_GRAPHICS(1, scroll_history) \ self->is_dirty = true; \ if (UNLIKELY(self->selections.count)) index_selection(self, &self->selections, false, top, bottom); \ if (UNLIKELY(self->url_ranges.count || self->url_ranges.in_progress)) clear_selection(&self->url_ranges); @@ -724,7 +724,7 @@ screen_resize(Screen *self, unsigned int lines, unsigned int columns) { Savepoint *sp = is_main ? &self->main_savepoint : &self->alt_savepoint; while (self->cursor->y + 1 < self->lines && self->lines - self->cursor->y > lines_after_cursor_before_resize) { if (!historybuf_pop_line(self->historybuf, self->alt_linebuf->line)) break; - INDEX_DOWN; + INDEX_DOWN(true); linebuf_copy_line_to(self->main_linebuf, self->alt_linebuf->line, 0); self->cursor->y++; sp->cursor.y = MIN(sp->cursor.y + 1, self->lines - 1); @@ -2551,7 +2551,7 @@ screen_reverse_index(Screen *self) { // Move cursor up one line, scrolling screen if needed unsigned int top = self->margin_top, bottom = self->margin_bottom; if (self->cursor->y == top) { - INDEX_DOWN; + INDEX_DOWN(false); } else screen_cursor_up(self, 1, false, -1); } @@ -2567,7 +2567,7 @@ _reverse_scroll(Screen *self, unsigned int count, bool fill_from_scrollback) { while (count-- > 0) { bool copied = false; if (fill_from_scrollback) copied = historybuf_pop_line(self->historybuf, self->alt_linebuf->line); - INDEX_DOWN; + INDEX_DOWN(copied); if (copied) linebuf_copy_line_to(self->main_linebuf, self->alt_linebuf->line, 0); } } diff --git a/kitty_tests/graphics.py b/kitty_tests/graphics.py index c9913dd9c..fb64217a9 100644 --- a/kitty_tests/graphics.py +++ b/kitty_tests/graphics.py @@ -1256,14 +1256,86 @@ class TestGraphics(BaseTest): s.reset() draw_placeholders() index_lines(s.lines, 5) - before = scroll_view(6, True) - self.ae(len(before), 2) + + def ref_rows(refs): + return sorted(round((1 - r['dest_rect']['top']) / dy) - s.scrolled_by for r in refs) + + before = ref_rows(scroll_view(6, True)) + self.ae(before, [-3, -2]) scroll_view(6, False) s.cursor_position(1, 1) s.reverse_index() s.update_only_line_graphics_data() - after = scroll_view(6, True) - self.ae(len(after), len(before)) + self.ae(ref_rows(scroll_view(6, True)), before) + + def test_unicode_placeholders_history_fill(self): + # Lines popped back from the history (CSI + T, or enlarging the window with + # scrollback_fill_enlarged_window) move the history down, so cell images + # rendered for lines in the history must move with them. + cw, ch = 5, 10 + s, dx, dy, put_image, put_ref, layers, rect_eq = put_helpers(self, cw, ch, lines=6) + put_image(s, 5, 10, num_cols=1, num_lines=1, unicode_placeholder=1, id=42) + + def ref_rows(scrolled_by=0): + s.update_only_line_graphics_data() + return sorted(round((1 - r['dest_rect']['top']) / dy) - scrolled_by for r in layers(s, scrolled_by)) + + def rows_in_history(): + # Render the history lines as when the view is scrolled back + n = s.historybuf.count + s.scroll(n, True) + ans = [r for r in ref_rows(n) if r < 0] + s.scroll(n, False) + s.update_only_line_graphics_data() + return ans + + def setup(region): + # Push 'plain' and then a placeholder line into the history + s.reset() + if region: + s.set_margins(1, s.lines - 1) + s.draw('plain') + s.carriage_return(), s.linefeed() + s.apply_sgr('38;5;42') + s.draw('\U0010eeee\u0305') + s.apply_sgr('39') + s.cursor_position(s.lines - 1 if region else s.lines, 1) + s.index(), s.index() + self.ae(str(s.historybuf.line(0)), '\U0010eeee\u0305') + self.ae(rows_in_history(), [-1]) + + for region in (False, True): + setup(region) + s.reverse_scroll(1, True) + self.ae(str(s.line(0)), '\U0010eeee\u0305') + self.ae(rows_in_history(), [], f'region={region}') + self.ae(ref_rows(), [0], f'region={region}') + + def classic_in_history(): + nonlocal s, dy, layers + s, _, dy, put_image, _, layers, _ = put_helpers(self, cw, ch, lines=6) + put_image(s, cw, ch) # a one cell image at (0, 0) + s.draw('img') + s.cursor_position(s.lines, 1) + s.index() + self.ae(ref_rows(1), [-1]) + + # A reverse index that does not pull lines from the history leaves classic + # placements in the history in place + classic_in_history() + s.cursor_position(1, 1) + s.reverse_index() + self.ae(ref_rows(), []) + self.ae(ref_rows(1), [-1]) + + # Enlarging the window pulls lines back from the history (cell images are + # removed on resize, so only classic placements are left to move) + classic_in_history() + self.set_options({'scrollback_fill_enlarged_window': True}) + s.cursor_position(2, 1) + s.resize(s.lines + 1, s.columns) + self.ae(str(s.line(0)), ' img') + self.ae(ref_rows(), [0]) def test_gr_scroll(self): cw, ch = 10, 20