diff options
Diffstat (limited to 'src/exec.zig')
| -rw-r--r-- | src/exec.zig | 51 |
1 files changed, 50 insertions, 1 deletions
diff --git a/src/exec.zig b/src/exec.zig index b3cbbdd9..327d4683 100644 --- a/src/exec.zig +++ b/src/exec.zig @@ -918,7 +918,7 @@ pub fn executeBuiltinLine(p: *Pardes, id: usize, txt: []const u8) bool { const head = std.meta.stringToEnum(Builtin, cmd[0 .. sp orelse break :blk null]) orelse break :blk null; break :blk if (builtins.registry.takesArg(head)) head else null; }; - const b = bi orelse return pardes.Kind.runLine(p, id, cmd); + const b = bi orelse return runLine(p, id, cmd); p.exec_depth += 1; defer p.exec_depth -= 1; const typed = if (sp) |s| std.mem.trim(u8, cmd[s + 1 ..], " \t") else ""; @@ -1235,3 +1235,52 @@ test "a switch clicked with a selection held flips unless the selection says on // A word that takes any argument still gets the selection. try std.testing.expectEqualStrings("Msg some text", withArg(p, "Msg", "some text")); } + +// ---- the words addons bring (Kind.Registry's) ---- + +/// A line whose first word is an addon's: run, and true; false for one +/// that is no addon's word. +fn runLine(p: *Pardes, id: usize, line: []const u8) bool { + const end = std.mem.indexOfAny(u8, line, " \t") orelse line.len; + const w = p.kinds.word(line[0..end]) orelse return false; + runWord(p, id, w, line, std.mem.trim(u8, line[end..], " \t")); + return true; +} + +/// Word `w` from pane `id`: one that opens its kind opens a pane of it +/// (`arg` its path), any other goes to the pane's kind, which must be +/// the word's. Its argument is checked first. +pub fn runWord(p: *Pardes, id: usize, w: *const pardes.Kind.Word, line: []const u8, arg: []const u8) void { + const name = w.name.slice(); + var said: [256]u8 = undefined; + if (w.arg == pardes.Kind.c.PA_ARG_NONE and arg.len > 0) return p.reportFailure(id, std.fmt.bufPrint(&said, "{s} takes no argument", .{name}) catch name); + if (w.arg == pardes.Kind.c.PA_ARG_REQUIRED and arg.len == 0) return p.reportFailure(id, std.fmt.bufPrint(&said, "{s} needs an argument", .{name}) catch name); + if (w.opens != 0) { + const free = p.freeSlot() orelse return p.reportError(id, name, error.NoPaneSlots); + var path_buf: [4096]u8 = undefined; + const path = if (arg.len == 0 or std.fs.path.isAbsolute(arg)) arg else std.fmt.bufPrint(&path_buf, "{s}/{s}", .{ std.mem.trimEnd(u8, if (p.panes[id]) |pane| Pardes.paneDir(pane) else p.sessionDir(), "/"), arg }) catch arg; + const made = (if (path.len > 0) pardes.Kind.openFile(p, free, w.kind, path) else pardes.Kind.open(p, free, w.kind, null, null)) catch |err| return p.reportError(id, name, err); + if (made.addon.?.refused.len > 0) { + made.vweight = 0; + p.unplaced.set(free); + return p.reportFailure(id, std.fmt.bufPrint(&said, "{s}: {s}", .{ name, made.addon.?.refused.slice() }) catch name); + } + return placeNew(p, id, id, free, .doc); + } + const kind = p.kinds.kinds[w.kind].nameOf(); + const here = if (p.panes[id]) |pane| if (pane.addon) |s| s.live and s.is(kind) else false else false; + if (!here) return p.reportFailure(id, std.fmt.bufPrint(&said, "{s} is a {s} pane's word: run it in one", .{ name, kind }) catch name); + var reply: [160]u8 = undefined; + if (pardes.Kind.ctl(p, id, line, &reply)) |why| return p.reportFailure(id, std.fmt.bufPrint(&said, "{s}: {s}", .{ name, why }) catch name); + if (arg.len == 0) return; + // Its last value, one line a word, which a kind with no record is + // given again when its pane comes back. + const s = p.panes[id].?.addon.?; + var next: pardes.Kind.Buf(256) = .{}; + var out: std.Io.Writer = .fixed(&next.bytes); + var lines = std.mem.tokenizeScalar(u8, s.values.slice(), '\n'); + while (lines.next()) |l| if (!std.mem.eql(u8, l[0 .. std.mem.indexOfScalar(u8, l, ' ') orelse l.len], name)) out.print("{s}\n", .{l}) catch return; + out.print("{s} {s}\n", .{ name, arg }) catch return; + next.len = @intCast(out.buffered().len); + s.values = next; +} |
