summary refs log tree commit diff
path: root/src/main.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.zig')
-rw-r--r--src/main.zig117
1 files changed, 54 insertions, 63 deletions
diff --git a/src/main.zig b/src/main.zig
index f34d878..d26cefa 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -14,6 +14,7 @@ const ns_per_s = std.time.ns_per_s;
 const smp_allocator = std.heap.smp_allocator;
 const std = @import("std");
 
+const Graphemes = vaxis.Graphemes;
 const Key = vaxis.Key;
 const Loop = vaxis.Loop;
 const Style = vaxis.Cell.Style;
@@ -24,71 +25,65 @@ const Winsize = vaxis.Winsize;
 const gwidth = vaxis.gwidth.gwidth;
 const Vaxis = vaxis.Vaxis;
 const vaxis = @import("vaxis");
-
-const Parser = tree_sitter.Parser;
-const tree_sitter = @import("tree-sitter");
+pub const panic = vaxis.panic_handler;
 
 const zsanett = @import("zsanett");
 pub fn zsanettIntern(T: type) bool {
     return switch (T) {
-        Environment, Key, Loop(Event) => true,
+        Buffer, Environment, Key, Loop(Event),
+        Selection, Selection.Unit => true,
         else => false,
     };
 }
 
+const Buffer = @import("Buffer.zig");
 const Config = @import("Config.zig");
-const selection = @import("selection.zig");
-const languages = @import("languages");
+const Selection = @import("Selection.zig");
+
+pub var graphemes: Graphemes = undefined;
 
 const Event = union(enum) {
     key_press: Key,
     winsize: Winsize,
+    paste: []const u8,
 };
 
 const Environment = struct {
     allocator: Allocator,
     loop: *Loop(Event),
-    parser: *Parser,
-    segment: *selection.Segment,
-    text: []const u8,
     tty: *Tty,
     vaxis: *Vaxis,
 
-    pub fn nextEvent(self: Environment) !Event {
-        while (true) {
-            const event = self.loop.nextEvent();
-            switch (event) {
-                .key_press => return event,
-                .winsize => |ws| {
-                    try self.vaxis.resize(self.allocator,
-                                          self.tty.anyWriter(), ws);
-                    try self.render();
-                },
-            }
-        }
+    pub fn nextEvent(self: Environment) Event {
+        return self.loop.nextEvent();
     }
 
-    pub fn render(self: Environment) !void {
+    pub fn render(self: Environment, buffer: Buffer) !void {
         const window = self.vaxis.window();
         window.clear();
         var col: u16 = 0;
         var row: u16 = 0;
-        var graphemes = self.vaxis.unicode.graphemeIterator(self.text);
-        while (graphemes.next()) |grapheme| {
+        var graphemes_iter = buffer.iterate();
+        while (graphemes_iter.next()) |grapheme| {
+            const in_head = buffer.selection.inHead(grapheme);
+            const in_body = buffer.selection.inBody(grapheme);
             const style = Style {
-                .reverse = self.segment.contains(grapheme),
+                .reverse = in_head,
+                .ul_style = if (in_body) .single else .off,
             };
-            const bytes = grapheme.bytes(self.text);
+            const bytes = buffer.bytes(grapheme);
             if (eql(u8, bytes, "\n")) {
-                const width = window.gwidth("$");
-                defer col = 0;
-                defer row += 1;
-                window.writeCell(col, row, .{
-                    .char = .{ .grapheme = "$", .width = @intCast(width) },
-                    .style = style,
-                });
+                while (col < window.width) : (col += 1)
+                    window.writeCell(col, row, .{
+                        .char = .{ .grapheme = " " },
+                        .style = style,
+                    });
+                col = 0;
+                row += 1;
+                if (row >= window.height)
+                    break;
             } else if (eql(u8, bytes, "\t")) {
-                var tab = self.vaxis.unicode.graphemeIterator("  ");
+                var tab = graphemes.iterator("  ");
                 while (tab.next()) |g| {
                     const b = g.bytes("  ");
                     const width = window.gwidth(b);
@@ -110,20 +105,23 @@ const Environment = struct {
         try self.vaxis.render(self.tty.anyWriter());
     }
 
-    pub fn goUp(self: Environment) void {
-        self.segment.goUp();
+    pub fn resize(env: Environment, ws: Winsize) !void {
+        try env.vaxis.resize(env.allocator, env.tty.anyWriter(), ws);
     }
 
-    pub fn goRight(self: Environment) void {
-        self.segment.goRight(self.vaxis.unicode.width_data.graphemes, self.text);
+    pub fn yank(env: Environment, buffer: Buffer) !void {
+        try env.vaxis.copyToSystemClipboard(env.tty.anyWriter(),
+                                            buffer.selectedBytes(),
+                                            env.allocator);
     }
 
-    pub fn goLeft(self: Environment) void {
-        self.segment.goLeft(self.vaxis.unicode.width_data.graphemes, self.text);
+    pub fn requestPaste(env: Environment) !void {
+        try env.vaxis.requestSystemClipboard(env.tty.anyWriter());
     }
 
-    pub fn goDown(self: Environment) void {
-        self.segment.goDown(self.vaxis.unicode.width_data.graphemes, self.text);
+    pub fn paste(env: Environment, buffer: Buffer, data: []const u8) Buffer {
+        defer env.vaxis.opts.system_clipboard_allocator.?.free(data);
+        return buffer.paste(data);
     }
 };
 
@@ -137,45 +135,38 @@ pub fn main() !void {
 
     var tty = try Tty.init();
     defer tty.deinit();
-    var vx = try vaxis.init(allocator, .{});
+    var vx = try vaxis.init(allocator, .{
+        .system_clipboard_allocator = allocator,
+    });
     defer vx.deinit(allocator, tty.anyWriter());
+    graphemes = vx.unicode.width_data.graphemes;
+    defer graphemes = undefined;
 
     var loop = Loop(Event){ .tty = &tty, .vaxis = &vx };
     try loop.init();
     try loop.start();
     defer loop.stop();
 
-    const parser = Parser.create();
-    defer parser.destroy();
-
     const args = try argsAlloc(allocator);
     defer argsFree(allocator, args);
-    const text = try cwd().readFileAlloc(allocator, args[1], maxInt(u32));
-    defer allocator.free(text);
     try vx.enterAltScreen(tty.anyWriter());
     try vx.queryTerminal(tty.anyWriter(), 1 * ns_per_s); // for alt screen
 
-    const language = languages.c();
-    defer language.destroy();
-    try parser.setLanguage(language);
-    const tree = parser.parseString(text, null).?;
-    defer tree.destroy();
-
     zsanett.init();
     defer zsanett.deinit();
-    var segment = selection.Segment{
-        .head = .{ .node = tree.rootNode() },
-        .tail = .{ .node = tree.rootNode() },
-    };
-    try zsanett.def("env", Environment{
+    try zsanett.def("kay/path", args[1], "Path to file.");
+    try zsanett.def("kay/env", Environment{
         .allocator = allocator,
         .loop = &loop,
-        .parser = parser,
-        .segment = &segment,
-        .text = text,
         .tty = &tty,
         .vaxis = &vx,
-    }, "eval environment");
+    }, "Eval environment.");
+    try zsanett.defn(Buffer.open, .{
+        .prefix = "kay",
+        .name = "open",
+        .documentation = "(kay/open text)\n\nOpen text for editing.",
+        .source = @src(),
+    });
     switch (try zsanett.eval(void, @embedFile("main.janet"), "main.janet")) {
         .ret => {},
         .err => |err| @panic(err),