szn

Bug 451 — DECSTBM full-screen scroll region trap in Screen.setScrollRegion discards scrolled-off lines instead of pushing to history

451. Full-screen DECSTBM margin trap erases lines scrolling past top of pane

Severity: HIGH
Status: Fixed
Files: src/screen.zig, src/input.zig


Symptoms

When using szn for interactive workflows (e.g. running commands after viewing files in less, vim, man, git log, or running programs that reset the terminal with clear / terminfo rmcup):

  1. Older scrollback history (from before running the interactive program) remains accessible in copy-mode and mouse scroll.
  2. Any newly executed command that produces long output appears to scroll normally on the screen, but when scrolling up into the scrollback buffer, the new output is missing / cut off right at the upper top row of the pane.
  3. The scrolled-off lines are never recorded into the scrollback buffer and cannot be retrieved.

Root Cause Analysis

  1. CSI r / full-screen margin sets non-null scroll_region: In src/input.zig:791, CSI r (DECSTBM) without parameters defaults to top row 1 and bottom row height:
    'r' => {
        if (self.intermediate == 0) {
            const top = (self.paramDefault(0, 1) -| 1);
            const bottom = (self.paramDefault(1, self.screen.grid.height) -| 1);
            self.screen.setScrollRegion(top, bottom);
        }
    },
    

    In Screen.setScrollRegion:

    pub fn setScrollRegion(self: *Screen, top: u32, bottom: u32) void {
        self.scroll_region = .{ @min(top, self.grid.height -| 1), @min(bottom, self.grid.height -| 1) };
        self.cursor.x = 0;
        self.cursor.y = if (self.mode.origin) self.scroll_region.?[0] else 0;
        self.dirty = true;
    }
    

    Per VT100/xterm specifications, when DECSTBM margins encompass the entire screen (top == 0 and bottom == height - 1), margins are disabled (scroll_region = null). In szn, setScrollRegion unconditionally assigns self.scroll_region = .{ 0, height - 1 }. Once any application sends \x1b[r, self.scroll_region != null becomes permanent for that pane.

  2. advanceLine routes full-screen scrolls to scrollUpInRegion: In Screen.advanceLine:
    if (self.scroll_region) |r| {
        if (self.cursor.y == r[1]) {
            try self.scrollUpInRegion();
        } else if (self.cursor.y + 1 < self.grid.height) {
            self.cursor.y += 1;
        }
        return;
    }
    if (self.cursor.y + 1 >= self.grid.height) {
        try self.grid.scrollUp();
        ...
    }
    

    Because self.scroll_region is .{ 0, height - 1 }, every linefeed at the bottom of the screen calls scrollUpInRegion() instead of self.grid.scrollUp().

  3. scrollUpInRegion permanently deletes line 0: In Screen.scrollUpInRegion:
    fn scrollUpInRegion(self: *Screen) Error!void {
        const top = if (self.scroll_region) |r| r[0] else 0;
        const bottom = if (self.scroll_region) |r| r[1] else self.grid.height - 1;
        if (self.cursor.y != bottom) return;
        var y = top;
        while (y < bottom) : (y += 1) {
            const line = self.grid.getLineMut(y);
            const next = self.grid.getLineMut(y + 1);
            std.mem.swap(GridLine, line, next);
        }
        const last = self.grid.getLineMut(bottom);
        self.decrementLineRefs(last.cells.items);
        @memset(last.cells.items, self.eraseCell());
        last.wrapped = false;
        last.dirty = true;
        self.shiftSixelAnchors(-1);
    }
    

    scrollUpInRegion swaps rows upward, moving row 0 to bottom, and then erases it with @memset(last.cells.items, self.eraseCell()). The line that scrolled off row 0 is never pushed to Grid.history, permanently discarding it.

  4. Persistence across pane resize: In Screen.resize:
    if (self.scroll_region) |r| {
        const top = @min(r[0], height -| 1);
        const bottom = @min(r[1], height -| 1);
        if (top >= bottom) {
            self.scroll_region = null;
        } else {
            self.scroll_region = .{ top, bottom };
        }
    }
    

    When a pane is split or resized, a stuck .{ 0, 23 } region clamps to .{ 0, 11 }, continuing to treat the resized pane as locked into region-scrolling mode.


Proposed Fix

  1. In Screen.setScrollRegion, detect when the region covers the full screen:
    const clamped_top = @min(top, self.grid.height -| 1);
    const clamped_bottom = @min(bottom, self.grid.height -| 1);
    if (clamped_top == 0 and clamped_bottom >= self.grid.height -| 1) {
        self.scroll_region = null;
    } else {
        self.scroll_region = .{ clamped_top, clamped_bottom };
    }
    
  2. In Screen.advanceLine, add a defensive check that full-height regions fall through to grid.scrollUp():
    if (self.scroll_region) |r| {
        if (r[0] == 0 and r[1] >= self.grid.height -| 1) {
            self.scroll_region = null;
        } else {
            ...
        }
    }
    
  3. In Screen.resize, reset self.scroll_region = null if top == 0 and bottom >= height -| 1.
  4. Add regression unit tests in src/input.zig and src/screen.zig verifying CSI r clears scroll_region and subsequent advanceLine calls push lines to Grid.history.