From 3b31e0484bd06658f10cd9165b76cb1ca3b198bd Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 5 Nov 2026 00:20:16 -0300 Subject: modal, normal, pdf_view, Text, syntax, edit: one key layer: an Action is the role the key named and what it was given (a count, a literal, ] or [), not a second union of sixteen sub-enums the roles were mapped onto one if at a time; a context's roles are tried in their enum order, the pending prefix and m sub-key are roles too, the text's executor and the PDF's switch flat on the role, and pdf_view.applyNormal(core, pane, action) does what a page cannot (Last, the leader, the tag, a search) instead of handing a seven-value enum back; no key does anything new; Text.normal's walked shape changes (wire semantics); helix-keys.md says how the keys are read Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/normal.zig | 436 +++++++++++++++++++++++++-------------------------------- 1 file changed, 187 insertions(+), 249 deletions(-) (limited to 'src/normal.zig') diff --git a/src/normal.zig b/src/normal.zig index 9e47cf45..8b1cf545 100644 --- a/src/normal.zig +++ b/src/normal.zig @@ -147,7 +147,7 @@ pub fn applySelRegex(p: *Pardes, pane: *Pane, t: *Text, pat: []const u8, mode: m /// 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 { +fn searchStep(p: *Pardes, t: *Text, text: []const u8, forward: bool, 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 { @@ -165,7 +165,7 @@ fn searchStep(p: *Pardes, t: *Text, text: []const u8, direction: modal.Normal.Di const from = @min(old.anchor, old.head); const to = @max(old.anchor, old.head); var wrapped = false; - const found = (if (direction == .forward) blk: { + 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); @@ -350,7 +350,7 @@ pub fn nodeSelect(p: *Pardes, pane: *Pane, t: *Text, walk: modal.Normal.NodeWalk const arena = p.scratch.allocator(); const rs = arena.alloc(modal.Selection, Text.max_selections) catch return; const got = t.ranges(text, 0, rs[0..Text.max_selections]); - if (walk == .shrink) if (f.node_history.pop()) |prev| { + if (walk == .shrink_node) if (f.node_history.pop()) |prev| { defer p.gpa.free(prev.ranges); // back to what Alt-o grew from, while the selection still holds it const holds = for (prev.ranges) |pr| { @@ -385,7 +385,7 @@ pub fn nodeSelect(p: *Pardes, pane: *Pane, t: *Text, walk: modal.Normal.NodeWalk .prev_sibling => .{ .anchor = s.to, .head = s.from }, // helix move_parent_node_end: a one-character range on the // bound, or stretched to it in select mode - .parent_end, .parent_start => bound: { + .parent_node_end, .parent_node_start => bound: { const head = s.from; if (t.select) { const anchor = r.anchor; @@ -400,7 +400,7 @@ pub fn nodeSelect(p: *Pardes, pane: *Pane, t: *Text, walk: modal.Normal.NodeWalk } } const same = m == got.n and std.mem.eql(u8, std.mem.sliceAsBytes(out[0..m]), std.mem.sliceAsBytes(rs[0..m])); - if (walk == .expand and !same) remember: { + if (walk == .expand_node and !same) remember: { const ranges = p.gpa.dupe(modal.Selection, rs[0..got.n]) catch break :remember; f.node_history.append(p.gpa, .{ .ranges = ranges, .primary = got.pri }) catch p.gpa.free(ranges); } @@ -595,19 +595,19 @@ pub fn handleNormal(p: *Pardes, t: *Text, key: Key) void { // `"` 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 semantic = switch (parsed) { + const action = switch (parsed) { .pending, .ignored, .unbound => return, .action => |value| value, }; t.normal.register = 0; p.register = named; defer p.register = 0; - if (t.nsel == 0 or semantic.scope() == .once) - return executeNormalAction(p, t, semantic); - replaySels(p, t, .{ .normal = semantic }); + if (t.nsel == 0 or action.once()) + return executeNormalAction(p, t, action); + replaySels(p, t, .{ .normal = action }); } -fn executeNormalAction(p: *Pardes, t: *Text, semantic: modal.Normal.Action) void { +fn executeNormalAction(p: *Pardes, t: *Text, a: modal.Normal.Action) void { // What reaches past the text -- a builtin, a search, a language query -- // acts for the pane the text belongs to; a header's is the active one. const pane = t.pane() orelse p.panes[p.active] orelse return; @@ -617,13 +617,13 @@ fn executeNormalAction(p: *Pardes, t: *Text, semantic: modal.Normal.Action) void pane.clearPointerSelection(sel_slot); pane.sel[sel_slot].state = .none; }; - if (raw_selection and semantic == .edit and semantic.edit.kind == .yank) return edit.normalYank(p, t); + if (raw_selection and a.role == .yank) return edit.normalYank(p, t); edit.pinPaneCursor(p, t); const text = edit.flatSurface(p, t) catch return; const range = t.primaryRange(text, 0); const cur = modal.selectionCursor(text, range); - switch (semantic) { + switch (a.role) { .escape => { t.select = false; // typed again by a macro, Esc is only helix's @@ -631,89 +631,86 @@ fn executeNormalAction(p: *Pardes, t: *Text, semantic: modal.Normal.Action) void tagline.exitHeader(p); // out of a header too, as Shift-Esc return exec.runBuiltin(p, .Last, p.active, "", null); }, - .goto => |go| switch (go.target) { - .file_start => { - const line = if (go.explicit_count) @min(@as(usize, go.count) - 1, Text.maxLine(text)) else 0; - return t.pointMove(text, range, modal.lineStartOffset(text, line)); - }, - .last_line => return t.pointMove(text, range, modal.lineStartOffset(text, Text.maxLine(text))), - .line_start => return t.pointMove(text, range, modal.lineStartOffset(text, modal.lineAtOffset(text, cur))), - .line_end => { - const line = modal.lineAtOffset(text, cur); - const ls = modal.lineStartOffset(text, line); - return t.pointMove(text, range, @max(ls, modal.prevGrapheme(text, modal.lineEndOffset(text, line)))); - }, - .first_nonws => { - const line = modal.lineAtOffset(text, cur); - const ls = modal.lineStartOffset(text, line); - const slice = text[ls..modal.lineEndOffset(text, line)]; - const nw = modal.firstNonWs(slice); - if (nw == slice.len) return; - return t.pointMove(text, range, ls + nw); - }, - .line_down => return t.verticalMove(text, range, true, go.count, edit.paneWrapWidth(p, t)), - .line_up => return t.verticalMove(text, range, false, go.count, edit.paneWrapWidth(p, t)), - .column => { - const line = modal.lineAtOffset(text, cur); - const ls = modal.lineStartOffset(text, line); - const slice = text[ls..modal.lineEndOffset(text, line)]; - return t.pointMove(text, range, ls + modal.graphemeAtColumn(slice, @as(usize, go.count) - 1)); - }, - .view_top => return t.gotoWindow(text, range, .top, go.count), - .view_bottom => return t.gotoWindow(text, range, .bottom, go.count), - .last_modification => { - const at = t.last_edit orelse return; - return t.pointMove(text, range, modal.offsetAt(text, .{ .row = @intCast(@max(0, at.row)), .col = @intCast(@max(0, at.col)) })); - }, + // gg and G: a count names the line; bare G is no move (helix) + .goto_file_start, .goto_line => { + if (!a.explicit and a.role == .goto_line) return; + const to = if (a.explicit) @min(@as(usize, a.count) - 1, Text.maxLine(text)) else 0; + return t.pointMove(text, range, modal.lineStartOffset(text, to)); }, - .view => |view| if (t.what == .body) switch (view) { - .top => { - pane.scrollBy(t.cur_row - pane.scroll()); - pane.ensureCursorVisible(); - }, - .center => { - pane.scrollBy(t.cur_row - (pane.scroll() + @divTrunc(@as(i32, pane.rows), 2) - 1)); - pane.ensureCursorVisible(); - }, - .bottom => { - pane.scrollBy(t.cur_row - (pane.scroll() + @as(i32, pane.rows) - 1)); - pane.ensureCursorVisible(); - }, - .middle => { - // helix align_view_middle: the cursor's column to the middle - // of an unwrapped view - panes.File.geometry(pane); - if (pane.body_wrapped) return; - const line = text[modal.lineStartOffset(text, modal.lineAtOffset(text, cur))..modal.lineEndOffset(text, modal.lineAtOffset(text, cur))]; - const w: usize = @intCast(@max(1, @as(i32, pane.cols) - @as(i32, panes.File.gutterWidth(pane)))); - const col = panes.File.rawDisplayCol(line, @intCast(@max(0, t.cur_col))); - pane.hscroll = @intCast(panes.File.rawAtDisplay(line, col -| w / 2)); - }, - .scroll_down => return t.scrollViewMove(text, range, 1), - .scroll_up => return t.scrollViewMove(text, range, -1), + .goto_last_line => return t.pointMove(text, range, modal.lineStartOffset(text, Text.maxLine(text))), + .goto_line_start, .line_start => return t.pointMove(text, range, modal.lineStartOffset(text, modal.lineAtOffset(text, cur))), + .goto_line_end, .line_end => { + const line = modal.lineAtOffset(text, cur); + const ls = modal.lineStartOffset(text, line); + return t.pointMove(text, range, @max(ls, modal.prevGrapheme(text, modal.lineEndOffset(text, line)))); }, - .find => |find| { - t.find_op = find.kind; - t.find_ch = find.char; - return t.findMove(text, range, find.char, find.kind, find.count); + .goto_first_nonws, .line_first_nonws => { + const line = modal.lineAtOffset(text, cur); + const ls = modal.lineStartOffset(text, line); + const slice = text[ls..modal.lineEndOffset(text, line)]; + const nw = modal.firstNonWs(slice); + if (nw == slice.len) return; + return t.pointMove(text, range, ls + nw); }, - .replace_char => |char| return edit.normalReplaceChar(p, t, char), - .match_bracket => { + .goto_line_down => return t.verticalMove(text, range, true, a.count, edit.paneWrapWidth(p, t)), + .goto_line_up => return t.verticalMove(text, range, false, a.count, edit.paneWrapWidth(p, t)), + .goto_column => { + const line = modal.lineAtOffset(text, cur); + const ls = modal.lineStartOffset(text, line); + return t.pointMove(text, range, ls + modal.graphemeAtColumn(text[ls..modal.lineEndOffset(text, line)], @as(usize, a.count) - 1)); + }, + .goto_view_top => return t.gotoWindow(text, range, .top, a.count), + .goto_view_bottom => return t.gotoWindow(text, range, .bottom, a.count), + .goto_last_modification => { + const at = t.last_edit orelse return; + return t.pointMove(text, range, modal.offsetAt(text, .{ .row = @intCast(@max(0, at.row)), .col = @intCast(@max(0, at.col)) })); + }, + .view_top, .view_center, .view_bottom => if (t.what == .body) { + const above: i32 = switch (a.role) { + .view_top => 0, + .view_center => @divTrunc(@as(i32, pane.rows), 2) - 1, + else => @as(i32, pane.rows) - 1, + }; + pane.scrollBy(t.cur_row - (pane.scroll() + above)); + pane.ensureCursorVisible(); + }, + // helix align_view_middle: the cursor's column to the middle of an + // unwrapped view + .view_middle => if (t.what == .body) { + panes.File.geometry(pane); + if (pane.body_wrapped) return; + const line = modal.lineAtOffset(text, cur); + const slice = text[modal.lineStartOffset(text, line)..modal.lineEndOffset(text, line)]; + const w: usize = @intCast(@max(1, @as(i32, pane.cols) - @as(i32, panes.File.gutterWidth(pane)))); + const col = panes.File.rawDisplayCol(slice, @intCast(@max(0, t.cur_col))); + pane.hscroll = @intCast(panes.File.rawAtDisplay(slice, col -| w / 2)); + }, + .view_scroll_down => if (t.what == .body) return t.scrollViewMove(text, range, 1), + .view_scroll_up => if (t.what == .body) return t.scrollViewMove(text, range, -1), + .find_char_fwd, .find_char_back, .till_char_fwd, .till_char_back => { + t.find_op = switch (a.role) { + .find_char_fwd => .forward, + .find_char_back => .backward, + .till_char_fwd => .till_forward, + else => .till_backward, + }; + t.find_ch = a.char; + return t.findMove(text, range, a.char, t.find_op.?, a.count); + }, + .replace_prefix => return edit.normalReplaceChar(p, t, a.char), + // `mm` + .match_prefix => { const lines = edit.paneCursorLines(p, t) catch return; const mc = modal.matchBracket(lines, modal.positionAt(text, cur)) orelse return; return t.pointMove(text, range, modal.offsetAt(text, mc)); }, - .textobject => |object| return edit.textobjectSelect(p, t, text, object.char, object.around), - .surround_add => |char| return edit.surroundAdd(p, t, char), - .surround_delete => |char| return edit.surroundDelete(p, t, char), - .surround_replace => |replace| return edit.surroundReplace(p, t, replace.from, replace.to), - .paragraph => |paragraph| { - const r2 = modal.moveParagraph(text, range, paragraph.count, paragraph.direction == .forward, t.select); - return t.setRange(text, 0, r2, false); - }, - .add_newline => |newline| return edit.addNewline(p, t, newline.direction == .forward, newline.count), - .diagnostic => |diagnostic| { - const fwd = diagnostic.direction == .forward; + .match_inside, .match_around => return edit.textobjectSelect(p, t, text, a.char, a.role == .match_around), + .surround_add => return edit.surroundAdd(p, t, a.char), + .surround_delete => return edit.surroundDelete(p, t, a.char), + .surround_replace => return edit.surroundReplace(p, t, a.from, a.char), + .goto_paragraph => return t.setRange(text, 0, modal.moveParagraph(text, range, a.count, a.forward, t.select), false), + .add_newline => return edit.addNewline(p, t, a.forward, a.count), + .goto_diagnostic, .goto_diagnostic_end => { // Once a Diagnostics query fed them, the diagnostics are this // text's places: ]d and [d step them as n and N do, ]D and [D // go to the last and the first. Until then, asked for. @@ -723,35 +720,27 @@ fn executeNormalAction(p: *Pardes, t: *Text, semantic: modal.Normal.Action) void else false; if (!fed) return p.lspRequest(p.active, .diagnostics, ""); - if (!diagnostic.endpoint) return set.step(p, pane, fwd, 1, false); + if (a.role == .goto_diagnostic) return set.step(p, pane, a.forward, 1, false); if (set.hits.items.len == 0) return; - set.goTo(pane, if (fwd) set.hits.items.len - 1 else 0, false); + set.goTo(pane, if (a.forward) set.hits.items.len - 1 else 0, false); }, - .move => |move| switch (move.motion) { - .left => { - var target = cur; - for (0..move.count) |_| target = modal.prevGrapheme(text, target); - return t.pointMove(text, range, target); - }, - .right => { - var target = cur; - for (0..move.count) |_| target = modal.nextGrapheme(text, target); - return t.pointMove(text, range, target); - }, - .down => return t.verticalMove(text, range, true, move.count, 0), - .up => return t.verticalMove(text, range, false, move.count, 0), - .next_word_start => return t.wordMove(text, range, move.count, .next_word_start), - .prev_word_start => return t.wordMove(text, range, move.count, .prev_word_start), - .next_word_end => return t.wordMove(text, range, move.count, .next_word_end), - .next_long_word_start => return t.wordMove(text, range, move.count, .next_long_word_start), - .prev_long_word_start => return t.wordMove(text, range, move.count, .prev_long_word_start), - .next_long_word_end => return t.wordMove(text, range, move.count, .next_long_word_end), + inline .goto_function, .goto_class, .goto_parameter, .goto_comment, .goto_test, .goto_entry, .goto_xml_element => |role| { + const object = @field(modal.Normal.Object, @tagName(role)["goto_".len..]); + return objectJump(p, pane, t, text, range, object, a.forward, a.count); }, - .repeat_find => |count| { + .move_left, .move_right => { + var target = cur; + for (0..a.count) |_| target = if (a.role == .move_left) modal.prevGrapheme(text, target) else modal.nextGrapheme(text, target); + return t.pointMove(text, range, target); + }, + .move_down => return t.verticalMove(text, range, true, a.count, 0), + .move_up => return t.verticalMove(text, range, false, a.count, 0), + inline .next_word_start, .prev_word_start, .next_word_end, .next_long_word_start, .prev_long_word_start, .next_long_word_end => |role| return t.wordMove(text, range, a.count, @field(modal.WordTarget, @tagName(role))), + .repeat_find => { const kind = t.find_op orelse return; var repeated = range; var moved = false; - for (0..count) |_| { + for (0..a.count) |_| { const cc = modal.selectionCursor(text, repeated); const target = modal.findTarget(text, cc, t.find_ch, kind, 1) orelse break; repeated = if (t.select) @@ -763,112 +752,82 @@ fn executeNormalAction(p: *Pardes, t: *Text, semantic: modal.Normal.Action) void if (!moved) return; return t.setRange(text, 0, repeated, false); }, - .line => |line_kind| switch (line_kind) { - .start => return t.pointMove(text, range, modal.lineStartOffset(text, modal.lineAtOffset(text, cur))), - .end => { - const line = modal.lineAtOffset(text, cur); - const ls = modal.lineStartOffset(text, line); - return t.pointMove(text, range, @max(ls, modal.prevGrapheme(text, modal.lineEndOffset(text, line)))); - }, - .first_nonws => { - const line = modal.lineAtOffset(text, cur); - const ls = modal.lineStartOffset(text, line); - const slice = text[ls..modal.lineEndOffset(text, line)]; - const nw = modal.firstNonWs(slice); - if (nw == slice.len) return; - return t.pointMove(text, range, ls + nw); - }, - }, - .goto_line => |go| { - if (!go.explicit) return; - const line = @min(@as(usize, go.count) - 1, Text.maxLine(text)); - return t.pointMove(text, range, modal.lineStartOffset(text, line)); - }, - .page => |page| switch (page.kind) { - // Counts were parsed historically but these four text view - // actions intentionally move exactly one viewport unit. - .half_down => return t.halfPageMove(text, range, true), - .half_up => return t.halfPageMove(text, range, false), - .down => return t.scrollViewMove(text, range, @as(i32, pane.rows)), - .up => return t.scrollViewMove(text, range, -@as(i32, pane.rows)), - }, - .insert => |insert| { - const where: edit.InsertAt = switch (insert.kind) { - .at => .at, + // These four move exactly one viewport unit, whatever the count. + .half_page_down => return t.halfPageMove(text, range, true), + .half_page_up => return t.halfPageMove(text, range, false), + .page_down => return t.scrollViewMove(text, range, @as(i32, pane.rows)), + .page_up => return t.scrollViewMove(text, range, -@as(i32, pane.rows)), + .insert, .append, .insert_line_start, .insert_line_end, .open_below, .open_above => { + const where: edit.InsertAt = switch (a.role) { + .insert => .at, .append => .append, - .line_start => .line_start, - .line_end => .line_end, + .insert_line_start => .line_start, + .insert_line_end => .line_end, .open_below => .open_below, - .open_above => .open_above, + else => .open_above, }; - return edit.enterInsert(p, t, where, insert.count); + return edit.enterInsert(p, t, where, a.count); }, - .select => |select| switch (select.kind) { - .mode => { - if (t.select) { - t.select = false; + .select_mode => { + if (t.select) { + t.select = false; + } else { + t.select = true; + if (t.msel.active) { + t.setRange(text, 0, range, true); + } else if (!t.vsel.active) { + t.vsel = .{ .active = true, .row = t.cur_row, .col = t.cur_col, .explicit = true }; } else { - t.select = true; - if (t.msel.active) { - t.setRange(text, 0, range, true); - } else if (!t.vsel.active) { - t.vsel = .{ .active = true, .row = t.cur_row, .col = t.cur_col, .explicit = true }; - } else { - t.vsel.explicit = true; - } + t.vsel.explicit = true; } - t.cur_pinned = true; - }, - .line => return t.lineSelect(text, range, select.count), - .line_bounds => return t.lineBoundsSelect(text, range), - .shrink_to_line_bounds => return t.shrinkSelToLineBounds(text, range), - .collapse => return t.setRange(text, 0, .{ .anchor = cur, .head = cur }, false), - .flip => { - const explicit = (t.vsel.active and t.vsel.explicit) or t.msel.active; - return t.setRange(text, 0, .{ .anchor = range.head, .head = range.anchor }, explicit); - }, - .all => return t.setRange(text, 0, .{ .anchor = 0, .head = text.len }, false), + } + t.cur_pinned = true; }, - .multi => |multi| { - if (multi.kind == .keep_primary) return t.dropSels(); - return t.multiSelAction(text, multi.kind, multi.count); + .select_line => return t.lineSelect(text, range, a.count), + .select_line_bounds => return t.lineBoundsSelect(text, range), + .shrink_to_line_bounds => return t.shrinkSelToLineBounds(text, range), + .collapse_selection => return t.setRange(text, 0, .{ .anchor = cur, .head = cur }, false), + .flip_selection => { + const explicit = (t.vsel.active and t.vsel.explicit) or t.msel.active; + return t.setRange(text, 0, .{ .anchor = range.head, .head = range.anchor }, explicit); }, + .select_all => return t.setRange(text, 0, .{ .anchor = 0, .head = text.len }, false), + .keep_primary_sel => return t.dropSels(), + .copy_sel_below, .copy_sel_above, .remove_primary_sel, .rotate_sel_fwd, .rotate_sel_back, .split_sel_newline, .merge_sels, .merge_consecutive_sels, .trim_sels, .ensure_forward => return t.multiSelAction(text, a.role, a.count), // s, S and | answer for the text they are pressed in; `/` searches // the body wherever it is pressed, as acme's Look from a tag does. - .select_regex => |mode| return startSelRegex(p, pane, t, mode), - .edit => |op| switch (op.kind) { - .delete => return edit.normalDelete(p, t, true), - .delete_noyank => return edit.normalDelete(p, t, false), - .change => return edit.normalChange(p, t), - .yank => return edit.normalYank(p, t), - .replace_with_yank => return edit.pasteRegister(p, t, .replace, op.count), - .paste_after => return edit.pasteRegister(p, t, .after, op.count), - .paste_before => return edit.pasteRegister(p, t, .before, op.count), - .switch_case => return edit.normalCase(p, t, .toggle), - .lowercase => return edit.normalCase(p, t, .lower), - .uppercase => return edit.normalCase(p, t, .upper), - .join_lines => return edit.normalJoin(p, t, false), - .join_select_space => return edit.normalJoin(p, t, true), - .rotate_contents_forward => return edit.normalRotateContents(p, t, true, op.count), - .rotate_contents_backward => return edit.normalRotateContents(p, t, false, op.count), - .align_selections => return edit.normalAlign(p, t), - .indent => return edit.normalIndent(p, t, op.count, true), - .unindent => return edit.normalIndent(p, t, op.count, false), - .comment_toggle => return edit.normalToggleComment(p, t), - .undo => return edit.undoStep(p, t, true), - .redo => return edit.undoStep(p, t, false), - }, + inline .select_regex, .split_regex, .keep_regex, .remove_regex => |role| return startSelRegex(p, pane, t, @field(modal.Normal.RegexMode, @tagName(role)[0 .. @tagName(role).len - "_regex".len])), + inline .expand_node, .shrink_node, .next_sibling, .prev_sibling, .all_siblings, .all_children, .parent_node_end, .parent_node_start => |role| return nodeSelect(p, pane, t, @field(modal.Normal.NodeWalk, @tagName(role))), + .delete => return edit.normalDelete(p, t, true), + .delete_noyank => return edit.normalDelete(p, t, false), + .change => return edit.normalChange(p, t), + .yank => return edit.normalYank(p, t), + .replace_with_yank => return edit.pasteRegister(p, t, .replace, a.count), + .paste_after => return edit.pasteRegister(p, t, .after, a.count), + .paste_before => return edit.pasteRegister(p, t, .before, a.count), + .switch_case => return edit.normalCase(p, t, .toggle), + .to_lowercase => return edit.normalCase(p, t, .lower), + .to_uppercase => return edit.normalCase(p, t, .upper), + .join_lines => return edit.normalJoin(p, t, false), + .join_select_space => return edit.normalJoin(p, t, true), + .rotate_contents_fwd => return edit.normalRotateContents(p, t, true, a.count), + .rotate_contents_back => return edit.normalRotateContents(p, t, false, a.count), + .align_selections => return edit.normalAlign(p, t), + .indent => return edit.normalIndent(p, t, a.count, true), + .unindent => return edit.normalIndent(p, t, a.count, false), + .comment_toggle => return edit.normalToggleComment(p, t), + .undo => return edit.undoStep(p, t, true), + .redo => return edit.undoStep(p, t, false), // Language shortcuts run the same builtins as tags and leader commands. - .lsp => |request| return switch (request) { - .definition => exec.runBuiltin(p, .Def, p.active, "", null), - .references => exec.runBuiltin(p, .Refs, p.active, "", null), - .callstack => exec.runBuiltin(p, .Callstack, p.active, "", null), - .declaration => p.lspRequest(p.active, .declaration, ""), - .type_definition => p.lspRequest(p.active, .type_definition, ""), - .implementation => p.lspRequest(p.active, .implementation, ""), - .format => p.lspRequest(p.active, .format, ""), - }, - .adjust_number => |delta| return edit.normalAdjustNumber(p, t, delta), + .goto_definition => return exec.runBuiltin(p, .Def, p.active, "", null), + .goto_references => return exec.runBuiltin(p, .Refs, p.active, "", null), + .goto_callstack => return exec.runBuiltin(p, .Callstack, p.active, "", null), + .goto_declaration => return p.lspRequest(p.active, .declaration, ""), + .goto_type_definition => return p.lspRequest(p.active, .type_definition, ""), + .goto_implementation => return p.lspRequest(p.active, .implementation, ""), + .format => return p.lspRequest(p.active, .format, ""), + .increment => return edit.normalAdjustNumber(p, t, @intCast(a.count)), + .decrement => return edit.normalAdjustNumber(p, t, -@as(i64, @intCast(a.count))), .leader => if (p.macro.replaying == 0) { p.leader_on = true; p.leader_n = 0; @@ -880,7 +839,11 @@ fn executeNormalAction(p: *Pardes, t: *Text, semantic: modal.Normal.Action) void .column, .workspace => tagline.exitHeader(p), .body, .input => tagline.enterTag(p, pane), }, - .pipe_selection => |how| return p.startPrompt(pane, t, .{ .pipe = how }), + .pipe_selection => return p.startPrompt(pane, t, .{ .pipe = .replace }), + .pipe_selection_to => return p.startPrompt(pane, t, .{ .pipe = .ignore }), + .insert_output => return p.startPrompt(pane, t, .{ .pipe = .insert }), + .append_output => return p.startPrompt(pane, t, .{ .pipe = .append }), + .keep_pipe => return p.startPrompt(pane, t, .{ .pipe = .keep }), // `/` in a text is its query pane's line 1 (Query.zig); a // terminal's still asks on the notice band. .search => if (pane.file != null and t.what == .body) @@ -889,12 +852,12 @@ fn executeNormalAction(p: *Pardes, t: *Text, semantic: modal.Normal.Action) void return p.startPrompt(pane, &pane.body, .{ .search = config.search_marker }), // In a file's text, n and N step its places while they answer for // the register (Places.answersN); else helix's search of the register. - .search_step => |step| if (t.what == .body and pane.file != null and pane.places.answersN(p, pane)) - return pane.places.step(p, pane, step.direction == .forward, step.count, t.select) + .search_next, .search_prev => if (t.what == .body and pane.file != null and pane.places.answersN(p, pane)) + return pane.places.step(p, pane, a.role == .search_next, a.count, t.select) else - return searchStep(p, t, text, step.direction, step.count), - .search_selection => |boundary| { - searchSelection(p, t, text, boundary); + return searchStep(p, t, text, a.role == .search_next, a.count), + .search_selection, .search_selection_word => { + searchSelection(p, t, text, a.role == .search_selection_word); // The pattern is line 1 of this text's query pane, made or // reused, the keyboard staying here: n and N step its places. if (t.what == .body and pane.file != null and !pane.hasPdf() and pane.query == null) { @@ -904,9 +867,7 @@ fn executeNormalAction(p: *Pardes, t: *Text, semantic: modal.Normal.Action) void pane.places.provenance = .search; } }, - .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), .macro_record => { const m = &p.macro; if (m.recording == 0) { @@ -922,14 +883,14 @@ fn executeNormalAction(p: *Pardes, t: *Text, semantic: modal.Normal.Action) void p.registers.put(p.gpa, m.recording, keys, 0, 1, true); m.recording = 0; }, - .macro_replay => |count| { + .macro_replay => { const name: u21 = if (p.register != 0) p.register else '@'; const notation = p.registers.value(name, 0) orelse return; const keys = Macro.decode(p.scratch.allocator(), notation) catch return; p.register = 0; - for (0..count) |_| p.replayKeys(keys); + for (0..a.count) |_| p.replayKeys(keys); }, - .repeat_insert => |count| { + .repeat_insert => { const m = &p.macro; if (m.last_insert.items.len == 0 or m.repeating) return; if (m.inserting) return; // the session is still open @@ -941,47 +902,24 @@ fn executeNormalAction(p: *Pardes, t: *Text, semantic: modal.Normal.Action) void const typed = @min(m.typed_from, keys.len); p.replayKeys(keys[0..typed]); if (keys.len == typed) return; - for (0..count) |_| p.replayKeys(keys[typed .. keys.len - 1]); + for (0..a.count) |_| p.replayKeys(keys[typed .. keys.len - 1]); p.replayKeys(keys[keys.len - 1 ..]); }, + // prefix keys: what they begin is the command + .goto_prefix, .view_prefix, .next_prefix, .prev_prefix, .register_prefix, .view_prefix_sticky => {}, } } pub fn handlePdfNormal(p: *Pardes, pane: *Pane, key: Key) void { if (comptime !pdf_enabled) return; if (pane.pdf == null) return; - if (pane.body.normal.prefix == .none and isPrefix(key, 'f')) { + if (pane.body.normal.prefix == null and isPrefix(key, 'f')) { pane.body.normal.clear(); return exec.runBuiltin(p, .PdfSections, p.active, "", null); } - const parsed = modal.Normal.parse(&pane.body.normal, normalInput(key)); - switch (parsed) { + switch (modal.Normal.parse(&pane.body.normal, normalInput(key))) { .pending, .ignored, .unbound => {}, - .action => |semantic| { - const result = panes.Pdf.applyNormal( - pane.pdf.?, - p.pdf_gpa, - semantic, - p.native_images, - .{ .w = p.cell_pixels.w, .h = p.cell_pixels.h }, - panes.Pdf.paneViewport(p, pane), - panes.Pdf.paneGeometry(p, pane), - ); - if (result.page_changed) panes.Pdf.resetPageChrome(pane); - switch (result.host) { - .none => {}, - // typed again by a macro, Esc is only the page's - .last => if (p.macro.replaying == 0) exec.runBuiltin(p, .Last, p.active, "", null), - .leader => { - p.leader_on = true; - p.leader_n = 0; - }, - .command_line => tagline.enterTag(p, pane), - .search => p.startPrompt(pane, &pane.body, .{ .search = config.search_marker }), - .search_forward => look.lookWalk(p, 1), - .search_backward => look.lookWalk(p, -1), - } - }, + .action => |action| panes.Pdf.applyNormal(p, pane, action), } } @@ -1073,7 +1011,7 @@ test "flat text movement and selection replay need no scratch rows" { try std.testing.expectEqual(@as(i32, 1), pane.body.cur_col); handleNormal(p, &pane.body, .{ .cp = 'h' }); try std.testing.expectEqual(@as(i32, 0), pane.body.cur_col); - executeNormalAction(p, &pane.body, .{ .textobject = .{ .char = 'w', .around = false } }); + executeNormalAction(p, &pane.body, .{ .role = .match_inside, .char = 'w' }); try std.testing.expectEqual(@as(i32, 2), pane.body.cur_col); try std.testing.expectEqual(@as(i32, 0), pane.body.vsel.col); @@ -1095,11 +1033,11 @@ test "flat text movement and selection replay need no scratch rows" { pane.body.nsel = 0; pane.body.cur_col = 4; - executeNormalAction(p, &pane.body, .match_bracket); + executeNormalAction(p, &pane.body, .{ .role = .match_prefix }); try std.testing.expect(denied.has_induced_failure); try std.testing.expectEqual(@as(i32, 4), pane.body.cur_col); denied.fail_index = std.math.maxInt(usize); - executeNormalAction(p, &pane.body, .match_bracket); + executeNormalAction(p, &pane.body, .{ .role = .match_prefix }); try std.testing.expectEqual(@as(i32, 8), pane.body.cur_col); } -- cgit v1.3