summaryrefslogtreecommitdiff
path: root/src/builtins.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/builtins.zig')
-rw-r--r--src/builtins.zig151
1 files changed, 114 insertions, 37 deletions
diff --git a/src/builtins.zig b/src/builtins.zig
index 84904959..cc7d1d4a 100644
--- a/src/builtins.zig
+++ b/src/builtins.zig
@@ -46,6 +46,10 @@ pub const PaneKind = enum {
/// An image pane, or a terminal, whose program's kitty graphics are
/// pictures too.
picture,
+ /// A terminal, or a +Pager: a program's colours.
+ program_colors,
+ /// A text pane showing a diff: a .diff, a .patch, a +Pager holding one.
+ diff,
pub fn has(kind: PaneKind, pane: *const panes.Pane) bool {
return switch (kind) {
@@ -53,6 +57,9 @@ pub const PaneKind = enum {
.pdf => pane.hasPdf(),
.image => pane.image != null,
.picture => pane.image != null or pane.isTerminal(),
+ .program_colors => pane.isTerminal() or (pane.file != null and pane.file.?.pager),
+ .diff => if (pane.file) |f| pane.colorAlgo() == .diff or
+ (f.output != null and std.meta.eql(f.output.?.from, panes.Output.Origin{ .cmd = .Diff })) else false,
};
}
@@ -62,6 +69,8 @@ pub const PaneKind = enum {
.pdf => "a PDF",
.image => "an image",
.picture => "an image or a terminal",
+ .program_colors => "a terminal or a +Pager",
+ .diff => "a diff or a +Diff",
};
}
};
@@ -89,11 +98,11 @@ fn isEnabled(comptime T: type) bool {
/// word that is one here, or in no build.
pub fn absentWhy(name: []const u8) ?[]const u8 {
@setEvalBranchQuota(20000);
- inline for (@typeInfo(@This()).@"struct".decls) |d| {
- if (comptime @TypeOf(@field(@This(), d.name)) == type) {
- const T = @field(@This(), d.name);
+ inline for (@typeInfo(@This()).@"struct".decl_names) |d| {
+ if (comptime @TypeOf(@field(@This(), d)) == type) {
+ const T = @field(@This(), d);
if (comptime @typeInfo(T) == .@"struct" and @hasDecl(T, "run") and !isEnabled(T)) {
- if (std.mem.eql(u8, d.name, name)) return if (@hasDecl(T, "gui_only")) "GUI-only" else "not in this build";
+ if (std.mem.eql(u8, d, name)) return if (@hasDecl(T, "gui_only")) "GUI-only" else "not in this build";
}
}
}
@@ -103,12 +112,12 @@ pub fn absentWhy(name: []const u8) ?[]const u8 {
fn manualBuiltinCount() comptime_int {
comptime {
var count = 0;
- for (@typeInfo(@This()).@"struct".decls) |d| {
- if (@TypeOf(@field(@This(), d.name)) != type) continue;
- const T = @field(@This(), d.name);
+ for (@typeInfo(@This()).@"struct".decl_names) |d| {
+ if (@TypeOf(@field(@This(), d)) != type) continue;
+ const T = @field(@This(), d);
if (@typeInfo(T) != .@"struct" or !@hasDecl(T, "run") or !isEnabled(T)) continue;
if (@TypeOf(T.run) != fn (Ctx) void)
- @compileError(d.name ++ ".run must have signature fn (Ctx) void");
+ @compileError(d ++ ".run must have signature fn (Ctx) void");
count += 1;
}
return count;
@@ -120,9 +129,9 @@ fn manualBuiltinList() [manualBuiltinCount()]type {
@setEvalBranchQuota(4000);
var list: [manualBuiltinCount()]type = undefined;
var count = 0;
- for (@typeInfo(@This()).@"struct".decls) |d| {
- if (@TypeOf(@field(@This(), d.name)) != type) continue;
- const T = @field(@This(), d.name);
+ for (@typeInfo(@This()).@"struct".decl_names) |d| {
+ if (@TypeOf(@field(@This(), d)) != type) continue;
+ const T = @field(@This(), d);
if (@typeInfo(T) != .@"struct" or !@hasDecl(T, "run") or !isEnabled(T)) continue;
list[count] = T;
count += 1;
@@ -190,7 +199,7 @@ pub const registry = struct {
pub fn takesArg(b: Builtin()) bool {
inline for (manualBuiltinList(), 0..) |T, i|
- if (@intFromEnum(b) == i) return @hasDecl(T, "takes_arg") and T.takes_arg;
+ if (@backingInt(b) == i) return @hasDecl(T, "takes_arg") and T.takes_arg;
// Every setting takes its value, and a switch flips when bare.
return true;
}
@@ -201,7 +210,7 @@ pub const registry = struct {
/// may be in (`Dump+2024.zon` is a name, not Dump's argument).
pub fn plusArg(b: Builtin()) bool {
inline for (manualBuiltinList(), 0..) |T, i|
- if (@intFromEnum(b) == i) return @hasDecl(T, "plus_arg") and T.plus_arg;
+ if (@backingInt(b) == i) return @hasDecl(T, "plus_arg") and T.plus_arg;
return false;
}
@@ -214,36 +223,36 @@ pub const registry = struct {
/// pane it is refused (exec.runBuiltinFrom), as Repl and Edit are.
pub fn needs(b: Builtin()) ?PaneKind {
inline for (manualBuiltinList(), 0..) |T, i|
- if (@intFromEnum(b) == i) return if (@hasDecl(T, "needs")) T.needs else null;
+ if (@backingInt(b) == i) return if (@hasDecl(T, "needs")) T.needs else null;
return null;
}
pub fn requiresArg(b: Builtin()) bool {
inline for (manualBuiltinList(), 0..) |T, i|
- if (@intFromEnum(b) == i) return @hasDecl(T, "requires_arg") and T.requires_arg;
+ if (@backingInt(b) == i) return @hasDecl(T, "requires_arg") and T.requires_arg;
inline for (comptime settingList(), manualBuiltinCount()..) |setting, i|
- if (@intFromEnum(b) == i) return switch (setting.action) {
+ if (@backingInt(b) == i) return switch (setting.action) {
// a switch flips bare, a choice steps, and DumpDir and Shell bare
// are the default
.toggle, .transition, .scene, .dump_dir, .choice, .lift, .shader_animation, .motion, .shell => false,
.theme, .font, .tagline_size, .window_opacity, .window_blur, .message_ms, .shader, .inactive_dim, .grip_width => true,
- // bare, it says the width (exec.applySettingBuiltin)
- .tab => false,
+ // bare, it says the current value (exec.applySettingBuiltin)
+ .tab, .callstack_depth => false,
};
unreachable;
}
pub fn scope(b: Builtin()) Scope {
inline for (manualBuiltinList(), 0..) |T, i|
- if (@intFromEnum(b) == i) return if (@hasDecl(T, "scope")) T.scope else .pane;
+ if (@backingInt(b) == i) return if (@hasDecl(T, "scope")) T.scope else .pane;
return .session; // a setting
}
pub fn outputTraits(b: Builtin()) ?OutputTraits {
inline for (manualBuiltinList(), 0..) |T, i|
- if (@intFromEnum(b) == i) return if (@hasDecl(T, "output")) T.output else null;
+ if (@backingInt(b) == i) return if (@hasDecl(T, "output")) T.output else null;
inline for (comptime settingList(), manualBuiltinCount()..) |_, i|
- if (@intFromEnum(b) == i) return null;
+ if (@backingInt(b) == i) return null;
unreachable;
}
@@ -251,15 +260,15 @@ pub const registry = struct {
/// The ones that own that row themselves declare `quiet` and do not.
pub fn announces(b: Builtin()) bool {
inline for (manualBuiltinList(), 0..) |T, i|
- if (@intFromEnum(b) == i) return !(@hasDecl(T, "quiet") and T.quiet);
+ if (@backingInt(b) == i) return !(@hasDecl(T, "quiet") and T.quiet);
return true; // a setting builtin changes something invisible; say so
}
pub fn dispatch(b: Builtin(), c: Ctx) void {
inline for (manualBuiltinList(), 0..) |T, i|
- if (@intFromEnum(b) == i) return T.run(c);
+ if (@backingInt(b) == i) return T.run(c);
inline for (comptime settingList(), manualBuiltinCount()..) |setting, i|
- if (@intFromEnum(b) == i) return exec.applySettingBuiltin(c.p, setting, c.arg);
+ if (@backingInt(b) == i) return exec.applySettingBuiltin(c.p, setting, c.arg);
unreachable;
}
};
@@ -308,7 +317,7 @@ test "Exit over a ctl write or a click names every pane it asks about, a second
first.file.?.saved_hash = 0; // edited: what it had is not what it has
p.newScratchBelow(p.active);
const second = p.panes[p.active].?;
- panes.File.setContent(p, &second.file.?, try std.testing.allocator.dupe(u8, "x" ** 120));
+ panes.File.setContent(p, &second.file.?, try std.testing.allocator.dupe(u8, &@as([120]u8, @splat('x'))));
try std.testing.expect(second.file.?.revision != second.file.?.saved_revision);
// A ctl write, or a click through exec (ctl.captured).
if (clicked) p.fs.capturing = true else p.fs.no_prompt = true;
@@ -332,7 +341,7 @@ test "Exit over a ctl write or a click names every pane it asks about, a second
try std.testing.expect(noticed);
// Edited since: only that one is named again (acme's rule).
p.fs.failure_len = 0;
- panes.File.setContent(p, &second.file.?, try std.testing.allocator.dupe(u8, "y" ** 120));
+ panes.File.setContent(p, &second.file.?, try std.testing.allocator.dupe(u8, &@as([120]u8, @splat('y'))));
try std.testing.expect(p.executeBuiltinLine(p.active, "Exit"));
try std.testing.expect(!p.quit);
const again = p.fs.failure[0..p.fs.failure_len];
@@ -735,15 +744,15 @@ pub const Restore = struct {
// so a Restore that could never happen spends no warning.
if (comptime pardes.hosted) {
const bytes = @import("fs.zig").readRestore(c.p.gpa, path, c.p.settings.dump_dir.get()) catch |err| {
- var buf: [limits.host_path_cap + 64]u8 = undefined;
- var words: [128]u8 = undefined;
- const why = switch (err) {
- error.FileNotFound => "no such file",
- error.PermissionDenied => "permission denied",
- error.IsDirectory => "a directory, not a dump",
- else => pardes.Messages.errorWords(err, &words),
- };
- return c.p.reportFailure(c.id, std.fmt.bufPrint(&buf, "Restore: {s}: {s}", .{ path, why }) catch "Restore: cannot read the dump");
+ var buf: [limits.host_path_cap + 64]u8 = undefined;
+ var words: [128]u8 = undefined;
+ const why = switch (err) {
+ error.FileNotFound => "no such file",
+ error.PermissionDenied => "permission denied",
+ error.IsDirectory => "a directory, not a dump",
+ else => pardes.Messages.errorWords(err, &words),
+ };
+ return c.p.reportFailure(c.id, std.fmt.bufPrint(&buf, "Restore: {s}: {s}", .{ path, why }) catch "Restore: cannot read the dump");
};
defer c.p.gpa.free(bytes);
// Nor does a file that is not a dump, before any warning.
@@ -1339,14 +1348,28 @@ pub const Del = struct {
}
};
-/// Recolour a terminal's program colours to the theme, or show them as sent, flipping between them.
+/// Recolour a terminal's or a +Pager's program colours to the theme, or show them as sent, flipping between them.
pub const Filter = struct {
- pub const needs: PaneKind = .terminal;
+ pub const needs: PaneKind = .program_colors;
pub fn run(c: Ctx) void {
c.pane.tty_filter = !c.pane.tty_filter;
}
};
+/// Stack a diff's hunks as a diff viewer shows them, each under its place and the declarations around it, in a +Diff; on a +Diff, show the diff itself as text.
+pub const Diff = struct {
+ pub const needs: PaneKind = .diff;
+ pub const output: OutputTraits = .{ .name = config.diff_buffer, .steps = true, .locations = true };
+ pub fn run(c: Ctx) void {
+ const f = &c.pane.file.?;
+ if (panes.Output.diffSource(c.p, f)) |source| return panes.Output.showDiffText(c.p, c.id, source);
+ _ = panes.Output.openDiff(c.p, c.id, f.path, f.content) catch |err| switch (err) {
+ error.NoHunks => c.p.reportFailure(c.id, "Diff: no hunk in it"),
+ else => c.p.reportError(c.id, "hunks", err),
+ };
+ }
+};
+
/// Cycle the pane's input modes: terminals add raw tty mode to normal/insert.
pub const Mode = struct {
pub fn run(c: Ctx) void {
@@ -1801,6 +1824,15 @@ pub const Right = struct {
}
};
+/// Window mode: h/j/k/l move an outline between the panes; Enter or Esc
+/// focuses the outlined pane, `:` its tag, the letter shown on a pane that one.
+pub const Window = struct {
+ pub fn run(c: Ctx) void {
+ tagline.exitHeader(c.p);
+ c.p.window_pick = if (c.p.panes[c.id] != null) c.id else c.p.active;
+ }
+};
+
// ---- the jump group ----
/// Ctrl-o: one step back into the history.
@@ -1819,6 +1851,22 @@ pub const Forward = struct {
}
};
+/// Select the next Look location across panes; Enter opens it.
+pub const LookNext = struct {
+ pub const quiet = true;
+ pub fn run(c: Ctx) void {
+ look.lookWalk(c.p, 1);
+ }
+};
+
+/// Select the previous Look location across panes; Enter opens it.
+pub const LookPrev = struct {
+ pub const quiet = true;
+ pub fn run(c: Ctx) void {
+ look.lookWalk(c.p, -1);
+ }
+};
+
/// Go back to the last place jumped from in another pane.
pub const Last = struct {
pub fn run(c: Ctx) void {
@@ -1856,6 +1904,28 @@ pub const Jumplist = struct {
// ---- the language group ----
+/// Go to the definition of the symbol under the cursor; with an argument
+/// (a 2-1 chord's selection), of that symbol, where it is nearest.
+pub const Def = struct {
+ pub const takes_arg = true;
+ /// Its answer says what it found, a jump or a list or why not; its
+ /// name would only stack above it, as gd never did.
+ pub const quiet = true;
+ pub fn run(c: Ctx) void {
+ c.p.lspRequest(c.id, .definition, std.mem.trim(u8, c.arg orelse "", " \t\r\n"));
+ }
+};
+
+/// List the references to the symbol under the cursor; with an argument
+/// (a 2-1 chord's selection), to that symbol, where it is nearest.
+pub const Refs = struct {
+ pub const takes_arg = true;
+ pub const quiet = true; // as Def
+ pub fn run(c: Ctx) void {
+ c.p.lspRequest(c.id, .references, std.mem.trim(u8, c.arg orelse "", " \t\r\n"));
+ }
+};
+
/// Ask the language server about the symbol under the cursor.
pub const Hover = struct {
pub fn run(c: Ctx) void {
@@ -1898,6 +1968,13 @@ pub const WsDiagnostics = struct {
}
};
+/// Show outermost callers first, with signatures and call sites (gc).
+pub const Callstack = struct {
+ pub fn run(c: Ctx) void {
+ c.p.lspRequest(c.id, .callstack, "");
+ }
+};
+
/// List the callers of the function under the cursor.
pub const Callers = struct {
pub fn run(c: Ctx) void {