From e1250cf8fb191d9ce5e9579dc2f7f96f1086e7b1 Mon Sep 17 00:00:00 2001 From: rudolf-blue Date: Thu, 8 Oct 2026 15:12:12 -0400 Subject: [PATCH] Fix erase in line and display orphaning halves of multicell characters --- kitty/screen.c | 23 +++++++++++++---- kitty_tests/multicell.py | 54 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 5 deletions(-) diff --git a/kitty/screen.c b/kitty/screen.c index 2c8215458..46441af42 100644 --- a/kitty/screen.c +++ b/kitty/screen.c @@ -2871,7 +2871,7 @@ screen_erase_in_line(Screen *self, unsigned int how, bool private) { default: break; } if (n > 0) { - nuke_multicell_char_intersecting_with(self, s, n, self->cursor->y, self->cursor->y + 1, false); + nuke_multicell_char_intersecting_with(self, s, s + n, self->cursor->y, self->cursor->y + 1, false); screen_dirty_line_graphics(self, self->cursor->y, self->cursor->y, self->linebuf == self->main_linebuf); linebuf_init_line(self->linebuf, self->cursor->y); if (private) { @@ -2932,6 +2932,22 @@ screen_move_into_scrollback(Screen *self) { } } +static void +nuke_multiline_chars_split_by_erase(Screen *self, unsigned int how, index_type a, index_type b) { + // remove chars crossing the erase boundary + for (int top = 1; top >= 0; top--) { + if (top ? how == 3 : how != 1) continue; + CPUCell *cp; + GPUCell *gp; + index_type y = top ? a : b - 1; + linebuf_init_cells(self->linebuf, y, &cp, &gp); + for (index_type x = 0; x < self->columns; x++) { + if (!cp[x].is_multicell) continue; + if (top ? cp[x].y > 0 : cp[x].y + 1 < cp[x].scale) nuke_multicell_char_at(self, x, y, false); + } + } +} + void screen_erase_in_display(Screen *self, unsigned int how, bool private) { /* Erases display in a specific way. @@ -2950,7 +2966,6 @@ screen_erase_in_display(Screen *self, unsigned int how, bool private) { :param bool private: when ``True`` character attributes are left unchanged */ unsigned int a, b; - bool nuke_multicell_chars = true; switch (how) { case 0: a = self->cursor->y + 1; @@ -2962,7 +2977,6 @@ screen_erase_in_display(Screen *self, unsigned int how, bool private) { break; case 22: screen_move_into_scrollback(self); - nuke_multicell_chars = false; // they have been moved into scrollback and we would get double deletions how = 2; /* fallthrough */ case 2: @@ -2974,11 +2988,11 @@ screen_erase_in_display(Screen *self, unsigned int how, bool private) { grman_clear(self->grman, how == 3, self->cell_size); a = 0; b = self->lines; - nuke_multicell_chars = false; break; default: return; } if (b > a) { + if (how < 3) nuke_multiline_chars_split_by_erase(self, how, a, b); if (how != 3) screen_dirty_line_graphics(self, a, b, self->linebuf == self->main_linebuf); if (private) { for (unsigned int i = a; i < b; i++) { @@ -2988,7 +3002,6 @@ screen_erase_in_display(Screen *self, unsigned int how, bool private) { linebuf_clear_attrs_and_dirty(self->linebuf, i); } } else linebuf_clear_lines(self->linebuf, self->cursor, a, b); - if (nuke_multicell_chars) nuke_multicell_char_intersecting_with(self, 0, self->columns, a, b, false); self->is_dirty = true; if (selection_intersects_screen_lines(&self->selections, a, b)) clear_selection(&self->selections); if (selection_intersects_screen_lines(&self->url_ranges, a, b)) clear_selection(&self->url_ranges); diff --git a/kitty_tests/multicell.py b/kitty_tests/multicell.py index 2a62827e9..765e20730 100644 --- a/kitty_tests/multicell.py +++ b/kitty_tests/multicell.py @@ -463,6 +463,24 @@ def test_multicell(self: TestMulticell) -> None: s.clear_scrollback() assert_line('\0\0\0\0\0\0') + # Erase in line with cursor in the right half + s.reset() + s.cursor.x = 4 + multicell(s, 'b', width=2) + s.cursor.x = 5 + s.erase_in_line(0) + for x in range(s.columns): + self.assertIsNone(s.cpu_cells(0, x)['mcd']) + for cx in (3, 4, 5): + s.reset() + s.cursor.x = cx - 1 + multicell(s, 'b', scale=2) + s.cursor.x = cx + s.erase_in_line(0) + for y in (0, 1): + for x in range(s.columns): + self.assertIsNone(s.cpu_cells(y, x)['mcd'], f'{cx} {y} {x}') + # Erase in display for x in (1, 2): s.reset(), s.draw('a'), multicell(s, 'b', scale=2), s.draw('c') @@ -483,6 +501,42 @@ def test_multicell(self: TestMulticell) -> None: self.ae(s.historybuf.line(1).as_ansi(), f'a\x1b]{TEXT_SIZE_CODE};s=2;b\x07c') self.ae(s.historybuf.line(0).as_ansi(), ' ') + # Erase in display splitting a multiline char + def no_mcd(*rows): + for y in rows: + for x in range(s.columns): + self.assertIsNone(s.cpu_cells(y, x)['mcd'], f'{y} {x}') + + for private in (False, True): + s.reset() + s.cursor.x, s.cursor.y = 2, 1 + multicell(s, 'a', scale=2) + s.cursor.x, s.cursor.y = 4, 1 + s.erase_in_display(0, private) + no_mcd(0, 1, 2) + s.reset() + s.cursor.x, s.cursor.y = 2, 1 + multicell(s, 'a', scale=2) + s.cursor.x, s.cursor.y = 0, 2 + s.erase_in_display(1, private) + no_mcd(0, 1, 2) + + # char split between history and screen + for how, y in ((1, 2), (2, 0)): + for private in (False, True): + s.reset() + s.cursor.x, s.cursor.y = 2, s.lines - 2 + multicell(s, 'a', scale=2) + s.cursor.y = s.lines - 1 + for i in range(s.lines - 1): + s.index() + self.ae(s.historybuf.count, s.lines - 1) + self.assertIsNotNone(s.cpu_cells(0, 2)['mcd']) + s.cursor.x, s.cursor.y = 0, y + s.erase_in_display(how, private) + no_mcd(0) + self.assertNotIn(str(TEXT_SIZE_CODE), s.historybuf.line(0).as_ansi()) + # Insert lines s.reset() multicell(s, 'a', scale=2)