X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;ds=sidebyside;f=helm%2Fmatita%2FmatitaConsole.ml;h=5fb44adf486511c92067e5fd595c5c3cd0faa94e;hb=fd96ce8e13f4f9adbeef2d9feb32f94dfcfaadad;hp=a2c0515bd05cc26efeed58bffbc955423e7b3df6;hpb=56415e42c04f40e9c8f7cfc59a3a3d87c3d373f7;p=helm.git diff --git a/helm/matita/matitaConsole.ml b/helm/matita/matitaConsole.ml index a2c0515bd..5fb44adf4 100644 --- a/helm/matita/matitaConsole.ml +++ b/helm/matita/matitaConsole.ml @@ -27,18 +27,44 @@ let default_prompt = "# " let default_phrase_sep = "." let default_callback = fun (phrase: string) -> () -let history_size = 100 - let message_props = [ `STYLE `ITALIC ] let error_props = [ `WEIGHT `BOLD ] 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 = - let ui_mark = `NAME "USER_INPUT_START" in object (self) inherit GText.view obj @@ -46,69 +72,113 @@ 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 -(* - (* 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 mutable _ignore_insert_text_signal = false + method ignore_insert_text_signal ignore = + _ignore_insert_text_signal <- ignore + + val history = new history BuildTimeConf.console_history_size initializer let buf = self#buffer in + self#set_wrap_mode `CHAR; (* 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" ~left_gravity:true buf#start_iter); - ignore (self#event#connect#key_press (fun key -> (* handle return ev. *) - if GdkEvent.Key.keyval key = GdkKeysyms._Return then begin - let insert_point = buf#get_iter_at_mark `INSERT in - if insert_point#compare buf#end_iter = 0 then (* insert pt. at end *) - let inserted_text = - buf#get_text ~start:(buf#get_iter_at_mark ui_mark) - ~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; - _callback inserted_text - end - end; - false (* continue event processing *))) + ignore (buf#connect#after#insert_text (fun iter text -> + if (not _ignore_insert_text_signal) && + (iter#compare buf#end_iter = 0) && (* insertion at end *) + (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 () + in + let pat = (Pcre.quote _phrase_sep) ^ "\\s*$" in + if Pcre.pmatch ~pat inserted_text then begin (* complete phrase *) + self#lock; + self#invoke_callback inserted_text; + self#echo_prompt () + end)); + (match evbox with (* history key bindings *) + | 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); + ]); + ignore (self#connect#after#move_cursor + (* avoid cursor being placed at prompt's left *) + ~callback:(fun step count ~extend -> + let buf = self#buffer in + let cursor_iter = buf#get_iter_at_mark `INSERT in + let prompt_iter = buf#get_iter_at_mark (`NAME "USER_INPUT_START") in + if prompt_iter#compare cursor_iter = 1 then (* prompt > cursor *) + buf#place_cursor ~where:prompt_iter)) + + 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 = let buf = self#buffer in let read_only = buf#create_tag [`EDITABLE false] in - let stop = buf#end_iter in - buf#apply_tag read_only ~start:buf#start_iter ~stop; - buf#move_mark ui_mark stop + buf#apply_tag read_only ~start:buf#start_iter ~stop:buf#end_iter; + buf#move_mark (`NAME "USER_INPUT_START") buf#end_iter method echo_prompt () = let buf = self#buffer in + self#ignore_insert_text_signal true; buf#insert ~iter:buf#end_iter ~tags:[buf#create_tag prompt_props] prompt; + self#ignore_insert_text_signal false; self#lock method echo_message msg = let buf = self#buffer in + self#ignore_insert_text_signal true; buf#insert ~iter:buf#end_iter ~tags:[buf#create_tag message_props] (msg ^ "\n"); + self#ignore_insert_text_signal false; self#lock method echo_error msg = 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 + + (** 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 -> () + 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 () = @@ -117,5 +187,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