]> matita.cs.unibo.it Git - helm.git/blob - helm/gTopLevel/termEditor.ml
- the mathql interpreter is not helm-dependent any more
[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 (* A WIDGET TO ENTER CIC TERMS *)
37
38 class type term_editor =
39  object
40    method coerce : GObj.widget
41    (* get_as_string returns the unquoted string *)
42    method get_as_string : string
43    method get_metasenv_and_term :
44      context:Cic.context ->
45      metasenv:Cic.metasenv -> Cic.metasenv * Cic.term
46    method reset : unit
47    (* The input of set_term is unquoted *)
48    method set_term : string -> unit
49    method id_to_uris : Disambiguate.domain_and_interpretation ref
50  end
51 ;;
52
53 let empty_id_to_uris = ([],function _ -> None);;
54
55 module Make(C:Disambiguate.Callbacks) =
56   struct
57
58    module Disambiguate' = Disambiguate.Make(C);;
59
60    class term_editor_impl mqi_handle ?packing ?width ?height ?isnotempty_callback
61     ?share_id_to_uris_with () : term_editor
62    =
63     let id_to_uris =
64      match share_id_to_uris_with with
65         None -> ref empty_id_to_uris
66       | Some obj -> obj#id_to_uris
67     in
68     let input = GEdit.text ~editable:true ?width ?height ?packing () in
69     let _ =
70      match isnotempty_callback with
71         None -> ()
72       | Some callback ->
73          ignore(input#connect#changed
74           (function () -> callback (input#length > 0)))
75     in
76      object(self)
77       method coerce = input#coerce
78       method reset =
79        input#delete_text 0 input#length
80       (* CSC: txt is now a string, but should be of type Cic.term *)
81       method set_term txt =
82        self#reset ;
83        ignore ((input#insert_text txt) ~pos:0)
84       (* CSC: this method should disappear *)
85       (* get_as_string returns the unquoted string *)
86       method get_as_string =
87        input#get_chars 0 input#length
88       method get_metasenv_and_term ~context ~metasenv =
89        let name_context =
90         List.map
91          (function
92              Some (n,_) -> Some n
93            | None -> None
94          ) context
95        in
96         let lexbuf = Lexing.from_string (input#get_chars 0 input#length) in
97          let dom,mk_metasenv_and_expr =
98           CicTextualParserContext.main
99            ~context:name_context ~metasenv CicTextualLexer.token lexbuf
100          in
101           let id_to_uris',metasenv,expr =
102            Disambiguate'.disambiguate_input mqi_handle
103             context metasenv dom mk_metasenv_and_expr ~id_to_uris:!id_to_uris
104           in
105            id_to_uris := id_to_uris' ;
106            metasenv,expr
107       method id_to_uris = id_to_uris
108    end
109
110    let term_editor = new term_editor_impl
111
112 end
113 ;;