From 069995012b85cbc5a1b9923eab22a24ec0a19ae9 Mon Sep 17 00:00:00 2001 From: Jaeseok Lee Date: Thu, 3 Dec 2026 21:21:03 +0900 Subject: [PATCH] Send an in-band size report when the terminal resizes A program that enables in-band size reports (mode 2048) learns its size from them and stops following SIGWINCH. Ghostty sends a report from its I/O layer whenever the terminal resizes, but libghostty-vt answers only when the mode is turned on, and ghostel never sent one on resize. Neovim enables the mode, so after a window resize it kept drawing at the size it started with, and neither :redraw! nor further resizes helped. When a redraw commits a resize and the mode is on, write the report to the PTY after resizing it, with the same size the XTWINOPS replies use. --- CHANGELOG.md | 3 +++ src/GhostelTerm.zig | 14 ++++++++++++-- src/handler.zig | 16 ++++++++++------ test/ghostel-terminal-test.el | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a7b8d24..6426d809 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ All notable changes to this project will be documented in this file. ## [Unreleased] ### Fixed +- A resized terminal sends an in-band size report while the program has + mode 2048 on, as Ghostty does. Neovim enables the mode and then ignores + SIGWINCH, so it kept drawing at the size it started with. - evil-ghostel: the editing operators and insert-state Ctrl keys drive the shell in a bridged terminal, such as a rexel or ghostel-tmux buffer. - A `file://` link without a host, or with `localhost`, opens on the remote diff --git a/src/GhostelTerm.zig b/src/GhostelTerm.zig index 847d6e4b..e15b1bc2 100644 --- a/src/GhostelTerm.zig +++ b/src/GhostelTerm.zig @@ -8,7 +8,8 @@ const Allocator = std.mem.Allocator; const emacs = @import("emacs.zig"); const gt = @import("ghostty-vt"); -const GhostelHandler = @import("handler.zig").GhostelHandler; +const handler = @import("handler.zig"); +const GhostelHandler = handler.GhostelHandler; const Renderer = @import("Renderer.zig"); const RecursiveMutex = @import("RecursiveMutex.zig"); const input = @import("input.zig"); @@ -102,6 +103,7 @@ pub fn redraw(self: *Self, force_full: bool, force_sync: bool) !bool { const env = emacs.current_env orelse return false; const pre_size = .{ self.terminal.cols, self.terminal.rows }; if (!try self.renderer.redraw(env, force_full, force_sync)) return false; + const resized = !std.meta.eql(pre_size, .{ self.terminal.cols, self.terminal.rows }); if (self.process) |proc| { const size = self.winSize(); @@ -109,13 +111,21 @@ pub fn redraw(self: *Self, force_full: bool, force_sync: bool) !bool { try proc.resizePty(size); self.pty_size = size; } - } else if (!std.meta.eql(pre_size, .{ self.terminal.cols, self.terminal.rows })) { + } else if (resized) { // Emacs-owned PTYs carry no pixel geometry. const process = env.symbolValue("ghostel--process"); if (env.isNotNil(process)) { _ = env.f("set-process-window-size", .{ process, self.terminal.rows, self.terminal.cols }); } } + + // Programs with mode 2048 on, such as Neovim, ignore SIGWINCH. + if (resized and self.terminal.modes.get(.in_band_size_reports)) { + var buf: [64]u8 = undefined; + var writer = std.Io.Writer.fixed(&buf); + try gt.size_report.encode(&writer, .mode_2048, handler.reportedSize(&self.terminal)); + try self.ptyWrite(writer.buffered()); + } return true; } diff --git a/src/handler.zig b/src/handler.zig index c8713b70..91fc204d 100644 --- a/src/handler.zig +++ b/src/handler.zig @@ -14,6 +14,15 @@ const log = std.log.scoped(.GhostelHandler); // xterm limits the title stack to 10 entries. const title_stack_max = 10; +pub fn reportedSize(terminal: *const gt.Terminal) gt.size_report.Size { + return .{ + .rows = terminal.rows, + .columns = terminal.cols, + .cell_width = terminal.width_px / terminal.cols, + .cell_height = terminal.height_px / terminal.rows, + }; +} + pub fn GhostelHandler(Effects: type) type { return struct { const Self = @This(); @@ -189,12 +198,7 @@ pub fn GhostelHandler(Effects: type) type { /// Called for XTWINOPS size queries (CSI 14/16/18 t). fn sizeCallback(handler: *gt.TerminalStream.Handler) ?gt.size_report.Size { - return .{ - .rows = handler.terminal.rows, - .columns = handler.terminal.cols, - .cell_width = handler.terminal.width_px / handler.terminal.cols, - .cell_height = handler.terminal.height_px / handler.terminal.rows, - }; + return reportedSize(handler.terminal); } /// Called when the terminal title changes. A cleared title (empty diff --git a/test/ghostel-terminal-test.el b/test/ghostel-terminal-test.el index 76634a93..1d1de22c 100644 --- a/test/ghostel-terminal-test.el +++ b/test/ghostel-terminal-test.el @@ -225,6 +225,41 @@ has been rendered." (should (equal "\e[6;23;9t\e[4;575;720t\e[8;25;80t" (apply #'concat (nreverse replies))))))) +(ert-deftest ghostel-test-in-band-size-report-on-resize () + "A committed resize sends an in-band size report while mode 2048 is on. +Neovim enables the mode and then ignores SIGWINCH." + :tags '(native) + (let ((buf (generate-new-buffer " *ghostel-test-in-band-resize*"))) + (unwind-protect + (with-current-buffer buf + (set-window-buffer (selected-window) buf) + (ghostel-mode) + (setq ghostel--term (ghostel--new 6 80 100)) + (let ((ghostel--process 'fake-process) + (replies nil)) + (cl-letf (((symbol-function 'process-send-string) + (lambda (_process string) (push string replies))) + ((symbol-function 'set-process-window-size) #'ignore)) + (ghostel--set-size ghostel--term 6 80 9 23) + (ghostel-test--redraw ghostel--term t) + (ghostel--write-vt ghostel--term "\e[?2048h") + (should (equal "\e[48;6;80;138;720t" + (apply #'concat (nreverse replies)))) + (setq replies nil) + (ghostel--set-size ghostel--term 4 40 9 23) + (should-not replies) + (ghostel-test--redraw ghostel--term t) + (should (equal "\e[48;4;40;92;360t" + (apply #'concat (nreverse replies)))) + (setq replies nil) + (ghostel-test--redraw ghostel--term t) + (should-not replies) + (ghostel--write-vt ghostel--term "\e[?2048l") + (ghostel--set-size ghostel--term 6 80 9 23) + (ghostel-test--redraw ghostel--term t) + (should-not replies)))) + (kill-buffer buf)))) + (defun ghostel-test--mouse-presses (modes cells &optional cell-size) "Return the concatenated reports for left presses at CELLS. CELLS is a list of (COL . ROW). MODES is written first to an 80x25