]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitaConsole.ml
snapshot, notably:
[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 open Printf
27
28 open MatitaTypes
29
30 type callback = string -> command_outcome
31
32 (* let default_prompt = "# " *)
33 let default_prompt = ""
34 let default_phrase_sep = "."
35 let default_callback = fun (phrase: string) -> (true, true)
36 let bullet = "∙"
37
38 let message_props = [ `STYLE `ITALIC ]
39 let error_props = [ `WEIGHT `BOLD ]
40 let prompt_props = [ ]
41
42 let trailing_NL_RE = Pcre.regexp "\n\\s*$"
43
44 exception History_failure
45
46   (** shell like phrase history *)
47 class history size =
48   let size = size + 1 in
49   let decr x = let x' = x - 1 in if x' < 0 then size + x' else x' in
50   let incr x = (x + 1) mod size in
51   object
52     val data = Array.create size ""
53     val mutable hd = 0  (* insertion point *)
54     val mutable tl = -1 (* oldest inserted item *)
55     val mutable cur = -1  (* current item for the history *)
56     method add s =
57       data.(hd) <- s;
58       if tl = -1 then tl <- hd;
59       hd <- incr hd;
60       if hd = tl then tl <- incr tl;
61       cur <- hd
62     method previous =
63       if cur = tl then raise History_failure;
64       cur <- decr cur;
65       data.(cur)
66     method next =
67       if cur = hd then raise History_failure;
68       cur <- incr cur;
69       if cur = hd then "" else data.(cur)
70   end
71
72 class console
73   ?(prompt = default_prompt) ?(phrase_sep = default_phrase_sep)
74   ?(callback = default_callback) ?evbox ~(paned:GPack.paned) obj
75 =
76   let console_height = 100 in (* pixels *)
77   object (self)
78     inherit GText.view obj
79
80     val mutable _phrase_sep = phrase_sep
81     method phrase_sep = _phrase_sep
82     method set_phrase_sep sep = _phrase_sep <- sep
83
84     val mutable _prompt = prompt
85     method prompt = _prompt
86     method set_prompt prompt = _prompt <- prompt
87
88     val mutable _callback = callback
89     method set_callback f = _callback <- f
90
91     val mutable _ignore_insert_text_signal = false
92     method ignore_insert_text_signal ignore =
93       _ignore_insert_text_signal <- ignore
94
95     val history = new history BuildTimeConf.console_history_size
96
97     val mutable handle_position = 450
98     val mutable last_phrase = ""
99
100     initializer
101       let buf = self#buffer in
102       self#set_wrap_mode `CHAR;
103       self#hide ();
104         (* create "USER_INPUT_START" mark. This mark will always point to the
105         * beginning of user input not yet processed *)
106       ignore (buf#create_mark ~name:"USER_INPUT_START"
107         ~left_gravity:true buf#start_iter);
108       ignore (buf#connect#after#insert_text (fun iter text ->
109         if (not _ignore_insert_text_signal) &&
110           (iter#compare buf#end_iter = 0) &&  (* insertion at end *)
111           (Pcre.pmatch ~rex:trailing_NL_RE text)
112         then
113           let inserted_text =
114             MatitaMisc.strip_trailing_blanks
115               (buf#get_text
116                 ~start:(buf#get_iter_at_mark (`NAME "USER_INPUT_START"))
117                 ~stop:buf#end_iter ())
118           in
119           let pat = (Pcre.quote _phrase_sep) ^ "\\s*$" in
120           if Pcre.pmatch ~pat inserted_text then begin (* complete phrase *)
121             self#lock;
122             last_phrase <- inserted_text;
123             self#invoke_callback inserted_text;
124             self#echo_prompt ()
125           end));
126       (match evbox with (* history key bindings *)
127       | None -> ()
128       | Some evbox ->
129           List.iter (fun (key, f) -> MatitaGtkMisc.add_key_binding key f evbox)
130             [ GdkKeysyms._p, (fun () -> self#previous_phrase);
131               GdkKeysyms._n, (fun () -> self#next_phrase);
132             ]);
133       ignore (self#connect#after#move_cursor
134         (* avoid cursor being placed at prompt's left *)
135         ~callback:(fun step count ~extend ->
136             let buf = self#buffer in
137             let cursor_iter = buf#get_iter_at_mark `INSERT in
138             let prompt_iter = buf#get_iter_at_mark (`NAME "USER_INPUT_START") in
139             if prompt_iter#compare cursor_iter = 1 then (* prompt > cursor *)
140               buf#place_cursor ~where:prompt_iter))
141
142     method private set_phrase phrase =
143       let buf = self#buffer in
144       buf#delete
145         ~start:(buf#get_iter_at_mark (`NAME "USER_INPUT_START"))
146         ~stop:buf#end_iter;
147       buf#insert ~iter:buf#end_iter phrase
148
149     method private invoke_callback phrase =
150       history#add phrase;
151       let (success, hide) = _callback phrase in
152       if hide then self#hide ()
153
154     method clear () =
155       let buf = self#buffer in
156       buf#delete ~start:buf#start_iter ~stop:buf#end_iter
157
158       (* lock old text and bump USER_INPUT_START mark *)
159     method private lock =
160       let buf = self#buffer in
161       let read_only = buf#create_tag [`EDITABLE false] in
162       buf#apply_tag read_only ~start:buf#start_iter ~stop:buf#end_iter;
163       buf#move_mark (`NAME "USER_INPUT_START") buf#end_iter
164
165     method echo_prompt () =
166       let buf = self#buffer in
167       self#ignore_insert_text_signal true;
168       buf#insert ~iter:buf#end_iter ~tags:[buf#create_tag prompt_props] prompt;
169       self#ignore_insert_text_signal false;
170       self#lock
171
172     method echo_message msg =
173       self#show ();
174       let buf = self#buffer in
175       self#ignore_insert_text_signal true;
176       buf#insert ~iter:buf#end_iter ~tags:[buf#create_tag message_props]
177         (msg ^ "\n");
178       self#ignore_insert_text_signal false;
179       self#lock
180
181     method echo_error msg =
182       self#show ();
183       let buf = self#buffer in
184       self#ignore_insert_text_signal true;
185       buf#insert ~iter:buf#end_iter ~tags:[buf#create_tag error_props]
186         (msg ^ "\n");
187       self#ignore_insert_text_signal false;
188       self#lock
189
190     method private get_paned_prop s =
191       Gobject.get { Gobject.name = s; Gobject.conv = Gobject.Data.int }
192         paned#as_widget
193     method private get_position = self#get_paned_prop "position"
194     method private get_min_position = self#get_paned_prop "min-position"
195     method private get_max_position = self#get_paned_prop "max-position"
196
197     method show ?(msg = "") () =
198       self#buffer#insert msg;
199       paned#set_position (self#get_max_position - console_height);
200       self#misc#grab_focus ()
201     method hide () =
202       paned#set_position self#get_max_position
203     method toggle () =
204       let pos = self#get_position in
205       if pos > self#get_max_position - console_height then
206         self#show ()
207       else
208         self#hide ()
209
210       (** navigation methods: history, cursor motion, ... *)
211
212     method private previous_phrase =
213       try self#set_phrase history#previous with History_failure -> ()
214     method private next_phrase =
215       try self#set_phrase history#next with History_failure -> ()
216
217     method wrap_exn: 'a. (unit -> 'a) -> 'a option =
218       fun f ->
219         try
220           Some (f ())
221         with exn ->
222           (match exn with (* highlight parse errors in user input *)
223           | CicTextualParser2.Parse_error (floc, msg) ->
224               let buf = self#buffer in
225               let (x, y) = CicAst.loc_of_floc floc in
226               let red = buf#create_tag [`FOREGROUND "red"] in
227               let (start_error_pos, end_error_pos) =
228                 buf#end_iter#backward_chars (String.length last_phrase - x),
229                 buf#end_iter#backward_chars (String.length last_phrase - y)
230               in
231               if x - y = 0 then (* no region to highlight, let's add an hint
232                                 about where the error occured *)
233                 buf#insert ~iter:end_error_pos ~tags:[red] bullet
234               else  (* highlight the region where the error occured *)
235                 buf#apply_tag red ~start:start_error_pos ~stop:end_error_pos;
236           | _ -> ());
237           self#echo_error (explain exn);
238           None
239
240   end
241
242 let console
243   ?(prompt = default_prompt) ?(phrase_sep = default_phrase_sep)
244   ?(callback = default_callback) ?evbox ~paned
245   ?buffer ?editable ?cursor_visible ?justification ?wrap_mode ?border_width
246   ?width ?height ?packing ?show ()
247 =
248   let view =
249     GText.view
250       ?buffer ?editable ?cursor_visible ?justification ?wrap_mode ?border_width
251       ?width ?height ?packing ?show ()
252   in
253   new console ~prompt ~phrase_sep ~callback ?evbox ~paned view#as_view
254
255 (* vim: set encoding=utf8: *)