#!/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 implements multiple workspaces in ratpoison by making calls to fdump, freestore. It was adapted from rpws which comes with ratpoison in the contrib directory. =head1 USAGE Add the following line in ~/.ratpoisonrc exec /path/to/rpws init 6 -k This creates 6 aliases rpws1, rpws2, etc. It also binds the keys C-1, C-2, etc to each rpwsN alias. Workspace names can be configured at the top of this script. The toggle command is analogous to the ratpoison "other" command for windows, except it is for groups and is implemented in this script instead of within ratpoison. The default workspace (number 0) can be toggled back and forth using the toggle_default command. This does not affect the memory of the toggle command. The toggle_default command is useful to keep a single window for notes etc that can always be easily accessed without changing anything else. =head1 FILES rpws requires use of a lockfile. It defaults to using /tmp/rpws..lock but this can be changed by setting the environment variable RPWS_LOCKFILE to your desired lockfile. =head1 AUTHOR Mike O'Connor =head1 COPYRIGHT Copyright (c) 2005 Mike O'Connor All rights reserved. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.