]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitaConsole.ml
renamed Http_client to Http_user_agent to avoid clashes with Gerd's
[helm.git] / helm / matita / matitaConsole.ml
1 (* Copyright (C) 2004, HELM Team.
2  * 
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.
6  * 
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.
11  * 
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.
16  *
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,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://helm.cs.unibo.it/
24  *)
25
26 let default_prompt = "# "
27 let default_phrase_sep = "."
28 let default_callback = fun (phrase: string) -> ()
29
30 let message_props = [ `STYLE `ITALIC ]
31 let error_props = [ `WEIGHT `BOLD ]
32 let prompt_props = [ ]
33
34 let trailing_NL_RE = Pcre.regexp "\n\\s*$"
35
36 exception History_failure
37
38   (** shell like phrase history *)
39 class history size =
40   let size = size + 1 in
41   let decr x = let x' = x - 1 in if x' < 0 then size + x' else x' in
42   let incr x = (x + 1) mod size in
43   object
44     val data = Array.create size ""
45     val mutable hd = 0  (* insertion point *)
46     val mutable tl = -1 (* oldest inserted item *)
47     val mutable cur = -1  (* current item for the history *)
48     method add s =
49       data.(hd) <- s;
50       if tl = -1 then tl <- hd;
51       hd <- incr hd;
52       if hd = tl then tl <- incr tl;
53       cur <- hd
54     method previous =
55       if cur = tl then raise History_failure;
56       cur <- decr cur;
57       data.(cur)
58     method next =
59       if cur = hd then raise History_failure;
60       cur <- incr cur;
61       if cur = hd then "" else data.(cur)
62   end
63
64 class console
65   ?(prompt = default_prompt) ?(phrase_sep = default_phrase_sep)
66   ?(callback = default_callback) ?evbox obj
67 =
68   object (self)
69     inherit GText.view obj
70
71     val mutable _phrase_sep = phrase_sep
72     method phrase_sep = _phrase_sep
73     method set_phrase_sep sep = _phrase_sep <- sep
74
75     val mutable _prompt = prompt
76     method prompt = _prompt
77     method set_prompt prompt = _prompt <- prompt
78
79     val mutable _callback = callback
80     method set_callback f = _callback <- f
81
82     val mutable _ignore_insert_text_signal = false
83     method ignore_insert_text_signal ignore =
84       _ignore_insert_text_signal <- ignore
85
86     val history = new history BuildTimeConf.console_history_size
87
88     initializer
89       let buf = self#buffer in
90       self#set_wrap_mode `CHAR;
91         (* create "USER_INPUT_START" mark. This mark will always point to the
92         * beginning of user input not yet processed *)
93       ignore (buf#create_mark ~name:"USER_INPUT_START"
94         ~left_gravity:true buf#start_iter);
95       ignore (buf#connect#after#insert_text (fun iter text ->
96         if (not _ignore_insert_text_signal) &&
97           (iter#compare buf#end_iter = 0) &&  (* insertion at end *)
98           (Pcre.pmatch ~rex:trailing_NL_RE text)
99         then
100           let inserted_text =
101             buf#get_text
102               ~start:(buf#get_iter_at_mark (`NAME "USER_INPUT_START"))
103               ~stop:buf#end_iter ()
104           in
105           let pat = (Pcre.quote _phrase_sep) ^ "\\s*$" in
106           if Pcre.pmatch ~pat inserted_text then begin (* complete phrase *)
107             self#lock;
108             self#invoke_callback inserted_text;
109             self#echo_prompt ()
110           end));
111       (match evbox with (* history key bindings *)
112       | None -> ()
113       | Some evbox ->
114           List.iter (fun (key, f) -> MatitaGtkMisc.add_key_binding key f evbox)
115             [ GdkKeysyms._p, (fun () -> self#previous_phrase);
116               GdkKeysyms._n, (fun () -> self#next_phrase);
117             ]);
118       ignore (self#connect#after#move_cursor
119         (* avoid cursor being placed at prompt's left *)
120         ~callback:(fun step count ~extend ->
121             let buf = self#buffer in
122             let cursor_iter = buf#get_iter_at_mark `INSERT in
123             let prompt_iter = buf#get_iter_at_mark (`NAME "USER_INPUT_START") in
124             if prompt_iter#compare cursor_iter = 1 then (* prompt > cursor *)
125               buf#place_cursor ~where:prompt_iter))
126
127     method private set_phrase phrase =
128       let buf = self#buffer in
129       buf#delete
130         ~start:(buf#get_iter_at_mark (`NAME "USER_INPUT_START"))
131         ~stop:buf#end_iter;
132       buf#insert ~iter:buf#end_iter phrase
133
134     method private invoke_callback phrase =
135       history#add (* do not push trailing phrase separator *)
136         (String.sub phrase 0
137           (String.length phrase - String.length _phrase_sep));
138       _callback phrase
139
140       (* lock old text and bump USER_INPUT_START mark *)
141     method private lock =
142       let buf = self#buffer in
143       let read_only = buf#create_tag [`EDITABLE false] in
144       buf#apply_tag read_only ~start:buf#start_iter ~stop:buf#end_iter;
145       buf#move_mark (`NAME "USER_INPUT_START") buf#end_iter
146
147     method echo_prompt () =
148       let buf = self#buffer in
149       self#ignore_insert_text_signal true;
150       buf#insert ~iter:buf#end_iter ~tags:[buf#create_tag prompt_props] prompt;
151       self#ignore_insert_text_signal false;
152       self#lock
153
154     method echo_message msg =
155       let buf = self#buffer in
156       self#ignore_insert_text_signal true;
157       buf#insert ~iter:buf#end_iter ~tags:[buf#create_tag message_props]
158         (msg ^ "\n");
159       self#ignore_insert_text_signal false;
160       self#lock
161
162     method echo_error msg =
163       let buf = self#buffer in
164       self#ignore_insert_text_signal true;
165       buf#insert ~iter:buf#end_iter ~tags:[buf#create_tag error_props]
166         (msg ^ "\n");
167       self#ignore_insert_text_signal false;
168       self#lock;
169       self#echo_prompt ()
170
171       (** navigation methods: history, cursor motion, ... *)
172
173     method private previous_phrase =
174       try self#set_phrase history#previous with History_failure -> ()
175     method private next_phrase =
176       try self#set_phrase history#next with History_failure -> ()
177
178   end
179
180 let console
181   ?(prompt = default_prompt) ?(phrase_sep = default_phrase_sep)
182   ?(callback = default_callback) ?evbox
183   ?buffer ?editable ?cursor_visible ?justification ?wrap_mode ?border_width
184   ?width ?height ?packing ?show ()
185 =
186   let view =
187     GText.view
188       ?buffer ?editable ?cursor_visible ?justification ?wrap_mode ?border_width
189       ?width ?height ?packing ?show ()
190   in
191   new console ~prompt ~phrase_sep ~callback ?evbox view#as_view
192