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/Query.zig | 2 +- src/Text.zig | 20 +- src/edit.zig | 4 +- src/modal.zig | 544 ++++++++++++++++--------------------------------------- src/normal.zig | 436 +++++++++++++++++++------------------------- src/pdf_view.zig | 185 +++++-------------- src/syntax.zig | 10 +- 7 files changed, 407 insertions(+), 794 deletions(-) (limited to 'src') diff --git a/src/Query.zig b/src/Query.zig index a0ccd0e5..84725085 100644 --- a/src/Query.zig +++ b/src/Query.zig @@ -1985,7 +1985,7 @@ pub fn key(p: *Pardes, id: usize, k: Key) bool { // shown where it points, the keyboard staying), Enter on a hit row lands // on it, as Enter typing does. if (t.mode == .normal) { - if (t.normal.prefix != .none or k.ctrl or k.alt or t.normal.count != 0) return false; + if (t.normal.prefix != null or k.ctrl or k.alt or t.normal.count != 0) return false; if (k.cp == 'n' or k.cp == 'N') { step(p, id, if (k.cp == 'n') 1 else -1); // The caret on the rows goes to the current row, as j and k diff --git a/src/Text.zig b/src/Text.zig index 1fbf8321..1d0df183 100644 --- a/src/Text.zig +++ b/src/Text.zig @@ -328,12 +328,12 @@ pub fn setRanges(t: *Text, text: []const u8, in: []const modal.Selection, sticky t.nsel = @intCast(w); } -pub fn multiSelAction(t: *Text, text: []const u8, kind: modal.Normal.Multi, cnt: usize) void { +pub fn multiSelAction(t: *Text, text: []const u8, role: modal.Normal.Role, cnt: usize) void { var rs: [max_selections]modal.Selection = undefined; const got = ranges(t, text, 0, &rs); const n = got.n; const expl = (t.vsel.active and t.vsel.explicit) or t.msel.active; - if (kind == .remove_primary) { + if (role == .remove_primary_sel) { if (n < 2) return; // helix: "no selections remaining" var out: [max_selections]modal.Selection = undefined; var m: usize = 0; @@ -346,12 +346,12 @@ pub fn multiSelAction(t: *Text, text: []const u8, kind: modal.Normal.Multi, cnt: // previous one when the primary was last return setRanges(t, text, out[0..m], &.{}, @min(got.pri, m - 1), expl); } - if (kind == .rotate_forward or kind == .rotate_backward) { + if (role == .rotate_sel_fwd or role == .rotate_sel_back) { const turn = cnt % n; - const pri = if (kind == .rotate_forward) (got.pri + turn) % n else (got.pri + (n - turn)) % n; + const pri = if (role == .rotate_sel_fwd) (got.pri + turn) % n else (got.pri + (n - turn)) % n; return setRanges(t, text, rs[0..n], &.{}, pri, expl); } - if (kind == .merge) { + if (role == .merge_sels) { // helix merge_selections: first.merge(last) — the ranges are // sorted, so that is simply the whole span const lo = @min(rs[0].anchor, rs[0].head); @@ -360,7 +360,7 @@ pub fn multiSelAction(t: *Text, text: []const u8, kind: modal.Normal.Multi, cnt: const one: modal.Selection = if (rev) .{ .anchor = hi, .head = lo } else .{ .anchor = lo, .head = hi }; return setRanges(t, text, &.{one}, &.{}, 0, expl); } - if (kind == .merge_consecutive) { + if (role == .merge_consecutive_sels) { var out: [max_selections]modal.Selection = undefined; var m: usize = 0; var pri: usize = 0; @@ -378,7 +378,7 @@ pub fn multiSelAction(t: *Text, text: []const u8, kind: modal.Normal.Multi, cnt: } return setRanges(t, text, out[0..m], &.{}, pri, expl); } - if (kind == .ensure_forward) { + if (role == .ensure_forward) { // helix ensure_selections_forward: every range points forward for (rs[0..n]) |*r| { const from = @min(r.anchor, r.head); @@ -387,7 +387,7 @@ pub fn multiSelAction(t: *Text, text: []const u8, kind: modal.Normal.Multi, cnt: } return setRanges(t, text, rs[0..n], &.{}, got.pri, expl); } - if (kind == .split_newline) { + if (role == .split_sel_newline) { // helix selection::split_on_newline — one range per line the // selection covers, the newlines themselves left out var out: [max_selections]modal.Selection = undefined; @@ -418,7 +418,7 @@ pub fn multiSelAction(t: *Text, text: []const u8, kind: modal.Normal.Multi, cnt: if (m == 0) return; return setRanges(t, text, out[0..m], &.{}, 0, true); // helix keeps primary 0 } - if (kind == .trim) { + if (role == .trim_sels) { // helix trim_selections: whitespace off both ends; ranges that are // empty or all whitespace are dropped entirely var out: [max_selections]modal.Selection = undefined; @@ -450,7 +450,7 @@ pub fn multiSelAction(t: *Text, text: []const u8, kind: modal.Normal.Multi, cnt: } return setRanges(t, text, out[0..m], &.{}, pri, expl); } - const below = kind == .copy_below; + const below = role == .copy_sel_below; var out: [max_selections]modal.Selection = undefined; var m: usize = 0; var pri: usize = 0; diff --git a/src/edit.zig b/src/edit.zig index 41859273..93a6f308 100644 --- a/src/edit.zig +++ b/src/edit.zig @@ -182,7 +182,7 @@ pub fn clipYank(p: *Pardes, pane: *Pane, main_only: bool) void { return; }; if (pane.body.nsel > 0 and !main_only) { - normal.replaySels(p, &pane.body, .{ .normal = .{ .edit = .{ .kind = .yank, .count = 1 } } }); + normal.replaySels(p, &pane.body, .{ .normal = .{ .role = .yank } }); } else { const others = pane.body.nsel; pane.body.nsel = 0; @@ -1020,7 +1020,7 @@ fn insertEdit(p: *Pardes, t: *Text, key: Key) void { const pane = t.pane() orelse break :smart; const f = pane.file orelse break :smart; if (!syntax.supportsPath(f.path)) break :smart; - return normal.nodeSelect(p, pane, t, .parent_end); + return normal.nodeSelect(p, pane, t, .parent_node_end); } insertTab(p, t); }, diff --git a/src/modal.zig b/src/modal.zig index a879b1dd..16a7ee8b 100644 --- a/src/modal.zig +++ b/src/modal.zig @@ -2,6 +2,10 @@ const std = @import("std"); const uucode = @import("uucode"); pub const Normal = struct { + /// Every normal-mode command, by the name config.zig binds it under. + /// Grouped by the context a key is read in (after `g`, `z`, `m`, `]` or + /// `[`, or none), and within each group in the order a key is tried: + /// the first role a key has is the command. pub const Role = enum { escape, @@ -161,6 +165,18 @@ pub const Normal = struct { search_selection_word, }; + /// The roles from `from` to `to`, in the order a context tries them. + fn roles(comptime from: Role, comptime to: Role) [@intFromEnum(to) - @intFromEnum(from) + 1]Role { + var out: [@intFromEnum(to) - @intFromEnum(from) + 1]Role = undefined; + for (&out, @intFromEnum(from)..) |*role, i| role.* = @enumFromInt(i); + return out; + } + const goto_roles: []const Role = &roles(.goto_file_start, .goto_callstack); + const view_roles: []const Role = &(roles(.view_top, .view_scroll_up) ++ roles(.half_page_down, .page_up)); + const match_roles: []const Role = &roles(.match_inside, .surround_delete); + const bracket_roles: []const Role = &roles(.goto_paragraph, .goto_xml_element); + const command_roles: []const Role = &roles(.move_left, .search_selection_word); + pub const Input = struct { roles: std.EnumSet(Role) = .empty, cp: u21, @@ -177,34 +193,15 @@ pub const Normal = struct { } }; - pub const Prefix = enum(u8) { - none, - goto, - view, - match, - find_fwd, - find_back, - till_fwd, - till_back, - replace, - next, - prev, - register, - }; - - pub const MatchSub = enum(u8) { - none, - inside, - around, - surround_add, - surround_replace, - surround_delete, - }; - pub const State = struct { count: u32 = 0, - prefix: Prefix = .none, - match_sub: MatchSub = .none, + /// The prefix key read (`g`, `z`, `m`, `f`, `]`, `"`...), its + /// continuation awaited. + prefix: ?Role = null, + /// After `m`: which of `mi`, `ma`, `ms`, `mr`, `md` waits for its + /// character. + match_sub: ?Role = null, + /// `mr`'s pair to replace, while its new one is awaited. held_char: u21 = 0, /// `"`: the register the next command names, 0 for none register: u21 = 0, @@ -213,7 +210,7 @@ pub const Normal = struct { /// Drop what is pending; sticky view mode stays armed through it. pub fn clear(state: *State) void { - state.* = if (state.sticky) .{ .sticky = true, .prefix = .view } else .{}; + state.* = if (state.sticky) .{ .sticky = true, .prefix = .view_prefix } else .{}; } }; @@ -237,164 +234,83 @@ pub const Normal = struct { } }; - /// helix's tree-sitter selection keys: where a range goes in the tree /// helix's tree-sitter textobjects, by what their queries call them pub const Object = enum { function, class, parameter, comment, @"test", entry, xml_element }; - pub const NodeWalk = enum { expand, shrink, next_sibling, prev_sibling, all_siblings, all_children, parent_end, parent_start }; + /// helix's tree-sitter selection keys: where a range goes in the tree + pub const NodeWalk = enum { expand_node, shrink_node, next_sibling, prev_sibling, all_siblings, all_children, parent_node_end, parent_node_start }; /// what the s/S/K/Alt-K prompt does with its pattern's matches pub const RegexMode = enum { select, split, keep, remove }; - pub const Scope = enum { once, per_selection }; - pub const Direction = enum { backward, forward }; - pub const Motion = enum { - left, - right, - down, - up, - next_word_start, - prev_word_start, - next_word_end, - next_long_word_start, - prev_long_word_start, - next_long_word_end, - }; - pub const Goto = enum { - file_start, - last_line, - line_start, - line_end, - first_nonws, - line_down, - line_up, - column, - view_top, - view_bottom, - last_modification, - }; - pub const View = enum { top, center, bottom, middle, scroll_down, scroll_up }; + /// f/F/t/T: to the character (or till it), forward or back pub const Find = enum { forward, backward, till_forward, till_backward }; - pub const Line = enum { start, end, first_nonws }; - pub const Page = enum { half_down, half_up, down, up }; - pub const Insert = enum { at, append, line_start, line_end, open_below, open_above }; - pub const Select = enum { - mode, - line, - line_bounds, - shrink_to_line_bounds, - collapse, - flip, - all, - }; - pub const Multi = enum { - copy_below, - copy_above, - keep_primary, - remove_primary, - rotate_forward, - rotate_backward, - split_newline, - merge, - merge_consecutive, - trim, - ensure_forward, - }; - pub const Edit = enum { - delete, - delete_noyank, - change, - yank, - replace_with_yank, - paste_after, - paste_before, - switch_case, - lowercase, - uppercase, - join_lines, - join_select_space, - rotate_contents_forward, - rotate_contents_backward, - align_selections, - indent, - unindent, - comment_toggle, - undo, - redo, - }; - pub const Lsp = enum { definition, declaration, type_definition, implementation, references, callstack, format }; - - pub const Counted = struct { - count: u32, - explicit: bool, - }; - - pub const Action = union(enum) { - escape, - goto: struct { target: Goto, count: u32, explicit_count: bool }, - view: View, - find: struct { kind: Find, char: u21, count: u32 }, - replace_char: u21, - match_bracket, - textobject: struct { char: u21, around: bool }, - surround_add: u21, - surround_delete: u21, - surround_replace: struct { from: u21, to: u21 }, - paragraph: struct { direction: Direction, count: u32 }, - add_newline: struct { direction: Direction, count: u32 }, - diagnostic: struct { direction: Direction, endpoint: bool }, - move: struct { motion: Motion, count: u32 }, - repeat_find: u32, - line: Line, - goto_line: Counted, - page: struct { kind: Page, count: u32 }, - insert: struct { kind: Insert, count: u32 }, - select: struct { kind: Select, count: u32 }, - multi: struct { kind: Multi, count: u32 }, - select_regex: RegexMode, - edit: struct { kind: Edit, count: u32 }, - lsp: Lsp, - adjust_number: i64, - leader, - command_line, - pipe_selection: PipeBehavior, - search, - search_step: struct { direction: Direction, count: u32 }, - search_selection: bool, - goto_word, - macro_record, - macro_replay: u32, - node: NodeWalk, - object_jump: struct { object: Object, direction: Direction, count: u32 }, - repeat_insert: u32, - pub fn scope(value: Action) Scope { - return switch (value) { + /// What a key sequence asks for: the command and what it was given. The + /// text's executor (normal.zig) and a PDF's (pdf_view.zig) both take it. + pub const Action = struct { + role: Role, + count: u32 = 1, + /// The count was typed (`5G`, `3gg`), not the 1 a bare key means. + explicit: bool = false, + /// The literal the command takes: f/t's target, r's replacement, the + /// pair of `mi`, `ma`, `ms` and `md`, and `mr`'s new one. + char: u21 = 0, + /// `mr`'s pair to replace. + from: u21 = 0, + /// After `]` (true) or `[` (false). + forward: bool = true, + + /// Run once for the whole selection, not at each range. + pub fn once(a: Action) bool { + return switch (a.role) { .escape, - .multi, + .copy_sel_below, + .copy_sel_above, + .keep_primary_sel, + .remove_primary_sel, + .rotate_sel_fwd, + .rotate_sel_back, + .split_sel_newline, + .merge_sels, + .merge_consecutive_sels, + .trim_sels, + .ensure_forward, .select_regex, + .split_regex, + .keep_regex, + .remove_regex, + .expand_node, + .shrink_node, + .next_sibling, + .prev_sibling, + .all_siblings, + .all_children, + .parent_node_end, + .parent_node_start, + .join_lines, + .join_select_space, + .rotate_contents_fwd, + .rotate_contents_back, + .align_selections, + .comment_toggle, + .undo, + .redo, .leader, .command_line, .pipe_selection, - .search, - .search_step, - .search_selection, + .pipe_selection_to, + .insert_output, + .append_output, + .keep_pipe, .macro_record, .macro_replay, - .node, - .goto_word, .repeat_insert, - => .once, - .edit => |edit| switch (edit.kind) { - .join_lines, - .join_select_space, - .rotate_contents_forward, - .rotate_contents_backward, - .align_selections, - .comment_toggle, - .undo, - .redo, - => .once, - else => .per_selection, - }, - else => .per_selection, + .search, + .search_next, + .search_prev, + .search_selection, + .search_selection_word, + .goto_word, + => true, + else => false, }; } }; @@ -406,18 +322,12 @@ pub const Normal = struct { action: Action, }; - fn resultAction(value: Action) Result { - return .{ .action = value }; - } - - fn armPrefix(state: *State, prefix: Prefix, saved_count: u32) Result { - state.prefix = prefix; - state.count = saved_count; - if (prefix == .match) { - state.match_sub = .none; - state.held_char = 0; - } - return .pending; + /// The first of `among` the key has, with the count it was given; `none` + /// when it has none of them. + fn first(key: Input, comptime among: []const Role, count: u32, explicit: bool, forward: bool, none: Result) Result { + inline for (among) |role| if (key.has(role)) + return .{ .action = .{ .role = role, .count = count, .explicit = explicit, .forward = forward } }; + return none; } pub fn parse(state: *State, key: Input) Result { @@ -426,12 +336,12 @@ pub const Normal = struct { const sticky = state.sticky; state.* = .{}; if (sticky) return .ignored; - return resultAction(.escape); + return .{ .action = .{ .role = .escape } }; } // A digit is a count only before a command/prefix. A leading zero keeps // its configured line-start role; after another digit it extends count. - if (state.prefix == .none and !key.ctrl and !key.alt and + if (state.prefix == null and !key.ctrl and !key.alt and key.cp >= '0' and key.cp <= '9' and !(key.cp == '0' and state.count == 0)) { @@ -440,228 +350,88 @@ pub const Normal = struct { return .pending; } - const counted: Counted = .{ .count = @max(1, state.count), .explicit = state.count != 0 }; + const explicit = state.count != 0; + const count = @max(1, state.count); state.count = 0; - const count = counted.count; - - switch (state.prefix) { - .goto => { - state.prefix = .none; - // each target's role is its name after `goto_` - inline for (comptime std.enums.values(Goto)) |target| - if (key.has(@field(Role, "goto_" ++ @tagName(target)))) return resultAction(.{ .goto = .{ .target = target, .count = count, .explicit_count = counted.explicit } }); - if (key.has(.goto_word)) return resultAction(.goto_word); - inline for (.{ .definition, .declaration, .type_definition, .implementation, .references, .callstack }) |lsp| - if (key.has(@field(Role, "goto_" ++ @tagName(lsp)))) return resultAction(.{ .lsp = lsp }); - return .ignored; - }, - .view => { - if (!state.sticky) state.prefix = .none; - if (key.has(.view_top)) return resultAction(.{ .view = .top }); - if (key.has(.view_middle)) return resultAction(.{ .view = .middle }); - if (key.has(.view_center)) return resultAction(.{ .view = .center }); - if (key.has(.view_bottom)) return resultAction(.{ .view = .bottom }); - if (key.has(.view_scroll_down)) return resultAction(.{ .view = .scroll_down }); - if (key.has(.view_scroll_up)) return resultAction(.{ .view = .scroll_up }); - if (key.has(.half_page_down)) return resultAction(.{ .page = .{ .kind = .half_down, .count = count } }); - if (key.has(.half_page_up)) return resultAction(.{ .page = .{ .kind = .half_up, .count = count } }); - if (key.has(.page_down)) return resultAction(.{ .page = .{ .kind = .down, .count = count } }); - if (key.has(.page_up)) return resultAction(.{ .page = .{ .kind = .up, .count = count } }); - return .ignored; + + if (state.prefix) |prefix| switch (prefix) { + .goto_prefix => { + state.prefix = null; + return first(key, goto_roles, count, explicit, true, .ignored); }, - .find_fwd, .find_back, .till_fwd, .till_back => |prefix| { - state.prefix = .none; - const char = key.literal() orelse return .ignored; - const kind: Find = switch (prefix) { - .find_fwd => .forward, - .find_back => .backward, - .till_fwd => .till_forward, - .till_back => .till_backward, - else => unreachable, - }; - return resultAction(.{ .find = .{ .kind = kind, .char = char, .count = count } }); + .view_prefix => { + if (!state.sticky) state.prefix = null; + return first(key, view_roles, count, explicit, true, .ignored); }, - .replace => { - state.prefix = .none; + .find_char_fwd, .find_char_back, .till_char_fwd, .till_char_back, .replace_prefix => { + state.prefix = null; const char = key.literal() orelse return .ignored; - return resultAction(.{ .replace_char = char }); + return .{ .action = .{ .role = prefix, .count = count, .explicit = explicit, .char = char } }; }, - .match => { - if (state.match_sub == .none) { + .match_prefix => { + const sub = state.match_sub orelse { + // `mm`: the bracket matching the one under the cursor if (key.has(.match_prefix)) { - state.prefix = .none; - return resultAction(.match_bracket); + state.prefix = null; + return .{ .action = .{ .role = .match_prefix } }; } - const sub: MatchSub = if (key.has(.match_inside)) - .inside - else if (key.has(.match_around)) - .around - else if (key.has(.surround_add)) - .surround_add - else if (key.has(.surround_replace)) - .surround_replace - else if (key.has(.surround_delete)) - .surround_delete - else { - state.prefix = .none; - return .ignored; + inline for (match_roles) |role| if (key.has(role)) { + state.match_sub = role; + return .pending; }; - state.match_sub = sub; - return .pending; - } + state.prefix = null; + return .ignored; + }; const char = key.literal() orelse { state.clear(); return .ignored; }; - if (state.match_sub == .surround_replace and state.held_char == 0) { + if (sub == .surround_replace and state.held_char == 0) { state.held_char = char; return .pending; } - const sub = state.match_sub; const from = state.held_char; state.clear(); - return switch (sub) { - .inside => resultAction(.{ .textobject = .{ .char = char, .around = false } }), - .around => resultAction(.{ .textobject = .{ .char = char, .around = true } }), - .surround_add => resultAction(.{ .surround_add = char }), - .surround_delete => resultAction(.{ .surround_delete = char }), - .surround_replace => resultAction(.{ .surround_replace = .{ .from = from, .to = char } }), - .none => unreachable, - }; + return .{ .action = .{ .role = sub, .char = char, .from = from } }; }, - .next, .prev => |prefix| { - state.prefix = .none; - const direction: Direction = if (prefix == .next) .forward else .backward; - if (key.has(.goto_paragraph)) return resultAction(.{ .paragraph = .{ .direction = direction, .count = count } }); - if (key.has(.add_newline)) return resultAction(.{ .add_newline = .{ .direction = direction, .count = count } }); - if (key.has(.goto_diagnostic)) return resultAction(.{ .diagnostic = .{ .direction = direction, .endpoint = false } }); - if (key.has(.goto_diagnostic_end)) return resultAction(.{ .diagnostic = .{ .direction = direction, .endpoint = true } }); - inline for (.{ .{ .goto_function, .function }, .{ .goto_class, .class }, .{ .goto_parameter, .parameter }, .{ .goto_comment, .comment }, .{ .goto_test, .@"test" }, .{ .goto_entry, .entry }, .{ .goto_xml_element, .xml_element } }) |pair| - if (key.has(pair[0])) return resultAction(.{ .object_jump = .{ .object = pair[1], .direction = direction, .count = count } }); - return .ignored; + .next_prefix, .prev_prefix => { + state.prefix = null; + return first(key, bracket_roles, count, explicit, prefix == .next_prefix, .ignored); }, - .register => { - state.prefix = .none; + .register_prefix => { + state.prefix = null; const char = key.literal() orelse return .ignored; state.register = char; // the count typed before `"` is still the command's - state.count = if (counted.explicit) count else 0; + state.count = if (explicit) count else 0; return .pending; }, - .none => {}, - } + else => unreachable, + }; // Prefix setters retain the count for their continuation. - if (key.has(.goto_prefix)) return armPrefix(state, .goto, if (counted.explicit) count else 0); - if (key.has(.view_prefix)) return armPrefix(state, .view, if (counted.explicit) count else 0); + inline for (.{ .goto_prefix, .view_prefix, .find_char_fwd, .find_char_back, .till_char_fwd, .till_char_back, .replace_prefix, .next_prefix, .prev_prefix, .register_prefix }) |prefix| + if (key.has(prefix)) { + state.prefix = prefix; + state.count = if (explicit) count else 0; + return .pending; + }; if (key.has(.view_prefix_sticky)) { state.sticky = true; - return armPrefix(state, .view, 0); + state.prefix = .view_prefix; + return .pending; + } + if (key.has(.match_prefix)) { + state.prefix = .match_prefix; + state.match_sub = null; + state.held_char = 0; + return .pending; } - if (key.has(.find_char_fwd)) return armPrefix(state, .find_fwd, if (counted.explicit) count else 0); - if (key.has(.find_char_back)) return armPrefix(state, .find_back, if (counted.explicit) count else 0); - if (key.has(.till_char_fwd)) return armPrefix(state, .till_fwd, if (counted.explicit) count else 0); - if (key.has(.till_char_back)) return armPrefix(state, .till_back, if (counted.explicit) count else 0); - if (key.has(.replace_prefix)) return armPrefix(state, .replace, if (counted.explicit) count else 0); - if (key.has(.next_prefix)) return armPrefix(state, .next, if (counted.explicit) count else 0); - if (key.has(.prev_prefix)) return armPrefix(state, .prev, if (counted.explicit) count else 0); - if (key.has(.match_prefix)) return armPrefix(state, .match, 0); - if (key.has(.register_prefix)) return armPrefix(state, .register, if (counted.explicit) count else 0); - - if (key.has(.move_left)) return resultAction(.{ .move = .{ .motion = .left, .count = count } }); - if (key.has(.move_right)) return resultAction(.{ .move = .{ .motion = .right, .count = count } }); - if (key.has(.move_down)) return resultAction(.{ .move = .{ .motion = .down, .count = count } }); - if (key.has(.move_up)) return resultAction(.{ .move = .{ .motion = .up, .count = count } }); - if (key.has(.next_word_start)) return resultAction(.{ .move = .{ .motion = .next_word_start, .count = count } }); - if (key.has(.prev_word_start)) return resultAction(.{ .move = .{ .motion = .prev_word_start, .count = count } }); - if (key.has(.next_word_end)) return resultAction(.{ .move = .{ .motion = .next_word_end, .count = count } }); - if (key.has(.next_long_word_start)) return resultAction(.{ .move = .{ .motion = .next_long_word_start, .count = count } }); - if (key.has(.prev_long_word_start)) return resultAction(.{ .move = .{ .motion = .prev_long_word_start, .count = count } }); - if (key.has(.next_long_word_end)) return resultAction(.{ .move = .{ .motion = .next_long_word_end, .count = count } }); - if (key.has(.repeat_find)) return resultAction(.{ .repeat_find = count }); - if (key.has(.line_start)) return resultAction(.{ .line = .start }); - if (key.has(.line_end)) return resultAction(.{ .line = .end }); - if (key.has(.line_first_nonws)) return resultAction(.{ .line = .first_nonws }); - if (key.has(.goto_line)) return resultAction(.{ .goto_line = counted }); - if (key.has(.half_page_down)) return resultAction(.{ .page = .{ .kind = .half_down, .count = count } }); - if (key.has(.half_page_up)) return resultAction(.{ .page = .{ .kind = .half_up, .count = count } }); - if (key.has(.page_down)) return resultAction(.{ .page = .{ .kind = .down, .count = count } }); - if (key.has(.page_up)) return resultAction(.{ .page = .{ .kind = .up, .count = count } }); - - inline for (.{ .{ .insert, .at }, .{ .append, .append }, .{ .insert_line_start, .line_start }, .{ .insert_line_end, .line_end }, .{ .open_below, .open_below }, .{ .open_above, .open_above } }) |pair| - if (key.has(pair[0])) return resultAction(.{ .insert = .{ .kind = pair[1], .count = count } }); - - inline for (.{ .{ .select_mode, .mode }, .{ .select_line, .line }, .{ .select_line_bounds, .line_bounds }, .{ .shrink_to_line_bounds, .shrink_to_line_bounds }, .{ .collapse_selection, .collapse }, .{ .flip_selection, .flip }, .{ .select_all, .all } }) |pair| - if (key.has(pair[0])) return resultAction(.{ .select = .{ .kind = pair[1], .count = count } }); - - if (key.has(.copy_sel_below)) return resultAction(.{ .multi = .{ .kind = .copy_below, .count = count } }); - if (key.has(.copy_sel_above)) return resultAction(.{ .multi = .{ .kind = .copy_above, .count = count } }); - if (key.has(.keep_primary_sel)) return resultAction(.{ .multi = .{ .kind = .keep_primary, .count = count } }); - if (key.has(.remove_primary_sel)) return resultAction(.{ .multi = .{ .kind = .remove_primary, .count = count } }); - if (key.has(.rotate_sel_fwd)) return resultAction(.{ .multi = .{ .kind = .rotate_forward, .count = count } }); - if (key.has(.rotate_sel_back)) return resultAction(.{ .multi = .{ .kind = .rotate_backward, .count = count } }); - if (key.has(.split_sel_newline)) return resultAction(.{ .multi = .{ .kind = .split_newline, .count = count } }); - if (key.has(.merge_sels)) return resultAction(.{ .multi = .{ .kind = .merge, .count = count } }); - if (key.has(.merge_consecutive_sels)) return resultAction(.{ .multi = .{ .kind = .merge_consecutive, .count = count } }); - if (key.has(.trim_sels)) return resultAction(.{ .multi = .{ .kind = .trim, .count = count } }); - if (key.has(.select_regex)) return resultAction(.{ .select_regex = .select }); - if (key.has(.split_regex)) return resultAction(.{ .select_regex = .split }); - if (key.has(.keep_regex)) return resultAction(.{ .select_regex = .keep }); - if (key.has(.remove_regex)) return resultAction(.{ .select_regex = .remove }); - if (key.has(.ensure_forward)) return resultAction(.{ .multi = .{ .kind = .ensure_forward, .count = count } }); - if (key.has(.expand_node)) return resultAction(.{ .node = .expand }); - if (key.has(.shrink_node)) return resultAction(.{ .node = .shrink }); - if (key.has(.next_sibling)) return resultAction(.{ .node = .next_sibling }); - if (key.has(.prev_sibling)) return resultAction(.{ .node = .prev_sibling }); - if (key.has(.all_siblings)) return resultAction(.{ .node = .all_siblings }); - if (key.has(.all_children)) return resultAction(.{ .node = .all_children }); - if (key.has(.parent_node_end)) return resultAction(.{ .node = .parent_end }); - if (key.has(.parent_node_start)) return resultAction(.{ .node = .parent_start }); - - if (key.has(.delete)) return resultAction(.{ .edit = .{ .kind = .delete, .count = count } }); - if (key.has(.delete_noyank)) return resultAction(.{ .edit = .{ .kind = .delete_noyank, .count = count } }); - if (key.has(.change)) return resultAction(.{ .edit = .{ .kind = .change, .count = count } }); - if (key.has(.yank)) return resultAction(.{ .edit = .{ .kind = .yank, .count = count } }); - if (key.has(.replace_with_yank)) return resultAction(.{ .edit = .{ .kind = .replace_with_yank, .count = count } }); - if (key.has(.paste_after)) return resultAction(.{ .edit = .{ .kind = .paste_after, .count = count } }); - if (key.has(.paste_before)) return resultAction(.{ .edit = .{ .kind = .paste_before, .count = count } }); - if (key.has(.switch_case)) return resultAction(.{ .edit = .{ .kind = .switch_case, .count = count } }); - if (key.has(.to_lowercase)) return resultAction(.{ .edit = .{ .kind = .lowercase, .count = count } }); - if (key.has(.to_uppercase)) return resultAction(.{ .edit = .{ .kind = .uppercase, .count = count } }); - if (key.has(.join_lines)) return resultAction(.{ .edit = .{ .kind = .join_lines, .count = count } }); - if (key.has(.join_select_space)) return resultAction(.{ .edit = .{ .kind = .join_select_space, .count = count } }); - if (key.has(.rotate_contents_fwd)) return resultAction(.{ .edit = .{ .kind = .rotate_contents_forward, .count = count } }); - if (key.has(.rotate_contents_back)) return resultAction(.{ .edit = .{ .kind = .rotate_contents_backward, .count = count } }); - if (key.has(.align_selections)) return resultAction(.{ .edit = .{ .kind = .align_selections, .count = count } }); - if (key.has(.indent)) return resultAction(.{ .edit = .{ .kind = .indent, .count = count } }); - if (key.has(.unindent)) return resultAction(.{ .edit = .{ .kind = .unindent, .count = count } }); - if (key.has(.format)) return resultAction(.{ .lsp = .format }); - if (key.has(.increment)) return resultAction(.{ .adjust_number = @intCast(count) }); - if (key.has(.decrement)) return resultAction(.{ .adjust_number = -@as(i64, @intCast(count)) }); - if (key.has(.comment_toggle)) return resultAction(.{ .edit = .{ .kind = .comment_toggle, .count = count } }); - if (key.has(.undo)) return resultAction(.{ .edit = .{ .kind = .undo, .count = count } }); - if (key.has(.redo)) return resultAction(.{ .edit = .{ .kind = .redo, .count = count } }); - - if (key.has(.leader)) return resultAction(.leader); - if (key.has(.command_line)) return resultAction(.command_line); - if (key.has(.pipe_selection)) return resultAction(.{ .pipe_selection = .replace }); - if (key.has(.pipe_selection_to)) return resultAction(.{ .pipe_selection = .ignore }); - if (key.has(.insert_output)) return resultAction(.{ .pipe_selection = .insert }); - if (key.has(.append_output)) return resultAction(.{ .pipe_selection = .append }); - if (key.has(.keep_pipe)) return resultAction(.{ .pipe_selection = .keep }); - if (key.has(.macro_record)) return resultAction(.macro_record); - if (key.has(.macro_replay)) return resultAction(.{ .macro_replay = count }); - if (key.has(.repeat_insert)) return resultAction(.{ .repeat_insert = count }); - if (key.has(.search)) return resultAction(.search); - if (key.has(.search_selection)) return resultAction(.{ .search_selection = false }); - if (key.has(.search_selection_word)) return resultAction(.{ .search_selection = true }); - if (key.has(.search_next)) return resultAction(.{ .search_step = .{ .direction = .forward, .count = count } }); - if (key.has(.search_prev)) return resultAction(.{ .search_step = .{ .direction = .backward, .count = count } }); - return .unbound; + return first(key, command_roles, count, explicit, true, .unbound); } - fn input(cp: u21, roles: []const Role) Input { - return .{ .cp = cp, .roles = .initMany(roles) }; + fn input(cp: u21, among: []const Role) Input { + return .{ .cp = cp, .roles = .initMany(among) }; } test "counts survive prefixes and identical parser actions can feed both adapters" { @@ -679,11 +449,7 @@ pub const Normal = struct { const ta = parse(&text, sequence[sequence.len - 1]); const pa = parse(&pdf, sequence[sequence.len - 1]); try std.testing.expectEqualDeep(ta, pa); - try std.testing.expectEqualDeep(Result{ .action = .{ .goto = .{ - .target = .line_down, - .count = 12, - .explicit_count = true, - } } }, ta); + try std.testing.expectEqualDeep(Result{ .action = .{ .role = .goto_line_down, .count = 12, .explicit = true } }, ta); try std.testing.expectEqual(State{}, text); try std.testing.expectEqual(State{}, pdf); } @@ -694,18 +460,18 @@ pub const Normal = struct { try std.testing.expectEqual(Result.pending, parse(&state, input('g', &.{.goto_prefix}))); try std.testing.expectEqual(Result.ignored, parse(&state, input('?', &.{}))); try std.testing.expectEqual(State{}, state); - try std.testing.expectEqualDeep(Result{ .action = .{ .move = .{ .motion = .down, .count = 1 } } }, parse(&state, input('j', &.{.move_down}))); + try std.testing.expectEqualDeep(Result{ .action = .{ .role = .move_down } }, parse(&state, input('j', &.{.move_down}))); } test "literal arguments retain conflicting command characters" { var state: State = .{}; try std.testing.expectEqual(Result.pending, parse(&state, input('f', &.{.find_char_fwd}))); - try std.testing.expectEqualDeep(Result{ .action = .{ .find = .{ .kind = .forward, .char = 'p', .count = 1 } } }, parse(&state, input('p', &.{.paste_after}))); + try std.testing.expectEqualDeep(Result{ .action = .{ .role = .find_char_fwd, .char = 'p' } }, parse(&state, input('p', &.{.paste_after}))); try std.testing.expectEqual(Result.pending, parse(&state, input('m', &.{.match_prefix}))); try std.testing.expectEqual(Result.pending, parse(&state, input('r', &.{.surround_replace}))); try std.testing.expectEqual(Result.pending, parse(&state, input('[', &.{.prev_prefix}))); - try std.testing.expectEqualDeep(Result{ .action = .{ .surround_replace = .{ .from = '[', .to = ']' } } }, parse(&state, input(']', &.{.next_prefix}))); + try std.testing.expectEqualDeep(Result{ .action = .{ .role = .surround_replace, .from = '[', .char = ']' } }, parse(&state, input(']', &.{.next_prefix}))); try std.testing.expectEqual(State{}, state); } @@ -722,15 +488,15 @@ pub const Normal = struct { test "replace accepts a Unicode literal" { var state: State = .{}; try std.testing.expectEqual(Result.pending, parse(&state, input('r', &.{.replace_prefix}))); - try std.testing.expectEqualDeep(Result{ .action = .{ .replace_char = '界' } }, parse(&state, input('界', &.{}))); + try std.testing.expectEqualDeep(Result{ .action = .{ .role = .replace_prefix, .char = '界' } }, parse(&state, input('界', &.{}))); try std.testing.expectEqual(State{}, state); } test "once versus per-selection is semantic action metadata" { - try std.testing.expectEqual(Scope.once, (@as(Action, .search)).scope()); - try std.testing.expectEqual(Scope.once, (Action{ .edit = .{ .kind = .undo, .count = 1 } }).scope()); - try std.testing.expectEqual(Scope.per_selection, (Action{ .edit = .{ .kind = .delete, .count = 1 } }).scope()); - try std.testing.expectEqual(Scope.per_selection, (Action{ .move = .{ .motion = .down, .count = 3 } }).scope()); + try std.testing.expect((Action{ .role = .search }).once()); + try std.testing.expect((Action{ .role = .undo }).once()); + try std.testing.expect(!(Action{ .role = .delete }).once()); + try std.testing.expect(!(Action{ .role = .move_down, .count = 3 }).once()); } }; 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); } diff --git a/src/pdf_view.zig b/src/pdf_view.zig index 654284f4..7f9c7b55 100644 --- a/src/pdf_view.zig +++ b/src/pdf_view.zig @@ -11,6 +11,7 @@ const filesystem = @import("fs.zig"); const tracy = @import("tracy.zig"); const image = @import("image.zig"); const look = @import("look.zig"); +const tagline = @import("tagline.zig"); const File = panes.File; const Output = panes.Output; @@ -1223,19 +1224,6 @@ pub const PanAxis = enum { horizontal, vertical }; pub const PanResult = enum { moved, edge, unavailable }; pub const ScrollResult = struct { active_page: usize }; pub const CellPixels = struct { w: u16, h: u16 }; -pub const NormalHost = enum { - none, - last, - leader, - command_line, - search, - search_forward, - search_backward, -}; -pub const NormalResult = struct { - host: NormalHost = .none, - page_changed: bool = false, -}; const PlacedGeometry = struct { geometry: image.NativeGeometry, @@ -2394,20 +2382,17 @@ fn movePage( viewport: ?Viewport, cell_pixels: CellPixels, direction: i8, - kind: modal.Normal.Page, + half: bool, count: u32, ) bool { if (!native_images) { const pages: i64 = @intCast(@max(@as(u32, 1), count)); return stepPage(state, gpa, if (direction > 0) pages else -pages); } - const base: u32 = switch (kind) { - .half_down, .half_up => if (viewport) |view| - @max(@as(u32, 1), view.pixel_h / 2) - else - cell_pixels.h, - .down, .up => if (viewport) |view| view.pixel_h else cell_pixels.h, - }; + const base: u32 = if (viewport) |view| + (if (half) @max(@as(u32, 1), view.pixel_h / 2) else view.pixel_h) + else + cell_pixels.h; return scrollNormal( state, gpa, @@ -2416,128 +2401,52 @@ fn movePage( ); } -pub fn applyNormal( - state: *State, - gpa: std.mem.Allocator, - semantic: modal.Normal.Action, - native_images: bool, - cell_pixels: CellPixels, - viewport: ?Viewport, - geometry: ?image.NativeGeometry, -) NormalResult { - if (comptime !enabled) return .{}; - var result: NormalResult = .{}; - switch (semantic) { +/// A normal-mode command in a PDF pane: the page moves, or the session is +/// asked for what a page cannot do (Last, the leader, the tag, a search). +pub fn applyNormal(core: *pardes.Pardes, pane: *pardes.Pane, a: modal.Normal.Action) void { + if (comptime !enabled) return; + const state = if (pane.pdf) |*pv| pv else return; + const gpa = core.pdf_gpa; + const native = core.native_images; + const cells: CellPixels = .{ .w = core.cell_pixels.w, .h = core.cell_pixels.h }; + const viewport = paneViewport(core, pane); + const placed = if (native) paneGeometry(core, pane) else null; + const page_changed = switch (a.role) { // Esc drops a selection or a search the page shows, else goes back - // a pane (text-only, the page shows neither). - .escape => if (!state.cancelChrome(gpa) or !native_images) { - result.host = .last; - }, - .move => |move| switch (move.motion) { - .down => result.page_changed = moveRows( - state, - gpa, - native_images, - viewport, - cell_pixels, - 1, - move.count, - ), - .up => result.page_changed = moveRows( - state, - gpa, - native_images, - viewport, - cell_pixels, - -1, - move.count, - ), - .left => if (native_images) if (geometry) |placed| { - _ = panPixels(state, placed, .horizontal, -1, scaledStep(cell_pixels.w, move.count)); - }, - .right => if (native_images) if (geometry) |placed| { - _ = panPixels(state, placed, .horizontal, 1, scaledStep(cell_pixels.w, move.count)); - }, - else => {}, - }, - .goto => |go| switch (go.target) { - .file_start => result.page_changed = state.activatePage( - gpa, - if (go.explicit_count) go.count -| 1 else 0, - true, - ), - .last_line => result.page_changed = state.activatePage(gpa, state.page_count -| 1, true), - .line_start, .first_nonws => if (native_images) if (geometry) |placed| - setHorizontalEdge(state, placed, false), - .line_end => if (native_images) if (geometry) |placed| - setHorizontalEdge(state, placed, true), - .line_down => result.page_changed = moveRows( - state, - gpa, - native_images, - viewport, - cell_pixels, - 1, - go.count, - ), - .line_up => result.page_changed = moveRows( - state, - gpa, - native_images, - viewport, - cell_pixels, - -1, - go.count, - ), - else => {}, - }, - .line => |line| switch (line) { - .start, .first_nonws => if (native_images) if (geometry) |placed| - setHorizontalEdge(state, placed, false), - .end => if (native_images) if (geometry) |placed| - setHorizontalEdge(state, placed, true), + // a pane (text-only, the page shows neither); typed again by a + // macro, Esc is only the page's. + .escape => { + if ((!state.cancelChrome(gpa) or !native) and core.macro.replaying == 0) exec.runBuiltin(core, .Last, core.active, "", null); + return; }, - .goto_line => |go| { - if (go.explicit) - result.page_changed = state.activatePage(gpa, go.count -| 1, true); + .move_down, .goto_line_down => moveRows(state, gpa, native, viewport, cells, 1, a.count), + .move_up, .goto_line_up => moveRows(state, gpa, native, viewport, cells, -1, a.count), + .view_scroll_down => moveRows(state, gpa, native, viewport, cells, 1, 1), + .view_scroll_up => moveRows(state, gpa, native, viewport, cells, -1, 1), + .move_left, .move_right => { + if (placed) |g| _ = panPixels(state, g, .horizontal, if (a.role == .move_left) -1 else 1, scaledStep(cells.w, a.count)); + return; }, - .page => |page| result.page_changed = switch (page.kind) { - .half_down, .down => movePage( - state, - gpa, - native_images, - viewport, - cell_pixels, - 1, - page.kind, - page.count, - ), - .half_up, .up => movePage( - state, - gpa, - native_images, - viewport, - cell_pixels, - -1, - page.kind, - page.count, - ), + .goto_line_start, .goto_first_nonws, .line_start, .line_first_nonws, .goto_line_end, .line_end => { + if (placed) |g| setHorizontalEdge(state, g, a.role == .goto_line_end or a.role == .line_end); + return; }, - .view => |view| result.page_changed = switch (view) { - .scroll_down => moveRows(state, gpa, native_images, viewport, cell_pixels, 1, 1), - .scroll_up => moveRows(state, gpa, native_images, viewport, cell_pixels, -1, 1), - else => false, + .goto_file_start => state.activatePage(gpa, if (a.explicit) a.count -| 1 else 0, true), + .goto_line => a.explicit and state.activatePage(gpa, a.count -| 1, true), + .goto_last_line => state.activatePage(gpa, state.page_count -| 1, true), + .half_page_down, .page_down => movePage(state, gpa, native, viewport, cells, 1, a.role == .half_page_down, a.count), + .half_page_up, .page_up => movePage(state, gpa, native, viewport, cells, -1, a.role == .half_page_up, a.count), + .leader => { + core.leader_on = true; + core.leader_n = 0; + return; }, - .leader => result.host = .leader, - .command_line => result.host = .command_line, - .search => result.host = .search, - .search_step => |step| result.host = if (step.direction == .forward) - .search_forward - else - .search_backward, - else => {}, - } - return result; + .command_line => return tagline.enterTag(core, pane), + .search => return core.startPrompt(pane, &pane.body, .{ .search = config.search_marker }), + .search_next, .search_prev => return look.lookWalk(core, if (a.role == .search_next) 1 else -1), + else => false, + }; + if (page_changed) resetPageChrome(pane); } pub fn captureLayoutAnchor(state: *State) void { diff --git a/src/syntax.zig b/src/syntax.zig index 1787335e..a498357c 100644 --- a/src/syntax.zig +++ b/src/syntax.zig @@ -436,9 +436,9 @@ pub fn walkNodes(tree_ptr: *anyopaque, walk: NodeWalk, from: usize, to: usize, a const tree: *const ts.Tree = @ptrCast(@alignCast(tree_ptr)); const lo: u32 = @intCast(@min(from, std.math.maxInt(u32))); const hi: u32 = @intCast(@min(to, std.math.maxInt(u32))); - if (walk == .parent_end or walk == .parent_start) { + if (walk == .parent_node_end or walk == .parent_node_start) { var node = tree.rootNode().namedDescendantForByteRange(lo, hi) orelse return 0; - if (walk == .parent_end) { + if (walk == .parent_node_end) { out[0] = .{ .from = node.endByte(), .to = node.endByte() }; return 1; } @@ -462,10 +462,10 @@ pub fn walkNodes(tree_ptr: *anyopaque, walk: NodeWalk, from: usize, to: usize, a if (cursor.gotoFirstChildForByte(lo) == null) break; } switch (walk) { - .expand => while (cursor.node().startByte() == lo and cursor.node().endByte() == hi) { + .expand_node => while (cursor.node().startByte() == lo and cursor.node().endByte() == hi) { if (!cursor.gotoParent()) break; }, - .shrink => _ = cursor.gotoFirstChild(), + .shrink_node => _ = cursor.gotoFirstChild(), .next_sibling => while (!cursor.gotoNextSibling()) { if (!cursor.gotoParent()) break; }, @@ -492,7 +492,7 @@ pub fn walkNodes(tree_ptr: *anyopaque, walk: NodeWalk, from: usize, to: usize, a } return n; }, - .parent_end, .parent_start => unreachable, + .parent_node_end, .parent_node_start => unreachable, } const node = cursor.node(); out[0] = .{ .from = node.startByte(), .to = node.endByte() }; -- cgit v1.3