summaryrefslogtreecommitdiff
path: root/core/root.zig
diff options
context:
space:
mode:
Diffstat (limited to 'core/root.zig')
-rw-r--r--core/root.zig445
1 files changed, 445 insertions, 0 deletions
diff --git a/core/root.zig b/core/root.zig
new file mode 100644
index 00000000..a0cba5ec
--- /dev/null
+++ b/core/root.zig
@@ -0,0 +1,445 @@
+const std = @import("std");
+pub const layout = @import("layout.zig");
+/// The core's step: `advance` runs a `.tick` a frame of this, and
+/// `nextWake` answers in whole frames of it.
+pub const frame_ms: u32 = 16;
+pub const frame_ns: u64 = frame_ms * std.time.ns_per_ms;
+pub const glide = @import("dev").glide;
+pub const FrameStats = @import("FrameStats.zig");
+pub const modal = @import("modal.zig");
+pub const look = @import("look.zig");
+pub const Plumb = @import("Plumb.zig");
+pub const filesystem = @import("fs.zig");
+pub const ctlfs = @import("ninep/tree.zig");
+pub const syntax = @import("syntax.zig");
+pub const tracy = @import("tracy.zig");
+pub const panes = @import("panes.zig");
+const locations = @import("locations.zig");
+pub const tagline = @import("tagline.zig");
+/// A program's colours on a pane's body (a terminal's cells, its images).
+pub const ansi = @import("ansi.zig");
+/// Predicted echo for a replica's terminal panes (`Pardes.echo`).
+pub const Echo = @import("echo.zig").Echo;
+pub const exec = @import("exec.zig");
+const recent_files = @import("recent.zig");
+const Store = @import("Store.zig");
+pub const Db = @import("Db.zig");
+pub const mouse = @import("mouse.zig");
+pub const normal = @import("normal.zig");
+pub const edit = @import("edit.zig");
+pub const draw = @import("draw.zig");
+pub const Messages = @import("Messages.zig");
+pub const Registers = @import("Registers.zig");
+pub const Macro = @import("Macro.zig");
+pub const Editor = @import("Editor.zig");
+pub const Pipe = @import("Pipe.zig");
+pub const Stream = @import("Stream.zig");
+pub const Handover = @import("Handover.zig");
+pub const colors = @import("colors.zig");
+pub const Theme = colors.Theme;
+pub const themes = colors.themes;
+pub const ChromeTheme = colors.ChromeTheme;
+/// The screen types live in surface.zig; every shell names them through here.
+pub const Color = @import("surface.zig").Color;
+pub const FontRole = @import("surface.zig").FontRole;
+pub const CellStyle = @import("surface.zig").CellStyle;
+pub const Cell = @import("surface.zig").Cell;
+pub const Placement = @import("surface.zig").Placement;
+pub const Raster = @import("surface.zig").Raster;
+pub const RectPx = @import("surface.zig").RectPx;
+pub const NativeGeometry = @import("surface.zig").NativeGeometry;
+pub const containGeometry = @import("surface.zig").containGeometry;
+pub const RectCell = @import("surface.zig").RectCell;
+pub const Surface = @import("surface.zig").Surface;
+pub const Region = @import("surface.zig").Region;
+pub const Chrome = @import("surface.zig").Chrome;
+pub const RailTick = @import("surface.zig").RailTick;
+pub const MAX_REGIONS = @import("surface.zig").MAX_REGIONS;
+/// What comes in (Event.zig).
+pub const Mouse = @import("Event.zig").Mouse;
+pub const Key = @import("Event.zig").Key;
+pub const hit = @import("Event.zig").hit;
+pub const sel_slot = @import("Event.zig").sel_slot;
+pub const CellPixels = @import("Event.zig").CellPixels;
+pub const Event = @import("Event.zig").Event;
+/// What goes out (Effect.zig).
+pub const PtySignal = @import("Effect.zig").PtySignal;
+pub const WatchMode = @import("Effect.zig").WatchMode;
+pub const effect_path_cap = @import("Effect.zig").effect_path_cap;
+pub const effect_write_cap = @import("Effect.zig").effect_write_cap;
+pub const Effect = @import("Effect.zig").Effect;
+pub const builtins = @import("builtins.zig");
+/// Every builtin as JSON, for the docs (tools/gen_builtins_json.zig).
+pub const builtins_json = @import("builtins_json.zig");
+const limits = memory.limits;
+pub const selection_pipe = @import("selection_pipe.zig");
+pub const config = @import("config.zig");
+pub const memory = @import("memory.zig");
+pub const tape = @import("tape.zig");
+pub const walk = @import("walk.zig");
+pub const wire = @import("wire");
+pub const views = @import("views.zig");
+pub const rollback = @import("rollback.zig");
+pub const replica = @import("replica.zig");
+pub const Kind = @import("Kind.zig");
+/// The terminal, as the core calls it (core/term/ behind it).
+pub const Term = @import("Term.zig");
+pub const keys = @import("keys.zig");
+pub const Addon = @import("Addon.zig");
+pub const dump = @import("dump.zig");
+pub const lsp = @import("lsp/lsp.zig");
+/// What the runtime's files name of the core beside the above (9p_io).
+pub const ctl = @import("ninep/ctl.zig");
+pub const edit_cmd = @import("edit_cmd.zig");
+pub const host_io = @import("dev").host_io;
+pub const file_watch = @import("dev").file_watch;
+pub const Host = host_io.Host;
+pub const frameMark = tracy.frameMark;
+
+/// The shell this build is for (build.zig's `-Dplatform`).
+pub const platform = @import("pardes_config").platform;
+
+pub const version = @import("pardes_config").version;
+pub const commit: ?[]const u8 = @import("pardes_config").commit;
+
+pub const isolated = @import("pardes_isolation").isolated;
+
+pub const font_picker = platform == .gui or platform == .macos;
+
+/// Whose turn it is with the core (Turn.zig): one per process, like the
+/// editor it belongs to. A process that never starts it -- a test -- has no
+/// second thread, and every yield is a no-op.
+pub var turn: Turn = .{};
+pub const Turn = @import("Turn.zig");
+pub const Jump = @import("Jump.zig");
+const Loc = Jump.Loc;
+
+pub const can_attach = platform == .tui or platform == .gui;
+
+pub const fonts = if (font_picker) @import("dev").fonts else struct {};
+
+pub const Pane = panes.Pane;
+const Text = panes.Text;
+
+/// Pane slots, where a session keeps many files and terminals open. A slot
+/// costs its share of the fixed tables (MAX_PANES-sized arrays in the core
+/// and its surface) whether or not a pane is in it; a pane itself is
+/// allocated when it opens.
+pub const MAX_PANES = 64;
+pub const Layer = @import("Layer.zig");
+pub const RowMetrics = Layer.Metrics;
+pub const TagHit = Layer.TagHit;
+pub const TagKind = Layer.Kind;
+/// A tag shows at most this many lines; past them it is cut, like a line
+/// too long for its width.
+pub const MAX_TAG_ROWS = 8;
+/// Panes, then columns, then the workspace bar, then each pane's notice
+/// bands: a band to a tag, however many lines it has.
+pub const MAX_TAG_LAYERS = MAX_PANES + MAX_COLS + 1 + MAX_PANES * Pane.Notices.max;
+pub const NOTICE_LAYER_BASE = MAX_PANES + MAX_COLS + 1;
+/// Columns a session holds (each costs about 8 KB of state, a column tag
+/// being most of it).
+pub const MAX_COLS = 16;
+const column_weight_unit = layout.column_weight_unit;
+pub const TOPBAR_H: u16 = 1;
+pub const COLBAR_H: u16 = 1;
+pub const BOX_H: u16 = 1;
+pub const TAG_TEXT_INSET: u16 = config.GUTTER;
+
+pub fn taglineOriginCol(p: *const Pardes, col: u16, row: u16) f32 {
+ const v = p.view();
+ if (row < p.topBarHeight()) return 0;
+ if (row < p.topBarHeight() + layout.columnBarHeight(p)) {
+ for (v.col_x[0..p.view().ncol], v.col_w[0..p.view().ncol]) |x, w|
+ if (col >= x and col - x < w) return @floatFromInt(x);
+ }
+ for (p.panes, 0..) |slot, id| {
+ const pane = slot orelse continue;
+ const r = v.rects[id];
+ const tag_y = p.tagTop(pane, r);
+ if (row >= tag_y and row < tag_y + pane.tag_rows and col >= r.x and col < r.x + r.w) return @floatFromInt(r.x);
+ }
+ return @floatFromInt(col);
+}
+
+pub fn gridColAt(p: ?*const Pardes, x: f32, row: u16, body_w: f32, tagline_w: f32) u16 {
+ const body = @max(body_w, 1);
+ const tag = @max(tagline_w, 1);
+ if (row < (if (p) |core| core.topBarHeight() else TOPBAR_H)) return colFromSpan(x, tag);
+ if (p) |core| if (row < core.topBarHeight() + layout.columnBarHeight(core)) {
+ for (core.view().col_x[0..core.view().ncol], core.view().col_w[0..core.view().ncol]) |origin, width| {
+ if (width == 0) continue;
+ const left = @as(f32, @floatFromInt(origin)) * body;
+ const right = @as(f32, @floatFromInt(origin + width)) * body;
+ if (x < left or x >= right) continue;
+ return origin + @as(u16, @intFromFloat(@min(
+ @floor(@max(0, x - left) / tag),
+ @as(f32, @floatFromInt(width - 1)),
+ )));
+ }
+ };
+ if (p) |core| for (core.panes, 0..) |slot, id| {
+ const pane = slot orelse continue;
+ const r = core.view().rects[id];
+ const tag_y = core.tagTop(pane, r);
+ if (row < tag_y or row >= tag_y + pane.tag_rows or r.w == 0) continue;
+ const left = @as(f32, @floatFromInt(r.x)) * body;
+ const right = @as(f32, @floatFromInt(r.x + r.w)) * body;
+ if (x < left or x >= right) continue;
+ const within: u16 = @intFromFloat(@min(
+ @floor(@max(0, x - left) / tag),
+ @as(f32, @floatFromInt(r.w - 1)),
+ ));
+ return r.x + within;
+ };
+ return colFromSpan(x, body);
+}
+
+fn colFromSpan(x: f32, span: f32) u16 {
+ return @intFromFloat(@min(@floor(@max(x, 0) / span), 10_000));
+}
+
+pub const Builtin = builtins.registry.Builtin();
+
+pub const test_api = if (@import("builtin").is_test) struct {
+ pub fn runBuiltin(
+ p: *Pardes,
+ name: []const u8,
+ id: usize,
+ txt: []const u8,
+ arg: ?[]const u8,
+ ) bool {
+ const command = std.meta.stringToEnum(Builtin, name) orelse return false;
+ exec.runBuiltin(p, command, id, txt, arg);
+ return true;
+ }
+
+ /// What the core wrote to pane `pane`'s pty (every pane's, for null)
+ /// since the last drain, every effect taken off: as much as `buf` holds.
+ pub fn ptyBytes(p: *Pardes, pane: ?u8, buf: []u8) []const u8 {
+ var n: usize = 0;
+ while (p.nextEffect()) |effect| switch (effect) {
+ .write => |w| {
+ if (pane != null and w.pane != pane.?) continue;
+ const b = w.bytes.slice();
+ if (n + b.len > buf.len) break;
+ @memcpy(buf[n..][0..b.len], b);
+ n += b.len;
+ },
+ else => {},
+ };
+ return buf[0..n];
+ }
+} else struct {};
+
+fn chordName(comptime c: config.Chord) []const u8 {
+ comptime {
+ const mods = (if (c.ctrl) "C-" else "") ++ (if (c.alt) "A-" else "") ++ (if (c.shift) "S-" else "");
+ for (@typeInfo(Key).@"struct".decl_names) |d| {
+ if (@TypeOf(@field(Key, d)) != u21) continue;
+ if (@field(Key, d) == c.cp) return mods ++ d;
+ }
+ if (c.cp < 0x20 or c.cp > 0x7e) @compileError("chord has no name for the index");
+ return mods ++ &[_]u8{@as(u8, @intCast(c.cp))};
+ }
+}
+
+fn shortcuts(comptime b: Builtin) []const u8 {
+ comptime {
+ @setEvalBranchQuota(20000);
+ // each part after ", "; the first one's is cut off at the end
+ var s: []const u8 = "";
+ if (b == .Window) s = s ++ ", " ++ chordName(config.window_prefix[0]);
+ for (config.jump_keys) |jk| if (jk.cmd == b) {
+ s = s ++ ", " ++ chordName(jk.chord);
+ };
+ if (config.look_cmd == b) {
+ for (config.look_key) |k| s = s ++ ", " ++ chordName(k);
+ s = s ++ ", " ++ @tagName(config.look_button) ++ "-click";
+ }
+ if (config.exec_cmd == b) {
+ for (config.exec_key) |k| s = s ++ ", " ++ chordName(k);
+ s = s ++ ", " ++ @tagName(config.exec_button) ++ "-click";
+ }
+ var it = std.mem.tokenizeScalar(u8, config.topbar_str, ' ');
+ while (it.next()) |w| if (std.mem.eql(u8, w, @tagName(b))) {
+ s = s ++ ", topbar";
+ };
+ return if (s.len == 0) s else s[2..];
+ }
+}
+
+fn leaderKeys(comptime path: []const u8) []const u8 {
+ comptime {
+ var s: []const u8 = "SPC";
+ for (path) |c| s = s ++ " " ++ [_]u8{c};
+ return s;
+ }
+}
+
+const Row = struct { path: ?[]const u8, cmd: Builtin, line: []const u8 };
+pub const builtin_rows: [std.enums.values(Builtin).len]Row = blk: {
+ // one pass per builtin per config table, and the insertion sort below is
+ // O(n²) comptime branches on top of it
+ @setEvalBranchQuota(200000);
+ var rows: [std.enums.values(Builtin).len]Row = undefined;
+ // the widest spaced-out path ("l D" = 3) and the widest name, so the three
+ // columns line up
+ var keyw: usize = 0;
+ var namew: usize = 0;
+ for (std.enums.values(Builtin)) |b| {
+ if (config.leader_path.get(b)) |p| keyw = @max(keyw, 2 * p.len - 1);
+ namew = @max(namew, @tagName(b).len);
+ }
+ for (std.enums.values(Builtin), 0..) |b, i| {
+ const leader = if (config.leader_path.get(b)) |path| leaderKeys(path) else "";
+ const rest = shortcuts(b);
+ const named = @tagName(b) ++ (if (rest.len == 0) "" else (&@as([namew - @tagName(b).len]u8, @splat(' '))) ++ " " ++ rest);
+ rows[i] = .{
+ .path = config.leader_path.get(b),
+ .cmd = b,
+ .line = leader ++ (&@as([4 + keyw - leader.len]u8, @splat(' '))) ++ " " ++ named,
+ };
+ }
+ const last = "\x7f";
+ for (1..rows.len) |i| {
+ var j = i;
+ while (j > 0 and std.mem.lessThan(u8, rows[j].path orelse last, rows[j - 1].path orelse last)) : (j -= 1) {
+ const t = rows[j];
+ rows[j] = rows[j - 1];
+ rows[j - 1] = t;
+ }
+ }
+ break :blk rows;
+};
+
+comptime {
+ @setEvalBranchQuota(20000); // one branch per string byte, behind the decl walk that folds Builtin
+ var it = std.mem.tokenizeScalar(u8, config.topbar_str, ' ');
+ words: while (it.next()) |w| {
+ for (@typeInfo(Builtin).@"enum".field_names) |name|
+ if (std.mem.eql(u8, name, w)) continue :words;
+ @compileError("topbar word is not a builtin: " ++ w);
+ }
+}
+
+/// Said at a focus-in when the Host's `newer_build` answers yes (upgrade.zig).
+pub const newer_build_words = "a newer pardes is installed: Upgrade (SPC s U) moves this session onto it";
+
+pub const Rect = layout.Rect;
+
+pub const Pardes = @import("Core.zig");
+pub const Options = Pardes.Options;
+pub const Boot = Pardes.Boot;
+pub const Haptic = Pardes.Haptic;
+
+test "a compact tagline anchors at its own pane, and both shells step from the same origin" {
+ if (Term.ops == null) return error.SkipZigTest; // a shell
+ const p = try Pardes.init(std.testing.allocator, .{ .cols = 120, .rows = 24 });
+ defer p.deinit();
+ _ = try p.newShell(1, "");
+ try std.testing.expect(layout.splitColumn(p, 0, 1, false));
+ p.sync();
+
+ // Two panes side by side put two tags on ONE row, which is the case a
+ // per-row origin gets wrong and the reason this is asked per cell.
+ const left = p.view().rects[0];
+ const right = p.view().rects[1];
+ try std.testing.expect(right.x > left.x);
+ try std.testing.expectEqual(@as(f32, @floatFromInt(left.x)), taglineOriginCol(p, left.x + 3, left.y));
+ try std.testing.expectEqual(@as(f32, @floatFromInt(right.x)), taglineOriginCol(p, right.x + 3, right.y));
+
+ // The topbar's origin is column zero always — it is not a pane rect, which
+ // is why row zero is right even in a window with no core to ask.
+ try std.testing.expectEqual(@as(f32, 0), taglineOriginCol(p, 40, 0));
+
+ const body_row = left.y + 2;
+ try std.testing.expectEqual(@as(f32, 7), taglineOriginCol(p, 7, body_row));
+
+ const body_w: f32 = 10;
+ const tag_w: f32 = 8;
+ for (p.view().col_x[0..p.view().ncol], p.view().col_w[0..p.view().ncol]) |origin, width| {
+ if (width == 0) continue;
+ const col = origin;
+ try std.testing.expectEqual(@as(f32, @floatFromInt(origin)), taglineOriginCol(p, col, TOPBAR_H));
+ const glyph_x = @as(f32, @floatFromInt(origin)) * body_w;
+ try std.testing.expectEqual(col, gridColAt(p, glyph_x + tag_w / 2, TOPBAR_H, body_w, tag_w));
+ }
+ for ([_]u16{ 0, 1, 5, 40, 119 }) |col| {
+ const origin = taglineOriginCol(p, col, left.y);
+ const sdl = origin * (body_w - tag_w) + @as(f32, @floatFromInt(col)) * tag_w;
+ const appkit = origin * body_w + (@as(f32, @floatFromInt(col)) - origin) * tag_w;
+ try std.testing.expectEqual(sdl, appkit);
+ }
+
+ for ([_]u16{ 0, 1, 4, 9 }) |offset| {
+ const col = left.x + offset;
+ if (offset >= left.w) break;
+ const origin = taglineOriginCol(p, col, left.y);
+ const glyph_x = origin * body_w + (@as(f32, @floatFromInt(col)) - origin) * tag_w;
+ try std.testing.expectEqual(col, gridColAt(p, glyph_x + tag_w / 2, left.y, body_w, tag_w));
+ }
+
+ // The topbar's pointer grid is compact from column zero, with no pane to
+ // anchor to — the one tag row that is right with or without a core.
+ try std.testing.expectEqual(@as(u16, 3), gridColAt(p, 3.5 * tag_w, 0, body_w, tag_w));
+ try std.testing.expectEqual(@as(u16, 3), gridColAt(null, 3.5 * tag_w, 0, body_w, tag_w));
+
+ // A body row is untouched: still the body grid, still divided by the body
+ // cell. Only tag rows compact.
+ try std.testing.expectEqual(@as(u16, 3), gridColAt(p, 3.5 * body_w, body_row, body_w, tag_w));
+}
+
+test {
+ _ = @import("Addon.zig");
+ _ = @import("rollback.zig");
+ _ = @import("Core.zig");
+ _ = @import("Plumb.zig");
+ _ = @import("lsp/Ask.zig");
+ _ = @import("Jump.zig");
+ _ = @import("keys.zig");
+ _ = @import("Effect.zig");
+ _ = @import("tape.zig");
+ _ = @import("replica.zig");
+ _ = Term;
+ _ = @import("echo.zig");
+ _ = @import("ansi.zig");
+ _ = @import("modal.zig");
+ _ = @import("tagline.zig");
+ _ = @import("hunks.zig");
+ _ = @import("builtins_json.zig");
+ _ = @import("exec.zig");
+ _ = @import("look.zig");
+ _ = @import("sam_edit.zig");
+ _ = @import("edit_cmd.zig");
+ _ = @import("mouse.zig");
+ _ = @import("normal.zig");
+ _ = @import("regexp.zig");
+ _ = @import("edit.zig");
+ _ = @import("Messages.zig");
+ _ = @import("Registers.zig");
+ _ = @import("Macro.zig");
+ _ = @import("selection_pipe.zig");
+ _ = @import("Pipe.zig");
+ _ = @import("colors.zig");
+ _ = @import("surface.zig");
+ _ = @import("body_layer.zig");
+ _ = @import("draw.zig");
+ _ = @import("dump.zig"); // its tests ran nowhere before
+ _ = @import("recent.zig");
+ _ = @import("Store.zig");
+ _ = @import("Db.zig");
+ _ = @import("Places.zig");
+ _ = @import("FrameStats.zig");
+ _ = @import("Query.zig");
+ _ = @import("Names.zig");
+ _ = @import("Stream.zig");
+ _ = @import("Tables.zig");
+ _ = @import("Handover.zig");
+ _ = @import("walk.zig");
+ _ = @import("views.zig");
+ // A window's build links the window's C (font.c over FreeType and
+ // HarfBuzz) into each of its programs, and their allocator (ui_malloc
+ // and the rest) is gui.zig's.
+ if (comptime @import("term_options").terminal and platform == .gui) _ = @import("dev").gui;
+}