#!/usr/bin/perl -w # # Copyright (c) 2005 Mike O'Connor # All rights reserved. # Author Mike O'Connor
# # Modified by Shawn Betts. # Modified by Ivan Beschastnikh. # # code was adapeted from rpws that comes from ratpoison containing the following copyright: # Copyright (C) 2003 Shawn Betts # Author: Shawn Betts # use strict; use Fcntl qw (:flock); use Getopt::Std; my $ratpoison = $ENV{ "RATPOISON" } || "ratpoison"; my $tmp=$ENV{ "TMP" } || "/tmp"; my $lockfile = $ENV{ "RPWS_LOCKFILE" } || "$tmp/rpws.$<.lock"; sub help { system("pod2usage", $0); print( "for more detailed documentation run \"perldoc $0\"\n" ); } sub rp_call { my $result = `$ratpoison -c "@_"`; chomp( $result ); chomp( $result ); return $result; } sub ws_init_ws { my $num = shift; rp_call( "gnew wspl$num" ); my $fd = fdump(); rp_call( "setenv fspl$num $fd" ) } sub fdump { return rp_call( "fdump" ); } sub ws_init { my $num = shift; # Backup the frames my $fd = fdump(); rp_call( "select -" ); rp_call( "only" ); my $i; for( my $i = 0; $i < $num; $i++ ) { ws_init_ws( $i ); } # Save number of workspaces rp_call( "setenv wsmax $num" ); # Workspace 1 uses the 'default' group. # Start in workspace 1. $fd = fdump(); rp_call( "gselect default" ); rp_call( "setenv fspl1 $fd" ); rp_call( "setenv wspl 1" ); # restore the frames rp_call( "frestore $fd" ); if( -e "$lockfile" ) { unlink ("$lockfile" ); } } sub ws_save { my $ws = rp_call( "getenv wspl" ); my $fd = fdump(); rp_call( "setenv fspl$ws $fd" ); } sub ws_restore { my $which = shift; ws_save(); if( $which == 1 ) { rp_call( "gselect default" ); } else { rp_call( "gselect wspl$which"); } rp_call( "echo Workspace $which" ); my $last = rp_call( "getenv fspl$which" ); rp_call( "frestore $last" ); rp_call( "setenv wspl $which" ); } sub ws_prev () { my $num = int(rp_call( "getenv wsmax" )); my $current = int(rp_call( "getenv wspl" )); my $new; ws_save(); if ( $current == 1 ) { $new=$num; rp_call ("gselect $new"); } elsif ( $current == 2 ) { $new=1; rp_call("gselect default"); } else { $new=$current-1; rp_call("gselect $new"); } rp_call( "echo Workspace $new" ); my $newfs = rp_call( "getenv fspl$new" ); rp_call( "frestore $newfs" ); rp_call( "setenv wspl $new" ); } sub ws_next () { ws_save(); my $current = int(rp_call( "getenv wspl" )); my $num = int(rp_call( "getenv wsmax" )); my $new; if ( $current == $num ) { $new=1; rp_call ( "gselect default" ); } else { $new=$current+1; rp_call ( "gselect $new" ); } rp_call( "echo Workspace $new" ); my $newfs = rp_call( "getenv fspl$new" ); rp_call ( "frestore $newfs" ); rp_call( "setenv wspl $new" ); } sub add_aliases { my $n = shift; foreach my $i (1..$n) { rp_call ( "alias rpws$i exec $0 $i" ); } } sub add_keys { my $n = shift; foreach my $i (1..$n) { rp_call ( "definekey top M-F$i rpws$i" ); } } my $arg = shift @ARGV || 'help'; if( $arg eq "help" ) { help(); } elsif( $arg eq "init" ) { my $num = shift @ARGV; my %opts; ws_init( $num ); getopts('ka', \%opts); add_aliases( $num ) if $opts{'a'} || $opts{'k'}; add_keys ( $num ) if $opts{'k'}; } elsif( $arg eq "-p" ) { ws_prev(); } elsif( $arg eq "-n" ) { ws_next(); } else { open LOCK, ">>$lockfile" or die "Cannot open lockfile: $lockfile"; flock(LOCK, LOCK_EX); ws_restore( $arg ); } __END__ =head1 NAME rpws - Implements multiple workspaces in ratpoison =head1 SYNOPSIS rpws init n [-k] [-a] - setup rpws with n workspaces. -a sets up command aliases; -k sets up key bindings and aliases. rpws help - this documentation rpws -p - switch to previous workspace rpws -n - switch to next workspace rpws n - switch to this workspace =head1 DESCRIPTION B