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/
31 module TA = GrafiteAst
34 let debug_print = if debug then prerr_endline else ignore
36 (** raised when one of the script margins (top or bottom) is reached *)
38 exception NoUnfinishedProof
39 exception ActionCancelled of string
41 let safe_substring s i j =
42 try String.sub s i j with Invalid_argument _ -> assert false
44 let heading_nl_RE = Pcre.regexp "^\\s*\n\\s*"
45 let heading_nl_RE' = Pcre.regexp "^(\\s*\n\\s*)"
46 let only_dust_RE = Pcre.regexp "^(\\s|\n|%%[^\n]*\n)*$"
47 let multiline_RE = Pcre.regexp "^\n[^\n]+$"
48 let newline_RE = Pcre.regexp "\n"
51 if Pcre.pmatch ~rex:multiline_RE str then
52 "\n(** " ^ (Pcre.replace ~rex:newline_RE str) ^ " *)"
54 "\n(**\n" ^ str ^ "\n*)"
57 let s = Pcre.replace ~rex:heading_nl_RE s in
59 let nl_pos = String.index s '\n' in
63 (** creates a statement AST for the Goal tactic, e.g. "goal 7" *)
65 let module A = GrafiteAst in
66 let loc = HExtlib.dummy_floc in
67 A.Executable (loc, A.Tactical (loc,
68 A.Tactic (loc, A.Goal (loc, n)),
72 mathviewer:MatitaTypes.mathViewer;
73 urichooser: UriManager.uri list -> UriManager.uri list;
74 ask_confirmation: title:string -> message:string -> [`YES | `NO | `CANCEL];
75 develcreator: containing:string option -> unit;
76 mutable filenamedata: string option * MatitamakeLib.development option
79 let eval_with_engine guistuff lexicon_status grafite_status user_goal
82 let module TAPp = GrafiteAstPp in
83 let parsed_text_length = String.length parsed_text in
84 let initial_space,parsed_text =
86 let pieces = Pcre.extract ~rex:heading_nl_RE' parsed_text in
87 let p1 = pieces.(1) in
88 let p1_len = String.length p1 in
89 let rest = String.sub parsed_text p1_len (parsed_text_length - p1_len) in
92 Not_found -> "", parsed_text in
93 let inital_space,new_grafite_status,new_lexicon_status,new_status_and_text_list' =
94 (* the code commented out adds the "select" command if needed *)
95 initial_space,grafite_status,lexicon_status,[] in
97 match st with TA.Executable (loc,ex) -> loc, ex | _ -> assert false in
98 match grafite_status.proof_status with
99 | Incomplete_proof { stack = stack }
100 when not (List.mem user_goal (Continuationals.head_goals stack)) ->
102 MatitaEngine.eval_ast
103 ~do_heavy_checks:true grafite_status (goal_ast user_goal)
105 let initial_space = if initial_space = "" then "\n" else initial_space
107 "\n", grafite_status,
109 initial_space ^ TAPp.pp_tactical (TA.Select (loc, [user_goal])) ]
110 | _ -> initial_space,grafite_status,[] in *)
111 let enriched_history_fragment =
112 MatitaEngine.eval_ast ~do_heavy_checks:true
113 new_lexicon_status new_grafite_status st
115 let _,new_text_list_rev =
116 let module DTE = DisambiguateTypes.Environment in
117 let module UM = UriManager in
119 fun (_,alias) (initial_space,acc) ->
121 None -> initial_space,initial_space::acc
122 | Some (k,((v,_) as value)) ->
125 if initial_space = "" then "\n" else initial_space
128 DisambiguatePp.pp_environment
129 (DisambiguateTypes.Environment.add k value
130 DisambiguateTypes.Environment.empty)
133 ) enriched_history_fragment (initial_space,[]) in
134 let new_text_list_rev =
135 match enriched_history_fragment,new_text_list_rev with
136 (_,None)::_, initial_space::tl -> (initial_space ^ parsed_text)::tl
137 | _,_ -> assert false
141 List.combine (fst (List.split enriched_history_fragment)) new_text_list_rev
143 Invalid_argument _ -> assert false
145 res,parsed_text_length
147 let wrap_with_developments guistuff f arg =
151 | DependenciesParser.UnableToInclude what
152 | LexiconEngine.IncludedFileNotCompiled what
153 | GrafiteEngine.IncludedFileNotCompiled what as exc ->
154 let compile_needed_and_go_on d =
155 let target = Pcre.replace ~pat:"lexicon$" ~templ:"moo" what in
157 while Glib.Main.pending () do ignore(Glib.Main.iteration false); done
159 if not(MatitamakeLib.build_development_in_bg ~target refresh_cb d) then
164 let do_nothing () = raise (ActionCancelled "Inclusion not performed") in
165 let handle_with_devel d =
166 let name = MatitamakeLib.name_for_development d in
167 let title = "Unable to include " ^ what in
169 what ^ " is handled by development <b>" ^ name ^ "</b>.\n\n" ^
170 "<i>Should I compile it and Its dependencies?</i>"
172 (match guistuff.ask_confirmation ~title ~message with
173 | `YES -> compile_needed_and_go_on d
175 | `CANCEL -> do_nothing ())
177 let handle_without_devel filename =
178 let title = "Unable to include " ^ what in
180 what ^ " is <b>not</b> handled by a development.\n" ^
181 "All dependencies are automatically solved for a development.\n\n" ^
182 "<i>Do you want to set up a development?</i>"
184 (match guistuff.ask_confirmation ~title ~message with
188 guistuff.develcreator ~containing:(Some (Filename.dirname f))
189 | None -> guistuff.develcreator ~containing:None);
192 | `CANCEL -> do_nothing())
194 match guistuff.filenamedata with
195 | None,None -> handle_without_devel None
196 | None,Some d -> handle_with_devel d
198 match MatitamakeLib.development_for_dir (Filename.dirname f) with
199 | None -> handle_without_devel (Some f)
200 | Some d -> handle_with_devel d
204 guistuff lexicon_status grafite_status user_goal parsed_text st
206 wrap_with_developments guistuff
208 guistuff lexicon_status grafite_status user_goal parsed_text) st
211 let pp_eager_statement_ast =
212 GrafiteAstPp.pp_statement ~term_pp:CicNotationPp.pp_term
213 ~lazy_term_pp:(fun _ -> assert false) ~obj_pp:(fun _ -> assert false)
215 let rec eval_macro include_paths (buffer : GText.buffer) guistuff lexicon_status grafite_status user_goal unparsed_text parsed_text script mac =
216 let module TAPp = GrafiteAstPp in
217 let module MQ = MetadataQuery in
218 let module MDB = LibraryDb in
219 let module CTC = CicTypeChecker in
220 let module CU = CicUniv in
221 (* no idea why ocaml wants this *)
222 let parsed_text_length = String.length parsed_text in
223 let dbd = LibraryDb.instance () in
224 (* XXX use a real CIC -> string pretty printer *)
225 let pp_macro = TAPp.pp_macro ~term_pp:CicPp.ppterm in
228 | TA.WMatch (loc, term) ->
229 let l = Whelp.match_term ~dbd term in
231 MatitaMisc.strip_suffix ~suffix:"."
232 (HExtlib.trim_blanks unparsed_text)
234 let entry = `Whelp (query_url, l) in
235 guistuff.mathviewer#show_uri_list ~reuse:true ~entry l;
236 [], parsed_text_length
237 | TA.WInstance (loc, term) ->
238 let l = Whelp.instance ~dbd term in
239 let entry = `Whelp (pp_macro (TA.WInstance (loc, term)), l) in
240 guistuff.mathviewer#show_uri_list ~reuse:true ~entry l;
241 [], parsed_text_length
242 | TA.WLocate (loc, s) ->
243 let l = Whelp.locate ~dbd s in
244 let entry = `Whelp (pp_macro (TA.WLocate (loc, s)), l) in
245 guistuff.mathviewer#show_uri_list ~reuse:true ~entry l;
246 [], parsed_text_length
247 | TA.WElim (loc, term) ->
250 | Cic.MutInd (uri,n,_) -> UriManager.uri_of_uriref uri n None
251 | _ -> failwith "Not a MutInd"
253 let l = Whelp.elim ~dbd uri in
254 let entry = `Whelp (pp_macro (TA.WElim (loc, term)), l) in
255 guistuff.mathviewer#show_uri_list ~reuse:true ~entry l;
256 [], parsed_text_length
257 | TA.WHint (loc, term) ->
258 let s = ((None,[0,[],term], Cic.Meta (0,[]) ,term),0) in
259 let l = List.map fst (MQ.experimental_hint ~dbd s) in
260 let entry = `Whelp (pp_macro (TA.WHint (loc, term)), l) in
261 guistuff.mathviewer#show_uri_list ~reuse:true ~entry l;
262 [], parsed_text_length
268 | None -> raise NoUnfinishedProof
270 let proof = GrafiteTypes.get_current_proof grafite_status in
271 let proof_status = proof,user_goal' in
272 let l = List.map fst (MQ.experimental_hint ~dbd proof_status) in
273 let selected = guistuff.urichooser l in
275 | [] -> [], parsed_text_length
277 let suri = UriManager.string_of_uri uri in
279 TA.Executable (loc, (TA.Tactical (loc,
281 TA.Apply (loc, CicNotationPt.Uri (suri, None))),
282 Some (TA.Dot loc)))) in
284 comment parsed_text ^ "\n" ^
285 pp_eager_statement_ast (ast HExtlib.dummy_floc) in
286 let text_len = String.length text in
287 let loc = HExtlib.floc_of_loc (0,text_len) in
288 let statement = `Ast (GrafiteParser.LSome (ast loc),text) in
289 let res,_parsed_text_len =
290 eval_statement include_paths buffer guistuff lexicon_status
291 grafite_status user_goal script statement
293 (* we need to replace all the parsed_text *)
294 res,String.length parsed_text
297 "The result of the urichooser should be only 1 uri, not:\n";
299 fun u -> HLog.error (UriManager.string_of_uri u ^ "\n")
302 | TA.Check (_,term) ->
303 let metasenv = GrafiteTypes.get_proof_metasenv grafite_status in
307 | Some n -> GrafiteTypes.get_proof_context grafite_status n in
308 let ty,_ = CTC.type_of_aux' metasenv context term CicUniv.empty_ugraph in
309 let t_and_ty = Cic.Cast (term,ty) in
310 guistuff.mathviewer#show_entry (`Cic (t_and_ty,metasenv));
311 [], parsed_text_length
313 | TA.Quit _ -> failwith "not implemented"
314 | TA.Print (_,kind) -> failwith "not implemented"
315 | TA.Search_pat (_, search_kind, str) -> failwith "not implemented"
316 | TA.Search_term (_, search_kind, term) -> failwith "not implemented"
318 and eval_executable include_paths (buffer : GText.buffer) guistuff lexicon_status grafite_status user_goal unparsed_text parsed_text script loc ex
320 let module TAPp = GrafiteAstPp in
321 let module MD = GrafiteDisambiguator in
322 let module ML = MatitaMisc in
326 | TA.Command (_,TA.Set (_,"baseuri",u)) ->
327 if Http_getter_storage.is_read_only u then
328 raise (ActionCancelled ("baseuri " ^ u ^ " is readonly"));
329 if not (Http_getter_storage.is_empty u) then
331 guistuff.ask_confirmation
332 ~title:"Baseuri redefinition"
334 "Baseuri " ^ u ^ " already exists.\n" ^
335 "Do you want to redefine the corresponding "^
336 "part of the library?")
338 | `YES -> LibraryClean.clean_baseuris [u]
340 | `CANCEL -> raise MatitaTypes.Cancel)
344 guistuff lexicon_status grafite_status user_goal parsed_text
345 (TA.Executable (loc, ex))
347 MatitaTypes.Cancel -> [], 0
348 | GrafiteEngine.Macro (_loc,lazy_macro) ->
352 | Some n -> GrafiteTypes.get_proof_context grafite_status n in
353 let grafite_status,macro = lazy_macro context in
354 eval_macro include_paths buffer guistuff lexicon_status grafite_status
355 user_goal unparsed_text parsed_text script macro
357 and eval_statement include_paths (buffer : GText.buffer) guistuff lexicon_status
358 grafite_status user_goal script statement
360 let (lexicon_status,st), unparsed_text =
363 if Pcre.pmatch ~rex:only_dust_RE text then raise Margin;
365 wrap_with_developments guistuff
366 (GrafiteParser.parse_statement
367 (Ulexing.from_utf8_string text) ~include_paths) lexicon_status
370 | `Ast (st, text) -> (lexicon_status, st), text
372 let text_of_loc loc =
373 let parsed_text_length = snd (HExtlib.loc_of_floc loc) in
374 let parsed_text = safe_substring unparsed_text 0 parsed_text_length in
375 parsed_text, parsed_text_length
378 | GrafiteParser.LNone loc ->
379 let parsed_text, parsed_text_length = text_of_loc loc in
380 [(grafite_status,lexicon_status),parsed_text],
382 | GrafiteParser.LSome (GrafiteAst.Comment (loc, _)) ->
383 let parsed_text, parsed_text_length = text_of_loc loc in
384 let remain_len = String.length unparsed_text - parsed_text_length in
385 let s = String.sub unparsed_text parsed_text_length remain_len in
388 eval_statement include_paths buffer guistuff lexicon_status
389 grafite_status user_goal script (`Raw s)
391 HExtlib.Localized (floc, exn) ->
392 HExtlib.raise_localized_exception ~offset:parsed_text_length floc exn
393 | GrafiteDisambiguator.DisambiguationError (offset,errorll) ->
395 (GrafiteDisambiguator.DisambiguationError
396 (offset+parsed_text_length, errorll))
399 | (statuses,text)::tl ->
400 (statuses,parsed_text ^ text)::tl,parsed_text_length + len
402 | GrafiteParser.LSome (GrafiteAst.Executable (loc, ex)) ->
403 let parsed_text, parsed_text_length = text_of_loc loc in
404 eval_executable include_paths buffer guistuff lexicon_status
405 grafite_status user_goal unparsed_text parsed_text script loc ex
407 let fresh_script_id =
411 class script ~(source_view: GSourceView.source_view)
412 ~(mathviewer: MatitaTypes.mathViewer)
418 let buffer = source_view#buffer in
419 let source_buffer = source_view#source_buffer in
420 let initial_statuses () =
421 (* these include_paths are used only to load the initial notation *)
423 Helm_registry.get_list Helm_registry.string "matita.includes" in
425 CicNotation2.load_notation ~include_paths
426 BuildTimeConf.core_notation_script in
427 let grafite_status = GrafiteSync.init () in
428 grafite_status,lexicon_status
431 val mutable include_paths =
432 Helm_registry.get_list Helm_registry.string "matita.includes"
434 val scriptId = fresh_script_id ()
437 mathviewer = mathviewer;
438 urichooser = urichooser;
439 ask_confirmation = ask_confirmation;
440 develcreator = develcreator;
441 filenamedata = (None, None)}
443 method private getFilename =
444 match guistuff.filenamedata with Some f,_ -> f | _ -> assert false
446 method filename = self#getFilename
448 method private ppFilename =
449 match guistuff.filenamedata with
451 | None,_ -> sprintf ".unnamed%d.ma" scriptId
454 ignore (GMain.Timeout.add ~ms:300000
455 ~callback:(fun _ -> self#_saveToBackupFile ();true));
456 ignore (buffer#connect#modified_changed
457 (fun _ -> set_star (Filename.basename self#ppFilename) buffer#modified))
459 val mutable statements = [] (** executed statements *)
461 val mutable history = [ initial_statuses () ]
462 (** list of states before having executed statements. Head element of this
463 * list is the current state, last element is the state at the beginning of
465 * Invariant: this list length is 1 + length of statements *)
467 (** goal as seen by the user (i.e. metano corresponding to current tab) *)
468 val mutable userGoal = None
470 (** text mark and tag representing locked part of a script *)
472 buffer#create_mark ~name:"locked" ~left_gravity:true buffer#start_iter
473 val locked_tag = buffer#create_tag [`BACKGROUND "lightblue"; `EDITABLE false]
474 val error_tag = buffer#create_tag [`UNDERLINE `SINGLE; `FOREGROUND "red"]
476 method locked_mark = locked_mark
477 method locked_tag = locked_tag
478 method error_tag = error_tag
480 (* history can't be empty, the invariant above grant that it contains at
481 * least the init grafite_status *)
482 method grafite_status = match history with (s,_)::_ -> s | _ -> assert false
483 method lexicon_status = match history with (_,ss)::_ -> ss | _ -> assert false
485 method private _advance ?statement () =
486 let s = match statement with Some s -> s | None -> self#getFuture in
487 HLog.debug ("evaluating: " ^ first_line s ^ " ...");
488 let (entries, parsed_len) =
490 eval_statement include_paths buffer guistuff self#lexicon_status
491 self#grafite_status userGoal self (`Raw s)
492 with End_of_file -> raise Margin
494 let new_statuses, new_statements =
495 let statuses, texts = List.split entries in
498 history <- new_statuses @ history;
499 statements <- new_statements @ statements;
500 let start = buffer#get_iter_at_mark (`MARK locked_mark) in
501 let new_text = String.concat "" (List.rev new_statements) in
502 if statement <> None then
503 buffer#insert ~iter:start new_text
505 if new_text <> String.sub s 0 parsed_len then begin
506 buffer#delete ~start ~stop:(start#copy#forward_chars parsed_len);
507 buffer#insert ~iter:start new_text;
510 self#moveMark (String.length new_text);
511 (* here we need to set the Goal in case we are going to cursor (or to
512 bottom) and we will face a macro *)
513 match self#grafite_status.proof_status with
514 Incomplete_proof p ->
516 (try Some (Continuationals.Stack.find_goal p.stack)
517 with Failure _ -> None)
518 | _ -> userGoal <- None
520 method private _retract offset lexicon_status grafite_status new_statements
523 let cur_grafite_status,cur_lexicon_status =
524 match history with s::_ -> s | [] -> assert false
526 LexiconSync.time_travel ~present:cur_lexicon_status ~past:lexicon_status;
527 GrafiteSync.time_travel ~present:cur_grafite_status ~past:grafite_status;
528 statements <- new_statements;
529 history <- new_history;
530 self#moveMark (- offset)
532 method advance ?statement () =
534 self#_advance ?statement ();
537 | Margin -> self#notify
538 | exc -> self#notify; raise exc
542 let cmp,new_statements,new_history,(grafite_status,lexicon_status) =
543 match statements,history with
544 stat::statements, _::(status::_ as history) ->
545 String.length stat, statements, history, status
546 | [],[_] -> raise Margin
547 | _,_ -> assert false
549 self#_retract cmp lexicon_status grafite_status new_statements
553 | Margin -> self#notify
554 | exc -> self#notify; raise exc
556 method private getFuture =
557 buffer#get_text ~start:(buffer#get_iter_at_mark (`MARK locked_mark))
558 ~stop:buffer#end_iter ()
561 (** @param rel_offset relative offset from current position of locked_mark *)
562 method private moveMark rel_offset =
563 let mark = `MARK locked_mark in
564 let old_insert = buffer#get_iter_at_mark `INSERT in
565 buffer#remove_tag locked_tag ~start:buffer#start_iter ~stop:buffer#end_iter;
566 let current_mark_pos = buffer#get_iter_at_mark mark in
568 match rel_offset with
569 | 0 -> current_mark_pos
570 | n when n > 0 -> current_mark_pos#forward_chars n
571 | n (* when n < 0 *) -> current_mark_pos#backward_chars (abs n)
573 buffer#move_mark mark ~where:new_mark_pos;
574 buffer#apply_tag locked_tag ~start:buffer#start_iter ~stop:new_mark_pos;
575 buffer#move_mark `INSERT old_insert;
576 let mark_position = buffer#get_iter_at_mark mark in
577 if source_view#move_mark_onscreen mark then
579 buffer#move_mark mark mark_position;
580 source_view#scroll_to_mark ~use_align:true ~xalign:1.0 ~yalign:0.1 mark;
582 while Glib.Main.pending () do ignore(Glib.Main.iteration false); done
584 method clean_dirty_lock =
585 let lock_mark_iter = buffer#get_iter_at_mark (`MARK locked_mark) in
586 buffer#remove_tag locked_tag ~start:buffer#start_iter ~stop:buffer#end_iter;
587 buffer#apply_tag locked_tag ~start:buffer#start_iter ~stop:lock_mark_iter
589 val mutable observers = []
591 method addObserver (o: LexiconEngine.status -> GrafiteTypes.status -> unit) =
592 observers <- o :: observers
594 method private notify =
595 let lexicon_status = self#lexicon_status in
596 let grafite_status = self#grafite_status in
597 List.iter (fun o -> o lexicon_status grafite_status) observers
599 method loadFromFile f =
600 buffer#set_text (HExtlib.input_file f);
602 buffer#set_modified false
604 method assignFileName file =
605 let abspath = MatitaMisc.absolute_path file in
606 let dirname = Filename.dirname abspath in
607 let devel = MatitamakeLib.development_for_dir dirname in
608 guistuff.filenamedata <- Some abspath, devel;
610 match MatitamakeLib.development_for_dir dirname with
612 | Some devel -> [MatitamakeLib.root_for_development devel] in
614 include_ @ (Helm_registry.get_list Helm_registry.string "matita.includes")
616 include_paths <- include_
618 method saveToFile () =
619 let oc = open_out self#getFilename in
620 output_string oc (buffer#get_text ~start:buffer#start_iter
621 ~stop:buffer#end_iter ());
623 buffer#set_modified false
625 method private _saveToBackupFile () =
626 if buffer#modified then
628 let f = self#ppFilename ^ "~" in
629 let oc = open_out f in
630 output_string oc (buffer#get_text ~start:buffer#start_iter
631 ~stop:buffer#end_iter ());
633 HLog.debug ("backup " ^ f ^ " saved")
636 method private goto_top =
637 let grafite_status,lexicon_status =
638 let rec last x = function
640 | hd::tl -> last hd tl
642 last (self#grafite_status,self#lexicon_status) history
644 (* FIXME: this is not correct since there is no undo for
645 * library_objects.set_default... *)
646 GrafiteSync.time_travel ~present:self#grafite_status ~past:grafite_status;
647 LexiconSync.time_travel ~present:self#lexicon_status ~past:lexicon_status
649 method private reset_buffer =
651 history <- [ initial_statuses () ];
654 buffer#remove_tag locked_tag ~start:buffer#start_iter ~stop:buffer#end_iter;
655 buffer#move_mark (`MARK locked_mark) ~where:buffer#start_iter
659 source_buffer#begin_not_undoable_action ();
660 buffer#delete ~start:buffer#start_iter ~stop:buffer#end_iter;
661 source_buffer#end_not_undoable_action ();
662 buffer#set_modified false;
665 let template = HExtlib.input_file BuildTimeConf.script_template in
666 buffer#insert ~iter:(buffer#get_iter `START) template;
667 let development = MatitamakeLib.development_for_dir (Unix.getcwd ()) in
668 guistuff.filenamedata <- (None,development);
670 match development with
672 | Some devel -> [MatitamakeLib.root_for_development devel ]
675 include_ @ (Helm_registry.get_list Helm_registry.string "matita.includes")
677 include_paths <- include_ ;
678 buffer#set_modified false;
679 set_star (Filename.basename self#ppFilename) false
681 method goto (pos: [`Top | `Bottom | `Cursor]) () =
682 let old_locked_mark =
684 (buffer#create_mark ~name:"old_locked_mark"
685 ~left_gravity:true (buffer#get_iter_at_mark (`MARK locked_mark))) in
686 let getpos _ = buffer#get_iter_at_mark (`MARK locked_mark) in
687 let getoldpos _ = buffer#get_iter_at_mark old_locked_mark in
688 let dispose_old_locked_mark () = buffer#delete_mark old_locked_mark in
691 dispose_old_locked_mark ();
699 let newpos = getpos () in
700 if (getoldpos ())#compare newpos < 0 then
702 buffer#move_mark old_locked_mark newpos;
707 dispose_old_locked_mark ();
710 | Margin -> dispose_old_locked_mark (); self#notify
711 | exc -> dispose_old_locked_mark (); self#notify; raise exc)
713 let locked_iter () = buffer#get_iter_at_mark (`NAME "locked") in
714 let cursor_iter () = buffer#get_iter_at_mark `INSERT in
717 (buffer#create_mark ~name:"initial_insert"
718 ~left_gravity:true (cursor_iter ())) in
719 let dispose_remember () = buffer#delete_mark remember in
720 let remember_iter () =
721 buffer#get_iter_at_mark (`NAME "initial_insert") in
722 let cmp () = (locked_iter ())#offset - (remember_iter ())#offset in
724 let forward_until_cursor () = (* go forward until locked > cursor *)
727 if cmp () < 0 && (getoldpos ())#compare (getpos ()) < 0
730 buffer#move_mark old_locked_mark (getpos ());
736 let rec back_until_cursor len = (* go backward until locked < cursor *)
738 statements, ((grafite_status,lexicon_status)::_ as history)
740 self#_retract (icmp - len) lexicon_status grafite_status statements
742 | statement::tl1, _::tl2 ->
743 back_until_cursor (len - String.length statement) (tl1,tl2)
744 | _,_ -> assert false
748 if icmp < 0 then (* locked < cursor *)
749 (forward_until_cursor (); self#notify)
750 else if icmp > 0 then (* locked > cursor *)
751 (back_until_cursor icmp (statements,history); self#notify)
752 else (* cursor = locked *)
756 dispose_old_locked_mark ();
758 | Margin -> dispose_remember (); dispose_old_locked_mark (); self#notify
759 | exc -> dispose_remember (); dispose_old_locked_mark ();
760 self#notify; raise exc)
762 method onGoingProof () =
763 match self#grafite_status.proof_status with
764 | No_proof | Proof _ -> false
765 | Incomplete_proof _ -> true
766 | Intermediate _ -> assert false
768 (* method proofStatus = MatitaTypes.get_proof_status self#status *)
769 method proofMetasenv = GrafiteTypes.get_proof_metasenv self#grafite_status
771 method proofContext =
774 | Some n -> GrafiteTypes.get_proof_context self#grafite_status n
776 method proofConclusion =
780 GrafiteTypes.get_proof_conclusion self#grafite_status n
782 method stack = GrafiteTypes.get_stack self#grafite_status
783 method setGoal n = userGoal <- n
784 method goal = userGoal
787 let s = self#getFuture in
788 let rec is_there_only_comments lexicon_status s =
789 if Pcre.pmatch ~rex:only_dust_RE s then raise Margin;
790 let lexicon_status,st =
791 GrafiteParser.parse_statement (Ulexing.from_utf8_string s)
792 ~include_paths lexicon_status
795 | GrafiteParser.LSome (GrafiteAst.Comment (loc,_)) ->
796 let parsed_text_length = snd (HExtlib.loc_of_floc loc) in
797 let remain_len = String.length s - parsed_text_length in
798 let next = String.sub s parsed_text_length remain_len in
799 is_there_only_comments lexicon_status next
800 | GrafiteParser.LNone _
801 | GrafiteParser.LSome (GrafiteAst.Executable _) -> false
804 is_there_only_comments self#lexicon_status s
806 | HExtlib.Localized _
807 | CicNotationParser.Parse_error _ -> false
808 | Margin | End_of_file -> true
812 HLog.debug "script status:";
813 HLog.debug ("history size: " ^ string_of_int (List.length history));
814 HLog.debug (sprintf "%d statements:" (List.length statements));
815 List.iter HLog.debug statements;
816 HLog.debug ("Current file name: " ^
817 (match guistuff.filenamedata with
818 |None,_ -> "[ no name ]"
823 let _script = ref None
825 let script ~source_view ~mathviewer ~urichooser ~develcreator ~ask_confirmation ~set_star ()
828 ~source_view ~mathviewer ~ask_confirmation ~urichooser ~develcreator ~set_star ()
833 let current () = match !_script with None -> assert false | Some s -> s