1 (* Copyright (C) 2004-2005, HELM Team.
3 * This file is part of HELM, an Hypertextual, Electronic
4 * Library of Mathematics, developed at the Computer Science
5 * Department, University of Bologna, Italy.
7 * HELM is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * HELM is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with HELM; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * For details, see the HELM World-Wide-Web page,
23 * http://helm.cs.unibo.it/
29 module TA = GrafiteAst
32 let debug_print = if debug then prerr_endline else ignore
34 (** raised when one of the script margins (top or bottom) is reached *)
37 let safe_substring s i j =
38 try String.sub s i j with Invalid_argument _ -> assert false
40 let heading_nl_RE = Pcre.regexp "^\\s*\n\\s*"
41 let heading_nl_RE' = Pcre.regexp "^(\\s*\n\\s*)((.|\n)*)"
42 let only_dust_RE = Pcre.regexp "^(\\s|\n|%%[^\n]*\n)*$"
43 let multiline_RE = Pcre.regexp "^\n[^\n]+$"
44 let newline_RE = Pcre.regexp "\n"
47 if Pcre.pmatch ~rex:multiline_RE str then
48 "\n(** " ^ (Pcre.replace ~rex:newline_RE str) ^ " *)"
50 "\n(**\n" ^ str ^ "\n*)"
53 let s = Pcre.replace ~rex:heading_nl_RE s in
55 let nl_pos = String.index s '\n' in
59 (** creates a statement AST for the Goal tactic, e.g. "goal 7" *)
61 let module A = GrafiteAst in
62 let loc = DisambiguateTypes.dummy_floc in
63 A.Executable (loc, A.Tactical (loc,
64 A.Tactic (loc, A.Goal (loc, n)),
68 mathviewer:MatitaTypes.mathViewer;
69 urichooser: UriManager.uri list -> UriManager.uri list;
70 ask_confirmation: title:string -> message:string -> [`YES | `NO | `CANCEL];
71 develcreator: containing:string option -> unit;
72 mutable filenamedata: string option * MatitamakeLib.development option
75 let eval_with_engine guistuff status user_goal parsed_text st =
76 let module TAPp = GrafiteAstPp in
78 match guistuff.filenamedata with
80 | None,Some devel -> [MatitamakeLib.root_for_development devel ]
82 match MatitamakeLib.development_for_dir (Filename.dirname f) with
84 | Some devel -> [MatitamakeLib.root_for_development devel ]
87 include_ @ (Helm_registry.get_list Helm_registry.string "matita.includes")
89 let parsed_text_length = String.length parsed_text in
91 match st with TA.Executable (loc,ex) -> loc, ex | _ -> assert false in
92 let initial_space,parsed_text =
94 let pieces = Pcre.extract ~rex:heading_nl_RE' parsed_text in
95 pieces.(1), pieces.(2)
97 Not_found -> "", parsed_text in
98 (* we add the goal command if needed *)
99 let inital_space,new_status,new_status_and_text_list' =
100 match status.proof_status with
101 (* | Incomplete_proof { stack = stack }
102 when not (List.mem user_goal (Continuationals.head_goals stack)) ->
104 MatitaEngine.eval_ast
105 ~do_heavy_checks:true status (goal_ast user_goal)
107 let initial_space = if initial_space = "" then "\n" else initial_space
111 initial_space ^ TAPp.pp_tactical (TA.Select (loc, [user_goal])) ] *)
112 | _ -> initial_space,status,[] in
114 GrafiteEngine.eval_ast
115 ~baseuri_of_script:(GrafiteParserMisc.baseuri_of_script ~include_paths:include_)
116 ~disambiguate_tactic:GrafiteDisambiguate.disambiguate_tactic
117 ~disambiguate_command:GrafiteDisambiguate.disambiguate_command
118 ~do_heavy_checks:true new_status st
122 | TA.Command (_, TA.Alias _)
123 | TA.Command (_, TA.Include _)
124 | TA.Command (_, TA.Interpretation _) -> []
125 | _ -> MatitaSync.alias_diff ~from:status new_status
127 (* we remove the defined object since we consider them "automatic aliases" *)
129 TA.Comment (DisambiguateTypes.dummy_floc,
130 TA.Note (DisambiguateTypes.dummy_floc, ""))
132 let initial_space,status,new_status_and_text_list_rev =
133 let module DTE = DisambiguateTypes.Environment in
134 let module UM = UriManager in
135 let baseuri = GrafiteTypes.get_string_option new_status "baseuri" in
137 fun (initial_space,status,acc) (k,((v,_) as value)) ->
140 UM.buri_of_uri (UM.uri_of_string v) = baseuri
142 UriManager.IllFormedUri _ -> false (* v is a description, not a URI *)
145 initial_space,status,acc
149 if initial_space = "" then "\n" else initial_space in
151 DisambiguatePp.pp_environment
152 (DisambiguateTypes.Environment.add k value
153 DisambiguateTypes.Environment.empty) in
155 MatitaSync.set_proof_aliases status [k,value]
157 "\n",new_status,((new_status, (new_text, dummy_st))::acc)
158 ) (initial_space,status,[]) new_aliases in
159 let parsed_text = initial_space ^ parsed_text in
161 List.rev new_status_and_text_list_rev @ new_status_and_text_list' @
162 [new_status, (parsed_text, st)]
164 res,parsed_text_length
166 let eval_with_engine guistuff status user_goal parsed_text st =
168 eval_with_engine guistuff status user_goal parsed_text st
170 | GrafiteParserMisc.UnableToInclude what
171 | GrafiteEngine.IncludedFileNotCompiled what as exc ->
172 let compile_needed_and_go_on d =
175 while Glib.Main.pending () do ignore(Glib.Main.iteration false); done
177 if not(MatitamakeLib.build_development_in_bg ~target refresh_cb d) then
180 eval_with_engine guistuff status user_goal parsed_text st
182 let do_nothing () = [], 0 in
183 let handle_with_devel d =
184 let name = MatitamakeLib.name_for_development d in
185 let title = "Unable to include " ^ what in
187 what ^ " is handled by development <b>" ^ name ^ "</b>.\n\n" ^
188 "<i>Should I compile it and Its dependencies?</i>"
190 (match guistuff.ask_confirmation ~title ~message with
191 | `YES -> compile_needed_and_go_on d
193 | `CANCEL -> do_nothing ())
195 let handle_without_devel filename =
196 let title = "Unable to include " ^ what in
198 what ^ " is <b>not</b> handled by a development.\n" ^
199 "All dependencies are automatically solved for a development.\n\n" ^
200 "<i>Do you want to set up a development?</i>"
202 (match guistuff.ask_confirmation ~title ~message with
206 guistuff.develcreator ~containing:(Some (Filename.dirname f))
207 | None -> guistuff.develcreator ~containing:None);
210 | `CANCEL -> do_nothing())
212 match guistuff.filenamedata with
213 | None,None -> handle_without_devel None
214 | None,Some d -> handle_with_devel d
216 match MatitamakeLib.development_for_dir (Filename.dirname f) with
217 | None -> handle_without_devel (Some f)
218 | Some d -> handle_with_devel d
221 let disambiguate_macro_term term status user_goal =
222 let module MD = MatitaDisambiguator in
223 let dbd = LibraryDb.instance () in
224 let metasenv = GrafiteTypes.get_proof_metasenv status in
225 let context = GrafiteTypes.get_proof_context status user_goal in
227 MD.disambiguate_term ~dbd ~context ~metasenv ~aliases:status.aliases
228 ~universe:(Some status.multi_aliases) term in
233 let pp_eager_statement_ast =
234 GrafiteAstPp.pp_statement ~term_pp:CicNotationPp.pp_term
235 ~lazy_term_pp:(fun _ -> assert false) ~obj_pp:(fun _ -> assert false)
237 let eval_macro guistuff status user_goal unparsed_text parsed_text script mac =
238 let module TAPp = GrafiteAstPp in
239 let module MQ = MetadataQuery in
240 let module MDB = LibraryDb in
241 let module CTC = CicTypeChecker in
242 let module CU = CicUniv in
243 (* no idea why ocaml wants this *)
244 let parsed_text_length = String.length parsed_text in
245 let dbd = LibraryDb.instance () in
246 (* XXX use a real CIC -> string pretty printer *)
247 let pp_macro = TAPp.pp_macro ~term_pp:CicPp.ppterm in
250 | TA.WMatch (loc, term) ->
251 let term = disambiguate_macro_term term status user_goal in
252 let l = Whelp.match_term ~dbd term in
254 MatitaMisc.strip_suffix ~suffix:"."
255 (HExtlib.trim_blanks unparsed_text)
257 let entry = `Whelp (query_url, l) in
258 guistuff.mathviewer#show_uri_list ~reuse:true ~entry l;
259 [], parsed_text_length
260 | TA.WInstance (loc, term) ->
261 let term = disambiguate_macro_term term status user_goal in
262 let l = Whelp.instance ~dbd term in
263 let entry = `Whelp (pp_macro (TA.WInstance (loc, term)), l) in
264 guistuff.mathviewer#show_uri_list ~reuse:true ~entry l;
265 [], parsed_text_length
266 | TA.WLocate (loc, s) ->
267 let l = Whelp.locate ~dbd s in
268 let entry = `Whelp (pp_macro (TA.WLocate (loc, s)), l) in
269 guistuff.mathviewer#show_uri_list ~reuse:true ~entry l;
270 [], parsed_text_length
271 | TA.WElim (loc, term) ->
272 let term = disambiguate_macro_term term status user_goal in
275 | Cic.MutInd (uri,n,_) -> UriManager.uri_of_uriref uri n None
276 | _ -> failwith "Not a MutInd"
278 let l = Whelp.elim ~dbd uri in
279 let entry = `Whelp (pp_macro (TA.WElim (loc, term)), l) in
280 guistuff.mathviewer#show_uri_list ~reuse:true ~entry l;
281 [], parsed_text_length
282 | TA.WHint (loc, term) ->
283 let term = disambiguate_macro_term term status user_goal in
284 let s = ((None,[0,[],term], Cic.Meta (0,[]) ,term),0) in
285 let l = List.map fst (MQ.experimental_hint ~dbd s) in
286 let entry = `Whelp (pp_macro (TA.WHint (loc, term)), l) in
287 guistuff.mathviewer#show_uri_list ~reuse:true ~entry l;
288 [], parsed_text_length
291 let proof = GrafiteTypes.get_current_proof status in
292 let proof_status = proof, user_goal in
293 let l = List.map fst (MQ.experimental_hint ~dbd proof_status) in
294 let selected = guistuff.urichooser l in
296 | [] -> [], parsed_text_length
298 let suri = UriManager.string_of_uri uri in
300 TA.Executable (loc, (TA.Tactical (loc,
302 TA.Apply (loc, CicNotationPt.Uri (suri, None))),
306 GrafiteEngine.eval_ast
307 ~baseuri_of_script:(fun _ -> assert false)
308 ~disambiguate_tactic:GrafiteDisambiguate.disambiguate_tactic
309 ~disambiguate_command:GrafiteDisambiguate.disambiguate_command
312 comment parsed_text ^ "\n" ^ pp_eager_statement_ast ast in
313 [ new_status , (extra_text, ast) ], parsed_text_length
316 "The result of the urichooser should be only 1 uri, not:\n";
318 fun u -> HLog.error (UriManager.string_of_uri u ^ "\n")
321 | TA.Check (_,term) ->
322 let metasenv = GrafiteTypes.get_proof_metasenv status in
323 let context = GrafiteTypes.get_proof_context status user_goal in
325 MatitaDisambiguator.disambiguate_term ~dbd ~context ~metasenv
326 ~aliases:status.aliases ~universe:(Some status.multi_aliases) term
328 let _, metasenv , term, ugraph =
333 let ty,_ = CTC.type_of_aux' metasenv context term ugraph in
334 let t_and_ty = Cic.Cast (term,ty) in
335 guistuff.mathviewer#show_entry (`Cic (t_and_ty,metasenv));
336 [], parsed_text_length
339 let status = script#status.proof_status in
342 | _ -> script#retract ();go_back()
344 [], parsed_text_length, Some go_back
345 | TA.Redo (_, Some i) -> [], parsed_text_length,
346 Some (fun () -> for j = 1 to i do advance () done)
347 | TA.Redo (_, None) -> [], parsed_text_length,
348 Some (fun () -> advance ())
349 | TA.Undo (_, Some i) -> [], parsed_text_length,
350 Some (fun () -> for j = 1 to i do script#retract () done)
351 | TA.Undo (_, None) -> [], parsed_text_length,
352 Some (fun () -> script#retract ()) *)
354 | TA.Quit _ -> failwith "not implemented"
355 | TA.Print (_,kind) -> failwith "not implemented"
356 | TA.Search_pat (_, search_kind, str) -> failwith "not implemented"
357 | TA.Search_term (_, search_kind, term) -> failwith "not implemented"
359 let eval_executable guistuff status user_goal unparsed_text parsed_text script
362 let module TAPp = GrafiteAstPp in
363 let module MD = MatitaDisambiguator in
364 let module ML = MatitaMisc in
366 | TA.Command (loc, _) | TA.Tactical (loc, _, _) ->
368 (match GrafiteParserMisc.baseuri_of_baseuri_decl (TA.Executable (loc,ex)) with
371 if not (GrafiteMisc.is_empty u) then
373 guistuff.ask_confirmation
374 ~title:"Baseuri redefinition"
376 "Baseuri " ^ u ^ " already exists.\n" ^
377 "Do you want to redefine the corresponding "^
378 "part of the library?")
381 let basedir = Helm_registry.get "matita.basedir" in
382 LibraryClean.clean_baseuris ~basedir [u]
384 | `CANCEL -> raise MatitaTypes.Cancel);
386 guistuff status user_goal parsed_text (TA.Executable (loc, ex))
387 with MatitaTypes.Cancel -> [], 0)
388 | TA.Macro (_,mac) ->
389 eval_macro guistuff status user_goal unparsed_text parsed_text script mac
391 let rec eval_statement (buffer : GText.buffer) guistuff status user_goal
394 let st, unparsed_text =
397 if Pcre.pmatch ~rex:only_dust_RE text then raise Margin;
398 GrafiteParser.parse_statement (Ulexing.from_utf8_string text), text
399 | `Ast (st, text) -> st, text
401 let text_of_loc loc =
402 let parsed_text_length = snd (HExtlib.loc_of_floc loc) in
403 let parsed_text = safe_substring unparsed_text 0 parsed_text_length in
404 parsed_text, parsed_text_length
407 | GrafiteAst.Comment (loc, _) ->
408 let parsed_text, parsed_text_length = text_of_loc loc in
409 let remain_len = String.length unparsed_text - parsed_text_length in
410 let s = String.sub unparsed_text parsed_text_length remain_len in
413 eval_statement buffer guistuff status user_goal script
416 HExtlib.Localized (floc, exn) ->
417 HExtlib.raise_localized_exception ~offset:parsed_text_length floc exn
418 | MatitaDisambiguator.DisambiguationError (offset,errorll) ->
420 (MatitaDisambiguator.DisambiguationError
421 (offset+parsed_text_length, errorll))
424 | (status, (text, ast)) :: tl ->
425 ((status, (parsed_text ^ text, ast))::tl), (parsed_text_length + len)
427 | GrafiteAst.Executable (loc, ex) ->
428 let parsed_text, parsed_text_length = text_of_loc loc in
429 eval_executable guistuff status user_goal unparsed_text parsed_text
432 let fresh_script_id =
436 class script ~(source_view: GSourceView.source_view)
437 ~(mathviewer: MatitaTypes.mathViewer)
443 let buffer = source_view#buffer in
444 let source_buffer = source_view#source_buffer in
446 val scriptId = fresh_script_id ()
449 mathviewer = mathviewer;
450 urichooser = urichooser;
451 ask_confirmation = ask_confirmation;
452 develcreator = develcreator;
453 filenamedata = (None, None)}
455 method private getFilename =
456 match guistuff.filenamedata with Some f,_ -> f | _ -> assert false
458 method filename = self#getFilename
460 method private ppFilename =
461 match guistuff.filenamedata with
463 | None,_ -> sprintf ".unnamed%d.ma" scriptId
466 ignore (GMain.Timeout.add ~ms:300000
467 ~callback:(fun _ -> self#_saveToBackupFile ();true));
468 ignore (buffer#connect#modified_changed
469 (fun _ -> set_star (Filename.basename self#ppFilename) buffer#modified))
471 val mutable statements = []; (** executed statements *)
472 val mutable history = [ MatitaSync.init () ];
473 (** list of states before having executed statements. Head element of this
474 * list is the current state, last element is the state at the beginning of
476 * Invariant: this list length is 1 + length of statements *)
478 (** goal as seen by the user (i.e. metano corresponding to current tab) *)
479 val mutable userGoal = ~-1
481 (** text mark and tag representing locked part of a script *)
483 buffer#create_mark ~name:"locked" ~left_gravity:true buffer#start_iter
484 val locked_tag = buffer#create_tag [`BACKGROUND "lightblue"; `EDITABLE false]
485 val error_tag = buffer#create_tag [`UNDERLINE `SINGLE; `FOREGROUND "red"]
487 method locked_mark = locked_mark
488 method locked_tag = locked_tag
489 method error_tag = error_tag
491 (* history can't be empty, the invariant above grant that it contains at
492 * least the init status *)
493 method status = match history with hd :: _ -> hd | _ -> assert false
495 method private _advance ?statement () =
497 let (entries, parsed_len) =
498 eval_statement buffer guistuff self#status userGoal self st
500 let (new_statuses, new_statements, new_asts) =
501 let statuses, statements = List.split entries in
502 let texts, asts = List.split statements in
503 statuses, texts, asts
505 history <- List.rev new_statuses @ history;
506 statements <- List.rev new_statements @ statements;
507 let start = buffer#get_iter_at_mark (`MARK locked_mark) in
508 let new_text = String.concat "" new_statements in
509 if statement <> None then
510 buffer#insert ~iter:start new_text
512 let s = match st with `Raw s | `Ast (_, s) -> s in
513 if new_text <> String.sub s 0 parsed_len then
515 let stop = start#copy#forward_chars parsed_len in
516 buffer#delete ~start ~stop;
517 buffer#insert ~iter:start new_text;
519 self#moveMark (String.length new_text);
521 (match List.rev new_asts with (* advance again on punctuation *)
522 | TA.Executable (_, TA.Tactical (_, tac, _)) :: _ ->
524 (buffer#get_iter_at_mark (`MARK locked_mark))#offset
526 let text = self#getFuture in
528 (match parse_statement baseoffset 0 buffer text with
529 | TA.Executable (loc, TA.Tactical (_, tac, None)) as st
530 when GrafiteAst.is_punctuation tac ->
531 let len = snd (CicNotationPt.loc_of_floc loc) in
532 aux (`Ast (st, String.sub text 0 len))
534 with CicNotationParser.Parse_error _ | End_of_file -> ())
538 let s = match statement with Some s -> s | None -> self#getFuture in
539 HLog.debug ("evaluating: " ^ first_line s ^ " ...");
540 (try aux (`Raw s) with End_of_file -> raise Margin)
542 method private _retract offset status new_statements new_history =
543 let cur_status = match history with s::_ -> s | [] -> assert false in
544 MatitaSync.time_travel ~present:cur_status ~past:status;
545 statements <- new_statements;
546 history <- new_history;
547 self#moveMark (- offset)
549 method advance ?statement () =
551 self#_advance ?statement ();
554 | Margin -> self#notify
555 | exc -> self#notify; raise exc
559 let cmp,new_statements,new_history,status =
560 match statements,history with
561 stat::statements, _::(status::_ as history) ->
562 String.length stat, statements, history, status
563 | [],[_] -> raise Margin
564 | _,_ -> assert false
566 self#_retract cmp status new_statements new_history;
569 | Margin -> self#notify
570 | exc -> self#notify; raise exc
572 method private getFuture =
573 buffer#get_text ~start:(buffer#get_iter_at_mark (`MARK locked_mark))
574 ~stop:buffer#end_iter ()
577 (** @param rel_offset relative offset from current position of locked_mark *)
578 method private moveMark rel_offset =
579 let mark = `MARK locked_mark in
580 let old_insert = buffer#get_iter_at_mark `INSERT in
581 buffer#remove_tag locked_tag ~start:buffer#start_iter ~stop:buffer#end_iter;
582 let current_mark_pos = buffer#get_iter_at_mark mark in
584 match rel_offset with
585 | 0 -> current_mark_pos
586 | n when n > 0 -> current_mark_pos#forward_chars n
587 | n (* when n < 0 *) -> current_mark_pos#backward_chars (abs n)
589 buffer#move_mark mark ~where:new_mark_pos;
590 buffer#apply_tag locked_tag ~start:buffer#start_iter ~stop:new_mark_pos;
591 buffer#move_mark `INSERT old_insert;
592 let mark_position = buffer#get_iter_at_mark mark in
593 if source_view#move_mark_onscreen mark then
595 buffer#move_mark mark mark_position;
596 source_view#scroll_to_mark ~use_align:true ~xalign:1.0 ~yalign:0.1 mark;
598 while Glib.Main.pending () do ignore(Glib.Main.iteration false); done
600 method clean_dirty_lock =
601 let lock_mark_iter = buffer#get_iter_at_mark (`MARK locked_mark) in
602 buffer#remove_tag locked_tag ~start:buffer#start_iter ~stop:buffer#end_iter;
603 buffer#apply_tag locked_tag ~start:buffer#start_iter ~stop:lock_mark_iter
605 val mutable observers = []
607 method addObserver (o: GrafiteTypes.status -> unit) =
608 observers <- o :: observers
610 method private notify =
611 let status = self#status in
612 List.iter (fun o -> o status) observers
614 method loadFromFile f =
615 buffer#set_text (HExtlib.input_file f);
617 buffer#set_modified false
619 method assignFileName file =
620 let abspath = MatitaMisc.absolute_path file in
621 let devel = MatitamakeLib.development_for_dir (Filename.dirname abspath) in
622 guistuff.filenamedata <- Some abspath, devel
624 method saveToFile () =
625 let oc = open_out self#getFilename in
626 output_string oc (buffer#get_text ~start:buffer#start_iter
627 ~stop:buffer#end_iter ());
629 buffer#set_modified false
631 method private _saveToBackupFile () =
632 if buffer#modified then
634 let f = self#ppFilename ^ "~" in
635 let oc = open_out f in
636 output_string oc (buffer#get_text ~start:buffer#start_iter
637 ~stop:buffer#end_iter ());
639 HLog.debug ("backup " ^ f ^ " saved")
642 method private goto_top =
644 let rec last x = function
646 | hd::tl -> last hd tl
648 last self#status history
650 (* FIXME: this is not correct since there is no undo for
651 * library_objects.set_default... *)
652 MatitaSync.time_travel ~present:self#status ~past:init
654 method private reset_buffer =
656 history <- [ MatitaSync.init () ];
659 buffer#remove_tag locked_tag ~start:buffer#start_iter ~stop:buffer#end_iter;
660 buffer#move_mark (`MARK locked_mark) ~where:buffer#start_iter
664 source_buffer#begin_not_undoable_action ();
665 buffer#delete ~start:buffer#start_iter ~stop:buffer#end_iter;
666 source_buffer#end_not_undoable_action ();
667 buffer#set_modified false;
670 let template = HExtlib.input_file BuildTimeConf.script_template in
671 buffer#insert ~iter:(buffer#get_iter `START) template;
672 guistuff.filenamedata <-
673 (None,MatitamakeLib.development_for_dir (Unix.getcwd ()));
674 buffer#set_modified false;
675 set_star (Filename.basename self#ppFilename) false
677 method goto (pos: [`Top | `Bottom | `Cursor]) () =
678 let old_locked_mark =
680 (buffer#create_mark ~name:"old_locked_mark"
681 ~left_gravity:true (buffer#get_iter_at_mark (`MARK locked_mark))) in
682 let getpos _ = buffer#get_iter_at_mark (`MARK locked_mark) in
683 let getoldpos _ = buffer#get_iter_at_mark old_locked_mark in
684 let dispose_old_locked_mark () = buffer#delete_mark old_locked_mark in
687 dispose_old_locked_mark ();
695 let newpos = getpos () in
696 if (getoldpos ())#compare newpos < 0 then
698 buffer#move_mark old_locked_mark newpos;
703 dispose_old_locked_mark ();
706 | Margin -> dispose_old_locked_mark (); self#notify
707 | exc -> dispose_old_locked_mark (); self#notify; raise exc)
709 let locked_iter () = buffer#get_iter_at_mark (`NAME "locked") in
710 let cursor_iter () = buffer#get_iter_at_mark `INSERT in
713 (buffer#create_mark ~name:"initial_insert"
714 ~left_gravity:true (cursor_iter ())) in
715 let dispose_remember () = buffer#delete_mark remember in
716 let remember_iter () =
717 buffer#get_iter_at_mark (`NAME "initial_insert") in
718 let cmp () = (locked_iter ())#offset - (remember_iter ())#offset in
720 let forward_until_cursor () = (* go forward until locked > cursor *)
723 if cmp () < 0 && (getoldpos ())#compare (getpos ()) < 0
726 buffer#move_mark old_locked_mark (getpos ());
732 let rec back_until_cursor len = (* go backward until locked < cursor *)
734 statements, (status::_ as history) when len <= 0 ->
735 self#_retract (icmp - len) status statements history
736 | statement::tl1, _::tl2 ->
737 back_until_cursor (len - String.length statement) (tl1,tl2)
738 | _,_ -> assert false
742 if icmp < 0 then (* locked < cursor *)
743 (forward_until_cursor (); self#notify)
744 else if icmp > 0 then (* locked > cursor *)
745 (back_until_cursor icmp (statements,history); self#notify)
746 else (* cursor = locked *)
750 dispose_old_locked_mark ();
752 | Margin -> dispose_remember (); dispose_old_locked_mark (); self#notify
753 | exc -> dispose_remember (); dispose_old_locked_mark ();
754 self#notify; raise exc)
756 method onGoingProof () =
757 match self#status.proof_status with
758 | No_proof | Proof _ -> false
759 | Incomplete_proof _ -> true
760 | Intermediate _ -> assert false
762 (* method proofStatus = MatitaTypes.get_proof_status self#status *)
763 method proofMetasenv = GrafiteTypes.get_proof_metasenv self#status
764 method proofContext = GrafiteTypes.get_proof_context self#status userGoal
765 method proofConclusion= GrafiteTypes.get_proof_conclusion self#status userGoal
766 method stack = GrafiteTypes.get_stack self#status
767 method setGoal n = userGoal <- n
768 method goal = userGoal
771 let s = self#getFuture in
772 let rec is_there_and_executable s =
773 if Pcre.pmatch ~rex:only_dust_RE s then raise Margin;
774 let st = GrafiteParser.parse_statement (Ulexing.from_utf8_string s) in
776 | GrafiteAst.Comment (loc,_)->
777 let parsed_text_length = snd (HExtlib.loc_of_floc loc) in
778 let remain_len = String.length s - parsed_text_length in
779 let next = String.sub s parsed_text_length remain_len in
780 is_there_and_executable next
781 | GrafiteAst.Executable (loc, ex) -> false
784 is_there_and_executable s
786 | CicNotationParser.Parse_error _ -> false
787 | Margin | End_of_file -> true
791 HLog.debug "script status:";
792 HLog.debug ("history size: " ^ string_of_int (List.length history));
793 HLog.debug (sprintf "%d statements:" (List.length statements));
794 List.iter HLog.debug statements;
795 HLog.debug ("Current file name: " ^
796 (match guistuff.filenamedata with
797 |None,_ -> "[ no name ]"
802 let _script = ref None
804 let script ~source_view ~mathviewer ~urichooser ~develcreator ~ask_confirmation ~set_star ()
807 ~source_view ~mathviewer ~ask_confirmation ~urichooser ~develcreator ~set_star ()
812 let current () = match !_script with None -> assert false | Some s -> s