1 (* Copyright (C) 2004, HELM Team.
3 * This file is part of HELM, an Hypertextual, Electronic
4 * Library of Mathematics, developed at the Computer Science
5 * Department, University of Bologna, Italy.
7 * HELM is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * HELM is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with HELM; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * For details, see the HELM World-Wide-Web page,
23 * http://helm.cs.unibo.it/
26 let default_prompt = "# "
27 let default_phrase_sep = "."
28 let default_callback = fun (phrase: string) -> ()
30 let history_size = 100
32 let message_props = [ `STYLE `ITALIC ]
33 let error_props = [ `WEIGHT `BOLD ]
34 let prompt_props = [ ]
36 let trailing_NL_RE = Pcre.regexp "\n\\s*$"
39 ?(prompt = default_prompt) ?(phrase_sep = default_phrase_sep)
40 ?(callback = default_callback)
44 inherit GText.view obj
46 val mutable _phrase_sep = phrase_sep
47 method phrase_sep = _phrase_sep
48 method set_phrase_sep sep = _phrase_sep <- sep
50 val mutable _callback = callback
51 method set_callback f = _callback <- f
53 val mutable _ignore_insert_text_signal = false
54 method ignore_insert_text_signal ignore =
55 _ignore_insert_text_signal <- ignore
58 (* TODO Zack: implement history.
59 IDEA: use CTRL-P/N a la emacs.
60 ISSUE: per-phrase or per-line history? *)
61 val phrases_history = Array.create history_size None
62 val mutable history_last_index = -1
63 val mutable history_cur_index = -1
67 let buf = self#buffer in
68 self#set_wrap_mode `CHAR;
69 (* create "USER_INPUT_START" mark. This mark will always point to the
70 * beginning of user input not yet processed *)
71 ignore (buf#create_mark ~name:"USER_INPUT_START"
72 ~left_gravity:true buf#start_iter);
73 ignore (buf#connect#after#insert_text (fun iter text ->
74 if (not _ignore_insert_text_signal) &&
75 (iter#compare buf#end_iter = 0) && (* insertion at end *)
76 (Pcre.pmatch ~rex:trailing_NL_RE text)
80 ~start:(buf#get_iter_at_mark (`NAME "USER_INPUT_START"))
83 let pat = (Pcre.quote _phrase_sep) ^ "\\s*$" in
84 if Pcre.pmatch ~pat inserted_text then begin (* complete phrase *)
86 _callback inserted_text;
90 (* lock old text and bump USER_INPUT_START mark *)
92 let buf = self#buffer in
93 let read_only = buf#create_tag [`EDITABLE false] in
94 buf#apply_tag read_only ~start:buf#start_iter ~stop:buf#end_iter;
95 buf#move_mark (`NAME "USER_INPUT_START") buf#end_iter
97 method echo_prompt () =
98 let buf = self#buffer in
99 self#ignore_insert_text_signal true;
100 buf#insert ~iter:buf#end_iter ~tags:[buf#create_tag prompt_props] prompt;
101 self#ignore_insert_text_signal false;
104 method echo_message msg =
105 let buf = self#buffer in
106 self#ignore_insert_text_signal true;
107 buf#insert ~iter:buf#end_iter ~tags:[buf#create_tag message_props]
109 self#ignore_insert_text_signal false;
112 method echo_error msg =
113 let buf = self#buffer in
114 self#ignore_insert_text_signal true;
115 buf#insert ~iter:buf#end_iter ~tags:[buf#create_tag error_props]
117 self#ignore_insert_text_signal false;
122 ?(prompt = default_prompt) ?(phrase_sep = default_phrase_sep)
123 ?(callback = default_callback)
124 ?buffer ?editable ?cursor_visible ?justification ?wrap_mode ?border_width
125 ?width ?height ?packing ?show ()
129 ?buffer ?editable ?cursor_visible ?justification ?wrap_mode ?border_width
130 ?width ?height ?packing ?show ()
132 new console ~prompt ~phrase_sep ~callback view#as_view