diff options
Diffstat (limited to 'src/normal.zig')
| -rw-r--r-- | src/normal.zig | 104 |
1 files changed, 71 insertions, 33 deletions
diff --git a/src/normal.zig b/src/normal.zig index 77f0c24c..c4c00824 100644 --- a/src/normal.zig +++ b/src/normal.zig @@ -14,14 +14,14 @@ const edit = @import("edit.zig"); const config = @import("config.zig"); const keyboard = @import("keys.zig"); const Pane = panes.Pane; -const Text = panes.Text; -const File = panes.File; -const Key = pardes.Key; -const hit = pardes.hit; +const Text = @import("Text.zig"); +const File = @import("File.zig"); +const Key = @import("plain").Key; +const hit = @import("Event.zig").hit; const sel_slot = pardes.sel_slot; const Pardes = pardes.Pardes; const Macro = pardes.Macro; -const Editor = pardes.Editor; +const Editor = @import("Editor.zig"); fn isPrefix(key: Key, cp: u21) bool { return key.cp == cp and !key.ctrl and !key.alt; @@ -55,8 +55,13 @@ pub fn selRegexArmed(pane: *Pane) ?struct { pat: []const u8, mode: modal.Normal. /// had when s/S/K/Alt-K was pressed; the prompt's snapshot of them lives on /// `pane`. pub fn applySelRegex(p: *Pardes, pane: *Pane, t: *Text, pat: []const u8, mode: modal.Normal.RegexMode) void { - const text = edit.flatSurface(p.editor(), t) catch |err| return p.reportError(p.view().active, "edit", err); - const snap = pane.sel_snap[0..pane.nsel_snap]; + selectByRegex(p.editor(), t, pane.sel_snap[0..pane.nsel_snap], pane.sel_snap_pri, pane.sel_snap_expl, pat, mode); +} + +/// Select (or split on, keep or remove by) `pat` inside `snap`, the +/// ranges there were (primary `pri`) when s/S/K/Alt-K was pressed. +pub fn selectByRegex(e: Editor, t: *Text, snap: []modal.Selection, pri: usize, expl: bool, pat: []const u8, mode: modal.Normal.RegexMode) void { + const text = edit.flatSurface(e, t) catch |err| return e.reportError(err); var out: [Text.max_selections]modal.Selection = undefined; var m: usize = 0; if (regexp.Regex.compile(pat)) |compiled| { @@ -64,7 +69,7 @@ pub fn applySelRegex(p: *Pardes, pane: *Pane, t: *Text, pat: []const u8, mode: m var hay_all = text; if (for (pat) |c| { if (std.ascii.isUpper(c)) break false; - } else true) hay_all = std.ascii.allocLowerString(p.scratch.allocator(), text) catch |err| return p.reportError(p.view().active, "edit", err); + } else true) hay_all = std.ascii.allocLowerString(e.scratch, text) catch |err| return e.reportError(err); for (snap) |r| { const from = @min(r.anchor, r.head); const to = @min(@max(r.anchor, r.head), text.len); @@ -111,7 +116,7 @@ pub fn applySelRegex(p: *Pardes, pane: *Pane, t: *Text, pat: []const u8, mode: m r.anchor = @min(r.anchor, text.len); r.head = @min(r.head, text.len); } - return t.setRanges(text, snap, &.{}, pane.sel_snap_pri, pane.sel_snap_expl); + return t.setRanges(text, snap, &.{}, pri, expl); } t.setRanges(text, out[0..m], &.{}, 0, true); // helix keeps primary 0 (its own TODO) } @@ -396,7 +401,7 @@ fn selStart(s: Text.SelRange) struct { row: i32, col: i32 } { } pub fn replaySels(e: Editor, t: *Text, what: Replay) void { - const id = e.p.view().active; + const id = if (comptime Editor.lib) 0 else e.p.view().active; // a header belongs to no pane and cannot go away under the replay const owner = t.pane(); const serial = if (owner) |pane| pane.serial else 0; @@ -459,7 +464,7 @@ pub fn replaySels(e: Editor, t: *Text, what: Replay) void { }; t.sticky_col = list[i].sticky; (switch (what) { - .normal => |normal_action| executeNormalAction(e.p, t, normal_action), + .normal => |normal_action| actionPass(e, t, normal_action), .insert => |insert_key| edit.insertKey(e, t, insert_key), }) catch |err| e.reportError(err); if (e.multi_stop.*) break; @@ -522,22 +527,53 @@ fn normalInput(key: Key) modal.Normal.Input { return out; } -pub fn handleNormal(e: Editor, t: *Text, key: Key) void { +/// A key in a text's normal or insert mode, as keys.zig hands it on after +/// its own intercepts, and as the library takes it (lib_editor.zig): what +/// the parse made of it, `.ignored` for a key typed in insert mode. +pub fn textKey(e: Editor, t: *Text, key: Key) modal.Normal.Result { + switch (t.mode) { + .normal => return handleNormal(e, t, key), + .insert => if (hit(key, config.escape) and !t.reg_wait) + edit.exitInsert(e, t) + else + edit.handleInsert(e, t, key), + .tty => unreachable, // the terminal's (keys.zig) + } + return .ignored; +} + +pub fn handleNormal(e: Editor, t: *Text, key: Key) modal.Normal.Result { // `"<reg>` is held by the parser until the command it names arrives const named = t.normal.register; const parsed = modal.Normal.parse(&t.normal, normalInput(key)); const action = switch (parsed) { - .pending, .ignored, .unbound => return, + .pending, .ignored, .unbound => return parsed, .action => |value| value, }; t.normal.register = 0; + // the core's roles are the library's caller's + if (comptime Editor.lib) if (!textRole(action.role)) return parsed; e.register.* = named; defer e.register.* = 0; + runAction(e, t, action); + return parsed; +} + +/// A normal-mode action run across every range, or once for the +/// selection as a whole. +pub fn runAction(e: Editor, t: *Text, action: modal.Normal.Action) void { if (t.nsel == 0 or action.once()) - return executeNormalAction(e.p, t, action) catch |err| e.reportError(err); + return actionPass(e, t, action) catch |err| e.reportError(err); replaySels(e, t, .{ .normal = action }); } +/// One pass of an action: the core's, which hands the text its own roles; +/// the library has the text's alone. +fn actionPass(e: Editor, t: *Text, action: modal.Normal.Action) edit.Error!void { + if (comptime Editor.lib) return textAction(e, t, action); + return executeNormalAction(e.p, t, action); +} + fn startSelRegex(p: *Pardes, pane: *Pane, t: *Text, mode: modal.Normal.RegexMode) edit.Error!void { const text = try edit.flatSurface(p.editor(), t); const got = t.ranges(text, 0, &pane.sel_snap); @@ -805,7 +841,8 @@ fn findMove(t: *Text, text: []const u8, range: modal.Selection, ch: u21, kind: m /// The file pane whose text `text` is, when `t` is its body: its rows are /// measured through their table pads (File.rowPads), so j and k keep the /// column a table shows, not the one its source has. -fn filePane(t: *Text, text: []const u8) ?*const Pane { +inline fn filePane(t: *Text, text: []const u8) ?*const Pane { + if (comptime Editor.lib) return null; if (t.what != .body) return null; const fp = t.pane() orelse return null; const f = if (fp.file) |*file| file else return null; @@ -966,6 +1003,7 @@ pub fn textAction(e: Editor, t: *Text, a: modal.Normal.Action) edit.Error!void { switch (a.role) { .escape => { t.select = false; + if (comptime Editor.lib) return; // typed again by a macro, Esc is only helix's if (e.p.macro.replaying > 0) return; tagline.exitHeader(e.p); // out of a header too, as Shift-Esc @@ -1359,12 +1397,12 @@ test "flat text movement and selection replay need no scratch rows" { p.scratch = .init(denied.allocator()); pane.body.cur_row = 4094; - handleNormal(p.editor(), &pane.body, .{ .cp = 'l' }); + _ = handleNormal(p.editor(), &pane.body, .{ .cp = 'l' }); try std.testing.expectEqual(@as(i32, 1), pane.body.cur_col); - handleNormal(p.editor(), &pane.body, .{ .cp = 'j' }); + _ = handleNormal(p.editor(), &pane.body, .{ .cp = 'j' }); try std.testing.expectEqual(@as(i32, 4095), pane.body.cur_row); try std.testing.expectEqual(@as(i32, 1), pane.body.cur_col); - handleNormal(p.editor(), &pane.body, .{ .cp = 'h' }); + _ = handleNormal(p.editor(), &pane.body, .{ .cp = 'h' }); try std.testing.expectEqual(@as(i32, 0), pane.body.cur_col); try executeNormalAction(p, &pane.body, .{ .role = .match_inside, .char = 'w' }); try std.testing.expectEqual(@as(i32, 2), pane.body.cur_col); @@ -1376,8 +1414,8 @@ test "flat text movement and selection replay need no scratch rows" { pane.body.nsel = 1; _ = pane.body.reserveSels(); pane.body.sels[0] = .{ .row = 3072, .col = 0, .arow = 3072, .acol = 0 }; - handleNormal(p.editor(), &pane.body, .{ .cp = 'l' }); - handleNormal(p.editor(), &pane.body, .{ .cp = 'j' }); + _ = handleNormal(p.editor(), &pane.body, .{ .cp = 'l' }); + _ = handleNormal(p.editor(), &pane.body, .{ .cp = 'j' }); try std.testing.expectEqual(@as(u16, 1), pane.body.nsel); try std.testing.expectEqual(@as(i32, 1025), pane.body.cur_row); try std.testing.expectEqual(@as(i32, 1), pane.body.cur_col); @@ -1480,19 +1518,19 @@ test "one cursor holds no room for other ranges; a second range makes it and one const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 60, .rows = 12 }); defer p.deinit(); const pane = try p.setTestFile("alpha beta\nalpha beta\n"); - for ("wxe%;gg") |cp| handleNormal(p.editor(), &pane.body, .{ .cp = cp }); + for ("wxe%;gg") |cp| _ = handleNormal(p.editor(), &pane.body, .{ .cp = cp }); try std.testing.expectEqual(@as(usize, 0), pane.body.sels.len); - handleNormal(p.editor(), &pane.body, .{ .cp = 'C' }); + _ = handleNormal(p.editor(), &pane.body, .{ .cp = 'C' }); try std.testing.expectEqual(@as(u16, 1), pane.body.nsel); try std.testing.expect(pane.body.sels.len > 0); - handleNormal(p.editor(), &pane.body, .{ .cp = 'l' }); // replayed at both, still two + _ = handleNormal(p.editor(), &pane.body, .{ .cp = 'l' }); // replayed at both, still two try std.testing.expectEqual(@as(u16, 1), pane.body.nsel); - handleNormal(p.editor(), &pane.body, .{ .cp = ',' }); + _ = handleNormal(p.editor(), &pane.body, .{ .cp = ',' }); try std.testing.expectEqual(@as(usize, 0), pane.body.sels.len); - handleNormal(p.editor(), &pane.body, .{ .cp = 'C' }); - handleNormal(p.editor(), &pane.body, .{ .cp = ';' }); - handleNormal(p.editor(), &pane.body, .{ .cp = 'x' }); // x at both: still two lines - handleNormal(p.editor(), &pane.body, .{ .cp = '_', .alt = true }); // merged to one + _ = handleNormal(p.editor(), &pane.body, .{ .cp = 'C' }); + _ = handleNormal(p.editor(), &pane.body, .{ .cp = ';' }); + _ = handleNormal(p.editor(), &pane.body, .{ .cp = 'x' }); // x at both: still two lines + _ = handleNormal(p.editor(), &pane.body, .{ .cp = '_', .alt = true }); // merged to one try std.testing.expectEqual(@as(u16, 0), pane.body.nsel); try std.testing.expectEqual(@as(usize, 0), pane.body.sels.len); } @@ -1503,9 +1541,9 @@ test "cursors that land on one place are one: a key typed there is typed once, a defer p.deinit(); const pane = try p.setTestFile("ab\ncd\n"); // A cursor below, then both up: the second lands on the first. - handleNormal(p.editor(), &pane.body, .{ .cp = 'C' }); + _ = handleNormal(p.editor(), &pane.body, .{ .cp = 'C' }); try std.testing.expectEqual(@as(u16, 1), pane.body.nsel); - handleNormal(p.editor(), &pane.body, .{ .cp = 'k' }); + _ = handleNormal(p.editor(), &pane.body, .{ .cp = 'k' }); try std.testing.expectEqual(@as(u16, 0), pane.body.nsel); p.update(.{ .key = .{ .cp = 'i', .text = "i" } }); p.update(.{ .key = .{ .cp = 'x', .text = "x" } }); @@ -1518,10 +1556,10 @@ test "r and Delete take whole graphemes" { defer p.deinit(); const pane = try p.setTestFile("A" ++ "e\u{301}" ++ "👩🏽\u{200d}🚀" ++ "🇧🇷" ++ "界" ++ "Z\n"); // é, the astronaut and the flag: three graphemes, three 界 - for ("lv2lr") |c| handleNormal(p.editor(), &pane.body, .{ .cp = c }); - handleNormal(p.editor(), &pane.body, .{ .cp = '界' }); + for ("lv2lr") |c| _ = handleNormal(p.editor(), &pane.body, .{ .cp = c }); + _ = handleNormal(p.editor(), &pane.body, .{ .cp = '界' }); try std.testing.expectEqualStrings("A界界界界Z\n", pane.file.?.content); - for ("u0li") |c| handleNormal(p.editor(), &pane.body, .{ .cp = c }); + for ("u0li") |c| _ = handleNormal(p.editor(), &pane.body, .{ .cp = c }); p.update(.{ .key = .{ .cp = Key.delete } }); try std.testing.expectEqualStrings("A" ++ "👩🏽\u{200d}🚀" ++ "🇧🇷" ++ "界" ++ "Z\n", pane.file.?.content); } |
