Phoenix.pm: xselect

Kevin Buettner kev at primenet.com
Wed Oct 20 21:49:14 CDT 1999


In response to a question on one of my company's internal mailing
lists, I've written a small perl program that I call xselect.  The
person in question claims to be supremely lazy and wanted a program
which reads from a pipe (stdin) and causes this data to be selected
(in X windows) so that he'd be able to paste it into another
application.  He deals with large diffs and didn't want to be bothered
with scrolling around and making selections in an editor window.  (He
did say that he was supremely lazy.)

After thinking about it for a while, I decided that I too might find
such a utility useful.  So I wrote one in perl.  Below is the bulk of
my reply to the individual wanting this utility.  (I too am lazy and
didn't want to substantially rewrite my message for a different
audience.)

........

It wasn't hard to throw together a small perl script to do what you
want.  The only catch is that you'll need to install Tk (for perl) on
your machine if it's not installed already.

The script is below.  I called it xselect, but of course you can
name it whatever you want.

To invoke it, you can use it either at the end of a pipe, or you can
specify one or more files which'll become the selection.  E.g, either

        diff -u old new | xselect

or

        xselect /tmp/diffs /tmp/otherdiffs

It'll pop up a small window with a "Quit" button in it.  I've arranged
things so that it'll go away when you click on the quit button or when
another selection (anywhere) is made.

If you really, truly want CLIPBOARD selections, you should uncomment
the appropriate line in the script below.  If you also run xclipboard,
you'll probably get the behavior that you really want.  xselect will
run and own the selection.  xclipboard will notice this, fetch the
selection from xselect, and then attempt to own the CLIPBOARD selection
again at which point xselect will exit.

If you don't use xclipboard, you'll need to leave xselect running
for as long as you wish to paste the selection it holds.  (I use PRIMARY
selections more often than CLIPBOARD selections which is why I wrote it
the way that I did.)

Hope you find this useful.

Kevin

--- xselect ---
#!/usr/local/bin/perl -w

use Tk;

$SELECTION = 'PRIMARY';
#$SELECTION = 'CLIPBOARD';

my $top;                # top level widget

undef $/;               # slurp mode
while (<>) {
    $data .= $_;        # slurp all of the data
}                       #   (while loop needed for multiple files)

$top = MainWindow->new;
$top->SelectionHandle(-selection => $SELECTION, \&selection_handler);
$top->SelectionOwn( -command => sub { exit(0); }, -selection => $SELECTION );
$top->Button(-text => "Quit", -command => sub { exit(0) })->pack();

MainLoop;

sub selection_handler {
    my ($offset, $length) = @_;
    return substr($data, $offset, $length);
}
--- end xselect ---

-- 
Kevin Buettner
kev at primenet.com, kevinb at cygnus.com



More information about the Phoenix-pm mailing list