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/
30 open MatitaGeneratedGui
34 exception Found of int
36 let all_disambiguation_passes = ref false
38 (* this is a shit and should be changed :-{ *)
39 let interactive_uri_choice
40 ?(selection_mode:[`SINGLE|`MULTIPLE] = `MULTIPLE) ?(title = "")
41 ?(msg = "") ?(nonvars_button = false) ?(hide_uri_entry=false)
42 ?(hide_try=false) ?(ok_label="_Auto") ?(ok_action:[`SELECT|`AUTO] = `AUTO)
46 if (selection_mode <> `SINGLE) &&
47 (Helm_registry.get_opt_default Helm_registry.get_bool ~default:true "matita.auto_disambiguation")
51 let dialog = new uriChoiceDialog () in
52 if hide_uri_entry then
53 dialog#uriEntryHBox#misc#hide ();
56 dialog#uriChoiceSelectedButton#misc#hide ();
57 dialog#uriChoiceConstantsButton#misc#hide ();
59 dialog#okLabel#set_label ok_label;
60 dialog#uriChoiceTreeView#selection#set_mode
61 (selection_mode :> Gtk.Tags.selection_mode);
62 let model = new stringListModel dialog#uriChoiceTreeView in
63 let choices = ref None in
67 dialog#copyButton#misc#show ();
68 connect_button dialog#copyButton
70 match model#easy_selection () with
73 dialog#uriChoiceDialog#set_title title;
74 dialog#uriChoiceLabel#set_text msg;
75 List.iter model#easy_append (List.map NReference.string_of_reference uris);
76 dialog#uriChoiceConstantsButton#misc#set_sensitive nonvars_button;
79 dialog#uriChoiceDialog#destroy ();
82 ignore (dialog#uriChoiceDialog#event#connect#delete (fun _ -> true));
83 connect_button dialog#uriChoiceConstantsButton (fun _ ->
85 if ok_action = `AUTO then
86 connect_button dialog#uriChoiceAutoButton (fun _ ->
87 Helm_registry.set_bool "matita.auto_disambiguation" true;
90 connect_button dialog#uriChoiceAutoButton (fun _ ->
91 match model#easy_selection () with
93 | uris -> return (Some (List.map NReference.reference_of_string uris)));
94 connect_button dialog#uriChoiceSelectedButton (fun _ ->
95 match model#easy_selection () with
97 | uris -> return (Some (List.map NReference.reference_of_string uris)));
98 connect_button dialog#uriChoiceAbortButton (fun _ -> return None);
99 dialog#uriChoiceDialog#show ();
102 | None -> raise MatitaTypes.Cancel
107 class console ~(buffer: GText.buffer) () =
109 val error_tag = buffer#create_tag [ `FOREGROUND "red" ]
110 val warning_tag = buffer#create_tag [ `FOREGROUND "orange" ]
111 val message_tag = buffer#create_tag []
112 val debug_tag = buffer#create_tag [ `FOREGROUND "#888888" ]
113 method message s = buffer#insert ~iter:buffer#end_iter ~tags:[message_tag] s
114 method error s = buffer#insert ~iter:buffer#end_iter ~tags:[error_tag] s
115 method warning s = buffer#insert ~iter:buffer#end_iter ~tags:[warning_tag] s
116 method debug s = buffer#insert ~iter:buffer#end_iter ~tags:[debug_tag] s
118 buffer#delete ~start:buffer#start_iter ~stop:buffer#end_iter
119 method log_callback (tag: HLog.log_tag) s =
120 let s = Pcre.replace ~pat:"
\e\\[0;3.m([^
\e]+)
\e\\[0m" ~templ:"$1" s in
122 | `Debug -> self#debug (s ^ "\n")
123 | `Error -> self#error (s ^ "\n")
124 | `Message -> self#message (s ^ "\n")
125 | `Warning -> self#warning (s ^ "\n")
128 let clean_current_baseuri status =
129 LibraryClean.clean_baseuris [status#baseuri]
131 let save_moo0 ~do_clean script status =
132 let baseuri = status#baseuri in
133 match script#bos, script#eos with
136 GrafiteTypes.Serializer.serialize ~baseuri:(NUri.uri_of_string baseuri)
138 | _ -> if do_clean then clean_current_baseuri status
141 let save_moo status =
142 let script = MatitaScript.current () in
143 save_moo0 ~do_clean:true script status
146 let ask_unsaved parent filename =
147 MatitaGtkMisc.ask_confirmation
148 ~parent ~title:"Unsaved work!"
149 ~message:("Script <b>" ^ filename ^ "</b> is modified.!\n\n"^
150 "<i>Do you want to save the script before continuing?</i>")
153 class interpErrorModel =
154 let cols = new GTree.column_list in
155 let id_col = cols#add Gobject.Data.string in
156 let dsc_col = cols#add Gobject.Data.string in
157 let interp_no_col = cols#add Gobject.Data.caml in
158 let tree_store = GTree.tree_store cols in
159 let id_renderer = GTree.cell_renderer_text [], ["text", id_col] in
160 let dsc_renderer = GTree.cell_renderer_text [], ["text", dsc_col] in
161 let id_view_col = GTree.view_column ~renderer:id_renderer () in
162 let dsc_view_col = GTree.view_column ~renderer:dsc_renderer () in
163 fun (tree_view: GTree.view) choices ->
166 tree_view#set_model (Some (tree_store :> GTree.model));
167 ignore (tree_view#append_column id_view_col);
168 ignore (tree_view#append_column dsc_view_col);
170 let idx1 = ref ~-1 in
175 if List.length choices = 1 then
178 (let loc_row = tree_store#append () in
181 [passes,envs_and_diffs,_,_] ->
182 tree_store#set ~row:loc_row ~column:id_col
183 ("Error location " ^ string_of_int (!idx1+1) ^
184 ", error message " ^ string_of_int (!idx1+1) ^ ".1" ^
186 String.concat " " (List.map string_of_int passes) ^
188 tree_store#set ~row:loc_row ~column:interp_no_col
191 tree_store#set ~row:loc_row ~column:id_col
192 ("Error location " ^ string_of_int (!idx1+1));
193 tree_store#set ~row:loc_row ~column:interp_no_col
197 let idx2 = ref ~-1 in
199 (fun passes,envs_and_diffs,_,_ ->
202 if List.length lll = 1 then
205 let msg_row = tree_store#append ?parent:loc_row () in
206 (tree_store#set ~row:msg_row ~column:id_col
207 ("Error message " ^ string_of_int (!idx1+1) ^ "." ^
208 string_of_int (!idx2+1) ^
210 String.concat " " (List.map string_of_int passes) ^
212 tree_store#set ~row:msg_row ~column:interp_no_col
213 (!idx1,Some !idx2,None);
215 let idx3 = ref ~-1 in
217 (fun (passes,env,_) ->
220 match envs_and_diffs with
222 let interp_row = tree_store#append ?parent:msg_row () in
223 tree_store#set ~row:interp_row ~column:id_col
224 ("Interpretation " ^ string_of_int (!idx3+1) ^
226 String.concat " " (List.map string_of_int passes) ^
228 tree_store#set ~row:interp_row ~column:interp_no_col
229 (!idx1,Some !idx2,Some !idx3);
236 let row = tree_store#append ?parent:interp_row () in
237 tree_store#set ~row ~column:id_col id;
238 tree_store#set ~row ~column:dsc_col dsc;
239 tree_store#set ~row ~column:interp_no_col
240 (!idx1,Some !idx2,Some !idx3)
244 if List.length lll > 1 then
246 (fun p -> tree_view#expand_row (tree_store#get_path p))
250 method get_interp_no tree_path =
251 let iter = tree_store#get_iter tree_path in
252 tree_store#get ~row:iter ~column:interp_no_col
255 exception UseLibrary;;
257 let interactive_error_interp ~all_passes
258 (source_buffer:GSourceView3.source_buffer) notify_exn offset errorll filename
260 (* hook to save a script for each disambiguation error *)
263 source_buffer#get_text ~start:source_buffer#start_iter
264 ~stop:source_buffer#end_iter () in
265 let md5 = Digest.to_hex (Digest.string text) in
267 Filename.chop_extension filename ^ ".error." ^ md5 ^ ".ma" in
268 let ch = open_out filename in
269 output_string ch text;
272 assert (List.flatten errorll <> []);
274 let remove_non_significant =
275 List.filter (fun (_env,_diff,_loc_msg,significant) -> significant) in
276 let annotated_errorll () =
279 (List.fold_left (fun (pass,res) item -> pass+1,(pass+1,item)::res) (0,[])
281 if all_passes then annotated_errorll () else
282 let safe_list_nth l n = try List.nth l n with Failure _ -> [] in
283 (* We remove passes 1,2 and 5,6 *)
286 ::(3,remove_non_significant (safe_list_nth errorll 2))
287 ::(4,remove_non_significant (safe_list_nth errorll 3))
290 if List.flatten (List.map snd res) <> [] then res
292 (* all errors (if any) are not significant: we keep them *)
295 ::(3,(safe_list_nth errorll 2))
296 ::(4,(safe_list_nth errorll 3))
299 if List.flatten (List.map snd res) <> [] then
302 "All disambiguation errors are not significant. Showing them anyway." ;
308 "No errors in phases 2 and 3. Showing all errors in all phases" ;
312 let choices = MatitaExcPp.compact_disambiguation_errors all_passes errorll' in
315 | [loffset,[_,envs_and_diffs,msg,significant]] ->
316 let _,env,diff = List.hd envs_and_diffs in
318 (MultiPassDisambiguator.DisambiguationError
319 (offset,[[env,diff,lazy (loffset,Lazy.force msg),significant]]));
321 let dialog = new disambiguationErrors () in
323 dialog#disambiguationErrorsMoreErrors#misc#set_sensitive false;
324 let model = new interpErrorModel dialog#treeview choices in
325 dialog#disambiguationErrors#set_title "Disambiguation error";
326 dialog#disambiguationErrorsLabel#set_label
327 "Click on an error to see the corresponding message:";
328 ignore (dialog#treeview#connect#cursor_changed
331 match fst (dialog#treeview#get_cursor ()) with
334 let idx1,idx2,idx3 = model#get_interp_no tree_path in
335 let loffset,lll = List.nth choices idx1 in
336 let _,envs_and_diffs,msg,significant =
338 Some idx2 -> List.nth lll idx2
340 [],[],lazy "Multiple error messages. Please select one.",true
344 Some idx3 -> List.nth envs_and_diffs idx3
345 | None -> [],[],[] (* dymmy value, used *) in
346 let script = MatitaScript.current () in
347 let error_tag = script#error_tag in
348 source_buffer#remove_tag error_tag
349 ~start:source_buffer#start_iter
350 ~stop:source_buffer#end_iter;
352 (MultiPassDisambiguator.DisambiguationError
353 (offset,[[env,diff,lazy(loffset,Lazy.force msg),significant]]))
356 dialog#disambiguationErrors#destroy ();
359 let fail _ = return () in
360 ignore(dialog#disambiguationErrors#event#connect#delete (fun _ -> true));
361 connect_button dialog#disambiguationErrorsOkButton
364 match fst (dialog#treeview#get_cursor ()) with
367 let idx1,idx2,idx3 = model#get_interp_no tree_path in
370 Some idx2, Some idx3 ->
371 let _,lll = List.nth choices idx1 in
372 let _,envs_and_diffs,_,_ = List.nth lll idx2 in
373 let _,_,diff = List.nth envs_and_diffs idx3 in
375 | _,_ -> assert false
384 | DisambiguateTypes.Id id ->
385 GrafiteAst.Ident_alias (id, desc)
386 | DisambiguateTypes.Symbol (symb, i)->
387 GrafiteAst.Symbol_alias (symb, i, desc)
388 | DisambiguateTypes.Num i ->
389 GrafiteAst.Number_alias (i, desc)
391 GrafiteAstPp.pp_alias alias)
396 (source_buffer#get_iter_at_mark
397 (`NAME "beginning_of_statement")) newtxt ;
400 connect_button dialog#disambiguationErrorsMoreErrors
401 (fun _ -> return () ; raise UseLibrary);
402 connect_button dialog#disambiguationErrorsCancelButton fail;
403 dialog#disambiguationErrors#show ();
408 (* creation order _is_ relevant for windows placement *)
409 let main = new mainWin () in
410 let sequents_viewer =
411 MatitaMathView.sequentsViewer_instance main#sequentsNotebook in
412 let findRepl = new findReplWin () in
413 let keyBindingBoxes = (* event boxes which should receive global key events *)
414 [ main#mainWinEventBox ]
416 let console = new console ~buffer:main#logTextView#buffer () in
418 val mutable chosen_file = None
419 val mutable _ok_not_exists = false
420 val mutable _only_directory = false
421 val mutable current_page = -1
424 let s () = MatitaScript.current () in
426 List.iter (* global key bindings *)
427 (fun (key, modifiers, callback) ->
428 self#addKeyBinding key ~modifiers callback)
431 toggle_win ~check:main#showProofMenuItem proof#proofWin;
433 toggle_win ~check:main#showCheckMenuItem check#checkWin;
436 GdkKeysyms._Page_Down, [`CONTROL], main#scriptNotebook#next_page;
437 GdkKeysyms._Page_Up, [`CONTROL], main#scriptNotebook#previous_page
440 let parse_txt_file file =
441 let ch = open_in (BuildTimeConf.runtime_base_dir ^ "/" ^ file) in
442 let l_rev = ref [] in
445 l_rev := input_line ch :: !l_rev;
454 ~authors:(parse_txt_file "AUTHORS")
455 (*~comments:"comments"*)
456 ~copyright:"Copyright (C) 2005, the HELM team"
457 ~license:(String.concat "\n" (parse_txt_file "LICENSE"))
458 ~logo:(GdkPixbuf.from_file (MatitaMisc.image_path "/matita_medium.png"))
460 ~version:BuildTimeConf.version
461 ~website:"http://matita.cs.unibo.it"
464 ignore(about_dialog#event#connect#delete (fun _ -> true));
465 ignore(about_dialog#connect#response (fun _ ->about_dialog#misc#hide ()));
466 connect_menu_item main#contentsMenuItem (fun () ->
467 if 0 = Sys.command "which gnome-help" then
469 sprintf "gnome-help ghelp://%s/C/matita.xml &" BuildTimeConf.help_dir
471 ignore (Sys.command cmd)
473 MatitaGtkMisc.report_error ~title:"help system error"
475 "The program gnome-help is not installed\n\n"^
476 "To browse the user manal it is necessary to install "^
477 "the gnome help syste (also known as yelp)")
478 ~parent:main#toplevel ());
479 connect_menu_item main#aboutMenuItem about_dialog#present;
481 let show_find_Repl () =
482 findRepl#toplevel#misc#show ();
483 findRepl#toplevel#misc#grab_focus ()
485 let hide_find_Repl () = findRepl#toplevel#misc#hide () in
487 let source_view = (s ())#source_view in
488 let highlight start end_ =
489 source_view#source_buffer#move_mark `INSERT ~where:start;
490 source_view#source_buffer#move_mark `SEL_BOUND ~where:end_;
491 source_view#scroll_mark_onscreen `INSERT
493 let text = findRepl#findEntry#text in
494 let iter = source_view#source_buffer#get_iter `SEL_BOUND in
495 match iter#forward_search text with
497 (match source_view#source_buffer#start_iter#forward_search text with
499 | Some (start,end_) -> highlight start end_)
500 | Some (start,end_) -> highlight start end_
503 let source_view = (s ())#source_view in
504 let text = findRepl#replaceEntry#text in
505 let ins = source_view#source_buffer#get_iter `INSERT in
506 let sel = source_view#source_buffer#get_iter `SEL_BOUND in
507 if ins#compare sel < 0 then
509 ignore(source_view#source_buffer#delete_selection ());
510 source_view#source_buffer#insert text
513 connect_button findRepl#findButton find_forward;
514 connect_button findRepl#findReplButton replace;
515 connect_button findRepl#cancelButton (fun _ -> hide_find_Repl ());
516 ignore(findRepl#toplevel#event#connect#delete
517 ~callback:(fun _ -> hide_find_Repl ();true));
518 connect_menu_item main#undoMenuItem
519 (fun () -> (MatitaScript.current ())#safe_undo);
520 main#undoMenuItem#misc#set_sensitive false;
521 connect_menu_item main#redoMenuItem
522 (fun () -> (MatitaScript.current ())#safe_redo);
523 main#redoMenuItem#misc#set_sensitive false;
524 connect_menu_item main#editMenu (fun () ->
525 main#copyMenuItem#misc#set_sensitive
526 (MatitaScript.current ())#canCopy;
527 main#cutMenuItem#misc#set_sensitive
528 (MatitaScript.current ())#canCut;
529 main#deleteMenuItem#misc#set_sensitive
530 (MatitaScript.current ())#canDelete;
531 main#pasteMenuItem#misc#set_sensitive
532 (MatitaScript.current ())#canPaste;
533 main#pastePatternMenuItem#misc#set_sensitive
534 (MatitaScript.current ())#canPastePattern);
535 connect_menu_item main#copyMenuItem
536 (fun () -> (MatitaScript.current ())#copy ());
537 connect_menu_item main#cutMenuItem
538 (fun () -> (MatitaScript.current ())#cut ());
539 connect_menu_item main#deleteMenuItem
540 (fun () -> (MatitaScript.current ())#delete ());
541 connect_menu_item main#pasteMenuItem
542 (fun () -> (MatitaScript.current ())#paste ());
543 connect_menu_item main#pastePatternMenuItem
544 (fun () -> (MatitaScript.current ())#pastePattern ());
545 connect_menu_item main#selectAllMenuItem (fun () ->
546 let source_view = (s ())#source_view in
547 source_view#source_buffer#move_mark `INSERT source_view#source_buffer#start_iter;
548 source_view#source_buffer#move_mark `SEL_BOUND source_view#source_buffer#end_iter);
549 connect_menu_item main#findReplMenuItem show_find_Repl;
550 connect_menu_item main#externalEditorMenuItem self#externalEditor;
551 connect_menu_item main#ligatureButton
552 (fun () -> (MatitaScript.current ())#nextSimilarSymbol);
553 ignore (findRepl#findEntry#connect#activate find_forward);
554 (* interface lockers *)
556 let source_view = (s ())#source_view in
557 main#buttonsToolbar#misc#set_sensitive false;
558 main#scriptMenu#misc#set_sensitive false;
559 source_view#set_editable false
562 let source_view = (s ())#source_view in
563 main#buttonsToolbar#misc#set_sensitive true;
564 main#scriptMenu#misc#set_sensitive true;
565 source_view#set_editable true;
566 (*The next line seems sufficient to avoid some unknown race condition *)
567 GtkThread.sync (fun () -> ()) ()
569 let worker_thread = ref None in
570 let notify_exn (source_view : GSourceView3.source_view) exn =
571 let floc, msg = MatitaExcPp.to_string exn in
576 let (x, y) = HExtlib.loc_of_floc floc in
577 let script = MatitaScript.current () in
578 let locked_mark = script#locked_mark in
579 let error_tag = script#error_tag in
581 (source_view#source_buffer#get_iter_at_mark (`MARK locked_mark))#offset in
582 let x' = baseoffset + x in
583 let y' = baseoffset + y in
584 let x_iter = source_view#source_buffer#get_iter (`OFFSET x') in
585 let y_iter = source_view#source_buffer#get_iter (`OFFSET y') in
586 source_view#source_buffer#apply_tag error_tag ~start:x_iter ~stop:y_iter;
588 id := Some (source_view#source_buffer#connect#changed ~callback:(fun () ->
589 source_view#source_buffer#remove_tag error_tag
590 ~start:source_view#source_buffer#start_iter
591 ~stop:source_view#source_buffer#end_iter;
593 | None -> assert false (* a race condition occurred *)
595 source_view#source_buffer#misc#disconnect id));
596 source_view#source_buffer#place_cursor
597 (source_view#source_buffer#get_iter (`OFFSET x'));
600 let locker f script =
601 let source_view = script#source_view in
605 let saved_use_library= !MultiPassDisambiguator.use_library in
607 MultiPassDisambiguator.use_library := !all_disambiguation_passes;
609 MultiPassDisambiguator.use_library := saved_use_library;
612 | MultiPassDisambiguator.DisambiguationError (offset,errorll) ->
614 interactive_error_interp
615 ~all_passes:!all_disambiguation_passes source_view#source_buffer
616 (notify_exn source_view) offset errorll (s())#filename
619 MultiPassDisambiguator.use_library := true;
622 | MultiPassDisambiguator.DisambiguationError (offset,errorll) ->
623 interactive_error_interp ~all_passes:true source_view#source_buffer
624 (notify_exn source_view) offset errorll (s())#filename
626 notify_exn source_view exc);
627 | exc -> notify_exn source_view exc);
628 MultiPassDisambiguator.use_library := saved_use_library;
631 (try notify_exn source_view exc
632 with Sys.Break as e -> notify_exn source_view e);
636 worker_thread := Some (Thread.create thread_main ())
639 (* the following lines are from Xavier Leroy: http://alan.petitepomme.net/cwn/2005.11.08.html *)
640 let interrupt = ref None in
641 let old_callback = ref (function _ -> ()) in
642 let force_interrupt n =
643 (* This function is called just before the thread's timeslice ends *)
645 if Some(Thread.id(Thread.self())) = !interrupt then
646 (interrupt := None; raise Sys.Break) in
648 match Sys.signal Sys.sigvtalrm (Sys.Signal_handle force_interrupt) with
649 Sys.Signal_handle f -> old_callback := f
651 | Sys.Signal_default -> assert false
654 match !worker_thread with
656 | Some t -> interrupt := Some (Thread.id t) in
657 let keep_focus f (script: MatitaScript.script) =
659 f script; script#source_view#misc#grab_focus ()
661 exc -> script#source_view#misc#grab_focus (); raise exc in
664 List.iter (fun w -> w#misc#set_sensitive false) [ main#saveMenuItem ];
666 let adj = main#logScrolledWin#vadjustment in
667 ignore (adj#connect#changed
668 (fun _ -> adj#set_value (adj#upper -. adj#page_size)));
669 console#message (sprintf "\tMatita version %s\n" BuildTimeConf.version);
670 (* natural deduction palette *)
671 main#tacticsButtonsHandlebox#misc#hide ();
672 MatitaGtkMisc.toggle_callback
674 if b then main#tacticsButtonsHandlebox#misc#show ()
675 else main#tacticsButtonsHandlebox#misc#hide ())
676 ~check:main#menuitemPalette;
677 connect_button main#butImpl_intro
678 (fun () -> (s ())#source_view#source_buffer#insert "apply rule (⇒#i […] (…));\n");
679 connect_button main#butAnd_intro
680 (fun () -> (s ())#source_view#source_buffer#insert
681 "apply rule (∧#i (…) (…));\n\t[\n\t|\n\t]\n");
682 connect_button main#butOr_intro_left
683 (fun () -> (s ())#source_view#source_buffer#insert "apply rule (∨#i_l (…));\n");
684 connect_button main#butOr_intro_right
685 (fun () -> (s ())#source_view#source_buffer#insert "apply rule (∨#i_r (…));\n");
686 connect_button main#butNot_intro
687 (fun () -> (s ())#source_view#source_buffer#insert "apply rule (¬#i […] (…));\n");
688 connect_button main#butTop_intro
689 (fun () -> (s ())#source_view#source_buffer#insert "apply rule (⊤#i);\n");
690 connect_button main#butImpl_elim
691 (fun () -> (s ())#source_view#source_buffer#insert
692 "apply rule (⇒#e (…) (…));\n\t[\n\t|\n\t]\n");
693 connect_button main#butAnd_elim_left
694 (fun () -> (s ())#source_view#source_buffer#insert "apply rule (∧#e_l (…));\n");
695 connect_button main#butAnd_elim_right
696 (fun () -> (s ())#source_view#source_buffer#insert "apply rule (∧#e_r (…));\n");
697 connect_button main#butOr_elim
698 (fun () -> (s ())#source_view#source_buffer#insert
699 "apply rule (∨#e (…) […] (…) […] (…));\n\t[\n\t|\n\t|\n\t]\n");
700 connect_button main#butNot_elim
701 (fun () -> (s ())#source_view#source_buffer#insert
702 "apply rule (¬#e (…) (…));\n\t[\n\t|\n\t]\n");
703 connect_button main#butBot_elim
704 (fun () -> (s ())#source_view#source_buffer#insert "apply rule (⊥#e (…));\n");
705 connect_button main#butRAA
706 (fun () -> (s ())#source_view#source_buffer#insert "apply rule (RAA […] (…));\n");
707 connect_button main#butUseLemma
708 (fun () -> (s ())#source_view#source_buffer#insert "apply rule (lem #premises name …);\n");
709 connect_button main#butDischarge
710 (fun () -> (s ())#source_view#source_buffer#insert "apply rule (discharge […]);\n");
712 connect_button main#butForall_intro
713 (fun () -> (s ())#source_view#source_buffer#insert "apply rule (∀#i {…} (…));\n");
714 connect_button main#butForall_elim
715 (fun () -> (s ())#source_view#source_buffer#insert "apply rule (∀#e {…} (…));\n");
716 connect_button main#butExists_intro
717 (fun () -> (s ())#source_view#source_buffer#insert "apply rule (∃#i {…} (…));\n");
718 connect_button main#butExists_elim
719 (fun () -> (s ())#source_view#source_buffer#insert
720 "apply rule (∃#e (…) {…} […] (…));\n\t[\n\t|\n\t]\n");
723 let module Hr = Helm_registry in
724 MatitaGtkMisc.toggle_callback ~check:main#fullscreenMenuItem
726 | true -> main#toplevel#fullscreen ()
727 | false -> main#toplevel#unfullscreen ());
728 main#fullscreenMenuItem#set_active false;
729 MatitaGtkMisc.toggle_callback ~check:main#ppNotationMenuItem
730 ~callback:(function b ->
732 let _status = Interpretations.toggle_active_interpretations s#status b
734 assert false (* MATITA 1.0 ???
735 s#set_grafite_status status*)
737 MatitaGtkMisc.toggle_callback ~check:main#hideCoercionsMenuItem
738 ~callback:(fun enabled -> Interpretations.hide_coercions := enabled);
739 MatitaGtkMisc.toggle_callback ~check:main#unicodeAsTexMenuItem
740 ~callback:(fun enabled ->
741 Helm_registry.set_bool "matita.paste_unicode_as_tex" enabled);
742 main#unicodeAsTexMenuItem#set_active
743 (Helm_registry.get_bool "matita.paste_unicode_as_tex");
745 HLog.set_log_callback (fun tag msg -> GtkThread.async (self#console#log_callback tag) msg);
746 GtkSignal.user_handler :=
748 | MatitaScript.ActionCancelled s -> HLog.error s
750 if not (Helm_registry.get_bool "matita.debug") then
751 (* MatitaScript.current is problably wrong, but what else
753 notify_exn (MatitaScript.current ())#source_view exn
757 match self#chooseFile () with
758 | Some f -> self#loadScript f
760 with MatitaTypes.Cancel -> ()
763 let source_view = (s ())#source_view in
764 source_view#source_buffer#place_cursor
765 (source_view#source_buffer#get_iter_at_mark (`NAME "locked")) in
766 let advance (script: MatitaScript.script) = script#advance (); cursor () in
767 let retract (script: MatitaScript.script) = script#retract (); cursor () in
768 let top (script: MatitaScript.script) = script#goto `Top (); cursor () in
769 let bottom (script: MatitaScript.script) = script#goto `Bottom (); cursor () in
770 let jump (script: MatitaScript.script) = script#goto `Cursor (); cursor () in
771 let advance () = locker (keep_focus advance) (MatitaScript.current ()) in
772 let retract () = locker (keep_focus retract) (MatitaScript.current ()) in
773 let top () = locker (keep_focus top) (MatitaScript.current ()) in
774 let bottom () = locker (keep_focus bottom) (MatitaScript.current ()) in
775 let jump () = locker (keep_focus jump) (MatitaScript.current ()) in
777 self#setQuitCallback (fun () ->
778 let cancel = ref false in
779 MatitaScript.iter_scripts
782 if not (self#closeScript0 script) then
786 connect_button main#scriptAdvanceButton advance;
787 connect_button main#scriptRetractButton retract;
788 connect_button main#scriptTopButton top;
789 connect_button main#scriptBottomButton bottom;
790 connect_button main#scriptJumpButton jump;
791 connect_button main#scriptAbortButton kill_worker;
792 connect_menu_item main#scriptAdvanceMenuItem advance;
793 connect_menu_item main#scriptRetractMenuItem retract;
794 connect_menu_item main#scriptTopMenuItem top;
795 connect_menu_item main#scriptBottomMenuItem bottom;
796 connect_menu_item main#scriptJumpMenuItem jump;
797 connect_menu_item main#openMenuItem loadScript;
798 connect_menu_item main#saveMenuItem
799 (fun () -> self#saveScript (MatitaScript.current ()));
800 connect_menu_item main#saveAsMenuItem
801 (fun () -> self#saveAsScript (MatitaScript.current ()));
802 connect_menu_item main#newMenuItem self#newScript;
803 connect_menu_item main#closeMenuItem self#closeCurrentScript;
804 connect_menu_item main#showCoercionsGraphMenuItem
805 (fun _ -> MatitaMathView.cicBrowser (Some (`About `Coercions)));
806 connect_menu_item main#showHintsDbMenuItem
807 (fun _ -> MatitaMathView.cicBrowser (Some (`About `Hints)));
808 connect_menu_item main#showTermGrammarMenuItem
809 (fun _ -> MatitaMathView.cicBrowser (Some (`About `Grammar)));
810 connect_menu_item main#showUnicodeTable
811 (fun _ -> MatitaMathView.cicBrowser (Some (`About `TeX)));
813 main#debugMenu#misc#hide ();
815 main#hintNotebook#misc#hide ();
817 main#hintLowImage#set_file (image_path "matita-bulb-low.png");
818 main#hintMediumImage#set_file (image_path "matita-bulb-medium.png");
819 main#hintHighImage#set_file (image_path "matita-bulb-high.png");
821 (* main win dimension *)
822 let width = Gdk.Screen.width ~screen:(Gdk.Screen.default ()) () in
823 let height = Gdk.Screen.height ~screen:(Gdk.Screen.default ()) () in
824 (* hack for xinerama, no proper support of monitors from lablgtk *)
825 let width = if width > 1600 then width / 2 else width in
826 let height = if height > 1200 then height / 2 else height in
827 let main_w = width * 90 / 100 in
828 let main_h = height * 80 / 100 in
829 let script_w = main_w * 6 / 10 in
830 main#toplevel#resize ~width:main_w ~height:main_h;
831 main#hpaneScriptSequent#set_position script_w;
832 (* math view handling *)
833 connect_menu_item main#newCicBrowserMenuItem (fun () ->
834 ignore(MatitaMathView.cicBrowser None));
835 connect_menu_item main#increaseFontSizeMenuItem
836 MatitaMisc.increase_font_size;
837 connect_menu_item main#decreaseFontSizeMenuItem
838 MatitaMisc.decrease_font_size;
839 connect_menu_item main#normalFontSizeMenuItem
840 MatitaMisc.reset_font_size;
841 ignore (main#scriptNotebook#connect#switch_page (fun page ->
843 current_page <- page;
844 let script = MatitaScript.at_page page in
846 main#undoMenuItem#misc#set_sensitive
847 script#source_view#source_buffer#can_undo ;
848 main#redoMenuItem#misc#set_sensitive
849 script#source_view#source_buffer#can_redo ;
850 main#saveMenuItem#misc#set_sensitive script#has_name))
852 method private externalEditor () =
853 let script = MatitaScript.current () in
854 let source_view = script#source_view in
855 let cmd = Helm_registry.get "matita.external_editor" in
856 (* ZACK uncomment to enable interactive ask of external editor command *)
859 "External editor command:
860 %f will be substitute for the script name,
861 %p for the cursor position in bytes,
862 %l for the execution point in bytes."
864 ask_text ~gui:self ~title:"External editor" ~msg ~multiline:false
865 ~default:(Helm_registry.get "matita.external_editor") ()
867 let fname = script#filename in
869 source_view#source_buffer#start_iter#get_slice
870 ~stop:(source_view#source_buffer#get_iter_at_mark mark)
872 let locked = `MARK script#locked_mark in
873 let string_pos mark = string_of_int (String.length (slice mark)) in
874 let cursor_pos = string_pos `INSERT in
875 let locked_pos = string_pos locked in
877 Pcre.replace ~pat:"%f" ~templ:fname
878 (Pcre.replace ~pat:"%p" ~templ:cursor_pos
879 (Pcre.replace ~pat:"%l" ~templ:locked_pos
882 let locked_before = slice locked in
883 let locked_offset = (source_view#source_buffer#get_iter_at_mark locked)#offset in
884 ignore (Unix.system cmd);
885 source_view#source_buffer#set_text (HExtlib.input_file fname);
886 let locked_iter = source_view#source_buffer#get_iter (`OFFSET locked_offset) in
887 source_view#source_buffer#move_mark locked locked_iter;
888 source_view#source_buffer#apply_tag script#locked_tag
889 ~start:source_view#source_buffer#start_iter ~stop:locked_iter;
890 let locked_after = slice locked in
894 for i = 0 to String.length locked_before - 1 do
895 if locked_before.[i] <> locked_after.[i] then begin
896 source_view#source_buffer#place_cursor
897 ~where:(source_view#source_buffer#get_iter (`LINEBYTE (!line, !col)));
898 script#goto `Cursor ();
900 end else if locked_before.[i] = '\n' then begin
907 | Invalid_argument _ -> script#goto `Bottom ()
909 method private saveAsScript script =
910 match self#chooseFile ~ok_not_exists:true () with
913 script#assignFileName (Some f);
914 script#saveToFile ();
915 console#message ("'"^f^"' saved.\n");
919 method private saveScript script =
920 if script#has_name then
921 (script#saveToFile ();
922 console#message ("'"^script#filename^"' saved.\n"))
923 else self#saveAsScript script
925 (* returns false if closure is aborted by the user *)
926 method private closeScript0 script =
927 if script#source_view#buffer#modified then
929 ask_unsaved main#toplevel (Filename.basename script#filename)
932 self#saveScript script;
933 save_moo script#status;
938 (save_moo script#status; true)
940 method private closeScript page script =
941 if self#closeScript0 script then
943 MatitaScript.destroy page;
944 ignore (main#scriptNotebook#remove_page page)
947 method private closeCurrentScript () =
948 let page = main#scriptNotebook#current_page in
949 let script = MatitaScript.at_page page in
950 self#closeScript page script
952 method private save_page () =
953 if current_page >= 0 then
954 let old_script = MatitaScript.at_page current_page in
955 save_moo0 ~do_clean:false old_script old_script#status
957 method newScript () =
959 let scrolledWindow = GBin.scrolled_window () in
960 let hbox = GPack.hbox () in
961 let tab_label = GMisc.label ~text:"foo" ~packing:hbox#pack () in
963 GMisc.label ~text:"" ~packing:(hbox#pack ~expand:true ~fill:true) () in
965 GButton.button ~relief:`NONE ~packing:hbox#pack () in
966 let image = GMisc.image ~stock:`CLOSE ~icon_size:`MENU () in
967 closebutton#set_image image#coerce;
968 let script = MatitaScript.script ~parent:scrolledWindow ~tab_label () in
969 ignore (main#scriptNotebook#prepend_page ~tab_label:hbox#coerce
970 scrolledWindow#coerce);
971 ignore (closebutton#connect#clicked (fun () ->
973 (main#scriptNotebook#page_num scrolledWindow#coerce) script));
974 main#scriptNotebook#goto_page 0;
975 sequents_viewer#reset;
976 sequents_viewer#load_logo;
977 let browser_observer _ = MatitaMathView.refresh_all_browsers () in
978 let sequents_observer status =
979 sequents_viewer#reset;
980 match status#ng_mode with
982 sequents_viewer#nload_sequents status;
985 Continuationals.Stack.find_goal status#stack
987 sequents_viewer#goto_sequent status goal
988 with Failure _ -> ());
989 | `CommandMode -> sequents_viewer#load_logo
991 script#addObserver sequents_observer;
992 script#addObserver browser_observer
994 method loadScript file =
995 let page = main#scriptNotebook#current_page in
996 let script = MatitaScript.at_page page in
997 if script#source_view#buffer#modified || script#has_name then
999 let script = MatitaScript.current () in
1000 let source_view = script#source_view in
1002 script#assignFileName (Some file);
1003 let file = script#filename in
1005 if Sys.file_exists file then file
1006 else BuildTimeConf.script_template
1008 source_view#source_buffer#begin_not_undoable_action ();
1009 script#loadFromFile content;
1010 source_view#source_buffer#end_not_undoable_action ();
1011 source_view#buffer#move_mark `INSERT source_view#buffer#start_iter;
1012 source_view#buffer#place_cursor source_view#buffer#start_iter;
1013 console#message ("'"^file^"' loaded.");
1014 self#_enableSaveTo file
1016 method private _enableSaveTo file =
1017 self#main#saveMenuItem#misc#set_sensitive true
1019 method private console = console
1020 method private findRepl = findRepl
1023 method private addKeyBinding key ?modifiers callback =
1024 (* List.iter (fun evbox -> add_key_binding key callback evbox) *)
1025 List.iter (fun evbox -> connect_key evbox#event key ?modifiers callback)
1028 method private setQuitCallback callback =
1029 connect_menu_item main#quitMenuItem callback;
1030 ignore (main#toplevel#event#connect#delete
1031 (fun _ -> callback ();true));
1032 self#addKeyBinding GdkKeysyms._q callback
1034 method private chooseFileOrDir ok_not_exists only_directory =
1035 let fileSel = GWindow.file_chooser_dialog
1037 ~title:"Select file"
1042 fileSel#add_select_button_stock `OPEN `OK;
1043 fileSel#add_button_stock `CANCEL `CANCEL;
1044 ignore (fileSel#set_current_folder(Sys.getcwd ())) ;
1047 match fileSel#run () with
1049 (match fileSel#filename with
1052 if Sys.file_exists fname then
1054 if HExtlib.is_regular fname && not (only_directory) then
1056 else if only_directory && HExtlib.is_dir fname then
1061 else if ok_not_exists then Some fname else aux ())
1063 | `DELETE_EVENT -> None in
1065 fileSel#destroy () ;
1068 method private chooseFile ?(ok_not_exists = false) () =
1069 self#chooseFileOrDir ok_not_exists false
1071 method private chooseDir ?(ok_not_exists = false) () =
1072 (* we should check that this is a directory *)
1073 self#chooseFileOrDir ok_not_exists true
1078 let g = new gui () in
1079 let rg = (g :> MatitaGuiTypes.gui) in
1080 MatitaMisc.set_gui rg;
1084 let instance = singleton gui
1086 let non p x = not (p x)
1089 let cols = new GTree.column_list in
1090 let id_col = cols#add Gobject.Data.string in
1091 let dsc_col = cols#add Gobject.Data.string in
1092 let interp_no_col = cols#add Gobject.Data.int in
1093 let tree_store = GTree.tree_store cols in
1094 let id_renderer = GTree.cell_renderer_text [], ["text", id_col] in
1095 let dsc_renderer = GTree.cell_renderer_text [], ["text", dsc_col] in
1096 let id_view_col = GTree.view_column ~renderer:id_renderer () in
1097 let dsc_view_col = GTree.view_column ~renderer:dsc_renderer () in
1098 fun tree_view choices ->
1101 tree_view#set_model (Some (tree_store :> GTree.model));
1102 ignore (tree_view#append_column id_view_col);
1103 ignore (tree_view#append_column dsc_view_col);
1104 let name_of_interp =
1105 (* try to find a reasonable name for an interpretation *)
1109 List.assoc "0" interp
1111 incr idx; string_of_int !idx
1113 tree_store#clear ();
1114 let idx = ref ~-1 in
1118 let interp_row = tree_store#append () in
1119 tree_store#set ~row:interp_row ~column:id_col
1120 (name_of_interp interp);
1121 tree_store#set ~row:interp_row ~column:interp_no_col !idx;
1124 let row = tree_store#append ~parent:interp_row () in
1125 tree_store#set ~row ~column:id_col id;
1126 tree_store#set ~row ~column:dsc_col dsc;
1127 tree_store#set ~row ~column:interp_no_col !idx)
1131 method get_interp_no tree_path =
1132 let iter = tree_store#get_iter tree_path in
1133 tree_store#get ~row:iter ~column:interp_no_col
1137 let interactive_string_choice
1138 text prefix_len ?(title = "") ?(msg = "") () ~id locs uris
1140 let dialog = new uriChoiceDialog () in
1141 dialog#uriEntryHBox#misc#hide ();
1142 dialog#uriChoiceSelectedButton#misc#hide ();
1143 dialog#uriChoiceAutoButton#misc#hide ();
1144 dialog#uriChoiceConstantsButton#misc#hide ();
1145 dialog#uriChoiceTreeView#selection#set_mode
1146 (`SINGLE :> Gtk.Tags.selection_mode);
1147 let model = new stringListModel dialog#uriChoiceTreeView in
1148 let choices = ref None in
1149 dialog#uriChoiceDialog#set_title title;
1150 let hack_len = MatitaGtkMisc.utf8_string_length text in
1151 let rec colorize acc_len = function
1153 let floc = HExtlib.floc_of_loc (acc_len,hack_len) in
1154 escape_pango_markup (fst(MatitaGtkMisc.utf8_parsed_text text floc))
1156 let start, stop = HExtlib.loc_of_floc he in
1157 let floc1 = HExtlib.floc_of_loc (acc_len,start) in
1158 let str1,_=MatitaGtkMisc.utf8_parsed_text text floc1 in
1159 let str2,_ = MatitaGtkMisc.utf8_parsed_text text he in
1160 escape_pango_markup str1 ^ "<b>" ^
1161 escape_pango_markup str2 ^ "</b>" ^
1164 (* List.iter (fun l -> let start, stop = HExtlib.loc_of_floc l in
1165 Printf.eprintf "(%d,%d)" start stop) locs; *)
1169 fst (HExtlib.loc_of_floc loc1) - fst (HExtlib.loc_of_floc loc2))
1172 (* prerr_endline "XXXXXXXXXXXXXXXXXXXX";
1173 List.iter (fun l -> let start, stop = HExtlib.loc_of_floc l in
1174 Printf.eprintf "(%d,%d)" start stop) locs;
1175 prerr_endline "XXXXXXXXXXXXXXXXXXXX2"; *)
1176 dialog#uriChoiceLabel#set_use_markup true;
1177 let txt = colorize 0 locs in
1178 let txt,_ = MatitaGtkMisc.utf8_parsed_text txt
1179 (HExtlib.floc_of_loc (prefix_len,MatitaGtkMisc.utf8_string_length txt))
1181 dialog#uriChoiceLabel#set_label txt;
1182 List.iter model#easy_append uris;
1185 dialog#uriChoiceDialog#destroy ();
1188 ignore (dialog#uriChoiceDialog#event#connect#delete (fun _ -> true));
1189 connect_button dialog#uriChoiceForwardButton (fun _ ->
1190 match model#easy_selection () with
1192 | uris -> return (Some uris));
1193 connect_button dialog#uriChoiceAbortButton (fun _ -> return None);
1194 dialog#uriChoiceDialog#show ();
1196 (match !choices with
1197 | None -> raise MatitaTypes.Cancel
1198 | Some uris -> uris)
1200 let interactive_interp_choice () text prefix_len choices =
1201 (*List.iter (fun l -> prerr_endline "==="; List.iter (fun (_,id,dsc) -> prerr_endline (id ^ " = " ^ dsc)) l) choices;*)
1202 let filter_choices filter =
1203 let rec is_compatible filter =
1206 | ([],_,_)::tl -> is_compatible filter tl
1207 | (loc::tlloc,id,dsc)::tl ->
1209 if List.assoc (loc,id) filter = dsc then
1210 is_compatible filter ((tlloc,id,dsc)::tl)
1216 List.filter (fun (_,interp) -> is_compatible filter interp)
1218 let rec get_choices loc id =
1223 List.find (fun (locs,id',_) -> id = id' && List.mem loc locs) he
1225 dsc :: (List.filter (fun dsc' -> dsc <> dsc') (get_choices loc id tl))
1227 let example_interp =
1231 let ask_user id locs choices =
1232 interactive_string_choice
1234 ~title:"Ambiguous input"
1235 ~msg:("Choose an interpretation for " ^ id) () ~id locs choices
1237 let rec classify ids filter partial_interpretations =
1239 [] -> List.map fst partial_interpretations
1240 | ([],_,_)::tl -> classify tl filter partial_interpretations
1241 | (loc::tlloc,id,dsc)::tl ->
1242 let choices = get_choices loc id partial_interpretations in
1245 [] -> prerr_endline ("NO CHOICES FOR " ^ id); assert false
1248 match ask_user id [loc] choices with
1252 let filter = ((loc,id),chosen_dsc)::filter in
1253 let compatible_interps = filter_choices filter partial_interpretations in
1254 classify ((tlloc,id,dsc)::tl) filter compatible_interps
1256 let enumerated_choices =
1257 let idx = ref ~-1 in
1258 List.map (fun interp -> incr idx; !idx,interp) choices
1260 classify example_interp [] enumerated_choices
1263 (* disambiguator callbacks *)
1264 Disambiguate.set_choose_uris_callback
1265 (fun ~selection_mode ?ok ?(enable_button_for_non_vars=false) ~title ~msg ->
1266 interactive_uri_choice ~selection_mode ?ok_label:ok ~title ~msg ());
1267 Disambiguate.set_choose_interp_callback (interactive_interp_choice ());
1268 (* gtk initialization *)
1269 GtkMain.Rc.add_default_file BuildTimeConf.gtkrc_file (* loads gtk rc *)