From 99f5b10eccd95aa0066ea6fe6bb75f367ab56a47 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 18 Nov 2026 22:03:14 -0300 Subject: core: src/ becomes core/, pardes.zig its root.zig (the reorganisation's tree, step 2d), and every mention of a src/ path follows: the build's paths, the core's imports of its root, the tests that read the repository's files by path (lspbench's, look.zig's spans over core/look.zig), the comments that cite files, the documents and the manual (the typst corpus reads core/tutor.txt and core/plumbing); left as they were: the /src tree's served names (src/pardes.zig and the rest are the 9P tree's, not the repository's), the CHANGELOG's history, fixtures that are only strings; fs-unit-test runs from the core's root (a module of the core rooted at fs.zig had the runtime's and the devices' `core` be fs.zig); nothing else changes: the ledger (pardes.test.* now root.test.*), frames byte-identical, replicas and process-test the base's Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- .agents/skills/pardes/SKILL.md | 2 +- abi/pardes.h | 6 +- abi/pardes_addon.h | 2 +- abi/plain.zig | 2 +- abi/term.zig | 6 +- abi/wire.zig | 18 +- addons/fake/fake.c | 2 +- addons/snake/snake.zig | 2 +- build.zig | 4 +- build.zig.zon | 2 +- build/core.zig | 40 +- build/docs.zig | 2 +- build/library.zig | 6 +- build/patches.zig | 2 +- build/tests.zig | 19 +- build/tools.zig | 4 +- core/Addon.zig | 942 +++++++ core/CHANGELOG.md | 1357 +++++++++ core/Core.zig | 4078 +++++++++++++++++++++++++++ core/Db.zig | 1432 ++++++++++ core/Dir.zig | 479 ++++ core/Editor.zig | 80 + core/Effect.zig | 698 +++++ core/Event.zig | 134 + core/File.zig | 2479 +++++++++++++++++ core/FrameStats.zig | 531 ++++ core/Handover.zig | 698 +++++ core/Jump.zig | 416 +++ core/Kind.zig | 1369 +++++++++ core/Layer.zig | 297 ++ core/Macro.zig | 173 ++ core/Messages.zig | 887 ++++++ core/Names.zig | 361 +++ core/Output.zig | 1101 ++++++++ core/Pipe.zig | 672 +++++ core/Places.zig | 894 ++++++ core/Plumb.zig | 1566 +++++++++++ core/Query.zig | 2757 ++++++++++++++++++ core/Registers.zig | 122 + core/Store.zig | 347 +++ core/Stream.zig | 1544 +++++++++++ core/Tables.zig | 1089 ++++++++ core/Term.zig | 497 ++++ core/Text.zig | 673 +++++ core/Turn.zig | 141 + core/ansi.zig | 495 ++++ core/body_layer.zig | 650 +++++ core/builtins.zig | 2124 ++++++++++++++ core/builtins_json.zig | 136 + core/c_heap.zig | 154 + core/colors.zig | 1210 ++++++++ core/config.zig | 1856 +++++++++++++ core/diff.zig | 679 +++++ core/draw.zig | 1292 +++++++++ core/dump.zig | 1255 +++++++++ core/echo.zig | 838 ++++++ core/edit.zig | 2573 +++++++++++++++++ core/edit_cmd.zig | 800 ++++++ core/exec.zig | 1152 ++++++++ core/fs-help.txt | 45 + core/fs.zig | 1522 ++++++++++ core/grammar_manifest.zig | 196 ++ core/hunks.zig | 1085 ++++++++ core/indent.zig | 367 +++ core/keys.zig | 829 ++++++ core/layout.zig | 2160 +++++++++++++++ core/lib_editor.zig | 605 ++++ core/lib_syntax.zig | 109 + core/locations.zig | 1162 ++++++++ core/look.zig | 2712 ++++++++++++++++++ core/lsp/Ask.zig | 612 ++++ core/lsp/lsp.zig | 207 ++ core/memory.zig | 565 ++++ core/mini.zig | 331 +++ core/modal.zig | 1880 +++++++++++++ core/mouse.zig | 1809 ++++++++++++ core/ninep/addr.zig | 680 +++++ core/ninep/cols.zig | 727 +++++ core/ninep/ctl.zig | 2909 +++++++++++++++++++ core/ninep/events.zig | 1497 ++++++++++ core/ninep/pane.zig | 1342 +++++++++ core/ninep/pty.zig | 1198 ++++++++ core/ninep/screen.zig | 307 ++ core/ninep/sources.zig | 148 + core/ninep/testing.zig | 168 ++ core/ninep/tree.zig | 2273 +++++++++++++++ core/normal.zig | 1633 +++++++++++ core/panes.zig | 1904 +++++++++++++ core/plumbing | 51 + core/recent.zig | 282 ++ core/regexp.zig | 644 +++++ core/replica.zig | 4931 +++++++++++++++++++++++++++++++++ core/rollback.zig | 803 ++++++ core/root.zig | 445 +++ core/sam_edit.zig | 1394 ++++++++++ core/selection_pipe.zig | 661 +++++ core/surface.zig | 496 ++++ core/syntax.zig | 2193 +++++++++++++++ core/tagline.zig | 544 ++++ core/tape.zig | 1463 ++++++++++ core/term/TerminalImages.zig | 382 +++ core/term/pager.zig | 363 +++ core/term/screen_state.zig | 1213 ++++++++ core/term/term.zig | 164 ++ core/term/terminal.zig | 1525 ++++++++++ core/themes/acme.zig | 62 + core/themes/alucard.zig | 76 + core/themes/atelier.zig | 53 + core/themes/ayu_dark.zig | 105 + core/themes/ayu_light.zig | 104 + core/themes/ayu_mirage.zig | 104 + core/themes/catppuccin_frappe.zig | 105 + core/themes/catppuccin_latte.zig | 105 + core/themes/catppuccin_macchiato.zig | 105 + core/themes/catppuccin_mocha.zig | 105 + core/themes/clay.zig | 54 + core/themes/dark.zig | 34 + core/themes/dark_plus.zig | 89 + core/themes/daybreak.zig | 53 + core/themes/doom_one.zig | 104 + core/themes/doom_one_light.zig | 106 + core/themes/dracula.zig | 79 + core/themes/dracula_soft.zig | 79 + core/themes/duckbones.zig | 92 + core/themes/dusk.zig | 53 + core/themes/forestbones_dark.zig | 92 + core/themes/forestbones_light.zig | 94 + core/themes/forge.zig | 55 + core/themes/forge_black.zig | 55 + core/themes/forge_soft.zig | 55 + core/themes/harvest.zig | 54 + core/themes/helix.zig | 60 + core/themes/ink.zig | 53 + core/themes/kanagawabones.zig | 93 + core/themes/lagoon.zig | 54 + core/themes/lapis.zig | 67 + core/themes/monokai.zig | 84 + core/themes/monokai_pro.zig | 85 + core/themes/monokai_pro_classic.zig | 85 + core/themes/monokai_pro_light.zig | 87 + core/themes/monokai_pro_light_sun.zig | 87 + core/themes/monokai_pro_machine.zig | 86 + core/themes/monokai_pro_octagon.zig | 86 + core/themes/monokai_pro_ristretto.zig | 86 + core/themes/monokai_pro_spectrum.zig | 85 + core/themes/neobones_dark.zig | 92 + core/themes/neobones_light.zig | 92 + core/themes/nord.zig | 124 + core/themes/nordbones.zig | 95 + core/themes/orchard.zig | 53 + core/themes/orchard_black.zig | 53 + core/themes/paper.zig | 53 + core/themes/rose_pine.zig | 104 + core/themes/rose_pine_dawn.zig | 104 + core/themes/rose_pine_moon.zig | 104 + core/themes/rosebones_dark.zig | 93 + core/themes/rosebones_light.zig | 94 + core/themes/seoul256.zig | 360 +++ core/themes/seoulbones_dark.zig | 95 + core/themes/seoulbones_light.zig | 95 + core/themes/solarium.zig | 54 + core/themes/solarized_dark.zig | 81 + core/themes/solarized_light.zig | 82 + core/themes/spectrum.zig | 54 + core/themes/tokyobones_dark.zig | 93 + core/themes/tokyobones_light.zig | 94 + core/themes/tokyonight_day.zig | 100 + core/themes/tokyonight_moon.zig | 99 + core/themes/tokyonight_night.zig | 97 + core/themes/tokyonight_storm.zig | 97 + core/themes/vimbones.zig | 95 + core/themes/zenbones_dark.zig | 92 + core/themes/zenbones_light.zig | 93 + core/themes/zenburned.zig | 92 + core/themes/zenwritten_dark.zig | 92 + core/themes/zenwritten_light.zig | 93 + core/tracy.zig | 50 + core/tree_sitter_heap.h | 27 + core/tutor.txt | 361 +++ core/views.zig | 922 ++++++ core/walk.zig | 2600 +++++++++++++++++ dev/gui/gui.zig | 4 +- dev/macos/Info.plist | 2 +- dev/macos/Sources/AppDelegate.swift | 4 +- dev/macos/Sources/PardesView.swift | 8 +- dev/tui/tui.zig | 6 +- docs/diff-pager-proposals.md | 4 +- docs/ghostty-macos-notes.md | 6 +- docs/helix-keys.md | 28 +- docs/lsp-evaluation.md | 12 +- docs/macos.md | 44 +- docs/next-daemon.md | 72 +- docs/next-drag.md | 2 +- docs/next-editor.md | 4 +- docs/next-plumber.md | 20 +- docs/next-run.md | 4 +- docs/next-searchrows.md | 4 +- docs/next-store.md | 2 +- docs/next-terminal.md | 24 +- docs/next-tests.md | 10 +- docs/search-proto-converge.md | 20 +- docs/selections.md | 2 +- docs/shared-libs.md | 4 +- docs/typ/README | 8 +- docs/typ/book.typ | 2 +- docs/typ/building.typ | 40 +- docs/typ/guide.typ | 4 +- docs/typ/reference.typ | 24 +- docs/typ/running.typ | 2 +- docs/typ/style.typ | 12 +- features.txt | 4 +- runtime/9p.zig | 4 +- runtime/daemon.zig | 2 +- runtime/main.zig | 4 +- src/Addon.zig | 942 ------- src/CHANGELOG.md | 1357 --------- src/Core.zig | 4078 --------------------------- src/Db.zig | 1432 ---------- src/Dir.zig | 479 ---- src/Editor.zig | 80 - src/Effect.zig | 698 ----- src/Event.zig | 134 - src/File.zig | 2479 ----------------- src/FrameStats.zig | 531 ---- src/Handover.zig | 698 ----- src/Jump.zig | 416 --- src/Kind.zig | 1369 --------- src/Layer.zig | 297 -- src/Macro.zig | 173 -- src/Messages.zig | 887 ------ src/Names.zig | 361 --- src/Output.zig | 1101 -------- src/Pipe.zig | 672 ----- src/Places.zig | 894 ------ src/Plumb.zig | 1566 ----------- src/Query.zig | 2757 ------------------ src/Registers.zig | 122 - src/Store.zig | 347 --- src/Stream.zig | 1544 ----------- src/Tables.zig | 1089 -------- src/Term.zig | 497 ---- src/Text.zig | 673 ----- src/Turn.zig | 141 - src/ansi.zig | 495 ---- src/body_layer.zig | 650 ----- src/builtins.zig | 2124 -------------- src/builtins_json.zig | 136 - src/c_heap.zig | 154 - src/colors.zig | 1210 -------- src/config.zig | 1856 ------------- src/diff.zig | 679 ----- src/draw.zig | 1292 --------- src/dump.zig | 1255 --------- src/echo.zig | 838 ------ src/edit.zig | 2573 ----------------- src/edit_cmd.zig | 800 ------ src/exec.zig | 1152 -------- src/fs-help.txt | 45 - src/fs.zig | 1522 ---------- src/grammar_manifest.zig | 196 -- src/hunks.zig | 1085 -------- src/indent.zig | 367 --- src/keys.zig | 829 ------ src/layout.zig | 2160 --------------- src/lib_editor.zig | 605 ---- src/lib_syntax.zig | 109 - src/locations.zig | 1162 -------- src/look.zig | 2712 ------------------ src/lsp/Ask.zig | 612 ---- src/lsp/lsp.zig | 207 -- src/memory.zig | 565 ---- src/mini.zig | 331 --- src/modal.zig | 1880 ------------- src/mouse.zig | 1809 ------------ src/ninep/addr.zig | 680 ----- src/ninep/cols.zig | 727 ----- src/ninep/ctl.zig | 2909 ------------------- src/ninep/events.zig | 1497 ---------- src/ninep/pane.zig | 1342 --------- src/ninep/pty.zig | 1198 -------- src/ninep/screen.zig | 307 -- src/ninep/sources.zig | 148 - src/ninep/testing.zig | 168 -- src/ninep/tree.zig | 2273 --------------- src/normal.zig | 1633 ----------- src/panes.zig | 1904 ------------- src/pardes.zig | 445 --- src/plumbing | 51 - src/recent.zig | 282 -- src/regexp.zig | 644 ----- src/replica.zig | 4931 --------------------------------- src/rollback.zig | 803 ------ src/sam_edit.zig | 1394 ---------- src/selection_pipe.zig | 661 ----- src/surface.zig | 496 ---- src/syntax.zig | 2193 --------------- src/tagline.zig | 544 ---- src/tape.zig | 1463 ---------- src/term/TerminalImages.zig | 382 --- src/term/pager.zig | 363 --- src/term/screen_state.zig | 1213 -------- src/term/term.zig | 164 -- src/term/terminal.zig | 1525 ---------- src/themes/acme.zig | 62 - src/themes/alucard.zig | 76 - src/themes/atelier.zig | 53 - src/themes/ayu_dark.zig | 105 - src/themes/ayu_light.zig | 104 - src/themes/ayu_mirage.zig | 104 - src/themes/catppuccin_frappe.zig | 105 - src/themes/catppuccin_latte.zig | 105 - src/themes/catppuccin_macchiato.zig | 105 - src/themes/catppuccin_mocha.zig | 105 - src/themes/clay.zig | 54 - src/themes/dark.zig | 34 - src/themes/dark_plus.zig | 89 - src/themes/daybreak.zig | 53 - src/themes/doom_one.zig | 104 - src/themes/doom_one_light.zig | 106 - src/themes/dracula.zig | 79 - src/themes/dracula_soft.zig | 79 - src/themes/duckbones.zig | 92 - src/themes/dusk.zig | 53 - src/themes/forestbones_dark.zig | 92 - src/themes/forestbones_light.zig | 94 - src/themes/forge.zig | 55 - src/themes/forge_black.zig | 55 - src/themes/forge_soft.zig | 55 - src/themes/harvest.zig | 54 - src/themes/helix.zig | 60 - src/themes/ink.zig | 53 - src/themes/kanagawabones.zig | 93 - src/themes/lagoon.zig | 54 - src/themes/lapis.zig | 67 - src/themes/monokai.zig | 84 - src/themes/monokai_pro.zig | 85 - src/themes/monokai_pro_classic.zig | 85 - src/themes/monokai_pro_light.zig | 87 - src/themes/monokai_pro_light_sun.zig | 87 - src/themes/monokai_pro_machine.zig | 86 - src/themes/monokai_pro_octagon.zig | 86 - src/themes/monokai_pro_ristretto.zig | 86 - src/themes/monokai_pro_spectrum.zig | 85 - src/themes/neobones_dark.zig | 92 - src/themes/neobones_light.zig | 92 - src/themes/nord.zig | 124 - src/themes/nordbones.zig | 95 - src/themes/orchard.zig | 53 - src/themes/orchard_black.zig | 53 - src/themes/paper.zig | 53 - src/themes/rose_pine.zig | 104 - src/themes/rose_pine_dawn.zig | 104 - src/themes/rose_pine_moon.zig | 104 - src/themes/rosebones_dark.zig | 93 - src/themes/rosebones_light.zig | 94 - src/themes/seoul256.zig | 360 --- src/themes/seoulbones_dark.zig | 95 - src/themes/seoulbones_light.zig | 95 - src/themes/solarium.zig | 54 - src/themes/solarized_dark.zig | 81 - src/themes/solarized_light.zig | 82 - src/themes/spectrum.zig | 54 - src/themes/tokyobones_dark.zig | 93 - src/themes/tokyobones_light.zig | 94 - src/themes/tokyonight_day.zig | 100 - src/themes/tokyonight_moon.zig | 99 - src/themes/tokyonight_night.zig | 97 - src/themes/tokyonight_storm.zig | 97 - src/themes/vimbones.zig | 95 - src/themes/zenbones_dark.zig | 92 - src/themes/zenbones_light.zig | 93 - src/themes/zenburned.zig | 92 - src/themes/zenwritten_dark.zig | 92 - src/themes/zenwritten_light.zig | 93 - src/tracy.zig | 50 - src/tree_sitter_heap.h | 27 - src/tutor.txt | 361 --- src/views.zig | 922 ------ src/walk.zig | 2600 ----------------- test/behaviour/addons.zig | 2 +- test/behaviour/language_servers.zig | 2 +- test/daemon.py | 2 +- test/deck/replay.zig | 2 +- test/e2e_harness.zig | 2 +- test/hxdiff.zig | 4 +- test/hxlib.zig | 2 +- test/kitty.zig | 2 +- test/kitty_client.zig | 2 +- test/lspbench.zig | 32 +- test/macos_e2e.swift | 2 +- test/monkey9p.zig | 2 +- test/process.zig | 2 +- test/process/tty_boot.zig | 2 +- test/record_demos.py | 6 +- test/replicas.zig | 12 +- test/rollback_bench.zig | 6 +- test/session.zig | 6 +- test/terminal.zig | 4 +- test/tty.zig | 2 +- tools/demos/jumps.txt | 2 +- tools/gen_themes.zig | 8 +- tools/shared_libs_check.zig | 2 +- vendor/queries/README.md | 2 +- vendor/queries/rust/indents.scm | 2 +- 404 files changed, 102025 insertions(+), 102024 deletions(-) create mode 100644 core/Addon.zig create mode 100644 core/CHANGELOG.md create mode 100644 core/Core.zig create mode 100644 core/Db.zig create mode 100644 core/Dir.zig create mode 100644 core/Editor.zig create mode 100644 core/Effect.zig create mode 100644 core/Event.zig create mode 100644 core/File.zig create mode 100644 core/FrameStats.zig create mode 100644 core/Handover.zig create mode 100644 core/Jump.zig create mode 100644 core/Kind.zig create mode 100644 core/Layer.zig create mode 100644 core/Macro.zig create mode 100644 core/Messages.zig create mode 100644 core/Names.zig create mode 100644 core/Output.zig create mode 100644 core/Pipe.zig create mode 100644 core/Places.zig create mode 100644 core/Plumb.zig create mode 100644 core/Query.zig create mode 100644 core/Registers.zig create mode 100644 core/Store.zig create mode 100644 core/Stream.zig create mode 100644 core/Tables.zig create mode 100644 core/Term.zig create mode 100644 core/Text.zig create mode 100644 core/Turn.zig create mode 100644 core/ansi.zig create mode 100644 core/body_layer.zig create mode 100644 core/builtins.zig create mode 100644 core/builtins_json.zig create mode 100644 core/c_heap.zig create mode 100644 core/colors.zig create mode 100644 core/config.zig create mode 100644 core/diff.zig create mode 100644 core/draw.zig create mode 100644 core/dump.zig create mode 100644 core/echo.zig create mode 100644 core/edit.zig create mode 100644 core/edit_cmd.zig create mode 100644 core/exec.zig create mode 100644 core/fs-help.txt create mode 100644 core/fs.zig create mode 100644 core/grammar_manifest.zig create mode 100644 core/hunks.zig create mode 100644 core/indent.zig create mode 100644 core/keys.zig create mode 100644 core/layout.zig create mode 100644 core/lib_editor.zig create mode 100644 core/lib_syntax.zig create mode 100644 core/locations.zig create mode 100644 core/look.zig create mode 100644 core/lsp/Ask.zig create mode 100644 core/lsp/lsp.zig create mode 100644 core/memory.zig create mode 100644 core/mini.zig create mode 100644 core/modal.zig create mode 100644 core/mouse.zig create mode 100644 core/ninep/addr.zig create mode 100644 core/ninep/cols.zig create mode 100644 core/ninep/ctl.zig create mode 100644 core/ninep/events.zig create mode 100644 core/ninep/pane.zig create mode 100644 core/ninep/pty.zig create mode 100644 core/ninep/screen.zig create mode 100644 core/ninep/sources.zig create mode 100644 core/ninep/testing.zig create mode 100644 core/ninep/tree.zig create mode 100644 core/normal.zig create mode 100644 core/panes.zig create mode 100644 core/plumbing create mode 100644 core/recent.zig create mode 100644 core/regexp.zig create mode 100644 core/replica.zig create mode 100644 core/rollback.zig create mode 100644 core/root.zig create mode 100644 core/sam_edit.zig create mode 100644 core/selection_pipe.zig create mode 100644 core/surface.zig create mode 100644 core/syntax.zig create mode 100644 core/tagline.zig create mode 100644 core/tape.zig create mode 100644 core/term/TerminalImages.zig create mode 100644 core/term/pager.zig create mode 100644 core/term/screen_state.zig create mode 100644 core/term/term.zig create mode 100644 core/term/terminal.zig create mode 100644 core/themes/acme.zig create mode 100644 core/themes/alucard.zig create mode 100644 core/themes/atelier.zig create mode 100644 core/themes/ayu_dark.zig create mode 100644 core/themes/ayu_light.zig create mode 100644 core/themes/ayu_mirage.zig create mode 100644 core/themes/catppuccin_frappe.zig create mode 100644 core/themes/catppuccin_latte.zig create mode 100644 core/themes/catppuccin_macchiato.zig create mode 100644 core/themes/catppuccin_mocha.zig create mode 100644 core/themes/clay.zig create mode 100644 core/themes/dark.zig create mode 100644 core/themes/dark_plus.zig create mode 100644 core/themes/daybreak.zig create mode 100644 core/themes/doom_one.zig create mode 100644 core/themes/doom_one_light.zig create mode 100644 core/themes/dracula.zig create mode 100644 core/themes/dracula_soft.zig create mode 100644 core/themes/duckbones.zig create mode 100644 core/themes/dusk.zig create mode 100644 core/themes/forestbones_dark.zig create mode 100644 core/themes/forestbones_light.zig create mode 100644 core/themes/forge.zig create mode 100644 core/themes/forge_black.zig create mode 100644 core/themes/forge_soft.zig create mode 100644 core/themes/harvest.zig create mode 100644 core/themes/helix.zig create mode 100644 core/themes/ink.zig create mode 100644 core/themes/kanagawabones.zig create mode 100644 core/themes/lagoon.zig create mode 100644 core/themes/lapis.zig create mode 100644 core/themes/monokai.zig create mode 100644 core/themes/monokai_pro.zig create mode 100644 core/themes/monokai_pro_classic.zig create mode 100644 core/themes/monokai_pro_light.zig create mode 100644 core/themes/monokai_pro_light_sun.zig create mode 100644 core/themes/monokai_pro_machine.zig create mode 100644 core/themes/monokai_pro_octagon.zig create mode 100644 core/themes/monokai_pro_ristretto.zig create mode 100644 core/themes/monokai_pro_spectrum.zig create mode 100644 core/themes/neobones_dark.zig create mode 100644 core/themes/neobones_light.zig create mode 100644 core/themes/nord.zig create mode 100644 core/themes/nordbones.zig create mode 100644 core/themes/orchard.zig create mode 100644 core/themes/orchard_black.zig create mode 100644 core/themes/paper.zig create mode 100644 core/themes/rose_pine.zig create mode 100644 core/themes/rose_pine_dawn.zig create mode 100644 core/themes/rose_pine_moon.zig create mode 100644 core/themes/rosebones_dark.zig create mode 100644 core/themes/rosebones_light.zig create mode 100644 core/themes/seoul256.zig create mode 100644 core/themes/seoulbones_dark.zig create mode 100644 core/themes/seoulbones_light.zig create mode 100644 core/themes/solarium.zig create mode 100644 core/themes/solarized_dark.zig create mode 100644 core/themes/solarized_light.zig create mode 100644 core/themes/spectrum.zig create mode 100644 core/themes/tokyobones_dark.zig create mode 100644 core/themes/tokyobones_light.zig create mode 100644 core/themes/tokyonight_day.zig create mode 100644 core/themes/tokyonight_moon.zig create mode 100644 core/themes/tokyonight_night.zig create mode 100644 core/themes/tokyonight_storm.zig create mode 100644 core/themes/vimbones.zig create mode 100644 core/themes/zenbones_dark.zig create mode 100644 core/themes/zenbones_light.zig create mode 100644 core/themes/zenburned.zig create mode 100644 core/themes/zenwritten_dark.zig create mode 100644 core/themes/zenwritten_light.zig create mode 100644 core/tracy.zig create mode 100644 core/tree_sitter_heap.h create mode 100644 core/tutor.txt create mode 100644 core/views.zig create mode 100644 core/walk.zig delete mode 100644 src/Addon.zig delete mode 100644 src/CHANGELOG.md delete mode 100644 src/Core.zig delete mode 100644 src/Db.zig delete mode 100644 src/Dir.zig delete mode 100644 src/Editor.zig delete mode 100644 src/Effect.zig delete mode 100644 src/Event.zig delete mode 100644 src/File.zig delete mode 100644 src/FrameStats.zig delete mode 100644 src/Handover.zig delete mode 100644 src/Jump.zig delete mode 100644 src/Kind.zig delete mode 100644 src/Layer.zig delete mode 100644 src/Macro.zig delete mode 100644 src/Messages.zig delete mode 100644 src/Names.zig delete mode 100644 src/Output.zig delete mode 100644 src/Pipe.zig delete mode 100644 src/Places.zig delete mode 100644 src/Plumb.zig delete mode 100644 src/Query.zig delete mode 100644 src/Registers.zig delete mode 100644 src/Store.zig delete mode 100644 src/Stream.zig delete mode 100644 src/Tables.zig delete mode 100644 src/Term.zig delete mode 100644 src/Text.zig delete mode 100644 src/Turn.zig delete mode 100644 src/ansi.zig delete mode 100644 src/body_layer.zig delete mode 100644 src/builtins.zig delete mode 100644 src/builtins_json.zig delete mode 100644 src/c_heap.zig delete mode 100644 src/colors.zig delete mode 100644 src/config.zig delete mode 100644 src/diff.zig delete mode 100644 src/draw.zig delete mode 100644 src/dump.zig delete mode 100644 src/echo.zig delete mode 100644 src/edit.zig delete mode 100644 src/edit_cmd.zig delete mode 100644 src/exec.zig delete mode 100644 src/fs-help.txt delete mode 100644 src/fs.zig delete mode 100644 src/grammar_manifest.zig delete mode 100644 src/hunks.zig delete mode 100644 src/indent.zig delete mode 100644 src/keys.zig delete mode 100644 src/layout.zig delete mode 100644 src/lib_editor.zig delete mode 100644 src/lib_syntax.zig delete mode 100644 src/locations.zig delete mode 100644 src/look.zig delete mode 100644 src/lsp/Ask.zig delete mode 100644 src/lsp/lsp.zig delete mode 100644 src/memory.zig delete mode 100644 src/mini.zig delete mode 100644 src/modal.zig delete mode 100644 src/mouse.zig delete mode 100644 src/ninep/addr.zig delete mode 100644 src/ninep/cols.zig delete mode 100644 src/ninep/ctl.zig delete mode 100644 src/ninep/events.zig delete mode 100644 src/ninep/pane.zig delete mode 100644 src/ninep/pty.zig delete mode 100644 src/ninep/screen.zig delete mode 100644 src/ninep/sources.zig delete mode 100644 src/ninep/testing.zig delete mode 100644 src/ninep/tree.zig delete mode 100644 src/normal.zig delete mode 100644 src/panes.zig delete mode 100644 src/pardes.zig delete mode 100644 src/plumbing delete mode 100644 src/recent.zig delete mode 100644 src/regexp.zig delete mode 100644 src/replica.zig delete mode 100644 src/rollback.zig delete mode 100644 src/sam_edit.zig delete mode 100644 src/selection_pipe.zig delete mode 100644 src/surface.zig delete mode 100644 src/syntax.zig delete mode 100644 src/tagline.zig delete mode 100644 src/tape.zig delete mode 100644 src/term/TerminalImages.zig delete mode 100644 src/term/pager.zig delete mode 100644 src/term/screen_state.zig delete mode 100644 src/term/term.zig delete mode 100644 src/term/terminal.zig delete mode 100644 src/themes/acme.zig delete mode 100644 src/themes/alucard.zig delete mode 100644 src/themes/atelier.zig delete mode 100644 src/themes/ayu_dark.zig delete mode 100644 src/themes/ayu_light.zig delete mode 100644 src/themes/ayu_mirage.zig delete mode 100644 src/themes/catppuccin_frappe.zig delete mode 100644 src/themes/catppuccin_latte.zig delete mode 100644 src/themes/catppuccin_macchiato.zig delete mode 100644 src/themes/catppuccin_mocha.zig delete mode 100644 src/themes/clay.zig delete mode 100644 src/themes/dark.zig delete mode 100644 src/themes/dark_plus.zig delete mode 100644 src/themes/daybreak.zig delete mode 100644 src/themes/doom_one.zig delete mode 100644 src/themes/doom_one_light.zig delete mode 100644 src/themes/dracula.zig delete mode 100644 src/themes/dracula_soft.zig delete mode 100644 src/themes/duckbones.zig delete mode 100644 src/themes/dusk.zig delete mode 100644 src/themes/forestbones_dark.zig delete mode 100644 src/themes/forestbones_light.zig delete mode 100644 src/themes/forge.zig delete mode 100644 src/themes/forge_black.zig delete mode 100644 src/themes/forge_soft.zig delete mode 100644 src/themes/harvest.zig delete mode 100644 src/themes/helix.zig delete mode 100644 src/themes/ink.zig delete mode 100644 src/themes/kanagawabones.zig delete mode 100644 src/themes/lagoon.zig delete mode 100644 src/themes/lapis.zig delete mode 100644 src/themes/monokai.zig delete mode 100644 src/themes/monokai_pro.zig delete mode 100644 src/themes/monokai_pro_classic.zig delete mode 100644 src/themes/monokai_pro_light.zig delete mode 100644 src/themes/monokai_pro_light_sun.zig delete mode 100644 src/themes/monokai_pro_machine.zig delete mode 100644 src/themes/monokai_pro_octagon.zig delete mode 100644 src/themes/monokai_pro_ristretto.zig delete mode 100644 src/themes/monokai_pro_spectrum.zig delete mode 100644 src/themes/neobones_dark.zig delete mode 100644 src/themes/neobones_light.zig delete mode 100644 src/themes/nord.zig delete mode 100644 src/themes/nordbones.zig delete mode 100644 src/themes/orchard.zig delete mode 100644 src/themes/orchard_black.zig delete mode 100644 src/themes/paper.zig delete mode 100644 src/themes/rose_pine.zig delete mode 100644 src/themes/rose_pine_dawn.zig delete mode 100644 src/themes/rose_pine_moon.zig delete mode 100644 src/themes/rosebones_dark.zig delete mode 100644 src/themes/rosebones_light.zig delete mode 100644 src/themes/seoul256.zig delete mode 100644 src/themes/seoulbones_dark.zig delete mode 100644 src/themes/seoulbones_light.zig delete mode 100644 src/themes/solarium.zig delete mode 100644 src/themes/solarized_dark.zig delete mode 100644 src/themes/solarized_light.zig delete mode 100644 src/themes/spectrum.zig delete mode 100644 src/themes/tokyobones_dark.zig delete mode 100644 src/themes/tokyobones_light.zig delete mode 100644 src/themes/tokyonight_day.zig delete mode 100644 src/themes/tokyonight_moon.zig delete mode 100644 src/themes/tokyonight_night.zig delete mode 100644 src/themes/tokyonight_storm.zig delete mode 100644 src/themes/vimbones.zig delete mode 100644 src/themes/zenbones_dark.zig delete mode 100644 src/themes/zenbones_light.zig delete mode 100644 src/themes/zenburned.zig delete mode 100644 src/themes/zenwritten_dark.zig delete mode 100644 src/themes/zenwritten_light.zig delete mode 100644 src/tracy.zig delete mode 100644 src/tree_sitter_heap.h delete mode 100644 src/tutor.txt delete mode 100644 src/views.zig delete mode 100644 src/walk.zig diff --git a/.agents/skills/pardes/SKILL.md b/.agents/skills/pardes/SKILL.md index 6e2f2eff..c9fb39ff 100644 --- a/.agents/skills/pardes/SKILL.md +++ b/.agents/skills/pardes/SKILL.md @@ -118,7 +118,7 @@ pardes --detach=work & # a session with no screen; --attach=work shows it These act on the user's session: do them when asked. Anything more (read panes, run commands, react to clicks) is the pardes-9p skill. Config is `~/.config/pardes/init`, a builtin a line; keys are compiled in -(`src/config.zig`). `$NINE_MOUNT/agents`, where present, is cloud9's +(`core/config.zig`). `$NINE_MOUNT/agents`, where present, is cloud9's read-only view of running coding agents. ## The book diff --git a/abi/pardes.h b/abi/pardes.h index c4470569..65ca5c60 100644 --- a/abi/pardes.h +++ b/abi/pardes.h @@ -1,7 +1,7 @@ // libpardes — the C ABI the macOS app links against. // // Hand-written, and kept honest by a test: build.zig translate-C's this file -// into the unit-test build, and src/macos.zig asserts every constant and +// into the unit-test build, and runtime/macos.zig asserts every constant and // struct layout below against the Zig side (ghostty's trick — see docs/macos.md). // Change nothing here without running `zig build unit-test -Dplatform=macos`. // @@ -27,7 +27,7 @@ extern "C" { // ---------------------------------------------------------------- frame -// One rendered cell. Mirrors pardes.Cell (src/pardes.zig) flattened for the +// One rendered cell. Mirrors pardes.Cell (core/root.zig) flattened for the // wire. typedef struct { // UTF-8 grapheme, `len` bytes. The core caps graphemes at seven bytes; the @@ -322,7 +322,7 @@ uint32_t pardes_topbar_pane_border_rgb(void); // Rows the global topbar occupies, and therefore the row index of the FIRST // pane tagline — the one the rule above joins the topbar band to. Mirrors -// pardes.TOPBAR_H, and src/macos.zig asserts the two agree. +// pardes.TOPBAR_H, and runtime/macos.zig asserts the two agree. #define PARDES_TOPBAR_H 1u // The active pane's tag row background, for hosts that afford the active diff --git a/abi/pardes_addon.h b/abi/pardes_addon.h index 11dcec38..2f5c53de 100644 --- a/abi/pardes_addon.h +++ b/abi/pardes_addon.h @@ -25,7 +25,7 @@ * its head's minor, an addon pardes's by api->size. * * addons/fake/fake.c implements every function; it is the template. In - * Zig, src/kinds/addon.zig makes the pa_ops from a struct, and + * Zig, abi/addon.zig makes the pa_ops from a struct, and * addons/snake/snake.zig is a game in it. */ #ifndef PARDES_ADDON_H diff --git a/abi/plain.zig b/abi/plain.zig index 6ca96fe9..c250de03 100644 --- a/abi/plain.zig +++ b/abi/plain.zig @@ -2,7 +2,7 @@ //! (docs/next-terminal.md, Phase 1): a frame's cells, their styles and //! colours, the pictures over them and a key read. Data that needs //! nothing else, in a module of its own: the core (surface.zig and -//! Event.zig, under their old names) and the terminal (src/term/) both +//! Event.zig, under their old names) and the terminal (core/term/) both //! import it, and neither imports the other. const std = @import("std"); /// The terminal as the core sees it: its entries and the records they diff --git a/abi/term.zig b/abi/term.zig index de969796..de32a575 100644 --- a/abi/term.zig +++ b/abi/term.zig @@ -2,8 +2,8 @@ //! entries (`Ops`) a terminal module gives the core, and the records they //! take and give. A terminal's state, its replicas' encoder and tracks are //! handles the core holds and never looks into; what it knows of them it is -//! told here, as numbers, bytes, text and cells. src/term/term.zig is the -//! one implementation (ghostty-vt behind it); src/Term.zig is the core's +//! told here, as numbers, bytes, text and cells. core/term/term.zig is the +//! one implementation (ghostty-vt behind it); core/Term.zig is the core's //! side of it. const std = @import("std"); const plain = @import("plain.zig"); @@ -282,7 +282,7 @@ pub const Peer = struct { pub const Applied = struct { seq: u32, extra: []const u8 }; /// What the dump keeps of a terminal beside its emulator, by name as every -/// dump is (src/walk.zig, through the core's `Term.State`). +/// dump is (core/walk.zig, through the core's `Term.State`). pub const Parts = struct { reply: []const u8 = "", reply_dropped: u32 = 0, diff --git a/abi/wire.zig b/abi/wire.zig index d6098325..cf3daf2e 100644 --- a/abi/wire.zig +++ b/abi/wire.zig @@ -16,9 +16,9 @@ const config = @import("pardes_config"); /// 22: a PDF's pixel scroll (0x1a) is gone with the core's PDF, the pdf /// addon's panes scrolled by the wheel as any kind's are; /// 21: a session's views are `Pardes.views` in its dump and its digest -/// (src/views.zig), the panes' shares of the others under `shares`; +/// (core/views.zig), the panes' shares of the others under `shares`; /// 20: a digest takes a blob of 4 KiB and up (a file's text, a register's, -/// an undo entry's bytes) by its length, not its bytes (src/walk.zig): a +/// an undo entry's bytes) by its length, not its bytes (core/walk.zig): a /// text's revision says what it holds; /// 19: a client joining again names the view it had in its hello, and a /// bye says whether it joins again for it (views.away); @@ -90,7 +90,7 @@ pub const ClientTag = enum(u8) { hello = 0x01, bye = 0x02, ping = 0x03, - /// A replica's word about a terminal's state (src/term/screen_state.zig): the + /// A replica's word about a terminal's state (core/term/screen_state.zig): the /// delta it holds, or rows it asks for. Opaque here. screen_ack = 0x04, /// What is the session's digest, in stream order (a replica checks @@ -103,7 +103,7 @@ pub const ClientTag = enum(u8) { want = 0x07, /// The session's clocks (realtime and monotonic, ns) as the client /// guessed them for the input that follows: its time, which the step - /// reads (src/replica.zig). + /// reads (core/replica.zig). stamp = 0x08, key = 0x10, @@ -166,26 +166,26 @@ pub const ServerTag = enum(u8) { refused = 0x02, /// A ping's value back. pong = 0x03, - /// A piece of the join's dump (src/dump.zig): the welcome says how long + /// A piece of the join's dump (core/dump.zig): the welcome says how long /// it is whole. dump = 0x04, - /// A unit of the authority's stream (src/replica.zig): its origin, seq, + /// A unit of the authority's stream (core/replica.zig): its origin, seq, /// clocks and tape. unit = 0x05, - /// A terminal's delta, or rows asked for (src/term/screen_state.zig), applied + /// A terminal's delta, or rows asked for (core/term/screen_state.zig), applied /// before the next unit. Opaque here. screen = 0x06, /// The session's digest (u64le), after every unit before it. check = 0x07, /// Part of the join's list of blobs, to a far client: each one's place - /// in the state, its length and its BLAKE3 hash (src/replica.zig). + /// in the state, its length and its BLAKE3 hash (core/replica.zig). blobs = 0x08, /// A piece of a far client's stream from here on, zstd-compressed: the /// messages in it are framed as these are. Opaque here. zstd = 0x09, /// A piece of a record's bytes by hash (tape.zig `Big`), before the /// unit naming it: the hash, the whole's length and this piece's place - /// (u64le each), then at most 1 MiB of it (src/replica.zig). + /// (u64le each), then at most 1 MiB of it (core/replica.zig). blob = 0x0a, }; diff --git a/addons/fake/fake.c b/addons/fake/fake.c index 648dcace..b3f6b288 100644 --- a/addons/fake/fake.c +++ b/addons/fake/fake.c @@ -2,7 +2,7 @@ * lines, and what the loader's tests drive (test/behaviour/addons.zig, * `pardes --addon-test`). * - * cc -shared -fPIC -I src/kinds -o fake.so addons/fake/fake.c + * cc -shared -fPIC -I abi -o fake.so addons/fake/fake.c * * A pane of it counts: keys and clicks and ticks and words each add to a * count it paints, dumps and answers `where` with; `:N` sets it. It paints diff --git a/addons/snake/snake.zig b/addons/snake/snake.zig index 5d1e06db..ef2283ac 100644 --- a/addons/snake/snake.zig +++ b/addons/snake/snake.zig @@ -1,4 +1,4 @@ -//! snake.zig: a snake game as a pardes addon, over src/kinds/addon.zig +//! snake.zig: a snake game as a pardes addon, over abi/addon.zig //! (`zig build addon-test` builds it as snake.so and runs snake.script). //! h j k l steer; once dead, any key starts again. It steps only while its //! pane has the keyboard, and asks for nothing otherwise. diff --git a/build.zig b/build.zig index 57112bad..a97933c7 100644 --- a/build.zig +++ b/build.zig @@ -181,14 +181,14 @@ pub fn build(b: *std.Build) void { const root_mod = root_set.root; // hxdiff's headless core and pardes-isolate are the primary shell's // configuration compiled two more ways, not two more frontends. - const core_cfg: core.Core = .{ .root = "src/pardes.zig", .config = opts, .builtin_addons = compiled_in, .tracy = true }; + const core_cfg: core.Core = .{ .root = "core/root.zig", .config = opts, .builtin_addons = compiled_in, .tracy = true }; const core_set = core.module(b, parts, core_cfg); const core_module = core_set.root; // The core without the terminal module (docs/next-terminal.md, Phase 1): // what core-test is compiled from. No ghostty-vt, wuffs, zstbi or C++; // a shell boot is a scratch pane, a test that needs a terminal skips, // and shell-test runs them all with one. - const lean_cfg: core.Core = .{ .root = "src/pardes.zig", .config = opts, .builtin_addons = compiled_in, .terminal = false }; + const lean_cfg: core.Core = .{ .root = "core/root.zig", .config = opts, .builtin_addons = compiled_in, .terminal = false }; const core_lean = core.module(b, parts, lean_cfg); const isolated_mod: ?*std.Build.Module = if (platform == .tui) core.module(b, parts, .{ .root = "runtime/main.zig", .config = opts, .builtin_addons = compiled_in, .isolated = true }).root else null; // `zig build static`'s: the addons compiled in (Addon.zig). Linked diff --git a/build.zig.zon b/build.zig.zon index 2804add7..b893f330 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -239,7 +239,7 @@ .hash = "N-V-__8AAG02ugUcWec-Ndp-i7JTsJ0dgF8nnJRUInkGLG7G", .lazy = true, }, - // zstd for a far replica client's stream (src/replica.zig): the + // zstd for a far replica client's stream (core/replica.zig): the // upstream release tarball, its lib/ compiled by build.zig. .zstd = .{ .url = "https://github.com/facebook/zstd/releases/download/v1.5.7/zstd-1.5.7.tar.gz", diff --git a/build/core.zig b/build/core.zig index 26de1669..974d5c7e 100644 --- a/build/core.zig +++ b/build/core.zig @@ -1,7 +1,7 @@ const std = @import("std"); const Platform = @import("../build.zig").Platform; const TreeSitterGrammars = @import("../build.zig").TreeSitterGrammars; -const grammar_manifest = @import("../src/grammar_manifest.zig"); +const grammar_manifest = @import("../core/grammar_manifest.zig"); const addons = @import("addons.zig"); const patches = @import("patches.zig"); /// The package manifest, imported UNTYPED on purpose — see the `version` @@ -16,7 +16,7 @@ else /// Every dependency the manifest pins, by its content hash: a grammar, /// ghostty or the Unicode tables steps a core differently, so they are part -/// of a replica's semantics hash (src/wire.zig `semantics`). +/// of a replica's semantics hash (abi/wire.zig `semantics`). const deps_text = blk: { var text: []const u8 = ""; for (@typeInfo(@TypeOf(zon.dependencies)).@"struct".field_names) |name| { @@ -71,8 +71,8 @@ pub const ShellConfig = struct { /// `Build` word's, stamped with the commit (see `stamp_commit`). change: ?[]const u8 = null, built: ?[]const u8 = null, - /// The library's editor (src/lib_editor.zig): a text with no pane, so - /// the core is not compiled (src/Editor.zig). + /// The library's editor (core/lib_editor.zig): a text with no pane, so + /// the core is not compiled (core/Editor.zig). lib: bool = false, }; @@ -189,7 +189,7 @@ pub fn treeSitter(b: *std.Build, io: std.Io, target: std.Build.ResolvedTarget, c // Every allocation a grammar's external scanner makes goes to the // runtime's allocator, which syntax.zig points at a Zig one: // TREE_SITTER_REUSE_ALLOCATOR for the scanners that use - // ts_malloc, and src/tree_sitter_heap.h for the ones that call + // ts_malloc, and core/tree_sitter_heap.h for the ones that call // malloc themselves. const ts_cflags = [_][]const u8{ "-fno-sanitize=function", @@ -197,7 +197,7 @@ pub fn treeSitter(b: *std.Build, io: std.Io, target: std.Build.ResolvedTarget, c // From the compiler's working directory, the root // package's: pardes's own, or the one that depends on it. "-include", - b.root.joinString(b.allocator, "src/tree_sitter_heap.h") catch @panic("OOM"), + b.root.joinString(b.allocator, "core/tree_sitter_heap.h") catch @panic("OOM"), }; lib.root_module.addCSourceFile(.{ .file = dep.path(g.src ++ "/parser.c"), .flags = &ts_cflags }); if (g.scanner) lib.root_module.addCSourceFile(.{ .file = dep.path(g.src ++ "/scanner.c"), .flags = &ts_cflags }); @@ -227,7 +227,7 @@ pub fn uucodeModule(b: *std.Build, target: std.Build.ResolvedTarget, optimize: s }) catch return null).module("uucode"); } -/// The editor as a library (src/lib_editor.zig, docs/next-editor.md): the +/// The editor as a library (core/lib_editor.zig, docs/next-editor.md): the /// text pane's own code built with `lib`, so no pane and no core, over /// tree-sitter, uucode and mvzr, with pardes-syntax in it as `.syntax`. /// `library` exports it to a package that asks; hxdiff runs its cases on it. @@ -243,7 +243,7 @@ pub fn editorModule(b: *std.Build, name: ?[]const u8, target: std.Build.Resolved const options: std.Build.Module.CreateOptions = .{ .target = target, .optimize = optimize, - .root_source_file = b.path("src/lib_editor.zig"), + .root_source_file = b.path("core/lib_editor.zig"), .link_libc = true, }; const m = if (name) |n| b.addModule(n, options) else b.createModule(options); @@ -254,7 +254,7 @@ pub fn editorModule(b: *std.Build, name: ?[]const u8, target: std.Build.Resolved m.addImport("generated_themes", d.themes); d.ts.wire(m); m.addImport("mvzr", d.mvzr); - m.addImport("c_heap", b.createModule(.{ .target = target, .optimize = optimize, .root_source_file = b.path("src/c_heap.zig") })); + m.addImport("c_heap", b.createModule(.{ .target = target, .optimize = optimize, .root_source_file = b.path("core/c_heap.zig") })); m.addImport("vaxis", d.vaxis); m.addImport("uucode", d.uucode); return m; @@ -299,7 +299,7 @@ pub const Parts = struct { /// Which core: its root and the few ways the products differ. pub const Core = struct { /// runtime/main.zig for a shell (runtime/macos.zig for the macOS - /// library), src/pardes.zig for the core, runtime/root.zig for the + /// library), core/root.zig for the core, runtime/root.zig for the /// runtime, or a core file whose tests run on the core's imports. root: []const u8, /// The shell's options (`pardes_config`): this build's, or the window's @@ -335,7 +335,7 @@ pub const Modules = struct { /// and link named here and nowhere else. pub fn module(b: *std.Build, p: Parts, c: Core) Modules { const in_runtime = std.mem.startsWith(u8, c.root, "runtime/"); - const m = b.createModule(.{ .target = p.target, .optimize = p.optimize, .root_source_file = b.path(if (in_runtime) "src/pardes.zig" else c.root), .link_libc = true }); + const m = b.createModule(.{ .target = p.target, .optimize = p.optimize, .root_source_file = b.path(if (in_runtime) "core/root.zig" else c.root), .link_libc = true }); // forkpty: in libc proper on glibc>=2.34 and darwin; the BSDs keep it in libutil if (c.util and (p.target.result.os.tag == .freebsd or p.target.result.os.tag == .netbsd or p.target.result.os.tag == .openbsd)) m.linkSystemLibrary("util", .{}); @@ -409,7 +409,7 @@ pub const Assembly = struct { /// The shell's options (`pardes_config`), and the window's in the bare build. opts: *std.Build.Step.Options, gui_opts: ?*std.Build.Step.Options, - /// What an addon in Zig imports (src/kinds/addon.zig). + /// What an addon in Zig imports (abi/addon.zig). addon_mod: *std.Build.Module, pdf: addons.Pdf, image_addon: *std.Build.Step.Compile, @@ -484,7 +484,7 @@ pub fn assemble(b: *std.Build, c: struct { const c_heap_mod = b.createModule(.{ .target = target, .optimize = optimize, - .root_source_file = b.path("src/c_heap.zig"), + .root_source_file = b.path("core/c_heap.zig"), }); // MuPDF and FreeType, lazy (build.zig's `pdf` option has why). @@ -499,10 +499,10 @@ pub fn assemble(b: *std.Build, c: struct { }) catch null else null; const freetype = if (freetype_dep) |d| d.artifact("freetype") else null; - // The kinds' C ABI (src/kinds/pardes_addon.h), translated once: the core's + // The kinds' C ABI (abi/pardes_addon.h), translated once: the core's // records are the header's, never a mirror of it. const kind_mod = b.addTranslateC(.{ .root_source_file = b.path("abi/pardes_addon.h"), .target = target, .optimize = optimize }).createModule(); - // ...and what an addon in Zig imports over it (src/kinds/addon.zig). + // ...and what an addon in Zig imports over it (abi/addon.zig). const addon_mod = b.createModule(.{ .root_source_file = b.path("abi/addon.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "pardes_addon", .module = kind_mod }} }); const pdf_built = addons.pdf(b, .{ .addon_mod = addon_mod, @@ -551,7 +551,7 @@ pub fn assemble(b: *std.Build, c: struct { patches.zstbi(b, patch, zstbi_dep, zstbi_mod); // One zstd (docs/shared-libs.md): a far replica's stream, compressed by - // its session and read by its client (src/replica.zig). The library's + // its session and read by its client (core/replica.zig). The library's // compressor and decompressor alone, single-threaded and without its // x86 assembly, each context made with ZSTD_customMem over c_heap; of // its block compressors only those of levels 3 (dfast) and 6 (lazy), @@ -588,16 +588,16 @@ pub fn assemble(b: *std.Build, c: struct { break :zstd lib; }; - // xitdb keeps what pardes writes to disk (src/Db.zig): pure Zig, no C + // xitdb keeps what pardes writes to disk (core/Db.zig): pure Zig, no C // library and no allocator hook of its own (it takes Zig allocators). const xitdb_mod = b.dependency("xitdb", .{ .target = target, .optimize = optimize }).module("xitdb"); const image_addon = addons.image(b, target, c_optimize, addon_mod, zstbi_dep); - // The records the core and the terminal both name (src/plain/plain.zig). + // The records the core and the terminal both name (abi/plain.zig). const plain_mod = b.createModule(.{ .root_source_file = b.path("abi/plain.zig"), .target = target, .optimize = optimize }); - // The terminal, a module of its own (src/term/term.zig): the emulator and + // The terminal, a module of its own (core/term/term.zig): the emulator and // what it brings are its, and every core but core-test's is given it. - const term_mod = b.createModule(.{ .root_source_file = b.path("src/term/term.zig"), .target = target, .optimize = optimize, .link_libc = true }); + const term_mod = b.createModule(.{ .root_source_file = b.path("core/term/term.zig"), .target = target, .optimize = optimize, .link_libc = true }); term_mod.addImport("plain", plain_mod); if (ghostty_vt_for_tests) |x| term_mod.addImport("ghostty-vt", x); if (wuffs_mod) |x| term_mod.addImport("wuffs", x); diff --git a/build/docs.zig b/build/docs.zig index b698da02..e6cd9e72 100644 --- a/build/docs.zig +++ b/build/docs.zig @@ -40,7 +40,7 @@ pub fn add(b: *std.Build) [3]*std.Build.Step.Run { } /// docs/typ/builtins-.json: every builtin, from the registry and the Help -/// rows (src/builtins_json.zig), rewritten in the source tree so a plain +/// rows (core/builtins_json.zig), rewritten in the source tree so a plain /// `typst compile` reads it too; a unit test fails when it is stale. /// A terminal build writes builtins-tui.json and a window build /// builtins-gui.json; style.typ merges the two into one glossary. diff --git a/build/library.zig b/build/library.zig index 049b4323..ebfd8596 100644 --- a/build/library.zig +++ b/build/library.zig @@ -4,7 +4,7 @@ const core = @import("core.zig"); const patches = @import("patches.zig"); /// pardes as a library, all a package that depends on it is given: the -/// `pardes-syntax` module (src/lib_syntax.zig), the grammars, highlighting +/// `pardes-syntax` module (core/lib_syntax.zig), the grammars, highlighting /// and themes for a program that draws source text itself. It takes /// tree-sitter and the grammars of its `tree-sitter` option and no other /// dependency, so nothing else is fetched (build.zig.zon). test/lib is such @@ -19,7 +19,7 @@ pub fn build(b: *std.Build) void { const mod = b.addModule("pardes-syntax", .{ .target = target, .optimize = optimize, - .root_source_file = b.path("src/lib_syntax.zig"), + .root_source_file = b.path("core/lib_syntax.zig"), .link_libc = true, }); // A library's options: its grammars, nothing stamped, no window. @@ -29,7 +29,7 @@ pub fn build(b: *std.Build) void { const themes = core.generatedThemes(b, io, target, optimize); mod.addImport("generated_themes", themes); ts.wire(mod); - mod.addImport("c_heap", b.createModule(.{ .target = target, .optimize = optimize, .root_source_file = b.path("src/c_heap.zig") })); + mod.addImport("c_heap", b.createModule(.{ .target = target, .optimize = optimize, .root_source_file = b.path("core/c_heap.zig") })); // The editor, `pardes-editor`, to a package that asks for it: uucode, // mvzr and vaxis (with what its build names) are fetched then and only // then. Of vaxis it takes the grapheme width, which uucode alone feeds. diff --git a/build/patches.zig b/build/patches.zig index b6b6b86d..5e120345 100644 --- a/build/patches.zig +++ b/build/patches.zig @@ -258,7 +258,7 @@ pub fn ghosttyKitty(b: *std.Build, patch: *std.Build.Step.Compile, dep: *std.Bui /// no bound on its work (its own test "do not make this test any /// longer": `a?` n times is 2^n steps), so each entry to matchPattern, /// which its backtracking recurses through, spends a step, and with none -/// left the match fails and says so (src/regexp.zig). +/// left the match fails and says so (core/regexp.zig). /// - A group's greedy match never gave back to what follows the group: on /// `aab`, `(a+)ab` missed where `a+ab` matched. hasAlt counts the group's /// own `)` as an alternative, so every group with pattern after it takes diff --git a/build/tests.zig b/build/tests.zig index 0eaf7c63..fe9e1355 100644 --- a/build/tests.zig +++ b/build/tests.zig @@ -74,9 +74,9 @@ pub fn add(b: *std.Build, c: struct { core_test_step.dependOn(&run_core_test.step); if (core_match) |check| check.addArtifactArg(core_test); } - // The terminal module's own tests (src/term/): the emulator without a core. + // The terminal module's own tests (core/term/): the emulator without a core. const run_term_test = units.add(b, b.addTest(.{ .root_module = c.parts.term, .filters = c.test_filters }), null); - b.step("term-test", "run the terminal module's own tests (src/term/)").dependOn(&run_term_test.step); + b.step("term-test", "run the terminal module's own tests (core/term/)").dependOn(&run_term_test.step); // test/terminal.zig reads the emulator behind a core's terminal. const pane_tests = units.root(b, "test/panes.zig", true, &.{ .{ .name = "pardes", .module = c.core_module }, .{ .name = "term", .module = c.parts.term }, .{ .name = "plain", .module = c.parts.plain } }); if (c.ghostty_vt_for_tests) |x| pane_tests.root_module.addImport("ghostty-vt", x); @@ -193,13 +193,14 @@ pub fn add(b: *std.Build, c: struct { // pty and the shell's output, is the kitty-harness step. _ = units.add(b, units.root(b, "test/kitty.zig", true, &.{.{ .name = "pardes", .module = c.core_module }}), b.step("kitty-test", "run the kitty graphics conformance cases for terminal panes")); - // 9p_io.zig's and fs.zig's tests as roots of their own, on the - // core's imports and C libraries: native transport integration - // stays reachable even when Zig's filter leaves the core's - // anonymous test-import block out. + // 9p_io.zig's and fs.zig's tests on the core's imports and C + // libraries: native transport integration stays reachable even when + // Zig's filter leaves the core's anonymous test-import block out. Each + // from its module's root (runtime/root.zig, core/root.zig): the + // modules name each other by module, whose `core` is the core's root. for ([_]struct { path: []const u8, step: []const u8, about: []const u8, filters: []const []const u8 }{ .{ .path = "runtime/root.zig", .step = "9p-io-test", .about = "run native 9P transport and client integration tests", .filters = &.{ "Unix and TCP", "one fetch", "expired sessions", "posts itself" } }, - .{ .path = "src/fs.zig", .step = "fs-unit-test", .about = "run filesystem unit tests", .filters = c.test_filters }, + .{ .path = "core/root.zig", .step = "fs-unit-test", .about = "run filesystem unit tests", .filters = c.test_filters }, }) |t| { var cfg = c.core_cfg; cfg.root = t.path; @@ -214,9 +215,9 @@ pub fn add(b: *std.Build, c: struct { protocol_step.dependOn(&units.add(b, protocol_tests, null).step); if (testMatch(b, c.unit_profile, c.test_filters, protocol_step)) |check| check.addArtifactArg(protocol_tests); _ = units.add(b, units.root(b, "dev/fonts.zig", true, cloud9), null); - _ = units.add(b, units.root(b, "src/c_heap.zig", false, cloud9), null); + _ = units.add(b, units.root(b, "core/c_heap.zig", false, cloud9), null); // Its lookup goes wrong only where the optimizer folds it. - _ = units.add(b, b.addTest(.{ .root_module = b.createModule(.{ .target = c.target, .optimize = .safe, .root_source_file = b.path("src/c_heap.zig"), .link_libc = false, .imports = cloud9 }), .filters = c.test_filters }), null); + _ = units.add(b, b.addTest(.{ .root_module = b.createModule(.{ .target = c.target, .optimize = .safe, .root_source_file = b.path("core/c_heap.zig"), .link_libc = false, .imports = cloud9 }), .filters = c.test_filters }), null); _ = units.add(b, units.root(b, "test/deck/replay.zig", null, &.{.{ .name = "deck", .module = b.createModule(.{ .target = c.target, .optimize = c.optimize, .root_source_file = b.path("dev/gui/deck.zig") }) }}), null); diff --git a/build/tools.zig b/build/tools.zig index c6f25e6d..1343fef9 100644 --- a/build/tools.zig +++ b/build/tools.zig @@ -107,7 +107,7 @@ pub fn add(b: *std.Build, c: struct { const hxdiff_step = b.step("hxdiff", "compare Helix results (-- --strict cases reference [waivers]; cases alone emits; --parity: a pty pane against a file pane)"); hxdiff_step.dependOn(&run_hxdiff.step); - // The same cases on the library (src/lib_editor.zig): test/hxlib.zig + // The same cases on the library (core/lib_editor.zig): test/hxlib.zig // types them into an editor value and prints its results, which // hxdiff holds against helix's goldens, or with `-- --parity` // against the file pane, so the library cannot drift from either. @@ -235,7 +235,7 @@ pub fn add(b: *std.Build, c: struct { // The syntax module's own tests and snapshots, on the core without // a terminal: its tests reach the editor's modes (modal.zig). var syntax_cfg = c.lean; - syntax_cfg.root = "src/syntax.zig"; + syntax_cfg.root = "core/syntax.zig"; const syntax_module = core.module(b, c.parts, syntax_cfg).root; const syntax_runner = program(b, "pardes-syntax", "test/syntax.zig", c.target, c.optimize, &.{.{ .name = "syntax", .module = syntax_module }}); const run_syntax = b.addRunArtifact(syntax_runner); diff --git a/core/Addon.zig b/core/Addon.zig new file mode 100644 index 00000000..5c3f84e2 --- /dev/null +++ b/core/Addon.zig @@ -0,0 +1,942 @@ +//! Addons: shared libraries that each bring a kind of pane (Kind.zig). +//! +//! The host's half: the libraries this process holds (each a copy of its +//! file's bytes in a memfd, its BLAKE3, its head checked and its init +//! run), shared by every core that took the same bytes and closed when no +//! core and no job holds one; the scan of `$XDG_DATA_HOME/pardes/addons` +//! and `/lib/pardes/addons` at start, which lists a library from +//! the head it kept of it while its file is the same, and loads it at the +//! first pane of its kind (`need`); the crash mark. An addon +//! runs in this process: one that crashes takes the session with it. So a +//! load is marked first (`addon-loading` in the data directory, holding +//! its path) and unmarked after; a start that finds the mark moves it to +//! `addon-crashed`, and skips that addon at every start until `Addon load` +//! asks for it by path again. +//! +//! The core's half, deterministic: the `Addon` word, the `addon` event's +//! update (the collisions, a reload's validate-then-swap, the panes that +//! waited), unload, and /addons. Nothing there reads a host fact: what a +//! library brought comes in the event, as data a replica's core takes too. +const std = @import("std"); +const Kind = @import("Kind.zig"); +const dump = @import("dump.zig"); +const pardes = @import("root.zig"); +const tape = @import("tape.zig"); +const Pardes = pardes.Pardes; + +const Why = Kind.Buf(160); + +/// What the effect asks: a library loaded (its path), loaded again for a +/// kind's reload (the kind's path), or let go (its BLAKE3, the arg's bytes). +pub const Op = enum { load, reload, release }; + +// ---- the libraries this process holds (the host's) ---- + +const Lib = struct { hash: [32]u8, handle: *anyopaque, ops: *const Kind.Ops, fd: c_int, refs: u32 = 1, jobs: u32 = 0 }; +var libs: [32]?Lib = @splat(null); + +fn libOf(ops: *const Kind.Ops) ?*Lib { + for (&libs) |*l| if (l.*) |*lib| if (lib.ops == ops) return lib; + return null; +} + +/// A job of `ops` under way (true), or answered: its library stays until +/// the last of them, so no worker is unmapped under its own code. +pub fn holdJob(ops: *const Kind.Ops, held: bool) void { + const lib = libOf(ops) orelse return; + if (held) lib.jobs += 1 else lib.jobs -|= 1; + closeIdle(lib); +} + +fn closeIdle(lib: *Lib) void { + if (lib.refs > 0 or lib.jobs > 0) return; + _ = std.c.dlclose(lib.handle); + _ = std.c.close(lib.fd); + for (&libs) |*l| if (l.*) |*x| if (x == lib) { + l.* = null; + }; +} + +/// The host's side of the `addon` effect: the library read and checked, +/// and what it brought (or why not) the core's `addon` event. +pub fn perform(p: *Pardes, op: Op, pane: u8, arg: []const u8) void { + if (op == .release) { + for (&libs) |*l| if (l.*) |*lib| if (arg.len == 32 and std.mem.eql(u8, &lib.hash, arg)) { + lib.refs -|= 1; + return closeIdle(lib); + }; + return; + } + var words: [64]LoadedWord = undefined; + var why: Why = .{}; + const l = readLoaded(arg, &words, &why); + p.update(.{ .addon = .{ .op = op, .pane = pane, .kind = &l } }); +} + +/// What a load reads off the library at `path` (held): the `addon` +/// event's, for a caller that steps its core itself (a replica's authority). +pub fn readLoaded(path: []const u8, words: *[64]LoadedWord, why: *Why) Loaded { + var data_buf: [4096]u8 = undefined; + return read(dataDir(&data_buf), path, words, why); +} + +/// The library at `path`, held (a second hold of the same bytes is the +/// first's library) and checked: what it brings, or why not. +fn read(data: []const u8, path: []const u8, words: *[64]LoadedWord, why: *Why) Loaded { + if (comptime compiled_in.len > 0 or @import("builtin").os.tag != .linux) return .{ .path = path, .why = why.print("not on this platform: its addons are compiled in", .{}) }; + var z: [4096]u8 = undefined; + const path_z = std.fmt.bufPrintSentinel(&z, "{s}", .{path}, 0) catch return .{ .path = path, .why = why.print("the path is too long", .{}) }; + const file = std.c.open(path_z, .{ .CLOEXEC = true }); + if (file < 0) return .{ .path = path, .why = why.print("cannot read it", .{}) }; + defer _ = std.c.close(file); + return readOpen(data, path, file, words, why); +} + +/// `read` of the library open at `file`. +fn readOpen(data: []const u8, path: []const u8, file: c_int, words: *[64]LoadedWord, why: *Why) Loaded { + mark(data, path); + defer unmark(); + var hash: [32]u8 = undefined; + const lib = hold(file, &hash, false, why) orelse return .{ .path = path, .why = why.slice() }; + const l = loadedOf(lib.ops, path, hash, words); + const refused: ?[]const u8 = refused: { + const h = &lib.ops.head; + if (h.size < @sizeOf(Kind.c.pa_head)) break :refused "refused: its head is too short for this pardes"; + if (h.abi >> 16 != Kind.abi_major) break :refused why.print("refused: wants abi {d}, this pardes speaks {d}", .{ h.abi >> 16, Kind.abi_major }); + if (lib.ops.paint == null) break :refused "refused: it has no paint"; + for (l.words) |w| if (w.opens == 0 and lib.ops.ctl == null) break :refused "refused: it has words for its panes and no ctl"; + if ((lib.ops.job_run == null) != (lib.ops.job_done == null)) break :refused "refused: a job needs job_run and job_done"; + if (h.nwords > words.len) break :refused "refused: no room for its words"; + if (lib.ops.init) |init| if (init(&Kind.api) != Kind.c.PA_OK) break :refused "refused: its init failed"; + break :refused null; + }; + const r = refused orelse return l; + if (r.ptr != &why.bytes) why.set(r); + // What is listed of it outlives its library: its name, in `words`. + const name: []u8 = @as([*]u8, @ptrCast(words))[0..@min(15, l.name.len)]; + @memcpy(name, l.name[0..name.len]); + lib.refs -= 1; + closeIdle(lib); + return .{ .name = name, .version = l.version, .abi = l.abi, .hash = hash, .path = path, .why = why.slice() }; +} + +/// The library open at `file` held: its bytes copied into a memfd (by the +/// kernel), so a reload of a path already open is the file as it is now +/// (dlopen hands back the handle it has for a path it has) and the file +/// can be written over while it runs, unless this process holds those very +/// bytes already. `hash` is their BLAKE3: computed here, or `known` (the +/// start's, of a file the same since). +fn hold(file: c_int, hash: *[32]u8, known: bool, why: *Why) ?*Lib { + if (known) for (&libs) |*l| if (l.*) |*lib| if (std.mem.eql(u8, &lib.hash, hash)) { + lib.refs += 1; + return lib; + }; + const linux = std.os.linux; + var st: linux.Statx = undefined; + if (std.c.statx(file, "", linux.AT.EMPTY_PATH, .{ .SIZE = true }, &st) != 0) return failNull(why, "cannot read it", .{}); + if (st.size > 256 << 20) return failNull(why, "it is over 256 MiB", .{}); + const size: usize = @intCast(st.size); + // The fd stays open while its library is: /proc/self/fd/ of a closed + // fd is the next load's path too, and dlopen would hand back this one. + const fd = std.c.memfd_create("pardes-addon", 0); + if (fd < 0) return failNull(why, "no memfd for it", .{}); + var off: std.c.off_t = 0; + while (off < size) if (std.c.sendfile(fd, file, &off, size - @as(usize, @intCast(off))) <= 0) { + _ = std.c.close(fd); + return failNull(why, "cannot copy it", .{}); + }; + if (!known) { + const at = if (size == 0) null else std.c.mmap(null, size, .{ .READ = true }, .{ .TYPE = .SHARED }, fd, 0); + if (at == std.c.MAP_FAILED) { + _ = std.c.close(fd); + return failNull(why, "cannot read it", .{}); + } + std.crypto.hash.Blake3.hash(if (at) |m| @as([*]const u8, @ptrCast(m))[0..size] else "", hash, .{}); + if (at) |m| _ = std.c.munmap(@alignCast(m), size); + for (&libs) |*l| if (l.*) |*lib| if (std.mem.eql(u8, &lib.hash, hash)) { + _ = std.c.close(fd); + lib.refs += 1; + return lib; + }; + } + const free = for (&libs) |*l| { + if (l.* == null) break l; + } else { + _ = std.c.close(fd); + return failNull(why, "this process holds 32 libraries already", .{}); + }; + var proc: [64]u8 = undefined; + const at = std.fmt.bufPrintSentinel(&proc, "/proc/self/fd/{d}", .{fd}, 0) catch unreachable; + const handle = std.c.dlopen(at, .{ .NOW = true }) orelse { + _ = std.c.close(fd); + return failNull(why, "{s}", .{if (std.c.dlerror()) |e| std.mem.span(e) else "cannot load it"}); + }; + const sym = std.c.dlsym(handle, "pardes_addon") orelse { + _ = std.c.dlclose(handle); + _ = std.c.close(fd); + return failNull(why, "refused: it has no pardes_addon", .{}); + }; + free.* = .{ .hash = hash.*, .handle = handle, .ops = @ptrCast(@alignCast(sym)), .fd = fd }; + return &free.*.?; +} + +fn failNull(why: *Why, comptime fmt: []const u8, args: anytype) ?*Lib { + _ = why.print(fmt, args); + return null; +} + +/// What the scan at start found, which every core this process makes +/// starts with (Options.addons). +var found: [Kind.max_kinds]Loaded = undefined; +var found_words: [Kind.max_kinds][64]LoadedWord = undefined; +var found_text: [Kind.max_kinds][2][4096]u8 = undefined; +/// A library listed from its head and not loaded yet: its file, open since +/// the scan (`need` loads it from there), else -1; and its head in `heads`. +var found_file: [Kind.max_kinds]c_int = @splat(-1); +var found_head: [Kind.max_kinds]?u8 = @splat(null); +var nfound: usize = 0; +var scanned = false; + +/// What a library brought at its last load, kept for the next start in the +/// data directory's `addon-heads` (one after another, in this build's +/// layout; the first line says which by pardes's version, not its commit, +/// so an installed build and one being worked on share it; 16 at most, any +/// prefix's). A library whose file is the same (device, inode, size, +/// modification and change times) is listed from its head and loaded at the +/// first pane of its kind (`need`); reading and hashing every library at +/// every start was most of a start's work. A cache: a start without it, or +/// with another version's, loads every library. +const Head = struct { + file: [5]i64, + path: Kind.Buf(256), + name: [16]u8, + version: u32, + abi: u32, + hash: [32]u8, + caps: Kind.Caps, + exts: Kind.Buf(512), + nwords: u8, + words: [64]Kind.Word, +}; +const heads_line = std.fmt.comptimePrint("pardes addon heads {s} {d} {x}\n", .{ pardes.version, @sizeOf(Head), Kind.abi }); +var heads: [Kind.max_kinds]Head = undefined; +var nheads: usize = 0; +var heads_changed = false; + +/// The file open at `fd` as a head keys it: its device, inode, size and +/// times; null where it cannot be told. +fn fileOf(fd: c_int) ?[5]i64 { + const linux = std.os.linux; + var st: linux.Statx = undefined; + if (std.c.statx(fd, "", linux.AT.EMPTY_PATH, .{ .INO = true, .SIZE = true, .MTIME = true, .CTIME = true }, &st) != 0) return null; + return .{ @as(i64, st.dev_major) << 32 | st.dev_minor, @bitCast(st.ino), @bitCast(st.size), st.mtime.sec * std.time.ns_per_s + st.mtime.nsec, st.ctime.sec * std.time.ns_per_s + st.ctime.nsec }; +} + +/// The addons compiled in (`zig build static`, and any build off Linux): +/// the image and pdf modules' `pardes_addon` declarations, which only a +/// library exports. A static binary cannot dlopen, and the loader below +/// is Linux's (a memfd): this is how either has image and PDF panes. +/// A build with -Dpdf=false has the image addon alone. +const compiled_in: []const *const Kind.Ops = if (!@import("builtin_addons").on) + &.{} +else if (@import("builtin_addons").pdf) + &.{ &@import("image_addon").pardes_addon, &@import("pdf_addon").pardes_addon } +else + &.{&@import("image_addon").pardes_addon}; + +/// The addons compiled in, as if loaded (`builtin` their path); else every +/// library of the two directories, in name order each, the user's first. A +/// library that crashed at its last load is listed, not loaded; one whose +/// head is kept is listed from it, loaded at its first use. +pub fn scan() []const Loaded { + if (scanned) return found[0..nfound]; + scanned = true; + for (compiled_in) |ops| { + found[nfound] = loadedOf(ops, "builtin", @splat(0), &found_words[nfound]); + if (ops.init) |init| if (init(&Kind.api) != Kind.c.PA_OK) { + found[nfound].why = "refused: its init failed"; + }; + nfound += 1; + } + if (compiled_in.len > 0) return found[0..nfound]; + var data_buf: [4096]u8 = undefined; + const data = dataDir(&data_buf); + const crashed = crashedPath(data); + var heads_buf: [4096]u8 = undefined; + const heads_path = if (data.len > 0) markPath(&heads_buf, data, "addon-heads") else null; + if (heads_path) |at| if (std.c.fopen(at, "r")) |f| { + var line: [heads_line.len]u8 = undefined; + if (std.c.fread(&line, 1, line.len, f) == line.len and std.mem.eql(u8, &line, heads_line)) + nheads = std.c.fread(@ptrCast(&heads), @sizeOf(Head), heads.len, f); + _ = std.c.fclose(f); + }; + var dir_buf: [4096]u8 = undefined; + if (data.len > 0) scanDir(data, std.fmt.bufPrint(&dir_buf, "{s}/addons", .{data}) catch "", crashed.slice()); + var exe: [4096]u8 = undefined; + const n = std.c.readlink("/proc/self/exe", &exe, exe.len); + if (n > 0) if (std.Io.Dir.path.dirname(exe[0..@intCast(n)])) |bin| if (std.Io.Dir.path.dirname(bin)) |prefix| + scanDir(data, std.fmt.bufPrint(&dir_buf, "{s}/lib/pardes/addons", .{prefix}) catch "", crashed.slice()); + // The heads kept, when one changed (other pardes builds' libraries + // with them): written beside and renamed over, so a start reading it + // meanwhile reads either. + if (heads_path) |at| if (heads_changed) { + var tmp_buf: [4096]u8 = undefined; + const tmp = std.fmt.bufPrintSentinel(&tmp_buf, "{s}.{d}", .{ at, std.c.getpid() }, 0) catch return found[0..nfound]; + const f = std.c.fopen(tmp, "w") orelse return found[0..nfound]; + var ok = std.c.fwrite(heads_line.ptr, 1, heads_line.len, f) == heads_line.len; + ok = ok and std.c.fwrite(@ptrCast(&heads), @sizeOf(Head), nheads, f) == nheads; + ok = std.c.fclose(f) == 0 and ok; + _ = if (ok) std.c.rename(tmp, at) else std.c.unlink(tmp); + }; + return found[0..nfound]; +} + +/// The library at `path` listed: from its head, its file the same as when +/// the head was kept, the file held open for its first use (`need`); else +/// read now, and its head kept for the next start. +fn listed(data: []const u8, path: []const u8, n: usize, why: *Why) Loaded { + var z: [4096]u8 = undefined; + const path_z = std.fmt.bufPrintSentinel(&z, "{s}", .{path}, 0) catch return read(data, path, &found_words[n], why); + const file = std.c.open(path_z, .{ .CLOEXEC = true }); + if (file < 0) return read(data, path, &found_words[n], why); + const same = fileOf(file); + if (same) |key| for (heads[0..nheads], 0..) |*h, i| if (std.mem.eql(i64, &h.file, &key) and std.mem.eql(u8, h.path.slice(), path)) { + found_file[n] = file; + found_head[n] = @intCast(i); + const words = found_words[n][0..h.nwords]; + for (h.words[0..h.nwords], words) |*w, *out| out.* = .{ .name = w.name.slice(), .leader = w.leader.slice(), .arg = w.arg, .opens = w.opens, .doc = w.doc.slice() }; + return .{ .name = std.mem.sliceTo(&h.name, 0), .version = h.version, .abi = h.abi, .hash = h.hash, .caps = h.caps, .path = path, .exts = h.exts.slice(), .words = words }; + }; + defer _ = std.c.close(file); + const l = readOpen(data, path, file, &found_words[n], why); + const key = same orelse return l; + // Kept only whole: a name, leader or path cut to fit would be listed + // otherwise than its library's. + if (l.why.len > 0 or path.len > 256 or l.exts.len > 512) return l; + for (l.words) |w| if (w.name.len > 32 or w.leader.len > 8) return l; + // In place of its path's old head, else a new one, else the last that + // no library listed now uses. + const at: usize = for (heads[0..nheads], 0..) |*old, i| { + if (std.mem.eql(u8, old.path.slice(), path)) break i; + } else if (nheads < heads.len) nheads else for (0..heads.len) |back| { + if (std.mem.indexOfScalar(?u8, found_head[0..n], @intCast(heads.len - 1 - back)) == null) break heads.len - 1 - back; + } else return l; + const h = &heads[at]; + h.* = .{ .file = key, .path = .{}, .name = @splat(0), .version = l.version, .abi = l.abi, .hash = l.hash, .caps = l.caps, .exts = .{}, .nwords = @intCast(l.words.len), .words = undefined }; + h.path.set(path); + @memcpy(h.name[0..l.name.len], l.name); + h.exts.set(l.exts); + // A doc is cut to the Word's room as a core's table cuts it. + for (l.words, h.words[0..l.words.len]) |w, *out| { + out.* = .{ .kind = 0, .arg = w.arg, .opens = w.opens }; + out.name.set(w.name); + out.leader.set(w.leader); + out.doc.set(w.doc); + } + found_head[n] = @intCast(at); + nheads = @max(nheads, at + 1); + heads_changed = true; + return l; +} + +/// Kind `k`'s code, a session's (a replica's is `plainOps`'s or none): its +/// library loaded at the first pane of its kind, from the file the start +/// listed it from (`listed`); null, and why, when it will not load. +pub fn need(r: *Kind.Registry, k: u8, why: *Why) ?*const Kind.Ops { + if (r.ops[k] != null) return r.ops[k]; + // A client's: its own code for a plain kind, loaded at its first pane. + if (tape.replaying()) { + r.ops[k] = plainOps(&r.kinds[k]); + return r.ops[k]; + } + const e = &r.kinds[k]; + for (found[0..nfound], 0..) |*l, i| if (l.why.len == 0 and std.mem.eql(u8, &l.hash, &e.hash) and std.mem.eql(u8, l.path, e.path.slice())) { + if (l.ops == 0) l.ops = loadListed(i, why) orelse return null; + r.ops[k] = @ptrFromInt(l.ops); + return r.ops[k]; + }; + _ = why.print("addon {s} is not loaded", .{e.nameOf()}); + return null; +} + +/// The library `found[i]` listed from its head, loaded: from its file as +/// the scan opened it, so a library renamed over since is still the one +/// listed; one written over in place since is refused unless its bytes +/// are the same. +fn loadListed(i: usize, why: *Why) ?usize { + if (comptime compiled_in.len > 0 or @import("builtin").os.tag != .linux) return null; + const l = &found[i]; + var data_buf: [4096]u8 = undefined; + mark(dataDir(&data_buf), l.path); + defer unmark(); + const h = &heads[found_head[i].?]; + const same = if (fileOf(found_file[i])) |key| std.mem.eql(i64, &key, &h.file) else false; + var hash = l.hash; + const lib = hold(found_file[i], &hash, same, why) orelse return null; + const changed = !std.mem.eql(u8, &hash, &l.hash); + const failed = !changed and if (lib.ops.init) |init| init(&Kind.api) != Kind.c.PA_OK else false; + if (changed or failed) { + if (changed) + _ = why.print("{s} changed on disk since this session listed it: Addon reload {s}", .{ std.Io.Dir.path.basename(l.path), l.name }) + else + _ = why.print("{s}: refused: its init failed", .{l.name}); + lib.refs -= 1; + closeIdle(lib); + return null; + } + _ = std.c.close(found_file[i]); + found_file[i] = -1; + return @intFromPtr(lib.ops); +} + +fn scanDir(data: []const u8, dir: []const u8, crashed: []const u8) void { + var z: [4096]u8 = undefined; + const dz = std.fmt.bufPrintSentinel(&z, "{s}", .{dir}, 0) catch return; + const d = std.c.opendir(dz) orelse return; + defer _ = std.c.closedir(d); + var names: [32][256]u8 = undefined; + var lens: [32]usize = undefined; + var count: usize = 0; + while (std.c.readdir(d)) |e| { + const name = std.mem.sliceTo(@as([*:0]const u8, @ptrCast(&e.name)), 0); + if (!std.mem.endsWith(u8, name, ".so") or name.len > 255 or count == names.len) continue; + @memcpy(names[count][0..name.len], name); + lens[count] = name.len; + count += 1; + } + var order: [32]usize = undefined; + for (0..count) |i| order[i] = i; + const Sort = struct { + names: *const [32][256]u8, + lens: *const [32]usize, + fn lt(s: @This(), a: usize, b: usize) bool { + return std.mem.lessThan(u8, s.names[a][0..s.lens[a]], s.names[b][0..s.lens[b]]); + } + }; + std.mem.sort(usize, order[0..count], Sort{ .names = &names, .lens = &lens }, Sort.lt); + for (order[0..count]) |i| { + if (nfound == found.len) return; + const text = &found_text[nfound]; + const path = std.fmt.bufPrint(&text[0], "{s}/{s}", .{ dir, names[i][0..lens[i]] }) catch continue; + var why: Why = .{}; + found[nfound] = if (crashed.len > 0 and std.mem.eql(u8, path, crashed)) + .{ .path = path, .why = "skipped: it crashed its last load (Addon load asks for it again)" } + else + listed(data, path, nfound, &why); + const w = found[nfound].why; + @memcpy(text[1][0..w.len], w); + found[nfound].why = text[1][0..w.len]; + nfound += 1; + } +} + +/// Where the crash mark goes: the data directory, none in a test (a +/// test's loads are no user's). +fn dataDir(buf: []u8) []const u8 { + if (@import("builtin").is_test) return ""; + return dump.defaultDirectory(buf) orelse ""; +} + +// ---- the crash mark ---- + +var marked: [4096]u8 = undefined; +var marked_len: usize = 0; + +fn markPath(buf: []u8, data: []const u8, comptime name: []const u8) ?[:0]const u8 { + return std.fmt.bufPrintSentinel(buf, "{s}/" ++ name, .{data}, 0) catch null; +} + +/// The load of `path` under way, on disk until `unmark`. +fn mark(data: []const u8, path: []const u8) void { + if (data.len == 0) return; + var buf: [4096]u8 = undefined; + const at = markPath(&buf, data, "addon-loading") orelse return; + const f = std.c.fopen(at, "w") orelse return; + _ = std.c.fwrite(path.ptr, 1, path.len, f); + _ = std.c.fclose(f); + @memcpy(marked[0..at.len], at); + marked_len = at.len; + // `Addon load` of the one that crashed asks for it again. + var cbuf: [4096]u8 = undefined; + const crashed = markPath(&cbuf, data, "addon-crashed") orelse return; + if (std.mem.eql(u8, readSmall(crashed).slice(), path)) _ = std.c.unlink(crashed); +} + +fn unmark() void { + if (marked_len == 0) return; + marked[marked_len] = 0; + _ = std.c.unlink(@ptrCast(&marked)); + marked_len = 0; +} + +/// The library that crashed its session's load: a mark left by a start +/// that never finished one becomes `addon-crashed`, which it stays. +fn crashedPath(data: []const u8) Kind.Buf(4096) { + var out: Kind.Buf(4096) = .{}; + if (data.len == 0) return out; + var lbuf: [4096]u8 = undefined; + var cbuf: [4096]u8 = undefined; + const loading = markPath(&lbuf, data, "addon-loading") orelse return out; + const crashed = markPath(&cbuf, data, "addon-crashed") orelse return out; + if (std.c.access(loading, 0) == 0) _ = std.c.rename(loading, crashed); + out.set(readSmall(crashed).slice()); + return out; +} + +fn readSmall(path: [*:0]const u8) Kind.Buf(4096) { + var out: Kind.Buf(4096) = .{}; + const f = std.c.fopen(path, "r") orelse return out; + defer _ = std.c.fclose(f); + out.len = @intCast(std.c.fread(&out.bytes, 1, out.bytes.len, f)); + return out; +} + +// ---- the tail: what a library brings, and a core's tables of it ---- + +/// What a library brings, as the host read it off its head (or why it +/// brought nothing): the `addon` event's, and the scan's at start. +pub const Loaded = struct { + name: []const u8 = "", + version: u32 = 0, + abi: u32 = 0, + /// The library file's BLAKE3; zero for one never read. + hash: [32]u8 = @splat(0), + caps: Kind.Caps = .{}, + path: []const u8 = "", + /// Empty: taken; else why not ("refused: ..."). + why: []const u8 = "", + exts: []const u8 = "", + words: []const LoadedWord = &.{}, + /// Its `pa_ops` in this process; a replica's update reads none. + ops: usize = 0, +}; +pub const LoadedWord = struct { name: []const u8, leader: []const u8 = "", arg: u8 = 0, opens: u8 = 0, doc: []const u8 = "" }; + +/// `ops`'s head as a `Loaded` (its words borrowed from it, in `words`). +pub fn loadedOf(ops: *const Kind.Ops, path: []const u8, hash: [32]u8, words: *[64]LoadedWord) Loaded { + const h = &ops.head; + const head_words = if (h.nwords == 0 or h.words == null) &.{} else h.words[0..@min(h.nwords, words.len)]; + for (head_words, words[0..head_words.len]) |*w, *out| out.* = .{ + .name = if (w.name == null) "" else std.mem.span(w.name), + .leader = if (w.leader == null) "" else std.mem.span(w.leader), + .arg = w.arg, + .opens = w.opens, + .doc = if (w.doc == null) "" else std.mem.span(w.doc), + }; + return .{ + .name = std.mem.sliceTo(&h.name, 0), + .version = h.version, + .abi = h.abi, + .hash = hash, + .path = path, + .exts = if (h.exts == null) "" else std.mem.span(h.exts), + .words = words[0..head_words.len], + .ops = @intFromPtr(ops), + .caps = .{ + .tick = ops.tick != null, + .key = ops.key != null, + .mouse = ops.mouse != null, + .ctl = ops.ctl != null, + .jobs = ops.job_run != null, + .load = ops.load != null, + .search = ops.search != null, + .address = ops.address != null, + .view = ops.view != null, + .pictures = h.flags & Kind.c.PA_PICTURES != 0, + .plain = h.flags & Kind.c.PA_PLAIN != 0, + }, + }; +} + +/// This process's code for a kind a session's core lists, which a client +/// runs itself: PA_PLAIN, of the same name, version, ABI and functions (a +/// compiled-in addon's library hash is 0: never by the hash), and not +/// caught breaking its promise here (`distrust`). Null: its panes say so. +pub fn plainOps(e: *const Kind.Entry) ?*const Kind.Ops { + if (!e.caps.plain or e.refused) return null; + // A client scans for its addons only when its session has a plain kind. + _ = scan(); + for (distrusted[0..ndistrusted]) |*d| if (std.mem.eql(u8, std.mem.sliceTo(d, 0), e.nameOf())) return null; + for (found[0..nfound], 0..) |*l, i| if (l.why.len == 0 and std.mem.eql(u8, l.name, e.nameOf()) and l.version == e.version and + l.abi == e.abi and @as(u16, @bitCast(l.caps)) == @as(u16, @bitCast(e.caps))) + { + var why: Why = .{}; + if (l.ops == 0) l.ops = loadListed(i, &why) orelse return null; + return @ptrFromInt(l.ops); + }; + return null; +} + +/// Kinds a client found breaking PA_PLAIN: shown by the session's line +/// from its next join on. A process's (test/replicas.zig keeps one a client). +pub var distrusted: [Kind.max_kinds][16]u8 = undefined; +pub var ndistrusted: usize = 0; + +pub fn distrust(name: []const u8) void { + if (ndistrusted == distrusted.len) return; + distrusted[ndistrusted] = @splat(0); + @memcpy(distrusted[ndistrusted][0..@min(15, name.len)], name[0..@min(15, name.len)]); + ndistrusted += 1; +} + +/// The kind that opens `path`, by its extension (any case). +pub fn forPath(r: *const Kind.Registry, path: []const u8) ?u8 { + const dot = std.mem.lastIndexOfScalar(u8, path, '.') orelse return null; + if (std.mem.indexOfScalarPos(u8, path, dot, '/') != null) return null; + for (r.exts[0..r.nexts]) |*e| if (std.ascii.eqlIgnoreCase(e.ext.slice(), path[dot..])) return e.kind; + return null; +} + +pub fn word(r: *const Kind.Registry, name: []const u8) ?*const Kind.Word { + for (r.words[0..r.nwords]) |*w| if (std.mem.eql(u8, w.name.slice(), name)) return w; + return null; +} + +/// The word whose leader path is `keys`, or whether one has a path +/// `keys` begins (`prefix`). +pub fn leader(r: *const Kind.Registry, keys: []const u8) union(enum) { none, prefix, word: *const Kind.Word } { + var begun = false; + for (r.words[0..r.nwords]) |*w| { + if (w.leader.len == 0) continue; + if (std.mem.eql(u8, w.leader.slice(), keys)) return .{ .word = w }; + if (std.mem.startsWith(u8, w.leader.slice(), keys)) begun = true; + } + return if (begun) .prefix else .none; +} + +/// `l` taken in, its words and extensions with it, or listed refused, +/// and why: collisions with the core's names and the kinds here +/// (`except`'s own looked past, a reload's old one). +pub fn take(r: *Kind.Registry, l: Loaded, except: []const u8) ?[]const u8 { + if (r.n == Kind.max_kinds) return "refused: no room for another kind"; + const e = &r.kinds[r.n]; + e.* = .{ .version = l.version, .abi = l.abi, .hash = l.hash, .caps = l.caps }; + @memcpy(e.name[0..@min(15, l.name.len)], l.name[0..@min(15, l.name.len)]); + e.path.set(l.path); + const why = if (l.why.len > 0) l.why else r.collides(l, except, &e.why); + if (why) |w| { + e.why.set(w); + e.refused = true; + r.n += 1; + return e.why.slice(); + } + e.why.set("loaded"); + // A client's: its own code for a plain kind, or none (the session's). + // A client's code for a plain kind is loaded at its first pane (`need`). + r.ops[r.n] = if (tape.replaying()) null else @ptrFromInt(l.ops); + for (l.words) |w| { + r.words[r.nwords] = .{ .kind = r.n, .arg = w.arg, .opens = w.opens }; + r.words[r.nwords].name.set(w.name); + r.words[r.nwords].leader.set(w.leader); + r.words[r.nwords].doc.set(w.doc); + r.nwords += 1; + } + var it = std.mem.tokenizeScalar(u8, l.exts, ' '); + while (it.next()) |ext| : (r.nexts += 1) { + r.exts[r.nexts] = .{ .kind = r.n }; + r.exts[r.nexts].ext.set(ext); + } + r.n += 1; + return null; +} + +pub fn collides(r: *const Kind.Registry, l: Loaded, except: []const u8, why: *Kind.Buf(128)) ?[]const u8 { + const mine = struct { + fn of(rr: *const Kind.Registry, k: u8, ex: []const u8) bool { + return ex.len > 0 and std.mem.eql(u8, rr.kinds[k].nameOf(), ex); + } + }.of; + if (l.name.len == 0 or l.name.len > 15) return "refused: its name is empty or longer than 15 bytes"; + for (l.name) |ch| if (!std.ascii.isAlphanumeric(ch) and ch != '_' and ch != '-') return "refused: its name is not a word"; + if (r.byName(l.name)) |k| if (!mine(r, k, except)) return why.print("refused: kind {s} is here already", .{l.name}); + if (r.nwords + l.words.len > r.words.len) return "refused: no room for its words"; + for (l.words, 0..) |w, i| { + if (w.name.len == 0 or w.name.len > 32) return "refused: a word's name is empty or longer than 32 bytes"; + if (coreWord(w.name)) return why.print("refused: word {s} is the core's", .{w.name}); + if (r.word(w.name)) |o| if (!mine(r, o.kind, except)) return why.print("refused: word {s} is {s}'s", .{ w.name, r.kinds[o.kind].nameOf() }); + for (l.words[0..i]) |before| if (std.mem.eql(u8, before.name, w.name)) return why.print("refused: word {s} twice", .{w.name}); + if (w.leader.len > 8) return why.print("refused: leader SPC {s} is longer than 8 keys", .{w.leader}); + if (w.leader.len > 0) { + if (@import("keys.zig").leaderTaken(w.leader)) return why.print("refused: leader SPC {s} is taken", .{w.leader}); + for (r.words[0..r.nwords]) |o| if (o.leader.len > 0 and !mine(r, o.kind, except) and (std.mem.startsWith(u8, w.leader, o.leader.slice()) or std.mem.startsWith(u8, o.leader.slice(), w.leader))) + return why.print("refused: leader SPC {s} is taken", .{w.leader}); + } + } + var it = std.mem.tokenizeScalar(u8, l.exts, ' '); + var n: usize = 0; + while (it.next()) |ext| : (n += 1) { + if (ext.len < 2 or ext[0] != '.' or ext.len > 16) return why.print("refused: extension {s} is no .ext", .{ext}); + if (r.forPath(ext)) |o| if (!mine(r, o, except)) return why.print("refused: extension {s} is {s}'s", .{ ext, r.kinds[o].nameOf() }); + } + if (r.nexts + n > r.exts.len) return "refused: no room for its extensions"; + return null; +} + +/// Kind `k` out, its words and extensions with it. +pub fn drop(r: *Kind.Registry, k: u8) void { + var n: u8 = 0; + for (r.words[0..r.nwords]) |w| if (w.kind != k) { + r.words[n] = w; + if (w.kind > k) r.words[n].kind -= 1; + n += 1; + }; + r.nwords = n; + n = 0; + for (r.exts[0..r.nexts]) |e| if (e.kind != k) { + r.exts[n] = e; + if (e.kind > k) r.exts[n].kind -= 1; + n += 1; + }; + r.nexts = n; + std.mem.copyForwards(Kind.Entry, r.kinds[k .. r.n - 1], r.kinds[k + 1 .. r.n]); + std.mem.copyForwards(?*const Kind.Ops, r.ops[k .. r.n - 1], r.ops[k + 1 .. r.n]); + r.n -= 1; + r.ops[r.n] = null; +} + +fn coreWord(name: []const u8) bool { + const builtins = @import("builtins.zig"); + return std.meta.stringToEnum(builtins.registry.Builtin(), name) != null or builtins.absentWhy(name) != null; +} + +// ---- the core's half ---- + +/// `Addon load ` (relative to the pane's directory), `Addon unload +/// ` (refused while a pane of its kind is open), `Addon reload +/// ` (its panes kept through it, as their dumps); bare, the kinds +/// this session has. +pub fn run(p: *Pardes, id: usize, arg: ?[]const u8) void { + const a = std.mem.trim(u8, arg orelse "", " \t"); + var said: [256]u8 = undefined; + if (a.len == 0) { + var w: std.Io.Writer = .fixed(&said); + w.writeAll("addons:") catch {}; + for (p.kinds.kinds[0..p.kinds.n]) |*e| if (!e.refused) w.print(" {s} {d}", .{ e.nameOf(), e.version }) catch {}; + return p.setMessage(id, w.buffered()); + } + const takes = "Addon takes load , unload or reload "; + const sp = std.mem.indexOfAny(u8, a, " \t") orelse return p.reportFailure(id, takes); + const op = std.meta.stringToEnum(enum { load, unload, reload }, a[0..sp]) orelse return p.reportFailure(id, takes); + const rest = std.mem.trim(u8, a[sp..], " \t"); + var target: [pardes.effect_path_cap]u8 = undefined; + if (op == .load) { + const dir = std.mem.trimEnd(u8, if (p.panes[id]) |pane| Pardes.paneDir(pane) else p.sessionDir(), "/"); + const path = if (std.Io.Dir.path.isAbsolute(rest)) rest else std.fmt.bufPrint(&target, "{s}/{s}", .{ dir, rest }) catch return p.reportFailure(id, "Addon: that path is too long"); + if (path.len > pardes.effect_path_cap) return p.reportFailure(id, "Addon: that path is too long"); + return p.emit(.{ .addon = .{ .op = .load, .pane = @intCast(id), .arg = .from(path) } }); + } + const k = p.kinds.byName(rest) orelse return p.reportFailure(id, std.fmt.bufPrint(&said, "Addon {t}: no addon brought kind {s}", .{ op, rest }) catch takes); + const e = &p.kinds.kinds[k]; + if (op == .reload and std.mem.eql(u8, e.path.slice(), "builtin")) return p.reportFailure(id, std.fmt.bufPrint(&said, "Addon reload: {s} is built into this pardes", .{rest}) catch takes); + if (op == .reload) return p.emit(.{ .addon = .{ .op = .reload, .pane = @intCast(id), .arg = .from(e.path.slice()) } }); + const n = openPanes(p, rest); + if (n > 0) return p.reportFailure(id, std.fmt.bufPrint(&said, "Addon unload: {d} {s} pane{s} open", .{ n, rest, if (n == 1) " is" else "s are" }) catch takes); + const hash = e.hash; + const gone = std.fmt.bufPrint(&said, "Addon unload: {s} is gone", .{rest}) catch "Addon unload"; + p.kinds.drop(k); + release(p, hash); + p.setMessage(id, gone); +} + +fn release(p: *Pardes, hash: [32]u8) void { + p.emit(.{ .addon = .{ .op = .release, .pane = 0, .arg = .from(&hash) } }); +} + +pub fn openPanes(p: *const Pardes, name: []const u8) usize { + var n: usize = 0; + for (p.panes) |slot| if (slot) |pane| if (pane.addon) |s| if (s.live and s.is(name)) { + n += 1; + }; + return n; +} + +/// The `addon` event: what a library brought taken in (or listed +/// refused), or a reload's swap; the panes that waited for it open. +pub fn landed(p: *Pardes, op: Op, pane: u8, l: Loaded) void { + const id = if (pane < p.panes.len and p.panes[pane] != null) pane else p.view().active; + var said: [320]u8 = undefined; + const base = std.Io.Dir.path.basename(l.path); + if (op == .reload) { + var why: Why = .{}; + if (!swap(p, l, &why)) { + if (l.why.len == 0) release(p, l.hash); + return p.reportFailure(id, std.fmt.bufPrint(&said, "Addon reload: {s}: {s}", .{ base, why.slice() }) catch "Addon reload: refused"); + } + const n = openPanes(p, l.name); + return p.setMessage(id, std.fmt.bufPrint(&said, "Addon reload: {s} {d}, {d} pane{s}", .{ l.name, l.version, n, if (n == 1) "" else "s" }) catch "Addon reload"); + } + // A load of a path refused before tries it afresh. + var i: u8 = 0; + while (i < p.kinds.n) { + if (p.kinds.kinds[i].refused and std.mem.eql(u8, p.kinds.kinds[i].path.slice(), l.path)) p.kinds.drop(i) else i += 1; + } + if (p.kinds.take(l, "")) |why| { + if (l.why.len == 0) release(p, l.hash); + return p.reportFailure(id, std.fmt.bufPrint(&said, "Addon load: {s}: {s}", .{ base, why }) catch "Addon load: refused"); + } + const n = reopenWaiting(p, l.name); + p.setMessage(id, std.fmt.bufPrint(&said, "Addon load: {s} {d}{s}", .{ l.name, l.version, if (n > 0) ", its panes back" else "" }) catch "Addon load"); +} + +/// The panes of kind `name` that waited for it (placeholders), opened. +pub fn reopenWaiting(p: *Pardes, name: []const u8) usize { + if (p.kinds.byName(name) == null) return 0; + var n: usize = 0; + // Each from its file, as a Restore's (a file not here: it waits on). + for (p.panes, 0..) |slot, id| if (slot) |pane| if (pane.addon) |s| if (!s.live and s.is(name)) { + dump.reviveSlot(p, id, s); + n += @intFromBool(s.live); + }; + return n; +} + +/// `Addon reload`: the kind at `l.path` replaced by what `l` brought, or +/// why not, nothing changed. Everything is checked first: its head (the +/// host's), its collisions (its old self looked past), its name the same, +/// and every open pane of the kind opened anew in a fresh arena from the +/// old state's record (its `load`, else its place and its words); only +/// then are the old states closed and the panes swapped, at a new +/// generation (a job the old one asked for is dropped as it answers). +fn swap(p: *Pardes, l: Loaded, why: *Why) bool { + const k = for (p.kinds.kinds[0..p.kinds.n], 0..) |*e, i| { + if (!e.refused and std.mem.eql(u8, e.path.slice(), l.path)) break @as(u8, @intCast(i)); + } else return fail(why, "no addon was loaded from it", .{}); + const name = p.kinds.kinds[k].nameOf(); + if (l.why.len > 0) return fail(why, "{s}", .{l.why}); + var scratch: Kind.Buf(128) = .{}; + if (p.kinds.collides(l, name, &scratch)) |r| return fail(why, "{s}", .{r}); + if (!std.mem.eql(u8, l.name, name)) return fail(why, "refused: its kind is now {s}, not {s}", .{ l.name, name }); + const old_ops = p.kinds.ops[k]; + const new_ops: ?*const Kind.Ops = if (tape.replaying()) null else @ptrFromInt(l.ops); + const gen = p.kinds.kinds[k].gen +% 1; + var fresh: [pardes.MAX_PANES]?*Kind.Slot = @splat(null); + defer for (fresh) |f| if (f) |s| { + const gpa = s.gpa; + s.free(); + gpa.destroy(s); + }; + for (p.panes, 0..) |pslot, id| if (pslot) |pane| if (pane.addon) |s| if (s.live and s.is(name)) { + const n = s.gpa.create(Kind.Slot) catch return fail(why, "out of memory", .{}); + // load receives the record's version, not the replacement library's. + n.* = .{ .ops = new_ops, .live = true, .gpa = s.gpa, .arena = .init(s.gpa), .name = s.name, .version = s.version, .gen = gen, .art = s.art, .values = s.values }; + fresh[id] = n; + n.path = s.gpa.dupe(u8, s.path) catch return fail(why, "out of memory", .{}); + // The old state's record, by the old library. + if (old_ops) |o| { + var place: [32]u8 = undefined; + n.where_text.set(Kind.where(s, &place)); + if (o.dump != null and l.caps.load) n.record = dump.slotRecord(s) catch return fail(why, "out of memory", .{}); + } + // From its file as it is, the record its small state. + const bytes: ?[]u8 = if (new_ops != null and n.path.len > 0) @import("fs.zig").read(p, n.path) catch null else null; + defer if (bytes) |b| p.gpa.free(b); + if (!Kind.reopen(p, id, n, bytes)) return fail(why, "refused: a pane of it would not open again: {s}", .{n.refused.slice()}); + }; + // Every pane opened: the old states go, the new take their slots. + for (p.panes, 0..) |pslot, id| if (pslot) |pane| if (pane.addon) |s| if (fresh[id]) |n| { + if (old_ops) |o| if (o.close) |f| f(s.st); + for (&s.jobs) |*j| if (j.*) |job| { + s.gpa.free(job.in); + j.* = null; + }; + s.arena.deinit(); + s.arena = n.arena; + n.arena = .init(n.gpa); + if (@import("builtin").mode == .debug) std.mem.swap(@TypeOf(s.used), &s.used, &n.used); + s.st = n.st; + s.ops = new_ops; + s.version = l.version; + s.gen = gen; + s.next_tick = n.next_tick; + n.st = null; + }; + const old_hash = p.kinds.kinds[k].hash; + p.kinds.drop(k); + _ = p.kinds.take(l, ""); + p.kinds.kinds[p.kinds.n - 1].gen = gen; + release(p, old_hash); + return true; +} + +fn fail(why: *Why, comptime fmt: []const u8, args: anytype) bool { + _ = why.print(fmt, args); + return false; +} + +/// /addons: ` +/// loaded|refused: why` a line (`-` for no path, no hash). +pub fn list(p: *const Pardes, out: *std.ArrayList(u8), gpa: std.mem.Allocator) !void { + for (p.kinds.kinds[0..p.kinds.n]) |*e| { + const name = if (e.nameOf().len > 0) e.nameOf() else std.Io.Dir.path.basename(e.path.slice()); + try out.print(gpa, "{s} {d} {d}.{d} {d} {s} ", .{ name, e.version, e.abi >> 16, e.abi & 0xffff, if (e.refused) 0 else openPanes(p, name), if (e.path.len > 0) e.path.slice() else "-" }); + if (std.mem.allEqual(u8, &e.hash, 0)) try out.append(gpa, '-') else try out.print(gpa, "{x}", .{e.hash}); + try out.print(gpa, " {s}\n", .{e.why.slice()}); + } +} + +test "addon reload gives migration the old record version and saves the new version" { + const Fake = struct { + var state: u8 = 7; + fn open(_: [*c]const u8, _: [*c]const u8, _: u64) callconv(.c) ?*anyopaque { + return &state; + } + fn paint(_: ?*anyopaque, _: [*c]const Kind.c.pa_window, _: [*c]Kind.c.pa_paint) callconv(.c) void {} + fn save(_: ?*anyopaque, out: [*c]u8, n: u32) callconv(.c) u32 { + if (n > 0) out[0] = state; + return 1; + } + fn load(_: ?*anyopaque, bytes: [*c]const u8, n: u32, version: u32) callconv(.c) c_int { + return if (version == 1 and n == 1 and bytes[0] == 7) Kind.c.PA_OK else 1; + } + }; + const p = try @import("ninep/testing.zig").withFile(std.testing.allocator, "x\n"); + defer p.deinit(); + var old = std.mem.zeroInit(Kind.Ops, .{ .head = std.mem.zeroInit(Kind.c.pa_head, .{ .size = @sizeOf(Kind.c.pa_head), .abi = Kind.abi, .version = 1 }), .open = Fake.open, .paint = Fake.paint, .dump = Fake.save, .load = Fake.load }); + @memcpy(old.head.name[0..7], "migrate"); + var words: [64]LoadedWord = undefined; + try std.testing.expect(p.kinds.take(loadedOf(&old, "test.so", @splat(0), &words), "") == null); + const pane = try Kind.open(p, 1, p.kinds.byName("migrate").?, null, null); + var new = old; + new.head.version = 2; + var why: Why = .{}; + try std.testing.expect(swap(p, loadedOf(&new, "test.so", @splat(1), &words), &why)); + try std.testing.expectEqual(@as(u32, 2), pane.addon.?.version); + try std.testing.expectEqual(@as(u32, 2), p.kinds.kinds[p.kinds.byName("migrate").?].version); + // A saved v1 placeholder revived by v2 must also save future records as v2. + const slot = pane.addon.?; + slot.version = 1; + slot.live = false; + slot.record = try slot.gpa.dupe(u8, &.{7}); + dump.reviveSlot(p, 1, slot); + try std.testing.expect(slot.live); + try std.testing.expectEqual(@as(u32, 2), slot.version); +} + +test "a load that never finished is skipped at the next start, and every start after, until a load asks for it" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + var buf: [4096]u8 = undefined; + const data = buf[0..try tmp.dir.realPath(std.testing.io, &buf)]; + try tmp.dir.createDirPath(std.testing.io, "addons"); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "addons/crashy.so", .data = "not a library" }); + var lib_buf: [4096]u8 = undefined; + const lib = try std.fmt.bufPrint(&lib_buf, "{s}/addons/crashy.so", .{data}); + // The mark a load leaves when its session dies in it. + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "addon-loading", .data = lib }); + var dir_buf: [4096]u8 = undefined; + const dir = try std.fmt.bufPrint(&dir_buf, "{s}/addons", .{data}); + for (0..2) |_| { + nfound = 0; + scanDir(data, dir, crashedPath(data).slice()); + try std.testing.expectEqualStrings(lib, found[0].path); + try std.testing.expect(std.mem.startsWith(u8, found[0].why, "skipped: it crashed its last load")); + } + nfound = 0; + // Asked for by name, it is tried again (and refused here for what it is). + var words: [64]LoadedWord = undefined; + var why: Why = .{}; + const l = read(data, lib, &words, &why); + try std.testing.expect(l.why.len > 0 and std.mem.indexOf(u8, l.why, "skipped") == null); + try std.testing.expectError(error.FileNotFound, tmp.dir.access(std.testing.io, "addon-crashed", .{})); + try std.testing.expectError(error.FileNotFound, tmp.dir.access(std.testing.io, "addon-loading", .{})); +} diff --git a/core/CHANGELOG.md b/core/CHANGELOG.md new file mode 100644 index 00000000..5fcfd378 --- /dev/null +++ b/core/CHANGELOG.md @@ -0,0 +1,1357 @@ +# Changelog + +## Unreleased + +- `pardes` has commands, and they describe themselves: `ps`, `attach`, + `kill` and `new` for sessions, `do WORD` and `words` for the builtins, + `fs ls|read|write|ask|follow|stat|help|complete PATH` for the tree (a + refusal in the session's own words, where a mount says EINVAL), and + `help`, `completion bash|fish|zsh`, `skill` and `version`. `pardes help` + is a map of them; `help CMD` ends in examples; `help WORD` and `help + PATH` print what the session's /commands and /README say, and with no + session running, what a core made in the command's own process says. + A command acts on `-s NAME`, else `$PARDES_9P`, else the only session. + `pardes FILE` and every flag work as before; `--ps`, `--attach`, + `--kill`, `--kill-all`, `--help` and `--version` are the commands' old + spellings, for a release. A file named like a command opens as `./ps`. + The reference's "The pardes command" is `pardes help --all`, written by + `zig build builtins-json -Dplatform=tui`, and a test says when it is + stale. + +- The text pane is a library too, `pardes-editor` (`src/lib_editor.zig`, + docs/next-editor.md): helix's keys on a text kept in memory, for a + program that draws it itself (a sketch with a language of its own). It + is a value of plain data, every capacity in its type, copied anywhere + and outliving the code that made it; each call runs pardes's own + normal.zig and edit.zig on it, built with no pane and no core, and hands + back what is not the text's (a view, `:`, `/`, `s`, a language server, a + macro) to its caller. A grammar can be handed in at run time beside the + compiled-in ones. A package asks for it with `.editor = true`; one that + asks for `pardes-syntax` alone fetches what it did. `zig build hxlib` + runs helix's cases on it, against the goldens and against the file pane. + Nothing a session does changes, and a session dumped before loads after. + The caller can hand it the parse it keeps (`Syntax`), and helix's + tree-sitter walks (Alt-o, Alt-i, Alt-n, Alt-p and kin, `]f`, `mif`) run + in the library on pardes's own functions, Alt-i going back the way + Alt-o came; `replace` rewrites a byte range as one step to undo, the + ranges following, and a drag that rewrites a number every frame can + join its frames into one. A new line's indent comes from that parse + where its grammar has an indent query (a `Language`'s `indents`), as a + file's does from its own, so a language whose lines end in `)` is not + pushed a level in at every line. +- The terminal is a module of its own (`src/term/`: the emulator, its + screen state for replicas, kitty images, the pager's parse), which never + imports the core and is reached through one table of plain-data + entries, `src/Term.zig` the core's side (docs/next-terminal.md). Editing + a terminal's text is the core's, on a copy of its rows, as before, and + predicted echo stays the core's; nothing a session does changes, and a + session dumped before loads after. `zig build core-test` builds the core + without the terminal (no ghostty, no C++): a shell boot is a scratch + pane there and a test that needs a terminal skips; `zig build + shell-test` runs the same tests with it. + +- A diff paged (`git show | pardes -`) is a plumbing message, as acme's + `plumb -i` sends what it shows: `type text/x-diff` to `edit`, which a + default rule makes `showdiff`, its +Pager stacked in a +Diff as before. + Take the rule out and the +Pager stays as text; a program reading + `plumb/edit` sees the diff. The `edit` port shows any message's data + with `action=showdata` (stacked with `showdiff`). + +- A port the plumbing rules name is a file a program reads, + `plumb/`, as Plan 9's plumber serves one: every open reads its + own copy of what is sent there from then on (`cat plumb/edit` sees each + file a look opens), a read held until a message comes. A message to a + port no one reads runs its rule set's `start`, or with `plumb client` + waits for the port's first reader. + +- A look asks the plumber first, as acme's does: once the pane's own + readings are done and a name already open in a pane is focused, what + is looked at is sent (`type text`, `src pardes`, `wdir` the pane's + directory), and only what no rule set takes is Look's own. The default + rules keep every look as it was: the browser for a URL, a bare `.diff` + or `.patch` stacked in a +Diff, `git show` of a commit hash in a git + work tree are now rules of `src/plumbing`, no longer written into + Look. `Diff` takes a file or a revision (`Diff fix.patch`, `Diff + 3f2a9c1`), and a rule's Diff that cannot declines, so the look goes + on. `Plumb` opens your plumbing file (`/plumbing`, `include + basic` until you save one), read at start and applied when saved; the + one-line `Plumb suffix .svg to image` rules are gone, refused in words + (write a rule set: `data matches '.*\.svg'`, `arg isfile $data`, + `plumb to image`). A program a rule starts runs detached, as Plan 9's + plumber starts one, with no pane; a failure to start it is said. + +- A search with a class whose range ends at `\xff` (`[\x80-\xff]`, in an + address, `s`, a plumbing rule) no longer takes the editor down: mvzr + counted the range past a byte's end. + +- The plumber, Plan 9's, is in the session: `plumb/rules` holds its + rules in plumb(6)'s language (rule sets of `object verb arg` lines, + variables, `include`, `matches` over the whole text or around a click, + `isfile` and `isdir` finding a name as a look does), and `plumb/send` + takes a message and sends it where they say, to a port pardes reads + (`edit`, `web`, a loaded kind) or to a word or command started, with + Plan 9's words when it cannot. A session starts with `include basic`, + pardes's rules (`src/plumbing`). Kept in a dump. `$1`-`$9` are refused, + the regular expressions having no submatches. + +- A query pane's row looked at on its address (its gutter) opens the + row's place however the look arrives, as a click there always did: an + empty look written back to the pane's `event` file, and the whole row + written to its `look` file, now take the address the row holds, not a + word of its text. A diff's lines answer all three ways through the same + one function (`look.rowAddressAt`). + +- Each pane's directory over 9P has a `rows` file: a line a body row of a + results pane (+Search, +Callstack, +Diff, a language server's list), + where it points (`path`, line, column, the address's end, where its code + starts) and how it is coloured (runs of `keyword`, `string`, ... and the + grounds `add`, `del`, `emph`, `line`), so a 9P client reads their + addresses as data, not by parsing the text. A write at offset 0 replaces + a pane's rows, a later one adds to them; rows its text cannot hold are + refused. An edit drops them, as before. The editor's own producers set + rows the same one way. + +- A drag draws no guide lines: no dashes where a pane or column would + land, no rail for a carried column, no dashed seam under a pane rule + held. The line between two columns no longer resizes them (nor, at a + pane's corner, the panes too): a press there is a press in the pane. + A column resizes by its box, left of its tag; a pane by its own box, + or in a window by the rule above it. + +- An edit to the core recompiles it about 2 s sooner again (`core-test`, + 16 % fewer of the compiler's instructions, 4.4 MB less code in a Debug + test): a load's note on a field (another build's dump) is written from + the type's and the field's names at run time, where every type and + field had its own copy of the code that writes one; one byte dump's + reader serves a session, a pane and the rest, where each had its own; + and what tells a replica's build from another's (`wire.semantics`) + joins its two long parts on, and the tape's shape writes its numbers + itself and copies each line it builds once, where it formatted them + and copied the line again for each piece, at every compile. + +- An edit to the core recompiles it about 3 s sooner (`core-test`, 13 s to + 10 s): the build keeps each grammar's highlight patterns that pardes + colours once, where every compile of every program that takes the core + did it again, a quarter of its time. A package that depends on pardes + is given the same filtered queries. + +- The 256-colour palette Petscii art and the window draw a palette colour + with is pardes's own (`colors.xterm`), ghostty-vt's default to the entry. + +- Commit patches open as `+Diff-` and reuse only their own + commit's pane, so another paged patch no longer replaces one already open. + +- zstbi is upstream's again (zig-gamedev/zstbi, its main): the fork pinned + for Zig 0.17 was deleted, and a fresh checkout could not fetch it. + +- pardes's syntax is a library: a Zig package that depends on pardes is + given the `pardes-syntax` module (`src/lib_syntax.zig`), with the + grammars, highlighting, declarations, syntax tree and themes as the + editor has them, for a program that draws source text itself. Every + dependency but tree-sitter's runtime is lazy in the manifest, so such a + package fetches the runtime and the grammars of its `tree-sitter` set + and nothing else; `zig build lib-test`, which `unit-test` runs, builds + one (`test/lib`). The markdown grammar is pinned by its commit: its + branch has moved, and a fresh checkout could not fetch it. + +- Enter, `o` and `O` give the new line helix's indent where the file's + grammar has an indent query (bash, C, C#, C++, Clojure, CSS, Elixir, + Fortran, Go, HTML, Java, JavaScript, JSON, Kotlin, OCaml, PHP, Python, + Ruby, Rust, Scala, Zig), from the file's syntax tree; elsewhere the + line's indent as before. Enter between a pair (`()`, `[]`, `{}`, quotes) + puts the closer on a line of its own and the cursor on one a level in. + A closer typed on its own line stays where it is typed, as in helix. + +- A session's end (`Exit`, the last pane's `Del`, its terminal or window + gone) leaves a slim dump in the state file: its panes with their files, + directories and places, not a clean file's text, an undo history or a + terminal's screen. `History` (`SPC f h`) lists the state file's dumps in a + `+History` pane, each as the command that brings it back, `Restore +