summaryrefslogtreecommitdiff
path: root/src/normal.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-08 12:00:28 -0300
committerGabriel Schneider <[email protected]>2026-10-22 10:03:15 -0300
commit13d5613b77c62c425c3a124a34c0c2528cc6251f (patch)
treef3cbe41683ef31641ea087039db9fc3be81e141f /src/normal.zig
parent046979b877f02a91cf52cc86b84c5619cb9363d3 (diff)
downloadpardes-13d5613b77c62c425c3a124a34c0c2528cc6251f.tar.gz
pardes-13d5613b77c62c425c3a124a34c0c2528cc6251f.zip
Follow Helix search registers for n/N; keep Look navigation as named builtins
Diffstat (limited to 'src/normal.zig')
-rw-r--r--src/normal.zig168
1 files changed, 164 insertions, 4 deletions
diff --git a/src/normal.zig b/src/normal.zig
index aec6b983..fad1836b 100644
--- a/src/normal.zig
+++ b/src/normal.zig
@@ -168,6 +168,168 @@ pub fn applySelRegex(p: *Pardes, pane: *Pane, t: *Text, pat: []const u8, mode: m
t.setRanges(text, out[0..m], &.{}, 0, true); // helix keeps primary 0 (its own TODO)
}
+/// Search only the primary range, as Helix does. Select mode adds a new
+/// match and makes it primary; normal mode replaces only the primary.
+fn searchStep(p: *Pardes, t: *Text, text: []const u8, direction: modal.Normal.Direction, count: u32) void {
+ const name = if (p.register != 0) p.register else p.registers.last_search;
+ const pat = p.registers.value(name, 0) orelse return;
+ var re = SearchRegex.compile(p.scratch.allocator(), pat) catch {
+ p.setMessage(p.active, "invalid search pattern");
+ return;
+ };
+ var hay = text;
+ if (for (pat) |c| {
+ if (std.ascii.isUpper(c)) break false;
+ } else true) {
+ const lower = p.scratch.allocator().dupe(u8, text) catch return;
+ for (lower) |*c| c.* = std.ascii.toLower(c.*);
+ hay = lower;
+ }
+ for (0..count) |_| {
+ var ranges: [Text.max_selections]modal.Selection = undefined;
+ const got = t.ranges(text, 0, &ranges);
+ const old = ranges[got.pri];
+ const from = @min(old.anchor, old.head);
+ const to = @max(old.anchor, old.head);
+ var wrapped = false;
+ const found = (if (direction == .forward) blk: {
+ if (tryFind(&re, hay, to, hay.len)) |h| break :blk h;
+ wrapped = true;
+ break :blk tryFind(&re, hay, 0, hay.len);
+ } else blk: {
+ if (lastMatch(&re, hay, 0, from)) |h| break :blk h;
+ wrapped = true;
+ break :blk lastMatch(&re, hay, from, hay.len);
+ }) orelse {
+ p.setMessage(p.active, "no more matches");
+ return;
+ };
+ if (found.end == 0) return;
+ const next: modal.Selection = if (old.anchor > old.head)
+ .{ .anchor = found.end, .head = found.start }
+ else
+ .{ .anchor = found.start, .head = found.end };
+ if (t.select and got.n < ranges.len) {
+ ranges[got.n] = next;
+ t.setRanges(text, ranges[0 .. got.n + 1], &.{}, got.n, true);
+ } else {
+ ranges[got.pri] = next;
+ t.setRanges(text, ranges[0..got.n], &.{}, got.pri, true);
+ }
+ if (wrapped) p.setMessage(p.active, "wrapped around document");
+ }
+ t.show();
+}
+
+fn tryFind(re: *SearchRegex, hay: []const u8, from: usize, to: usize) ?regexp.Regex.Match {
+ return re.find(hay, @min(from, hay.len), to);
+}
+
+fn lastMatch(re: *SearchRegex, hay: []const u8, from: usize, to: usize) ?regexp.Regex.Match {
+ var last: ?regexp.Regex.Match = null;
+ var at = from;
+ while (at <= to) {
+ const found = tryFind(re, hay, at, to) orelse break;
+ last = found;
+ at = if (found.end > found.start) found.end else modal.nextGrapheme(hay, found.end);
+ if (at <= found.start) break;
+ }
+ return last;
+}
+
+pub fn searchSelection(p: *Pardes, t: *Text, text: []const u8, boundary: bool) void {
+ var ranges: [Text.max_selections]modal.Selection = undefined;
+ const got = t.ranges(text, 0, &ranges);
+ var pattern: std.ArrayList(u8) = .empty;
+ const arena = p.scratch.allocator();
+ for (ranges[0..got.n], 0..) |r, i| {
+ const from = @min(r.anchor, r.head);
+ const to = @min(text.len, @max(r.anchor, r.head));
+ if (i > 0) pattern.append(arena, '|') catch return;
+ if (boundary and modal.searchWordAt(text, from) and !modal.searchWordBefore(text, from)) pattern.appendSlice(arena, "\\b") catch return;
+ appendSearchLiteral(&pattern, arena, text[from..to]) catch return;
+ if (boundary and to < text.len and modal.searchWordBefore(text, to) and !modal.searchWordAt(text, to)) pattern.appendSlice(arena, "\\b") catch return;
+ }
+ const name = if (p.register != 0) p.register else '/';
+ p.registers.put(p.gpa, name, pattern.items, 0, 1, true);
+ p.registers.last_search = name;
+}
+
+// mvzr's word boundary is ASCII-only. Strip outer boundaries and check
+// them against Unicode characters for search-selection patterns.
+const SearchRegex = struct {
+ const Branch = struct { re: regexp.Regex, start_word: bool, end_word: bool };
+ branches: []Branch,
+
+ fn compile(arena: std.mem.Allocator, pattern: []const u8) !SearchRegex {
+ if (std.mem.indexOf(u8, pattern, "\\b") == null) {
+ const branches = try arena.alloc(Branch, 1);
+ branches[0] = try branch(pattern);
+ return .{ .branches = branches };
+ }
+ var branches: std.ArrayList(Branch) = .empty;
+ var from: usize = 0;
+ var depth: usize = 0;
+ var escaped = false;
+ var in_class = false;
+ for (pattern, 0..) |c, i| {
+ if (escaped) {
+ escaped = false;
+ continue;
+ }
+ if (c == '\\') {
+ escaped = true;
+ continue;
+ }
+ if (c == '[') in_class = true else if (c == ']') in_class = false;
+ if (in_class) continue;
+ if (c == '(') depth += 1 else if (c == ')') depth -|= 1;
+ if (c == '|' and depth == 0) {
+ try branches.append(arena, try branch(pattern[from..i]));
+ from = i + 1;
+ }
+ }
+ try branches.append(arena, try branch(pattern[from..]));
+ for (branches.items) |*b| b.re.steps = regexp.Regex.budget / branches.items.len;
+ return .{ .branches = branches.items };
+ }
+
+ fn branch(pattern: []const u8) !Branch {
+ var pat = pattern;
+ const start = std.mem.startsWith(u8, pat, "\\b");
+ if (start) pat = pat[2..];
+ const end = std.mem.endsWith(u8, pat, "\\b") and !std.mem.endsWith(u8, pat, "\\\\b");
+ if (end) pat = pat[0 .. pat.len - 2];
+ return .{ .re = try regexp.Regex.compile(pat), .start_word = start, .end_word = end };
+ }
+
+ fn find(self: *SearchRegex, hay: []const u8, from: usize, to: usize) ?regexp.Regex.Match {
+ var best: ?regexp.Regex.Match = null;
+ for (self.branches) |*b| {
+ var at = from;
+ while (at <= to) {
+ const found = (b.re.find(hay, at, to, to) catch break) orelse break;
+ if ((!b.start_word or modal.searchWordBefore(hay, found.start) != modal.searchWordAt(hay, found.start)) and
+ (!b.end_word or modal.searchWordBefore(hay, found.end) != modal.searchWordAt(hay, found.end)))
+ {
+ if (best == null or found.start < best.?.start) best = found;
+ break;
+ }
+ at = modal.nextGrapheme(hay, found.start);
+ if (at <= found.start) break;
+ }
+ }
+ return best;
+ }
+};
+
+pub fn appendSearchLiteral(out: *std.ArrayList(u8), arena: std.mem.Allocator, text: []const u8) !void {
+ for (text) |c| {
+ if (std.mem.indexOfScalar(u8, "\\^$.[]()|*+?{}", c) != null) try out.append(arena, '\\');
+ if (c == '\n') try out.appendSlice(arena, "\\n") else try out.append(arena, c);
+ }
+}
+
/// The file's parse, made on the first key that walks it after an edit and
/// kept until the next: never made again for a key that changed nothing.
pub fn fileTree(p: *Pardes, f: *panes.File.State) ?*anyopaque {
@@ -766,10 +928,8 @@ fn executeNormalAction(p: *Pardes, t: *Text, semantic: modal.Normal.Action) void
},
.pipe_selection => |how| return p.startPrompt(pane, t, .{ .pipe = how }),
.search => return p.startPrompt(pane, &pane.body, .{ .search = config.search_marker }),
- .search_step => |direction| return look.lookWalk(
- p,
- if (direction == .forward) @as(i32, 1) else -1,
- ),
+ .search_step => |step| return searchStep(p, t, text, step.direction, step.count),
+ .search_selection => |boundary| return searchSelection(p, t, text, boundary),
.node => |walk| return nodeSelect(p, pane, t, walk),
.goto_word => return startJump(p, pane, t),
.object_jump => |jump| return objectJump(p, pane, t, text, range, jump.object, jump.direction == .forward, jump.count),