From 1bf03eabaa94cf8dd4e0b27876c22540e3979762 Mon Sep 17 00:00:00 2001 From: fairyhunter13 <12372147+fairyhunter13@users.noreply.github.com> Date: Thu, 8 Oct 2026 09:18:41 +0700 Subject: [PATCH] Graphics: move Unicode placeholder cell images with lines scrolled into history from a region A line scrolled out of a region whose top is row 0 (tmux with a bottom status line) enters the history, but scroll_filter_margins_func left the refs of history lines in place, so they drew over other lines on the next scroll back. A reverse scroll also moved history refs although the history does not move. --- kitty/graphics.c | 8 ++++++ kitty/graphics.h | 1 + kitty/screen.c | 7 +++--- kitty_tests/graphics.py | 55 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 3 deletions(-) diff --git a/kitty/graphics.c b/kitty/graphics.c index 6848250bb..4711f3f12 100644 --- a/kitty/graphics.c +++ b/kitty/graphics.c @@ -2309,6 +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; 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 @@ -2345,6 +2347,12 @@ 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) { + ref->start_row += d->amt; + return ref->start_row + (int32_t)ref->effective_num_rows <= d->limit; + } if (ref_within_region(ref, d->margin_top, d->margin_bottom)) { ref->start_row += d->amt; if (ref_outside_region(ref, d->margin_top, d->margin_bottom)) return true; diff --git a/kitty/graphics.h b/kitty/graphics.h index f120e5315..f40cdf89b 100644 --- a/kitty/graphics.h +++ b/kitty/graphics.h @@ -180,6 +180,7 @@ typedef struct { int32_t amt, limit; index_type margin_top, margin_bottom; bool has_margins; + bool add_to_history; } ScrollData; diff --git a/kitty/screen.c b/kitty/screen.c index fc9a9f751..c99cc4b81 100644 --- a/kitty/screen.c +++ b/kitty/screen.c @@ -427,12 +427,13 @@ index_selection(const Screen *self, Selections *selections, bool up, index_type } -#define INDEX_GRAPHICS(amtv) \ +#define INDEX_GRAPHICS(amtv, histv) \ { \ if (UNLIKELY(grman_has_any_images(self->grman))) { \ bool is_main = self->linebuf == self->main_linebuf; \ static ScrollData s; \ s.amt = amtv; \ + s.add_to_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; \ @@ -450,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) \ + INDEX_GRAPHICS(1, false) \ 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); @@ -2493,7 +2494,7 @@ screen_cursor_to_column(Screen *self, unsigned int column) { #define INDEX_UP(add_to_history) \ linebuf_index(self->linebuf, top, bottom); \ - INDEX_GRAPHICS(-1) \ + INDEX_GRAPHICS(-1, add_to_history) \ if (add_to_history) { \ /* Only add to history when no top margin has been set */ \ linebuf_init_line(self->linebuf, bottom); \ diff --git a/kitty_tests/graphics.py b/kitty_tests/graphics.py index 9d1fffad4..c9913dd9c 100644 --- a/kitty_tests/graphics.py +++ b/kitty_tests/graphics.py @@ -1210,6 +1210,61 @@ class TestGraphics(BaseTest): s.update_only_line_graphics_data() self.ae(len(layers(s)), 0) + def test_unicode_placeholders_history_margins(self): + # Lines scrolled out of a region at the top of the screen (as tmux does for + # a pane above its status line) enter the history, and the cell images + # rendered for them while the view was scrolled back 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, 20, num_cols=1, num_lines=2, unicode_placeholder=1, id=42) + + def draw_placeholders(): + s.apply_sgr('38;5;42') + s.cursor_position(3, 1) + s.draw('\U0010eeee\u0305') + s.cursor_position(4, 1) + s.draw('\U0010eeee\u030d') + s.apply_sgr('39') + + def index_lines(row, n): + s.cursor_position(row, 1) + for i in range(n): + s.draw('plain') + s.index() + s.carriage_return() + s.update_only_line_graphics_data() + + def scroll_view(amt, up): + s.scroll(amt, up) + s.update_only_line_graphics_data() + return layers(s, s.scrolled_by) + + for region in (True, False): + s.reset() + if region: + s.set_margins(1, s.lines - 1) + bottom = s.lines - 1 if region else s.lines + draw_placeholders() + index_lines(bottom, 8) + self.ae(len(scroll_view(6, True)), 2) + scroll_view(6, False) + index_lines(bottom, 3) + self.ae(len(scroll_view(9, True)), 0, f'region={region}') + scroll_view(9, False) + + # A reverse index must not move the refs of lines in the history + s.reset() + draw_placeholders() + index_lines(s.lines, 5) + before = scroll_view(6, True) + self.ae(len(before), 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)) + def test_gr_scroll(self): cw, ch = 10, 20 s, dx, dy, put_image, put_ref, layers, rect_eq = put_helpers(self, cw, ch)