1% BEGIN LICENSE BLOCK
2% Version: CMPL 1.1
3%
4% The contents of this file are subject to the Cisco-style Mozilla Public
5% License Version 1.1 (the "License"); you may not use this file except
6% in compliance with the License.  You may obtain a copy of the License
7% at www.eclipse-clp.org/license.
8% 
9% Software distributed under the License is distributed on an "AS IS"
10% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See
11% the License for the specific language governing rights and limitations
12% under the License. 
13% 
14% The Original Code is  The ECLiPSe Constraint Logic Programming System. 
15% The Initial Developer of the Original Code is  Cisco Systems, Inc. 
16% Portions created by the Initial Developer are
17% Copyright (C) 2006 Cisco Systems, Inc.  All Rights Reserved.
18% 
19% Contributor(s): Joachim Schimpf, IC-Parc
20% 
21% END LICENSE BLOCK
22%
23% $Id: embtcl.tex,v 1.3 2015/07/04 17:21:01 kish_shen Exp $
24%
25% Author:       Joachim Schimpf, IC-Parc
26%
27
28%----------------------------------------------------------------------
29\chapter{Embedding into Tcl/Tk}
30\label{chaptcl}
31%HEVEA\cutdef[1]{section}
32%----------------------------------------------------------------------
33
34This chapter describes how to embed {\eclipse} into a Tcl host program.
35Tcl/Tk is a cross-platform toolkit for the development of graphical
36user interfaces. 
37The facilities described here make it possible to implement {\eclipse}
38applications with platform-independent graphical user interfaces.
39The interface is similar in spirit to the {\eclipse} embedding
40interfaces for other languages.
41
42An alternative method of using {\eclipse} with Tcl is to use the Tcl remote
43interface, described in chapter~\ref{chapremote}. In this case, the
44{\eclipse} is ran as a separate program. The facilities provided by the
45remote and embedding interfaces are largely compatible, so that it is
46possible to reuse the same Tcl and {\eclipse} code in both interface. The
47advantage of the embedding interface is that {\eclipse} is much more
48tightly coupled with the Tcl program, and communication between the two is
49more efficient. The advantage of the remote interface is that the Tcl and
50{\eclipse} programs are not tightly coupled, and in fact can be run on
51separate machines.  
52
53The {\bf tkeclipse} development environment is entirely
54implemented using the facilities described in this chapter. The toplevel of
55{\bf tkeclipse} is currently implemented using only the embedding interface,
56but the development tools can be used with both the embedding and remote
57interfaces. 
58
59
60
61%----------------------------------------------------------------------
62\section{Loading the interface}
63%----------------------------------------------------------------------
64The {\eclipse} interface is provided as a Tcl-package called {\bf eclipse},
65and can be loaded as follows:
66\index{package eclipse}
67\begin{quote}\begin{verbatim}
68lappend auto_path "/location/of/my/eclipse/lib_tcl"
69package require eclipse
70\end{verbatim}\end{quote}
71% It might also be necessary to provide information about where the
72% DLLs or shared library files can be found.
73% On Unix systems this is done by setting the LD_LIBRARY_PATH environment
74% variable, e.g.\
75% \begin{quote}\begin{verbatim}
76% LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/location/of/my/eclipse/lib/sparc_sunos5
77% \end{verbatim}\end{quote}
78
79%----------------------------------------------------------------------
80\section{Initialising the {\eclipse} Subsystem}
81%----------------------------------------------------------------------
82These are the Tcl commands needed to initialize an embedded  {\eclipse}.
83\begin{description}
84\item[\index{ec_set_option}ec_set_option {\it option_name option_value}]\ \\
85        Set the value of an initialisation option for {\eclipse}.
86        This must be done before invoking ec_init.
87        The available option_names are:
88        localsize, globalsize, privatesize, sharedsize,
89        default_module, eclipsedir, io.
90	See Appendix \ref{chapecoptions} for their meaning.
91
92\item[\index{ec_init}ec_init {\it ?peername?}]\ \\
93        Initialise the {\eclipse} engine. This is required before any other
94        commands of this interface (except ec_set_option) can be used. The
95	optional argument {\it peername} is the name of the embedding peer,
96        which defaults to `master'.
97\end{description}
98Example Tcl code for initialising {\eclipse}:
99\begin{quote}\begin{verbatim}
100lappend auto_path "/location/of/my/eclipse/lib_tcl"
101package require eclipse
102#ec_set_option io 0;    # input/output/error via tty (for testing)
103ec_set_option io 2;     # input/output/error via queues (default)
104ec_init
105\end{verbatim}\end{quote}
106
107Apart from the basic functionality in {\bf package eclipse} which
108takes care of linking Tcl to {\eclipse}, there is a
109\index{package eclipse_tools}
110{\bf package eclipse_tools} containing Tk interfaces to
111{\eclipse} facilities like debugging and development support.
112This package should be used when developing Tcl/Tk/{\eclipse} applications.
113To add these tools to your application, load the package and add the
114tools menu to your application's menu bar. Your code should then
115contain the following pattern:
116\begin{quote}\begin{verbatim}
117package require eclipse
118package require eclipse_tools
119...
120menu .mbar
121...
122ec_init
123...
124ec_tools_init .mbar.tools
125\end{verbatim}\end{quote}
126See also the examples in the lib_tcl directory of the {\eclipse} installation.
127
128
129%----------------------------------------------------------------------
130\section{Shutting down the {\eclipse} Subsystem}
131%----------------------------------------------------------------------
132
133The embedded {\eclipse} is terminated when quitting from the Tcl/Tk
134application. The following Tcl command should be called just before the
135application is terminated to allow {\eclipse} to shutdown:
136
137\begin{description}
138\item[\index{ec_cleanup}ec_cleanup]\ \\
139        Shutdown the {\eclipse} engine. 
140\end{description}
141
142%----------------------------------------------------------------------
143\section{Passing Goals and Control to \eclipse}
144%----------------------------------------------------------------------
145The control flow between Tcl and {\eclipse} is conceptually thread-based.
146An {\eclipse} goal is executed by using the {\bf ec_rpc} mechanism. The
147goal is posted from Tcl, and control is transferred automatically to
148{\eclipse} to allow the goal to be executed. Control can also be explicitly
149transferred to {\eclipse} using {\bf ec_resume}. Furthermore, handler goals
150can be implicitly invoked on I/O operations on queues (this is described in
151more detail in section~\ref{tclembedqueues}, with implicit
152transfer of control.
153
154The related commands are the following:
155\begin{description}
156\item[\index{ec_rpc (Tcl embedding interface)}ec_rpc {\it goal ?format?}]\ \\
157        Remote {\eclipse} predicate call.
158        It calls goal in the default module. The goal should be simple
159	in the sense that it can only succeed, fail or throw.
160	It must not call
161	\bipref{yield/2}{../bips/kernel/externals/yield-2.html}.
162        Any choicepoints the goal leaves will be discarded.
163
164	Unlike {\bf ec_resume}, calls to {\bf ec_rpc} can be nested
165	and can be used from within Tcl queue event handlers.
166
167	If no format argument is given, the goal is assumed to be in
168	{\eclipse} syntax. If a {\it format} argument is provided,
169	the {\eclipse} goal is constructed from {\it goal} and {\it format},
170	according to the conversion rules explained in section \ref{secexdrtcl}.
171
172        On success, {\bf ec_rpc} returns the (possibly more instantiated)
173	goal as a Tcl data structure, otherwise "fail" or "throw" respectively.
174
175	This is the recommended way of executing {\eclipse} code from Tcl,
176	and passing the results back (via output arguments) to Tcl.
177
178\item[\index{ec_running (Tcl embedding interface)}ec_running]\ \\
179	checks whether an asynchronous {\eclipse} thread is still running.
180	If that is the case, the only interface function that can be
181	invoked reliably is {\bf ec_post_event}.
182
183\item[\index{ec_resume (Tcl embedding interface)}ec_resume {\it ?async?}]\ \\
184        resume execution of the {\eclipse} engine:  All posted events
185        and goals will be executed.  The return value will be "success"
186        if the posted goals succeed, "fail" if the goals fail, and
187	"yield" if control was transferred because of a
188        \bipref{yield/2}{../bips/kernel/externals/yield-2.html}
189        predicate call in the {\eclipse} code. No parameters can be passed.
190
191	If the {\it async} parameter is 1 (default 0), the {\eclipse}
192	execution is resumed in a separate thread, provided this is
193	supported by the operating system.  The effect of this is that
194	Tcl/Tk events can still be handled while {\eclipse} is
195	running, so the GUI does not freeze during computation. 
196	However, only one {\eclipse} thread can be running at any
197	time, so before doing another call to {\bf ec_resume}, {\bf
198	ec_handle_events} or {\bf ec_rpc} one should use {\bf
199	ec_running} to check whether there is not a thread still running. 
200
201\item[\index{ec_flush (Tcl embedding interface)}ec_flush {\it ?stream_nr?
202	?nbytes?}]\ \\
203        flushes the Tcl end of a to-{\eclipse} queue (see
204        section~\ref{tclembedqueues}) that has the
205        {\eclipse} stream number {\it stream_nr}. Control is then
206        briefly transferred to {\eclipse} so that any events that are raised
207        can be handled. Afterwards the control is passed back to Tcl.
208        {\it nbytes\/} is a dummy argument
209        and is provided for compatibility with the Tcl remote interface
210        only.
211
212\end{description}
213
214
215%----------------------------------------------------------------------
216\section{Communication via Queues}
217%----------------------------------------------------------------------
218\label{tclembedqueues}
219
220\label{ecqueueconnect}
221The most flexible way of passing data between {\eclipse} and Tcl is
222via the I/O facilities of the two languages,
223ie.\ via {\eclipse} queue streams which can be connected to Tcl channels.
224
225Currently, a communication channel between {\eclipse} and Tcl is created
226from Tcl, which appears as an {\eclipse} queue in {\eclipse}, and a channel
227in Tcl. The queue has a symbolic name and a stream number in {\eclipse},
228and has a channel name in Tcl. Facilities are provided to interconvert
229between these names. 
230
231Queues pass data between {\eclipse} and Tcl in one direction: either from
232{\eclipse} to Tcl (from-{\eclipse}), or from Tcl to {\eclipse}
233(to-{\eclipse}). Queues are created with the direction specified. The
234queues should be viewed as communication channels: data is written to the
235queue, and it only becomes available to the other side when the queue is
236flushed. This is done by calling the predicate \bipref{flush/1}{../bips/kernel/iostream/flush-1.html} on the
237{\eclipse} side, and by invoking {\bf ec_flush} on the Tcl side. The flush
238also has the effect of briefly transferring control to the other side to
239allow handlers to handle the data (see
240section~\ref{embtclhandlers})\footnote{Strictly speaking, flushing is not
241necessary in the embedding case to make the data available to the other
242side. However, it is needed in the remote case, and for compatibility and
243good practice, flushing is recommended.}
244
245
246\begin{description}
247\item[\index{ec_queue_create (Tcl embedding interface)}ec_queue_create {\it
248eclipse_stream_name mode ?command? ?event?}]\ \\
249        Creates a queue between Tcl and {\eclipse}. On
250        the Tcl side, a Tcl channel is created. On the {\eclipse} side, the 
251        queue would be given the symbolic name {\it eclipse_stream_name}. 
252        The {\it mode} argument indicates the direction of the queue, and
253        can either be fromec or toec\footnote{For compatibility with previous
254        versions of the embedding Tcl interface, the mode can also be
255        specified as r (equivalent to fromec) or w (equivalent to
256        toec). These can be somewhat confusing as read/write status depends
257        on from which side the queue is viewed (a read queue in {\eclipse} is a
258        write queue in Tcl).}.
259        The procedure returns a channel identifier for use in commands
260        like {\bf puts}, {\bf read}, {\bf ec_read_exdr},
261        {\bf ec_write_exdr} or {\bf close}. The optional arguments {\it
262        command\/} and {\it event\/} specifies the data handler for the
263        queue: {\it command\/} is the name
264        of the Tcl
265        procedure for handling the data, with its user defined arguments. 
266        {\it event} is the name of the event that will be
267        raised on the {\eclipse} side.
268        As a handler can only be defined for one side,
269        either {\it event\/} or {\it command\/} should be undefined
270        (\verb'{}'). 
271
272\item[\index{ec_queue_close (Tcl embedding interface)}ec_queue_close {\it
273eclipse_stream_name}]\ \\
274	Closes the queue with the {\eclipse} name of {\it
275	ec_stream_name}. 
276
277\item[\index{ec_stream_nr (Tcl embedding interface)}ec_stream_nr {\it eclipse_stream_name}]\ \\
278        This command returns the {\eclipse} stream number given a
279        symbolic stream name (this is the same operation that the
280        {\eclipse} built-in
281        \bipref{get_stream/2}{../bips/kernel/iostream/get_stream-2.html}
282        performs). 
283
284\item[\index{ec_streamname_to_streamnum (Tcl embedding
285        interface)}ec_streamname_to_streamnum {\it eclipse_stream_name}]\ \\
286	This is an alias for {\it ec_stream_nr} for compatibility purposes.
287
288\item[\index{ec_streamname_to_channel (Tcl embedding interface)}ec_streamname_to_channel {\it eclipse_stream_name}]\ \\
289	Returns the Tcl channel name for the  queue with the
290	symbolic name {\it eclipse_name}. 
291
292\item[\index{ec_streamnum_to_channel (Tcl embedding interface)}ec_streamnum_to_channel {\it eclipse_stream_number}]\ \\
293	Returns the Tcl channel name for the  queue with the
294	{\eclipse} stream number {\it eclipse_stream_number}. 
295
296\item[\index{ec_async_queue_create (Tcl embedding
297	interface)}ec_async_queue_create {\it eclipse_stream_name mode
298	?command? ?event?}]\ \\
299	This is provided mainly for compatibility with the Tcl remote
300	interface. The command is an alias for {\bf ec_queue_create} in the
301	embedding interface. Certain uses of the queues in the embedding
302	interface cannot be duplicated using the synchronous queues of the
303	remote interface. Instead, asynchronous queues are needed (see
304	chapter~\ref{chapremote} for more details). This command is
305	provided to allow the same code to be used for both interfaces. 
306	Note that it is possible to use the asynchronous queues of the remote
307	interface in ways that are incompatible with the embedding interface.
308
309\end{description}
310
311%++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
312\subsection{From-{\eclipse} to Tcl}
313%++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
314To create a queue from {\eclipse} to Tcl, use {\bf ec_queue_create} with
315the {\it mode\/} argument set to {\bf fromec}, e.g.\
316
317\begin{quote}\begin{verbatim}
318Tcl:            set my_in_channel [ec_queue_create my_out_queue fromec]
319\end{verbatim}\end{quote}
320
321Once the queue is created, it
322can be used, e.g.\ by writing into it with {\eclipse}'s
323\bipref{write/2}{../bips/kernel/ioterm/write-2.html} builtin,
324and reading using Tcl's {\bf read} command:
325\begin{quote}\begin{verbatim}
326ECLiPSe:        write(my_out_queue, hello),
327                flush(my_out_queue).
328
329Tcl:            set result [read $my_in_channel 5]
330\end{verbatim}\end{quote}
331The disadvantage of using these low-level primitives is that
332for reading one must know exactly how many bytes to read.
333It is therefore recommended to use the EXDR ({\eclipse} external data
334representation, see section \ref{secexdrtcl}) format for communication.
335This allows to send and receive structured and typed data.
336The primitives to do that are
337\bipref{write_exdr/2}{../bips/kernel/ioterm/write_exdr-2.html}
338on the {\eclipse} side and
339ec_read_exdr (section \ref{ecreadexdr}) on the Tcl side:
340\begin{quote}\begin{verbatim}
341ECLiPSe:        write_exdr(my_out_queue, foo(bar,3)),
342                flush(my_out_queue).
343	        
344Tcl:            set result [ec_read_exdr $my_in_channel]
345\end{verbatim}\end{quote}
346In the example, the Tcl result will be the list {\tt \{foo bar 3\}}.
347For details about the mapping see section \ref{secexdrtcl}.
348
349%++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
350\subsection{To-{\eclipse} from Tcl}
351%++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
352To create a queue from Tcl to {\eclipse} to Tcl, use {\bf ec_queue_create} with
353the {\it mode\/} argument set to {\bf toec}, e.g.\
354\begin{quote}\begin{verbatim}
355Tcl:            set my_out_channel [ec_queue_create my_in_queue toec]
356\end{verbatim}\end{quote}
357Now the queue can be used, e.g.\ by writing into it with Tcl's {\bf puts}
358command and by reading using {\eclipse}'s
359\bipref{read_string/4}{../bips/kernel/iochar/read_string-4.html}
360builtin:
361\begin{quote}\begin{verbatim}
362Tcl:            puts $my_out_channel hello
363                ec_flush [ec_streamname_to_streamnum my_in_queue] 5
364
365ECLiPSe:        read_string(my_in_queue, "", 5, Result).
366\end{verbatim}\end{quote}
367The disadvantage of using these low-level primitives is that
368for reading one must know exactly how many bytes to read, or define
369a delimiter character.
370It is therefore recommended to use the EXDR ({\eclipse} external data
371representation, see section \ref{secexdrtcl}) format for communication.
372This allows to send and receive structured and typed data.
373The primitives to do that are
374ec_read_exdr (section \ref{ecwriteexdr}) on the Tcl side and
375\bipref{read_exdr/2}{../bips/kernel/ioterm/read_exdr-2.html}
376on the {\eclipse} side:
377\begin{quote}\begin{verbatim}
378Tcl:            ec_write_exdr $my_out_channel {foo bar 3} (SI)
379                ec_flush [ec_streamname_to_streamnum my_in_queue]
380
381ECLiPSe:        read_exdr(my_in_queue, Result).
382\end{verbatim}\end{quote}
383In the example, the {\eclipse} result will be the term {\tt foo("bar",3)}.
384For details about the mapping see section \ref{secexdrtcl}.
385
386
387%++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
388%\section{Event-driven Communication}
389\section{Attaching Handlers to Queues}
390\label{embtclhandlers}
391
392In order to handle {\eclipse} I/O on queues more conveniently,
393it is possible to associate a handler with every queue, either on the Tcl
394or {\eclipse} side.
395These handlers can be invoked automatically whenever the other side initiates
396an I/O operation.
397
398\subsection{Tcl handlers}
399%++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
400
401\subsubsection{To-{\eclipse} queues}
402
403For this purpose, the to-{\eclipse} queue must be created with the {\it
404command\/} argument set. The following example creates a queue that
405can be written from the {\eclipse} side, and whose contents, if flushed,
406is automatically displayed in a text widget:
407\begin{quote}\begin{verbatim}
408Tcl:      pack [text .tout]
409          ec_queue_create my_out_queue toec {ec_stream_to_window "" .tout} {}
410\end{verbatim}\end{quote}
411Assume that {\eclipse} is then resumed, writes to the queue and
412flushes it.  This will briefly pass control back to Tcl, the {\bf
413ec_stream_to_window}-handler will be executed (with the stream number
414added to its arguments), afterwards control is passed back to
415{\eclipse}.  Note that {\bf ec_stream_to_window} is a predefined
416handler which reads the whole queue contents and displays it in the
417given text widget.
418
419\subsubsection{From-{\eclipse} queues}
420Similarly, a from-{\eclipse} queue could be created as follows:
421\begin{quote}\begin{verbatim}
422Tcl:      ec_queue_connect my_in_queue fromec \
423                        {ec_stream_input_popup "Type here:"} {}
424\end{verbatim}\end{quote}
425Assume that {\eclipse} is then resumed and reads from my_in_queue.
426This will briefly yield control back to Tcl, the
427{\bf ec_stream_input_popup}-handler will be executed,
428afterwards control is passed back to {\eclipse}.
429
430
431Three predefined handlers are provided:
432\begin{description}
433\item[\index{ec_stream_to_window (Tcl embedding interface)}ec_stream_to_window {\it tag text_widget stream_nr}]\ \\
434        Inserts all the current contents of the specified queue stream
435        at the end of the existing text_widget, using tag.
436\item[\index{ec_stream_to_window_sync (Tcl embedding interface)}ec_stream_to_window_sync {\it tag text_widget stream_nr ?length?}]\ \\
437	This is provided for compatibility with the Tcl remote
438	interface. This command is essentially an alias for {\it
439	ec_stream_to_window}, with an optional dummy argument {\it length}
440	that is ignored.
441
442\item[\index{ec_stream_output_popup (Tcl embedding interface)}ec_stream_output_popup {\it label_text stream_nr}]\ \\
443        Pops up a window displaying the label_text,
444        a text field displaying the contents of the specified queue stream,
445        and an ok-button for closing.
446
447\item[\index{ec_stream_input_popup (Tcl embedding interface)}ec_stream_input_popup {\it label_text stream_nr}]\ \\
448        Pops up a window displaying the label_text, an input field
449        and an ok-button. The text typed into the input field will
450        be written into the specified queue stream.
451\end{description}
452
453
454When {\eclipse} is initialised with the default options, its {\bf output} and
455{\bf error} streams are queues and have the {\bf ec_stream_output_popup} handler
456associated.  Similarly, the {\bf input} stream is a queue with the
457{\bf ec_stream_input_popup} handler attached.
458These handler settings may be changed by the user's Tcl code.
459
460
461
462
463%++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
464\subsection{{\eclipse} handlers}
465\label{secqevent}
466%++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
467A to-{\eclipse} queue can be configured to
468raise an {\eclipse}-event (see
469\bipref{event/1}{../bips/kernel/event/event-1.html}
470and the User Manual chapter on event handling)
471whenever the Tcl program writes something into the
472previously empty queue.
473To allow that, the queue must have been created with the {\it event\/}
474argument of {\bf ec_queue_create} set, 
475e.g.\footnote{It is possible to use the same name for both the queue stream itself and
476the event. This simplifies the event handler code because it receives that
477name as an argument.}
478\begin{quote}\begin{verbatim}
479Tcl:  set my_out_channel [ec_queue_create my_queue toec {} my_queue_event]
480\end{verbatim}\end{quote}
481Assuming something was written into the queue from within Tcl code, the
482{\eclipse} event will be handled if the queue is flushed on the Tcl side
483with the command {\bf ec_flush}:
484
485\begin{quote}\begin{verbatim}
486Tcl:  puts $my_out_channel hello
487      ec_flush [ec_streamname_to_streamnum myqueue]
488\end{verbatim}\end{quote}
489
490If myqueue was empty, then the {\bf puts} would raise the event
491\verb'my_queue_event'. The {\bf ec_flush} transfer control over to
492{\eclipse}, so that the event can be handled.
493
494
495%----------------------------------------------------------------------
496\section{Obtaining the Interface Type}
497%----------------------------------------------------------------------
498
499Generally, the Tcl embedded and remote interfaces are designed to allow the
500user to write code that can be used via both interfaces. However, sometimes
501it may be necessary to distinguish the two. This can be done via:
502
503\begin{description}
504\item[\index{ec_interface_type (Tcl embedding interface)}ec_interface_type]\ \\
505	returns embedded for the embedding interface, and remote for the
506        remote interface.
507\end{description}
508
509
510%----------------------------------------------------------------------
511\section{Type conversion between Tcl and {\eclipse}}
512\label{secexdrtcl}
513%----------------------------------------------------------------------
514
515EXDR ({\eclipse} External Data Representation, see also chapter
516\ref{chapexdr}) is a data encoding that allows to represent a
517significant subset of the {\eclipse} data types.  The following Tcl
518primitives are provided to handle EXDR-format:
519\begin{description}
520\item[\label{ecwriteexdr}\index{ec_write_exdr}ec_write_exdr {\it channel data ?format?}]\ \\
521        write an EXDR-term onto the given channel.
522        The term is constructed using the {\it data} argument and
523        the additional type information provided in the {\it format}
524        argument. If no format is specified, it defaults to S (string).
525
526%\item[ec_read_exdr {\it channel ?typevar?}]
527\item[\label{ecreadexdr}\index{ec_read_exdr}ec_read_exdr {\it channel}]\ \\
528        reads an EXDR-term from the given channel and returns it
529        as a Tcl data structure, according to its type. Note that,
530        since Tcl does not have a strong type system, some typing
531        information can get lost in this process (e.g.\ string vs.\ atom).
532
533\item[\index{ec_tcl2exdr}ec_tcl2exdr {\it data ?format?}]\ \\
534        This is the low-level primitive to encode the given {\it data} and
535        type information in {\it format} to an EXDR-string which is
536        suitable for sending over communication links to {\eclipse} or
537        other agents which can decode EXDR-format.
538        If no format is specified, it defaults to S (string).
539
540\item[\index{ec_exdr2tcl}ec_exdr2tcl {\it exdr_string}]\ \\
541        This is the low-level primitive to decode an EXDR-string.
542        It returns a Tcl data structure, according to the type information
543        encoded in the EXDR-string. Note that,
544        since Tcl does not have a strong type system, some typing
545        information can get lost in this process (e.g.\ string vs.\ atom).
546\end{description}
547
548Since Tcl is an untyped language, all commands which create EXDR terms
549accept, in addition to the data, an optional {\bf format} argument
550which allows all EXDR data types to be specified.
551The syntax is as follows:
552
553\begin{quote}
554\begin{tabular}{|lll|}
555\hline
556{\bf To create EXDR type} & {\bf use \lt format\gt} & {\bf data required}\\
557\hline
558String        &  S              & string (binary) \\
559String        &  U              & string (utf8) \\
560Integer/Long  &  I              & integer \\
561Double        &  D              & double \\
562List          &  \lbr\lt {\bf formats}\gt\rbr    & fixed length list \\
563List          &  \lbr\lt {\bf formats}\gt *\rbr   & list \\
564Struct        &  (\lt {\bf formats}\gt)    & fixed list, first elem functor name \\
565Struct        &  (\lt {\bf formats}\gt *)   & list, first elem functor name \\
566Anonymous Variable     &  _              & string "_" \\
567\hline
568\end{tabular}
569\end{quote}
570
571Here are some examples that show which Tcl data/format pair corresponds
572to which {\eclipse} term (the curly brackets are just Tcl quotes and
573not part of the format string):
574\begin{quote}\begin{verbatim}
575Tcl data        Tcl format      Eclipse term
576
577hello           S               "hello"
578hello           ()              hello
579123             S               "123"
580123             I               123
581123             D               123.0
582123             ()              '123'
583{a 3 4.5}       {[SID]}         ["a", 3, 4.5]
584{a 3 4.5}       S               "a 3 4.5"
585{1 2 3 4}       {[I*]}          [1, 2, 3, 4]
586{f 1 2 3}       {(I*)}          f(1,2,3)
587{is _ {- 1 2}}  {(_(II))}       _ is 1-2
588\end{verbatim}\end{quote}
589
590\section{Incompatible and obsolete commands}
591
592Here is a list of commands in the embedding interface that are retained for
593compatibility purposes with previous versions. They have no equivalent in
594the Tcl remote interface, and their use for new code is discouraged.
595
596\begin{description}
597\item[\index{ec_post_goal}ec_post_goal {\it goal ?format?}]\ \\
598        post a goal that will be executed when {\eclipse} is resumed.
599	If no {\it format} argument is given, the goal is taken to be a string
600	in {\eclipse} syntax.  Note that (unlike with the C/C++
601	interface) it is not possible to retrieve any variable
602	bindings from {\eclipse} after successful execution of the
603	goal.
604	To pass information from {\eclipse} to Tcl, use queue streams
605	as described later on.  Example:
606	\begin{verbatim}
607	ec_post_goal {go("hello",27)}
608	\end{verbatim}
609
610	If a {\it format} argument is provided, the {\eclipse} goal is
611	constructed from {\it goal} data and {\it format}, according to
612	the conversion rules explained in section \ref{secexdrtcl}. Example:
613	\begin{verbatim}
614	ec_post_goal {go hello 27} (SI)
615	\end{verbatim}
616
617	Posting several goals is the same as posting the conjunction
618	of these goals.  Note that simple, deterministic goals can be
619	executed independently of the posted goals using the {\bf
620	ec_rpc} command (see below).
621
622\item[\index{ec_post_event}ec_post_event {\it event_name}]\ \\
623        Post an event to the {\eclipse} engine. This will lead to the
624        execution of the corresponding event handler once the {\eclipse}
625        execution is resumed. See also \bipref{event/1}{../bips/kernel/event/event-1.html} and the User Manual
626        chapter on event handling for more information.
627        This mechanism is mainly recommended for asynchronous posting
628        of events, e.g.\ from within signal handlers or to abort execution.
629        Otherwise it is more convenient to raise an event by writing into
630        an event-raising queue stream (see section \ref{secqevent}).
631
632\item[\index{ec_handle_events}ec_handle_events]\ \\
633        resume execution of the {\eclipse} engine for the purpose of
634	event handling only. All events that have been posted via
635	ec_post_event or raised by writing into event-raising queues
636	will be handled (in an unspecified order).
637	The return value will always be "success", except when an
638	asynchronous {\eclipse} thread is still running, in which case
639	the return value is "running" and it is undefined whether the
640	events may have been handled by that thread or not.
641
642first create an {\eclipse} queue stream using {\eclipse}'s
643\bipref{open/3}{../bips/kernel/iostream/open-3.html}
644or
645\bipref{open/4}{../bips/kernel/iostream/open-4.html}
646predicate, then connect that stream to a Tcl channel by invoking
647the {\bf ec_queue_connect} command from within Tcl code.
648
649\item[\index{ec_queue_connect (Tcl embedding interface)}ec_queue_connect {\it eclipse_stream_name mode ?command?}]\ \\
650        Creates a Tcl channel and connects it to the given {\eclipse}
651        stream ({\it eclipse_stream_name} can be a symbolic name or the
652        {\eclipse} stream number).
653        The {\it mode} argument is either r or w, indicating a read or write channel.
654        The procedure returns a channel identifier for use in commands
655        like {\bf puts}, {\bf read}, {\bf ec_read_exdr},
656        {\bf ec_write_exdr} or {\bf close}.
657        The channel identifier is of the form {\tt ec_queueX}, where {\tt X}
658        is the {\eclipse} stream number of the queue.
659        This identifier can either be stored in a variable or reconstructed
660        using the Tcl expression
661        \begin{verbatim}
662        ec_queue[ec_stream_nr eclipse_stream_name]
663        \end{verbatim}
664	If a {\it command} argument is provided, this command is set as the
665	handler to be called when data needs to be flushed or read from
666	the channel (see {\bf ec_set_queue_handler}).
667
668\item[\index{ec_set_queue_handler (Tcl embedding interface)}ec_set_queue_handler {\it eclipse_stream_name mode command}]\ \\
669	Sets {\it command} as the Tcl-handler to be called when the
670	specified queue needs to be serviced from the Tcl side.
671	Unlike {\bf ec_queue_connect}, this command does not create
672	a Tcl channel.
673	The {\it mode} argument is either r or w, indicating whether
674	the Tcl end of the queue is readable or writable.
675	For readable queues, the handler is invoked when the
676	{\eclipse} side flushes the queue.  The Tcl-handler is
677	expected to read and empty the queue.
678	For writable queues, the handler is invoked when the
679	{\eclipse} side reads from the empty queue. The Tcl-handler is
680	expected to write data into the queue.
681	In any case, the handler {\it command} will be invoked with
682	the {\eclipse} stream number appended as an extra argument.
683
684\end{description}
685
686%HEVEA\cutend
687