pub const Buffer = @import("Buffer.zig"); pub const c = @import("c.zig"); pub const conf = @import("es-config"); pub const Config = @import("Config.zig"); pub const Editor = @import("Editor.zig"); pub const files = @import("files.zig"); pub const Highlight = @import("highlight.zig").Highlight; pub const Key = @import("key.zig").Key; pub const KeyMap = @import("KeyMap.zig"); pub const KeyReader = @import("KeyReader.zig"); pub const RawMode = @import("RawMode.zig"); pub const Row = @import("Row.zig"); pub const search = @import("search.zig").search; pub const StringBuilder = @import("StringBuilder.zig"); pub const Syntax = @import("Syntax.zig"); const builtin = @import("builtin"); const std = @import("std"); const Allocator = std.mem.Allocator; const File = std.fs.File; const GPA = std.heap.GeneralPurposeAllocator(.{}); var log_file: ?File = null; // TODO[zigbug]: Why isn't getpid defined for Darwin? const getpid = switch (builtin.os.tag) { .macos, .ios, .tvos, .watchos => c.getpid, else => std.os.system.getpid, }; pub fn log( comptime level: std.log.Level, comptime scope: @TypeOf(.EnumLiteral), comptime format: []const u8, args: anytype, ) void { const file = if (log_file) |f| f else std.io.getStdErr(); const writer = file.writer(); nosuspend writer.print( "[" ++ @tagName(scope) ++ ":" ++ level.asText() ++ "] " ++ format ++ "\n", args, ) catch return; } pub fn main() !void { var gpa = GPA{}; defer _ = gpa.deinit(); const allocator = gpa.allocator(); const log_file_name = try std.fmt.allocPrint( allocator, "/tmp/es.{}.log", .{getpid()}, ); defer { std.log.info("Logs in {s}", .{log_file_name}); allocator.free(log_file_name); } log_file = try std.fs.createFileAbsolute( log_file_name, .{ .exclusive = true, .truncate = true }, ); defer { log_file.?.close(); log_file = null; } const raw_mode = try RawMode.init(); defer raw_mode.deinit(); var editor = try Editor.init(allocator); defer editor.deinit(); try processCommandLineArgs(allocator, &editor); try editor.setStatusMessage("C-x C-s = Save | C-x C-c = Quit | C-s = Search", .{}); while (!editor.should_exit) { try editor.refreshScreen(); try editor.processKeypress(); } } fn processCommandLineArgs(allocator: Allocator, editor: *Editor) !void { const args = try std.process.argsAlloc(allocator); defer std.process.argsFree(allocator, args); if (args.len <= 1) { // Do nothing } else if (args.len == 2) { try editor.open(args[1]); } else { std.log.err("What am I to do with {} arguments?", .{args.len}); return error.CommandLineArgs; } }