]> matita.cs.unibo.it Git - helm.git/blob - helm/gTopLevel/termEditor.ml
ocaml 3.09 transition
[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 (* A WIDGET TO ENTER CIC TERMS *)
39
40 class type term_editor =
41  object
42    method coerce : GObj.widget
43    (* get_as_string returns the unquoted string *)
44    method get_as_string : string
45    method get_metasenv_and_term :
46      context:Cic.context ->
47      metasenv:Cic.metasenv -> 
48      Cic.metasenv * Cic.term * CicUniv.universe_graph
49    method reset : unit
50    (* The input of set_term is unquoted *)
51    method set_term : string -> unit
52    method environment : DisambiguatingParser.EnvironmentP3.t ref
53  end
54
55 module Make(C:DisambiguateTypes.Callbacks) =
56   struct
57
58    module Disambiguate' = DisambiguatingParser.Make(C);;
59
60    class term_editor_impl ~(dbd:Mysql.dbd) ?packing ?width ?height
61     ?isnotempty_callback ?share_environment_with () : term_editor
62    =
63     let environment =
64      match share_environment_with with
65         None -> ref (*DisambiguateTypes.empty_environment*)
66           (DisambiguatingParser.EnvironmentP3.of_string
67             DisambiguatingParser.EnvironmentP3.empty)
68       | Some obj -> obj#environment
69     in
70     let input = GText.view ~editable:true ?width ?height ?packing () in
71     let _ =
72      match isnotempty_callback with
73         None -> ()
74       | Some callback ->
75          ignore(input#buffer#connect#changed
76           (function () -> callback (input#buffer#char_count > 0)))
77     in
78      object(self)
79
80       method coerce = input#coerce
81
82       method reset =
83        input#buffer#delete input#buffer#start_iter input#buffer#end_iter
84       (* CSC: txt is now a string, but should be of type Cic.term *)
85
86       method set_term txt =
87        self#reset ;
88        ignore (input#buffer#insert txt)
89
90       (* CSC: this method should disappear *)
91       (* get_as_string returns the unquoted string *)
92       method get_as_string = input#buffer#get_text ()
93
94       method get_metasenv_and_term ~context ~metasenv =
95        let name_context =
96         List.map
97          (function
98              Some (n,_) -> Some n
99            | None -> None
100          ) context
101        in
102         let environment',metasenv,expr,ugraph =
103          match
104           Disambiguate'.disambiguate_term ~dbd ~context ~metasenv
105            (input#buffer#get_text ()) ~initial_ugraph:CicUniv.empty_ugraph
106             ~aliases:!environment
107          with
108             [environment',metasenv,expr,u] -> environment',metasenv,expr,u
109           | _ -> assert false
110         in
111         environment := environment';
112         (metasenv, expr,ugraph) 
113           (* TASSI: FIXME ?are we sure we have to keep this?  *)
114
115       method environment = environment
116    end
117
118    let term_editor = new term_editor_impl
119
120 end
121