summaryrefslogtreecommitdiff
path: root/src/normal.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/normal.zig')
-rw-r--r--src/normal.zig194
1 files changed, 97 insertions, 97 deletions
diff --git a/src/normal.zig b/src/normal.zig
index 6d7eb69c..446f0e3e 100644
--- a/src/normal.zig
+++ b/src/normal.zig
@@ -25,40 +25,11 @@ fn isPrefix(key: Key, cp: u21) bool {
return key.cp == cp and !key.ctrl and !key.alt;
}
-fn normalInput(key: Key) modal.Normal.Input {
- var out: modal.Normal.Input = .{
- .cp = key.cp,
- .ctrl = key.ctrl,
- .alt = key.alt,
- };
- inline for (std.enums.values(modal.Normal.Role)) |role| {
- const name = @tagName(role);
- const binding = @field(config, name);
- const matched = if (@TypeOf(binding) == comptime_int)
- isPrefix(key, binding)
- else if (@TypeOf(binding) == []const config.Chord)
- hit(key, binding)
- else
- @compileError("normal input binding has unsupported type: " ++ name);
- if (matched) out.roles.insert(role);
- }
- return out;
-}
-
pub fn multiOnce(p: *Pardes) bool {
p.multi_stop = true;
return p.multi_first;
}
-fn startSelRegex(p: *Pardes, pane: *Pane, t: *Text, mode: modal.Normal.RegexMode) edit.Error!void {
- const text = try edit.flatSurface(p, t);
- const got = t.ranges(text, 0, &pane.sel_snap);
- pane.nsel_snap = @intCast(got.n);
- pane.sel_snap_pri = @intCast(got.pri);
- pane.sel_snap_expl = t.vsel.active and t.vsel.explicit;
- panes.startPrompt(p, pane, t, .{ .search = regexMarker(mode) });
-}
-
fn regexMarker(mode: modal.Normal.RegexMode) []const u8 {
return switch (mode) {
.select => config.select_marker,
@@ -143,56 +114,6 @@ 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, forward: bool, count: u32) edit.Error!void {
- const v = p.view();
- const name = if (v.register != 0) v.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(v.active, "invalid search pattern");
- return;
- };
- var hay = text;
- if (for (pat) |c| {
- if (std.ascii.isUpper(c)) break false;
- } else true) hay = try std.ascii.allocLowerString(p.scratch.allocator(), text);
- 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 (forward) blk: {
- if (re.find(hay, to, hay.len)) |h| break :blk h;
- wrapped = true;
- break :blk re.find(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(v.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(v.active, "wrapped around document");
- }
- t.show();
-}
-
fn lastMatch(re: *SearchRegex, hay: []const u8, from: usize, to: usize) ?regexp.Regex.Match {
var last: ?regexp.Regex.Match = null;
var at = from;
@@ -205,24 +126,6 @@ fn lastMatch(re: *SearchRegex, hay: []const u8, from: usize, to: usize) ?regexp.
return last;
}
-pub fn searchSelection(p: *Pardes, t: *Text, text: []const u8, boundary: bool) edit.Error!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) try pattern.append(arena, '|');
- if (boundary and modal.searchWordAt(text, from) and !modal.searchWordBefore(text, from)) try pattern.appendSlice(arena, "\\b");
- try appendSearchLiteral(&pattern, arena, text[from..to]);
- if (boundary and to < text.len and modal.searchWordBefore(text, to) and !modal.searchWordAt(text, to)) try pattern.appendSlice(arena, "\\b");
- }
- const name = if (p.view().register != 0) p.view().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. A pane's
// Places keep one of these, compiled with the gpa, for as long as they live.
@@ -589,6 +492,26 @@ pub fn replaySels(p: *Pardes, t: *Text, what: Replay) void {
t.show(); // the view follows the PRIMARY, not the last pass
}
+fn normalInput(key: Key) modal.Normal.Input {
+ var out: modal.Normal.Input = .{
+ .cp = key.cp,
+ .ctrl = key.ctrl,
+ .alt = key.alt,
+ };
+ inline for (std.enums.values(modal.Normal.Role)) |role| {
+ const name = @tagName(role);
+ const binding = @field(config, name);
+ const matched = if (@TypeOf(binding) == comptime_int)
+ isPrefix(key, binding)
+ else if (@TypeOf(binding) == []const config.Chord)
+ hit(key, binding)
+ else
+ @compileError("normal input binding has unsupported type: " ++ name);
+ if (matched) out.roles.insert(role);
+ }
+ return out;
+}
+
pub fn handleNormal(p: *Pardes, t: *Text, key: Key) void {
// `"<reg>` is held by the parser until the command it names arrives
const named = t.normal.register;
@@ -605,6 +528,83 @@ pub fn handleNormal(p: *Pardes, t: *Text, key: Key) void {
replaySels(p, t, .{ .normal = action });
}
+fn startSelRegex(p: *Pardes, pane: *Pane, t: *Text, mode: modal.Normal.RegexMode) edit.Error!void {
+ const text = try edit.flatSurface(p, t);
+ const got = t.ranges(text, 0, &pane.sel_snap);
+ pane.nsel_snap = @intCast(got.n);
+ pane.sel_snap_pri = @intCast(got.pri);
+ pane.sel_snap_expl = t.vsel.active and t.vsel.explicit;
+ panes.startPrompt(p, pane, t, .{ .search = regexMarker(mode) });
+}
+
+/// 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, forward: bool, count: u32) edit.Error!void {
+ const v = p.view();
+ const name = if (v.register != 0) v.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(v.active, "invalid search pattern");
+ return;
+ };
+ var hay = text;
+ if (for (pat) |c| {
+ if (std.ascii.isUpper(c)) break false;
+ } else true) hay = try std.ascii.allocLowerString(p.scratch.allocator(), text);
+ 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 (forward) blk: {
+ if (re.find(hay, to, hay.len)) |h| break :blk h;
+ wrapped = true;
+ break :blk re.find(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(v.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(v.active, "wrapped around document");
+ }
+ t.show();
+}
+
+pub fn searchSelection(p: *Pardes, t: *Text, text: []const u8, boundary: bool) edit.Error!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) try pattern.append(arena, '|');
+ if (boundary and modal.searchWordAt(text, from) and !modal.searchWordBefore(text, from)) try pattern.appendSlice(arena, "\\b");
+ try appendSearchLiteral(&pattern, arena, text[from..to]);
+ if (boundary and to < text.len and modal.searchWordBefore(text, to) and !modal.searchWordAt(text, to)) try pattern.appendSlice(arena, "\\b");
+ }
+ const name = if (p.view().register != 0) p.view().register else '/';
+ p.registers.put(p.gpa, name, pattern.items, 0, 1, true);
+ p.registers.last_search = name;
+}
+
fn executeNormalAction(p: *Pardes, t: *Text, a: modal.Normal.Action) edit.Error!void {
const v = p.view();
// What reaches past the text -- a builtin, a search, a language query --