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 gui_instance = ref None
38 class type browserWin =
39 (* this class exists only because GEdit.combo_box_entry is not supported by
40 * lablgladecc :-(((( *)
42 inherit MatitaGeneratedGui.browserWin
43 method browserUri: GEdit.combo_box_entry
46 class console ~(buffer: GText.buffer) () =
48 val error_tag = buffer#create_tag [ `FOREGROUND "red" ]
49 val warning_tag = buffer#create_tag [ `FOREGROUND "orange" ]
50 val message_tag = buffer#create_tag []
51 val debug_tag = buffer#create_tag [ `FOREGROUND "#888888" ]
52 method message s = buffer#insert ~iter:buffer#end_iter ~tags:[message_tag] s
53 method error s = buffer#insert ~iter:buffer#end_iter ~tags:[error_tag] s
54 method warning s = buffer#insert ~iter:buffer#end_iter ~tags:[warning_tag] s
55 method debug s = buffer#insert ~iter:buffer#end_iter ~tags:[debug_tag] s
57 buffer#delete ~start:buffer#start_iter ~stop:buffer#end_iter
58 method log_callback (tag: HLog.log_tag) s =
60 | `Debug -> self#debug (s ^ "\n")
61 | `Error -> self#error (s ^ "\n")
62 | `Message -> self#message (s ^ "\n")
63 | `Warning -> self#warning (s ^ "\n")
66 let clean_current_baseuri grafite_status =
68 let baseuri = GrafiteTypes.get_string_option grafite_status "baseuri" in
69 LibraryClean.clean_baseuris [baseuri]
70 with GrafiteTypes.Option_error _ -> ()
72 let ask_and_save_moo_if_needed parent fname lexicon_status grafite_status =
74 try Some (GrafiteTypes.get_string_option grafite_status "baseuri")
75 with GrafiteTypes.Option_error _ -> None
77 if (MatitaScript.current ())#eos &&
78 grafite_status.GrafiteTypes.proof_status = GrafiteTypes.No_proof &&
82 let baseuri = match baseuri with Some b -> b | None -> assert false in
84 LibraryMisc.obj_file_of_baseuri ~must_exist:false ~baseuri
88 LibraryMisc.lexicon_file_of_baseuri
89 ~must_exist:false ~baseuri ~writable:true
91 GrafiteMarshal.save_moo moo_fname
92 grafite_status.GrafiteTypes.moo_content_rev;
93 LexiconMarshal.save_lexicon lexicon_fname
94 lexicon_status.LexiconEngine.lexicon_content_rev
98 MatitaGtkMisc.ask_confirmation
99 ~title:"A .moo can be generated"
100 ~message:(Printf.sprintf
101 "%s can be generated for %s.\n<i>Should I generate it?</i>"
102 (Filename.basename moo_fname) (Filename.basename fname))
109 | `CANCEL -> raise MatitaTypes.Cancel
114 clean_current_baseuri grafite_status
118 clean_current_baseuri grafite_status
120 let ask_unsaved parent =
121 MatitaGtkMisc.ask_confirmation
122 ~parent ~title:"Unsaved work!"
123 ~message:("Your work is <b>unsaved</b>!\n\n"^
124 "<i>Do you want to save the script before continuing?</i>")
127 class interpErrorModel =
128 let cols = new GTree.column_list in
129 let id_col = cols#add Gobject.Data.string in
130 let dsc_col = cols#add Gobject.Data.string in
131 let interp_no_col = cols#add Gobject.Data.caml in
132 let tree_store = GTree.tree_store cols in
133 let id_renderer = GTree.cell_renderer_text [], ["text", id_col] in
134 let dsc_renderer = GTree.cell_renderer_text [], ["text", dsc_col] in
135 let id_view_col = GTree.view_column ~renderer:id_renderer () in
136 let dsc_view_col = GTree.view_column ~renderer:dsc_renderer () in
137 fun (tree_view: GTree.view) choices ->
140 tree_view#set_model (Some (tree_store :> GTree.model));
141 ignore (tree_view#append_column id_view_col);
142 ignore (tree_view#append_column dsc_view_col);
144 let idx1 = ref ~-1 in
149 if List.length choices = 1 then
152 (let loc_row = tree_store#append () in
155 [passes,envs_and_diffs,_,_] ->
156 tree_store#set ~row:loc_row ~column:id_col
157 ("Error location " ^ string_of_int (!idx1+1) ^
158 ", error message " ^ string_of_int (!idx1+1) ^ ".1" ^
160 String.concat " " (List.map string_of_int passes) ^
162 tree_store#set ~row:loc_row ~column:interp_no_col
165 tree_store#set ~row:loc_row ~column:id_col
166 ("Error location " ^ string_of_int (!idx1+1));
167 tree_store#set ~row:loc_row ~column:interp_no_col
171 let idx2 = ref ~-1 in
173 (fun passes,envs_and_diffs,_,_ ->
176 if List.length lll = 1 then
179 let msg_row = tree_store#append ?parent:loc_row () in
180 (tree_store#set ~row:msg_row ~column:id_col
181 ("Error message " ^ string_of_int (!idx1+1) ^ "." ^
182 string_of_int (!idx2+1) ^
184 String.concat " " (List.map string_of_int passes) ^
186 tree_store#set ~row:msg_row ~column:interp_no_col
187 (!idx1,Some !idx2,None);
189 let idx3 = ref ~-1 in
191 (fun (passes,env,_) ->
194 match envs_and_diffs with
196 let interp_row = tree_store#append ?parent:msg_row () in
197 tree_store#set ~row:interp_row ~column:id_col
198 ("Interpretation " ^ string_of_int (!idx3+1) ^
200 String.concat " " (List.map string_of_int passes) ^
202 tree_store#set ~row:interp_row ~column:interp_no_col
203 (!idx1,Some !idx2,Some !idx3);
210 let row = tree_store#append ?parent:interp_row () in
211 tree_store#set ~row ~column:id_col id;
212 tree_store#set ~row ~column:dsc_col dsc;
213 tree_store#set ~row ~column:interp_no_col
214 (!idx1,Some !idx2,Some !idx3)
218 if List.length lll > 1 then
220 (fun p -> tree_view#expand_row (tree_store#get_path p))
224 method get_interp_no tree_path =
225 let iter = tree_store#get_iter tree_path in
226 tree_store#get ~row:iter ~column:interp_no_col
230 let rec interactive_error_interp ?(all_passes=false) (source_buffer:GSourceView.source_buffer) notify_exn offset errorll
232 assert (List.flatten errorll <> []);
234 let remove_non_significant =
235 List.filter (fun (_env,_diff,_loc,_msg,significant) -> significant) in
236 let annotated_errorll () =
239 (List.fold_left (fun (pass,res) item -> pass+1,(pass+1,item)::res) (0,[])
241 if all_passes then annotated_errorll () else
242 let safe_list_nth l n = try List.nth l n with Failure _ -> [] in
243 (* We remove passes 1,2 and 5,6 *)
246 ::(3,remove_non_significant (safe_list_nth errorll 2))
247 ::(4,remove_non_significant (safe_list_nth errorll 3))
250 if List.flatten (List.map snd res) <> [] then res
252 (* all errors (if any) are not significant: we keep them *)
255 ::(3,(safe_list_nth errorll 2))
256 ::(4,(safe_list_nth errorll 3))
259 if List.flatten (List.map snd res) <> [] then
262 "All disambiguation errors are not significant. Showing them anyway." ;
268 "No errors in phases 2 and 3. Showing all errors in all phases" ;
277 (fun (env,diff,offset,msg,significant) ->
278 offset, [[pass], [[pass], env, diff], msg, significant]) l
280 (* Here we are doing a stable sort and list_uniq returns the latter
281 "equal" element. I.e. we are showing the error corresponding to the
282 most advanced disambiguation pass *)
284 let choices_compare (o1,_) (o2,_) = compare o1 o2 in
285 let choices_compare_by_passes (p1,_,_,_) (p2,_,_,_) =
291 | (o1,res1)::(o2,res2)::tl when o1 = o2 ->
292 let merge_by_name errors =
293 let merge_by_env errors =
294 let choices_compare_by_env (_,e1,_) (_,e2,_) = compare e1 e2 in
295 let choices_compare_by_passes (p1,_,_) (p2,_,_) =
297 let rec uniq_by_env =
301 | (p1,e1,_)::(p2,e2,d2)::tl when e1 = e2 ->
302 uniq_by_env ((p1@p2,e2,d2) :: tl)
303 | h1::tl -> h1 :: uniq_by_env tl
305 List.sort choices_compare_by_passes
306 (uniq_by_env (List.stable_sort choices_compare_by_env errors))
308 let choices_compare_by_msg (_,_,m1,_) (_,_,m2,_) =
309 compare (Lazy.force m1) (Lazy.force m2) in
310 let rec uniq_by_msg =
314 | (p1,i1,m1,s1)::(p2,i2,m2,s2)::tl
315 when Lazy.force m1 = Lazy.force m2 && s1 = s2 ->
316 uniq_by_msg ((p1@p2,merge_by_env (i1@i2),m2,s2) :: tl)
317 | h1::tl -> h1 :: uniq_by_msg tl
319 List.sort choices_compare_by_msg
320 (uniq_by_msg (List.stable_sort choices_compare_by_msg errors))
322 let res = merge_by_name (res1@res2) in
323 uniq ((o1,res) :: tl)
324 | h1::tl -> h1 :: uniq tl
326 (* Errors in phase 3 that are not also in phase 4 are filtered out *)
327 let filter_phase_3 choices =
328 if all_passes then choices
333 (loffset,messages) ->
334 let filtered_messages =
341 if filtered_messages = [] then
344 Some (loffset,filtered_messages))
349 (List.map (fun o,l -> o,List.sort choices_compare_by_passes l)
350 (uniq (List.stable_sort choices_compare choices)))
354 | [loffset,[_,envs_and_diffs,msg,significant]] ->
355 let _,env,diff = List.hd envs_and_diffs in
357 (GrafiteDisambiguator.DisambiguationError
358 (offset,[[env,diff,loffset,msg,significant]]));
360 let dialog = new disambiguationErrors () in
361 dialog#check_widgets ();
363 dialog#disambiguationErrorsMoreErrors#misc#set_sensitive false;
364 let model = new interpErrorModel dialog#treeview choices in
365 dialog#disambiguationErrors#set_title "Disambiguation error";
366 dialog#disambiguationErrorsLabel#set_label
367 "Click on an error to see the corresponding message:";
368 ignore (dialog#treeview#connect#cursor_changed
371 match fst (dialog#treeview#get_cursor ()) with
374 let idx1,idx2,idx3 = model#get_interp_no tree_path in
375 let loffset,lll = List.nth choices idx1 in
376 let _,envs_and_diffs,msg,significant =
378 Some idx2 -> List.nth lll idx2
380 [],[],lazy "Multiple error messages. Please select one.",true
384 Some idx3 -> List.nth envs_and_diffs idx3
385 | None -> [],[],[] (* dymmy value, used *) in
386 let script = MatitaScript.current () in
387 let error_tag = script#error_tag in
388 source_buffer#remove_tag error_tag
389 ~start:source_buffer#start_iter
390 ~stop:source_buffer#end_iter;
392 (GrafiteDisambiguator.DisambiguationError
393 (offset,[[env,diff,loffset,msg,significant]]))
396 dialog#disambiguationErrors#destroy ();
399 let fail _ = return () in
400 ignore(dialog#disambiguationErrors#event#connect#delete (fun _ -> true));
401 connect_button dialog#disambiguationErrorsOkButton
404 match fst (dialog#treeview#get_cursor ()) with
407 let idx1,idx2,idx3 = model#get_interp_no tree_path in
410 Some idx2, Some idx3 ->
411 let _,lll = List.nth choices idx1 in
412 let _,envs_and_diffs,_,_ = List.nth lll idx2 in
413 let _,_,diff = List.nth envs_and_diffs idx3 in
415 | _,_ -> assert false
422 DisambiguatePp.pp_environment
423 (DisambiguateTypes.Environment.add k value
424 DisambiguateTypes.Environment.empty))
429 (source_buffer#get_iter_at_mark
430 (`NAME "beginning_of_statement")) newtxt ;
433 connect_button dialog#disambiguationErrorsMoreErrors
434 (fun _ -> return () ;
435 interactive_error_interp ~all_passes:true source_buffer notify_exn offset
437 connect_button dialog#disambiguationErrorsCancelButton fail;
438 dialog#disambiguationErrors#show ();
442 (** Selection handling
443 * Two clipboards are used: "clipboard" and "primary".
444 * "primary" is used by X, when you hit the middle button mouse is content is
445 * pasted between applications. In Matita this selection always contain the
446 * textual version of the selected term.
447 * "clipboard" is used inside Matita only and support ATM two different targets:
448 * "TERM" and "PATTERN", in the future other targets like "MATHMLCONTENT" may
453 (* creation order _is_ relevant for windows placement *)
454 let main = new mainWin () in
455 let fileSel = new fileSelectionWin () in
456 let findRepl = new findReplWin () in
457 let develList = new develListWin () in
458 let newDevel = new newDevelWin () in
459 let keyBindingBoxes = (* event boxes which should receive global key events *)
460 [ main#mainWinEventBox ]
462 let console = new console ~buffer:main#logTextView#buffer () in
463 let (source_view: GSourceView.source_view) =
464 GSourceView.source_view
466 ~insert_spaces_instead_of_tabs:true ~tabs_width:2
467 ~margin:80 ~show_margin:true
469 ~packing:main#scriptScrolledWin#add
472 let default_font_size =
473 Helm_registry.get_opt_default Helm_registry.int
474 ~default:BuildTimeConf.default_font_size "matita.font_size"
476 let source_buffer = source_view#source_buffer in
478 val mutable chosen_file = None
479 val mutable _ok_not_exists = false
480 val mutable _only_directory = false
481 val mutable script_fname = None
482 val mutable font_size = default_font_size
483 val mutable next_devel_must_contain = None
484 val mutable next_ligatures = []
485 val clipboard = GData.clipboard Gdk.Atom.clipboard
486 val primary = GData.clipboard Gdk.Atom.primary
489 (* glade's check widgets *)
490 List.iter (fun w -> w#check_widgets ())
491 (let c w = (w :> <check_widgets: unit -> unit>) in
492 [ c fileSel; c main; c findRepl]);
494 List.iter (* global key bindings *)
495 (fun (key, callback) -> self#addKeyBinding key callback)
498 toggle_win ~check:main#showProofMenuItem proof#proofWin;
500 toggle_win ~check:main#showCheckMenuItem check#checkWin;
504 let parse_txt_file file =
505 let ch = open_in (BuildTimeConf.runtime_base_dir ^ "/" ^ file) in
506 let l_rev = ref [] in
509 l_rev := input_line ch :: !l_rev;
518 ~authors:(parse_txt_file "AUTHORS")
519 (*~comments:"comments"*)
520 ~copyright:"Copyright (C) 2005, the HELM team"
521 ~license:(String.concat "\n" (parse_txt_file "LICENSE"))
522 ~logo:(GdkPixbuf.from_file (MatitaMisc.image_path "/matita_medium.png"))
524 ~version:BuildTimeConf.version
525 ~website:"http://helm.cs.unibo.it"
528 connect_menu_item main#contentsMenuItem (fun () ->
530 sprintf "gnome-help ghelp://%s/C/matita.xml &" BuildTimeConf.help_dir
532 ignore (Sys.command cmd));
533 connect_menu_item main#aboutMenuItem about_dialog#present;
535 let show_find_Repl () =
536 findRepl#toplevel#misc#show ();
537 findRepl#toplevel#misc#grab_focus ()
539 let hide_find_Repl () = findRepl#toplevel#misc#hide () in
541 let highlight start end_ =
542 source_buffer#move_mark `INSERT ~where:start;
543 source_buffer#move_mark `SEL_BOUND ~where:end_;
544 source_view#scroll_mark_onscreen `INSERT
546 let text = findRepl#findEntry#text in
547 let iter = source_buffer#get_iter `SEL_BOUND in
548 match iter#forward_search text with
550 (match source_buffer#start_iter#forward_search text with
552 | Some (start,end_) -> highlight start end_)
553 | Some (start,end_) -> highlight start end_
556 let text = findRepl#replaceEntry#text in
557 let ins = source_buffer#get_iter `INSERT in
558 let sel = source_buffer#get_iter `SEL_BOUND in
559 if ins#compare sel < 0 then
561 ignore(source_buffer#delete_selection ());
562 source_buffer#insert text
565 connect_button findRepl#findButton find_forward;
566 connect_button findRepl#findReplButton replace;
567 connect_button findRepl#cancelButton (fun _ -> hide_find_Repl ());
568 ignore(findRepl#toplevel#event#connect#delete
569 ~callback:(fun _ -> hide_find_Repl ();true));
572 (* phase 1: we save the actual status of the marks and we undo *)
573 let locked_mark = `MARK ((MatitaScript.current ())#locked_mark) in
574 let locked_iter = source_view#buffer#get_iter_at_mark locked_mark in
575 let locked_iter_offset = locked_iter#offset in
578 (source_view#buffer#create_mark ~name:"lock_point"
579 ~left_gravity:true locked_iter) in
580 source_view#source_buffer#undo ();
581 (* phase 2: we save the cursor position and we redo, restoring
582 the previous status of all the marks *)
583 let cursor_iter = source_view#buffer#get_iter_at_mark `INSERT in
586 (source_view#buffer#create_mark ~name:"undo_point"
587 ~left_gravity:true cursor_iter)
589 source_view#source_buffer#redo ();
590 let mark_iter = source_view#buffer#get_iter_at_mark mark in
591 let mark2_iter = source_view#buffer#get_iter_at_mark mark2 in
592 let mark2_iter = mark2_iter#set_offset locked_iter_offset in
593 source_view#buffer#move_mark locked_mark ~where:mark2_iter;
594 source_view#buffer#delete_mark mark;
595 source_view#buffer#delete_mark mark2;
596 (* phase 3: if after the undo the cursor was in the locked area,
597 then we move it there again and we perform a goto *)
598 if mark_iter#offset < locked_iter_offset then
600 source_view#buffer#move_mark `INSERT ~where:mark_iter;
601 (MatitaScript.current ())#goto `Cursor ();
603 (* phase 4: we perform again the undo. This time we are sure that
604 the text to undo is not locked *)
605 source_view#source_buffer#undo ();
606 source_view#misc#grab_focus () in
609 (* phase 1: we save the actual status of the marks, we redo and
611 let locked_mark = `MARK ((MatitaScript.current ())#locked_mark) in
612 let locked_iter = source_view#buffer#get_iter_at_mark locked_mark in
613 let locked_iter_offset = locked_iter#offset in
616 (source_view#buffer#create_mark ~name:"lock_point"
617 ~left_gravity:true locked_iter) in
618 source_view#source_buffer#redo ();
619 source_view#source_buffer#undo ();
620 (* phase 2: we save the cursor position and we restore
621 the previous status of all the marks *)
622 let cursor_iter = source_view#buffer#get_iter_at_mark `INSERT in
625 (source_view#buffer#create_mark ~name:"undo_point"
626 ~left_gravity:true cursor_iter)
628 let mark_iter = source_view#buffer#get_iter_at_mark mark in
629 let mark2_iter = source_view#buffer#get_iter_at_mark mark2 in
630 let mark2_iter = mark2_iter#set_offset locked_iter_offset in
631 source_view#buffer#move_mark locked_mark ~where:mark2_iter;
632 source_view#buffer#delete_mark mark;
633 source_view#buffer#delete_mark mark2;
634 (* phase 3: if after the undo the cursor is in the locked area,
635 then we move it there again and we perform a goto *)
636 if mark_iter#offset < locked_iter_offset then
638 source_view#buffer#move_mark `INSERT ~where:mark_iter;
639 (MatitaScript.current ())#goto `Cursor ();
641 (* phase 4: we perform again the redo. This time we are sure that
642 the text to redo is not locked *)
643 source_view#source_buffer#redo ();
644 source_view#misc#grab_focus ()
646 connect_menu_item main#undoMenuItem safe_undo;
647 ignore(source_view#source_buffer#connect#can_undo
648 ~callback:main#undoMenuItem#misc#set_sensitive);
649 connect_menu_item main#redoMenuItem safe_redo;
650 ignore(source_view#source_buffer#connect#can_redo
651 ~callback:main#redoMenuItem#misc#set_sensitive);
652 ignore(source_view#connect#after#populate_popup
653 ~callback:(fun pre_menu ->
654 let menu = new GMenu.menu pre_menu in
655 let menuItems = menu#children in
656 let undoMenuItem, redoMenuItem =
658 [undo;redo;sep1;cut;copy;paste;delete;sep2;
659 selectall;sep3;inputmethod;insertunicodecharacter] ->
660 List.iter menu#remove [ copy; cut; delete; paste ];
662 | _ -> assert false in
664 let i = ref 2 in (* last occupied position *)
665 fun ?label ?stock () ->
667 GMenu.image_menu_item ?label ?stock ~packing:(menu#insert ~pos:!i)
670 let copy = add_menu_item ~stock:`COPY () in
671 let cut = add_menu_item ~stock:`CUT () in
672 let delete = add_menu_item ~stock:`DELETE () in
673 let paste = add_menu_item ~stock:`PASTE () in
674 let paste_pattern = add_menu_item ~label:"Paste as pattern" () in
675 copy#misc#set_sensitive self#canCopy;
676 cut#misc#set_sensitive self#canCut;
677 delete#misc#set_sensitive self#canDelete;
678 paste#misc#set_sensitive self#canPaste;
679 paste_pattern#misc#set_sensitive self#canPastePattern;
680 connect_menu_item copy self#copy;
681 connect_menu_item cut self#cut;
682 connect_menu_item delete self#delete;
683 connect_menu_item paste self#paste;
684 connect_menu_item paste_pattern self#pastePattern;
685 let new_undoMenuItem =
686 GMenu.image_menu_item
687 ~image:(GMisc.image ~stock:`UNDO ())
690 ~packing:(menu#insert ~pos:0) () in
691 new_undoMenuItem#misc#set_sensitive
692 (undoMenuItem#misc#get_flag `SENSITIVE);
693 menu#remove (undoMenuItem :> GMenu.menu_item);
694 connect_menu_item new_undoMenuItem safe_undo;
695 let new_redoMenuItem =
696 GMenu.image_menu_item
697 ~image:(GMisc.image ~stock:`REDO ())
700 ~packing:(menu#insert ~pos:1) () in
701 new_redoMenuItem#misc#set_sensitive
702 (redoMenuItem#misc#get_flag `SENSITIVE);
703 menu#remove (redoMenuItem :> GMenu.menu_item);
704 connect_menu_item new_redoMenuItem safe_redo));
706 connect_menu_item main#editMenu (fun () ->
707 main#copyMenuItem#misc#set_sensitive self#canCopy;
708 main#cutMenuItem#misc#set_sensitive self#canCut;
709 main#deleteMenuItem#misc#set_sensitive self#canDelete;
710 main#pasteMenuItem#misc#set_sensitive self#canPaste;
711 main#pastePatternMenuItem#misc#set_sensitive self#canPastePattern);
712 connect_menu_item main#copyMenuItem self#copy;
713 connect_menu_item main#cutMenuItem self#cut;
714 connect_menu_item main#deleteMenuItem self#delete;
715 connect_menu_item main#pasteMenuItem self#paste;
716 connect_menu_item main#pastePatternMenuItem self#pastePattern;
717 connect_menu_item main#selectAllMenuItem (fun () ->
718 source_buffer#move_mark `INSERT source_buffer#start_iter;
719 source_buffer#move_mark `SEL_BOUND source_buffer#end_iter);
720 connect_menu_item main#findReplMenuItem show_find_Repl;
721 connect_menu_item main#externalEditorMenuItem self#externalEditor;
722 connect_menu_item main#ligatureButton self#nextLigature;
723 ignore (findRepl#findEntry#connect#activate find_forward);
724 (* interface lockers *)
726 main#buttonsToolbar#misc#set_sensitive false;
727 develList#buttonsHbox#misc#set_sensitive false;
728 main#scriptMenu#misc#set_sensitive false;
729 source_view#set_editable false
732 main#buttonsToolbar#misc#set_sensitive true;
733 develList#buttonsHbox#misc#set_sensitive true;
734 main#scriptMenu#misc#set_sensitive true;
735 source_view#set_editable true;
736 (*The next line seems sufficient to avoid some unknown race condition *)
737 GtkThread.sync (fun () -> ()) ()
739 let worker_thread = ref None in
741 let floc, msg = MatitaExcPp.to_string exn in
746 let (x, y) = HExtlib.loc_of_floc floc in
747 let script = MatitaScript.current () in
748 let locked_mark = script#locked_mark in
749 let error_tag = script#error_tag in
751 (source_buffer#get_iter_at_mark (`MARK locked_mark))#offset in
752 let x' = baseoffset + x in
753 let y' = baseoffset + y in
754 let x_iter = source_buffer#get_iter (`OFFSET x') in
755 let y_iter = source_buffer#get_iter (`OFFSET y') in
756 source_buffer#apply_tag error_tag ~start:x_iter ~stop:y_iter;
758 id := Some (source_buffer#connect#changed ~callback:(fun () ->
759 source_buffer#remove_tag error_tag
760 ~start:source_buffer#start_iter
761 ~stop:source_buffer#end_iter;
763 | None -> assert false (* a race condition occurred *)
765 (new GObj.gobject_ops source_buffer#as_buffer)#disconnect id));
766 source_buffer#place_cursor
767 (source_buffer#get_iter (`OFFSET x'));
778 | GrafiteDisambiguator.DisambiguationError (offset,errorll) ->
780 interactive_error_interp source_buffer notify_exn offset
783 exc -> notify_exn exc);
790 worker_thread := Some (Thread.create thread_main ())
793 (* the following lines are from Xavier Leroy: http://alan.petitepomme.net/cwn/2005.11.08.html *)
794 let interrupt = ref None in
795 let old_callback = ref (function _ -> ()) in
796 let force_interrupt n =
797 (* This function is called just before the thread's timeslice ends *)
799 if Some(Thread.id(Thread.self())) = !interrupt then
800 (interrupt := None; raise Sys.Break) in
802 match Sys.signal Sys.sigvtalrm (Sys.Signal_handle force_interrupt) with
803 Sys.Signal_handle f -> old_callback := f
805 | Sys.Signal_default -> assert false
808 match !worker_thread with
810 | Some t -> interrupt := Some (Thread.id t) in
814 f (); source_view#misc#grab_focus ()
816 exc -> source_view#misc#grab_focus (); raise exc in
817 (* developments win *)
819 new MatitaGtkMisc.multiStringListModel
820 ~cols:2 develList#developmentsTreeview
822 let refresh_devels_win () =
823 model#list_store#clear ();
825 (fun (name, root) -> model#easy_mappend [name;root])
826 (MatitamakeLib.list_known_developments ())
828 let get_devel_selected () =
829 match model#easy_mselection () with
830 | [[name;_]] -> MatitamakeLib.development_for_name name
834 while Glib.Main.pending () do
835 ignore(Glib.Main.iteration false);
838 connect_button develList#newButton
840 next_devel_must_contain <- None;
841 newDevel#toplevel#misc#show());
842 connect_button develList#deleteButton
844 (match get_devel_selected () with
846 | Some d -> MatitamakeLib.destroy_development_in_bg refresh d);
847 refresh_devels_win ()));
848 connect_button develList#buildButton
850 match get_devel_selected () with
854 (fun () -> MatitamakeLib.build_development_in_bg refresh d)
857 connect_button develList#cleanButton
859 match get_devel_selected () with
863 (fun () -> MatitamakeLib.clean_development_in_bg refresh d)
866 (* publish button hidden, use command line
867 connect_button develList#publishButton
869 match get_devel_selected () with
872 let publish = locker (fun () ->
873 MatitamakeLib.publish_development_in_bg refresh d) in
874 ignore(publish ())));
876 develList#publishButton#misc#hide ();
877 connect_button develList#graphButton (fun () ->
878 match get_devel_selected () with
881 (match MatitamakeLib.dot_for_development d with
884 let browser = MatitaMathView.cicBrowser () in
885 browser#load (`Development
886 (MatitamakeLib.name_for_development d))));
887 connect_button develList#closeButton
888 (fun () -> develList#toplevel#misc#hide());
889 ignore(develList#toplevel#event#connect#delete
890 (fun _ -> develList#toplevel#misc#hide();true));
891 connect_menu_item main#developmentsMenuItem
892 (fun () -> refresh_devels_win ();develList#toplevel#misc#show ());
894 (* add development win *)
895 let check_if_root_contains root =
896 match next_devel_must_contain with
899 let is_prefix_of d1 d2 =
900 let d1 = MatitamakeLib.normalize_path d1 in
901 let d2 = MatitamakeLib.normalize_path d2 in
902 let len1 = String.length d1 in
903 let len2 = String.length d2 in
907 let pref = String.sub d2 0 len1 in
910 is_prefix_of root path
912 connect_button newDevel#addButton
914 let name = newDevel#nameEntry#text in
915 let root = newDevel#rootEntry#text in
916 if check_if_root_contains root then
918 ignore (MatitamakeLib.initialize_development name root);
919 refresh_devels_win ();
920 newDevel#nameEntry#set_text "";
921 newDevel#rootEntry#set_text "";
922 newDevel#toplevel#misc#hide()
925 HLog.error ("The selected root does not contain " ^
926 match next_devel_must_contain with
928 | _ -> assert false));
929 connect_button newDevel#chooseRootButton
931 let path = self#chooseDir () in
933 | Some path -> newDevel#rootEntry#set_text path
935 connect_button newDevel#cancelButton
936 (fun () -> newDevel#toplevel#misc#hide ());
937 ignore(newDevel#toplevel#event#connect#delete
938 (fun _ -> newDevel#toplevel#misc#hide();true));
940 (* file selection win *)
941 ignore (fileSel#fileSelectionWin#event#connect#delete (fun _ -> true));
942 ignore (fileSel#fileSelectionWin#connect#response (fun event ->
945 fileSel#fileSelectionWin#misc#hide ();
950 let fname = fileSel#fileSelectionWin#filename in
951 if Sys.file_exists fname then
953 if HExtlib.is_regular fname && not (_only_directory) then
955 else if _only_directory && HExtlib.is_dir fname then
960 if _ok_not_exists then
963 | `CANCEL -> return None
965 | `DELETE_EVENT -> return None));
967 List.iter (fun w -> w#misc#set_sensitive false) [ main#saveMenuItem ];
969 let adj = main#logScrolledWin#vadjustment in
970 ignore (adj#connect#changed
971 (fun _ -> adj#set_value (adj#upper -. adj#page_size)));
972 console#message (sprintf "\tMatita version %s\n" BuildTimeConf.version);
974 main#tacticsButtonsHandlebox#misc#hide ();
975 main#tacticsBarMenuItem#misc#hide ();
976 main#scriptNotebook#remove_page 1;
977 main#scriptNotebook#set_show_tabs false;
978 (* / TO BE REMOVED *)
979 let module Hr = Helm_registry in
980 MatitaGtkMisc.toggle_callback ~check:main#fullscreenMenuItem
982 | true -> main#toplevel#fullscreen ()
983 | false -> main#toplevel#unfullscreen ());
984 main#fullscreenMenuItem#set_active false;
985 MatitaGtkMisc.toggle_callback ~check:main#ppNotationMenuItem
988 CicNotation.set_active_notations
989 (List.map fst (CicNotation.get_all_notations ()))
991 CicNotation.set_active_notations []);
992 MatitaGtkMisc.toggle_callback ~check:main#hideCoercionsMenuItem
993 ~callback:(fun enabled -> Acic2content.hide_coercions := enabled);
994 MatitaGtkMisc.toggle_callback ~check:main#unicodeAsTexMenuItem
995 ~callback:(fun enabled ->
996 Helm_registry.set_bool "matita.paste_unicode_as_tex" enabled);
997 main#unicodeAsTexMenuItem#set_active
998 (Helm_registry.get_bool "matita.paste_unicode_as_tex");
1000 HLog.set_log_callback self#console#log_callback;
1001 GtkSignal.user_handler :=
1003 | MatitaScript.ActionCancelled s -> HLog.error s
1005 if not (Helm_registry.get_bool "matita.debug") then
1009 ignore (source_buffer#connect#mark_set (fun _ _ -> next_ligatures <- []));
1011 match GSourceView.source_language_from_file BuildTimeConf.lang_file with
1013 HLog.warn (sprintf "can't load language file %s"
1014 BuildTimeConf.lang_file)
1015 | Some matita_lang ->
1016 source_buffer#set_language matita_lang;
1017 source_buffer#set_highlight true
1019 let s () = MatitaScript.current () in
1020 let disableSave () =
1021 script_fname <- None;
1022 main#saveMenuItem#misc#set_sensitive false
1024 let saveAsScript () =
1025 let script = s () in
1026 match self#chooseFile ~ok_not_exists:true () with
1028 script#assignFileName f;
1029 script#saveToFile ();
1030 console#message ("'"^f^"' saved.\n");
1031 self#_enableSaveTo f
1035 match script_fname with
1036 | None -> saveAsScript ()
1038 (s ())#assignFileName f;
1039 (s ())#saveToFile ();
1040 console#message ("'"^f^"' saved.\n");
1042 let abandon_script () =
1043 let lexicon_status = (s ())#lexicon_status in
1044 let grafite_status = (s ())#grafite_status in
1045 if source_view#buffer#modified then
1046 (match ask_unsaved main#toplevel with
1047 | `YES -> saveScript ()
1049 | `CANCEL -> raise MatitaTypes.Cancel);
1050 (match script_fname with
1053 ask_and_save_moo_if_needed main#toplevel fname
1054 lexicon_status grafite_status);
1057 let script = s () in
1059 match self#chooseFile () with
1063 script#assignFileName f;
1064 source_view#source_buffer#begin_not_undoable_action ();
1065 script#loadFromFile f;
1066 source_view#source_buffer#end_not_undoable_action ();
1067 console#message ("'"^f^"' loaded.\n");
1068 self#_enableSaveTo f
1070 with MatitaTypes.Cancel -> ()
1074 source_view#source_buffer#begin_not_undoable_action ();
1077 source_view#source_buffer#end_not_undoable_action ();
1079 script_fname <- None
1082 source_buffer#place_cursor
1083 (source_buffer#get_iter_at_mark (`NAME "locked")) in
1084 let advance _ = (MatitaScript.current ())#advance (); cursor () in
1085 let retract _ = (MatitaScript.current ())#retract (); cursor () in
1086 let top _ = (MatitaScript.current ())#goto `Top (); cursor () in
1087 let bottom _ = (MatitaScript.current ())#goto `Bottom (); cursor () in
1088 let jump _ = (MatitaScript.current ())#goto `Cursor (); cursor () in
1089 let advance = locker (keep_focus advance) in
1090 let retract = locker (keep_focus retract) in
1091 let top = locker (keep_focus top) in
1092 let bottom = locker (keep_focus bottom) in
1093 let jump = locker (keep_focus jump) in
1095 self#setQuitCallback (fun () ->
1096 let lexicon_status = (MatitaScript.current ())#lexicon_status in
1097 let grafite_status = (MatitaScript.current ())#grafite_status in
1098 if source_view#buffer#modified then
1100 let rc = ask_unsaved main#toplevel in
1103 | `YES -> saveScript ();
1104 if not source_view#buffer#modified then
1106 (match script_fname with
1109 ask_and_save_moo_if_needed main#toplevel
1110 fname lexicon_status grafite_status);
1113 | `NO -> GMain.Main.quit ()
1114 | `CANCEL -> raise MatitaTypes.Cancel
1115 with MatitaTypes.Cancel -> ()
1119 (match script_fname with
1120 | None -> clean_current_baseuri grafite_status; GMain.Main.quit ()
1123 ask_and_save_moo_if_needed main#toplevel fname lexicon_status
1126 with MatitaTypes.Cancel -> ())
1128 connect_button main#scriptAdvanceButton advance;
1129 connect_button main#scriptRetractButton retract;
1130 connect_button main#scriptTopButton top;
1131 connect_button main#scriptBottomButton bottom;
1132 connect_button main#scriptJumpButton jump;
1133 connect_button main#scriptAbortButton kill_worker;
1134 connect_menu_item main#scriptAdvanceMenuItem advance;
1135 connect_menu_item main#scriptRetractMenuItem retract;
1136 connect_menu_item main#scriptTopMenuItem top;
1137 connect_menu_item main#scriptBottomMenuItem bottom;
1138 connect_menu_item main#scriptJumpMenuItem jump;
1139 connect_menu_item main#openMenuItem loadScript;
1140 connect_menu_item main#saveMenuItem saveScript;
1141 connect_menu_item main#saveAsMenuItem saveAsScript;
1142 connect_menu_item main#newMenuItem newScript;
1143 connect_menu_item main#showCoercionsGraphMenuItem
1145 let c = MatitaMathView.cicBrowser () in
1146 c#load (`About `Coercions));
1147 connect_menu_item main#showAutoGuiMenuItem
1148 (fun _ -> MatitaAutoGui.auto_dialog Auto.get_auto_status);
1149 (* script monospace font stuff *)
1150 self#updateFontSize ();
1152 main#debugMenu#misc#hide ();
1154 main#hintNotebook#misc#hide ();
1156 main#hintLowImage#set_file (image_path "matita-bulb-low.png");
1157 main#hintMediumImage#set_file (image_path "matita-bulb-medium.png");
1158 main#hintHighImage#set_file (image_path "matita-bulb-high.png");
1161 self#sourceView#misc#grab_focus ();
1162 (* main win dimension *)
1163 let width = Gdk.Screen.width () in
1164 let height = Gdk.Screen.height () in
1165 let main_w = width * 90 / 100 in
1166 let main_h = height * 80 / 100 in
1167 let script_w = main_w * 6 / 10 in
1168 main#toplevel#resize ~width:main_w ~height:main_h;
1169 main#hpaneScriptSequent#set_position script_w;
1171 ignore(source_view#connect#after#paste_clipboard
1172 ~callback:(fun () -> (MatitaScript.current ())#clean_dirty_lock));
1173 (* clean_locked is set to true only "during" a PRIMARY paste
1174 operation (i.e. by clicking with the second mouse button) *)
1175 let clean_locked = ref false in
1176 ignore(source_view#event#connect#button_press
1179 if GdkEvent.Button.button button = 2 then
1180 clean_locked := true;
1183 ignore(source_view#event#connect#button_release
1184 ~callback:(fun button -> clean_locked := false; false));
1185 ignore(source_view#buffer#connect#after#apply_tag
1187 fun tag ~start:_ ~stop:_ ->
1189 tag#get_oid = (MatitaScript.current ())#locked_tag#get_oid
1192 clean_locked := false;
1193 (MatitaScript.current ())#clean_dirty_lock;
1194 clean_locked := true
1196 (* math view handling *)
1197 connect_menu_item main#newCicBrowserMenuItem (fun () ->
1198 ignore (MatitaMathView.cicBrowser ()));
1199 connect_menu_item main#increaseFontSizeMenuItem (fun () ->
1200 self#increaseFontSize ();
1201 MatitaMathView.increase_font_size ();
1202 MatitaMathView.update_font_sizes ());
1203 connect_menu_item main#decreaseFontSizeMenuItem (fun () ->
1204 self#decreaseFontSize ();
1205 MatitaMathView.decrease_font_size ();
1206 MatitaMathView.update_font_sizes ());
1207 connect_menu_item main#normalFontSizeMenuItem (fun () ->
1208 self#resetFontSize ();
1209 MatitaMathView.reset_font_size ();
1210 MatitaMathView.update_font_sizes ());
1211 MatitaMathView.reset_font_size ();
1213 (** selections / clipboards handling *)
1215 method markupSelected = MatitaMathView.has_selection ()
1216 method private textSelected =
1217 (source_buffer#get_iter_at_mark `INSERT)#compare
1218 (source_buffer#get_iter_at_mark `SEL_BOUND) <> 0
1219 method private somethingSelected = self#markupSelected || self#textSelected
1220 method private markupStored = MatitaMathView.has_clipboard ()
1221 method private textStored = clipboard#text <> None
1222 method private somethingStored = self#markupStored || self#textStored
1224 method canCopy = self#somethingSelected
1225 method canCut = self#textSelected
1226 method canDelete = self#textSelected
1227 method canPaste = self#somethingStored
1228 method canPastePattern = self#markupStored
1231 if self#textSelected
1233 MatitaMathView.empty_clipboard ();
1234 source_view#buffer#copy_clipboard clipboard;
1236 MatitaMathView.copy_selection ()
1238 source_view#buffer#cut_clipboard clipboard;
1239 MatitaMathView.empty_clipboard ()
1240 method delete () = ignore (source_view#buffer#delete_selection ())
1242 if MatitaMathView.has_clipboard ()
1243 then source_view#buffer#insert (MatitaMathView.paste_clipboard `Term)
1244 else source_view#buffer#paste_clipboard clipboard;
1245 (MatitaScript.current ())#clean_dirty_lock
1246 method pastePattern () =
1247 source_view#buffer#insert (MatitaMathView.paste_clipboard `Pattern)
1249 method private nextLigature () =
1250 let iter = source_buffer#get_iter_at_mark `INSERT in
1251 let write_ligature len s =
1252 assert(Glib.Utf8.validate s);
1253 source_buffer#delete ~start:iter ~stop:(iter#copy#backward_chars len);
1254 source_buffer#insert ~iter:(source_buffer#get_iter_at_mark `INSERT) s
1256 let get_ligature word =
1257 let len = String.length word in
1260 for i = len - 1 downto 0 do
1261 if HExtlib.is_alpha word.[i] then ()
1263 (if word.[i] = '\\' then raise (Found i) else raise (Found ~-1))
1267 if i = ~-1 then None else Some (String.sub word i (len - i))
1269 let aux_ligature () =
1271 for i = len - 1 downto 0 do
1272 if CicNotationLexer.is_ligature_char word.[i] then ()
1273 else raise (Found (i+1))
1279 Some (String.sub word i (len - i))
1280 with Invalid_argument _ -> None)
1282 match aux_tex () with
1283 | Some macro -> macro
1284 | None -> (match aux_ligature () with Some l -> l | None -> word)
1286 (match next_ligatures with
1287 | [] -> (* find ligatures and fill next_ligatures, then try again *)
1290 ~stop:(iter#copy#backward_find_char Glib.Unichar.isspace)
1292 let ligature = get_ligature last_word in
1293 (match CicNotationLexer.lookup_ligatures ligature with
1296 write_ligature (MatitaGtkMisc.utf8_string_length ligature) hd;
1297 next_ligatures <- tl @ [ hd ])
1299 write_ligature 1 hd;
1300 next_ligatures <- tl @ [ hd ])
1302 method private externalEditor () =
1303 let cmd = Helm_registry.get "matita.external_editor" in
1304 (* ZACK uncomment to enable interactive ask of external editor command *)
1307 "External editor command:
1308 %f will be substitute for the script name,
1309 %p for the cursor position in bytes,
1310 %l for the execution point in bytes."
1312 ask_text ~gui:self ~title:"External editor" ~msg ~multiline:false
1313 ~default:(Helm_registry.get "matita.external_editor") ()
1315 let fname = (MatitaScript.current ())#filename in
1317 source_buffer#start_iter#get_slice
1318 ~stop:(source_buffer#get_iter_at_mark mark)
1320 let script = MatitaScript.current () in
1321 let locked = `MARK script#locked_mark in
1322 let string_pos mark = string_of_int (String.length (slice mark)) in
1323 let cursor_pos = string_pos `INSERT in
1324 let locked_pos = string_pos locked in
1326 Pcre.replace ~pat:"%f" ~templ:fname
1327 (Pcre.replace ~pat:"%p" ~templ:cursor_pos
1328 (Pcre.replace ~pat:"%l" ~templ:locked_pos
1331 let locked_before = slice locked in
1332 let locked_offset = (source_buffer#get_iter_at_mark locked)#offset in
1333 ignore (Unix.system cmd);
1334 source_buffer#set_text (HExtlib.input_file fname);
1335 let locked_iter = source_buffer#get_iter (`OFFSET locked_offset) in
1336 source_buffer#move_mark locked locked_iter;
1337 source_buffer#apply_tag script#locked_tag
1338 ~start:source_buffer#start_iter ~stop:locked_iter;
1339 let locked_after = slice locked in
1343 for i = 0 to String.length locked_before - 1 do
1344 if locked_before.[i] <> locked_after.[i] then begin
1345 source_buffer#place_cursor
1346 ~where:(source_buffer#get_iter (`LINEBYTE (!line, !col)));
1347 script#goto `Cursor ();
1349 end else if locked_before.[i] = '\n' then begin
1356 | Invalid_argument _ -> script#goto `Bottom ()
1358 method loadScript file =
1359 let script = MatitaScript.current () in
1361 if Pcre.pmatch ~pat:"\\.p$" file then
1364 Helm_registry.get_opt_default Helm_registry.string ~default:"./"
1367 let data = Matitaprover.p_to_ma ~filename:file ~tptppath () in
1368 let filename = Pcre.replace ~pat:"\\.p$" ~templ:".ma" file in
1369 script#assignFileName filename;
1370 source_view#source_buffer#begin_not_undoable_action ();
1371 script#loadFromString data;
1372 source_view#source_buffer#end_not_undoable_action ();
1373 console#message ("'"^filename^"' loaded.");
1374 self#_enableSaveTo filename
1378 script#assignFileName file;
1380 if Sys.file_exists file then file
1381 else BuildTimeConf.script_template
1383 source_view#source_buffer#begin_not_undoable_action ();
1384 script#loadFromFile content;
1385 source_view#source_buffer#end_not_undoable_action ();
1386 console#message ("'"^file^"' loaded.");
1387 self#_enableSaveTo file
1390 method setStar name b =
1391 let w = main#toplevel in
1392 let set x = w#set_title x in
1393 let name = "Matita - " ^ name in
1399 method private _enableSaveTo file =
1400 script_fname <- Some file;
1401 self#main#saveMenuItem#misc#set_sensitive true
1403 method console = console
1404 method sourceView: GSourceView.source_view =
1405 (source_view: GSourceView.source_view)
1406 method fileSel = fileSel
1407 method findRepl = findRepl
1409 method develList = develList
1410 method newDevel = newDevel
1412 method newBrowserWin () =
1414 inherit browserWin ()
1415 val combo = GEdit.combo_box_entry ()
1417 self#check_widgets ();
1418 let combo_widget = combo#coerce in
1419 uriHBox#pack ~from:`END ~fill:true ~expand:true combo_widget;
1420 combo#entry#misc#grab_focus ()
1421 method browserUri = combo
1424 method newUriDialog () =
1425 let dialog = new uriChoiceDialog () in
1426 dialog#check_widgets ();
1429 method newConfirmationDialog () =
1430 let dialog = new confirmationDialog () in
1431 dialog#check_widgets ();
1434 method newEmptyDialog () =
1435 let dialog = new emptyDialog () in
1436 dialog#check_widgets ();
1439 method private addKeyBinding key callback =
1440 List.iter (fun evbox -> add_key_binding key callback evbox)
1443 method setQuitCallback callback =
1444 connect_menu_item main#quitMenuItem callback;
1445 ignore (main#toplevel#event#connect#delete
1446 (fun _ -> callback ();true));
1447 self#addKeyBinding GdkKeysyms._q callback
1449 method chooseFile ?(ok_not_exists = false) () =
1450 _ok_not_exists <- ok_not_exists;
1451 _only_directory <- false;
1452 fileSel#fileSelectionWin#show ();
1456 method private chooseDir ?(ok_not_exists = false) () =
1457 _ok_not_exists <- ok_not_exists;
1458 _only_directory <- true;
1459 fileSel#fileSelectionWin#show ();
1461 (* we should check that this is a directory *)
1464 method createDevelopment ~containing =
1465 next_devel_must_contain <- containing;
1466 newDevel#toplevel#misc#show()
1468 method askText ?(title = "") ?(msg = "") () =
1469 let dialog = new textDialog () in
1470 dialog#textDialog#set_title title;
1471 dialog#textDialogLabel#set_label msg;
1472 let text = ref None in
1475 dialog#textDialog#destroy ();
1478 ignore (dialog#textDialog#event#connect#delete (fun _ -> true));
1479 connect_button dialog#textDialogCancelButton (fun _ -> return None);
1480 connect_button dialog#textDialogOkButton (fun _ ->
1481 let text = dialog#textDialogTextView#buffer#get_text () in
1482 return (Some text));
1483 dialog#textDialog#show ();
1487 method private updateFontSize () =
1488 self#sourceView#misc#modify_font_by_name
1489 (sprintf "%s %d" BuildTimeConf.script_font font_size)
1491 method increaseFontSize () =
1492 font_size <- font_size + 1;
1493 self#updateFontSize ()
1495 method decreaseFontSize () =
1496 font_size <- font_size - 1;
1497 self#updateFontSize ()
1499 method resetFontSize () =
1500 font_size <- default_font_size;
1501 self#updateFontSize ()
1506 let g = new gui () in
1507 gui_instance := Some g;
1508 MatitaMathView.set_gui g;
1511 let instance = singleton gui
1513 let non p x = not (p x)
1515 (* this is a shit and should be changed :-{ *)
1516 let interactive_uri_choice
1517 ?(selection_mode:[`SINGLE|`MULTIPLE] = `MULTIPLE) ?(title = "")
1518 ?(msg = "") ?(nonvars_button = false) ?(hide_uri_entry=false)
1519 ?(hide_try=false) ?(ok_label="_Auto") ?(ok_action:[`SELECT|`AUTO] = `AUTO)
1523 let gui = instance () in
1524 let nonvars_uris = lazy (List.filter (non UriManager.uri_is_var) uris) in
1525 if (selection_mode <> `SINGLE) &&
1526 (Helm_registry.get_opt_default Helm_registry.get_bool ~default:true "matita.auto_disambiguation")
1528 Lazy.force nonvars_uris
1530 let dialog = gui#newUriDialog () in
1531 if hide_uri_entry then
1532 dialog#uriEntryHBox#misc#hide ();
1535 dialog#uriChoiceSelectedButton#misc#hide ();
1536 dialog#uriChoiceConstantsButton#misc#hide ();
1538 dialog#okLabel#set_label ok_label;
1539 dialog#uriChoiceTreeView#selection#set_mode
1540 (selection_mode :> Gtk.Tags.selection_mode);
1541 let model = new stringListModel dialog#uriChoiceTreeView in
1542 let choices = ref None in
1546 dialog#copyButton#misc#show ();
1547 connect_button dialog#copyButton
1549 match model#easy_selection () with
1552 dialog#uriChoiceDialog#set_title title;
1553 dialog#uriChoiceLabel#set_text msg;
1554 List.iter model#easy_append (List.map UriManager.string_of_uri uris);
1555 dialog#uriChoiceConstantsButton#misc#set_sensitive nonvars_button;
1558 dialog#uriChoiceDialog#destroy ();
1561 ignore (dialog#uriChoiceDialog#event#connect#delete (fun _ -> true));
1562 connect_button dialog#uriChoiceConstantsButton (fun _ ->
1563 return (Some (Lazy.force nonvars_uris)));
1564 if ok_action = `AUTO then
1565 connect_button dialog#uriChoiceAutoButton (fun _ ->
1566 Helm_registry.set_bool "matita.auto_disambiguation" true;
1567 return (Some (Lazy.force nonvars_uris)))
1569 connect_button dialog#uriChoiceAutoButton (fun _ ->
1570 match model#easy_selection () with
1572 | uris -> return (Some (List.map UriManager.uri_of_string uris)));
1573 connect_button dialog#uriChoiceSelectedButton (fun _ ->
1574 match model#easy_selection () with
1576 | uris -> return (Some (List.map UriManager.uri_of_string uris)));
1577 connect_button dialog#uriChoiceAbortButton (fun _ -> return None);
1578 dialog#uriChoiceDialog#show ();
1580 (match !choices with
1581 | None -> raise MatitaTypes.Cancel
1582 | Some uris -> uris)
1586 let cols = new GTree.column_list in
1587 let id_col = cols#add Gobject.Data.string in
1588 let dsc_col = cols#add Gobject.Data.string in
1589 let interp_no_col = cols#add Gobject.Data.int in
1590 let tree_store = GTree.tree_store cols in
1591 let id_renderer = GTree.cell_renderer_text [], ["text", id_col] in
1592 let dsc_renderer = GTree.cell_renderer_text [], ["text", dsc_col] in
1593 let id_view_col = GTree.view_column ~renderer:id_renderer () in
1594 let dsc_view_col = GTree.view_column ~renderer:dsc_renderer () in
1595 fun tree_view choices ->
1598 tree_view#set_model (Some (tree_store :> GTree.model));
1599 ignore (tree_view#append_column id_view_col);
1600 ignore (tree_view#append_column dsc_view_col);
1601 let name_of_interp =
1602 (* try to find a reasonable name for an interpretation *)
1606 List.assoc "0" interp
1608 incr idx; string_of_int !idx
1610 tree_store#clear ();
1611 let idx = ref ~-1 in
1615 let interp_row = tree_store#append () in
1616 tree_store#set ~row:interp_row ~column:id_col
1617 (name_of_interp interp);
1618 tree_store#set ~row:interp_row ~column:interp_no_col !idx;
1621 let row = tree_store#append ~parent:interp_row () in
1622 tree_store#set ~row ~column:id_col id;
1623 tree_store#set ~row ~column:dsc_col dsc;
1624 tree_store#set ~row ~column:interp_no_col !idx)
1628 method get_interp_no tree_path =
1629 let iter = tree_store#get_iter tree_path in
1630 tree_store#get ~row:iter ~column:interp_no_col
1633 let interactive_string_choice
1634 text prefix_len ?(title = "") ?(msg = "") () ~id locs uris
1636 let gui = instance () in
1637 let dialog = gui#newUriDialog () in
1638 dialog#uriEntryHBox#misc#hide ();
1639 dialog#uriChoiceSelectedButton#misc#hide ();
1640 dialog#uriChoiceAutoButton#misc#hide ();
1641 dialog#uriChoiceConstantsButton#misc#hide ();
1642 dialog#uriChoiceTreeView#selection#set_mode
1643 (`SINGLE :> Gtk.Tags.selection_mode);
1644 let model = new stringListModel dialog#uriChoiceTreeView in
1645 let choices = ref None in
1646 dialog#uriChoiceDialog#set_title title;
1647 let hack_len = MatitaGtkMisc.utf8_string_length text in
1648 let rec colorize acc_len = function
1650 let floc = HExtlib.floc_of_loc (acc_len,hack_len) in
1651 fst(MatitaGtkMisc.utf8_parsed_text text floc)
1653 let start, stop = HExtlib.loc_of_floc he in
1654 let floc1 = HExtlib.floc_of_loc (acc_len,start) in
1655 let str1,_=MatitaGtkMisc.utf8_parsed_text text floc1 in
1656 let str2,_ = MatitaGtkMisc.utf8_parsed_text text he in
1657 str1 ^ "<b>" ^ str2 ^ "</b>" ^ colorize stop tl
1659 (* List.iter (fun l -> let start, stop = HExtlib.loc_of_floc l in
1660 Printf.eprintf "(%d,%d)" start stop) locs; *)
1664 fst (HExtlib.loc_of_floc loc1) - fst (HExtlib.loc_of_floc loc2))
1667 (* prerr_endline "XXXXXXXXXXXXXXXXXXXX";
1668 List.iter (fun l -> let start, stop = HExtlib.loc_of_floc l in
1669 Printf.eprintf "(%d,%d)" start stop) locs;
1670 prerr_endline "XXXXXXXXXXXXXXXXXXXX2"; *)
1671 dialog#uriChoiceLabel#set_use_markup true;
1672 let txt = colorize 0 locs in
1673 let txt,_ = MatitaGtkMisc.utf8_parsed_text txt
1674 (HExtlib.floc_of_loc (prefix_len,MatitaGtkMisc.utf8_string_length txt))
1676 dialog#uriChoiceLabel#set_label txt;
1677 List.iter model#easy_append uris;
1680 dialog#uriChoiceDialog#destroy ();
1683 ignore (dialog#uriChoiceDialog#event#connect#delete (fun _ -> true));
1684 connect_button dialog#uriChoiceForwardButton (fun _ ->
1685 match model#easy_selection () with
1687 | uris -> return (Some uris));
1688 connect_button dialog#uriChoiceAbortButton (fun _ -> return None);
1689 dialog#uriChoiceDialog#show ();
1691 (match !choices with
1692 | None -> raise MatitaTypes.Cancel
1693 | Some uris -> uris)
1695 let interactive_interp_choice () text prefix_len choices =
1696 (*List.iter (fun l -> prerr_endline "==="; List.iter (fun (_,id,dsc) -> prerr_endline (id ^ " = " ^ dsc)) l) choices;*)
1697 let filter_choices filter =
1698 let rec is_compatible filter =
1701 | ([],_,_)::tl -> is_compatible filter tl
1702 | (loc::tlloc,id,dsc)::tl ->
1704 if List.assoc (loc,id) filter = dsc then
1705 is_compatible filter ((tlloc,id,dsc)::tl)
1711 List.filter (fun (_,interp) -> is_compatible filter interp)
1713 let rec get_choices loc id =
1718 List.find (fun (locs,id',_) -> id = id' && List.mem loc locs) he
1720 dsc :: (List.filter (fun dsc' -> dsc <> dsc') (get_choices loc id tl))
1722 let example_interp =
1726 let ask_user id locs choices =
1727 interactive_string_choice
1729 ~title:"Ambiguous input"
1730 ~msg:("Choose an interpretation for " ^ id) () ~id locs choices
1732 let rec classify ids filter partial_interpretations =
1734 [] -> List.map fst partial_interpretations
1735 | ([],_,_)::tl -> classify tl filter partial_interpretations
1736 | (loc::tlloc,id,dsc)::tl ->
1737 let choices = get_choices loc id partial_interpretations in
1740 [] -> prerr_endline ("NO CHOICES FOR " ^ id); assert false
1743 match ask_user id [loc] choices with
1747 let filter = ((loc,id),chosen_dsc)::filter in
1748 let compatible_interps = filter_choices filter partial_interpretations in
1749 classify ((tlloc,id,dsc)::tl) filter compatible_interps
1751 let enumerated_choices =
1752 let idx = ref ~-1 in
1753 List.map (fun interp -> incr idx; !idx,interp) choices
1755 classify example_interp [] enumerated_choices
1758 (* disambiguator callbacks *)
1759 GrafiteDisambiguator.set_choose_uris_callback (interactive_uri_choice ());
1760 GrafiteDisambiguator.set_choose_interp_callback (interactive_interp_choice ());
1761 (* gtk initialization *)
1762 GtkMain.Rc.add_default_file BuildTimeConf.gtkrc_file; (* loads gtk rc *)
1763 GMathView.add_configuration_path BuildTimeConf.gtkmathview_conf;
1764 ignore (GMain.Main.init ())