#!/usr/bin/perl -w
use
vars
qw($VERSION $tk_opt $tree $server $portfile $Mblib @I $debug)
;
$VERSION
=
sprintf
(
"%d.%02d"
,
q$Revision: 5.7 $
=~ /(\d+)\.(\d+)/);
sub
INIT {
my
$home
=
$ENV
{
'HOME'
} ||
$ENV
{
'HOMEDRIVE'
}.
$ENV
{
'HOMEPATH'
};
$portfile
=
"$home/.tkpodsn"
;
my
$port
=
$ENV
{
'TKPODPORT'
};
return
if
$^C;
unless
(
defined
$port
) {
if
(
open
(SN,
"$portfile"
)) {
$port
= <SN>;
close
(SN);
}
}
if
(
defined
$port
) {
my
$sock
= IO::Socket::INET->new(
PeerAddr
=>
'localhost'
,
PeerPort
=>
$port
,
Proto
=>
'tcp'
);
if
(
$sock
) {
binmode
(
$sock
);
$sock
->autoflush;
foreach
my
$file
(
@ARGV
) {
unless
(
print
$sock
"$file\n"
) {
die
"Cannot print $file to socket: $!"
;
}
print
"Requested '$file'\n"
;
}
$sock
->
close
||
die
"Cannot close socket: $!"
;
exit
(0);
}
else
{
warn
"Cannot connect to server on $port: $!"
;
}
}
}
BEGIN {
if
(
$Tk::VERSION
< 800.024012) {
local
$^W = 0;
my
$orig_tk_listbox_classinit
= \
&Tk::Listbox::ClassInit
;
*Tk::Listbox::ClassInit
=
sub
{
my
(
$class
,
$mw
)=
@_
;
$orig_tk_listbox_classinit
->(
@_
);
$mw
->
bind
(
$class
,
"<4>"
, [
'yview'
,
'scroll'
, -5,
'units'
]);
$mw
->
bind
(
$class
,
"<5>"
, [
'yview'
,
'scroll'
, +5,
'units'
]);
$mw
->
bind
(
$class
,
'<MouseWheel>'
,
[
sub
{
$_
[0]->yview(
'scroll'
,-(
$_
[1]/120)*3,
'units'
) }, Tk::Ev(
"D"
)]);
};
my
$orig_tk_text_classinit
= \
&Tk::ROText::ClassInit
;
*Tk::ROText::ClassInit
=
sub
{
my
(
$class
,
$mw
)=
@_
;
$orig_tk_text_classinit
->(
@_
);
$mw
->
bind
(
$class
,
"<4>"
, [
'yview'
,
'scroll'
, -5,
'units'
]);
$mw
->
bind
(
$class
,
"<5>"
, [
'yview'
,
'scroll'
, +5,
'units'
]);
$mw
->
bind
(
$class
,
'<MouseWheel>'
,
[
sub
{
$_
[0]->yview(
'scroll'
,-(
$_
[1]/120)*3,
'units'
) }, Tk::Ev(
"D"
)]);
};
my
$orig_tk_hlist_classinit
= \
&Tk::HList::ClassInit
;
*Tk::HList::ClassInit
=
sub
{
my
(
$class
,
$mw
)=
@_
;
$orig_tk_hlist_classinit
->(
@_
);
$mw
->
bind
(
$class
,
"<4>"
, [
'yview'
,
'scroll'
, -5,
'units'
]);
$mw
->
bind
(
$class
,
"<5>"
, [
'yview'
,
'scroll'
, +5,
'units'
]);
$mw
->
bind
(
$class
,
'<MouseWheel>'
,
[
sub
{
$_
[0]->yview(
'scroll'
,-(
$_
[1]/120)*3,
'units'
) }, Tk::Ev(
"D"
)]);
};
}
}
my
$mw
= MainWindow->new();
my
$orig_state
=
$mw
->state;
$mw
->withdraw;
my
$function
;
my
$question
;
$tree
= 0;
if
(!GetOptions(
"tk"
=> \
$tk_opt
,
"tree"
=> \
$tree
,
"notree"
=>
sub
{
$tree
= 0 },
"s|server!"
=> \
$server
,
"Mblib"
=> \
$Mblib
,
"I=s@"
=> \
@I
,
"d|debug!"
=> \
$debug
,
"f=s"
=> \
$function
,
"q=s"
=> \
$question
,
"filedialog=s"
=>
sub
{
my
$mod
=
$_
[1];
eval
qq{ use $mod qw(as_default) }
;
die
$@
if
$@;
},
)) {
die
<<EOT;
Usage: $0 [-tk] [[-no]tree] [-Mblib] [-I dir] [-d|debug] [-s|server]
[-filedialog module]
[-f function | -q FAQRegex | directory | name [...]]
EOT
}
if
(
$tk_opt
) {
my
$tkdir
;
foreach
(
reverse
@INC
) {
$tkdir
=
"$_/Tk"
;
unshift
@ARGV
,
$tkdir
if
-d
$tkdir
;
}
}
if
(
$debug
) {
$ENV
{
'TKPODDEBUG'
} =
$debug
;
}
my
$use_reloader
= 0;
if
(
$ENV
{
'TKPODDEBUG'
}) {
warn
"Loaded Tk::App::Reloader ...\n"
;
$Tk::App::Reloader::VERBOSE
=
$Tk::App::Reloader::VERBOSE
= 1;
Tk::App::Reloader::shortcut();
$use_reloader
= 1;
}
warn
"Loaded Tk::App::Debug...\n"
;
}
}
start_server()
if
$server
;
my
$ufont
=
$mw
->optionGet(
'userFont'
,
'UserFont'
);
my
$sfont
=
$mw
->optionGet(
'systemFont'
,
'SystemFont'
);
if
(
defined
(
$ufont
) and
defined
(
$sfont
)) {
foreach
(
$ufont
,
$sfont
) { s/:$//; };
$mw
->optionAdd(
'*Font'
,
$sfont
);
$mw
->optionAdd(
'*Entry.Font'
,
$ufont
);
$mw
->optionAdd(
'*Text.Font'
,
$ufont
);
}
if
(1 && $^O ne
"MSWin32"
) {
my
$lighter
=
$mw
->Darken(Tk::NORMAL_BG, 110);
foreach
my
$class
(
qw(Entry BrowseEntry.Entry More*ROText Pod*Tree)
) {
$mw
->optionAdd(
"*$class*background"
,
$lighter
,
"userDefault"
);
}
$mw
->optionAdd(
"*Pod*Pod*Frame*More*ROText*background"
,
$lighter
,
"interactive"
);
}
$mw
->optionAdd(
'*Menu.tearOff'
,
$Tk::platform
ne
'MSWin32'
? 1 : 0);
my
@extra_dirs
;
if
(
defined
$Mblib
) {
blib->
import
;
}
if
(
@I
) {
push
@extra_dirs
,
@I
;
}
Tk::Pod->Dir(
@extra_dirs
)
if
@extra_dirs
;
if
(
$ENV
{TKPODDIRS}) {
for
my
$dir
(
split
$Config::Config
{
'path_sep'
},
$ENV
{TKPODDIRS}) {
Tk::Pod->Dir(
$dir
);
}
}
my
$tl
;
my
$file
;
my
$opened
= 0;
foreach
$file
(
@ARGV
)
{
if
(-d
$file
&& !Tk::Pod::Text->findpod(
$file
,
-quiet
=> 1))
{
Tk::Pod->Dir(
$file
);
}
else
{
$tl
=
$mw
->Pod(
-tree
=>
$tree
,
-exitbutton
=> 1);
$tl
->configure(
-file
=>
$file
);
$opened
++;
}
}
if
(
defined
$function
)
{
$tl
=
$mw
->Pod(
-tree
=>
$tree
,
-exitbutton
=> 1);
$tl
->configure(
$tl
->getpodargs(
-f
=>
$function
));
$opened
++;
}
if
(
defined
$question
)
{
$tl
=
$mw
->Pod(
-tree
=>
$tree
,
-exitbutton
=> 1);
$tl
->configure(
$tl
->getpodargs(
-q
=>
$question
));
$opened
++;
}
if
(!
$opened
)
{
if
(
$tree
)
{
$tl
=
$mw
->Pod(
-tree
=> 1,
-exitbutton
=> 1);
}
else
{
$tl
=
$mw
->Pod(
-tree
=>
$tree
,
-exitbutton
=> 1);
$tl
->configure(
-file
=>
"perl"
);
}
}
if
(Tk::Exists(
$tl
) &&
$orig_state
eq
'iconic'
) {
$tl
->iconify;
}
if
(
$mw
->children) {
$mw
->repeat(1000,
sub
{
if
(Tk::Exists(
$mw
)) {
foreach
(
$mw
->children) {
return
if
"$_"
=~ /^Tk::Pod/
}
$mw
->destroy;
}
});
}
else
{
$mw
->destroy;
}
Tk::App::Reloader::check_loop()
if
$use_reloader
;
MainLoop;
unlink
(
$portfile
);
exit
(0);
sub
start_server {
my
$sock
= IO::Socket::INET->new(
Listen
=> 5,
Proto
=>
'tcp'
);
die
"Cannot open listen socket: $!"
unless
defined
$sock
;
binmode
(
$sock
);
my
$port
=
$sock
->sockport;
$ENV
{
'TKPODPORT'
} =
$port
;
open
(SN,
">$portfile"
) ||
die
"Cannot open $portfile: $!"
;
print
SN
$port
;
close
(SN);
print
STDERR
"Accepting connections on $port\n"
;
$mw
->fileevent(
$sock
,
'readable'
,
sub
{
print
STDERR
"accepting $sock\n"
;
my
$client
=
$sock
->
accept
;
if
(
defined
$client
) {
binmode
(
$client
);
print
STDERR
"Connection $client\n"
;
$mw
->fileevent(
$client
,
'readable'
,[\
&PodRequest
,
$client
]);
}
});
$SIG
{TERM} = \
&server_cleanup
;
}
sub
server_cleanup {
unlink
$portfile
if
-e
$portfile
;
}
sub
PodRequest {
my
(
$client
) =
@_
;
local
$_
;
while
(<
$client
>) {
chomp
(
$_
);
print
STDERR
"'$_'\n"
;
my
$pod
=
$mw
->Pod(
-tree
=>
$tree
,
-exitbutton
=> 1);
$pod
->configure(
-file
=>
$_
);
}
warn
"Odd $!"
unless
eof
(
$client
);
$mw
->fileevent(
$client
,
'readable'
,
''
);
print
STDERR
"Close $client\n"
;
$client
->
close
;
}