szn

Bug 555 — Copy-mode search bugs: wide-char padding spaces, column-0 backward loop, and incomplete wrap-around

555. CopyMode search breaks on CJK/wide characters, gets stuck at column 0 in backward search, and skips same-line wrap matches

Severity: HIGH
Status: ✅ RESOLVED
Files: src/mode_copy.zig:426-483, src/mode_copy.zig:518-558, src/mode_copy.zig:654-682

Problem

  1. lineBytes inserts ' ' for every wide-character padding cell (src/mode_copy.zig:654-682):
    for (0..col_limit) |col| {
        const c = self.getCellAtY(grid, @intCast(col), @intCast(r));
        const cp: u21 = if (c.char == 0) ' ' else c.char;
        var utf8: [4]u8 = undefined;
        const n = std.unicode.utf8Encode(cp, &utf8) catch continue;
        try out.appendSlice(self.allocator, utf8[0..n]);
        ...
    

    Every width-2 character (CJK ideographs, Hangul, fullwidth forms, wide emoji) occupies two cells: the lead cell (char = cp, width = 2) and a trailing padding cell (char = 0, is_padding = true). Because lineBytes does not skip c.is_padding (nor c.attr.sixel), it encodes ' ' after every wide character (e.g. "日本語" becomes "日 本 語 "), making multi-character CJK search queries impossible to match and skewing column indices in byteOffsetToCol (which also fails to advance by 2 columns for wide characters).

  2. searchBackward gets permanently stuck at column 0 (src/mode_copy.zig:518-538):
    const max_col: ?u32 = if (li == cursor_logical)
        cursor_abs_col -| 1
    else
        null;
    

    When the cursor is already at cursor_abs_col == 0 (e.g. after finding a match at column 0), 0 -| 1 evaluates to 0. lastIndexOfIgnoreCase treats max_col = 0 as inclusive (pos <= 0), so pressing n (in ? backward search) or N (in / forward search) matches column 0 of the current line again and never moves to earlier lines.

  3. Wrap-around (Pass 2) skips the starting logical line (src/mode_copy.zig:463,541):
    • In searchForward, Pass 2 iterates while (li < cursor_logical) : (li += 1), so if the only match (or an earlier match on the same line) is at col <= cursor_abs_col on cursor_logical, wrap-around misses it.
    • In searchBackward, Pass 2 iterates while (li > cursor_logical), missing matches at col >= cursor_abs_col on cursor_logical.
    • Furthermore, searchForward initializes var cursor_logical: usize = cursor_abs; without scanning backward to find the logical line containing cursor_abs when the cursor sits on a soft-wrapped continuation row (unlike searchBackward:509-515).

Proposed Fix

  1. In CopyMode.lineBytes and byteOffsetToCol:
    • Skip c.is_padding cells;
    • Treat c.attr.sixel cells as ' ' (char == 0);
    • In byteOffsetToCol, advance col by 2 when the matched cell is followed by an is_padding cell.
  2. In CopyMode.searchBackward, when li == cursor_logical and cursor_abs_col == 0, skip li in Pass 1 (if (li == 0) break; li -= 1; continue;).
  3. Include cursor_logical at the end of Pass 2 in both searchForward and searchBackward, and normalize cursor_logical in searchForward the same way searchBackward does.