2 # the next line restarts using wish \
6 # This script implements a simple remote-control mechanism for
7 # Tk applications. It allows you to select an application and
8 # then type commands to that application.
10 # RCS: @(#) $Id: rmt,v 1.3 2001/10/29 16:23:32 dkf Exp $
12 wm title .
"Tk Remote Controller"
13 wm iconname .
"Tk Remote"
16 # The global variable below keeps track of the remote application
17 # that we're sending to. If it's an empty string then we execute
18 # the commands locally.
22 # The global variable below keeps track of whether we're in the
23 # middle of executing a command entered via the text.
27 # The global variable below keeps track of the last command executed,
28 # so it can be re-executed in response to !! commands.
32 # Create menu bar. Arrange to recreate all the information in the
33 # applications sub-menu whenever it is cascaded to.
35 . configure
-menu [menu .menu
]
37 menu .menu.
file.apps
-postcommand fillAppsMenu
38 .menu add cascade
-label "File" -underline 0 -menu .menu.
file
39 .menu.
file add cascade
-label "Select Application" -underline 0 \
41 .menu.
file add
command -label "Quit" -command "destroy ." -underline 0
43 # Create text window and scrollbar.
45 text .t
-relief sunken
-bd 2 -yscrollcommand ".s set" -setgrid true
46 scrollbar .s
-command ".t yview"
47 grid .t .s
-sticky nsew
48 grid rowconfigure .
0 -weight 1
49 grid columnconfigure .
0 -weight 1
51 # Create a binding to forward commands to the target application,
52 # plus modify many of the built-in bindings so that only information
53 # in the current command can be deleted (can still set the cursor
54 # earlier in the text and select and insert; just can't delete).
56 bindtags .t
{.t Text . all
}
58 .t mark
set insert
{end
- 1c
}
64 catch
{.t tag remove sel sel.first promptEnd
}
65 if {[.t tag nextrange sel
1.0 end
] == ""} {
66 if [.t compare insert
< promptEnd
] {
72 catch
{.t tag remove sel sel.first promptEnd
}
73 if {[.t tag nextrange sel
1.0 end
] == ""} {
74 if [.t compare insert
<= promptEnd
] {
80 if [.t compare insert
< promptEnd
] {
85 if [.t compare insert
< promptEnd
] {
86 .t mark
set insert promptEnd
90 if [.t compare insert
< promptEnd
] {
95 if [.t compare insert
< promptEnd
] {
99 bind .t
<Meta-BackSpace
> {
100 if [.t compare insert
<= promptEnd
] {
104 bind .t
<Control-h
> {
105 if [.t compare insert
<= promptEnd
] {
109 auto_load tkTextInsert
110 proc tkTextInsert
{w s
} {
115 if {[$w compare sel.first
<= insert
]
116 && [$w compare sel.last
>= insert
]} {
117 $w tag remove sel sel.first promptEnd
118 $w delete sel.first sel.last
125 .t configure
-font {Courier
12}
126 .t tag configure bold
-font {Courier
12 bold
}
128 # The procedure below is used to print out a prompt at the
129 # insertion point (which should be at the beginning of a line
134 .t insert insert
"$app: "
135 .t mark
set promptEnd
{insert
}
136 .t mark gravity promptEnd left
137 .t tag add bold
{promptEnd linestart
} promptEnd
140 # The procedure below executes a command (it takes everything on the
141 # current line after the prompt and either sends it to the remote
142 # application or executes it locally, depending on "app".
145 global app executing lastCommand
146 set cmd
[.t get promptEnd insert
]
148 if [info complete
$cmd] {
149 if {$cmd == "!!\n"} {
154 if {$app == "local"} {
155 set result
[catch
[list uplevel
#0 $cmd] msg]
157 set result
[catch
[list send
$app $cmd] msg
]
160 .t insert insert
"Error: $msg\n"
163 .t insert insert
$msg\n
167 .t mark
set promptEnd insert
170 .t yview
-pickplace insert
173 # The following procedure is invoked to change the application that
174 # we're talking to. It also updates the prompt for the current
175 # command, unless we're in the middle of executing a command from
176 # the text item (in which case a new prompt is about to be output
177 # so there's no need to change the old one).
179 proc newApp appName
{
183 .t mark gravity promptEnd right
184 .t delete
"promptEnd linestart" promptEnd
185 .t insert promptEnd
"$appName: "
186 .t tag add bold
"promptEnd linestart" promptEnd
187 .t mark gravity promptEnd left
192 # The procedure below will fill in the applications sub-menu with a list
193 # of all the applications that currently exist.
195 proc fillAppsMenu
{} {
196 set m .menu.
file.apps
197 catch
{$m delete
0 last
}
198 foreach i
[lsort
[winfo interps
]] {
199 $m add
command -label $i -command [list newApp
$i]
201 $m add
command -label local -command {newApp
local}
204 set app
[winfo name .
]