From 72e3bab78fc96bc9e48e85e6b046c492e277f27e Mon Sep 17 00:00:00 2001 From: cqc Date: Thu, 16 Sep 2021 10:42:13 -0500 Subject: [PATCH] pressing e lets you edit the file, but it doesn't save it yet --- main.ml | 88 +++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 63 insertions(+), 25 deletions(-) diff --git a/main.ml b/main.ml index 95ac742..249c46b 100644 --- a/main.ml +++ b/main.ml @@ -838,7 +838,9 @@ module Panel = struct @@ empty type textedit = - {ze: unit Zed_edit.t; zc: Zed_cursor.t; keybind: Input.Bind.state} + { mutable ze: unit Zed_edit.t + ; mutable zc: Zed_cursor.t + ; mutable keybind: Input.Bind.state } let make_textedit ?(keybinds = textedit_bindings) () = let z = Zed_edit.create () in @@ -846,6 +848,11 @@ module Panel = struct ; zc= Zed_edit.new_cursor z ; keybind= Input.Bind.init keybinds } + let clear_textedit ?(keybinds = textedit_bindings) te = + te.ze <- Zed_edit.create () ; + te.zc <- Zed_edit.new_cursor te.ze ; + te.keybind <- Input.Bind.init keybinds + let str_of_textedit (te : textedit) = Zed_string.to_utf8 (Zed_rope.to_string (Zed_edit.text te.ze)) @@ -1024,22 +1031,52 @@ module Store = struct let editor ?(branch = "current") storepath : Panel.t = let sv = make_storeview storepath branch in - let keybinds = - let open CamomileLibrary in + let globalbinds = + let open Input.Bind in + add [([], Char 'e')] + [Custom (fun () -> sv.editmode <- not sv.editmode)] + empty in + let navbinds = let open Input.Bind in add [([], Char 'n')] [Custom (navigate sv `Next)] @@ add [([], Char 'p')] [Custom (navigate sv `Prev)] @@ add [([], Char 'w')] [Custom (navigate sv `Prev)] @@ add [([], Char 's')] [Custom (navigate sv `Next)] @@ add [([], Char 'd')] [Custom (navigate sv `Sub)] - @@ add [([], Char 'a')] [Custom (navigate sv `Sup)] empty in - let bindstate = Input.Bind.init keybinds in + @@ add [([], Char 'a')] [Custom (navigate sv `Sup)] globalbinds + in + let editbinds = + let open Input.Bind in + add [([], Char 'e')] + [Custom (fun () -> sv.editmode <- not sv.editmode)] + Panel.textedit_bindings in + let bindstate = Input.Bind.init navbinds in + let te = Panel.make_textedit () in { act= (fun panel events -> + if sv.editmode then bindstate.bindings <- editbinds + else bindstate.bindings <- navbinds ; List.iter Input.Bind.(function Custom f -> f () | _ -> ()) (Event.actions_of_events bindstate events) ; - (Panel.vbox panel.subpanels).act panel events ) + if not sv.editmode then ( + let contents = + Lwt_main.run + ( Istore.get_tree sv.store sv.view + >>= fun t -> + Istore.Tree.kind t sv.selection + >>= function + | Some a -> ( + match a with + | `Contents -> Istore.Tree.get t sv.selection + | `Node -> Lwt.return "Node..." ) + | None -> Lwt.return "Invalid Selection..." ) in + Panel.clear_textedit te ; + let zctx = Zed_edit.context te.ze te.zc in + Zed_edit.insert zctx + (Zed_rope.of_string (Zed_string.of_utf8 contents)) ) ; + (Panel.vbox panel.subpanels).act panel + (if sv.editmode then events else []) ) ; subpanels= [ Panel.hbox [ Panel.prettyprint (fun pp -> @@ -1050,15 +1087,15 @@ module Store = struct (fun _i (step, node) -> Format.pp_open_vbox pp 0 ; Format.pp_open_hbox pp () ; - for _ = 0 to !indent do + for _ = 1 to !indent do Format.pp_print_space pp () done ; if sel = [step] then Format.pp_open_stag pp Display.( - Panel.Color_bg + Panel.Cursor (Wall.Color.v 0.99 0.99 0.125 0.3)) ; - Format.fprintf pp "%d-%s@." !indent step ; + Format.fprintf pp "%s@." step ; if sel = [step] then Format.pp_close_stag pp () ; Format.pp_close_box pp () ; @@ -1074,22 +1111,23 @@ module Store = struct let root = Lwt_main.run (Istore.get_tree sv.store sv.view) in - draw_levels root sv.selection ) - ; Panel.prettyprint (fun pp -> - let contents = - Lwt_main.run - ( Istore.get_tree sv.store sv.view - >>= fun t -> - Istore.Tree.kind t sv.selection - >>= function - | Some a -> ( - match a with - | `Contents -> Istore.Tree.get t sv.selection - | `Node -> Lwt.return "Node..." ) - | None -> Lwt.return "Invalid Selection..." ) - in - Format.fprintf pp "%s @." contents ) ] - ; Panel.bindingstate bindstate ] + draw_levels root sv.selection ); Panel.textedit te + (*; Panel.prettyprint (fun pp -> + let contents = + Lwt_main.run + ( Istore.get_tree sv.store sv.view + >>= fun t -> + Istore.Tree.kind t sv.selection + >>= function + | Some a -> ( + match a with + | `Contents -> Istore.Tree.get t sv.selection + | `Node -> Lwt.return "Node..." ) + | None -> Lwt.return "Invalid Selection..." ) + in Format.fprintf pp "%s @." contents ) *) ] + ; Panel.bindingstate bindstate + ; Panel.prettyprint (fun pp -> + Format.fprintf pp "sv.editmode = %b @." sv.editmode ) ] ; tag= "store-editor" } end