| 1 |
#!/usr/bin/env perl -w |
| 2 |
# |
| 3 |
# POD documentation after __END__ |
| 4 |
|
| 5 |
# This program is contributed by Achim Bohnet. It demonstrates how to |
| 6 |
# use the callback functions in the GNU Readline Library. This script |
| 7 |
# is essentially equivalent with executing the following lines in |
| 8 |
# `eg/perlsh'; |
| 9 |
# $PerlSh::term->tkRunning(1); |
| 10 |
# use Tk; |
| 11 |
# $mw = MainWindow->new(); |
| 12 |
# |
| 13 |
# Hiroo Hayashi |
| 14 |
|
| 15 |
require 5.003_92; |
| 16 |
|
| 17 |
use Tk; |
| 18 |
|
| 19 |
# Bug: Require script does not work with all possibilities of |
| 20 |
# missing/existing new MainWindow and MainLoop. Therefore |
| 21 |
# I have disabled it. |
| 22 |
# Mainloop in script would be the end. No readline :-( |
| 23 |
#require shift @ARGV if (@ARGV); |
| 24 |
|
| 25 |
|
| 26 |
package Tk::RL; |
| 27 |
use Tk; |
| 28 |
use Term::ReadLine; |
| 29 |
|
| 30 |
$name = 'ptksh+'; |
| 31 |
|
| 32 |
$mw = MainWindow->new() unless ($mw = Tk::Exists 'MainWindow'); |
| 33 |
$mw->title($name); |
| 34 |
$mw->iconname($name); |
| 35 |
$mw->protocol('WM_DELETE_WINDOW' => \&quit); |
| 36 |
|
| 37 |
|
| 38 |
##### Gnu Readline Stuff ##### |
| 39 |
my $term = new Term::ReadLine $name; |
| 40 |
my $attribs = $term->Attribs; |
| 41 |
|
| 42 |
$term->callback_handler_install("$name> ", \&doline); |
| 43 |
|
| 44 |
$mw->fileevent(STDIN,'readable', |
| 45 |
$attribs->{callback_read_char}); |
| 46 |
|
| 47 |
sub quit { |
| 48 |
$mw->fileevent(STDIN,'readable',''); |
| 49 |
$term->callback_handler_remove(); |
| 50 |
$mw->destroy; |
| 51 |
} |
| 52 |
|
| 53 |
my $outstream = $attribs->{outstream}; |
| 54 |
sub doline { |
| 55 |
my $line = shift; |
| 56 |
|
| 57 |
if (defined $line) { |
| 58 |
if ($line =~ /^p\s(.*)$/) { |
| 59 |
$line = "print $1, \"!\\n\";"; |
| 60 |
} |
| 61 |
|
| 62 |
eval "{package main; $line }"; |
| 63 |
print $outstream "$@\n" if $@; |
| 64 |
$term->add_history($line) if $line ne ""; |
| 65 |
$attribs->{line_buffer} = ''; # needed for eval errors |
| 66 |
} else { |
| 67 |
quit() unless defined $line; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
# To test if Tk is not blocked: Tk::RL::tk_active<return> |
| 72 |
sub tk_active { |
| 73 |
print STDERR "I'm working behing the scene\n"; |
| 74 |
$mw->after(1500,\&tk_active); |
| 75 |
} |
| 76 |
#$mw->after(1500,\&tk_active); |
| 77 |
|
| 78 |
|
| 79 |
package main; |
| 80 |
|
| 81 |
# be gentle if 'required' script defined $mw; |
| 82 |
$mw = $Tk::RL::mw if not defined $mw; |
| 83 |
|
| 84 |
MainLoop; |
| 85 |
print "\n"; |
| 86 |
|
| 87 |
__END__ |
| 88 |
|
| 89 |
=head1 NAME |
| 90 |
|
| 91 |
ptksh+ - Simple perl/Tk shell that uses the Gnu Readline features |
| 92 |
|
| 93 |
=head1 SYNOPSIS |
| 94 |
|
| 95 |
% ptksh+ |
| 96 |
ptksh+> $b=$mw->Button(-text=>'hello',-command=>sub{print STDERR 'hello'}) |
| 97 |
ptksh+> $b->pack; |
| 98 |
ptksh+> ... |
| 99 |
ptksh+> ^D |
| 100 |
% |
| 101 |
|
| 102 |
=head1 DESCRIPTION |
| 103 |
|
| 104 |
This (very) simple perl/Tk shell allows you to enter perl/Tk commands |
| 105 |
interactively. |
| 106 |
Additionally it supports command line editing and keeps a history |
| 107 |
of previously entered commands. It requires C<Term-Readline-Gnu> |
| 108 |
to be installed. |
| 109 |
|
| 110 |
You can exit ptksh+ with ^D or using your Window Manager 'Close' |
| 111 |
item. |
| 112 |
|
| 113 |
=head1 SEE ALSO |
| 114 |
|
| 115 |
Term::Readline, Term::Readline::Gnu, Tk, perldebug |
| 116 |
|
| 117 |
=head1 AUTHOR |
| 118 |
|
| 119 |
Achim Bohnet <F<[email protected]>>, URL:L<http://www.xray.mpe.mpg.de/~ach/> |
| 120 |
|
| 121 |
Copyright (c) 1996-1997 Achim Bohnet. All rights reserved. This program |
| 122 |
is free software; you can redistribute it and/or modify it under the same |
| 123 |
terms as Perl itself. |
| 124 |
|
| 125 |
=cut |