+ (match s with
+ | (status, text) :: tl ->
+ ((status, parsed_text ^ text)::tl), (parsed_text_length + len)
+ | [] -> [], 0)
+ | TacticAst.Executable (loc, ex) ->
+ let parsed_text, parsed_text_length = text_of_loc loc in
+ eval_executable
+ status mathviewer urichooser ask_confirmation
+ user_goal parsed_text script ex
+
+let fresh_script_id =
+ let i = ref 0 in
+ fun () -> incr i; !i
+
+class script ~(buffer: GText.buffer) ~(init: MatitaTypes.status)
+ ~(mathviewer: MatitaTypes.mathViewer)
+ ~set_star
+ ~ask_confirmation
+ ~urichooser () =
+object (self)
+ val mutable filename = None
+ val scriptId = fresh_script_id ()
+ method private getFilename =
+ match filename with Some f -> f | _ -> assert false
+ method private ppFilename =
+ match filename with Some f -> f | None -> sprintf ".unnamed%d.ma" scriptId
+
+ initializer
+ ignore(GMain.Timeout.add ~ms:300000
+ ~callback:(fun _ -> self#_saveToBackuptFile ();true));
+ ignore(buffer#connect#modified_changed
+ (fun _ -> if buffer#modified then
+ set_star self#ppFilename true
+ else
+ set_star self#ppFilename false));
+ self#reset ();
+ self#template ()
+
+ val mutable statements = []; (** executed statements *)
+ val mutable history = [ init ];
+ (** list of states before having executed statements. Head element of this
+ * list is the current state, last element is the state at the beginning of
+ * the script.
+ * Invariant: this list length is 1 + length of statements *)
+
+ (** goal as seen by the user (i.e. metano corresponding to current tab) *)
+ val mutable userGoal = ~-1
+ val mutable lock_mark_pos = 0
+
+ (** text mark and tag representing locked part of a script *)
+ val locked_mark =
+ buffer#create_mark ~name:"locked" ~left_gravity:true buffer#start_iter
+ val locked_tag = buffer#create_tag [`BACKGROUND "lightblue"; `EDITABLE false]
+
+ (* history can't be empty, the invariant above grant that it contains at
+ * least the init status *)
+ method status = match history with hd :: _ -> hd | _ -> assert false
+
+ method private _advance ?statement () =
+ let s = match statement with Some s -> s | None -> self#getFuture in
+ MatitaLog.debug ("evaluating: " ^ first_line s ^ " ...");
+ let (entries, parsed_len) =
+ eval_statement self#status
+ mathviewer urichooser ask_confirmation userGoal self s
+ in
+ let (new_statuses, new_statements) = List.split entries in
+(*
+prerr_endline "evalStatement returned";
+List.iter (fun s -> prerr_endline ("'" ^ s ^ "'")) new_statements;
+*)
+ history <- List.rev new_statuses @ history;
+ statements <- List.rev new_statements @ statements;
+ let start = buffer#get_iter_at_mark (`MARK locked_mark) in
+ let new_text = String.concat "" new_statements in
+ if new_text <> String.sub s 0 parsed_len then
+ begin
+(* prerr_endline ("new:" ^ new_text); *)
+(* prerr_endline ("s:" ^ String.sub s 0 parsed_len); *)
+ let stop = start#copy#forward_chars parsed_len in
+ buffer#delete ~start ~stop;
+ buffer#insert ~iter:start new_text;
+(* prerr_endline "AUTOMATICALLY MODIFIED!!!!!" *)
+ end;
+ self#moveMark (String.length new_text)
+
+ method private _retract () =
+ match statements, history with
+ | last_statement :: _, cur_status :: prev_status :: _ ->
+ MatitaSync.time_travel ~present:cur_status ~past:prev_status;
+ statements <- List.tl statements;
+ history <- List.tl history;
+ self#moveMark (- (String.length last_statement));
+ | _ -> raise Margin
+
+ method advance ?statement () =
+ try
+ self#_advance ?statement ();
+ self#notify
+ with
+ | Margin -> self#notify
+ | exc -> self#notify; raise exc
+
+ method retract () =
+ try
+ self#_retract ();
+ self#notify
+ with
+ | Margin -> self#notify
+ | exc -> self#notify; raise exc
+
+ method private getFuture =
+ buffer#get_text ~start:(buffer#get_iter_at_mark (`MARK locked_mark))
+ ~stop:buffer#end_iter ()
+
+
+ (** @param rel_offset relative offset from current position of locked_mark *)
+ method private moveMark rel_offset =
+ let mark = `MARK locked_mark in
+ let old_insert = buffer#get_iter_at_mark `INSERT in
+ buffer#remove_tag locked_tag ~start:buffer#start_iter ~stop:buffer#end_iter;
+ let current_mark_pos = buffer#get_iter_at_mark mark in
+ let new_mark_pos =
+ match rel_offset with
+ | 0 -> current_mark_pos
+ | n when n > 0 -> current_mark_pos#forward_chars n
+ | n (* when n < 0 *) -> current_mark_pos#backward_chars (abs n)
+ in
+ buffer#move_mark mark ~where:new_mark_pos;
+ buffer#apply_tag locked_tag ~start:buffer#start_iter ~stop:new_mark_pos;
+ lock_mark_pos <- new_mark_pos#offset;
+ buffer#move_mark `INSERT old_insert;
+ begin
+ match self#status.proof_status with
+ Incomplete_proof (_,goal) -> self#setGoal goal
+ | _ -> ()
+ end ;
+ while Glib.Main.pending () do ignore(Glib.Main.iteration false); done
+
+ method clean_dirty_lock =
+ let lock_mark_iter = buffer#get_iter (`OFFSET lock_mark_pos) in
+ buffer#remove_tag locked_tag ~start:buffer#start_iter ~stop:buffer#end_iter;
+ buffer#apply_tag locked_tag ~start:buffer#start_iter ~stop:lock_mark_iter
+
+ val mutable observers = []
+
+ method addObserver (o: MatitaTypes.status -> unit) =
+ observers <- o :: observers
+
+ method private notify =
+ let status = self#status in
+ List.iter (fun o -> o status) observers
+
+ method loadFromFile () =
+ buffer#set_text (MatitaMisc.input_file self#getFilename);
+ self#goto_top;
+ buffer#set_modified false
+
+ method assignFileName file =
+ filename <- Some file;
+
+ method saveToFile () =
+ let oc = open_out self#getFilename in
+ output_string oc (buffer#get_text ~start:buffer#start_iter
+ ~stop:buffer#end_iter ());
+ close_out oc;
+ buffer#set_modified false
+
+ method private _saveToBackuptFile () =
+ if buffer#modified then
+ begin
+ let f = self#ppFilename ^ "~" in
+ let oc = open_out f in
+ output_string oc (buffer#get_text ~start:buffer#start_iter
+ ~stop:buffer#end_iter ());
+ close_out oc;
+ MatitaLog.debug ("backup " ^ f ^ " saved")
+ end
+
+ method private goto_top =
+ MatitaSync.time_travel ~present:self#status ~past:init;
+ statements <- [];
+ history <- [ init ];
+ userGoal <- ~-1;
+ buffer#remove_tag locked_tag ~start:buffer#start_iter ~stop:buffer#end_iter;
+ buffer#move_mark (`MARK locked_mark) ~where:buffer#start_iter
+
+ method reset () =
+ self#goto_top;
+ buffer#delete ~start:buffer#start_iter ~stop:buffer#end_iter;
+ self#notify;
+ buffer#set_modified false
+
+ method template () =
+ let template = MatitaMisc.input_file BuildTimeConf.script_template in
+ buffer#insert ~iter:(buffer#get_iter `START) template;
+ filename <- None;
+ buffer#set_modified false;
+ set_star self#ppFilename false
+
+ method goto (pos: [`Top | `Bottom | `Cursor]) () =
+ match pos with
+ | `Top -> self#goto_top; self#notify
+ | `Bottom ->
+ (try
+ let getpos _ = buffer#get_iter_at_mark (`MARK locked_mark) in
+ let rec dowhile pos =
+ self#_advance ();
+ if pos#compare (getpos ()) < 0 then
+ dowhile (getpos ())