pax_global_header00006660000000000000000000000064136453076110014520gustar00rootroot0000000000000052 comment=e2a5ca84d9c3c4142a47ef867b2bbc44083246c5 mew-0.1.0/000077500000000000000000000000001364530761100123065ustar00rootroot00000000000000mew-0.1.0/.gitignore000066400000000000000000000001571364530761100143010ustar00rootroot00000000000000**/*.annot **/.\#* **/*~ **/Session.vim **/.*.swp **/*.o **/*.a **/*.so **/*.cm* _build .merlin mew.install mew-0.1.0/CHANGES.md000066400000000000000000000000701364530761100136750ustar00rootroot000000000000000.1.0 (2020-04-14) ------------------ initial release mew-0.1.0/LICENSE000066400000000000000000000021031364530761100133070ustar00rootroot00000000000000The MIT License Copyright (c) 2020, ZAN DoYe Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. mew-0.1.0/Makefile000066400000000000000000000004641364530761100137520ustar00rootroot00000000000000default: dune build install: dune install uninstall: dune uninstall doc: dune build @doc clean: dune clean runtest: dune runtest all-supported-ocaml-versions: dune build @install --workspace dune-workspace.4.02.dev --root . dune build @install @runtest --workspace dune-workspace.dev --root . mew-0.1.0/README.md000066400000000000000000000007751364530761100135760ustar00rootroot00000000000000# mew — Modal Editing Witch This is the core module of mew, a general modal editing engine generator. ## Usage You can provide your `Key`, `Mode`, `Concurrent` modules to define the real world environment to get the core component of a modal editing engine. The core compoment support recursive key mapping ossociated with user provided modes. After the core component is generated, you may extended it with a translater to interpret user key sequence, so you'll get a complete modal editing engine. mew-0.1.0/VERSION000066400000000000000000000000061364530761100133520ustar00rootroot000000000000000.1.0 mew-0.1.0/dune-project000066400000000000000000000000201364530761100146200ustar00rootroot00000000000000(lang dune 1.1) mew-0.1.0/dune-workspace.4.02.dev000066400000000000000000000001041364530761100163120ustar00rootroot00000000000000(lang dune 1.1) (context (opam (switch 4.02.3))) (profile release) mew-0.1.0/dune-workspace.dev000066400000000000000000000001641364530761100157360ustar00rootroot00000000000000(lang dune 1.1) (context (opam (switch 4.03.0))) (context (opam (switch 4.06.1))) (context (opam (switch 4.08.1))) mew-0.1.0/mew.opam000066400000000000000000000007501364530761100137560ustar00rootroot00000000000000opam-version: "2.0" maintainer: "zandoye@gmail.com" authors: [ "ZAN DoYe" ] homepage: "https://github.com/kandu/mew" bug-reports: "https://github.com/kandu/mew/issues" license: "MIT" dev-repo: "git+https://github.com/kandu/mew.git" build: [ ["dune" "build" "-p" name "-j" jobs] ] depends: [ "ocaml" {>= "4.02.3"} "result" "trie" "dune" {>= "1.1.0"} ] synopsis: "Modal editing witch" description: """ This is the core module of mew, a general modal editing engine generator.""" mew-0.1.0/src/000077500000000000000000000000001364530761100130755ustar00rootroot00000000000000mew-0.1.0/src/concurrent.ml000066400000000000000000000013221364530761100156070ustar00rootroot00000000000000(* * concurrent.ml * ----------- * Copyright : (c) 2019 - 2020, ZAN DoYe * Licence : MIT * * This file is a part of mew. *) module type S = sig module Thread : sig type 'a t val bind : 'a t -> ('a -> 'b t) -> 'b t val return : 'a -> 'a t val both : 'a t -> 'b t -> ('a * 'b) t val join : unit t list -> unit t val pick : 'a t list -> 'a t val choose : 'a t list -> 'a t val async : (unit -> unit t)-> unit val cancel : 'a t-> unit val sleep : float -> unit t val run : 'a t -> 'a end module MsgBox : sig type 'a t val create : unit -> 'a t val put : 'a t -> 'a -> unit Thread.t val get : 'a t -> 'a Thread.t end end mew-0.1.0/src/dune000066400000000000000000000001501364530761100137470ustar00rootroot00000000000000(library (name mew) (public_name mew) (libraries result trie) (flags (:standard -safe-string))) mew-0.1.0/src/key.ml000066400000000000000000000015751364530761100142270ustar00rootroot00000000000000(* * key.ml * ----------- * Copyright : (c) 2019 - 2020, ZAN DoYe * Licence : MIT * * This file is a part of mew. *) let hash code modifier= let shift= List.length modifier in let code= code lsl shift in let modifier= List.mapi (fun i m-> m lsl i) modifier in let key= List.fold_left (fun sum m-> sum land m) code modifier in let result= let key= float_of_int key in mod_float (key *. 0.6180339887) 1. *. 16384. |> int_of_float in result module type S = sig type t type code type modifier type modifiers val create : code:code -> modifiers:modifiers -> t val create_modifiers : modifier list -> modifiers val code : t -> code val modifiers : t -> modifiers val modifier : key:t -> modifier:modifier -> bool val compare : t -> t -> int val to_string : t -> string include Hashtbl.HashedType with type t:= t end mew-0.1.0/src/mew.ml000066400000000000000000000073531364530761100142270ustar00rootroot00000000000000(* * mew.ml * ----------- * Copyright : (c) 2019 - 2020, ZAN DoYe * Licence : MIT * * This file is a part of mew. *) module Concurrent = Concurrent module Modal = Modal module Mode = Mode module Key = Key module Make (Modal:Modal.S) (Concurrent:Concurrent.S) = struct module Key = Modal.Key module Mode = Modal.Mode module MsgBox = Concurrent.MsgBox module Thread = Concurrent.Thread let (>>=)= Thread.bind class edit state= object(self) val i= MsgBox.create () val o: Key.t MsgBox.t= MsgBox.create () val mutable curr_mode: Mode.t= let _, mode= state#default_mode in mode method keyin (key:Key.t)= MsgBox.put i key method i= i method o= o method getMode= curr_mode method setMode mode= curr_mode <- Mode.Modes.find mode state#modes method timeout= match Mode.timeout self#getMode with | Some timeout-> timeout | None-> state#timeout method bindings= Mode.bindings self#getMode initializer let rec get_key sources= match sources with | []-> MsgBox.get i >>= fun key-> Thread.return (key, sources) | source::tl-> match Queue.take source with | key-> Thread.return (key, sources) | exception Queue.Empty-> get_key tl in let output_seq o seq= let rec output_seq ()= match Queue.take seq with | key-> MsgBox.put o key >>= output_seq | exception Queue.Empty-> Thread.return () in output_seq () in let perform action sources= match action with | Mode.Switch name-> self#setMode name; sources | Key key-> let seq= Queue.create() in Queue.add key seq; seq::sources | KeySeq keyseq-> keyseq::sources | Custom f-> f (); sources in let rec listen sources mem_key last node= match node with | Some node-> Thread.pick [ (Thread.sleep self#timeout >>= fun ()-> Thread.return None); get_key sources >>= fun (key, sources)-> Thread.return (Some (key, sources)) ] >>= (function | Some (key, sources)-> Queue.add key mem_key; try_matching sources mem_key last node key | None-> skip_matching sources mem_key last) | None-> let node= self#bindings in get_key sources >>= fun (key, sources)-> Queue.add key mem_key; try_matching sources mem_key last node key and try_matching sources mem_key last node key= match Mode.KeyTrie.sub node [key] with | Some node-> let last= match Mode.KeyTrie.get node [] with | Some action-> Some (Queue.copy mem_key, action) | None-> last in if Mode.KeyTrie.is_leaf node then skip_matching sources mem_key last else listen sources mem_key last (Some node) | None-> skip_matching sources mem_key last and skip_matching sources mem_key last= match last with | Some (seq, action)-> Utils.Queue.drop (Queue.length seq) mem_key; let sources= perform action sources in listen (mem_key::sources) (Queue.create ()) None None | None-> output_seq o mem_key >>= fun ()-> listen sources (Queue.create()) None None in Thread.async (fun ()-> listen [] (Queue.create ()) None None) end class state modes= object(self) val mutable timeout= 1. val mutable default_mode= Modal.Mode.default_mode modes method edit= new edit self method modes= modes method default_mode= default_mode method timeout= timeout end end mew-0.1.0/src/modal.ml000066400000000000000000000004211364530761100145200ustar00rootroot00000000000000(* * modal.ml * ----------- * Copyright : (c) 2019 - 2020, ZAN DoYe * Licence : MIT * * This file is a part of mew. *) module type S = sig module Key : Key.S module Name : Mode.Name module Mode : module type of Mode.Make(Key)(Name) end mew-0.1.0/src/mode.ml000066400000000000000000000016071364530761100143570ustar00rootroot00000000000000(* * mode.ml * ----------- * Copyright : (c) 2019 - 2020, ZAN DoYe * Licence : MIT * * This file is a part of mew. *) module type Name = sig type t val compare : t -> t -> int end module Make(Key:Key.S) (Name:Name) = struct module KeyTrie = Trie.Make(Key) type name= Name.t type action= | Switch of name | Key of Key.t | KeySeq of Key.t Queue.t | Custom of (unit -> unit) type t= { name: name; timeout: float option; bindings: action KeyTrie.node; } module Modes = Map.Make(Name) type modes= t Modes.t let name m= m.name let timeout m= m.timeout let bindings m= m.bindings let compare m1 m2= compare m1.name m2.name let default_mode modes= Modes.bindings modes |> List.hd let bind mode keyseq action= KeyTrie.set mode.bindings keyseq action let unbind mode keyseq= KeyTrie.unset mode.bindings keyseq end mew-0.1.0/src/utils.ml000066400000000000000000000006631364530761100145740ustar00rootroot00000000000000(* * utils.ml * ----------- * Copyright : (c) 2019 - 2020, ZAN DoYe * Licence : MIT * * This file is a part of mew. * This module implements strict impure trie tree data structure. *) module Queue = struct let rec drop n q= if n > 0 then (ignore (Queue.take q); drop (n-1) q) let to_list_rev q= Queue.fold (fun l key-> key::l) [] q let to_list q= q |> to_list_rev |> List.rev end mew-0.1.0/test/000077500000000000000000000000001364530761100132655ustar00rootroot00000000000000mew-0.1.0/test/dune000066400000000000000000000001761364530761100141470ustar00rootroot00000000000000(library (name test) (flags (:standard -safe-string)) (libraries mew) (inline_tests) (preprocess (pps ppx_expect))) mew-0.1.0/test/test.ml000066400000000000000000000000001364530761100145640ustar00rootroot00000000000000