From 4803daec3e038485bb5df5dbc9aa0cf0e3828af4 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Sun, 1 Feb 2026 19:48:03 +0100 Subject: [PATCH 1/3] fix: make reflow wrap *before* width limit, not after it --- src/buffer/reflow.zig | 46 ++++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/src/buffer/reflow.zig b/src/buffer/reflow.zig index fa3214a..76ea9dd 100644 --- a/src/buffer/reflow.zig +++ b/src/buffer/reflow.zig @@ -8,25 +8,35 @@ pub fn reflow(allocator: std.mem.Allocator, text: []const u8, width: usize) erro var first = true; var line_len: usize = 0; for (words) |word| { - if (line_len == 0) { - if (first) { - try writer.writeAll(prefix.first); - first = false; - } else { - try writer.writeAll(prefix.continuation); - var pad = prefix.first.len - prefix.continuation.len; - while (pad > 0) : (pad -= 1) + const state: enum { + begin, + words, + } = if (line_len == 0) .begin else .words; + blk: switch (state) { + .begin => { + if (first) { + try writer.writeAll(prefix.first); + first = false; + } else { + try writer.writeAll(prefix.continuation); + var pad = prefix.first.len - prefix.continuation.len; + while (pad > 0) : (pad -= 1) + try writer.writeByte(' '); + } + line_len += prefix.len; + continue :blk .words; + }, + .words => { + if (line_len + word.len + 1 >= width) { + try writer.writeByte('\n'); + line_len = 0; + continue :blk .begin; + } + if (line_len > prefix.len) try writer.writeByte(' '); - } - line_len += prefix.len; - } - if (line_len > prefix.len) - try writer.writeByte(' '); - try writer.writeAll(word); - line_len += word.len; - if (line_len >= width) { - try writer.writeByte('\n'); - line_len = 0; + try writer.writeAll(word); + line_len += word.len; + }, } } From 9f775c9ec8c744c3b0c73d35a78b679a7f380fd7 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Sun, 1 Feb 2026 19:51:48 +0100 Subject: [PATCH 2/3] feat: add integer arg support to reflow command --- src/tui/editor.zig | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/tui/editor.zig b/src/tui/editor.zig index 4af99ac..df8754d 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -6829,7 +6829,9 @@ pub const Editor = struct { self.filter_ = null; } - fn reflow_cursel(self: *Self, root_: Buffer.Root, cursel: *CurSel, allocator: Allocator) error{Stop}!Buffer.Root { + fn reflow_cursel(self: *Self, root_: Buffer.Root, cursel: *CurSel, allocator: Allocator, ctx: Context) error{Stop}!Buffer.Root { + var reflow_width: usize = tui.config().reflow_width; + _ = ctx.args.match(.{tp.extract(&reflow_width)}) catch {}; var root = root_; var sel = cursel.selection orelse return root; sel.normalize(); @@ -6837,7 +6839,7 @@ pub const Editor = struct { const sfa_allocator = sfa.get(); const cut_text = copy_selection(root, sel, sfa_allocator, self.metrics) catch return error.Stop; defer sfa_allocator.free(cut_text); - const reflowed = Buffer.reflow(sfa_allocator, cut_text, tui.config().reflow_width) catch return error.Stop; + const reflowed = Buffer.reflow(sfa_allocator, cut_text, reflow_width) catch return error.Stop; defer sfa_allocator.free(reflowed); root = try self.delete_selection(root, cursel, allocator); root = self.insert(root, cursel, reflowed, allocator) catch return error.Stop; @@ -6845,13 +6847,16 @@ pub const Editor = struct { return root; } - pub fn reflow(self: *Self, _: Context) Result { + pub fn reflow(self: *Self, ctx: Context) Result { const b = try self.buf_for_update(); - const root = try self.with_cursels_mut_once(b.root, reflow_cursel, b.allocator); + const root = try self.with_cursels_mut_once_arg(b.root, reflow_cursel, b.allocator, ctx); try self.update_buf(root); self.clamp(); } - pub const reflow_meta: Meta = .{ .description = "Reflow selection" }; + pub const reflow_meta: Meta = .{ + .description = "Reflow selection", + .arguments = &.{.integer}, + }; fn to_upper_cursel(self: *Self, root_: Buffer.Root, cursel: *CurSel, allocator: Allocator) error{Stop}!Buffer.Root { var root = root_; From 9c4f30c4ee297d6799bf00cca22e24b1431fceed Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Sun, 1 Feb 2026 19:52:01 +0100 Subject: [PATCH 3/3] fix: reflow width is off by one --- src/buffer/reflow.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/buffer/reflow.zig b/src/buffer/reflow.zig index 76ea9dd..b191a06 100644 --- a/src/buffer/reflow.zig +++ b/src/buffer/reflow.zig @@ -27,7 +27,7 @@ pub fn reflow(allocator: std.mem.Allocator, text: []const u8, width: usize) erro continue :blk .words; }, .words => { - if (line_len + word.len + 1 >= width) { + if (line_len + word.len + 1 >= width - 1) { try writer.writeByte('\n'); line_len = 0; continue :blk .begin;