[packages/tmux] Rel 2. Backport sync output and wide character fixes.

arekm arekm at pld-linux.org
Sat Nov 7 13:25:22 CET 2026


commit 3dc8a9d2f99e2e931d4633f0d69436c4a695c0b0
Author: Arkadiusz Miśkiewicz <arekm at maven.pl>
Date:   Sat Nov 7 13:23:52 2026 +0100

    Rel 2. Backport sync output and wide character fixes.
    
    PR 5758 (unmerged), 5611, 5754, 5779, 5819 and leak fixes from master.

 tmux-clear-padding-el-ed.patch       |  53 +++++
 tmux-clear-padding-ich-dch-ech.patch | 259 ++++++++++++++++++++
 tmux-combine-write-path.patch        | 183 ++++++++++++++
 tmux-dl-il-scroll-region.patch       | 123 ++++++++++
 tmux-memory-leaks.patch              |  90 +++++++
 tmux-sync-dirty-cells.patch          | 447 +++++++++++++++++++++++++++++++++++
 tmux-sync-scroll.patch               | 327 +++++++++++++++++++++++++
 tmux.spec                            |  18 +-
 8 files changed, 1498 insertions(+), 2 deletions(-)
---
diff --git a/tmux.spec b/tmux.spec
index 292b88c..35572a9 100644
--- a/tmux.spec
+++ b/tmux.spec
@@ -9,7 +9,7 @@ Summary(hu.UTF-8):	tmux egy terminál-sokszorozó
 Summary(pl.UTF-8):	tmux - multiplekser terminali
 Name:		tmux
 Version:	3.8
-Release:	1
+Release:	2
 License:	ISC
 Group:		Applications/Terminal
 #Source0Download: https://github.com/tmux/tmux/releases
@@ -21,7 +21,14 @@ Source2:	https://raw.githubusercontent.com/keith/tmux.vim/master/syntax/tmux.vim
 Source3:	https://raw.githubusercontent.com/imomaliev/tmux-bash-completion/master/completions/tmux
 # Source3-md5:	376dc7062c0e575ff98767747cb23554
 Source4:	tmux at .service
-URL:		http://tmux.github.io/
+Patch0:		%{name}-dl-il-scroll-region.patch
+Patch1:		%{name}-combine-write-path.patch
+Patch2:		%{name}-clear-padding-ich-dch-ech.patch
+Patch3:		%{name}-clear-padding-el-ed.patch
+Patch4:		%{name}-sync-scroll.patch
+Patch5:		%{name}-sync-dirty-cells.patch
+Patch6:		%{name}-memory-leaks.patch
+URL:		https://tmux.github.io/
 BuildRequires:	bison
 BuildRequires:	libevent-devel
 BuildRequires:	libutempter-devel
@@ -80,6 +87,13 @@ Ten pakiet dostarcza bashowe dopełnianie składni dla polecenia tmux.
 
 %prep
 %setup -q
+%patch -P0 -p1
+%patch -P1 -p1
+%patch -P2 -p1
+%patch -P3 -p1
+%patch -P4 -p1
+%patch -P5 -p1
+%patch -P6 -p1
 
 %build
 [ ! -x configure ] && ./autogen.sh
diff --git a/tmux-clear-padding-el-ed.patch b/tmux-clear-padding-el-ed.patch
new file mode 100644
index 0000000..d06a713
--- /dev/null
+++ b/tmux-clear-padding-el-ed.patch
@@ -0,0 +1,53 @@
+Clear tabs and wide characters cut by EL and ED instead of leaving half of
+them in the grid.
+https://github.com/tmux/tmux/issues/5819
+https://github.com/tmux/tmux/commit/4a3176186eaa60412db26d5716edc8642a6f0fa0 (tests: ad1000c9)
+
+diff --git a/screen-write.c b/screen-write.c
+index 9bd868fe..a429e39a 100644
+--- a/screen-write.c
++++ b/screen-write.c
+@@ -1666,6 +1666,7 @@ screen_write_clearendofline(struct screen_write_ctx *ctx, u_int bg)
+ 		ctx->wp->flags |= PANE_REDRAW;
+ #endif
+ 
++	screen_write_clear_padding(ctx, s->cx);
+ 	grid_view_clear(s->grid, s->cx, s->cy, sx - s->cx, 1, bg);
+ 
+ 	ci->x = s->cx;
+@@ -1695,8 +1696,10 @@ screen_write_clearstartofline(struct screen_write_ctx *ctx, u_int bg)
+ 
+ 	if (s->cx > sx - 1)
+ 		grid_view_clear(s->grid, 0, s->cy, sx, 1, bg);
+-	else
++	else {
++		screen_write_clear_padding(ctx, s->cx + 1);
+ 		grid_view_clear(s->grid, 0, s->cy, s->cx + 1, 1, bg);
++	}
+ 
+ 	ci->x = 0;
+ 	ci->used = s->cx + 1;
+@@ -1935,8 +1938,10 @@ screen_write_clearendofscreen(struct screen_write_ctx *ctx, u_int bg)
+ 	    options_get_number(ctx->wp->options, "scroll-on-clear"))
+ 		grid_view_clear_history(gd, bg);
+ 	else {
+-		if (s->cx <= sx - 1)
++		if (s->cx <= sx - 1) {
++			screen_write_clear_padding(ctx, s->cx);
+ 			grid_view_clear(gd, s->cx, s->cy, sx - s->cx, 1, bg);
++		}
+ 		grid_view_clear(gd, 0, s->cy + 1, sx, sy - (s->cy + 1), bg);
+ 	}
+ 
+@@ -2012,8 +2017,10 @@ screen_write_clearstartofscreen(struct screen_write_ctx *ctx, u_int bg)
+ 		grid_view_clear(s->grid, 0, 0, sx, s->cy, bg);
+ 	if (s->cx > sx - 1)
+ 		grid_view_clear(s->grid, 0, s->cy, sx, 1, bg);
+-	else
++	else {
++		screen_write_clear_padding(ctx, s->cx + 1);
+ 		grid_view_clear(s->grid, 0, s->cy, s->cx + 1, 1, bg);
++	}
+ 
+ 	screen_write_collect_clear(ctx, 0, s->cy);
+ 	screen_write_collect_flush(ctx, 0, __func__);
diff --git a/tmux-clear-padding-ich-dch-ech.patch b/tmux-clear-padding-ich-dch-ech.patch
new file mode 100644
index 0000000..29fd1f5
--- /dev/null
+++ b/tmux-clear-padding-ich-dch-ech.patch
@@ -0,0 +1,259 @@
+Clear tabs and wide characters cut by ICH, DCH, ECH and insert mode instead
+of leaving half of them in the grid.
+https://github.com/tmux/tmux/issues/5779
+https://github.com/tmux/tmux/commit/b03b71fd4a662e9e24814db16e9beb4ba4721026 (tests: 2f36533e)
+
+diff --git a/grid-view.c b/grid-view.c
+index 5659d576..ec8dbf52 100644
+--- a/grid-view.c
++++ b/grid-view.c
+@@ -1,4 +1,4 @@
+-/* $OpenBSD: grid-view.c,v 1.39 2026/08/03 12:58:53 nicm Exp $ */
++/* $OpenBSD: grid-view.c,v 1.40 2026/10/20 12:47:57 nicm Exp $ */
+ 
+ /*
+  * Copyright (c) 2008 Nicholas Marriott <nicholas.marriott at gmail.com>
+@@ -203,10 +203,8 @@ grid_view_insert_cells(struct grid *gd, u_int px, u_int py, u_int nx, u_int bg)
+ 
+ 	sx = grid_view_x(gd, gd->sx);
+ 
+-	if (px >= sx - 1)
+-		grid_clear(gd, px, py, 1, 1, bg);
+-	else
+-		grid_move_cells(gd, px + nx, px, py, sx - px - nx, bg);
++	grid_move_cells(gd, px + nx, px, py, sx - px - nx, bg);
++	grid_clear(gd, px, py, nx, 1, bg);
+ }
+ 
+ /* Delete characters. */
+diff --git a/screen-write.c b/screen-write.c
+index 64ad3c6b..9bd868fe 100644
+--- a/screen-write.c
++++ b/screen-write.c
+@@ -34,8 +34,7 @@ static void	screen_write_collect_clear(struct screen_write_ctx *, u_int,
+ static void	screen_write_collect_scroll(struct screen_write_ctx *, u_int);
+ static void	screen_write_collect_flush(struct screen_write_ctx *, int,
+ 		    const char *);
+-static int	screen_write_overwrite(struct screen_write_ctx *,
+-		    struct grid_cell *, u_int);
++static void	screen_write_clear_cell(struct grid *, u_int, u_int);
+ static int	screen_write_combine(struct screen_write_ctx *,
+ 		    const struct grid_cell *);
+ static void	screen_write_flush_dirty(struct window_pane *);
+@@ -1352,6 +1351,45 @@ screen_write_alignmenttest(struct screen_write_ctx *ctx)
+ 	screen_write_redraw_pane(ctx, &ttyctx);
+ }
+ 
++/*
++ * If the cell at px is padding, it belongs to a tab or wide character that
++ * starts to its left and is about to be cut in two by a write, insert, delete
++ * or clear. Replace it and its padding with blank cells, keeping the background.
++ */
++static int
++screen_write_clear_padding(struct screen_write_ctx *ctx, u_int px)
++{
++	struct screen		*s = ctx->s;
++	struct grid		*gd = s->grid;
++	struct grid_cell	 gc;
++	u_int			 xx, sx = screen_size_x(s);
++
++	if (px >= sx)
++		return (0);
++	grid_view_get_cell(gd, px, s->cy, &gc);
++	if (~gc.flags & GRID_FLAG_PADDING)
++		return (0);
++
++	/* Find the leading cell. */
++	for (xx = px; xx > 0; xx--) {
++		grid_view_get_cell(gd, xx - 1, s->cy, &gc);
++		if (~gc.flags & GRID_FLAG_PADDING) {
++			xx--;
++			break;
++		}
++	}
++
++	/* Clear it and the padding that follows. */
++	screen_write_clear_cell(gd, xx, s->cy);
++	while (++xx < sx) {
++		grid_view_get_cell(gd, xx, s->cy, &gc);
++		if (~gc.flags & GRID_FLAG_PADDING)
++			break;
++		screen_write_clear_cell(gd, xx, s->cy);
++	}
++	return (1);
++}
++
+ /* Insert nx characters. */
+ void
+ screen_write_insertcharacter(struct screen_write_ctx *ctx, u_int nx, u_int bg)
+@@ -1378,6 +1416,8 @@ screen_write_insertcharacter(struct screen_write_ctx *ctx, u_int nx, u_int bg)
+ 	screen_write_initctx(ctx, &ttyctx, 0, 1);
+ 	ttyctx.bg = bg;
+ 
++	screen_write_clear_padding(ctx, s->cx);
++	screen_write_clear_padding(ctx, screen_size_x(s) - nx);
+ 	grid_view_insert_cells(s->grid, s->cx, s->cy, nx, bg);
+ 
+ 	screen_write_collect_flush(ctx, 0, __func__);
+@@ -1419,6 +1459,8 @@ screen_write_deletecharacter(struct screen_write_ctx *ctx, u_int nx, u_int bg)
+ 	screen_write_initctx(ctx, &ttyctx, 0, 1);
+ 	ttyctx.bg = bg;
+ 
++	screen_write_clear_padding(ctx, s->cx);
++	screen_write_clear_padding(ctx, s->cx + nx);
+ 	grid_view_delete_cells(s->grid, s->cx, s->cy, nx, bg);
+ 
+ 	screen_write_collect_flush(ctx, 0, __func__);
+@@ -1460,6 +1502,8 @@ screen_write_clearcharacter(struct screen_write_ctx *ctx, u_int nx, u_int bg)
+ 	screen_write_initctx(ctx, &ttyctx, 0, 1);
+ 	ttyctx.bg = bg;
+ 
++	screen_write_clear_padding(ctx, s->cx);
++	screen_write_clear_padding(ctx, s->cx + nx);
+ 	grid_view_clear(s->grid, s->cx, s->cy, nx, 1, bg);
+ 
+ 	screen_write_collect_flush(ctx, 0, __func__);
+@@ -2622,11 +2666,11 @@ screen_write_cell1(struct screen_write_ctx *ctx, const struct grid_cell *gc,
+ 	const struct utf8_data	*ud = &gc->data;
+ 	struct grid_line	*gl;
+ 	struct grid_cell_entry	*gce;
+-	struct grid_cell	 tmp_gc, now_gc;
++	struct grid_cell	 tmp_gc;
+ 	struct tty_ctx		 ttyctx;
+ 	u_int			 sx = screen_size_x(s), sy = screen_size_y(s);
+ 	u_int			 width = ud->width, xx, not_wrap, i, n, vis;
+-	u_int			 insert_width = width;
++	u_int			 ins_width = width;
+ 	int			 selected, skip = 1, redraw = 0;
+ 	int			 yoff = 0, xoff = 0;
+ 	struct visible_ranges	*r;
+@@ -2648,7 +2692,7 @@ screen_write_cell1(struct screen_write_ctx *ctx, const struct grid_cell *gc,
+ 
+ 	/* Do not insert again for columns already written on this line. */
+ 	if (s->cx <= sx - width)
+-		insert_width -= old_width;
++		ins_width -= old_width;
+ 
+ 	/* Check this will fit on the current line and wrap if not. */
+ 	if ((s->mode & MODE_WRAP) && s->cx > sx - width) {
+@@ -2663,20 +2707,27 @@ screen_write_cell1(struct screen_write_ctx *ctx, const struct grid_cell *gc,
+ 		return;
+ 
+ 	/* If in insert mode, make space for the new columns. */
+-	if ((s->mode & MODE_INSERT) && insert_width != 0) {
+-		grid_view_insert_cells(s->grid, s->cx + width - insert_width,
+-		    s->cy, insert_width, 8);
++	if ((s->mode & MODE_INSERT) && ins_width != 0) {
++		if (screen_write_clear_padding(ctx, s->cx + width - ins_width))
++			redraw = 1;
++		if (screen_write_clear_padding(ctx, sx - ins_width))
++			redraw = 1;
++		grid_view_insert_cells(s->grid, s->cx + width - ins_width,
++		    s->cy, ins_width, 8);
+ 		skip = 0;
+ 	}
+ 	screen_write_initctx(ctx, &ttyctx, 0, 0);
+ 	if (old_width != 0)
+ 		ttyctx.flags |= TTY_CTX_CELL_INVALIDATE;
+ 
+-	/* Handle overwriting of UTF-8 characters. */
++	/* Clear tabs and wide characters cut by either end of the write. */
+ 	gl = grid_get_line(s->grid, s->grid->hsize + s->cy);
+ 	if (gl->flags & GRID_LINE_EXTENDED) {
+-		grid_view_get_cell(gd, s->cx, s->cy, &now_gc);
+-		if (screen_write_overwrite(ctx, &now_gc, width)) {
++		if (screen_write_clear_padding(ctx, s->cx)) {
++			redraw = 1;
++			skip = 0;
++		}
++		if (screen_write_clear_padding(ctx, s->cx + width)) {
+ 			redraw = 1;
+ 			skip = 0;
+ 		}
+@@ -2750,13 +2801,13 @@ screen_write_cell1(struct screen_write_ctx *ctx, const struct grid_cell *gc,
+ 		screen_write_set_cursor(ctx, sx - not_wrap, -1);
+ 
+ 	/* Create space for character in insert mode. */
+-	if ((s->mode & MODE_INSERT) && insert_width != 0) {
++	if ((s->mode & MODE_INSERT) && ins_width != 0) {
+ 		screen_write_collect_flush(ctx, 0, __func__);
+-		ttyctx.ocx += width - insert_width;
+-		ttyctx.n = insert_width;
++		ttyctx.ocx += width - ins_width;
++		ttyctx.n = ins_width;
+ 		if (screen_write_should_draw_line(ctx, s->cy))
+ 			tty_write(tty_cmd_insertcharacter, &ttyctx);
+-		ttyctx.ocx -= width - insert_width;
++		ttyctx.ocx -= width - ins_width;
+ 	}
+ 
+ 	/* If not writing, done now. */
+@@ -2958,68 +3009,6 @@ screen_write_combine(struct screen_write_ctx *ctx, const struct grid_cell *gc)
+ 	return (1);
+ }
+ 
+-/*
+- * UTF-8 wide characters are a bit of an annoyance. They take up more than one
+- * cell on the screen, so following cells must not be drawn by marking them as
+- * padding.
+- *
+- * So far, so good. The problem is, when overwriting a padding cell, or a UTF-8
+- * character, it is necessary to also overwrite any other cells which covered
+- * by the same character.
+- */
+-static int
+-screen_write_overwrite(struct screen_write_ctx *ctx, struct grid_cell *gc,
+-    u_int width)
+-{
+-	struct screen		*s = ctx->s;
+-	struct grid		*gd = s->grid;
+-	struct grid_cell	 tmp_gc;
+-	u_int			 xx;
+-	int			 done = 0;
+-
+-	if (gc->flags & GRID_FLAG_PADDING) {
+-		/*
+-		 * A padding cell, so clear any following and leading padding
+-		 * cells back to the character. Don't overwrite the current
+-		 * cell as that happens later anyway.
+-		 */
+-		xx = s->cx + 1;
+-		while (--xx > 0) {
+-			grid_view_get_cell(gd, xx, s->cy, &tmp_gc);
+-			if (~tmp_gc.flags & GRID_FLAG_PADDING)
+-				break;
+-			log_debug("%s: padding at %u,%u", __func__, xx, s->cy);
+-			screen_write_clear_cell(gd, xx, s->cy);
+-		}
+-
+-		/* Overwrite the character at the start of this padding. */
+-		log_debug("%s: character at %u,%u", __func__, xx, s->cy);
+-		screen_write_clear_cell(gd, xx, s->cy);
+-		done = 1;
+-	}
+-
+-	/*
+-	 * Overwrite any padding cells that belong to any UTF-8 characters
+-	 * we'll be overwriting with the current character.
+-	 */
+-	if (width != 1 ||
+-	    gc->data.width != 1 ||
+-	    gc->flags & GRID_FLAG_PADDING) {
+-		xx = s->cx + width - 1;
+-		while (++xx < screen_size_x(s)) {
+-			grid_view_get_cell(gd, xx, s->cy, &tmp_gc);
+-			if (~tmp_gc.flags & GRID_FLAG_PADDING)
+-				break;
+-			log_debug("%s: overwrite at %u,%u", __func__, xx,
+-			    s->cy);
+-			screen_write_clear_cell(gd, xx, s->cy);
+-			done = 1;
+-		}
+-	}
+-
+-	return (done);
+-}
+-
+ /* Set external clipboard. */
+ void
+ screen_write_setselection(struct screen_write_ctx *ctx, const char *clip,
diff --git a/tmux-combine-write-path.patch b/tmux-combine-write-path.patch
new file mode 100644
index 0000000..1a35ba3
--- /dev/null
+++ b/tmux-combine-write-path.patch
@@ -0,0 +1,183 @@
+Write a combined (forced wide) character through the normal cell path, so
+overlapping wide characters are cleared; needed by the padding fixes.
+https://github.com/tmux/tmux/commit/49ea3a57a27b58bd923fd53a21a044cef82182eb
+
+diff --git a/screen-write.c b/screen-write.c
+index d8a8a56d..64ad3c6b 100644
+--- a/screen-write.c
++++ b/screen-write.c
+@@ -2611,9 +2611,10 @@ screen_write_collect_add(struct screen_write_ctx *ctx,
+ 	ctx->s->write_list[s->cy].data[s->cx + ci->used++] = gc->data.data[0];
+ }
+ 
+-/* Write cell data. */
+-void
+-screen_write_cell(struct screen_write_ctx *ctx, const struct grid_cell *gc)
++/* Write a cell, replacing any columns already written for its base. */
++static void
++screen_write_cell1(struct screen_write_ctx *ctx, const struct grid_cell *gc,
++    u_int old_width)
+ {
+ 	struct screen		*s = ctx->s;
+ 	struct window_pane	*wp = ctx->wp;
+@@ -2625,17 +2626,14 @@ screen_write_cell(struct screen_write_ctx *ctx, const struct grid_cell *gc)
+ 	struct tty_ctx		 ttyctx;
+ 	u_int			 sx = screen_size_x(s), sy = screen_size_y(s);
+ 	u_int			 width = ud->width, xx, not_wrap, i, n, vis;
++	u_int			 insert_width = width;
+ 	int			 selected, skip = 1, redraw = 0;
+ 	int			 yoff = 0, xoff = 0;
+ 	struct visible_ranges	*r;
+ 	struct visible_range	*ri;
+ 
+-	/* Ignore padding cells. */
+-	if (gc->flags & GRID_FLAG_PADDING)
+-		return;
+-
+-	/* Get the previous cell to check for combining. */
+-	if (screen_write_combine(ctx, gc) != 0)
++	/* If this character is wider than the screen, ignore it. */
++	if (width > sx)
+ 		return;
+ 
+ 	/* Flush any existing scrolling. */
+@@ -2644,14 +2642,13 @@ screen_write_cell(struct screen_write_ctx *ctx, const struct grid_cell *gc)
+ 	/* If this character doesn't fit, ignore it. */
+ 	if ((~s->mode & MODE_WRAP) &&
+ 	    width > 1 &&
+-	    (width > sx || (s->cx != sx && s->cx > sx - width)))
++	    s->cx != sx &&
++	    s->cx > sx - width)
+ 		return;
+ 
+-	/* If in insert mode, make space for the cells. */
+-	if (s->mode & MODE_INSERT) {
+-		grid_view_insert_cells(s->grid, s->cx, s->cy, width, 8);
+-		skip = 0;
+-	}
++	/* Do not insert again for columns already written on this line. */
++	if (s->cx <= sx - width)
++		insert_width -= old_width;
+ 
+ 	/* Check this will fit on the current line and wrap if not. */
+ 	if ((s->mode & MODE_WRAP) && s->cx > sx - width) {
+@@ -2664,7 +2661,16 @@ screen_write_cell(struct screen_write_ctx *ctx, const struct grid_cell *gc)
+ 	/* Sanity check cursor position. */
+ 	if (s->cx > sx - width || s->cy > sy - 1)
+ 		return;
++
++	/* If in insert mode, make space for the new columns. */
++	if ((s->mode & MODE_INSERT) && insert_width != 0) {
++		grid_view_insert_cells(s->grid, s->cx + width - insert_width,
++		    s->cy, insert_width, 8);
++		skip = 0;
++	}
+ 	screen_write_initctx(ctx, &ttyctx, 0, 0);
++	if (old_width != 0)
++		ttyctx.flags |= TTY_CTX_CELL_INVALIDATE;
+ 
+ 	/* Handle overwriting of UTF-8 characters. */
+ 	gl = grid_get_line(s->grid, s->grid->hsize + s->cy);
+@@ -2738,17 +2744,19 @@ screen_write_cell(struct screen_write_ctx *ctx, const struct grid_cell *gc)
+ 	 * replace it.
+ 	 */
+ 	not_wrap = !(s->mode & MODE_WRAP);
+-	if (s->cx <= sx - not_wrap - width)
++	if (width <= sx - not_wrap && s->cx <= sx - not_wrap - width)
+ 		screen_write_set_cursor(ctx, s->cx + width, -1);
+ 	else
+ 		screen_write_set_cursor(ctx, sx - not_wrap, -1);
+ 
+ 	/* Create space for character in insert mode. */
+-	if (s->mode & MODE_INSERT) {
++	if ((s->mode & MODE_INSERT) && insert_width != 0) {
+ 		screen_write_collect_flush(ctx, 0, __func__);
+-		ttyctx.n = width;
++		ttyctx.ocx += width - insert_width;
++		ttyctx.n = insert_width;
+ 		if (screen_write_should_draw_line(ctx, s->cy))
+ 			tty_write(tty_cmd_insertcharacter, &ttyctx);
++		ttyctx.ocx -= width - insert_width;
+ 	}
+ 
+ 	/* If not writing, done now. */
+@@ -2795,6 +2803,17 @@ screen_write_cell(struct screen_write_ctx *ctx, const struct grid_cell *gc)
+ 	}
+ }
+ 
++/* Write cell data. */
++void
++screen_write_cell(struct screen_write_ctx *ctx, const struct grid_cell *gc)
++{
++	if (gc->flags & GRID_FLAG_PADDING)
++		return;
++	if (screen_write_combine(ctx, gc) != 0)
++		return;
++	screen_write_cell1(ctx, gc, 0);
++}
++
+ /* Combine a UTF-8 zero-width character onto the previous if necessary. */
+ static int
+ screen_write_combine(struct screen_write_ctx *ctx, const struct grid_cell *gc)
+@@ -2805,7 +2824,8 @@ screen_write_combine(struct screen_write_ctx *ctx, const struct grid_cell *gc)
+ 	const struct utf8_data	*ud = &gc->data;
+ 	struct options		*oo = global_options;
+ 	u_int			 i, n, cx = s->cx, cy = s->cy, vis;
+-	struct grid_cell	 last;
++	u_int			 sx = screen_size_x(s);
++	struct grid_cell	 last, tmp_gc;
+ 	struct tty_ctx		 ttyctx;
+ 	int			 force_wide = 0, zero_width = 0;
+ 	int			 xoff = 0, yoff = 0;
+@@ -2886,16 +2906,26 @@ screen_write_combine(struct screen_write_ctx *ctx, const struct grid_cell *gc)
+ 
+ 	/* Force the width to 2 for modifiers and variation selector. */
+ 	if (last.data.width == 1 && force_wide) {
++		if (sx < 2 || (cx == sx && (~s->mode & MODE_WRAP)))
++			return (1);
+ 		last.data.width = 2;
+-		n = 2;
+-		cx++;
+-	} else
+-		force_wide = 0;
++
++		/* Clear the old character before moving to the next line. */
++		if (cx == sx) {
++			memcpy(&tmp_gc, &last, sizeof tmp_gc);
++			utf8_set(&tmp_gc.data, ' ');
++			screen_write_set_cursor(ctx, cx - 1, cy);
++			screen_write_cell1(ctx, &tmp_gc, 1);
++		}
++
++		/* Write through the normal path. */
++		screen_write_set_cursor(ctx, cx - 1, cy);
++		screen_write_cell1(ctx, &last, 1);
++		return (1);
++	}
+ 
+ 	/* Set the new cell. */
+ 	grid_view_set_cell(gd, cx - n, cy, &last);
+-	if (force_wide)
+-		grid_view_set_padding(gd, cx - 1, cy, last.bg);
+ 
+ 	/*
+ 	 * Check if all of this character is visible. No character will be
+@@ -2917,17 +2947,10 @@ screen_write_combine(struct screen_write_ctx *ctx, const struct grid_cell *gc)
+ 		return (1);
+ 	}
+ 
+-	/*
+-	 * Redraw the combined cell. If forcing the cell to width 2, reset the
+-	 * cached cursor position in the tty, since we don't really know
+-	 * whether the terminal thought the character was width 1 or width 2
+-	 * and what it is going to do now.
+-	 */
++	/* Redraw the combined cell. */
+ 	screen_write_set_cursor(ctx, cx - n, cy);
+ 	screen_write_initctx(ctx, &ttyctx, 0, 0);
+ 	ttyctx.cell = &last;
+-	if (force_wide)
+-		ttyctx.flags |= TTY_CTX_CELL_INVALIDATE;
+ 	if (screen_write_should_draw_line(ctx, cy))
+ 		tty_write(tty_cmd_cell, &ttyctx);
+ 	screen_write_set_cursor(ctx, cx, cy);
diff --git a/tmux-dl-il-scroll-region.patch b/tmux-dl-il-scroll-region.patch
new file mode 100644
index 0000000..75f3634
--- /dev/null
+++ b/tmux-dl-il-scroll-region.patch
@@ -0,0 +1,123 @@
+Ignore IL and DL outside the scroll region like xterm; in a synchronized
+update the client kept the old lines.
+https://github.com/tmux/tmux/issues/5754
+https://github.com/tmux/tmux/commit/5965228c1f20a835ca9bd3bdf5152b40e531d81c
+
+diff --git a/screen-write.c b/screen-write.c
+index d7bc6b2a..d8a8a56d 100644
+--- a/screen-write.c
++++ b/screen-write.c
+@@ -1482,40 +1482,18 @@ screen_write_insertline(struct screen_write_ctx *ctx, u_int ny, u_int bg)
+ 	struct screen	*s = ctx->s;
+ 	struct grid	*gd = s->grid;
+ 	struct tty_ctx	 ttyctx;
+-	u_int		 sy = screen_size_y(s);
+ 
+ 	if (ny == 0)
+ 		ny = 1;
+ 
+ #ifdef ENABLE_SIXEL
+-	if (image_check_line(s, s->cy, sy - s->cy) && ctx->wp != NULL)
++	if (image_check_line(s, s->cy, screen_size_y(s) - s->cy) &&
++	    ctx->wp != NULL)
+ 		ctx->wp->flags |= PANE_REDRAW;
+ #endif
+ 
+-	if (s->cy < s->rupper || s->cy > s->rlower) {
+-		if (ny > sy - s->cy)
+-			ny = sy - s->cy;
+-		if (ny == 0)
+-			return;
+-
+-		screen_write_initctx(ctx, &ttyctx, 1, 1);
+-		ttyctx.bg = bg;
+-
+-		grid_view_insert_lines(gd, s->cy, ny, bg);
+-
+-		screen_write_collect_flush(ctx, 0, __func__);
+-		ttyctx.n = ny;
+-
+-		if (!screen_write_should_draw_lines(ctx, s->cy, sy - s->cy))
+-			return;
+-		if (~ttyctx.flags & TTY_CTX_PANE_OBSCURED || ctx->wp == NULL) {
+-			tty_write(tty_cmd_insertline, &ttyctx);
+-			return;
+-		}
+-
+-		screen_write_redraw_pane(ctx, &ttyctx);
++	if (s->cy < s->rupper || s->cy > s->rlower)
+ 		return;
+-	}
+ 
+ 	if (ny > s->rlower + 1 - s->cy)
+ 		ny = s->rlower + 1 - s->cy;
+@@ -1525,10 +1503,7 @@ screen_write_insertline(struct screen_write_ctx *ctx, u_int ny, u_int bg)
+ 	screen_write_initctx(ctx, &ttyctx, 1, 1);
+ 	ttyctx.bg = bg;
+ 
+-	if (s->cy < s->rupper || s->cy > s->rlower)
+-		grid_view_insert_lines(gd, s->cy, ny, bg);
+-	else
+-		grid_view_insert_lines_region(gd, s->rlower, s->cy, ny, bg);
++	grid_view_insert_lines_region(gd, s->rlower, s->cy, ny, bg);
+ 
+ 	screen_write_collect_flush(ctx, 0, __func__);
+ 	ttyctx.n = ny;
+@@ -1550,41 +1525,19 @@ screen_write_deleteline(struct screen_write_ctx *ctx, u_int ny, u_int bg)
+ 	struct screen	*s = ctx->s;
+ 	struct grid	*gd = s->grid;
+ 	struct tty_ctx	 ttyctx;
+-	u_int		 sy = screen_size_y(s), ry;
++	u_int		 ry;
+ 
+ 	if (ny == 0)
+ 		ny = 1;
+ 
+ #ifdef ENABLE_SIXEL
+-	if (image_check_line(s, s->cy, sy - s->cy) && ctx->wp != NULL)
++	if (image_check_line(s, s->cy, screen_size_y(s) - s->cy) &&
++	    ctx->wp != NULL)
+ 		ctx->wp->flags |= PANE_REDRAW;
+ #endif
+ 
+-	if (s->cy < s->rupper || s->cy > s->rlower) {
+-		if (ny > sy - s->cy)
+-			ny = sy - s->cy;
+-		if (ny == 0)
+-			return;
+-
+-		screen_write_initctx(ctx, &ttyctx, 1, 1);
+-		ttyctx.bg = bg;
+-
+-		grid_view_delete_lines(gd, s->cy, ny, bg);
+-
+-		screen_write_collect_flush(ctx, 0, __func__);
+-		ttyctx.n = ny;
+-
+-		ry = s->rlower + 1 - s->rupper;
+-		if (!screen_write_should_draw_lines(ctx, s->rupper, ry))
+-			return;
+-		if (~ttyctx.flags & TTY_CTX_PANE_OBSCURED || ctx->wp == NULL) {
+-			tty_write(tty_cmd_deleteline, &ttyctx);
+-			return;
+-		}
+-
+-		screen_write_redraw_pane(ctx, &ttyctx);
++	if (s->cy < s->rupper || s->cy > s->rlower)
+ 		return;
+-	}
+ 
+ 	ry = s->rlower + 1 - s->cy;
+ 	if (ny > ry)
+@@ -1595,10 +1548,7 @@ screen_write_deleteline(struct screen_write_ctx *ctx, u_int ny, u_int bg)
+ 	screen_write_initctx(ctx, &ttyctx, 1, 1);
+ 	ttyctx.bg = bg;
+ 
+-	if (s->cy < s->rupper || s->cy > s->rlower)
+-		grid_view_delete_lines(gd, s->cy, ny, bg);
+-	else
+-		grid_view_delete_lines_region(gd, s->rlower, s->cy, ny, bg);
++	grid_view_delete_lines_region(gd, s->rlower, s->cy, ny, bg);
+ 
+ 	screen_write_collect_flush(ctx, 0, __func__);
+ 	ttyctx.n = ny;
diff --git a/tmux-memory-leaks.patch b/tmux-memory-leaks.patch
new file mode 100644
index 0000000..f0f4094
--- /dev/null
+++ b/tmux-memory-leaks.patch
@@ -0,0 +1,90 @@
+Fix memory leaks in hooks, command dispatch, customize mode and terminal
+capability removal.
+https://github.com/tmux/tmux/commit/173e6a9e05e9e6a81d25b56a1899e3be96822d2f
+
+diff --git a/hooks.c b/hooks.c
+index acc11704..5cef0b14 100644
+--- a/hooks.c
++++ b/hooks.c
+@@ -1,4 +1,4 @@
+-/* $OpenBSD: hooks.c,v 1.16 2026/07/27 19:15:58 nicm Exp $ */
++/* $OpenBSD: hooks.c,v 1.17 2026/10/15 08:21:11 nicm Exp $ */
+ 
+ /*
+  * Copyright (c) 2026 Nicholas Marriott <nicholas.marriott at gmail.com>
+@@ -164,6 +164,7 @@ hooks_insert(struct cmdq_item *item, struct hooks_data *hd)
+ 			break;
+ 		case CMD_PARSE_SUCCESS:
+ 			hooks_insert_one(item, hd, pr->cmdlist, state);
++			cmd_list_free(pr->cmdlist);
+ 			break;
+ 		}
+ 	} else {
+@@ -178,10 +179,12 @@ hooks_insert(struct cmdq_item *item, struct hooks_data *hd)
+ 						cmdq_error(item, "%s",
+ 						    pr->error);
+ 					}
++					free(pr->error);
+ 					break;
+ 				case CMD_PARSE_SUCCESS:
+ 					item = hooks_insert_one(item, hd,
+ 					    pr->cmdlist, state);
++					cmd_list_free(pr->cmdlist);
+ 					break;
+ 				}
+ 			} else {
+diff --git a/server-client.c b/server-client.c
+index 95088099..eccd1e3e 100644
+--- a/server-client.c
++++ b/server-client.c
+@@ -2871,6 +2871,8 @@ server_client_dispatch_command(struct client *c, struct imsg *imsg)
+ 	} else {
+ 		values = args_from_vector(argc, argv);
+ 		pr = cmd_parse_from_arguments(values, argc, NULL);
++		args_free_values(values, argc);
++		free(values);
+ 		switch (pr->status) {
+ 		case CMD_PARSE_ERROR:
+ 			cause = pr->error;
+@@ -2878,8 +2880,6 @@ server_client_dispatch_command(struct client *c, struct imsg *imsg)
+ 		case CMD_PARSE_SUCCESS:
+ 			break;
+ 		}
+-		args_free_values(values, argc);
+-		free(values);
+ 		cmd_free_argv(argc, argv);
+ 		if ((c->flags & CLIENT_READONLY) &&
+ 		    !cmd_list_all_have(pr->cmdlist, CMD_READONLY)) {
+diff --git a/tty-term.c b/tty-term.c
+index 1ac078cd..a42c0713 100644
+--- a/tty-term.c
++++ b/tty-term.c
+@@ -413,6 +413,8 @@ tty_term_apply(struct tty_term *term, const char *capabilities, int quiet,
+ 				continue;
+ 
+ 			if (remove) {
++				if (code->type == TTYCODE_STRING)
++					free(code->value.string);
+ 				code->type = TTYCODE_NONE;
+ 				continue;
+ 			}
+diff --git a/window-customize.c b/window-customize.c
+index 1bf311ff..ff234ff6 100644
+--- a/window-customize.c
++++ b/window-customize.c
+@@ -1430,6 +1430,7 @@ monitor:
+ 	if (wo != NULL && options_owner(o) != wo) {
+ 		parent = options_get_only(wo, name);
+ 		if (parent != NULL) {
++			free(value);
+ 			value = options_to_string(parent, NULL, 0);
+ 			xsnprintf(label, sizeof label,
+ 			    "Window value (from window %u): ", fs.wl->idx);
+@@ -1442,6 +1443,7 @@ monitor:
+ 	if (go != NULL && options_owner(o) != go) {
+ 		parent = options_get_only(go, name);
+ 		if (parent != NULL) {
++			free(value);
+ 			value = options_to_string(parent, NULL, 0);
+ 			if (!window_customize_write_value(ctx, s->cx, sx,
+ 			    sy - (s->cy - cy), 0, "Global value: ", "%s%s%s",
diff --git a/tmux-sync-dirty-cells.patch b/tmux-sync-dirty-cells.patch
new file mode 100644
index 0000000..27e35fa
--- /dev/null
+++ b/tmux-sync-dirty-cells.patch
@@ -0,0 +1,447 @@
+Redraw only the changed cells after a synchronized update instead of whole
+lines; one changed cell on a 200-column RGB line took 6977 bytes per frame.
+Upstream pull request, not merged yet:
+https://github.com/tmux/tmux/pull/5758
+commits 64fbe9c9, 0e22529c, c0fbac92, adcd1d42; redraw_cb keeps the
+one-argument signature tmux 3.8 has.
+
+diff --git a/hyperlinks.c b/hyperlinks.c
+index 1f2f3eaa..6f6dc278 100644
+--- a/hyperlinks.c
++++ b/hyperlinks.c
+@@ -31,6 +31,7 @@
+  *
+  * Each hyperlink and ID combination is assigned a number ("inner" in this
+  * file) which is stored in an extended grid cell and maps into a tree here.
++ * Numbers are shared by all screens and are not reused.
+  *
+  * Each URI has one inner number and one external ID (which tmux uses to send
+  * the hyperlink to the terminal) and one internal ID (which is received from
+@@ -44,6 +45,7 @@
+ #define MAX_HYPERLINK_URI 1024
+ 
+ static long long hyperlinks_next_external_id = 1;
++static u_int hyperlinks_next_inner = 1;
+ static u_int global_hyperlinks_count;
+ 
+ struct hyperlinks_uri {
+@@ -66,7 +68,6 @@ static struct hyperlinks_list global_hyperlinks =
+     TAILQ_HEAD_INITIALIZER(global_hyperlinks);
+ 
+ struct hyperlinks {
+-	u_int				next_inner;
+ 	struct hyperlinks_by_inner_tree	by_inner;
+ 	struct hyperlinks_by_uri_tree	by_uri;
+ 	u_int				references;
+@@ -171,10 +172,16 @@ hyperlinks_put(struct hyperlinks *hl, const char *uri_in,
+ 			return (hlu->inner);
+ 		}
+ 	}
++	/* Numbers are not reused, so once they run out add no more. */
++	if (hyperlinks_next_inner == 0) {
++		free(uri);
++		free(internal_id);
++		return (0);
++	}
+ 	xasprintf(&external_id, "tmux%llX", hyperlinks_next_external_id++);
+ 
+ 	hlu = xcalloc(1, sizeof *hlu);
+-	hlu->inner = hl->next_inner++;
++	hlu->inner = hyperlinks_next_inner++;
+ 	hlu->internal_id = internal_id;
+ 	hlu->external_id = external_id;
+ 	hlu->uri = uri;
+@@ -216,7 +223,6 @@ hyperlinks_init(void)
+ 	struct hyperlinks	*hl;
+ 
+ 	hl = xcalloc(1, sizeof *hl);
+-	hl->next_inner = 1;
+ 	RB_INIT(&hl->by_uri);
+ 	RB_INIT(&hl->by_inner);
+ 	hl->references = 1;
+diff --git a/screen-write.c b/screen-write.c
+index 05be53a4..0890cba2 100644
+--- a/screen-write.c
++++ b/screen-write.c
+@@ -208,50 +208,85 @@ screen_write_pane_is_obscured(struct screen_write_ctx *ctx)
+ 	return (0);
+ }
+ 
+-/* Allocate dirty lines for this screen size. */
+-static bitstr_t *
+-screen_write_sync_allocate_dirty(struct window_pane *wp, u_int sy)
++/* Mark cells x to x + nx - 1 on line y as dirty. */
++static void
++screen_write_sync_dirty_cells(struct window_pane *wp, u_int x, u_int y,
++    u_int nx)
+ {
+-	bitstr_t	*bs = wp->sync_dirty;
+-
+-	if (bs != NULL && wp->sync_dirty_size == sy)
+-		return (bs);
++	bitstr_t	**bs = &wp->sync_dirty[y];
+ 
+-	free(bs);
+-	bs = wp->sync_dirty = bit_alloc(sy);
+-	if (bs == NULL)
+-		fatal("bit_alloc failed");
+-	if (wp->sync_dirty_size != 0) {
+-		bit_nset(bs, 0, sy - 1);
+-		wp->sync_scrolled = 0;
++	if (*bs == NULL) {
++		*bs = bit_alloc(wp->sync_dirty_sx);
++		if (*bs == NULL)
++			fatal("bit_alloc failed");
+ 	}
+-	wp->sync_dirty_size = sy;
+-	return (bs);
++	bit_nset(*bs, x, x + nx - 1);
++}
++
++/* Mark lines y to y + ny - 1 as dirty. */
++static void
++screen_write_sync_dirty_lines(struct window_pane *wp, u_int y, u_int ny)
++{
++	u_int	yy;
++
++	for (yy = y; yy < y + ny && yy < wp->sync_dirty_sy; yy++)
++		screen_write_sync_dirty_cells(wp, 0, yy, wp->sync_dirty_sx);
++}
++
++/* Allocate dirty cells for this screen size, a bitmap for each line marked. */
++static bitstr_t **
++screen_write_sync_allocate_dirty(struct window_pane *wp, u_int sx, u_int sy)
++{
++	int	resized = (wp->sync_dirty != NULL);
++
++	if (resized && wp->sync_dirty_sx == sx && wp->sync_dirty_sy == sy)
++		return (wp->sync_dirty);
++
++	screen_write_sync_clear_dirty(wp);
++	wp->sync_dirty = xcalloc(sy, sizeof *wp->sync_dirty);
++	wp->sync_dirty_sx = sx;
++	wp->sync_dirty_sy = sy;
++	if (resized)
++		screen_write_sync_dirty_lines(wp, 0, sy);
++	return (wp->sync_dirty);
+ }
+ 
+-/* Should we draw to the TTY? */
++/* Should we draw these cells to the TTY? If syncing, mark them dirty. */
+ static int
+-screen_write_should_draw_lines(struct screen_write_ctx *ctx, u_int y, u_int ny)
++screen_write_should_draw_cells(struct screen_write_ctx *ctx, u_int px,
++    u_int py, u_int nx, u_int ny)
+ {
+ 	struct window_pane	*wp = ctx->wp;
+ 	struct screen		*s = ctx->s;
+-	u_int			 sy = screen_size_y(s);
+-	bitstr_t		*bs;
++	u_int			 sx = screen_size_x(s), sy = screen_size_y(s);
++	u_int			 y;
+ 
+ 	if (wp != NULL && (wp->flags & (PANE_REDRAW|PANE_DROP)))
+ 		return (0);
+ 	if (s->mode & MODE_SYNC) {
+-		if (wp != NULL && y < sy && ny != 0) {
+-			if (ny > sy - y)
+-				ny = sy - y;
+-			bs = screen_write_sync_allocate_dirty(wp, sy);
+-			bit_nset(bs, y, y + ny - 1);
++		if (wp != NULL && px < sx && py < sy && nx != 0 && ny != 0) {
++			if (nx > sx - px)
++				nx = sx - px;
++			if (ny > sy - py)
++				ny = sy - py;
++			screen_write_sync_allocate_dirty(wp, sx, sy);
++			for (y = py; y < py + ny; y++)
++				screen_write_sync_dirty_cells(wp, px, y, nx);
+ 		}
+ 		return (0);
+ 	}
+ 	return (1);
+ }
+ 
++/* Should we draw these lines to the TTY? */
++static int
++screen_write_should_draw_lines(struct screen_write_ctx *ctx, u_int y, u_int ny)
++{
++	u_int	sx = screen_size_x(ctx->s);
++
++	return (screen_write_should_draw_cells(ctx, 0, y, sx, ny));
++}
++
+ /* Should we draw this line to the TTY? */
+ static int
+ screen_write_should_draw_line(struct screen_write_ctx *ctx, u_int y)
+@@ -1221,10 +1256,13 @@ screen_write_cell_is_single(const struct grid_cell *gc)
+ 	return (1);
+ }
+ 
+-/* Redraw all visible cells on a line. */
++/*
++ * Redraw visible cells from px to px + nx - 1 on a line, writing a lone plain
++ * ASCII cell directly if single is set.
++ */
+ static void
+-screen_write_redraw_line(struct screen_write_ctx *ctx, struct tty_ctx *ttyctx,
+-    u_int yy)
++screen_write_redraw_cells(struct screen_write_ctx *ctx,
++    struct tty_ctx *ttyctx, u_int px, u_int yy, u_int nx, int single)
+ {
+ 	struct window_pane	*wp = ctx->wp;
+ 	struct screen		*s = ctx->s;
+@@ -1234,7 +1272,7 @@ screen_write_redraw_line(struct screen_write_ctx *ctx, struct tty_ctx *ttyctx,
+ 	struct visible_ranges	*r;
+ 	struct visible_range	*ri;
+ 
+-	r = window_visible_ranges(wp, xoff, yoff + yy, sx, NULL);
++	r = window_visible_ranges(wp, xoff + px, yoff + yy, nx, NULL);
+ 	for (i = 0; i < r->used; i++) {
+ 		ri = &r->ranges[i];
+ 		if (ri->nx == 0)
+@@ -1252,7 +1290,7 @@ screen_write_redraw_line(struct screen_write_ctx *ctx, struct tty_ctx *ttyctx,
+ 		ttyctx->ocx = cx;
+ 		ttyctx->ocy = yy;
+ 
+-		if (ttyctx->n != 1) {
++		if (ttyctx->n != 1 || !single) {
+ 			tty_write(tty_cmd_redrawline, ttyctx);
+ 			continue;
+ 		}
+@@ -1272,14 +1310,46 @@ screen_write_redraw_line(struct screen_write_ctx *ctx, struct tty_ctx *ttyctx,
+ 	}
+ }
+ 
+-/* Redraw dirty lines. */
++/* Redraw all visible cells on a line. */
++static void
++screen_write_redraw_line(struct screen_write_ctx *ctx, struct tty_ctx *ttyctx,
++    u_int yy)
++{
++	u_int	sx = screen_size_x(ctx->s);
++
++	screen_write_redraw_cells(ctx, ttyctx, 0, yy, sx, 1);
++}
++
++/* Redraw each run of dirty cells on a line, returning how many there were. */
++static u_int
++screen_write_sync_redraw_runs(struct screen_write_ctx *ctx,
++    struct tty_ctx *ttyctx, u_int y)
++{
++	bitstr_t	*bs = ctx->wp->sync_dirty[y];
++	u_int		 sx = screen_size_x(ctx->s), x, end, runs = 0;
++
++	if (bs == NULL)
++		return (0);
++	for (x = 0; x < sx; x++) {
++		if (!bit_test(bs, x))
++			continue;
++		for (end = x + 1; end < sx && bit_test(bs, end); end++)
++			/* nothing */;
++		screen_write_redraw_cells(ctx, ttyctx, x, y, end - x, 0);
++		runs++;
++		x = end;
++	}
++	return (runs);
++}
++
++/* Redraw dirty cells. */
+ static void
+ screen_write_sync_flush_dirty(struct window_pane *wp)
+ {
+ 	struct screen_write_ctx	 ctx;
+ 	struct tty_ctx		 ttyctx;
+ 	struct screen		*s = &wp->base;
+-	u_int			 y, sy = screen_size_y(s), lines = 0;
++	u_int			 y, sy = screen_size_y(s), n = 0;
+ 
+ 	if (wp->sync_dirty == NULL)
+ 		return;
+@@ -1291,14 +1361,10 @@ screen_write_sync_flush_dirty(struct window_pane *wp)
+ 		screen_write_sync_apply_scroll(&ctx, &ttyctx);
+ 
+ 	if (~wp->flags & PANE_REDRAW) {
+-		for (y = 0; y < sy; y++) {
+-			if (bit_test(wp->sync_dirty, y)) {
+-				screen_write_redraw_line(&ctx, &ttyctx, y);
+-				lines++;
+-			}
+-		}
++		for (y = 0; y < sy; y++)
++			n += screen_write_sync_redraw_runs(&ctx, &ttyctx, y);
+ 	}
+-	log_debug("%s: %%%u had %u dirty lines", __func__, wp->id, lines);
++	log_debug("%s: %%%u had %u dirty runs", __func__, wp->id, n);
+ 
+ 	screen_write_stop(&ctx);
+ 	screen_write_sync_clear_dirty(wp);
+@@ -1308,10 +1374,15 @@ screen_write_sync_flush_dirty(struct window_pane *wp)
+ void
+ screen_write_sync_clear_dirty(struct window_pane *wp)
+ {
++	u_int	y;
++
+ 	if (wp != NULL) {
++		for (y = 0; y < wp->sync_dirty_sy; y++)
++			free(wp->sync_dirty[y]);
+ 		free(wp->sync_dirty);
+ 		wp->sync_dirty = NULL;
+-		wp->sync_dirty_size = 0;
++		wp->sync_dirty_sx = 0;
++		wp->sync_dirty_sy = 0;
+ 		wp->sync_scrolled = 0;
+ 	}
+ }
+@@ -1322,9 +1393,10 @@ screen_write_sync_scroll_dirty(struct screen_write_ctx *ctx)
+ {
+ 	struct window_pane	*wp = ctx->wp;
+ 	struct screen		*s = ctx->s;
+-	u_int			 n = ctx->scrolled, y, sy = screen_size_y(s);
++	u_int			 n = ctx->scrolled, y, sx = screen_size_x(s);
++	u_int			 sy = screen_size_y(s);
+ 	u_int			 ry = s->rlower + 1 - s->rupper;
+-	bitstr_t		*bs;
++	bitstr_t		**bs;
+ 
+ 	if (n > ry)
+ 		n = ry;
+@@ -1337,7 +1409,7 @@ screen_write_sync_scroll_dirty(struct screen_write_ctx *ctx)
+ 	}
+ 	if (wp->flags & (PANE_REDRAW|PANE_DROP))
+ 		return;
+-	bs = screen_write_sync_allocate_dirty(wp, sy);
++	bs = screen_write_sync_allocate_dirty(wp, sx, sy);
+ 
+ 	if (wp->sync_scrolled != 0 &&
+ 	    (wp->sync_rupper != s->rupper ||
+@@ -1345,26 +1417,25 @@ screen_write_sync_scroll_dirty(struct screen_write_ctx *ctx)
+ 	    wp->sync_bg != ctx->bg)) {
+ 		log_debug("%s: %%%u region changed, redrawing all", __func__,
+ 		    wp->id);
+-		bit_nset(bs, 0, sy - 1);
++		screen_write_sync_dirty_lines(wp, 0, sy);
+ 		wp->sync_scrolled = 0;
+ 		return;
+ 	}
+ 
+ 	/* Keep dirty lines aligned with the scrolled grid. */
+-	for (y = s->rupper; y + n <= s->rlower; y++) {
+-		if (bit_test(bs, y + n))
+-			bit_set(bs, y);
+-		else
+-			bit_clear(bs, y);
+-	}
+-	bit_nset(bs, s->rlower + 1 - n, s->rlower);
++	for (y = s->rupper; y < s->rupper + n; y++)
++		free(bs[y]);
++	memmove(&bs[s->rupper], &bs[s->rupper + n], (ry - n) * sizeof *bs);
++	for (y = s->rlower + 1 - n; y <= s->rlower; y++)
++		bs[y] = NULL;
++	screen_write_sync_dirty_lines(wp, s->rlower + 1 - n, n);
+ 
+ 	wp->sync_rupper = s->rupper;
+ 	wp->sync_rlower = s->rlower;
+ 	wp->sync_bg = ctx->bg;
+ 	wp->sync_scrolled += n;
+ 	if (wp->sync_scrolled >= ry) {
+-		bit_nset(bs, s->rupper, s->rlower);
++		screen_write_sync_dirty_lines(wp, s->rupper, ry);
+ 		wp->sync_scrolled = 0;
+ 	}
+ 	window_pane_scrollbar_redraw(wp);
+@@ -1379,7 +1450,8 @@ screen_write_sync_redraw_cb(const struct tty_ctx *ttyctx)
+ {
+ 	struct window_pane	*wp = ttyctx->arg;
+ 
+-	bit_nset(wp->sync_dirty, wp->sync_rupper, wp->sync_rlower);
++	screen_write_sync_dirty_lines(wp, wp->sync_rupper,
++	    wp->sync_rlower + 1 - wp->sync_rupper);
+ }
+ 
+ /* Send the deferred scroll to the client. */
+@@ -1396,12 +1468,13 @@ screen_write_sync_apply_scroll(struct screen_write_ctx *ctx,
+ 	if (wp->flags & PANE_REDRAW)
+ 		return;
+ 	if (wp->sync_rlower >= sy || wp->sync_rupper > wp->sync_rlower) {
+-		bit_nset(wp->sync_dirty, 0, sy - 1);
++		screen_write_sync_dirty_lines(wp, 0, sy);
+ 		return;
+ 	}
+ 	if ((ttyctx->flags & TTY_CTX_PANE_OBSCURED) ||
+ 	    window_pane_scrollbar_overlay_visible(wp)) {
+-		bit_nset(wp->sync_dirty, wp->sync_rupper, wp->sync_rlower);
++		screen_write_sync_dirty_lines(wp, wp->sync_rupper,
++		    wp->sync_rlower + 1 - wp->sync_rupper);
+ 		return;
+ 	}
+ 
+@@ -1931,6 +2004,13 @@ screen_write_linefeed(struct screen_write_ctx *ctx, int wrapped, u_int bg)
+ 	if (wrapped)
+ 		gl->flags |= GRID_LINE_WRAPPED;
+ 
++	/* A terminal only joins wrapped lines if both are drawn in full. */
++	if (wrapped && (s->mode & MODE_SYNC)) {
++		screen_write_collect_flush(ctx, 1, __func__);
++		screen_write_should_draw_lines(ctx, s->cy,
++		    s->cy == s->rlower ? 1 : 2);
++	}
++
+ 	log_debug("%s: at %u,%u (region %u-%u)", __func__, s->cx, s->cy,
+ 	    rupper, rlower);
+ 
+@@ -2549,8 +2629,9 @@ screen_write_collect_flush(struct screen_write_ctx *ctx, int scroll_only,
+ 			screen_write_sync_scroll_dirty(ctx);
+ 		for (y = 0; y < screen_size_y(s); y++) {
+ 			cl = &s->write_list[y];
+-			if (!TAILQ_EMPTY(&cl->items))
+-				screen_write_should_draw_line(ctx, y);
++			TAILQ_FOREACH(ci, &cl->items, entry)
++				screen_write_should_draw_cells(ctx, ci->x, y,
++				    ci->used, 1);
+ 		}
+ 		goto discard;
+ 	}
+@@ -2941,7 +3022,13 @@ screen_write_cell1(struct screen_write_ctx *ctx, const struct grid_cell *gc,
+ 	}
+ 
+ 	/* If not writing, done now. */
+-	if (skip || !screen_write_should_draw_line(ctx, s->cy))
++	if (skip)
++		return;
++	if (redraw) {
++		if (!screen_write_should_draw_line(ctx, s->cy))
++			return;
++	} else if (!screen_write_should_draw_cells(ctx, ttyctx.ocx, s->cy,
++	    width, 1))
+ 		return;
+ 
+ 	/* Do a full line redraw if needed. */
+diff --git a/tmux.h b/tmux.h
+index ef0f9e63..367e6f29 100644
+--- a/tmux.h
++++ b/tmux.h
+@@ -1343,8 +1343,9 @@ struct window_pane {
+ #define PANE_FLOATOVERZOOM 0x200000
+ #define PANE_CLOSEONCANCEL 0x400000
+ 
+-	bitstr_t	*sync_dirty;
+-	u_int		 sync_dirty_size;
++	bitstr_t	**sync_dirty;
++	u_int		 sync_dirty_sx;
++	u_int		 sync_dirty_sy;
+ 	u_int		 sync_scrolled;
+ 	u_int		 sync_rupper;
+ 	u_int		 sync_rlower;
+diff --git a/tty-draw.c b/tty-draw.c
+index ee2a7198..c4969fcf 100644
+--- a/tty-draw.c
++++ b/tty-draw.c
+@@ -178,7 +178,6 @@ tty_draw_line(struct tty *tty, struct screen *s, u_int px, u_int py, u_int nx,
+ 	/* Start with the default cell as the last cell. */
+ 	memcpy(&last, &grid_default_cell, sizeof last);
+ 	last.bg = defaults->bg;
+-	tty_default_attributes(tty, 8, style_ctx);
+ 
+ 	/*
+ 	 * If there is padding at the start, we must have truncated a wide
diff --git a/tmux-sync-scroll.patch b/tmux-sync-scroll.patch
new file mode 100644
index 0000000..95ca15c
--- /dev/null
+++ b/tmux-sync-scroll.patch
@@ -0,0 +1,327 @@
+Send lines scrolled during a synchronized update to the client as a scroll
+instead of redrawing every line of the scroll region.
+https://github.com/tmux/tmux/issues/5611
+https://github.com/tmux/tmux/commit/7fc7f1bf693ae298253bb0cb9a2047c494ac32d5
+
+diff --git a/screen-redraw.c b/screen-redraw.c
+index 3fb612a7..a8a0842b 100644
+--- a/screen-redraw.c
++++ b/screen-redraw.c
+@@ -1,4 +1,4 @@
+-/* $OpenBSD: screen-redraw.c,v 1.161 2026/09/22 16:56:07 nicm Exp $ */
++/* $OpenBSD: screen-redraw.c,v 1.162 2026/09/24 11:19:39 nicm Exp $ */
+ 
+ /*
+  * Copyright (c) 2026 Nicholas Marriott <nicholas.marriott at gmail.com>
+@@ -1758,14 +1758,14 @@ redraw_draw(struct client *c, struct window_pane *wp, int flags)
+ 		if (wp != NULL) {
+ 			if (wp->base.mode & MODE_SYNC)
+ 				screen_write_stop_sync(wp);
+-			screen_write_clear_dirty(wp);
++			screen_write_sync_clear_dirty(wp);
+ 		} else {
+ 			TAILQ_FOREACH(loop, &scene->w->panes, entry) {
+ 				if (!window_pane_is_visible(loop))
+ 					continue;
+ 				if (loop->base.mode & MODE_SYNC)
+ 					screen_write_stop_sync(loop);
+-				screen_write_clear_dirty(loop);
++				screen_write_sync_clear_dirty(loop);
+ 			}
+ 		}
+ 	}
+diff --git a/screen-write.c b/screen-write.c
+index a429e39a..05be53a4 100644
+--- a/screen-write.c
++++ b/screen-write.c
+@@ -37,7 +37,9 @@ static void	screen_write_collect_flush(struct screen_write_ctx *, int,
+ static void	screen_write_clear_cell(struct grid *, u_int, u_int);
+ static int	screen_write_combine(struct screen_write_ctx *,
+ 		    const struct grid_cell *);
+-static void	screen_write_flush_dirty(struct window_pane *);
++static void	screen_write_sync_flush_dirty(struct window_pane *);
++static void	screen_write_sync_apply_scroll(struct screen_write_ctx *,
++		    struct tty_ctx *);
+ 
+ struct screen_write_citem {
+ 	u_int				x;
+@@ -206,6 +208,27 @@ screen_write_pane_is_obscured(struct screen_write_ctx *ctx)
+ 	return (0);
+ }
+ 
++/* Allocate dirty lines for this screen size. */
++static bitstr_t *
++screen_write_sync_allocate_dirty(struct window_pane *wp, u_int sy)
++{
++	bitstr_t	*bs = wp->sync_dirty;
++
++	if (bs != NULL && wp->sync_dirty_size == sy)
++		return (bs);
++
++	free(bs);
++	bs = wp->sync_dirty = bit_alloc(sy);
++	if (bs == NULL)
++		fatal("bit_alloc failed");
++	if (wp->sync_dirty_size != 0) {
++		bit_nset(bs, 0, sy - 1);
++		wp->sync_scrolled = 0;
++	}
++	wp->sync_dirty_size = sy;
++	return (bs);
++}
++
+ /* Should we draw to the TTY? */
+ static int
+ screen_write_should_draw_lines(struct screen_write_ctx *ctx, u_int y, u_int ny)
+@@ -219,21 +242,9 @@ screen_write_should_draw_lines(struct screen_write_ctx *ctx, u_int y, u_int ny)
+ 		return (0);
+ 	if (s->mode & MODE_SYNC) {
+ 		if (wp != NULL && y < sy && ny != 0) {
+-			bs = wp->sync_dirty;
+ 			if (ny > sy - y)
+ 				ny = sy - y;
+-			if (bs == NULL || wp->sync_dirty_size != sy) {
+-				if (bs != NULL && wp->sync_dirty_size != sy) {
+-					y = 0;
+-					ny = sy;
+-				}
+-				free(bs);
+-
+-				bs = wp->sync_dirty = bit_alloc(sy);
+-				if (bs == NULL)
+-					fatal("bit_alloc failed");
+-				wp->sync_dirty_size = sy;
+-			}
++			bs = screen_write_sync_allocate_dirty(wp, sy);
+ 			bit_nset(bs, y, y + ny - 1);
+ 		}
+ 		return (0);
+@@ -1023,7 +1034,7 @@ screen_write_sync_callback(__unused int fd, __unused short events, void *arg)
+ 
+ 	if (wp->base.mode & MODE_SYNC) {
+ 		wp->base.mode &= ~MODE_SYNC;
+-		screen_write_flush_dirty(wp);
++		screen_write_sync_flush_dirty(wp);
+ 	}
+ }
+ 
+@@ -1055,7 +1066,7 @@ screen_write_stop_sync(struct window_pane *wp)
+ 		evtimer_del(&wp->sync_timer);
+ 	wp->base.mode &= ~MODE_SYNC;
+ 
+-	screen_write_flush_dirty(wp);
++	screen_write_sync_flush_dirty(wp);
+ 
+ 	log_debug("%s: %%%u stopped sync mode", __func__, wp->id);
+ }
+@@ -1263,7 +1274,7 @@ screen_write_redraw_line(struct screen_write_ctx *ctx, struct tty_ctx *ttyctx,
+ 
+ /* Redraw dirty lines. */
+ static void
+-screen_write_flush_dirty(struct window_pane *wp)
++screen_write_sync_flush_dirty(struct window_pane *wp)
+ {
+ 	struct screen_write_ctx	 ctx;
+ 	struct tty_ctx		 ttyctx;
+@@ -1276,29 +1287,143 @@ screen_write_flush_dirty(struct window_pane *wp)
+ 	screen_write_start_pane(&ctx, wp, s);
+ 	screen_write_initctx(&ctx, &ttyctx, 1, 1);
+ 
+-	for (y = 0; y < sy; y++) {
+-		if (bit_test(wp->sync_dirty, y)) {
+-			screen_write_redraw_line(&ctx, &ttyctx, y);
+-			lines++;
++	if (wp->sync_scrolled != 0)
++		screen_write_sync_apply_scroll(&ctx, &ttyctx);
++
++	if (~wp->flags & PANE_REDRAW) {
++		for (y = 0; y < sy; y++) {
++			if (bit_test(wp->sync_dirty, y)) {
++				screen_write_redraw_line(&ctx, &ttyctx, y);
++				lines++;
++			}
+ 		}
+ 	}
+ 	log_debug("%s: %%%u had %u dirty lines", __func__, wp->id, lines);
+ 
+ 	screen_write_stop(&ctx);
+-	screen_write_clear_dirty(wp);
++	screen_write_sync_clear_dirty(wp);
+ }
+ 
+-/* Clear any dirty lines. */
++/* Clear pending synchronized output. */
+ void
+-screen_write_clear_dirty(struct window_pane *wp)
++screen_write_sync_clear_dirty(struct window_pane *wp)
+ {
+-	if (wp != NULL && wp->sync_dirty != NULL) {
++	if (wp != NULL) {
+ 		free(wp->sync_dirty);
+ 		wp->sync_dirty = NULL;
+ 		wp->sync_dirty_size = 0;
++		wp->sync_scrolled = 0;
+ 	}
+ }
+ 
++/* Defer scrolling until the synchronized update ends. */
++static void
++screen_write_sync_scroll_dirty(struct screen_write_ctx *ctx)
++{
++	struct window_pane	*wp = ctx->wp;
++	struct screen		*s = ctx->s;
++	u_int			 n = ctx->scrolled, y, sy = screen_size_y(s);
++	u_int			 ry = s->rlower + 1 - s->rupper;
++	bitstr_t		*bs;
++
++	if (n > ry)
++		n = ry;
++	if (wp == NULL ||
++	    n == ry ||
++	    s->rlower >= sy ||
++	    window_pane_scrollbar_overlay_visible(wp)) {
++		screen_write_should_draw_lines(ctx, s->rupper, ry);
++		return;
++	}
++	if (wp->flags & (PANE_REDRAW|PANE_DROP))
++		return;
++	bs = screen_write_sync_allocate_dirty(wp, sy);
++
++	if (wp->sync_scrolled != 0 &&
++	    (wp->sync_rupper != s->rupper ||
++	    wp->sync_rlower != s->rlower ||
++	    wp->sync_bg != ctx->bg)) {
++		log_debug("%s: %%%u region changed, redrawing all", __func__,
++		    wp->id);
++		bit_nset(bs, 0, sy - 1);
++		wp->sync_scrolled = 0;
++		return;
++	}
++
++	/* Keep dirty lines aligned with the scrolled grid. */
++	for (y = s->rupper; y + n <= s->rlower; y++) {
++		if (bit_test(bs, y + n))
++			bit_set(bs, y);
++		else
++			bit_clear(bs, y);
++	}
++	bit_nset(bs, s->rlower + 1 - n, s->rlower);
++
++	wp->sync_rupper = s->rupper;
++	wp->sync_rlower = s->rlower;
++	wp->sync_bg = ctx->bg;
++	wp->sync_scrolled += n;
++	if (wp->sync_scrolled >= ry) {
++		bit_nset(bs, s->rupper, s->rlower);
++		wp->sync_scrolled = 0;
++	}
++	window_pane_scrollbar_redraw(wp);
++
++	log_debug("%s: %%%u deferred scroll of %u (region %u-%u)", __func__,
++	    wp->id, wp->sync_scrolled, s->rupper, s->rlower);
++}
++
++/* Redraw the scrolled lines for a client which cannot scroll them. */
++static void
++screen_write_sync_redraw_cb(const struct tty_ctx *ttyctx)
++{
++	struct window_pane	*wp = ttyctx->arg;
++
++	bit_nset(wp->sync_dirty, wp->sync_rupper, wp->sync_rlower);
++}
++
++/* Send the deferred scroll to the client. */
++static void
++screen_write_sync_apply_scroll(struct screen_write_ctx *ctx,
++    struct tty_ctx *ttyctx)
++{
++	struct window_pane	*wp = ctx->wp;
++	struct screen		*s = ctx->s;
++	u_int			 sy = screen_size_y(s), n = wp->sync_scrolled;
++	tty_ctx_redraw_cb	 redraw_cb;
++
++	wp->sync_scrolled = 0;
++	if (wp->flags & PANE_REDRAW)
++		return;
++	if (wp->sync_rlower >= sy || wp->sync_rupper > wp->sync_rlower) {
++		bit_nset(wp->sync_dirty, 0, sy - 1);
++		return;
++	}
++	if ((ttyctx->flags & TTY_CTX_PANE_OBSCURED) ||
++	    window_pane_scrollbar_overlay_visible(wp)) {
++		bit_nset(wp->sync_dirty, wp->sync_rupper, wp->sync_rlower);
++		return;
++	}
++
++	log_debug("%s: %%%u scroll of %u (region %u-%u)", __func__, wp->id, n,
++	    wp->sync_rupper, wp->sync_rlower);
++
++	ttyctx->orupper = wp->sync_rupper;
++	ttyctx->orlower = wp->sync_rlower;
++	if (wp->yoff + wp->sy > wp->window->sy)
++		ttyctx->orlower -= (wp->yoff + wp->sy - wp->window->sy);
++	ttyctx->n = n;
++	ttyctx->bg = wp->sync_bg;
++	redraw_cb = ttyctx->redraw_cb;
++	ttyctx->redraw_cb = screen_write_sync_redraw_cb;
++	tty_write(tty_cmd_scrollup, ttyctx);
++
++	ttyctx->redraw_cb = redraw_cb;
++	ttyctx->orupper = s->rupper;
++	ttyctx->orlower = s->rlower;
++	ttyctx->n = 0;
++}
++
+ /* Redraw all visible cells in a pane. */
+ static void
+ screen_write_redraw_pane(struct screen_write_ctx *ctx, struct tty_ctx *ttyctx)
+@@ -2420,10 +2545,8 @@ screen_write_collect_flush(struct screen_write_ctx *ctx, int scroll_only,
+ 	if (wp != NULL && (wp->flags & (PANE_REDRAW|PANE_DROP)))
+ 		goto discard;
+ 	if (s->mode & MODE_SYNC) {
+-		if (ctx->scrolled != 0) {
+-			screen_write_should_draw_lines(ctx, s->rupper,
+-			    s->rlower + 1 - s->rupper);
+-		}
++		if (ctx->scrolled != 0)
++			screen_write_sync_scroll_dirty(ctx);
+ 		for (y = 0; y < screen_size_y(s); y++) {
+ 			cl = &s->write_list[y];
+ 			if (!TAILQ_EMPTY(&cl->items))
+diff --git a/tmux.h b/tmux.h
+index b53dae8e..ef0f9e63 100644
+--- a/tmux.h
++++ b/tmux.h
+@@ -1345,6 +1345,10 @@ struct window_pane {
+ 
+ 	bitstr_t	*sync_dirty;
+ 	u_int		 sync_dirty_size;
++	u_int		 sync_scrolled;
++	u_int		 sync_rupper;
++	u_int		 sync_rlower;
++	u_int		 sync_bg;
+ 
+ 	u_int		 sb_slider_y;
+ 	u_int		 sb_slider_h;
+@@ -3615,7 +3619,7 @@ void	 screen_write_mode_clear(struct screen_write_ctx *, int);
+ void	 screen_write_start_sync(struct window_pane *);
+ void	 screen_write_stop_sync(struct window_pane *);
+ void	 screen_write_end_sync(struct screen_write_ctx *);
+-void	 screen_write_clear_dirty(struct window_pane *);
++void	 screen_write_sync_clear_dirty(struct window_pane *);
+ void	 screen_write_cursorup(struct screen_write_ctx *, u_int);
+ void	 screen_write_cursordown(struct screen_write_ctx *, u_int);
+ void	 screen_write_cursorright(struct screen_write_ctx *, u_int);
+diff --git a/window.c b/window.c
+index 775afcb3..96c97588 100644
+--- a/window.c
++++ b/window.c
+@@ -1575,7 +1575,7 @@ window_pane_destroy(struct window_pane *wp)
+ 	window_pane_clear_prompt(wp);
+ 
+ 	window_pane_free_modes(wp);
+-	screen_write_clear_dirty(wp);
++	screen_write_sync_clear_dirty(wp);
+ 
+ 	if (wp->fd != -1) {
+ #ifdef HAVE_UTEMPTER
================================================================

---- gitweb:

http://git.pld-linux.org/gitweb.cgi/packages/tmux.git/commitdiff/3dc8a9d2f99e2e931d4633f0d69436c4a695c0b0



More information about the pld-cvs-commit mailing list