]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/matita/matitaConsole.ml
snapshot
[helm.git] / helm / matita / matitaConsole.ml
index 9c3c5a9b5aa7fb702ffa64f0d1e2e9695a91f7cf..1f86e51a0c522aefdc68bc393ff7c7a6aec7fba4 100644 (file)
@@ -35,10 +35,37 @@ let prompt_props = [ ]
 
 let trailing_NL_RE = Pcre.regexp "\n\\s*$"
 
+exception History_failure
+
+  (** shell like phrase history *)
+class history size =
+  let size = size + 1 in
+  let decr x = let x' = x - 1 in if x' < 0 then size + x' else x' in
+  let incr x = (x + 1) mod size in
+  object
+    val data = Array.create size ""
+    val mutable hd = 0  (* insertion point *)
+    val mutable tl = -1 (* oldest inserted item *)
+    val mutable cur = -1  (* current item for the history *)
+    method add s =
+      data.(hd) <- s;
+      if tl = -1 then tl <- hd;
+      hd <- incr hd;
+      if hd = tl then tl <- incr tl;
+      cur <- hd
+    method previous =
+      if cur = tl then raise History_failure;
+      cur <- decr cur;
+      data.(cur)
+    method next =
+      if cur = hd then raise History_failure;
+      cur <- incr cur;
+      if cur = hd then "" else data.(cur)
+  end
+
 class console
   ?(prompt = default_prompt) ?(phrase_sep = default_phrase_sep)
-  ?(callback = default_callback)
-  obj
+  ?(callback = default_callback) ?evbox obj
 =
   object (self)
     inherit GText.view obj
@@ -47,6 +74,10 @@ class console
     method phrase_sep = _phrase_sep
     method set_phrase_sep sep = _phrase_sep <- sep
 
+    val mutable _prompt = prompt
+    method prompt = _prompt
+    method set_prompt prompt = _prompt <- prompt
+
     val mutable _callback = callback
     method set_callback f = _callback <- f
 
@@ -54,14 +85,7 @@ class console
     method ignore_insert_text_signal ignore =
       _ignore_insert_text_signal <- ignore
 
-(*
-    (* TODO Zack: implement history.
-       IDEA: use CTRL-P/N a la emacs.
-       ISSUE: per-phrase or per-line history? *)
-    val phrases_history = Array.create history_size None
-    val mutable history_last_index = -1
-    val mutable history_cur_index = -1
-*)
+    val history = new history history_size
 
     initializer
       let buf = self#buffer in
@@ -83,9 +107,33 @@ class console
           let pat = (Pcre.quote _phrase_sep) ^ "\\s*$" in
           if Pcre.pmatch ~pat inserted_text then begin (* complete phrase *)
             self#lock;
-            _callback inserted_text;
+            self#invoke_callback inserted_text;
             self#echo_prompt ()
-          end))
+          end));
+      (match evbox with
+      | None -> ()
+      | Some evbox ->
+          List.iter (fun (key, f) -> MatitaGtkMisc.add_key_binding key f evbox)
+            [ GdkKeysyms._p, (fun () -> self#previous_phrase);
+              GdkKeysyms._n, (fun () -> self#next_phrase);
+              GdkKeysyms._a, (fun () -> self#beginning_of_phrase);
+              GdkKeysyms._e, (fun () -> self#end_of_phrase);
+              GdkKeysyms._f, (fun () -> self#forward_char);
+              GdkKeysyms._b, (fun () -> self#backward_char);
+            ])
+
+    method private set_phrase phrase =
+      let buf = self#buffer in
+      buf#delete
+        ~start:(buf#get_iter_at_mark (`NAME "USER_INPUT_START"))
+        ~stop:buf#end_iter;
+      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
 
       (* lock old text and bump USER_INPUT_START mark *)
     method private lock =
@@ -116,11 +164,31 @@ class console
         (msg ^ "\n");
       self#ignore_insert_text_signal false;
       self#lock
+
+      (** navigation methods: history, cursor motion, ... *)
+
+    method private previous_phrase =
+      try self#set_phrase history#previous with History_failure -> ()
+    method private next_phrase =
+      try self#set_phrase history#next with History_failure -> ()
+    method private beginning_of_phrase =
+      let buf = self#buffer in
+      buf#place_cursor ~where:(buf#get_iter_at_mark (`NAME "USER_INPUT_START"))
+    method private end_of_phrase =
+      let buf = self#buffer in
+      buf#place_cursor ~where:buf#end_iter
+    method private forward_char =
+      let buf = self#buffer in
+      buf#place_cursor ~where:(buf#get_iter_at_mark `INSERT)#forward_char
+    method private backward_char =
+      let buf = self#buffer in
+      buf#place_cursor ~where:(buf#get_iter_at_mark `INSERT)#backward_char
+
   end
 
 let console
   ?(prompt = default_prompt) ?(phrase_sep = default_phrase_sep)
-  ?(callback = default_callback)
+  ?(callback = default_callback) ?evbox
   ?buffer ?editable ?cursor_visible ?justification ?wrap_mode ?border_width
   ?width ?height ?packing ?show ()
 =
@@ -129,5 +197,5 @@ let console
       ?buffer ?editable ?cursor_visible ?justification ?wrap_mode ?border_width
       ?width ?height ?packing ?show ()
   in
-  new console ~prompt ~phrase_sep ~callback view#as_view
+  new console ~prompt ~phrase_sep ~callback ?evbox view#as_view