]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/matita/matitaConsole.ml
snapshot, notably:
[helm.git] / helm / matita / matitaConsole.ml
index eb77ffa5d62ac8f95bac2b34ff198cd438f8ceaf..2f36fc647a065479bf6c553f6a4cfbdf1dcd557c 100644 (file)
  * http://helm.cs.unibo.it/
  *)
 
-let default_prompt = "# "
+open Printf
+
+open MatitaTypes
+
+type callback = string -> command_outcome
+
+(* let default_prompt = "# " *)
+let default_prompt = ""
 let default_phrase_sep = "."
-let default_callback = fun (phrase: string) -> ()
+let default_callback = fun (phrase: string) -> (true, true)
+let bullet = "∙"
 
 let message_props = [ `STYLE `ITALIC ]
 let error_props = [ `WEIGHT `BOLD ]
@@ -63,8 +71,9 @@ 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
 =
+  let console_height = 100 in (* pixels *)
   object (self)
     inherit GText.view obj
 
@@ -85,9 +94,13 @@ class console
 
     val history = new history BuildTimeConf.console_history_size
 
+    val mutable handle_position = 450
+    val mutable last_phrase = ""
+
     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"
@@ -98,13 +111,15 @@ class console
           (Pcre.pmatch ~rex:trailing_NL_RE text)
         then
           let inserted_text =
-            buf#get_text
-              ~start:(buf#get_iter_at_mark (`NAME "USER_INPUT_START"))
-              ~stop:buf#end_iter ()
+            MatitaMisc.strip_trailing_blanks
+              (buf#get_text
+                ~start:(buf#get_iter_at_mark (`NAME "USER_INPUT_START"))
+                ~stop:buf#end_iter ())
           in
           let pat = (Pcre.quote _phrase_sep) ^ "\\s*$" in
           if Pcre.pmatch ~pat inserted_text then begin (* complete phrase *)
             self#lock;
+            last_phrase <- inserted_text;
             self#invoke_callback inserted_text;
             self#echo_prompt ()
           end));
@@ -132,10 +147,13 @@ class console
       buf#insert ~iter:buf#end_iter phrase
 
     method private invoke_callback phrase =
-      history#add (* do not push trailing phrase separator *)
-        (String.sub phrase 0
-          (String.length phrase - String.length _phrase_sep));
-      _callback phrase
+      history#add phrase;
+      let (success, hide) = _callback phrase in
+      if hide then self#hide ()
+
+    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 =
@@ -152,6 +170,7 @@ class console
       self#lock
 
     method echo_message msg =
+      self#show ();
       let buf = self#buffer in
       self#ignore_insert_text_signal true;
       buf#insert ~iter:buf#end_iter ~tags:[buf#create_tag message_props]
@@ -160,13 +179,33 @@ 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 private get_paned_prop s =
+      Gobject.get { Gobject.name = s; Gobject.conv = Gobject.Data.int }
+        paned#as_widget
+    method private get_position = self#get_paned_prop "position"
+    method private get_min_position = self#get_paned_prop "min-position"
+    method private get_max_position = self#get_paned_prop "max-position"
+
+    method show ?(msg = "") () =
+      self#buffer#insert msg;
+      paned#set_position (self#get_max_position - console_height);
+      self#misc#grab_focus ()
+    method hide () =
+      paned#set_position self#get_max_position
+    method toggle () =
+      let pos = self#get_position in
+      if pos > self#get_max_position - console_height then
+        self#show ()
+      else
+        self#hide ()
 
       (** navigation methods: history, cursor motion, ... *)
 
@@ -175,11 +214,34 @@ class console
     method private next_phrase =
       try self#set_phrase history#next with History_failure -> ()
 
+    method wrap_exn: 'a. (unit -> 'a) -> 'a option =
+      fun f ->
+        try
+          Some (f ())
+        with exn ->
+          (match exn with (* highlight parse errors in user input *)
+          | CicTextualParser2.Parse_error (floc, msg) ->
+              let buf = self#buffer in
+              let (x, y) = CicAst.loc_of_floc floc in
+              let red = buf#create_tag [`FOREGROUND "red"] in
+              let (start_error_pos, end_error_pos) =
+                buf#end_iter#backward_chars (String.length last_phrase - x),
+                buf#end_iter#backward_chars (String.length last_phrase - y)
+              in
+              if x - y = 0 then (* no region to highlight, let's add an hint
+                                about where the error occured *)
+                buf#insert ~iter:end_error_pos ~tags:[red] bullet
+              else  (* highlight the region where the error occured *)
+                buf#apply_tag red ~start:start_error_pos ~stop:end_error_pos;
+          | _ -> ());
+          self#echo_error (explain exn);
+          None
+
   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 +250,6 @@ 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
 
+(* vim: set encoding=utf8: *)