Debian Codegraph @

contrib/rpws

raw
1 #!/usr/bin/env perl
2
3 #
4 # Copyright (c) 2009 Hatem Nassrat <[email protected]>
5 # All rights reserved. Modifications under same license as original script.
6 # ChangeLog:
7 #
8 # Added C-M-Right and C-M-Left for next and prev workspace
9 # Added move window to next prev workspace
10 # Added dump/restore workspaces
11 #
12 # Copyright (c) 2005 Mike O'Connor
13 # All rights reserved.
14 # Author Mike O'Connor <[email protected]>
15 #
16 # Modified by Shawn Betts.
17 #
18 # Code was adapted from rpws that comes from ratpoison containing the
19 # following copyright:
20 # Copyright (C) 2003 Shawn Betts
21 # Author: Shawn Betts
22 #
23
24 use strict;
25 use warnings;
26 use Fcntl qw (:flock);
27 use Getopt::Std;
28
29 my $ratpoison = $ENV{ "RATPOISON" } || "ratpoison";
30 my $tmp=$ENV{ "TMP" } || "/tmp";
31 my $lockfile = $ENV{ "RPWS_LOCKFILE" } || "$tmp/rpws.$<.lock";
32
33 sub help
34 {
35 system("pod2usage", $0);
36 print( "for more detailed documentation run \"perldoc $0\"\n" );
37 }
38
39 sub rp_call
40 {
41 my $result = `$ratpoison -c "@_"`;
42 chomp( $result );
43 chomp( $result );
44 return $result;
45 }
46
47 sub ws_init_ws
48 {
49
50 my $num = shift;
51
52 rp_call( "gnew wspl$num" );
53 my $fd = fdump();
54 rp_call( "setenv fspl$num $fd" );
55 }
56
57 sub fdump
58 {
59 return rp_call( "fdump" );
60 }
61
62 sub ws_init
63 {
64 my $num = shift;
65
66 $num >= 2 || die "You must create at least 2 workspaces.";
67
68 # Backup the frames
69 my $fd = fdump();
70
71 rp_call( "select -" );
72 rp_call( "only" );
73
74 my $i;
75 for my $i (2..$num)
76 {
77 ws_init_ws( $i );
78 }
79
80 # Workspace 1 uses the 'default' group.
81 # Start in workspace 1.
82 $fd = fdump();
83 rp_call( "gselect default" );
84 rp_call( "setenv fspl1 $fd" );
85 rp_call( "setenv wspl 1" );
86
87 # Keep track of workspace count
88 rp_call( "setenv wspc $num" );
89
90 # restore the frames
91 rp_call( "frestore $fd" );
92
93 if( -e "$lockfile" )
94 {
95 unlink ("$lockfile" );
96 }
97 }
98
99 sub ws_save
100 {
101 my $ws = rp_call( "getenv wspl" );
102 my $fd = fdump();
103 rp_call( "setenv fspl$ws $fd" );
104 }
105
106 sub ws_restore
107 {
108 my $which = shift;
109 my $which_name;
110 my $move;
111
112 # Step1: determine which workspace to move to
113
114 if ( $which =~ /^move/ )
115 {
116 $move = 1;
117 $which = substr($which, 4);
118 }
119
120 if ( $which =~ /^(?:next|prev)$/ )
121 {
122 my $ws = rp_call( "getenv wspl" );
123 my $wspc = rp_call( "getenv wspc" );
124
125 if ( $which eq 'next' )
126 {
127 $ws++;
128 }
129 else
130 {
131 $ws--;
132 }
133 $which = ( ( $ws - 1 ) % $wspc ) + 1;
134 }
135
136 if ( $which == 1 )
137 {
138 $which_name = "default";
139 }
140 else
141 {
142 $which_name = "wspl$which";
143 }
144
145 # Step 2a: Moving Window if necessary
146 if ( $move )
147 {
148 rp_call( "gmove $which_name" );
149 rp_call( "select -" );
150 rp_call( "prev" );
151 }
152
153 # Step2: Process the workspace switch
154 ws_save();
155 rp_call( "gselect $which_name");
156 my $last = rp_call( "getenv fspl$which" );
157 rp_call( "frestore $last" );
158 rp_call( "setenv wspl $which" );
159
160 # Step 2x: Display the moved Window
161 if ( $move )
162 {
163 rp_call( "next" );
164 }
165
166 rp_call( "echo Workspace $which" );
167
168 }
169
170 sub ws_dump
171 {
172 my $ws_dumpf = shift;
173 my $wspc = rp_call( "getenv wspc" );
174
175 open WSDUMP, ">$ws_dumpf" or die $!;
176
177 ws_save();
178
179 for my $i (1..$wspc)
180 {
181 print WSDUMP rp_call( "getenv fspl$i" )."\n";
182 }
183
184 close WSDUMP;
185
186 rp_call( "echo Workspaces dumped to $ws_dumpf" );
187 }
188
189 sub ws_dumprestore
190 {
191 my $ws_dumpf = shift;
192 my $ws = rp_call( "getenv wspl" );
193 my $wspc = rp_call( "getenv wspc" );
194
195 # Get dumped frame info
196 open WSDUMP, "$ws_dumpf" or die $!;
197 my @wsdata=<WSDUMP>;
198 close WSDUMP;
199
200 my $wspc_dump = scalar @wsdata;
201
202 if ($wspc < $wspc_dump)
203 {
204 # Number of workspaces to restore more than available
205 die "Cannot restore all workspaces (dump_count = " . $wspc_dump . ", rp_count = " . $wspc . ")";
206 }
207
208 # Restoring all workspaces
209 for my $i (1..$wspc_dump)
210 {
211 # calculating previous workspace number
212 my $j = ( $wspc + $i - 2 ) % $wspc + 1;
213 # moving to workspace $i -1
214 ws_restore( $j );
215 # restoring workspace $i
216 rp_call( "setenv fspl$i $wsdata[$i-1]" );
217 }
218
219 # goto the originally visible workspace
220 ws_restore( $ws );
221
222 rp_call( "echo Workspaces loaded from $ws_dumpf" );
223 }
224
225 sub add_aliases
226 {
227 my $n = shift;
228 foreach my $i (1..$n) {
229 # Switch Workspace
230 rp_call ( "alias rpws$i exec $0 $i" );
231 # Move Window to wrokspace
232 rp_call ( "alias rpwsm$i exec $0 move$i" );
233 }
234 rp_call ( "alias rpwsn exec $0 next" );
235 rp_call ( "alias rpwsp exec $0 prev" );
236 rp_call ( "alias rpwsmn exec $0 movenext" );
237 rp_call ( "alias rpwsmp exec $0 moveprev" );
238
239
240 }
241
242 sub add_keys
243 {
244 my $n = shift;
245 foreach my $i (1..$n) {
246 # Switch Workspace
247 rp_call ( "definekey top M-F$i rpws$i" );
248 # Move Window to wrokspace
249 rp_call ( "definekey top C-M-S-F$i rpwsm$i" );
250 }
251 rp_call ( "definekey top C-M-Right rpwsn" );
252 rp_call ( "definekey top C-M-Left rpwsp" );
253 rp_call ( "definekey top C-M-greater rpwsmn" );
254 rp_call ( "definekey top C-M-less rpwsmp" );
255 }
256
257 my $arg = shift @ARGV || 'help';
258
259 if( $arg eq "help" ) {
260 help();
261 } elsif( $arg eq "init" ) {
262 my $num = shift @ARGV;
263 my %opts;
264 ws_init( $num );
265 getopts('ka', \%opts);
266 add_aliases( $num ) if $opts{'a'} || $opts{'k'};
267 add_keys ( $num ) if $opts{'k'};
268 } elsif( $arg eq "dump" ) {
269 my $filename = shift @ARGV;
270 ws_dump($filename);
271 } elsif( $arg eq "restore" ) {
272 my $filename = shift @ARGV;
273 ws_dumprestore($filename);
274 } elsif ( $arg eq "current" ) {
275 my $cws = rp_call( "getenv wspl" );
276 print "$cws";
277 } else {
278 open LOCK, ">>$lockfile" or die "Cannot open lockfile: $lockfile";
279 flock(LOCK, LOCK_EX);
280 ws_restore( $arg );
281 }
282
283 __END__
284
285 =head1 NAME
286
287 rpws - Implements multiple workspaces in ratpoison
288
289 =head1 SYNOPSIS
290
291 rpws init n [-k] [-a] - setup rpws with n workspaces.
292 -a sets up command aliases;
293 -k sets up key bindings and aliases.
294 rpws dump <fname> - dumps the current layout to <fname>
295 rpws restore <fname> - restores rpws workspaces from <fname>
296 rpws current - print the current workspace
297 rpws help - this documentation
298 rpws n - switch to this workspace
299
300
301 =head1 DESCRIPTION
302
303 B<rpws> implements multiple workspaces in ratpoison by making calls
304 to fdump, freestore. It was adapted from rpws which comes with
305 ratpoison in the contrib directory.
306
307 =head1 USAGE
308
309 Add the following line in ~/.ratpoisonrc
310
311 exec /path/to/rpws init 6 -k
312
313 This creates 6 aliases rpws1, rpws2, etc. It also binds the keys M-F1,
314 M-F2, etc to each rpwsN alias. Moreover, rpwsn (Next) and rpwsp (Prev) are
315 created, and C-M-{Right,Left} are bound to rpws{n,p}. Aliases rpwsm1, rpwsm2,
316 etc. and rpwsm{n,p} are used to move the focused window to another workspace.
317
318 Full list of keybindings created are:
319
320 M-F$i Goto workspace $i
321 C-M-Right Goto Next workspace
322 C-M-Left Goto Prev workspace
323 C-M-S-F$i Move window to workspace $i
324 C-M-greater Move current window to next workspace
325 C-M-less Move current window to prev workspace
326
327 =head1 FILES
328
329 rpws requires use of a lockfile. It defaults to using
330 /tmp/rpws.<UID>.lock but this can be changed by setting the
331 environment variable RPWS_LOCKFILE to your desired lockfile.
332
333 =head1 AUTHOR
334
335 Mike O'Connor <[email protected]>
336
337 =head1 COPYRIGHT
338
339 Copyright (c) 2005 Mike O'Connor
340 All rights reserved.
341
342 This program is free software; you can redistribute it and/or
343 modify it under the terms of the GNU General Public License
344 as published by the Free Software Foundation; either version 2
345 of the License, or (at your option) any later version.
346
347 This program is distributed in the hope that it will be useful,
348 but WITHOUT ANY WARRANTY; without even the implied warranty of
349 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
350 GNU General Public License for more details.