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