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
80 skipped_txt nonskipped_txt st
82 let module TAPp = GrafiteAstPp in
83 let module DTE = DisambiguateTypes.Environment in
84 let module DP = DisambiguatePp in
85 let parsed_text_length =
86 String.length skipped_txt + String.length nonskipped_txt
88 let text = skipped_txt ^ nonskipped_txt in
89 let prefix_len = MatitaGtkMisc.utf8_string_length skipped_txt in
90 let enriched_history_fragment =
91 MatitaEngine.eval_ast ~do_heavy_checks:true
92 lexicon_status grafite_status (text,prefix_len,st)
94 let enriched_history_fragment = List.rev enriched_history_fragment in
98 (fun (acc, to_prepend) (status,alias) ->
100 | None -> (status,to_prepend ^ nonskipped_txt)::acc,""
101 | Some (k,((v,_) as value)) ->
102 let newtxt = DP.pp_environment (DTE.add k value DTE.empty) in
103 (status,to_prepend ^ newtxt ^ "\n")::acc, "")
104 ([],skipped_txt) enriched_history_fragment
106 res,parsed_text_length
108 let wrap_with_developments guistuff f arg =
109 let compile_needed_and_go_on lexiconfile d exc =
110 let target = Pcre.replace ~pat:"lexicon$" ~templ:"moo" lexiconfile in
111 let target = Pcre.replace ~pat:"metadata$" ~templ:"moo" target in
113 while Glib.Main.pending () do ignore(Glib.Main.iteration false); done
115 if not(MatitamakeLib.build_development_in_bg ~target refresh_cb d) then
120 let do_nothing () = raise (ActionCancelled "Inclusion not performed") in
121 let check_if_file_is_exists f =
122 assert(Pcre.pmatch ~pat:"ma$" f);
123 let pwd = Sys.getcwd () in
124 let f_pwd = pwd ^ "/" ^ f in
125 if not (HExtlib.is_regular f_pwd) then
126 raise (ActionCancelled ("File "^f_pwd^" does not exists!"))
130 ("Internal error: "^f_pwd^" exists but I'm unable to include it!"))
132 let handle_with_devel d lexiconfile exc =
133 let name = MatitamakeLib.name_for_development d in
134 let title = "Unable to include " ^ lexiconfile in
136 lexiconfile ^ " is handled by development <b>" ^ name ^ "</b>.\n\n" ^
137 "<i>Should I compile it and Its dependencies?</i>"
139 (match guistuff.ask_confirmation ~title ~message with
140 | `YES -> compile_needed_and_go_on lexiconfile d exc
142 | `CANCEL -> do_nothing ())
144 let handle_without_devel mafilename exc =
145 let title = "Unable to include " ^ mafilename in
147 mafilename ^ " is <b>not</b> handled by a development.\n" ^
148 "All dependencies are automatically solved for a development.\n\n" ^
149 "<i>Do you want to set up a development?</i>"
151 (match guistuff.ask_confirmation ~title ~message with
153 guistuff.develcreator ~containing:(Some (Filename.dirname mafilename));
156 | `CANCEL -> do_nothing())
161 | DependenciesParser.UnableToInclude mafilename ->
162 assert (Pcre.pmatch ~pat:"ma$" mafilename);
163 check_if_file_is_exists mafilename
164 | LexiconEngine.IncludedFileNotCompiled (xfilename,mafilename)
165 | GrafiteEngine.IncludedFileNotCompiled (xfilename,mafilename) as exn ->
166 assert (Pcre.pmatch ~pat:"ma$" mafilename);
167 assert (Pcre.pmatch ~pat:"lexicon$" xfilename ||
168 Pcre.pmatch ~pat:"mo$" xfilename );
169 (* we know that someone was able to include the .ma, get the baseuri
170 * but was unable to get the compilation output 'xfilename' *)
171 match MatitamakeLib.development_for_dir (Filename.dirname mafilename) with
172 | None -> handle_without_devel mafilename exn
173 | Some d -> handle_with_devel d xfilename exn
177 guistuff lexicon_status grafite_status user_goal
178 skipped_txt nonskipped_txt st
180 wrap_with_developments guistuff
182 guistuff lexicon_status grafite_status user_goal
183 skipped_txt nonskipped_txt) st
186 let pp_eager_statement_ast =
187 GrafiteAstPp.pp_statement ~term_pp:CicNotationPp.pp_term
188 ~lazy_term_pp:(fun _ -> assert false) ~obj_pp:(fun _ -> assert false)
190 let rec eval_macro include_paths (buffer : GText.buffer) guistuff lexicon_status grafite_status user_goal unparsed_text parsed_text script mac =
191 let module TAPp = GrafiteAstPp in
192 let module MQ = MetadataQuery in
193 let module MDB = LibraryDb in
194 let module CTC = CicTypeChecker in
195 let module CU = CicUniv in
196 (* no idea why ocaml wants this *)
197 let parsed_text_length = String.length parsed_text in
198 let dbd = LibraryDb.instance () in
200 let f t = ProofEngineReduction.replace
201 ~equality:(fun _ t -> match t with Cic.Meta _ -> true | _ -> false)
202 ~what:[()] ~with_what:[Cic.Implicit None] ~where:t
204 let metasenv = GrafiteTypes.get_proof_metasenv grafite_status in
207 ApplyTransformation.txt_of_cic_term max_int metasenv [] (f x))
211 | TA.WMatch (loc, term) ->
212 let l = Whelp.match_term ~dbd term in
213 let entry = `Whelp (pp_macro mac, l) in
214 guistuff.mathviewer#show_uri_list ~reuse:true ~entry l;
215 [], parsed_text_length
216 | TA.WInstance (loc, term) ->
217 let l = Whelp.instance ~dbd term in
218 let entry = `Whelp (pp_macro mac, l) in
219 guistuff.mathviewer#show_uri_list ~reuse:true ~entry l;
220 [], parsed_text_length
221 | TA.WLocate (loc, s) ->
222 let l = Whelp.locate ~dbd s in
223 let entry = `Whelp (pp_macro mac, l) in
224 guistuff.mathviewer#show_uri_list ~reuse:true ~entry l;
225 [], parsed_text_length
226 | TA.WElim (loc, term) ->
229 | Cic.MutInd (uri,n,_) -> UriManager.uri_of_uriref uri n None
230 | _ -> failwith "Not a MutInd"
232 let l = Whelp.elim ~dbd uri in
233 let entry = `Whelp (pp_macro mac, l) in
234 guistuff.mathviewer#show_uri_list ~reuse:true ~entry l;
235 [], parsed_text_length
236 | TA.WHint (loc, term) ->
237 let s = ((None,[0,[],term], Cic.Meta (0,[]) ,term),0) in
238 let l = List.map fst (MQ.experimental_hint ~dbd s) in
239 let entry = `Whelp (pp_macro mac, l) in
240 guistuff.mathviewer#show_uri_list ~reuse:true ~entry l;
241 [], parsed_text_length
247 | None -> raise NoUnfinishedProof
249 let proof = GrafiteTypes.get_current_proof grafite_status in
250 let proof_status = proof,user_goal' in
251 let l = List.map fst (MQ.experimental_hint ~dbd proof_status) in
252 let selected = guistuff.urichooser l in
254 | [] -> [], parsed_text_length
256 let suri = UriManager.string_of_uri uri in
258 TA.Executable (loc, (TA.Tactical (loc,
260 TA.Apply (loc, CicNotationPt.Uri (suri, None))),
261 Some (TA.Dot loc)))) in
263 comment parsed_text ^ "\n" ^
264 pp_eager_statement_ast (ast HExtlib.dummy_floc) in
265 let text_len = MatitaGtkMisc.utf8_string_length text in
266 let loc = HExtlib.floc_of_loc (0,text_len) in
267 let statement = `Ast (GrafiteParser.LSome (ast loc),text) in
268 let res,_parsed_text_len =
269 eval_statement include_paths buffer guistuff lexicon_status
270 grafite_status user_goal script statement
272 (* we need to replace all the parsed_text *)
273 res,String.length parsed_text
276 "The result of the urichooser should be only 1 uri, not:\n";
278 fun u -> HLog.error (UriManager.string_of_uri u ^ "\n")
281 | TA.Check (_,term) ->
282 let metasenv = GrafiteTypes.get_proof_metasenv grafite_status in
286 | Some n -> GrafiteTypes.get_proof_context grafite_status n in
287 let ty,_ = CTC.type_of_aux' metasenv context term CicUniv.empty_ugraph in
288 let t_and_ty = Cic.Cast (term,ty) in
289 guistuff.mathviewer#show_entry (`Cic (t_and_ty,metasenv));
290 [], parsed_text_length
292 | TA.Quit _ -> failwith "not implemented"
293 | TA.Print (_,kind) -> failwith "not implemented"
295 and eval_executable include_paths (buffer : GText.buffer) guistuff
296 lexicon_status grafite_status user_goal unparsed_text skipped_txt nonskipped_txt
299 let module TAPp = GrafiteAstPp in
300 let module MD = GrafiteDisambiguator in
301 let module ML = MatitaMisc in
305 | TA.Command (_,TA.Set (_,"baseuri",u)) ->
306 if Http_getter_storage.is_read_only u then
307 raise (ActionCancelled ("baseuri " ^ u ^ " is readonly"));
308 if not (Http_getter_storage.is_empty u) then
310 guistuff.ask_confirmation
311 ~title:"Baseuri redefinition"
313 "Baseuri " ^ u ^ " already exists.\n" ^
314 "Do you want to redefine the corresponding "^
315 "part of the library?")
317 | `YES -> LibraryClean.clean_baseuris [u]
319 | `CANCEL -> raise MatitaTypes.Cancel)
323 guistuff lexicon_status grafite_status user_goal skipped_txt nonskipped_txt
324 (TA.Executable (loc, ex))
326 MatitaTypes.Cancel -> [], 0
327 | GrafiteEngine.Macro (_loc,lazy_macro) ->
331 | Some n -> GrafiteTypes.get_proof_context grafite_status n in
332 let grafite_status,macro = lazy_macro context in
333 eval_macro include_paths buffer guistuff lexicon_status grafite_status
334 user_goal unparsed_text (skipped_txt ^ nonskipped_txt) script macro
336 and eval_statement include_paths (buffer : GText.buffer) guistuff lexicon_status
337 grafite_status user_goal script statement
339 let (lexicon_status,st), unparsed_text =
342 if Pcre.pmatch ~rex:only_dust_RE text then raise Margin;
344 wrap_with_developments guistuff
345 (GrafiteParser.parse_statement
346 (Ulexing.from_utf8_string text) ~include_paths) lexicon_status
349 | `Ast (st, text) -> (lexicon_status, st), text
351 let text_of_loc floc =
352 let nonskipped_txt,_ = MatitaGtkMisc.utf8_parsed_text unparsed_text floc in
353 let start, stop = HExtlib.loc_of_floc floc in
354 let floc = HExtlib.floc_of_loc (0, start) in
355 let skipped_txt,_ = MatitaGtkMisc.utf8_parsed_text unparsed_text floc in
356 let floc = HExtlib.floc_of_loc (0, stop) in
357 let txt,len = MatitaGtkMisc.utf8_parsed_text unparsed_text floc in
358 txt,nonskipped_txt,skipped_txt,len
361 | GrafiteParser.LNone loc ->
362 let parsed_text, _, _, parsed_text_length = text_of_loc loc in
363 [(grafite_status,lexicon_status),parsed_text],
365 | GrafiteParser.LSome (GrafiteAst.Comment (loc, _)) ->
366 let parsed_text, _, _, parsed_text_length = text_of_loc loc in
367 let remain_len = String.length unparsed_text - parsed_text_length in
368 let s = String.sub unparsed_text parsed_text_length remain_len in
371 eval_statement include_paths buffer guistuff lexicon_status
372 grafite_status user_goal script (`Raw s)
374 HExtlib.Localized (floc, exn) ->
375 HExtlib.raise_localized_exception
376 ~offset:(MatitaGtkMisc.utf8_string_length parsed_text) floc exn
377 | GrafiteDisambiguator.DisambiguationError (offset,errorll) ->
379 (GrafiteDisambiguator.DisambiguationError
380 (offset+parsed_text_length, errorll))
383 | (statuses,text)::tl ->
384 (statuses,parsed_text ^ text)::tl,parsed_text_length + len
386 | GrafiteParser.LSome (GrafiteAst.Executable (loc, ex)) ->
387 let _, nonskipped, skipped, parsed_text_length =
390 eval_executable include_paths buffer guistuff lexicon_status
391 grafite_status user_goal unparsed_text skipped nonskipped script ex loc
393 let fresh_script_id =
397 class script ~(source_view: GSourceView.source_view)
398 ~(mathviewer: MatitaTypes.mathViewer)
404 let buffer = source_view#buffer in
405 let source_buffer = source_view#source_buffer in
406 let initial_statuses () =
407 (* these include_paths are used only to load the initial notation *)
409 Helm_registry.get_list Helm_registry.string "matita.includes" in
411 CicNotation2.load_notation ~include_paths
412 BuildTimeConf.core_notation_script in
413 let grafite_status = GrafiteSync.init () in
414 grafite_status,lexicon_status
417 val mutable include_paths =
418 Helm_registry.get_list Helm_registry.string "matita.includes"
420 val scriptId = fresh_script_id ()
423 mathviewer = mathviewer;
424 urichooser = urichooser;
425 ask_confirmation = ask_confirmation;
426 develcreator = develcreator;
427 filenamedata = (None, None)}
429 method private getFilename =
430 match guistuff.filenamedata with Some f,_ -> f | _ -> assert false
432 method filename = self#getFilename
434 method private ppFilename =
435 match guistuff.filenamedata with
437 | None,_ -> sprintf ".unnamed%d.ma" scriptId
440 ignore (GMain.Timeout.add ~ms:300000
441 ~callback:(fun _ -> self#_saveToBackupFile ();true));
442 ignore (buffer#connect#modified_changed
443 (fun _ -> set_star (Filename.basename self#ppFilename) buffer#modified))
445 val mutable statements = [] (** executed statements *)
447 val mutable history = [ initial_statuses () ]
448 (** list of states before having executed statements. Head element of this
449 * list is the current state, last element is the state at the beginning of
451 * Invariant: this list length is 1 + length of statements *)
453 (** goal as seen by the user (i.e. metano corresponding to current tab) *)
454 val mutable userGoal = None
456 (** text mark and tag representing locked part of a script *)
458 buffer#create_mark ~name:"locked" ~left_gravity:true buffer#start_iter
459 val locked_tag = buffer#create_tag [`BACKGROUND "lightblue"; `EDITABLE false]
460 val error_tag = buffer#create_tag [`UNDERLINE `SINGLE; `FOREGROUND "red"]
462 method locked_mark = locked_mark
463 method locked_tag = locked_tag
464 method error_tag = error_tag
466 (* history can't be empty, the invariant above grant that it contains at
467 * least the init grafite_status *)
468 method grafite_status = match history with (s,_)::_ -> s | _ -> assert false
469 method lexicon_status = match history with (_,ss)::_ -> ss | _ -> assert false
471 method private _advance ?statement () =
472 let s = match statement with Some s -> s | None -> self#getFuture in
473 HLog.debug ("evaluating: " ^ first_line s ^ " ...");
474 let (entries, parsed_len) =
476 eval_statement include_paths buffer guistuff self#lexicon_status
477 self#grafite_status userGoal self (`Raw s)
478 with End_of_file -> raise Margin
480 let new_statuses, new_statements =
481 let statuses, texts = List.split entries in
484 history <- new_statuses @ history;
485 statements <- new_statements @ statements;
486 let start = buffer#get_iter_at_mark (`MARK locked_mark) in
487 let new_text = String.concat "" (List.rev new_statements) in
488 if statement <> None then
489 buffer#insert ~iter:start new_text
491 let parsed_text = String.sub s 0 parsed_len in
492 if new_text <> parsed_text then begin
493 let stop = start#copy#forward_chars (Glib.Utf8.length parsed_text) in
494 buffer#delete ~start ~stop;
495 buffer#insert ~iter:start new_text;
498 self#moveMark (Glib.Utf8.length new_text);
499 (* here we need to set the Goal in case we are going to cursor (or to
500 bottom) and we will face a macro *)
501 match self#grafite_status.proof_status with
502 Incomplete_proof p ->
504 (try Some (Continuationals.Stack.find_goal p.stack)
505 with Failure _ -> None)
506 | _ -> userGoal <- None
508 method private _retract offset lexicon_status grafite_status new_statements
511 let cur_grafite_status,cur_lexicon_status =
512 match history with s::_ -> s | [] -> assert false
514 LexiconSync.time_travel ~present:cur_lexicon_status ~past:lexicon_status;
515 GrafiteSync.time_travel ~present:cur_grafite_status ~past:grafite_status;
516 statements <- new_statements;
517 history <- new_history;
518 self#moveMark (- offset)
520 method advance ?statement () =
522 self#_advance ?statement ();
525 | Margin -> self#notify
526 | Not_found -> assert false
527 | exc -> self#notify; raise exc
531 let cmp,new_statements,new_history,(grafite_status,lexicon_status) =
532 match statements,history with
533 stat::statements, _::(status::_ as history) ->
534 assert (Glib.Utf8.validate stat);
535 Glib.Utf8.length stat, statements, history, status
536 | [],[_] -> raise Margin
537 | _,_ -> assert false
539 self#_retract cmp lexicon_status grafite_status new_statements
543 | Margin -> self#notify
544 | exc -> self#notify; raise exc
546 method private getFuture =
547 buffer#get_text ~start:(buffer#get_iter_at_mark (`MARK locked_mark))
548 ~stop:buffer#end_iter ()
551 (** @param rel_offset relative offset from current position of locked_mark *)
552 method private moveMark rel_offset =
553 let mark = `MARK locked_mark in
554 let old_insert = buffer#get_iter_at_mark `INSERT in
555 buffer#remove_tag locked_tag ~start:buffer#start_iter ~stop:buffer#end_iter;
556 let current_mark_pos = buffer#get_iter_at_mark mark in
558 match rel_offset with
559 | 0 -> current_mark_pos
560 | n when n > 0 -> current_mark_pos#forward_chars n
561 | n (* when n < 0 *) -> current_mark_pos#backward_chars (abs n)
563 buffer#move_mark mark ~where:new_mark_pos;
564 buffer#apply_tag locked_tag ~start:buffer#start_iter ~stop:new_mark_pos;
565 buffer#move_mark `INSERT old_insert;
566 let mark_position = buffer#get_iter_at_mark mark in
567 if source_view#move_mark_onscreen mark then
569 buffer#move_mark mark mark_position;
570 source_view#scroll_to_mark ~use_align:true ~xalign:1.0 ~yalign:0.1 mark;
572 while Glib.Main.pending () do ignore(Glib.Main.iteration false); done
574 method clean_dirty_lock =
575 let lock_mark_iter = buffer#get_iter_at_mark (`MARK locked_mark) in
576 buffer#remove_tag locked_tag ~start:buffer#start_iter ~stop:buffer#end_iter;
577 buffer#apply_tag locked_tag ~start:buffer#start_iter ~stop:lock_mark_iter
579 val mutable observers = []
581 method addObserver (o: LexiconEngine.status -> GrafiteTypes.status -> unit) =
582 observers <- o :: observers
584 method private notify =
585 let lexicon_status = self#lexicon_status in
586 let grafite_status = self#grafite_status in
587 List.iter (fun o -> o lexicon_status grafite_status) observers
589 method loadFromFile f =
590 buffer#set_text (HExtlib.input_file f);
592 buffer#set_modified false
594 method assignFileName file =
595 let abspath = MatitaMisc.absolute_path file in
596 let dirname = Filename.dirname abspath in
597 let devel = MatitamakeLib.development_for_dir dirname in
598 guistuff.filenamedata <- Some abspath, devel;
600 match MatitamakeLib.development_for_dir dirname with
602 | Some devel -> [MatitamakeLib.root_for_development devel] in
604 include_ @ (Helm_registry.get_list Helm_registry.string "matita.includes")
606 include_paths <- include_
608 method saveToFile () =
609 let oc = open_out self#getFilename in
610 output_string oc (buffer#get_text ~start:buffer#start_iter
611 ~stop:buffer#end_iter ());
613 buffer#set_modified false
615 method private _saveToBackupFile () =
616 if buffer#modified then
618 let f = self#ppFilename ^ "~" in
619 let oc = open_out f in
620 output_string oc (buffer#get_text ~start:buffer#start_iter
621 ~stop:buffer#end_iter ());
623 HLog.debug ("backup " ^ f ^ " saved")
626 method private goto_top =
627 let grafite_status,lexicon_status =
628 let rec last x = function
630 | hd::tl -> last hd tl
632 last (self#grafite_status,self#lexicon_status) history
634 (* FIXME: this is not correct since there is no undo for
635 * library_objects.set_default... *)
636 GrafiteSync.time_travel ~present:self#grafite_status ~past:grafite_status;
637 LexiconSync.time_travel ~present:self#lexicon_status ~past:lexicon_status
639 method private reset_buffer =
641 history <- [ initial_statuses () ];
644 buffer#remove_tag locked_tag ~start:buffer#start_iter ~stop:buffer#end_iter;
645 buffer#move_mark (`MARK locked_mark) ~where:buffer#start_iter
649 source_buffer#begin_not_undoable_action ();
650 buffer#delete ~start:buffer#start_iter ~stop:buffer#end_iter;
651 source_buffer#end_not_undoable_action ();
652 buffer#set_modified false;
655 let template = HExtlib.input_file BuildTimeConf.script_template in
656 buffer#insert ~iter:(buffer#get_iter `START) template;
657 let development = MatitamakeLib.development_for_dir (Unix.getcwd ()) in
658 guistuff.filenamedata <- (None,development);
660 match development with
662 | Some devel -> [MatitamakeLib.root_for_development devel ]
665 include_ @ (Helm_registry.get_list Helm_registry.string "matita.includes")
667 include_paths <- include_ ;
668 buffer#set_modified false;
669 set_star (Filename.basename self#ppFilename) false
671 method goto (pos: [`Top | `Bottom | `Cursor]) () =
672 let old_locked_mark =
674 (buffer#create_mark ~name:"old_locked_mark"
675 ~left_gravity:true (buffer#get_iter_at_mark (`MARK locked_mark))) in
676 let getpos _ = buffer#get_iter_at_mark (`MARK locked_mark) in
677 let getoldpos _ = buffer#get_iter_at_mark old_locked_mark in
678 let dispose_old_locked_mark () = buffer#delete_mark old_locked_mark in
681 dispose_old_locked_mark ();
689 let newpos = getpos () in
690 if (getoldpos ())#compare newpos < 0 then
692 buffer#move_mark old_locked_mark newpos;
697 dispose_old_locked_mark ();
700 | Margin -> dispose_old_locked_mark (); self#notify
701 | exc -> dispose_old_locked_mark (); self#notify; raise exc)
703 let locked_iter () = buffer#get_iter_at_mark (`NAME "locked") in
704 let cursor_iter () = buffer#get_iter_at_mark `INSERT in
707 (buffer#create_mark ~name:"initial_insert"
708 ~left_gravity:true (cursor_iter ())) in
709 let dispose_remember () = buffer#delete_mark remember in
710 let remember_iter () =
711 buffer#get_iter_at_mark (`NAME "initial_insert") in
712 let cmp () = (locked_iter ())#offset - (remember_iter ())#offset in
714 let forward_until_cursor () = (* go forward until locked > cursor *)
717 if cmp () < 0 && (getoldpos ())#compare (getpos ()) < 0
720 buffer#move_mark old_locked_mark (getpos ());
726 let rec back_until_cursor len = (* go backward until locked < cursor *)
728 statements, ((grafite_status,lexicon_status)::_ as history)
730 self#_retract (icmp - len) lexicon_status grafite_status statements
732 | statement::tl1, _::tl2 ->
733 back_until_cursor (len - MatitaGtkMisc.utf8_string_length statement) (tl1,tl2)
734 | _,_ -> assert false
738 if icmp < 0 then (* locked < cursor *)
739 (forward_until_cursor (); self#notify)
740 else if icmp > 0 then (* locked > cursor *)
741 (back_until_cursor icmp (statements,history); self#notify)
742 else (* cursor = locked *)
746 dispose_old_locked_mark ();
748 | Margin -> dispose_remember (); dispose_old_locked_mark (); self#notify
749 | exc -> dispose_remember (); dispose_old_locked_mark ();
750 self#notify; raise exc)
752 method onGoingProof () =
753 match self#grafite_status.proof_status with
754 | No_proof | Proof _ -> false
755 | Incomplete_proof _ -> true
756 | Intermediate _ -> assert false
758 (* method proofStatus = MatitaTypes.get_proof_status self#status *)
759 method proofMetasenv = GrafiteTypes.get_proof_metasenv self#grafite_status
761 method proofContext =
764 | Some n -> GrafiteTypes.get_proof_context self#grafite_status n
766 method proofConclusion =
770 GrafiteTypes.get_proof_conclusion self#grafite_status n
772 method stack = GrafiteTypes.get_stack self#grafite_status
773 method setGoal n = userGoal <- n
774 method goal = userGoal
777 let rec is_there_only_comments lexicon_status s =
778 if Pcre.pmatch ~rex:only_dust_RE s then raise Margin;
779 let lexicon_status,st =
780 GrafiteParser.parse_statement (Ulexing.from_utf8_string s)
781 ~include_paths lexicon_status
784 | GrafiteParser.LSome (GrafiteAst.Comment (loc,_)) ->
785 let _,parsed_text_length = MatitaGtkMisc.utf8_parsed_text s loc in
786 (* CSC: why +1 in the following lines ???? *)
787 let parsed_text_length = parsed_text_length + 1 in
788 prerr_endline ("## " ^ string_of_int parsed_text_length);
789 let remain_len = String.length s - parsed_text_length in
790 let next = String.sub s parsed_text_length remain_len in
791 is_there_only_comments lexicon_status next
792 | GrafiteParser.LNone _
793 | GrafiteParser.LSome (GrafiteAst.Executable _) -> false
796 is_there_only_comments self#lexicon_status self#getFuture
798 | LexiconEngine.IncludedFileNotCompiled _
799 | HExtlib.Localized _
800 | CicNotationParser.Parse_error _ -> false
801 | Margin | End_of_file -> true
805 HLog.debug "script status:";
806 HLog.debug ("history size: " ^ string_of_int (List.length history));
807 HLog.debug (sprintf "%d statements:" (List.length statements));
808 List.iter HLog.debug statements;
809 HLog.debug ("Current file name: " ^
810 (match guistuff.filenamedata with
811 |None,_ -> "[ no name ]"
816 let _script = ref None
818 let script ~source_view ~mathviewer ~urichooser ~develcreator ~ask_confirmation ~set_star ()
821 ~source_view ~mathviewer ~ask_confirmation ~urichooser ~develcreator ~set_star ()
826 let current () = match !_script with None -> assert false | Some s -> s