#!/usr/bin/perl -wT
require
5.002;
sub
get_eval_status;
sub
prompt;
$ENV
{HOME} =
'/home/bug'
;
my
$MW
= MainWindow->new;
$MW
->minsize(1, 1);
$MW
->ErrorDialog->configure(
'-cleanupcode'
=> \
&prompt
);
my
$app
=
"local"
;
my
$lastCommand
=
""
;
my
$menu
=
$MW
->Frame(
-relief
=>
'raised'
,
-bd
=> 2);
my
$menu_file
=
$menu
->Menubutton(
-text
=>
"File"
,
-underline
=> 0);
my
$SELECT_APPLICATION
=
'Select Application'
;
$menu_file
->cascade(
-label
=>
$SELECT_APPLICATION
,
-underline
=> 0);
$menu_file
->command(
-label
=>
'Quit'
,
-command
=> \
&exit
,
-underline
=> 0);
my
$menu_file_m
=
$menu_file
->cget(-menu);
my
$menu_file_m_apps
=
$menu_file_m
->Menu;
$menu_file_m
->entryconfigure(
$SELECT_APPLICATION
,
-menu
=>
$menu_file_m_apps
);
$menu_file_m
->configure(
-postcommand
=> \
&fillAppsMenu
);
$menu
->
pack
(
-side
=>
'top'
,
-fill
=>
'x'
);
$menu_file
->
pack
(
-side
=>
'left'
);
my
$t
=
$MW
->Text(
-relief
=>
"raised"
,
-borderwidth
=> 2,
-setgrid
=> 1);
my
$s
=
$MW
->Scrollbar(
-relief
=>
"flat"
,
-command
=> [
'yview'
,
$t
]);
$t
->configure(
-yscrollcommand
=> [
'set'
,
$s
]);
$s
->
pack
(
-side
=>
'right'
,
-fill
=>
'both'
);
$t
->
pack
(
-side
=>
'left'
);
$SIG
{
'__WARN__'
} = \
&get_eval_status
;
$t
->bindtags([
$t
,
'Tk::Text'
,
$MW
,
'all'
]);
$t
->
bind
(
'<Return>'
=>
sub
{
my
$t
=
shift
;
$t
->mark(
'set'
,
'insert'
,
'end - 1c'
);
$t
->insert(
'insert'
,
"\n"
);
&invoke
();
$t
->break;
});
$t
->
bind
(
'<Delete>'
=>
sub
{
my
$t
=
shift
;
if
(
defined
$t
->tag(
'nextrange'
,
'sel'
,
'1.0'
,
'end'
)) {
$t
->tag(
'remove'
,
'sel'
,
'sel.first'
,
'promptEnd'
);
}
else
{
$t
->break
if
$t
->compare(
'insert'
,
'<'
,
'promptEnd'
);
}
});
$t
->
bind
(
'<BackSpace>'
=>
sub
{
my
$t
=
shift
;
if
(
defined
$t
->tag(
'nextrange'
,
'sel'
,
'1.0'
,
'end'
)) {
$t
->tag(
'remove'
,
'sel'
,
'sel.first'
,
'promptEnd'
);
}
else
{
$t
->break
if
$t
->compare(
'insert'
,
'<'
,
'promptEnd'
);
}
});
$t
->
bind
(
'<Control-d>'
=>
sub
{
my
$t
=
shift
;
$t
->break
if
$t
->compare(
'insert'
,
'<'
,
'promptEnd'
);
});
$t
->
bind
(
'<Control-k>'
=>
sub
{
my
$t
=
shift
;
$t
->mark(
'set'
,
'insert'
,
'promptEnd'
)
if
$t
->compare(
'insert'
,
'<'
,
'promptEnd'
);
});
$t
->
bind
(
'<Control-t>'
=>
sub
{
my
$t
=
shift
;
$t
->break
if
$t
->compare(
'insert'
,
'<'
,
'promptEnd'
);
});
$t
->
bind
(
'<Meta-d>'
=>
sub
{
my
$t
=
shift
;
$t
->break
if
$t
->compare(
'insert'
,
'<'
,
'promptEnd'
);
});
$t
->
bind
(
'<Meta-BackSpace>'
=>
sub
{
my
$t
=
shift
;
$t
->break
if
$t
->compare(
'insert'
,
'<'
,
'promptEnd'
);
});
$t
->
bind
(
'<Control-h>'
=>
sub
{
my
$t
=
shift
;
$t
->break
if
$t
->compare(
'insert'
,
'<'
,
'promptEnd'
);
});
$t
->
bind
(
'<Control-x>'
=>
sub
{
my
$t
=
shift
;
$t
->tag(
'remove'
,
'sel'
,
'sel.first'
,
'promptEnd'
);
});
$t
->tag(
'configure'
,
'bold'
,
-font
=>
"*-Courier-Bold-R-Normal-*-120-*-*-*-*-*-*"
,
);
$app
=
$MW
->name;
$MW
->title(
"Tk Remote Controller - $app"
);
$MW
->iconname(
$app
);
prompt;
$t
->focus();
MainLoop;
sub
prompt {
$t
->insert(
'insert'
,
"$app: "
);
$t
->mark(
'set'
,
'promptEnd'
,
'insert'
);
$t
->mark(
'gravity'
,
'promptEnd'
,
'left'
);
$t
->tag(
'add'
,
'bold'
,
'promptEnd linestart'
,
'promptEnd'
);
}
sub
invoke {
my
$cmd
=
$t
->get(
'promptEnd'
,
'insert'
);
my
$result
=
''
;
if
(
$cmd
eq
"!!\n"
) {
$cmd
=
$lastCommand
;
}
else
{
$lastCommand
=
$cmd
;
}
if
(
$app
eq
"local"
) {
eval
$cmd
; get_eval_status;
}
else
{
$t
->
send
(
$app
,
$cmd
);
}
prompt;
$t
->mark(
'set'
,
'promptEnd'
,
'insert'
);
$t
->yview(
-pickplace
=>
'insert'
);
}
sub
newApp {
my
$appName
=
shift
;
$app
=
$appName
;
$t
->mark(
'gravity'
,
'promptEnd'
,
'right'
);
$t
->
delete
(
"promptEnd linestart"
,
"promptEnd"
);
$t
->insert(
"promptEnd"
,
"$appName: "
);
$t
->tag(
"add"
,
"bold"
,
"promptEnd linestart"
,
"promptEnd"
);
$t
->mark(
'gravity'
,
'promptEnd'
,
'left'
);
return
''
;
}
sub
fillAppsMenu {
my
$i
;
eval
{
$menu_file_m_apps
->
delete
(0,
'last'
)};
foreach
$i
(
sort
$MW
->interps) {
$menu_file_m_apps
->add(
"command"
,
-label
=>
$i
,
-command
=> [
sub
{
&newApp
(
$_
[0]);},
$i
],
);
}
$menu_file_m_apps
->add(
"command"
,
-label
=>
"local"
,
-command
=>
sub
{
&newApp
(
"local"
); },
);
}
sub
get_eval_status {
chomp
(
$EVAL_ERROR
,
@_
);
my
$errors
=
join
''
,
$EVAL_ERROR
,
@_
;
$t
->insert(
'insert'
,
"$errors\n"
)
if
$errors
;
$EVAL_ERROR
=
''
;
}
sub
Tk::Receive {
my
(
$window
,
$cmd
) =
@_
;
chop
$cmd
;
$cmd
=~ /(.*)/;
$cmd
= $1;
eval
$cmd
; get_eval_status;
}