File: tkpod

package info (click to toggle)
libtk-pod-perl 0.9939-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny, squeeze
  • size: 332 kB
  • ctags: 254
  • sloc: perl: 4,416; makefile: 43
file content (440 lines) | stat: -rwxr-xr-x 11,263 bytes parent folder | download
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#!/usr/bin/perl -w

use strict;
use vars qw($VERSION $tk_opt $tree $server $portfile $Mblib @I $debug);

$VERSION = sprintf("%d.%02d", q$Revision: 5.7 $ =~ /(\d+)\.(\d+)/);

use IO::Socket;

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: $!";
	}
    }
}

use Tk;

# Experimental mousewheel support. This is part of newer Tk versions.
# XXX <MouseWheel> support for Windows is untested.
BEGIN {
    if ($Tk::VERSION < 800.024012) {
	local $^W = 0;
	require Tk::Listbox;
	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")]);
	};

	require Tk::ROText;
	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")]);
	};

	require Tk::HList;
	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")]);
	};
    }
}

### Problems under Windows... do not use it anymore
#BEGIN { eval { require Tk::FcyEntry; }; };
use Tk::Pod 4.18;
use Tk::Pod::Text; # for findpod
use Getopt::Long;
#require Tk::ErrorDialog;

my $mw = MainWindow->new();
my $orig_state = $mw->state; # may be iconic
$mw->withdraw;

my $function;
my $question;

$tree = 0;
#XXX Getopt::Long::Configure ("bundling");
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
}

# Add 'Tk' subdirectories to search path so, e.g.,
# 'Scrolled' will find doc in 'Tk/Scrolled'
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'}) {
    if (eval { require Tk::App::Reloader; 1 }) {
	warn "Loaded Tk::App::Reloader ...\n";
	$Tk::App::Reloader::VERBOSE = $Tk::App::Reloader::VERBOSE = 1;
	Tk::App::Reloader::shortcut();
	$use_reloader = 1;
    }
    if (eval { require Tk::App::Debug; 1 }) {
	warn "Loaded Tk::App::Debug...\n";
    }
}

start_server() if $server;

# CDE use Font Settings if available
my $ufont = $mw->optionGet('userFont','UserFont');     # fixed width
my $sfont = $mw->optionGet('systemFont','SystemFont'); # proportional
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") { # XXX still decide
    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) {
    # XXX better to use Tk::Pod->Dir? blib/scripts => Tk::Pod->ScriptDir?
    require blib;
    blib->import;
}
if (@I) {
    push @extra_dirs, @I;
}
Tk::Pod->Dir(@extra_dirs) if @extra_dirs;
if ($ENV{TKPODDIRS}) {
    require Config;
    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);
    # -file => ... should be called after creating the Pod window,
    # because -title => ... is set implicitly by Pod's new
    $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) # This may happen if all arguments are directories
 {
  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;
}

# xxx dirty but it works. A simple $mw->destroy if $mw->children
# does not work because Tk::ErrorDialogs could be created.
# (they are withdrawn after Ok instead of destory'ed I guess)

if ($mw->children) {
    $mw->repeat(1000, sub {
        if (Tk::Exists($mw)) {
	    # ErrorDialog is withdrawn not deleted :-(
	    foreach ($mw->children) {
		return if "$_" =~ /^Tk::Pod/  # ->isa('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;
}

__END__

=head1 NAME

tkpod - Perl/Tk Pod browser

=head1 SYNOPSIS

    tkpod   [-tk] [[-no]tree] [-Mblib] [-I dir] [-d|debug] [-s|server]
	    [-filedialog module]
            [-f function | -q FAQRegex | directory | name [...]]


=head1 DESCRIPTION

B<tkpod> is a simple Pod browser with hypertext capabilities.
Pod (L<Plain Old Document|perlpod>) is a simple and readable
markup language that could be mixed with L<perl> code.

Pods are searched by default in C<@INC> and C<$ENV{PATH}>. Directories
listed on the command line or with the B<-I> option are added to the
default search path.

For each C<name> listed on the command line B<tkpod> tries
to find Pod in C<name>, C<name.pod> and C<name.pm> in the search
path.  For each C<name> a new Pod browser window is opened.

If no C<name> is listed, then the main C<perl> pod is opened instead.

=head1 OPTIONS

=over 4

=item B<-tree>

When specified, C<tkpod> will show a tree window with all available
Pods on the local host. However, this may be slow on startup,
especially first time because there is no cache yet. You can always
turn on the tree view with the menu entry 'View' -E<gt> 'Pod Tree'.

=item B<-tk>

Useful for perl/Tk documentation.  When specified it adds all
C<Tk> subdirectories in C<@INC> to the Pod search path.   This way
when C<Scrolled> is selected in the browser the C<Tk/Scrolled>
documentation is found.

=item B<-s> or B<-server>

Start C<tkpod> in server mode. Subsequent calls to C<tkpod> (without
the B<-s> option) will cause to load the requested Pods into the
server program, thus minimizing startup time and memory usage. Note
that there is no access control, so this might be a security hole!

=item B<-d> or B<-debug>

Turn debugging on.

=item B<-Mblib>

Add the C<blib> directories under the current directory to the Pod
search path.

=item B<-I> I<dir>

Add another directory to the Pod search path. Note that the space is
mandatory.

=item B<-f> I<function>

Show documentation for I<function>.

=item B<-q> I<FAQRegex>

Show the FAQ entry matching I<FAQRegex>.

=item B<-filedialog> I<module>

Use an alternative file dialog module, e.g. L<Tk::FileSelect>,
L<Tk::FBox> or L<Tk::PathEntry::Dialog>.

=back


=head1 USAGE

How to navigate with the Pod browser is described in L<Tk::Pod_usage>.
It's also accessible via the menu 'Help' -> 'Usage...'.

=head1 ENVIRONMENT

=over

=item TKPODPORT

Force a port for tkpod's server mode.

=item TKPODDIRS

A list of directories (on Unix usually separated by C<:>, on Windows
by C<;>) for additional Pod directories. These directories will appear
in the "local dirs" section of the tree view.

=back

See L<Tk::Pod::Text/Environment> and L<Tk::Pod::FindPods/Environment>
for more environment variables.

=head1 KNOWN BUGS

see L<Tk::Pod::Text>

=head1 SEE ALSO

L<perlpod|perlpod>
L<pod2man|pod2man>
L<pod2text|pod2text>
L<pod2html|pod2html>
L<Tk::Pod|Tk::Pod>
L<Tk::Pod::Text|Tk::Pod::Text>
L<Tk::Pod::Tree|Tk::Pod::Tree>

=head1 AUTHOR

Nick Ing-Simmons <F<nick@ni-s.u-net.com>>

Former maintainer: Achim Bohnet <F<ach@mpe.mpg.de>>.

Code currently maintained by Slaven Rezic <F<slaven@rezic.de>>.

Copyright (c) 1997-1998 Nick Ing-Simmons.  All rights reserved.  This program
is free software; you can redistribute it and/or modify it under the same
terms as Perl itself.

=cut