2 Commits

Author SHA1 Message Date
cqc
79af294f51 store editor tree navigation works???? 2021-09-13 16:02:14 -05:00
cqc
5d96ed12d2 refactored all keybindings 2021-09-03 09:24:24 -05:00
2 changed files with 174 additions and 159 deletions

14
irc.ml
View File

@ -5,6 +5,10 @@ we need to design this somehow before implementing it
really the graphical drawing / window management funcitons i think at this point. really the graphical drawing / window management funcitons i think at this point.
features:
- message drafts? more like, if you send too many messages to someone all at once it will hold them so you can respond later and not flood people.......
- i mean really what you want is an editable stream, so you can stage messages for later
- because i mean, if this is a bicycle, and you can make it however you want, you can just fuck with the conversation thread with computer assistance instaed of just relying on your memory.
*) *)
@ -34,7 +38,8 @@ let callback connection result =
>>= fun () -> >>= fun () ->
Lwt_io.flush Lwt_io.stdout Lwt_io.flush Lwt_io.stdout
>>= fun () -> >>= fun () ->
C.send_privmsg ~connection ~target:"cqc" ~message:("ack: " ^ data) C.send_privmsg ~connection ~target:"cqc"
~message:("ack: " ^ data)
| Result.Ok msg -> | Result.Ok msg ->
Lwt_io.printf "Got message: %s\n" (M.to_string msg) Lwt_io.printf "Got message: %s\n" (M.to_string msg)
>>= fun () -> Lwt_io.flush Lwt_io.stdout >>= fun () -> Lwt_io.flush Lwt_io.stdout
@ -44,15 +49,16 @@ let lwt_main () =
C.reconnect_loop ~after:30 C.reconnect_loop ~after:30
~connect:(fun () -> ~connect:(fun () ->
Lwt_io.printl "Connecting..." Lwt_io.printl "Connecting..."
>>= fun () -> C.connect_by_name ~server:!host ~port:!port ~nick:!nick () >>= fun () ->
) C.connect_by_name ~server:!host ~port:!port ~nick:!nick () )
~f:(fun connection -> ~f:(fun connection ->
Lwt_io.printl "Connected" Lwt_io.printl "Connected"
>>= fun () -> >>= fun () ->
Lwt_io.printl "send join msg" Lwt_io.printl "send join msg"
>>= fun () -> >>= fun () ->
C.send_join ~connection ~channel:!channel C.send_join ~connection ~channel:!channel
>>= fun () -> C.send_privmsg ~connection ~target:!channel ~message ) >>= fun () ->
C.send_privmsg ~connection ~target:!channel ~message )
~callback () ~callback ()
let _ = let _ =

291
main.ml
View File

@ -27,7 +27,7 @@ module Input = struct
(** Type of key code. *) (** Type of key code. *)
type code = type code =
| Char of UChar.t (** A unicode character. *) | UChar of UChar.t (** A unicode character. *)
| Enter | Enter
| Escape | Escape
| Tab | Tab
@ -57,6 +57,8 @@ module Input = struct
| Unknown | Unknown
| None | None
type key = Char of char | Code of code
module KeymodSet = struct module KeymodSet = struct
type t = Shift | Ctrl | Meta | Fn type t = Shift | Ctrl | Meta | Fn
@ -67,10 +69,10 @@ module Input = struct
let modset = Keymod.of_list let modset = Keymod.of_list
type key = {mods: Keymod.t; code: code} type keystate = {mods: Keymod.t; code: code}
module Key = struct module Key = struct
type t = key type t = keystate
let compare = compare let compare = compare
end end
@ -88,14 +90,18 @@ module Input = struct
type state = type state =
{ mutable bindings: t { mutable bindings: t
; mutable state: result ; mutable state: result
; mutable last_keyseq: key list ; mutable last_keyseq: keystate list
; mutable last_actions: action list } ; mutable last_actions: action list }
let add events action bindings = let add events action bindings =
let events = let events =
List.map List.map
(fun (m, k) -> (fun (m, k) ->
{mods= Keymod.of_list m; code= Char (UChar.of_char k)} ) { mods= Keymod.of_list m
; code=
( match k with
| Char c -> UChar (UChar.of_char c)
| Code c -> c ) } )
events in events in
S.add events action bindings S.add events action bindings
@ -111,7 +117,7 @@ module Input = struct
(* stolen from lambda-term/src/lTerm_{edit,key}.ml{,i} *) (* stolen from lambda-term/src/lTerm_{edit,key}.ml{,i} *)
let string_of_code = function let string_of_code = function
| Char ch -> Printf.sprintf "Char 0x%02x" (UChar.code ch) | UChar ch -> Printf.sprintf "Char 0x%02x" (UChar.code ch)
| Enter -> "Enter" | Enter -> "Enter"
| Escape -> "Escape" | Escape -> "Escape"
| Tab -> "Tab" | Tab -> "Tab"
@ -157,7 +163,7 @@ module Input = struct
if Keymod.mem Shift key.mods then Buffer.add_string buffer "S-" ; if Keymod.mem Shift key.mods then Buffer.add_string buffer "S-" ;
if Keymod.mem Fn key.mods then Buffer.add_string buffer "Fn-" ; if Keymod.mem Fn key.mods then Buffer.add_string buffer "Fn-" ;
( match key.code with ( match key.code with
| Char ch -> | UChar ch ->
let code = UChar.code ch in let code = UChar.code ch in
if code <= 255 then if code <= 255 then
match Char.chr code with match Char.chr code with
@ -191,8 +197,8 @@ module Event = struct
type mouse = int * int type mouse = int * int
type t = type t =
[ `Key_down of Input.key [ `Key_down of Input.keystate
| `Key_up of Input.key | `Key_up of Input.keystate
| `Text_editing of string | `Text_editing of string
| `Text_input of string | `Text_input of string
| `Mouse of mouse | `Mouse of mouse
@ -270,7 +276,7 @@ module Event = struct
|'&' | '$' | '*' | '%' | '!' | '?' | ',' | ';' | ':' |'&' | '$' | '*' | '%' | '!' | '?' | ',' | ';' | ':'
|'/' | '\\' | '.' | '@' | '=' | '+' | '-' | ' ' | '"' |'/' | '\\' | '.' | '@' | '=' | '+' | '-' | ' ' | '"'
|'\'' | '>' | '<' | '^' | '`' | '|' -> |'\'' | '>' | '<' | '^' | '`' | '|' ->
Char (UChar.of_int k) UChar (UChar.of_int k)
| _ -> None ) in | _ -> None ) in
let mods = let mods =
List.filter_map List.filter_map
@ -340,7 +346,7 @@ module Event = struct
| `Window_event -> `Unknown "`Window_event " | `Window_event -> `Unknown "`Window_event "
| `Display_event -> `Unknown "`Display_event " | `Display_event -> `Unknown "`Display_event "
| `Sensor_update -> `Unknown "`Sensor_update " in | `Sensor_update -> `Unknown "`Sensor_update " in
(* F.epr "event_of_sdlevent: %s@." (to_string r) ;*) (*F.epr "event_of_sdlevent: %s@." (to_string r) ;*)
r r
let key_up : Sdl.keycode = 0x40000052 let key_up : Sdl.keycode = 0x40000052
@ -351,9 +357,12 @@ module Event = struct
let actions_of_events (state : Input.Bind.state) (events : events) = let actions_of_events (state : Input.Bind.state) (events : events) =
let open Input.Bind in let open Input.Bind in
List.iter List.flatten
(function (List.filter_map
| `Key_down (k : Input.key) -> (fun e ->
(*F.epr "action_of_events: %s@." (to_string e) ;*)
match e with
| `Key_down (k : Input.keystate) -> (
( match state.state with ( match state.state with
| Continue _ -> () | Continue _ -> ()
| _ -> state.last_keyseq <- [] ) ; | _ -> state.last_keyseq <- [] ) ;
@ -361,17 +370,17 @@ module Event = struct
resolve k resolve k
(get_resolver state.state (get_resolver state.state
(default_resolver state.bindings) ) ; (default_resolver state.bindings) ) ;
state.last_keyseq <- k :: state.last_keyseq state.last_keyseq <- k :: state.last_keyseq ;
| _ -> () )
events ;
match state.state with match state.state with
| Accepted a -> | Accepted a ->
state.last_actions <- a ; state.last_actions <- a ;
a Some a
| Rejected -> | Rejected ->
state.last_actions <- [] ; state.last_actions <- [] ;
[] None
| _ -> [] | _ -> None )
| _ -> None )
events )
end end
module Display = struct module Display = struct
@ -787,89 +796,51 @@ module Panel = struct
, ( Box2.of_pts (Box2.o s.box) (P2.v !max_x (Box2.maxy !box)) , ( Box2.of_pts (Box2.o s.box) (P2.v !max_x (Box2.maxy !box))
, !node ) ) , !node ) )
let default_bindings = let textedit_bindings =
let open Input.Bind in let open Input.Bind in
let open CamomileLibrary in add [([], Code Left)] [Zed Prev_char]
let open Zed_edit in @@ add [([], Code Right)] [Zed Next_char]
let m = Input.Keymod.of_list in @@ add [([], Code Up)] [Zed Prev_line]
let b = ref empty in @@ add [([], Code Down)] [Zed Next_line]
let add e a = b := Input.Bind.S.add e a !b in @@ add [([], Code Home)] [Zed Goto_bol]
add [{mods= m []; code= Left}] [Zed Prev_char] ; @@ add [([], Code End)] [Zed Goto_eol]
add [{mods= m []; code= Right}] [Zed Next_char] ; @@ add [([], Code Insert)] [Zed Switch_erase_mode]
add [{mods= m []; code= Up}] [Zed Prev_line] ; @@ add [([], Code Delete)] [Zed Delete_next_char]
add [{mods= m []; code= Down}] [Zed Next_line] ; @@ add [([], Code Enter)] [Zed Newline]
add [{mods= m []; code= Home}] [Zed Goto_bol] ; @@ add [([Ctrl], Char ' ')] [Zed Set_mark]
add [{mods= m []; code= End}] [Zed Goto_eol] ; @@ add [([Ctrl], Char 'a')] [Zed Goto_bol]
add [{mods= m []; code= Insert}] [Zed Switch_erase_mode] ; @@ add [([Ctrl], Char 'e')] [Zed Goto_eol]
add [{mods= m []; code= Delete}] [Zed Delete_next_char] ; @@ add [([Ctrl], Char 'd')] [Zed Delete_next_char]
add [{mods= m []; code= Enter}] [Zed Newline] ; @@ add [([Ctrl], Char 'h')] [Zed Delete_prev_char]
add @@ add [([Ctrl], Char 'k')] [Zed Kill_next_line]
[{mods= m [Ctrl]; code= Char (UChar.of_char ' ')}] @@ add [([Ctrl], Char 'u')] [Zed Kill_prev_line]
[Zed Set_mark] ; @@ add [([Ctrl], Char 'n')] [Zed Next_line]
add @@ add [([Ctrl], Char 'p')] [Zed Prev_line]
[{mods= m [Ctrl]; code= Char (UChar.of_char 'a')}] @@ add [([Ctrl], Char 'w')] [Zed Kill]
[Zed Goto_bol] ; @@ add [([Ctrl], Char 'y')] [Zed Yank]
add @@ add [([], Code Backspace)] [Zed Delete_prev_char]
[{mods= m [Ctrl]; code= Char (UChar.of_char 'e')}] @@ add [([Meta], Char 'w')] [Zed Copy]
[Zed Goto_eol] ; @@ add [([Meta], Char 'c')] [Zed Capitalize_word]
add @@ add [([Meta], Char 'l')] [Zed Lowercase_word]
[{mods= m [Ctrl]; code= Char (UChar.of_char 'd')}] @@ add [([Meta], Char 'u')] [Zed Uppercase_word]
[Zed Delete_next_char] ; @@ add [([Meta], Char 'b')] [Zed Prev_word]
add @@ add [([Meta], Char 'f')] [Zed Next_word]
[{mods= m [Ctrl]; code= Char (UChar.of_char 'h')}] @@ add [([Meta], Code Right)] [Zed Next_word]
[Zed Delete_prev_char] ; @@ add [([Meta], Code Left)] [Zed Prev_word]
add @@ add [([Ctrl], Code Right)] [Zed Next_word]
[{mods= m [Ctrl]; code= Char (UChar.of_char 'k')}] @@ add [([Ctrl], Code Left)] [Zed Prev_word]
[Zed Kill_next_line] ; @@ add [([Meta], Code Backspace)] [Zed Kill_prev_word]
add @@ add [([Meta], Code Delete)] [Zed Kill_prev_word]
[{mods= m [Ctrl]; code= Char (UChar.of_char 'u')}] @@ add [([Ctrl], Code Delete)] [Zed Kill_next_word]
[Zed Kill_prev_line] ; @@ add [([Meta], Char 'd')] [Zed Kill_next_word]
add @@ add [([Ctrl], Char '/')] [Zed Undo]
[{mods= m [Ctrl]; code= Char (UChar.of_char 'n')}] @@ add [([Ctrl], Char 'x'); ([], Char 'u')] [Zed Undo]
[Zed Next_line] ; @@ empty
add
[{mods= m [Ctrl]; code= Char (UChar.of_char 'p')}]
[Zed Prev_line] ;
add [{mods= m [Ctrl]; code= Char (UChar.of_char 'w')}] [Zed Kill] ;
add [{mods= m [Ctrl]; code= Char (UChar.of_char 'y')}] [Zed Yank] ;
add [{mods= m []; code= Backspace}] [Zed Delete_prev_char] ;
add [{mods= m [Meta]; code= Char (UChar.of_char 'w')}] [Zed Copy] ;
add
[{mods= m [Meta]; code= Char (UChar.of_char 'c')}]
[Zed Capitalize_word] ;
add
[{mods= m [Meta]; code= Char (UChar.of_char 'l')}]
[Zed Lowercase_word] ;
add
[{mods= m [Meta]; code= Char (UChar.of_char 'u')}]
[Zed Uppercase_word] ;
add
[{mods= m [Meta]; code= Char (UChar.of_char 'b')}]
[Zed Prev_word] ;
add
[{mods= m [Meta]; code= Char (UChar.of_char 'f')}]
[Zed Next_word] ;
add [{mods= m [Meta]; code= Right}] [Zed Next_word] ;
add [{mods= m [Meta]; code= Left}] [Zed Prev_word] ;
add [{mods= m [Ctrl]; code= Right}] [Zed Next_word] ;
add [{mods= m [Ctrl]; code= Left}] [Zed Prev_word] ;
add [{mods= m [Meta]; code= Backspace}] [Zed Kill_prev_word] ;
add [{mods= m [Meta]; code= Delete}] [Zed Kill_prev_word] ;
add [{mods= m [Ctrl]; code= Delete}] [Zed Kill_next_word] ;
add
[{mods= m [Meta]; code= Char (UChar.of_char 'd')}]
[Zed Kill_next_word] ;
add [{mods= m [Ctrl]; code= Char (UChar.of_char '/')}] [Zed Undo] ;
add
[ {mods= m [Ctrl]; code= Char (UChar.of_char 'x')}
; {mods= m []; code= Char (UChar.of_char 'u')} ]
[Zed Undo] ;
!b
type textedit = type textedit =
{ze: unit Zed_edit.t; zc: Zed_cursor.t; keybind: Input.Bind.state} {ze: unit Zed_edit.t; zc: Zed_cursor.t; keybind: Input.Bind.state}
let make_textedit ?(keybinds = default_bindings) () = let make_textedit ?(keybinds = textedit_bindings) () =
let z = Zed_edit.create () in let z = Zed_edit.create () in
{ ze= z { ze= z
; zc= Zed_edit.new_cursor z ; zc= Zed_edit.new_cursor z
@ -885,7 +856,7 @@ module Panel = struct
(* collect events and update Zed context *) (* collect events and update Zed context *)
List.iter List.iter
(function (function
| `Key_down (k : Input.key) -> ( | `Key_down (k : Input.keystate) -> (
let open Input.Bind in let open Input.Bind in
( match te.keybind.state with ( match te.keybind.state with
| Accepted _ | Rejected -> | Accepted _ | Rejected ->
@ -990,6 +961,8 @@ module Text = Wall_text
module Store = struct module Store = struct
module Istore = Irmin_unix.Git.FS.KV (Irmin.Contents.String) module Istore = Irmin_unix.Git.FS.KV (Irmin.Contents.String)
(* storeview shows items of the selected level *)
type storeview = type storeview =
{ store: Istore.t { store: Istore.t
; mutable view: string list ; mutable view: string list
@ -1005,12 +978,77 @@ module Store = struct
(Istore.Repo.v (Irmin_git.config storepath)) ) (Istore.Repo.v (Irmin_git.config storepath)) )
branch ) branch )
; view= path ; view= path
; selected= [2] ; selected= [1]
; edit= false } ; edit= false }
let draw_storeview tree selected pp = let navigate sv action =
let rec nodecount (ipath : int list) tree =
match ipath with
| [] ->
Istore.Tree.list tree []
>>= fun l -> Lwt.return (List.length l)
| a :: b ->
Istore.Tree.list tree []
>>= fun l -> nodecount b (snd (List.nth l a)) in
let removelast l = List.rev (List.tl (List.rev l)) in
let last l = List.nth l (List.length l - 1) in
fun () ->
Lwt_main.run
( Istore.get_tree sv.store sv.view
>>= fun top ->
nodecount (removelast sv.selected) top
>>= fun seln ->
nodecount sv.selected top
>>= fun subn ->
Lwt.return
( ( match action with
| `Next ->
F.epr
"navigate `Next: (last sv.selected)=%d seln=%d@."
(last sv.selected) seln ;
if last sv.selected < seln - 1 then
sv.selected <-
List.mapi
(fun i a ->
if i >= List.length sv.selected - 1 then a + 1
else a )
sv.selected
| `Prev ->
if last sv.selected > 0 then
sv.selected <-
List.mapi
(fun i a ->
if i >= List.length sv.selected - 1 then a - 1
else a )
sv.selected
| `Sub -> if subn > 0 then sv.selected <- sv.selected @ [0]
| `Sup ->
if List.length sv.selected > 1 then
sv.selected <- removelast sv.selected ) ;
F.epr "Store.editor selected: %d@."
(List.nth sv.selected (List.length sv.selected - 1)) )
)
let editor ?(branch = "current") storepath : Panel.t =
let sv = make_storeview storepath branch in
let keybinds =
let open CamomileLibrary in
let open Input.Bind in
add [([], Char 'n')] [Custom (navigate sv `Next)]
@@ add [([], Char 'p')] [Custom (navigate sv `Prev)]
@@ add [([], Char 'd')] [Custom (navigate sv `Sub)]
@@ add [([], Char 'u')] [Custom (navigate sv `Sup)] empty in
let bindstate = Input.Bind.init keybinds in
{ act=
(fun panel events ->
List.iter
Input.Bind.(function Custom f -> f () | _ -> ())
(Event.actions_of_events bindstate events) ;
(Panel.vbox panel.subpanels).act panel events )
; subpanels=
[ Panel.prettyprint (fun pp ->
let indent = ref 0 in let indent = ref 0 in
let rec draw_levels ttree sel = let rec draw_levels tree sel =
indent := !indent + 1 ; indent := !indent + 1 ;
List.iteri List.iteri
(fun i (step, node) -> (fun i (step, node) ->
@ -1022,55 +1060,26 @@ module Store = struct
if sel = [i] then if sel = [i] then
Format.pp_open_stag pp Format.pp_open_stag pp
Display.( Display.(
Panel.Color_bg (Wall.Color.v 0.99 0.99 0.125 0.3)) ; Panel.Color_bg
(Wall.Color.v 0.99 0.99 0.125 0.3)) ;
Format.fprintf pp "%d-%s@." !indent step ; Format.fprintf pp "%d-%s@." !indent step ;
if sel = [i] then Format.pp_close_stag pp () ; if sel = [i] then Format.pp_close_stag pp () ;
Format.pp_close_box pp () ; Format.pp_close_box pp () ;
let subtree = Lwt_main.run (Istore.Tree.list node []) in let subtree =
Lwt_main.run (Istore.Tree.list node []) in
let subsel = let subsel =
if List.length sel > 0 && List.hd sel = i then List.tl sel if List.length sel > 0 && List.hd sel = i then
List.tl sel
else [] in else [] in
draw_levels subtree subsel ; draw_levels subtree subsel ;
Format.pp_close_box pp () ) Format.pp_close_box pp () )
ttree ; tree ;
indent := !indent - 1 in indent := !indent - 1 in
draw_levels tree selected
let navigate sv action =
let _root =
Lwt_main.run
( Istore.get_tree sv.store sv.view
>>= fun n -> Istore.Tree.list n [] ) in
let rec listlast f = function
| [] -> []
| [x] -> F.epr "%d@." x ; [f x]
| _ :: x -> listlast f x in
fun () ->
match action with
| `Next -> sv.selected <- listlast succ sv.selected
| `Prev -> sv.selected <- listlast pred sv.selected
let editor ?(branch = "current") storepath : Panel.t =
let sv = make_storeview storepath branch in
let keybinds =
let open CamomileLibrary in
let open Input.Bind in
add [([], 'n')] [Custom (navigate sv `Next)]
@@ add [([], 'p')] [Custom (navigate sv `Prev)] empty in
let bindstate = Input.Bind.init keybinds in
{ act=
(fun panel events ->
List.iter
Input.Bind.(function Custom f -> f () | _ -> ())
(Event.actions_of_events bindstate events) ;
(Panel.vbox panel.subpanels).act panel events )
; subpanels=
[ Panel.prettyprint (fun pp ->
let root = let root =
Lwt_main.run Lwt_main.run
( Istore.get_tree sv.store sv.view ( Istore.get_tree sv.store sv.view
>>= fun n -> Istore.Tree.list n [] ) in >>= fun n -> Istore.Tree.list n [] ) in
draw_storeview root sv.selected pp ) draw_levels root sv.selected )
; Panel.bindingstate bindstate ] ; Panel.bindingstate bindstate ]
; tag= "store-editor" } ; tag= "store-editor" }
end end
@ -1176,8 +1185,8 @@ let top_panel (t : top) =
F.epr "Exception in pane_top//eval@." in F.epr "Exception in pane_top//eval@." in
t.te.keybind.bindings <- t.te.keybind.bindings <-
Input.( Input.(
Bind.S.add Bind.add
[{mods= Keymod.of_list [Ctrl]; code= Enter}] [([Ctrl], Code Enter)]
Bind.[Custom eval] Bind.[Custom eval]
t.te.keybind.bindings) ; t.te.keybind.bindings) ;
Panel.( Panel.(