Debian Codegraph @

ptksh

raw ◀ ▶
1 Debian patch touch this file
1 #!perl -w
2 #
3 # PTKSH 2.0
4 #
5 # A graphical user interface for testing Perl/Tk commands and scripts.
6 #
7 # VERSION HISTORY:
8 # ...truncated earlier stuff...
9 # 4/23/98 V1.7 Achim Bohnet -- some fixes to "o" command
10 # 6/08/98 V2.01 M. Beller -- merge in GUI code for "wish"-like interface
11 #
12 # 2.01d1 6/6/98 First development version
13 #
14 # 2.01d2 6/7/98
15 # - apply A.B. patch for pod and -option
16 # - fix "use of uninitialized variable" in END{ } block (for -c option)
17 # - support h and ? only for help
18 # - misc. pod fixes (PITFALLS)
19 # - use default fonts and default colors ## NOT YET--still working on it
20 # - get rid of Data::Dumper for history
21 #
22 # 2.01d3 6/8/98
23 # - Remove "use Data::Dumper" line
24 # - Put in hack for unix vs. win32 window manager focus problem
25 # - Achim's pod and histfile patch
26 #
27 # 2.01d4 6/18/98
28 # - Slaven's patch to make <Home> work properly
29 # - Add help message to banner (per Steve Lydie)
30 # - Fix horizontal scrolling (turn off wrapping in console window)
31 # - Clarify <Up> in docs and help means "up arrow"
32 # - Use HOMEDRIVE/HOMEPATH on Win32
33 #
34 # For more history look at the various Changes files in the Perl/Tk
35 # distribution.
36
37 =head1 NAME
38
39 ptksh - Perl/Tk script to provide a graphical user interface for testing Perl/Tk
40 commands and scripts.
41
42 =head1 SYNOPSIS
43
44 % ptksh ?scriptfile?
45 ... version information ...
46 ptksh> $b=$mw->Button(-text=>'Hi',-command=>sub{print 'Hi'})
47 ptksh> $b->pack
48 ptksh> o $b
49 ... list of options ...
50 ptksh> help
51 ... help information ...
52 ptksh> exit
53 %
54
55
56 =head1 DESCRIPTION
57
58 ptksh is a perl/Tk shell to enter perl commands
59 interactively. When one starts ptksh a L<MainWindow|Tk::MainWindow>
•60 is automatically created, along with a ptksh command window.
61 One can access the main window by typing commands using the
62 variable $mw at the 'ptksh> ' prompt of the command window.
63
64 ptksh supports command line editing and history. Just type "<Up>" at
65 the command prompt to see a history list. The last 50 commands entered
66 are saved, then reloaded into history list the next time you start ptksh.
67
68 ptksh supports some convenient commands for inspecting Tk widgets. See below.
69
70 To exit ptksh use: C<exit>.
71
72 ptksh is B<*not*> a full symbolic debugger.
73 To debug perl/Tk programs at a low level use the more powerful
74 L<perl debugger|perldebug>. (Just enter ``O tk'' on debuggers
75 command line to start the Tk eventloop.)
76
77 =head1 FEATURES
78
79 =head2 History
80
81 Press <Up> (the Up Arrow) in the perlwish window to obtain a gui-based history list.
82 Press <Enter> on any history line to enter it into the perlwish window.
83 Then hit return. So, for example, repeat last command is <Up><Enter><Enter>.
84 You can quit the history window with <Escape>. NOTE: history is only saved
85 if exit is "graceful" (i.e. by the "exit" command from the console or by
86 quitting all main windows--NOT by interrupt).
87
88 =head2 Debugging Support
89
90 ptksh provides some convenience function to make browsing
91 in perl/Tk widget easier:
92
93 =over 4
94
95 =item B<?>, or B<h>
96
97 displays a short help summary.
98
99 =item B<d>, or B<x> ?I<args>, ...?
100
101 Dumps recursively arguments to stdout. (see L<Data::Dumper>).
102 You must have <Data::Dumper> installed to support this feature.
103
104 B<x> was introduced for perl debugger compatibility.
105
106 =item B<p> ?I<arg>, ...?
107
108 appends "|\n" to each of it's arguments and prints it.
109 If value is B<undef>, '(undef)' is printed to stdout.
110
111 =item B<o> I<$widget> ?I<-option> ...?
112
113 prints the option(s) of I<$widget> one on each line.
114 If no options are given all options of the widget are
115 listed. See L<Tk::options> for more details on the
116 format and contents of the returned list.
117
118 =item B<o> I<$widget> B</>I<regexp>B</>
119
120 Lists options of I<$widget> matching the
121 L<regular expression|perlre> I<regexp>.
122
123 =item B<u> ?I<class>?
124
125 If no argument is given it lists the modules loaded
126 by the commands you executed or since the last time you
127 called C<u>.
128
129 If argument is the empty string lists all modules that are
130 loaded by ptksh.
131
132 If argument is a string, ``text'' it tries to do a ``use Tk::Text;''.
133
134 =back
135
136 =head2 Packages
137
138 Ptksh compiles into package Tk::ptksh. Your code is eval'ed into package
139 main. The coolness of this is that your eval code should not interfere with
140 ptksh itself.
141
142 =head2 Multiline Commands
143
144 ptksh will accept multiline commands. Simply put a "\" character immediately
145 before the newline, and ptksh will continue your command onto the next line.
146
147 =head2 Source File Support
148
149 If you have a perl/Tk script that you want to do debugging on, try running the
150 command
151
152 ptksh> do 'myscript';
153
154 -- or (at shell command prompt) --
155
156 % ptksh myscript
157
158 Then use the perl/Tk commands to try out different operations on your script.
159
160 =head1 ENVIRONMENT
161
162 Looks for your .ptksh_history in the directory specified by
163 the $HOME environment variable ($HOMEPATH on Win32 systems).
164
165 =head1 FILES
166
167 =over 4
168
169 =item F<.ptksh_init>
170
171 If found in current directory it is read in an evaluated
172 after the mainwindow I<$mw> is created. F<.ptksh_init>
173 can contain any valid perl code.
174
175 =item F<~/.ptksh_history>
176
177 Contains the last 50 lines entered in ptksh session(s).
178
179 =back
180
181 =head1 PITFALLS
182
183 It is best not to use "my" in the commands you type into ptksh.
184 For example "my $v" will make $v local just to the command or commands
185 entered until <Return> is pressed.
186 For a related reason, there are no file-scopy "my" variables in the
187 ptksh code itself (else the user might trounce on them by accident).
188
189 =head1 BUGS
190
191 B<Tk::MainLoop> function interactively entered or sourced in a
192 init or script file will block ptksh.
193
194 =head1 SEE ALSO
195
196 L<Tk|Tk>
197 L<perldebug|perldebug>
198
199 =head1 VERSION
200
201 VERSION 2.03
202
203 =head1 AUTHORS
204
205 Mike Beller <[email protected]>,
206 Achim Bohnet <[email protected]>
207
208 Copyright (c) 1996 - 1998 Achim Bohnet and Mike Beller. All rights reserved.
209 This program is free software; you can redistribute it and/or modify it
210 under the same terms as Perl itself.
211
212 =cut
213
214 package Tk::ptksh;
215 require 5.004;
216 use strict;
217 use Tk;
218
219 ##### Constants
220
221 use vars qw($NAME $VERSION $FONT @FONT $WIN32 $HOME $HISTFILE $HISTSAVE $PROMPT $INITFILE);
222
223 $NAME = 'ptksh';
224 $VERSION = '2.03';
225 $WIN32 = 1 if $^O =~ /Win32/;
226 $HOME = $WIN32 ? ($ENV{HOMEDRIVE} . $ENV{HOMEPATH} . '\\') || 'C:\\' : $ENV{HOME} . "/";
227 @FONT = ($WIN32 ? (-font => 'systemfixed') : () );
228 #@FONT = ($WIN32 ? (-font => ['courier', 9, 'normal']) : () );
229 $HISTFILE = "${HOME}.${NAME}_history";
230 $HISTSAVE = 50;
231 $INITFILE = ".${NAME}_init";
232 $PROMPT = "$NAME> ";
233
234 sub Win32Fix { my $p = shift; $p =~ s'\\'/'g; $p =~ s'/$''; return $p }
235
236 use vars qw($mw $st $t @hist $hist $list $isStartOfCommand);
237
238 # NOTE: mainwindow creation order seems to impact who gets focus, and
239 # order is different on Win32 & *nix!! So hack is to create the windows
240 # in an order dependent on the OS!
241
242 $mw = Tk::MainWindow->new unless $WIN32; # &&& hack to work around focus problem
243
244 ##### set up user's main window
245 package main;
246 $main::mw = Tk::MainWindow->new;
247 $main::mw->title('$mw');
248 $main::mw->geometry("+1+1");
249 package Tk::ptksh;
250
251 ##### Set up ptksh windows
252 $mw = Tk::MainWindow->new if $WIN32; # &&& hack to work around focus problem
253 $mw->title($NAME);
254 $st = $mw->Scrolled('Text', -scrollbars => 'osoe',
255 -wrap => 'none',
256 -width => 80, -height => 25, @FONT);
257 $t = $st->Subwidget('scrolled');
258 $st->pack(-fill => 'both', -expand => 'true');
259 # $mw_mapped assures that Center is only called exactly twice: first time
260 # will determine geometry of window, second time centering will work.
261 # I observed a couple of further <Map> events, which are now ignored
262 # and thus window creation seems to be faster now.
263 my $mw_mapped;
264 $mw->bind('<Map>', sub {return if $mw_mapped && $mw_mapped>=2; Center($mw); $mw_mapped++;} );
265
266 # Event bindings
267 $t->bindtags([$t, ref($t), $t->toplevel, 'all']); # take first crack at events
268 $t->bind('<Return>', \&EvalInput);
269 $t->bind('<BackSpace>', \&BackSpace);
270 $t->bind('<Escape>', \&HistKill);
271 $t->bind('<Up>', \&History);
272 $t->bind('<Control-a>', \&BeginLine);
273 $t->bind('<Home>', \&BeginLine);
274 $t->bind('<Any-KeyPress>', [\&Key, Tk::Ev('K'), Tk::Ev('A')]);
275
276 my $default_font = $t->cget(-font);
277 my %default_font = $t->fontActual($default_font);
278 my $normal_font;
279 if (!$t->fontMetrics($default_font, '-fixed')) {
280 $normal_font = $t->fontCreate(%default_font, -family => "courier");
281 $t->configure(-font => $normal_font);
282 } else {
283 $normal_font = $default_font;
284 }
285 my %normal_font = $t->fontActual($normal_font);
286 my $bold_font = $t->fontCreate(%normal_font, -weight => "bold");
287
288 # Set up different colors for the various window outputs
289 #$t->tagConfigure('prompt', -underline => 'true');
290 $t->tagConfigure('prompt', -foreground => 'blue', -font => $bold_font);
291 $t->tagConfigure('result', -foreground => 'purple');
292 $t->tagConfigure('error', -foreground => 'red');
293 $t->tagConfigure('output', -foreground => 'blue');
294
295 # The tag 'limit' is the beginning of the input command line
296 $t->markSet('limit', 'insert');
297 $t->markGravity('limit', 'left');
298
299 # redirect stdout
300 #tie (*STDOUT, 'Tk::Text', $t);
301 tie (*STDOUT, 'Tk::ptksh');
302 #tie (*STDERR, 'Tk::ptksh');
303
304 # Print banner
305 print "$NAME V$VERSION";
306 print " perl V$] Tk V$Tk::VERSION MainWindow -> \$mw\n";
307 print "\n\t\@INC:\n";
308 foreach (@INC) { print "\t $_\n" };
309 print "Type 'h<Return>' at the prompt for help\n";
310
311 ##### Read .ptkshinit
312 if ( -r $INITFILE)
313 {
314 print "Reading $INITFILE ...\n";
315 package main;
316 do $Tk::ptksh::INITFILE;
317 package Tk::ptksh;
318 }
319
320 ###### Source the file if given as argument 0
321 if (defined($ARGV[0]) && -r $ARGV[0])
322 {
323 print "Reading $ARGV[0] ...\n";
324 package main;
325 do $ARGV[0];
326 package Tk::ptksh;
327 }
328
329 ##### Read history
330 @hist = ();
331 if ( -r $HISTFILE and open(HIST, $HISTFILE) ) {
332 print "Reading history ...\n";
333 my $c = "";
334 while (<HIST>) {
335 chomp;
336 $c .= $_;
337 if ($_ !~ /\\$/) { #end of command if no trailing "\"
338 push @hist, $c;
339 $c = "";
340 } else {
341 chop $c; # kill trailing "\"
342 $c .= "\n";
343 }
344 }
345 close HIST;
346 }
347
348 ##### Initial prompt
349 Prompt($PROMPT);
350 $Tk::ptksh::mw->focus;
351 $t->focus;
352 #$mw->after(1000, sub {print STDERR "now\n"; $mw->focus; $t->focus;});
353
354 ##### Now enter main loop
355 #$mw->afterIdle(sub {Center($mw);});
356 MainLoop();
357
358 ####### Callbacks/etc.
359
360 # EvalInput -- Eval the input area (between 'limit' and 'insert')
361 # in package main;
362 use vars qw($command $result); # use globals instead of "my" to avoid conflict w/ 'eval'
363 sub EvalInput {
364 # If return is hit when not inside the command entry range, reprompt
365 if ($t->compare('insert', '<=', 'limit')) {
366 $t->markSet('insert', 'end');
367 Prompt($PROMPT);
368 Tk->break;
369 }
370
371 # Support multi-line commands
372 if ($t->get('insert-1c', 'insert') eq "\\") {
373 $t->insert('insert', "\n");
374 $t->insert('insert', "> ", 'prompt'); # must use this pattern for continue
375 $t->see('insert');
376 Tk->break;
377 }
378
379 # Get the command and strip out continuations
380 $command = $t->get('limit','end');
381 $t->markSet('insert','end');
382 $command =~ s/\\\n>\s/\n/mg;
383
384 # Eval it
385 if ( $command !~ /^\s*$/) {
386 chomp $command;
387 push(@hist, $command)
388 unless @hist && ($command eq $hist[$#hist]); #could elim more redundancy
389
390 $t->insert('insert', "\n");
391
392 $isStartOfCommand = 1;
393
394 $command = PtkshCommand($command);
395
396 exit if ($command eq 'exit');
397
398 package main;
399 no strict;
400 $Tk::ptksh::result = eval "local \$^W=0; $Tk::ptksh::command;";
401 use strict;
402 package Tk::ptksh;
403
404 if ($t->compare('insert', '!=', 'insert linestart')) {
405 $t->insert('insert', "\n");
406 }
407 if ($@) {
408 $t->insert('insert', '## ' . $@, 'error');
409 } else {
410 $result = "" if !defined($result);
411 $t->insert('insert', '# ' . $result, 'result');
412 }
413 }
414
415 Prompt($PROMPT);
416
417 Tk->break;
418 }
419
420 sub Prompt {
421 my $pr = shift;
422
423 if ($t->compare('insert', '!=', 'insert linestart')) {
424 $t->insert('insert', "\n");
425 }
426
427 $t->insert('insert', $pr, 'prompt');
428 $t->see('insert');
429 $t->markSet('limit', 'insert');
430
431 }
432
433 sub BackSpace {
434 if ($t->tagNextrange('sel', '1.0', 'end')) {
435 $t->delete('sel.first', 'sel.last');
436 } elsif ($t->compare('insert', '>', 'limit')) {
437 $t->delete('insert-1c');
438 $t->see('insert');
439 }
440 Tk->break;
441 }
442
443 sub BeginLine {
444 $t->SetCursor('limit');
445 $t->break;
446 }
447
448 sub Key {
449 my ($self, $k, $a) = @_;
450 #print "key event: ", $k, "\n";
451 if ($t->compare('insert', '<', 'limit')) {
452 $t->markSet('insert', 'end');
453 }
454 #$t->break; #for testing bindtags
455 }
456
457 sub History {
458 Tk->break if defined($hist);
459
460 $hist = $mw->Toplevel;
461 $hist->title('History');
462 $list = $hist->ScrlListbox(-scrollbars => 'oe',
463 -width => 30, -height => 10, @FONT)->pack(qw(-fill both -expand 1));
464 Center($hist);
465 $list->insert('end', @hist);
466 $list->see('end');
467 $list->activate('end');
468 $hist->bind('<Double-1>', \&HistPick);
469 $hist->bind('<Return>', \&HistPick);
470 $hist->bind('<Escape>', \&HistKill);
471 my $hist_mapped; # see above for $mw_mapped
472 $hist->bind('<Map>', sub {return if $hist_mapped && $hist_mapped>=2; Center($hist); $hist_mapped++;} );
473 $hist->bind('<Destroy>', \&HistDestroy);
474 $hist->focus;
475 $list->focus;
476 $hist->grab;
477 #$mw->afterIdle(sub {Center($hist);});
478 Tk->break;
479 }
480
481 sub HistPick {
482 my $item = $list->get('active');
483 return if (!$item);
484 $t->markSet('insert', 'end');
485 $t->insert('insert',$item);
486 $t->see('insert');
487 $mw->focus;
488 $t->focus;
489 HistKill();
490 }
491
492 sub HistKill {
493 if ($hist) {
494 $hist->grabRelease;
495 $hist->destroy;
496 }
497 }
498
499 # Called from destroy event mapping
500 sub HistDestroy {
501 if (defined($hist) && (shift == $hist)) {
502 $hist = undef;
503 $mw->focus;
504 $t->focus;
505 }
506 }
507
508 sub LastCommand {
509 if ($t->compare('insert', '==', 'limit')) {
510 $t->insert('insert', $hist[$#hist]);
511 $t->break;
512 }
513 }
514
515 # Center a toplevel on screen or above parent
516 sub Center {
517 my $w = shift;
518 my ($x, $y);
519
520 if ($w->parent) {
521 #print STDERR $w->screenwidth, " ", $w->width, "\n";
522 $x = $w->parent->x + ($w->parent->width - $w->width)/2;
523 $y = $w->parent->y + ($w->parent->height - $w->height)/2;
524 } else {
525 #print STDERR $w->screenwidth, " ", $w->width, "\n";
526 $x = ($w->screenwidth - $w->width)/2;
527 $y = ($w->screenheight - $w->height)/2;
528 }
529 $x = int($x);
530 $y = int($y);
531 my $g = "+$x+$y";
532 #print STDERR "Setting geometry to $g\n";
533 $w->geometry($g);
534 }
535
536 # To deal with "TIE".
537 # We have to make sure the prints don't go into the command entry range.
538
539 sub TIEHANDLE { # just to capture the tied calls
540 my $self = [];
541 return bless $self;
542
543 }
544
545 sub PRINT {
546 my ($bogus) = shift;
547
548 $t->markSet('insert', 'end');
549
550 if ($isStartOfCommand) { # Then no prints have happened in this command yet so...
551 if ($t->compare('insert', '!=', 'insert linestart')) {
552 $t->insert('insert', "\n");
553 }
554 # set flag so we know at least one print happened in this eval
555 $isStartOfCommand = 0;
556 }
557
558 while (@_) {
559 $t->insert('end', shift, 'output');
560 }
561
562 $t->see('insert');
563
564 $t->markSet('limit', 'insert'); # don't interpret print as an input command
565 }
566
567 sub PRINTF
568 {
569 my $w = shift;
570 $w->PRINT(sprintf(shift,@_));
571 }
572
573 ###
574 ### Utility function
575 ###
576
577 sub _o
578 {
579 my $w = shift;
580 my $what = shift;
581
582 $what =~ s/^\s+//;
583 $what =~ s/\s+$//;
584 my (@opt) = split " ", $what;
585
586 print 'o(', join('|', @opt), ")\n";
587 require Tk::Pretty;
588
589 # check for regexp
590 if ($opt[0] =~ s|^/(.*)/$|$1|)
591 {
592 print "options matching /$opt[0]/:\n";
593 foreach ($w->configure())
594 {
595 print Tk::Pretty::Pretty($_),"\n" if $_->[0] =~ /\Q$opt[0]\E/;
596 }
597 return;
598 }
599
600 # list of options (allow as bar words)
601 foreach (@opt)
602 {
603 s/^['"]//;
604 s/,$//;
605 s/['"]$//;
606 s/^([^-])/-$1/;
607 }
608 if (length $what)
609 {
610 foreach (@opt)
611 {
612 print Tk::Pretty::Pretty($w->configure($_)),"\n";
613 }
614 }
615 else
616 {
617 foreach ($w->configure()) { print Tk::Pretty::Pretty($_),"\n" }
618 }
619 }
620
621 sub _p {
622 foreach (@_) { print $_, "|\n"; }
623 }
624
625 use vars qw($u_init %u_last $u_cnt);
626 $u_init = 0;
627 %u_last = ();
628 sub _u {
629 my $module = shift;
630 if (defined($module) and $module ne '') {
631 $module = "Tk/".ucfirst($module).".pm" unless $module =~ /^Tk/;
632 print " --- Loading $module ---\n";
633 require "$module";
634 print $@ if $@;
635 } else {
636 %u_last = () if defined $module;
637 $u_cnt = 0;
638 foreach (sort keys %INC) {
639 next if exists $u_last{$_};
640 $u_cnt++;
641 $u_last{$_} = 1;
642 #next if m,^/, and m,\.ix$,; # Ignore autoloader files
643 #next if m,\.ix$,; # Ignore autoloader files
644
645 if (length($_) < 20 ) {
646 printf "%-20s -> %s\n", $_, $INC{$_};
647 } else {
648 print "$_ -> $INC{$_}\n";
649 }
650 }
651 print STDERR "No modules loaded since last 'u' command (or startup)\n"
652 unless $u_cnt;
653 }
654 }
655
656 sub _d
657 {
658 require Data::Dumper;
659 local $Data::Dumper::Deparse;
660 $Data::Dumper::Deparse = 1;
661 print Data::Dumper::Dumper(@_);
662 }
663
664 sub _h
665 {
666 print <<'EOT';
667
668 ? or h print this message
669 d or x arg,... calls Data::Dumper::Dumper
670 p arg,... print args, each on a line and "|\n"
671 o $w /regexp/ print options of widget matching regexp
672 o $w [opt ...] print (all) options of widget
673 u xxx xxx = string : load Tk::Xxx
674 = '' : list all modules loaded
675 = undef : list modules loaded since last u call
676 (or after ptksh startup)
677
678 Press <Up> (the "up arrow" key) for command history
679 Press <Escape> to leave command history window
680 Type "exit" to quit (saves history)
681 Type \<Return> for continuation of command to following line
682
683 EOT
684 }
685
686
687 # Substitute our special commands into the command line
688 sub PtkshCommand {
689 $_ = shift;
690
691 foreach ($_) {
692 last if s/^\?\s*$/Tk::ptksh::_h /;
693 last if s/^h\s*$/Tk::ptksh::_h /;
694 last if s/^u(\s+|$)/Tk::ptksh::_u /;
695 last if s/^[dx]\s+/Tk::ptksh::_d /;
696 last if s/^u\s+(\S+)/Tk::ptksh::_u('$1')/;
697 last if s/^p\s+(.*)$/Tk::ptksh::_p $1;/;
698 last if s/^o\s+(\S+)\s*?$/Tk::ptksh::_o $1;/;
699 last if s/^o\s+(\S+)\s*,?\s+(.*)?$/Tk::ptksh::_o $1, '$2';/;
700 }
701 %u_last = %INC unless $u_init++;
702
703 # print STDERR "Command is: $_\n";
704
705 $_;
706 }
707
708 ###
709 ### Save History -- use Data::Dumper to preserve multiline commands
710 ###
711
712 END {
713 if ($HISTFILE) { # because this is probably perl -c if $HISTFILE is not set
714 $#hist-- if $hist[-1] =~ /^(q$|x$|\s*exit\b)/; # chop off the exit command
715
716 @hist = @hist[($#hist-$HISTSAVE)..($#hist)] if $#hist > $HISTSAVE;
717
718 if( open HIST, ">$HISTFILE" ) {
719 while ($_ = shift(@hist)) {
720 s/\n/\\\n/mg;
721 print HIST "$_\n";
722 }
723 close HIST;
724 } else {
725 print STDERR "Error: Unable to open history file '$HISTFILE'\n";
726 }
727 }
728 }
729
730 1; # just in case we decide to be "use"'able in the future.