- 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