]> git.mdlowis.com Git - archive/tide-ocaml.git/commitdiff
replaced imperative iterator with more idiomatic iter_from function
authorMichael D. Lowis <mike@mdlowis.com>
Wed, 11 Oct 2017 02:26:11 +0000 (22:26 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Wed, 11 Oct 2017 02:26:11 +0000 (22:26 -0400)
edit.ml
lib/buf.ml
lib/rope.ml
lib/x11_prims.c

diff --git a/edit.ml b/edit.ml
index 848d7e20614dd9739176e5e29e35b11f55ba3db8..d1215f95a99b3c7aff27b4bf33505d4da60ae624 100644 (file)
--- a/edit.ml
+++ b/edit.ml
@@ -2,6 +2,8 @@ open X11
 
 (*let font = font_load "Times New Roman:pixelsize=14"*)
 let font = font_load "Monospace:size=10"
+let tags_buf = ref Buf.create
+let edit_buf = ref Buf.create
 
 (* Drawing functions
  ******************************************************************************)
@@ -91,6 +93,8 @@ let onevent = function
 (* Main Routine
  ******************************************************************************)
 let () =
+  if Array.length Sys.argv > 1 then
+    edit_buf := Buf.load Sys.argv.(1);
   let win = make_window 640 480 in
   show_window win true;
   event_loop 50 onevent
index bb811bed54d608c150bb9c5b2ce7392bab2abbd6..cb0c0253dacd2f93662300f0fdf910ba339dd904 100644 (file)
@@ -23,6 +23,9 @@ type buf = {
   redo : bufstate list;
 }
 
+let iter_from fn buf i =
+  Rope.iter_from fn buf i
+
 let create =
   let state = { nlines = 0; outpoint = 0; rope = Rope.empty }
   and info  = { path = ""; modtime = 0; charset = Utf8; crlf = Unix } in
index 128adc5fc30950908133ebad3ffe0a2359043334..f072b3eb8c69db1815505025dca0095bbd5fb328 100644 (file)
@@ -45,7 +45,7 @@ let rec getc rope i =
   check_index rope i;
   match rope with
   | Leaf (s,off,_) -> s.[off + i]
-  | Node (l,r,len)   ->
+  | Node (l,r,len) ->
       let left_len = (length l) in
       if i < left_len then
         getc l i
@@ -62,43 +62,19 @@ let del rope i j =
   let (r_left,r_right) = split l_right (j - i) in
   (join l_left r_right)
 
-module Iter = struct
-  type t = {
-    rope: rope;
-    length: int;
-    mutable pos: int;
-  }
-
-  let make rope index =
-    check_index rope index;
-    { rope = rope; length = (length rope); pos = index }
-
-  let pos itr = itr.pos
-
-  let incr itr = itr.pos <- (itr.pos + 1)
-
-  let decr itr = itr.pos <- (itr.pos - 1)
-
-  let goto itr pos= itr.pos <- pos
-
-  let move itr off = itr.pos <- (itr.pos + off)
-
-  let get itr = getc itr.rope itr.pos
-
-  let has_next itr = (itr.pos + 1) <= itr.length
+let rec iter_from fn rope pos =
+  if pos < (length rope) && (fn (getc rope pos)) then
+    iter_from fn rope (pos + 1)
 
-  let has_prev itr = (itr.pos - 1) > 0
-end
+let rec iteri_from fn rope pos =
+  if pos < (length rope) && (fn pos (getc rope pos)) then
+    iteri_from fn rope (pos + 1)
 
 let iteri fn rope =
-  let it = Iter.make rope 0 in
-  while (Iter.has_next it) do
-    fn (Iter.pos it) (Iter.get it);
-    Iter.incr it
-  done
+  iteri_from (fun i c -> (fn i c); true) rope 0
 
 let iter fn rope =
-  iteri (fun i c -> (fn c)) rope
+  iter_from (fun c -> (fn c); true) rope 0
 
 let map fn rope =
   let buf = Bytes.create (length rope) in
@@ -111,12 +87,12 @@ let mapi fn rope =
   from_string (Bytes.unsafe_to_string buf)
 
 let gets rope i j =
-  let buf = Bytes.create (j - i)
-  and it  = Iter.make rope 0 in
-  while (Iter.has_next it) && ((Iter.pos it) <= j) do
-    Bytes.set buf ((Iter.pos it) - i) (Iter.get it);
-    Iter.incr it;
-  done;
+  let buf = Bytes.create (j - i) in
+  iteri_from
+    (fun n c ->
+      Bytes.set buf (n - i) (getc rope i);
+      (n <= j))
+    rope i;
   Bytes.unsafe_to_string buf
 
 let to_string rope =
index 0b228791af47f886279b340d747c6e441fc5e64f..5daf87a40769a973e2e64ba7782c1a3cfe95ad6f 100644 (file)
@@ -261,13 +261,13 @@ CAMLprim value x11_draw_glyph(value color, value glyph, value coord) {
         .x     = intfield(coord,0) + intfield(glyph,4),
         .y     = intfield(coord,1) + font->ascent
     };
-    printf("c: '%c' w: %d x: %d y: %d xoff: %d yoff: %d\n",
-        intfield(glyph,2),
-        intfield(glyph,3),
-        intfield(glyph,4),
-        intfield(glyph,5),
-        intfield(glyph,6),
-        intfield(glyph,7));
+//    printf("c: '%c' w: %d x: %d y: %d xoff: %d yoff: %d\n",
+//        intfield(glyph,2),
+//        intfield(glyph,3),
+//        intfield(glyph,4),
+//        intfield(glyph,5),
+//        intfield(glyph,6),
+//        intfield(glyph,7));
     XftColor fgc;
     xftcolor(&fgc, Int_val(color));
     XftDrawGlyphFontSpec(X.xft, &fgc, &spec, 1);