#!/usr/bin/perl -w # # Copyright (c) 2005 Mike O'Connor # All rights reserved. # Author Mike O'Connor
# # Modified by Shawn Betts. # # code was adapeted from rpws that comes from ratpoison containing the follwing 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"; my @ws_names; $ws_names[0] = "default"; # don't change this one for( my $i = 1; $i <= 9; $i++ ) { $ws_names[$i] = "rpws$i"; } # user-customized workspace names: $ws_names[1] = "firefox"; $ws_names[2] = "email"; $ws_names[3] = "music"; $ws_names[4] = "admin"; $ws_names[5] = "school"; $ws_names[6] = "projects"; 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; if ($num > 0) { rp_call( "gnew $ws_names[$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 ); } # Start in workspace 0. $fd = fdump(); rp_call( "gselect default" ); rp_call( "setenv fspl0 $fd" ); rp_call( "setenv wspl 0" ); rp_call( "setenv wspl_prev 1" ); rp_call( "setenv default_partner 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" ); rp_call( "setenv wspl_prev $ws" ); } sub ws_restore { my $which = shift; my $cur_ws = rp_call( "getenv wspl" ); if ($which == $cur_ws) { rp_call( "echo Already on workspace $which" ); } else { ws_save(); rp_call( "gselect $ws_names[$which]"); rp_call( "echo Workspace: $ws_names[$which]" ); my $last = rp_call( "getenv fspl$which" ); rp_call( "frestore $last" ); rp_call( "setenv wspl $which" ); } } sub ws_toggle { my $cur_ws = rp_call( "getenv wspl" ); if ($cur_ws == 0) { default_ws_toggle(); } else { my $old_ws = rp_call( "getenv wspl_prev" ); ws_restore($old_ws); } } sub default_ws_restore { my $which = shift; my $cur_ws = rp_call( "getenv wspl" ); rp_call( "gselect $ws_names[$which]"); rp_call( "echo Workspace: $ws_names[$which]" ); my $last = rp_call( "getenv fspl$which" ); rp_call( "frestore $last" ); rp_call( "setenv wspl $which" ); } sub default_ws_toggle { my $cur_ws = rp_call( "getenv wspl" ); if ($cur_ws == 0) { # toggle from the default group my $old_ws = rp_call( "getenv default_partner" ); default_ws_restore($old_ws); } else { # toggle to the default group default_ws_restore(0); rp_call( "setenv default_partner $cur_ws" ); } } sub add_aliases { my $n = shift; foreach my $i (1..$n) { rp_call ( "alias rpws$i exec $0 $i" ); } rp_call ( "alias rpws_toggle exec $0 toggle" ); rp_call ( "alias rpws_toggle_default exec $0 toggle_default" ); } sub add_keys { my $n = shift; foreach my $i (1..$n) { rp_call ( "bind C-$i rpws$i" ); } rp_call ( "bind C-g rpws_toggle" ); rp_call ( "bind e rpws_toggle_default" ); rp_call ( "bind C-e rpws_toggle_default" ); } 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'}; } else { open LOCK, ">>$lockfile" or die "Cannot open lockfile: $lockfile"; flock(LOCK, LOCK_EX); if ( $arg eq "toggle" ) { ws_toggle(); } elsif ( $arg eq "toggle_default" ) { default_ws_toggle(); } elsif ( $arg eq "current" ) { my $cws = rp_call( "getenv wspl" ); print "$cws"; } else { 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 n - switch to this workspace rpws toggle - switch to previous workspace (other than default) rpws toggle_default - switch back and forth to between default workspace rpws current - print the index of the current workspace =head1 DESCRIPTION B