From 67ef9afea6f1292e2231690e488c60dd3bd3e8ee Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Sat, 21 Oct 2017 13:42:18 -0400 Subject: [PATCH] added function to create a cursor --- edit.ml | 14 +++++++------- lib/buf.ml | 32 ++++++++++++++++++++++---------- lib/rope.ml | 6 ++++++ 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/edit.ml b/edit.ml index c477612..49f71af 100644 --- a/edit.ml +++ b/edit.ml @@ -1,12 +1,12 @@ open X11 -(* -let font = font_load "Times New Roman:size=12" -let font = font_load "Monaco:size=10::antialias=true:autohint=true" -*) -let font = font_load "Verdana:size=11:antialias=true:autohint=true" -let tags_buf = ref Buf.create -let edit_buf = ref Buf.create +let font_times = font_load "Times New Roman:size=12" +let font_monaco = font_load "Monaco:size=10::antialias=true:autohint=true" +let font_verdana = font_load "Verdana:size=11:antialias=true:autohint=true" + +let font = font_verdana +let tags_buf = ref Buf.empty +let edit_buf = ref Buf.empty (* Drawing functions ******************************************************************************) diff --git a/lib/buf.ml b/lib/buf.ml index 4a58ba7..a6be456 100644 --- a/lib/buf.ml +++ b/lib/buf.ml @@ -1,4 +1,7 @@ -type item = Ch | Word | Line +type cursor = { + start : int; + stop : int +} type buf = { path : string; @@ -8,21 +11,30 @@ type buf = { let iter_from fn buf i = Rope.iter_from fn buf.rope i -let create = +let empty = { path = ""; rope = Rope.empty } let load path = { path = path; rope = Rope.from_string (Misc.load_file path) } -let saveas buf path = +let make_cursor buf start stop = + { start = (Rope.limit_index buf.rope start); + stop = (Rope.limit_index buf.rope stop) } + +let move_rune count csr buf ext = + let newstop = csr.stop + count in + let newstart = if ext then csr.start else newstop in + make_cursor buf newstart newstop + +let move_word count csr buf ext = () -let save buf = - saveas buf buf.path +let move_line count csr buf ext = + () + +(* Unit Tests *****************************************************************) -let move item count buf pos = - match item with - | Ch -> pos + count - | Word -> pos + count - | Line -> pos + count +let run_unit_tests () = + let open Test in + () diff --git a/lib/rope.ml b/lib/rope.ml index 69dd58b..8acfbed 100644 --- a/lib/rope.ml +++ b/lib/rope.ml @@ -19,6 +19,12 @@ let check_index rope i = if i < 0 || i >= (length rope) then raise (Out_of_bounds "Rope.check_index") +let limit_index rope i = + if i < 0 then 0 + else if i > 0 && i >= (length rope) then + ((length rope) - 1) + else i + let join left right = let left_len = (length left) in let right_len = (length right) in -- 2.49.0