From e25c8c5db5bce5514817e282cc42dffa1497998f Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Wed, 8 Nov 2017 09:09:13 -0500 Subject: [PATCH] Fixed scrolling and cleaned up buffer/cursor interface --- docs/Buf.Cursor.html | 4 ++-- docs/Buf.html | 3 +-- docs/Rope.html | 3 --- docs/index_exceptions.html | 2 -- docs/index_values.html | 4 ---- docs/type_Buf.Cursor.html | 6 ++++-- lib/buf.ml | 14 ++++++-------- lib/buf.mli | 12 ++++-------- lib/rope.ml | 17 +++++++---------- lib/scrollmap.ml | 17 +++++++---------- 10 files changed, 31 insertions(+), 51 deletions(-) diff --git a/docs/Buf.Cursor.html b/docs/Buf.Cursor.html index 8e5a261..c9d7c7e 100644 --- a/docs/Buf.Cursor.html +++ b/docs/Buf.Cursor.html @@ -49,9 +49,9 @@
val make : Buf.buf -> int -> csr
val clone : csr -> csr
-
val iter : 'a -> 'b -> 'c -> unit
-
val offset : 'a -> csr -> int
+
val offset : csr -> int
val goto : Buf.buf -> csr -> int -> unit
+
val iter : (csr -> Rope.rune -> bool) -> Buf.buf -> csr -> unit
val getc : Buf.buf -> csr -> Rope.rune
val nextc : Buf.buf -> csr -> int
val prevc : Buf.buf -> csr -> int
diff --git a/docs/Buf.html b/docs/Buf.html index 81f30d3..d47d555 100644 --- a/docs/Buf.html +++ b/docs/Buf.html @@ -53,7 +53,6 @@
val empty : buf
val load : string -> buf
-
val iter : (Rope.rune -> bool) -> buf -> int -> unit
val iteri : (int -> Rope.rune -> bool) -> buf -> int -> unit
-
val is_eol : buf -> int -> bool
+
val iter : (Rope.rune -> bool) -> buf -> int -> unit
module Cursor: sig .. end
\ No newline at end of file diff --git a/docs/Rope.html b/docs/Rope.html index ba5d84c..33749d9 100644 --- a/docs/Rope.html +++ b/docs/Rope.html @@ -70,9 +70,6 @@
val getb : t -> int -> char
val getc : t -> int -> int
val putc : 'a -> 'b -> 'c -> 'a
-
exception Break_loop
- -
val iteri_leaf : (int -> int -> bool) -> int -> string -> int -> int -> unit
val iteri : (int -> int -> bool) -> t -> int -> unit
val gets : t -> int -> int -> string
val puts : t -> string -> int -> t
diff --git a/docs/index_exceptions.html b/docs/index_exceptions.html index d9c6c7e..79682f4 100644 --- a/docs/index_exceptions.html +++ b/docs/index_exceptions.html @@ -24,8 +24,6 @@
B Bad_rotation [Rope] -Break_loop [Rope] -
O Out_of_bounds [Rope] diff --git a/docs/index_values.html b/docs/index_values.html index f415397..e88a90c 100644 --- a/docs/index_values.html +++ b/docs/index_values.html @@ -161,8 +161,6 @@ is_eol [Buf.Cursor] -is_eol [Buf] - is_eol [Rope] iter [Buf.Cursor] @@ -173,8 +171,6 @@ iteri [Rope] -iteri_leaf [Rope] -
J join [Rope] diff --git a/docs/type_Buf.Cursor.html b/docs/type_Buf.Cursor.html index 6f55025..5bc2e2f 100644 --- a/docs/type_Buf.Cursor.html +++ b/docs/type_Buf.Cursor.html @@ -21,9 +21,11 @@   type t = Buf.Cursor.csr
  val make : Buf.buf -> int -> Buf.Cursor.csr
  val clone : Buf.Cursor.csr -> Buf.Cursor.csr
-  val iter : '-> '-> '-> unit
-  val offset : '-> Buf.Cursor.csr -> int
+  val offset : Buf.Cursor.csr -> int
  val goto : Buf.buf -> Buf.Cursor.csr -> int -> unit
+  val iter :
+    (Buf.Cursor.csr -> Rope.rune -> bool) ->
+    Buf.buf -> Buf.Cursor.csr -> unit
  val getc : Buf.buf -> Buf.Cursor.csr -> Rope.rune
  val nextc : Buf.buf -> Buf.Cursor.csr -> int
  val prevc : Buf.buf -> Buf.Cursor.csr -> int
diff --git a/lib/buf.ml b/lib/buf.ml index f8a37f3..1219ee3 100644 --- a/lib/buf.ml +++ b/lib/buf.ml @@ -11,14 +11,11 @@ let empty = let load path = { path = path; rope = Rope.from_string (Misc.load_file path) } -let iter fn buf i = - Rope.iteri (fun i c -> (fn c)) buf.rope i - let iteri fn buf i = Rope.iteri fn buf.rope i -let is_eol buf off = - Rope.is_eol buf.rope off +let iter fn buf i = + iteri (fun i c -> (fn c)) buf i module Cursor = struct type csr = { @@ -34,14 +31,15 @@ module Cursor = struct let clone csr = { start = csr.start; stop = csr.stop } - let iter fn buf csr = () - - let offset buf csr = + let offset csr = csr.stop let goto buf csr idx = csr.stop <- (Rope.limit_index buf.rope idx) + let iter fn buf csr = + Rope.iteri (fun i c -> csr.stop <- i; (fn csr c)) buf.rope csr.stop + let getc buf csr = Rope.getc buf.rope csr.stop diff --git a/lib/buf.mli b/lib/buf.mli index a5aa8bc..4bf4215 100644 --- a/lib/buf.mli +++ b/lib/buf.mli @@ -5,31 +5,27 @@ val load : string -> t val iter : (int -> bool) -> t -> int -> unit val iteri : (int -> int -> bool) -> t -> int -> unit -val is_eol : buf -> int -> bool - module Cursor : sig type t val make : buf -> int -> t val clone : t -> t - val iter : (t -> bool) -> buf -> t -> unit - val offset : buf -> t -> int + val iter : (t -> int -> bool) -> buf -> t -> unit + val offset : t -> int val goto : buf -> t -> int -> unit val getc : buf -> t -> int - (* +(* val putc : buf -> t -> int -> unit val gets : buf -> t -> string val puts : buf -> t -> string -> unit - *) +*) val nextc : buf -> t -> int val prevc : buf -> t -> int val nextln : buf -> t -> int val prevln : buf -> t -> int - (* val is_bol : buf -> t -> bool val to_eol : buf -> t -> int *) - val is_eol : buf -> t -> bool val to_bol : buf -> t -> int end diff --git a/lib/rope.ml b/lib/rope.ml index bb5ff38..7752789 100644 --- a/lib/rope.ml +++ b/lib/rope.ml @@ -102,11 +102,17 @@ let putc rope i c = rope (******************************************************************************) +(* inefficient form of iteri *) +let rec iteri fn rope pos = + if pos < (length rope) && (fn pos (getc rope pos)) then + iteri fn rope (pos + 1) + +(* More efficient form of iteri? exception Break_loop let iteri_leaf fn pos str off len = let offset = pos - off in - for i = off to off + len - 1 do (* break loop when fn returns false *) + for i = off to off + len - 1 do if (fn (i + offset) (Char.code str.[i])) == false then raise Break_loop done @@ -119,15 +125,6 @@ let rec iteri fn rope pos = | Node (l,r,_,_) -> iteri fn l pos; iteri fn r (pos + (length l)) - -(* -let rec iter_from fn rope pos = - if pos < (length rope) && (fn (getc rope pos)) then - iter_from fn rope (pos + 1) - -let rec iteri_from fn rope pos = - if pos < (length rope) && (fn pos (getc rope pos)) then - iteri_from fn rope (pos + 1) *) (******************************************************************************) diff --git a/lib/scrollmap.ml b/lib/scrollmap.ml index 7db51d0..6cb8cd4 100644 --- a/lib/scrollmap.ml +++ b/lib/scrollmap.ml @@ -11,25 +11,22 @@ let rec find_line lines off idx = idx let make buf width off = - let bcsr = Buf.Cursor.make buf off in - let dcsr = Draw.Cursor.make (width, 0) 0 0 in - let lines = ref [Buf.Cursor.to_bol buf bcsr] in + let csr = Draw.Cursor.make (width, 0) 0 0 in + (*let bol = Buf.goto StartOfLine buf off in*) + let bol = Buf.Cursor.to_bol buf (Buf.Cursor.make buf off) in + let lines = ref [bol] in let process_glyph i c = - let not_eol = ((Buf.is_eol buf i) == false) in - if (Draw.Cursor.next_glyph dcsr c) && not_eol then + let not_eol = (c != 0x0A) in + if (Draw.Cursor.next_glyph csr c) && not_eol then lines := i :: !lines; not_eol in - Buf.iteri process_glyph buf off; + Buf.iteri process_glyph buf bol; let lines = (Array.of_list (List.rev !lines)) in - print_string "map: "; - Array.iter (fun x -> Printf.printf "%d " x) lines; - print_endline ""; let index = (find_line lines off ((Array.length lines) - 1)) in { width = width; lines = lines; index = index } let first map = - Printf.printf "first: %d\n" map.lines.(map.index); map.lines.(map.index) let bopl buf off = -- 2.52.0