]> matita.cs.unibo.it Git - helm.git/blob - helm/gTopLevel/termEditor.ml
(temporary, waiting for abstraction over disambiguators) ported to Andrea &
[helm.git] / helm / gTopLevel / termEditor.ml
1 (* Copyright (C) 2000-2002, 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://cs.unibo.it/helm/.
24  *)
25
26 (******************************************************************************)
27 (*                                                                            *)
28 (*                               PROJECT HELM                                 *)
29 (*                                                                            *)
30 (*                Claudio Sacerdoti Coen <sacerdot@cs.unibo.it>               *)
31 (*                                 06/01/2002                                 *)
32 (*                                                                            *)
33 (*                                                                            *)
34 (******************************************************************************)
35
36 open Printf
37
38 open Disambiguate_types
39
40 (* A WIDGET TO ENTER CIC TERMS *)
41
42 class type term_editor =
43  object
44    method coerce : GObj.widget
45    (* get_as_string returns the unquoted string *)
46    method get_as_string : string
47    method get_metasenv_and_term :
48      context:Cic.context ->
49      metasenv:Cic.metasenv -> Cic.metasenv * Cic.term
50    method reset : unit
51    (* The input of set_term is unquoted *)
52    method set_term : string -> unit
53    method id_to_uris : environment ref
54  end
55
56 let empty_id_to_uris = Environment.empty
57
58 module Make(C: Callbacks) =
59   struct
60
61    module Disambiguate' = Disambiguate.Make(C);;
62
63    class term_editor_impl mqi_handle ?packing ?width ?height
64     ?isnotempty_callback ?share_id_to_uris_with () : term_editor
65    =
66     let id_to_uris =
67      match share_id_to_uris_with with
68         None -> ref empty_id_to_uris
69       | Some obj -> obj#id_to_uris
70     in
71     let input = GText.view ~editable:true ?width ?height ?packing () in
72     let _ =
73      match isnotempty_callback with
74         None -> ()
75       | Some callback ->
76          ignore(input#buffer#connect#changed
77           (function () -> callback (input#buffer#char_count > 0)))
78     in
79      object(self)
80
81       method coerce = input#coerce
82
83       method reset =
84        input#buffer#delete input#buffer#start_iter input#buffer#end_iter
85       (* CSC: txt is now a string, but should be of type Cic.term *)
86
87       method set_term txt =
88        self#reset ;
89        ignore (input#buffer#insert txt)
90
91       (* CSC: this method should disappear *)
92       (* get_as_string returns the unquoted string *)
93       method get_as_string = input#buffer#get_text ()
94
95       method get_metasenv_and_term ~context ~metasenv =
96        let name_context =
97         List.map
98          (function
99              Some (n,_) -> Some n
100            | None -> None
101          ) context
102        in
103         let term =
104           Parser.parse_term (Stream.of_string (input#buffer#get_text ()))
105         in
106         let id_to_uris',metasenv,expr =
107           Disambiguate'.disambiguate_term mqi_handle context metasenv term
108             ~aliases:!id_to_uris
109         in
110         id_to_uris := id_to_uris';
111         (metasenv, expr)
112
113       method id_to_uris = id_to_uris
114    end
115
116    let term_editor = new term_editor_impl
117
118 end
119