aboutsummaryrefslogtreecommitdiff
path: root/src/sheet.ml
diff options
context:
space:
mode:
Diffstat (limited to 'src/sheet.ml')
-rwxr-xr-xsrc/sheet.ml167
1 files changed, 65 insertions, 102 deletions
diff --git a/src/sheet.ml b/src/sheet.ml
index 6d3c34a..881bf58 100755
--- a/src/sheet.ml
+++ b/src/sheet.ml
@@ -1,11 +1,5 @@
type cell = int * int
-type search = [
- | `Pattern of ScTypes.Result.t option
- | `Next
- | `Previous
-]
-
module Raw = struct
type content = {
@@ -25,14 +19,10 @@ module Raw = struct
of 8×8 cells, and each array is stored in a tree. *)
module Map = PageMap.SplayMap(struct type t = content let default = empty_cell end)
- type t = Map.t
-
(** The sheet is a map which always contains evaluated values. When a cell is
updated, all the cell which references this value are also updated.
*)
- let empty = Map.empty
-
- let get_value id t = (Map.find id t).value
+ type t = Map.t
let get_expr id t = (Map.find id t).expr
@@ -41,7 +31,7 @@ module Raw = struct
@return Some map if the map has been updated
*)
let update catalog cell content t = begin
- let new_val = Expression.eval content.expr catalog (fun id -> get_value id t) in
+ let new_val = Expression.eval content.expr catalog (fun id -> (Map.find id t).value) in
match content.value with
| None ->
(* If the previous value wasn't defined, update the map *)
@@ -188,7 +178,7 @@ module Raw = struct
let f cell t = begin
{ cell with
expr = expression ;
- value = Some (Expression.eval expression catalog (fun id -> get_value id t)) }
+ value = Some (Expression.eval expression catalog (fun id -> (Map.find id t).value)) }
end in
add_element catalog id f t
end
@@ -198,147 +188,120 @@ module Raw = struct
let f cell t =
{ cell with
expr = expr ;
- value = Some (Expression.eval expr catalog (fun id -> get_value id t))
+ value = Some (Expression.eval expr catalog (fun id -> (Map.find id t).value))
} in
add_element catalog id f t
end
- let search pattern t = begin
- let exception Found of (int * int) in
-
- let _search key content () =
- if content.value = pattern then raise (Found key) in
-
- try
- (* Iteration*)
- Map.fold _search t ();
- None
- with Found key -> Some key
- end
-
let get_sink id t =
try (Map.find id t).sink
with Not_found -> Cell.Set.empty
- (** Fold over each defined value *)
- let fold f a t = begin
- Map.fold (fun key content a ->
- match content.value with
- | None -> a
- | Some x ->
- f a key (content.expr, x)
- ) t a
- end
-
end
-type yank = cell * Raw.content
-type history = ((cell * Expression.t) list) list
-
-type t = {
- selected: Selection.t; (* The selected cell *)
+type sheet = {
data: Raw.t;
- history: history; (* Unlimited history *)
- yank: yank list;
+ history: ((cell * Expression.t) list) list; (* Unlimited history *)
+ yank: (cell * Raw.content) list;
catalog: Functions.C.t;
}
+type t = sheet ref
+
let undo t = begin
- match t.history with
- | [] -> None
+ let catalog = (!t).catalog in
+ match (!t).history with
+ | [] -> false
| hd::tl ->
let data = List.fold_left (
fun data (id, expression) ->
if Expression.is_defined expression then
- snd @@ Raw.add id expression t.catalog data
+ snd @@ Raw.add id expression catalog data
else
- snd @@ Raw.remove id t.catalog data
- ) t.data hd in
- Some { t with data = data; history = tl}
+ snd @@ Raw.remove id catalog data
+ ) (!t).data hd in
+ t:= { (!t) with data = data; history = tl};
+ true
end
-let move direction t =
- let position = Selection.extract t.selected in
- let position' = begin match direction with
- | Actions.Left quant -> Tools.Tuple2.replace1 (max 1 ((fst position) - quant)) position
- | Actions.Right quant -> Tools.Tuple2.replace1 ((fst position) + quant) position
- | Actions.Up quant -> Tools.Tuple2.replace2 (max 1 ((snd position) - quant)) position
- | Actions.Down quant -> Tools.Tuple2.replace2 ((snd position) + quant) position
- | Actions.Absolute (x, y) -> (x, y)
- end in
- if position = position' then
- None
- else
- Some {t with selected = Selection.create position'}
-
-let delete t = begin
- let catalog = t.catalog in
- let history = Selection.fold (fun acc id -> (id, Raw.get_expr id t.data)::acc) [] t.selected in
- let count, data' = Selection.fold (fun (count, c) t ->
- (count + 1, snd @@ Raw.remove t catalog c)) (0, t.data) t.selected in
- let t' = { t with
+let delete selected t = begin
+ let catalog = (!t).catalog in
+ let history = Selection.fold (fun acc id -> (id, Raw.get_expr id (!t).data)::acc) [] selected in
+ let count, data' = Selection.fold (fun (count, data) c ->
+ (count + 1, snd @@ Raw.remove c catalog (!t).data)) (0, (!t).data) selected in
+ t := { !t with
data = data';
- history = history::t.history
- } in
- t', count
+ history = history::(!t).history
+ };
+ count
end
-let yank t = begin
+let yank selected t = begin
- let shift = Selection.shift t.selected in
+ let shift = Selection.shift selected in
let origin = shift (0, 0) in
let _yank (count, extracted) cell = begin
let content =
- try let content = (Raw.Map.find cell t.data) in
+ try let content = (Raw.Map.find cell (!t).data) in
{ content with Raw.expr = Expression.shift origin content.Raw.expr }
with Not_found -> Raw.empty_cell in
count + 1, (shift cell,content)::extracted
end in
- let count, yanked = Selection.fold _yank (0, []) t.selected in
- let t' = { t with yank = List.rev yanked; } in
- t', count
+ let count, yanked = Selection.fold _yank (0, []) selected in
+ t := { !t with yank = List.rev yanked; };
+ count
end
-let paste t = begin
- let catalog = t.catalog in
+let paste shift t = begin
+ let catalog = (!t).catalog in
(* Origin of first cell *)
- let (shift_x, shift_y) as shift = Selection.extract t.selected in
+ let (shift_x, shift_y) as shift = shift in
let history' = List.map (fun ((x, y), content) ->
let id = shift_x + x, shift_y + y in
- id, Raw.get_expr id t.data) t.yank in
+ id, Raw.get_expr id (!t).data) (!t).yank in
let _paste (count, t) ((x, y), content) = begin
count + 1, snd @@ Raw.paste catalog (shift_x + x, shift_y + y) shift content t
end in
- let count, data' = List.fold_left _paste (0, t.data) t.yank in
- let t' = { t with data = data'; history = history'::t.history } in
- t', count
+ let count, data' = List.fold_left _paste (0, (!t).data) (!t).yank in
+ t := { !t with data = data'; history = history'::(!t).history };
+ count
end
-let add expression t = begin
- let id = Selection.extract t.selected in
- let prev_expression = Raw.get_expr id t.data in
- let cells, data' = Raw.add id expression t.catalog t.data in
- cells, { t with data = data'; history = [id, prev_expression]::t.history }
-end
+let add ~history expression id t = begin
+ let prev_expression = Raw.get_expr id (!t).data in
+ let cells, data' = Raw.add id expression (!t).catalog (!t).data in
-let search action t = begin match action with
- | `Pattern pattern ->
- begin match Raw.search pattern t.data with
- | None -> None
- | Some x -> Some {t with selected = Selection.create x}
- end
- | _ -> None
+ let () = if history then
+ t:= { !t with data = data'; history = [id, prev_expression]::(!t).history }
+ else
+ t:= { !t with data = data' } in
+
+ cells
end
-let create catalog data = {
- data = data;
- selected = Selection.create (1, 1);
+let create catalog = ref {
+ data = Raw.Map.empty;
history = [];
yank = [];
catalog = catalog
}
+
+(** Fold over each defined value *)
+let fold f a t = begin
+ Raw.Map.fold (fun key content a ->
+ match content.Raw.value with
+ | None -> a
+ | Some x ->
+ f a key (content.Raw.expr, x)
+ ) (!t).data a
+end
+
+let get_cell id t = begin
+ let cell = Raw.Map.find id (!t).data in
+ cell.expr, cell.value, cell.sink
+end