]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/gTopLevel/gTopLevel.ml
Interface change: the get_as_string and set_term methods of the term-editors
[helm.git] / helm / gTopLevel / gTopLevel.ml
index c1dc822b68761242022f9df50932748ebe0952ca..3dd4573844b426b79ab0467c1c68d00eaa6d75cb 100644 (file)
 (*                                                                            *)
 (******************************************************************************)
 
+open Printf;;
+
+(* DEBUGGING *)
+
+module MQI = MQueryInterpreter
+module MQIC = MQIConn
 
 (* GLOBAL CONSTANTS *)
 
+let mqi_flags = [] (* default MathQL interpreter options *)
+let mqi_handle = MQIC.init mqi_flags prerr_string
+
 let xlinkns = Gdome.domString "http://www.w3.org/1999/xlink";;
 
 let htmlheader =
@@ -62,13 +71,6 @@ let prooffiletype =
   Not_found -> "/public/currentprooftype"
 ;;
 
-let postgresqlconnectionstring =
- try
-  Sys.getenv "POSTGRESQL_CONNECTION_STRING"
- with
-  Not_found -> "host=mowgli.cs.unibo.it dbname=helm_mowgli_new_schema user=helm"
-;;
-
 (* GLOBAL REFERENCES (USED BY CALLBACKS) *)
 
 let htmlheader_and_content = ref htmlheader;;
@@ -194,7 +196,7 @@ let check_window outputhtml uris =
         let expr =
          let term =
           term_of_cic_textual_parser_uri
-           (Misc.cic_textual_parser_uri_of_string uri)
+           (MQueryMisc.cic_textual_parser_uri_of_string uri)
          in
           (Cic.Cast (term, CicTypeChecker.type_of_aux' [] [] term))
         in
@@ -312,7 +314,7 @@ let
         end));
   window#set_position `CENTER ;
   window#show () ;
-  GMain.Main.main () ;
+  GtkThread.main ();
   if !chosen then
    if !use_only_constants then
     List.filter
@@ -373,7 +375,7 @@ let interactive_interpretation_choice interpretations =
     (function () -> chosen := Some notebook#current_page ; window#destroy ())) ;
  window#set_position `CENTER ;
  window#show () ;
- GMain.Main.main () ;
+ GtkThread.main ();
  match !chosen with
     None -> raise NoChoice
   | Some n -> n
@@ -447,11 +449,6 @@ let
 
 (* CALLBACKS *)
 
-(*
-ignore(domImpl#saveDocumentToFile ~doc:sequent_doc
- ~name:"/home/galata/miohelm/guruguru1" ~indent:true ()) ;
-*)
-
 exception OpenConjecturesStillThere;;
 exception WrongProof;;
 
@@ -504,34 +501,18 @@ let qed () =
   | _ -> raise OpenConjecturesStillThere
 ;;
 
-let save () =
+  (** save an unfinished proof on the filesystem *)
+let save_unfinished_proof () =
  let outputhtml = ((rendering_window ())#outputhtml : GHtml.xmhtml) in
-  match !ProofEngine.proof with
-     None -> assert false
-   | Some (uri, metasenv, bo, ty) ->
-      let currentproof =
-       (*CSC: Wrong: [] is just plainly wrong *)
-       Cic.CurrentProof (UriManager.name_of_uri uri,metasenv,bo,ty,[])
-      in
-       let (acurrentproof,_,_,ids_to_inner_sorts,_,_,_) =
-        Cic2acic.acic_object_of_cic_object currentproof
-       in
-        let xml, bodyxml =
-         match
-          Cic2Xml.print_object uri ~ids_to_inner_sorts
-           ~ask_dtd_to_the_getter:true acurrentproof
-         with
-            xml,Some bodyxml -> xml,bodyxml
-          | _,None -> assert false
-        in
-         Xml.pp ~quiet:true xml (Some prooffiletype) ;
-         output_html outputhtml
-          ("<h1 color=\"Green\">Current proof type saved to " ^
-           prooffiletype ^ "</h1>") ;
-         Xml.pp ~quiet:true bodyxml (Some prooffile) ;
-         output_html outputhtml
-          ("<h1 color=\"Green\">Current proof body saved to " ^
-           prooffile ^ "</h1>")
+ let (xml, bodyxml) = ProofEngine.get_current_status_as_xml () in
+ Xml.pp ~quiet:true xml (Some prooffiletype) ;
+ output_html outputhtml
+  ("<h1 color=\"Green\">Current proof type saved to " ^
+   prooffiletype ^ "</h1>") ;
+ Xml.pp ~quiet:true bodyxml (Some prooffile) ;
+ output_html outputhtml
+  ("<h1 color=\"Green\">Current proof body saved to " ^
+   prooffile ^ "</h1>")
 ;;
 
 (* Used to typecheck the loaded proofs *)
@@ -552,7 +533,7 @@ let decompose_uris_choice_callback uris =
   let module U = UriManager in 
    List.map 
     (function uri ->
-      match Misc.cic_textual_parser_uri_of_string uri with
+      match MQueryMisc.cic_textual_parser_uri_of_string uri with
          CicTextualParser0.IndTyUri (uri,typeno) -> (uri,typeno,[])
        | _ -> assert false)
     (interactive_user_uri_choice 
@@ -587,7 +568,17 @@ let refresh_proof (output : TermViewer.proof_viewer) =
    match !ProofEngine.proof with
       None -> assert false
     | Some (uri,metasenv,bo,ty) ->
-       !qed_set_sensitive (List.length metasenv = 0) ;
+       if List.length metasenv = 0 then
+        begin
+         !qed_set_sensitive true ;
+prerr_endline "CSC: ###### REFRESH_PROOF, Hbugs.clear ()" ;
+         Hbugs.clear ()
+        end
+       else
+begin
+prerr_endline "CSC: ###### REFRESH_PROOF, Hbugs.notify ()" ;
+        Hbugs.notify () ;
+end ;
        (*CSC: Wrong: [] is just plainly wrong *)
        uri,
         (Cic.CurrentProof (UriManager.name_of_uri uri, metasenv, bo, ty, []))
@@ -660,7 +651,6 @@ let currentsequent = List.find (function (m,_,_) -> m=metano) metasenv in
       raise (InvokeTactics.RefreshSequentException e)
 with Not_found -> prerr_endline ("Offending sequent " ^ string_of_int metano ^ " unknown."); raise (InvokeTactics.RefreshSequentException e)
 
-
 module InvokeTacticsCallbacks =
  struct
   let sequent_viewer () = (rendering_window ())#notebook#proofw
@@ -680,11 +670,12 @@ module InvokeTacticsCallbacks =
   let output_html msg = output_html (outputhtml ()) msg
  end
 ;;
+module InvokeTactics' = InvokeTactics.Make (InvokeTacticsCallbacks);;
+(* Just to initialize the Hbugs module *)
+module Ignore = Hbugs.Initialize (InvokeTactics');;
 
-module InvokeTactics' = InvokeTactics.Make(InvokeTacticsCallbacks);;
-
-
-let load () =
+  (** load an unfinished proof from filesystem *)
+let load_unfinished_proof () =
  let outputhtml = ((rendering_window ())#outputhtml : GHtml.xmhtml) in
  let output = ((rendering_window ())#output : TermViewer.proof_viewer) in
  let notebook = (rendering_window ())#notebook in
@@ -714,7 +705,7 @@ let load () =
              output_html outputhtml
               ("<h1 color=\"Green\">Current proof body loaded from " ^
                 prooffile ^ "</h1>") ;
-            !save_set_sensitive true
+            !save_set_sensitive true;
          | _ -> assert false
   with
      InvokeTactics.RefreshSequentException e ->
@@ -766,13 +757,20 @@ let edit_aliases () =
           let uri =
            match resolve_id v with
               None -> assert false
-            | Some uri -> uri
+            | Some (CicTextualParser0.Uri uri) -> uri
+            | Some (CicTextualParser0.Term _)
+            | Some CicTextualParser0.Implicit -> assert false
           in
-           "alias " ^ v ^ " " ^
-             (string_of_cic_textual_parser_uri uri)
+           "alias " ^
+            (match v with
+                CicTextualParser0.Id id -> id
+              | CicTextualParser0.Symbol (descr,_) ->
+                 (* CSC: To be implemented *)
+                 assert false
+            )^ " " ^ (string_of_cic_textual_parser_uri uri)
         ) dom))) ;
   window#show () ;
-  GMain.Main.main () ;
+  GtkThread.main ();
   if !chosen then
    let dom,resolve_id =
     let inputtext = input#get_chars 0 input#length in
@@ -789,9 +787,9 @@ let edit_aliases () =
      let rec aux n =
       try
        let n' = Str.search_forward regexpr inputtext n in
-        let id = Str.matched_group 2 inputtext in
+        let id = CicTextualParser0.Id (Str.matched_group 2 inputtext) in
         let uri =
-         Misc.cic_textual_parser_uri_of_string
+         MQueryMisc.cic_textual_parser_uri_of_string
           ("cic:" ^ (Str.matched_group 5 inputtext))
         in
          let dom,resolve_id = aux (n' + 1) in
@@ -799,7 +797,10 @@ let edit_aliases () =
            dom,resolve_id
           else
            id::dom,
-            (function id' -> if id = id' then Some uri else resolve_id id')
+            (function id' ->
+              if id = id' then
+               Some (CicTextualParser0.Uri uri)
+              else resolve_id id')
       with
        Not_found -> TermEditor.empty_id_to_uris
      in
@@ -888,7 +889,9 @@ let
  let scrolled_window =
   GBin.scrolled_window ~border_width:10 ~packing:window#add () in
  let mmlwidget =
-  GMathViewAux.single_selection_math_view ~packing:scrolled_window#add ~width:600 ~height:400 () in
+  GMathViewAux.single_selection_math_view
+    ~packing:scrolled_window#add ~width:600 ~height:400 ()
+ in
  let _ = window#event#connect#delete (fun _ -> window#misc#hide () ; true ) in
  let href = Gdome.domString "href" in
   let show_in_show_window_obj uri obj =
@@ -916,14 +919,17 @@ let
    let obj = CicEnvironment.get_obj uri in
     show_in_show_window_obj uri obj
   in
-   let show_in_show_window_callback mmlwidget (n : Gdome.element) _ =
-    if n#hasAttributeNS ~namespaceURI:xlinkns ~localName:href then
-     let uri =
-      (n#getAttributeNS ~namespaceURI:xlinkns ~localName:href)#to_string
-     in 
-      show_in_show_window_uri (UriManager.uri_of_string uri)
-    else
-     ignore (mmlwidget#action_toggle n)
+   let show_in_show_window_callback mmlwidget (n : Gdome.element option) _ =
+    match n with
+       None -> ()
+     | Some n' ->
+        if n'#hasAttributeNS ~namespaceURI:xlinkns ~localName:href then
+         let uri =
+          (n'#getAttributeNS ~namespaceURI:xlinkns ~localName:href)#to_string
+         in 
+          show_in_show_window_uri (UriManager.uri_of_string uri)
+        else
+         ignore (mmlwidget#action_toggle n')
    in
     let _ =
      mmlwidget#connect#click (show_in_show_window_callback mmlwidget)
@@ -951,11 +957,11 @@ let user_uri_choice ~title ~msg uris =
 
 let locate_callback id =
  let outputhtml = ((rendering_window ())#outputhtml : GHtml.xmhtml) in
- let result = MQueryGenerator.locate id in
+ let result = MQueryGenerator.locate mqi_handle id in
  let uris =
   List.map
    (function uri,_ ->
-     Misc.wrong_xpointer_format_from_wrong_xpointer_format' uri)
+     MQueryMisc.wrong_xpointer_format_from_wrong_xpointer_format' uri)
    result in
  let html =
   (" <h1>Locate Query: </h1><pre>" ^ get_last_query result ^ "</pre>")
@@ -1067,7 +1073,7 @@ let input_or_locate_uri ~title =
         locate_input#delete_text 0 (String.length id)
    )) ;
   window#show () ;
-  GMain.Main.main () ;
+  GtkThread.main ();
   match !uri with
      None -> raise NoChoice
    | Some uri -> UriManager.uri_of_string ("cic:" ^ uri)
@@ -1077,8 +1083,18 @@ exception AmbiguousInput;;
 
 (* A WIDGET TO ENTER CIC TERMS *)
 
+module ChosenTermEditor  = TexTermEditor;;
+module ChosenTextualParser0 = TexCicTextualParser0;;
+(*
+module ChosenTermEditor = TermEditor;;
+module ChosenTextualParser0 = CicTextualParser0;;
+*)
+
 module Callbacks =
  struct
+  let get_metasenv () = !ChosenTextualParser0.metasenv
+  let set_metasenv metasenv = ChosenTextualParser0.metasenv := metasenv
+
   let output_html msg = output_html (outputhtml ()) msg;;
   let interactive_user_uri_choice =
    fun ~selection_mode ?ok ?enable_button_for_non_vars ~title ~msg ~id ->
@@ -1089,7 +1105,7 @@ module Callbacks =
  end
 ;;
 
-module TermEditor' = TermEditor.Make(Callbacks);;
+module TexTermEditor' = ChosenTermEditor.Make(Callbacks);;
 
 (* OTHER FUNCTIONS *)
 
@@ -1235,7 +1251,8 @@ let new_inductive () =
        GBin.scrolled_window ~border_width:5
         ~packing:(vbox#pack ~expand:true ~padding:0) () in
       let newinputt =
-       TermEditor'.term_editor
+       TexTermEditor'.term_editor
+        mqi_handle
         ~width:400 ~height:20 ~packing:scrolled_window#add 
         ~share_id_to_uris_with:inputt ()
         ~isnotempty_callback:
@@ -1346,7 +1363,8 @@ let new_inductive () =
        GBin.scrolled_window ~border_width:5
         ~packing:(vbox#pack ~expand:true ~padding:0) () in
       let newinputt =
-       TermEditor'.term_editor
+       TexTermEditor'.term_editor
+        mqi_handle
         ~width:400 ~height:20 ~packing:scrolled_window#add
         ~share_id_to_uris_with:inputt ()
         ~isnotempty_callback:
@@ -1398,7 +1416,7 @@ let new_inductive () =
           ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>") ;
      )) ;
   window2#show () ;
-  GMain.Main.main () ;
+  GtkThread.main ();
   let okb_pressed = !chosen in
    chosen := false ;
    if (not okb_pressed) then
@@ -1412,7 +1430,7 @@ let new_inductive () =
   phase1 () ;
   (* No more phases left or Abort pressed *) 
   window#show () ;
-  GMain.Main.main () ;
+  GtkThread.main ();
   window#destroy () ;
   if !chosen then
    try
@@ -1490,7 +1508,9 @@ let new_proof () =
    ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
  (* moved here to have visibility of the ok button *)
  let newinputt =
-  TermEditor'.term_editor ~width:400 ~height:100 ~packing:scrolled_window#add
+  TexTermEditor'.term_editor
+   mqi_handle
+   ~width:400 ~height:100 ~packing:scrolled_window#add
    ~share_id_to_uris_with:inputt ()
    ~isnotempty_callback:
     (function b ->
@@ -1498,7 +1518,12 @@ let new_proof () =
       okb#misc#set_sensitive (b && uri_entry#text <> ""))
  in
  let _ =
+let xxx = inputt#get_as_string in
+prerr_endline ("######################## " ^ xxx) ;
+  newinputt#set_term xxx ;
+(*
   newinputt#set_term inputt#get_as_string ;
+*)
   inputt#reset in
  let _ =
   uri_entry#connect#changed
@@ -1534,7 +1559,7 @@ let new_proof () =
          ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>") ;
   )) ;
  window#show () ;
- GMain.Main.main () ;
+ GtkThread.main ();
  if !chosen then
   try
    let metasenv,expr = !get_metasenv_and_term () in
@@ -1680,8 +1705,8 @@ let show_query_results results =
      (fun ~row ~column ~event ->
        let (uristr,_) = List.nth results row in
         match
-         Misc.cic_textual_parser_uri_of_string
-          (Misc.wrong_xpointer_format_from_wrong_xpointer_format'
+         MQueryMisc.cic_textual_parser_uri_of_string
+          (MQueryMisc.wrong_xpointer_format_from_wrong_xpointer_format'
             uristr)
         with
            CicTextualParser0.ConUri uri
@@ -1872,7 +1897,7 @@ let refine_constraints (must_obj,must_rel,must_sort) =
    (okb#connect#clicked (function () -> chosen := true ; window#destroy ()));
   window#set_position `CENTER ;
   window#show () ;
-  GMain.Main.main () ;
+  GtkThread.main ();
   if !chosen then
    let chosen_must_rel =
     List.map
@@ -1905,7 +1930,7 @@ let completeSearchPattern () =
    let metasenv,expr = inputt#get_metasenv_and_term ~context:[] ~metasenv:[] in
    let must = MQueryLevels2.get_constraints expr in
    let must',only = refine_constraints must in
-   let results = MQueryGenerator.searchPattern must' only in 
+   let results = MQueryGenerator.searchPattern mqi_handle must' only in 
     show_query_results results
   with
    e ->
@@ -1967,12 +1992,12 @@ let insertQuery () =
                ignore (input#insert_text text ~pos:0))) ;
    window#set_position `CENTER ;
    window#show () ;
-   GMain.Main.main () ;
+   GtkThread.main ();
    match !chosen with
       None -> ()
     | Some q ->
        let results =
-        Mqint.execute (MQueryUtil.query_of_text (Lexing.from_string q))
+        MQI.execute mqi_handle (MQueryUtil.query_of_text (Lexing.from_string q))
        in
         show_query_results results
   with
@@ -2095,7 +2120,7 @@ let choose_must list_of_must only =
     (function () -> chosen := Some notebook#current_page ; window#destroy ())) ;
  window#set_position `CENTER ;
  window#show () ;
- GMain.Main.main () ;
+ GtkThread.main ();
  match !chosen with
     None -> raise NoChoice
   | Some n ->
@@ -2110,83 +2135,27 @@ let searchPattern () =
  let inputt = ((rendering_window ())#inputt : TermEditor.term_editor) in
  let outputhtml = ((rendering_window ())#outputhtml : GHtml.xmhtml) in
   try
-    let metasenv =
+    let proof =
      match !ProofEngine.proof with
         None -> assert false
-      | Some (_,metasenv,_,_) -> metasenv
+      | Some proof -> proof
     in
      match !ProofEngine.goal with
-        None -> ()
+      | None -> ()
       | Some metano ->
-         let (_, ey ,ty) = List.find (function (m,_,_) -> m=metano) metasenv in
-          let list_of_must,only = MQueryLevels.out_restr metasenv ey ty in
-         let must = choose_must list_of_must only in
-         let torigth_restriction (u,b) =
-          let p =
-            if b then
-             "http://www.cs.unibo.it/helm/schemas/schema-helm#MainConclusion" 
-           else
-             "http://www.cs.unibo.it/helm/schemas/schema-helm#InConclusion"
-           in
-           (u,p,None)
-         in
-         let rigth_must = List.map torigth_restriction must in
-         let rigth_only = Some (List.map torigth_restriction only) in
-         let result =
-           MQueryGenerator.searchPattern
-            (rigth_must,[],[]) (rigth_only,None,None) in 
-          let uris =
-           List.map
-            (function uri,_ ->
-              Misc.wrong_xpointer_format_from_wrong_xpointer_format' uri
-            ) result in
-          let html =
-           " <h1>Backward Query: </h1>" ^
-          " <pre>" ^ get_last_query result ^ "</pre>"
-          in
-           output_html outputhtml html ;
-           let uris',exc =
-            let rec filter_out =
-             function
-                [] -> [],""
-              | uri::tl ->
-                 let tl',exc = filter_out tl in
-                  try
-                   if
-                    ProofEngine.can_apply
-                     (term_of_cic_textual_parser_uri
-                      (Misc.cic_textual_parser_uri_of_string uri))
-                   then
-                    uri::tl',exc
-                   else
-                    tl',exc
-                  with
-                   e ->
-                    let exc' =
-                     "<h1 color=\"red\"> ^ Exception raised trying to apply " ^
-                      uri ^ ": " ^ Printexc.to_string e ^ " </h1>" ^ exc
-                    in
-                     tl',exc'
-            in
-             filter_out uris
-           in
-            let html' =
-             " <h1>Objects that can actually be applied: </h1> " ^
-             String.concat "<br>" uris' ^ exc ^
-             " <h1>Number of false matches: " ^
-              string_of_int (List.length uris - List.length uris') ^ "</h1>" ^
-             " <h1>Number of good matches: " ^
-              string_of_int (List.length uris') ^ "</h1>"
-            in
-             output_html outputhtml html' ;
-             let uri' =
-              user_uri_choice ~title:"Ambiguous input."
-              ~msg:
-                "Many lemmas can be successfully applied. Please, choose one:"
-               uris'
-             in
-              inputt#set_term uri' ;
-              InvokeTactics'.apply ()
+         let uris' =
+           TacticChaser.searchPattern
+           mqi_handle
+            ~output_html:(output_html outputhtml) ~choose_must ()
+            ~status:(proof, metano)
+         in
+         let uri' =
+          user_uri_choice ~title:"Ambiguous input."
+          ~msg: "Many lemmas can be successfully applied. Please, choose one:"
+           uris'
+         in
+          inputt#set_term uri' ;
+          InvokeTactics'.apply ()
   with
    e -> 
     output_html outputhtml 
@@ -2342,7 +2311,9 @@ end;;
 
 class scratch_window =
  let window =
-  GWindow.window ~title:"MathML viewer" ~border_width:2 () in
+  GWindow.window
+    ~title:"MathML viewer"
+    ~border_width:2 () in
  let vbox =
   GPack.vbox ~packing:window#add () in
  let hbox =
@@ -2538,6 +2509,12 @@ object(self)
    let replaceb =
     GButton.button ~label:"Replace"
      ~packing:(hbox7#pack ~expand:false ~fill:false ~padding:5) () in
+   let injectionb =
+    GButton.button ~label:"Injection"
+     ~packing:(hbox7#pack ~expand:false ~fill:false ~padding:5) () in
+   let discriminateb =
+    GButton.button ~label:"Discriminate"
+     ~packing:(hbox7#pack ~expand:false ~fill:false ~padding:5) () in
 (* Zack: spostare in una toolbar
    let generalizeb =
     GButton.button ~label:"Generalize"
@@ -2587,6 +2564,8 @@ object(self)
    ignore(introsb#connect#clicked InvokeTactics'.intros) ;
    ignore(decomposeb#connect#clicked InvokeTactics'.decompose) ;
    ignore(searchpatternb#connect#clicked searchPattern) ;
+   ignore(injectionb#connect#clicked InvokeTactics'.injection) ;
+   ignore(discriminateb#connect#clicked InvokeTactics'.discriminate) ;
 (* Zack: spostare in una toolbar
    ignore(whdb#connect#clicked whd) ;
    ignore(reduceb#connect#clicked reduce) ;
@@ -2678,8 +2657,9 @@ end
 class rendering_window output (notebook : notebook) =
  let scratch_window = new scratch_window in
  let window =
-  GWindow.window ~title:"MathML viewer" ~border_width:0
-   ~allow_shrink:false () in
+  GWindow.window
+   ~title:"gTopLevel - Helm's Proof Assistant"
+   ~border_width:0 ~allow_shrink:false () in
  let vbox_for_menu = GPack.vbox ~packing:window#add () in
  (* menus *)
  let handle_box = GBin.handle_box ~border_width:2
@@ -2709,9 +2689,10 @@ class rendering_window output (notebook : notebook) =
    ignore (factory1#add_separator ()) ;
    ignore
     (factory1#add_item "Load Unfinished Proof..." ~key:GdkKeysyms._L
-      ~callback:load) ;
+      ~callback:load_unfinished_proof) ;
    let save_menu_item =
-    factory1#add_item "Save Unfinished Proof" ~key:GdkKeysyms._S ~callback:save
+    factory1#add_item "Save Unfinished Proof" ~key:GdkKeysyms._S
+      ~callback:save_unfinished_proof
    in
    ignore
     (save_set_sensitive := function b -> save_menu_item#misc#set_sensitive b);
@@ -2754,8 +2735,8 @@ class rendering_window output (notebook : notebook) =
    ~callback:(check scratch_window) in
  let _ = check_menu_item#misc#set_sensitive false in
  (* search menu *)
- let settings_menu = factory0#add_submenu "Search" in
- let factory4 = new GMenu.factory settings_menu ~accel_group in
+ let search_menu = factory0#add_submenu "Search" in
+ let factory4 = new GMenu.factory search_menu ~accel_group in
  let _ =
   factory4#add_item "Locate..." ~key:GdkKeysyms._T
    ~callback:locate in
@@ -2767,8 +2748,15 @@ class rendering_window output (notebook : notebook) =
   factory4#add_item "Show..." ~key:GdkKeysyms._H ~callback:show
  in
  let insert_query_item =
-  factory4#add_item "Insert Query (Experts Only)..." ~key:GdkKeysyms._U
+  factory4#add_item "Insert Query (Experts Only)..." ~key:GdkKeysyms._Y
    ~callback:insertQuery in
+ (* hbugs menu *)
+ let hbugs_menu = factory0#add_submenu "HBugs" in
+ let factory6 = new GMenu.factory hbugs_menu ~accel_group in
+ let toggle_hbugs_menu_item =
+  factory6#add_check_item
+    ~active:false ~key:GdkKeysyms._F5 ~callback:Hbugs.toggle "HBugs enabled"
+ in
  (* settings menu *)
  let settings_menu = factory0#add_submenu "Settings" in
  let factory3 = new GMenu.factory settings_menu ~accel_group in
@@ -2798,7 +2786,8 @@ class rendering_window output (notebook : notebook) =
   GBin.scrolled_window ~border_width:5
    ~packing:frame#add () in
  let inputt =
-  TermEditor'.term_editor
+  TexTermEditor'.term_editor
+   mqi_handle
    ~width:400 ~height:100 ~packing:scrolled_window1#add ()
    ~isnotempty_callback:
     (function b ->
@@ -2854,17 +2843,27 @@ let initialize_everything () =
   let notebook = new notebook in
    let rendering_window' = new rendering_window output notebook in
     set_rendering_window rendering_window' ;
-    rendering_window'#show () ;
-    GMain.Main.main ()
+    let print_error_as_html prefix msg =
+     output_html (outputhtml ())
+      ("<h1 color=\"red\">" ^ prefix ^ msg ^ "</h1>")
+    in
+     Gdome_xslt.setErrorCallback (Some (print_error_as_html "XSLT Error: "));
+     Gdome_xslt.setDebugCallback
+      (Some (print_error_as_html "XSLT Debug Message: "));
+     rendering_window'#show () ;
+(*      Hbugs.toggle true; *)
+     GtkThread.main ()
 ;;
 
-let _ =
- if !usedb then
-  begin
-   Mqint.set_database Mqint.postgres_db ;
-   Mqint.init postgresqlconnectionstring ;
-  end ;
+let main () =
  ignore (GtkMain.Main.init ()) ;
  initialize_everything () ;
- if !usedb then Mqint.close ();
+ MQIC.close mqi_handle;
+ Hbugs.quit ()
 ;;
+
+try
+  Sys.catch_break true;
+  main ();
+with Sys.Break -> ()  (* exit nicely, invoking at_exit functions *)
+