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
=
object (self)
inherit GText.view obj
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
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 BuildTimeConf.console_history_size
initializer
let buf = self#buffer in
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 (* 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 =
(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 ()
=
?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