From 235f74aad290186785bf4c66aec805bb4577f7eb Mon Sep 17 00:00:00 2001 From: Federico Igne Date: Thu, 11 Jan 2024 20:33:01 +0100 Subject: fix: open an empty buffer if requested file does not exist. --- lib/editorBuffer.ml | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'lib/editorBuffer.ml') diff --git a/lib/editorBuffer.ml b/lib/editorBuffer.ml index 5104549..715855c 100644 --- a/lib/editorBuffer.ml +++ b/lib/editorBuffer.ml @@ -1,4 +1,5 @@ open Base +open Stdio open Zipper open Util @@ -48,9 +49,19 @@ module Action = struct end let from_file f = - let lines = Stdio.In_channel.read_lines f in - let line_to_zipper l = String.to_list l |> Sequence.of_list |> of_seq in - let content = Sequence.(of_list lines |> map ~f:line_to_zipper) |> of_seq in + let lines = + try + let fd = Unix.(openfile f [ O_RDONLY ] 0o640) in + let ic = Unix.in_channel_of_descr fd in + let lines = In_channel.input_lines ic in + In_channel.close ic; + lines + with Unix.Unix_error (ENOENT, _, _) -> [] + in + let content = + let line_to_zipper l = String.to_list l |> Sequence.of_list |> of_seq in + Sequence.(of_list lines |> map ~f:line_to_zipper) |> of_seq + in { kind = File f; content = Ok content } let cursor b = -- cgit v1.2.3