1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
<Html>
<Head>
<Title>Tcl Evaluator-In-A-Page</Title>
<!-- Author: bwelch -->
</Head>
<Body bgcolor=white text=black>
<h1>Tcl Evaluator-in-a-Page</h1>
<h3>[<a HREF="http://www.sun.com/">Sun Home</a> | <a HREF="index.html">Tcl
Plugin</a> | <a HREF="applets.html">Demos</a>]</h3>
<p>
<hr>
<p>
Below is a little evaluator for Tcl commands. Type any valid Tcl command
in and see the result immediately. Check out our quick tour of the
<a HREF="syntax.html">Tcl syntax</a>. For example, to create a new
button, type the following:
<pre>
button .b -text hello -background red
pack .b
</pre> When you're done with the button, type:
<pre>
destroy .b
</pre> and it's gone. You may also want to use the <code>puts</code>
command to output results from within loops. For example:
<pre>
foreach proc [info procs] {
puts "$proc [info args $proc]"
}
</pre>
<br>
<embed src=eval.tcl width=400 height=400 JUNK=7 junk=8>
<p>
<p>
To learn more about Tcl, read either <a href=http://www.smli.com/people/brent.welch/book/index.html>Brent
Welch's</a>or <a href=http://www.aw.com/cp/Oust.html>John Ousterhout's</a>
Tcl books. Many more Tcl and Tk resources are available <a href=http://www.smli.com/research/tcl/>here</a>.
<h2>Source:</h2>
<p>
<hr>
<p>
Here is the source for the evaluator application:
<pre>
# A frame, scrollbar, and text
frame .eval
set _t [text .eval.t -width 40 -height 15 -yscrollcommand {.eval.s set}]
scrollbar .eval.s -command {.eval.t yview}
pack .eval.s -side left -fill y
pack .eval.t -side right -fill both -expand true
pack .eval -fill both -expand true
# Insert the prompt and initialize the limit mark
.eval.t insert insert "Tcl eval log\n"
set prompt "tcl> "
.eval.t insert insert $prompt
.eval.t mark set limit insert
.eval.t mark gravity limit left
focus .eval.t
# Keybindings that limit input and eval things
bind .eval.t <Return> { _Eval .eval.t ; break }
bind .eval.t <Any-Key> {
if [%W compare insert < limit] {
%W mark set insert end
}
}
bindtags .eval.t {.eval.t Text all}
proc _Eval { t } {
global prompt
set command [$t get limit end]
if [info complete $command] {
$t insert insert \n
set err [catch {uplevel #0 $command} result]
if {[string length $result] > 0} {
$t insert insert $result\n
}
$t insert insert $prompt
$t see insert
$t mark set limit insert
return
} else {
$t insert insert \n
}
}
proc puts {args} {
if {[string match -nonewline* $args]} {
set args [lrange $args 1 end]
set nonewline 1
}
.eval.t insert end [lindex $args end] ;# Ignore file specifier
if ![info exists nonewline] {
.eval.t insert end \n
}
}
</pre>
</Body>
</Html>
|