]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/matita/matitaConsole.ml
snapshot:
[helm.git] / helm / matita / matitaConsole.ml
index eb77ffa5d62ac8f95bac2b34ff198cd438f8ceaf..810914393549a720125bcfab14c8d51ab0e024d0 100644 (file)
  * http://helm.cs.unibo.it/
  *)
 
-let default_prompt = "# "
+open Printf
+
+open MatitaTypes
+
+(* let default_prompt = "# " *)
+let default_prompt = ""
 let default_phrase_sep = "."
-let default_callback = fun (phrase: string) -> ()
+let default_callback = fun (phrase: string) -> true
 
 let message_props = [ `STYLE `ITALIC ]
 let error_props = [ `WEIGHT `BOLD ]
@@ -63,7 +68,7 @@ class history size =
 
 class console
   ?(prompt = default_prompt) ?(phrase_sep = default_phrase_sep)
-  ?(callback = default_callback) ?evbox obj
+  ?(callback = default_callback) ?evbox ~(paned:GPack.paned) obj
 =
   object (self)
     inherit GText.view obj
@@ -85,9 +90,12 @@ class console
 
     val history = new history BuildTimeConf.console_history_size
 
+    val mutable handle_position = 450
+
     initializer
       let buf = self#buffer in
       self#set_wrap_mode `CHAR;
+      self#hide ();
         (* create "USER_INPUT_START" mark. This mark will always point to the
         * beginning of user input not yet processed *)
       ignore (buf#create_mark ~name:"USER_INPUT_START"
@@ -135,7 +143,14 @@ class console
       history#add (* do not push trailing phrase separator *)
         (String.sub phrase 0
           (String.length phrase - String.length _phrase_sep));
-      _callback phrase
+      if _callback phrase then
+        self#hide ()
+        (* else callback encountered troubles, don't hide console which
+        probably contains error message *)
+
+    method clear () =
+      let buf = self#buffer in
+      buf#delete ~start:buf#start_iter ~stop:buf#end_iter
 
       (* lock old text and bump USER_INPUT_START mark *)
     method private lock =
@@ -160,13 +175,21 @@ class console
       self#lock
 
     method echo_error msg =
+      self#show ();
       let buf = self#buffer in
       self#ignore_insert_text_signal true;
       buf#insert ~iter:buf#end_iter ~tags:[buf#create_tag error_props]
         (msg ^ "\n");
       self#ignore_insert_text_signal false;
       self#lock
-(*       self#echo_prompt () *)
+
+    method show ?(msg = "") () =
+      self#buffer#insert msg;
+      paned#set_position handle_position;
+      self#misc#grab_focus ()
+    method hide () =
+      self#clear ();
+      paned#set_position max_int
 
       (** navigation methods: history, cursor motion, ... *)
 
@@ -175,11 +198,18 @@ class console
     method private next_phrase =
       try self#set_phrase history#next with History_failure -> ()
 
+    method wrap_exn f =
+      try
+        f ()
+      with exn ->
+        self#echo_error (sprintf "Uncaught exception: %s"
+          (Printexc.to_string exn))
+
   end
 
 let console
   ?(prompt = default_prompt) ?(phrase_sep = default_phrase_sep)
-  ?(callback = default_callback) ?evbox
+  ?(callback = default_callback) ?evbox ~paned
   ?buffer ?editable ?cursor_visible ?justification ?wrap_mode ?border_width
   ?width ?height ?packing ?show ()
 =
@@ -188,5 +218,5 @@ let console
       ?buffer ?editable ?cursor_visible ?justification ?wrap_mode ?border_width
       ?width ?height ?packing ?show ()
   in
-  new console ~prompt ~phrase_sep ~callback ?evbox view#as_view
+  new console ~prompt ~phrase_sep ~callback ?evbox ~paned view#as_view