-# Save window layout to an arbitrary file and load layouts upon demand\r
-# Useful for being able to temporarily reorder your windows and then reverting to your "normal" layout\r
-# Also useful as an easy way to reorder your windows\r
-#\r
-# A special thanks to billnye, Zed` and Bazerka for their help\r
-#\r
-# Usage:\r
-# /layout_save filename\r
-# Saves the layout to the textfile "filename.layout"\r
-# /layout_load filename\r
-# Loads the layout from the textfile "filename.layout"\r
-# /set layout_savepath path\r
-# Use to set a default path for layouts\r
-#\r
-# TODO:\r
-# Check the layout file for a number used twice\r
-#\r
-\r
-use strict;\r
-use Irssi;\r
-use Data::Dumper;\r
-use vars qw($VERSION %IRSSI);\r
-\r
-%IRSSI = (\r
- authors => "Isaac G",\r
- contact => "irssi\@isaac.otherinbox.com",\r
- name => "reorder",\r
- description => "Reordering windows based on a textfile.",\r
- license => "GPL",\r
-);\r
-\r
-sub doFilenameFixing\r
-{\r
- my ($filename) = @_;\r
- unless ($filename)\r
- {\r
- print "No filename specified!";\r
- return;\r
- }\r
-\r
- $filename = glob($filename);\r
-\r
- if ($filename =~ /\//)\r
- {\r
- unless ($filename =~ /^\//)\r
- {\r
- print "I don't like /'s in filenames. Unless you want to specify an absolute path.";\r
- return;\r
- }\r
- # Let it go?\r
- }\r
-\r
- $filename .= '.layout' unless ($filename =~ /.layout$/);\r
-\r
- my $path = Irssi::settings_get_str('layout_savepath');\r
- $path .= '/' unless ($path =~ /\/$/);\r
- $filename = $path . $filename unless ($filename =~ /\//);\r
-\r
- return $filename;\r
-}\r
-\r
-sub canReadFile\r
-{\r
- my ($filename) = @_;\r
- unless (-f $filename)\r
- {\r
- print "No such file $filename";\r
- return;\r
- }\r
- unless (-r $filename)\r
- {\r
- print "Can not read file $filename";\r
- return;\r
- }\r
- return 1;\r
-}\r
-\r
-# Save a list of refnum and window info to file\r
-sub cmd_layout_save\r
-{\r
- my ($filename, $data, $more) = @_;\r
- my $FH;\r
-\r
- $filename = doFilenameFixing($filename);\r
- return unless ($filename);\r
-\r
- unless(open $FH, ">", $filename)\r
- {\r
- print "Can not open $filename";\r
- return;\r
- }\r
-\r
- # Order by ref. Print ref and an id tag\r
- for my $win (sort {$a->{'refnum'} <=> $b->{'refnum'}} Irssi::windows())\r
- {\r
- my $id = $win->{'name'} ? $win->{'name'} : $win->{'active'}->{'name'} . ":" . $win->{'active'}->{'server'}->{'tag'};\r
- printf $FH "%d\t%s\n", $win->{'refnum'}, $id;\r
- }\r
- close $FH;\r
- print "Layout saved to $filename";\r
-}\r
-\r
-# Load a list and use it to reorder\r
-sub cmd_layout_load\r
-{\r
- # Check filename supplied, exists and readable\r
- my ($filename, $data, $more) = @_;\r
- $filename = doFilenameFixing($filename);\r
- return unless ($filename);\r
-\r
- return unless canReadFile($filename);\r
-\r
- my @layout;\r
- my ($ref, $id, $FH);\r
-\r
- # Pull the refnum and id\r
- unless(open $FH, "<", $filename)\r
- {\r
- print "Can not open file $filename.";\r
- return;\r
- }\r
- while (my $line = <$FH>)\r
- {\r
- chomp $line;\r
- my ($ref, $id) = split(/\t/, $line, 2);\r
- next unless ($ref and $id);\r
-\r
- push @layout, {refnum => $ref, id => $id};\r
- }\r
- close $FH;\r
-\r
- # For each layout item from the file, find the window and set it's ref to that number\r
- for my $position (sort {$a->{'refnum'} <=> $b->{'refnum'}} @layout)\r
- {\r
- for my $win (Irssi::windows())\r
- {\r
- $id = $win->{'name'} ? $win->{'name'} : $win->{'active'}->{'name'} . ":" . $win->{'active'}->{'server'}->{'tag'};\r
- if ($id eq $position->{'id'})\r
- {\r
- $win->set_refnum($position->{'refnum'});\r
- last;\r
- }\r
- }\r
- }\r
-}\r
-\r
-Irssi::settings_add_str('misc', 'layout_savepath', Irssi::get_irssi_dir());\r
-\r
-Irssi::command_bind( 'layout_save', 'cmd_layout_save' );\r
-Irssi::command_bind( 'layout_load', 'cmd_layout_load' );
\ No newline at end of file
+# Save window layout to an arbitrary file and load layouts upon demand
+# Useful for being able to temporarily reorder your windows and then reverting to your "normal" layout
+# Also useful as an easy way to reorder your windows
+#
+# A special thanks to billnye, Zed` and Bazerka for their help
+#
+# Usage:
+# /layout_save filename
+# Saves the layout to the textfile "filename.layout"
+# /layout_load filename
+# Loads the layout from the textfile "filename.layout"
+#
+# TODO:
+# Check the layout file for a number used twice
+# On script load, run a layout_load
+# On channel join, run load: channel joined
+#
+
+use strict;
+use Irssi;
+use Data::Dumper;
+use vars qw($VERSION %IRSSI);
+use POSIX 'strftime';
+
+%IRSSI = (
+ authors => "Isaac Good",
+ contact => "irssi\@isaacgood.com",
+ name => "reorder",
+ description => "Reordering windows based on a textfile.",
+ license => "GPL",
+);
+$VERSION = '1.0';
+
+# Map user input to a valid filename
+sub GetFilename
+{
+ my ($filename) = @_;
+
+ # On no input, use a default filename.
+ unless (length($filename))
+ {
+ my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
+ $filename = POSIX::strftime("%y%m%d", $sec, $min, $hour, $mday, $mon, $year);
+ # If you prefer not having datestamped filenames, uncomment:
+ # $filename = "default";
+ }
+
+ # Use glob expansion to match things like ~/
+ my $glob = glob($filename);
+ $filename = $glob if $glob;
+
+ # Only handle directories when using an absolute path.
+ if ($filename =~ /\// and $filename !~ /^\//)
+ {
+ print "I don't like /'s in filenames. Unless you want to specify an absolute path.";
+ return;
+ }
+
+ # Add a file extension
+ $filename .= '.layout' unless ($filename =~ /.layout$/);
+
+ # Use get_irssi_dir() unless using an absolute path
+ if ($filename !~ /\//) {
+ my $path = Irssi::get_irssi_dir();
+ $path .= '/' unless ($path =~ /\/$/);
+ $filename = $path . $filename;
+ }
+
+ return $filename;
+}
+
+# Check a filename exists and can be read.
+sub CanReadFile
+{
+ my ($filename) = @_;
+ unless (-f $filename)
+ {
+ print "No such file $filename";
+ return 0;
+ }
+ unless (-r $filename)
+ {
+ print "Can not read file $filename";
+ return 0;
+ }
+ return 1;
+}
+
+# Save the current layout to file
+sub CmdLayoutSave
+{
+ my ($filename, $data, $more) = @_;
+ my $FH;
+
+ $filename = GetFilename($filename);
+ return unless ($filename);
+
+ unless(open $FH, ">", $filename)
+ {
+ print "Can not open $filename";
+ return;
+ }
+
+ # Order by ref. Print ref and an id tag
+ for my $win (sort {$a->{'refnum'} <=> $b->{'refnum'}} Irssi::windows())
+ {
+ my $id = $win->{'name'} ? $win->{'name'} : $win->{'active'}->{'name'};
+ my $tag = $win->{'active'}->{'server'}->{'tag'};
+ printf $FH "%d\t%s:%s\n", $win->{'refnum'}, $id, $tag;
+ }
+ close $FH;
+ print "Layout saved to $filename";
+}
+
+# Load a list and use it to reorder
+sub CmdLayoutLoad
+{
+ my ($filename, $data, $more) = @_;
+ $filename = GetFilename($filename);
+
+ return unless ($filename);
+ return unless CanReadFile($filename);
+
+ my @layout;
+ my ($ref, $id, $tag, $FH);
+
+ # Pull the refnum and id
+ unless(open $FH, "<", $filename)
+ {
+ print "Can not open file $filename.";
+ return;
+ }
+ while (my $line = <$FH>)
+ {
+ chomp $line;
+ my ($ref, $id) = split(/\t/, $line, 2);
+ next unless ($ref and $id);
+
+ push @layout, {refnum => $ref, id => $id};
+ }
+ close $FH;
+
+ # For each layout item from the file, find the window and set it's ref to that number
+ for my $position (sort {$a->{'refnum'} <=> $b->{'refnum'}} @layout)
+ {
+ for my $win (Irssi::windows())
+ {
+ $id = $win->{'name'} ? $win->{'name'} : $win->{'active'}->{'name'};
+ $tag = $win->{'active'}->{'server'}->{'tag'};
+ $id .= ":" . $tag;
+ if ($id eq $position->{'id'})
+ {
+ $win->set_refnum($position->{'refnum'});
+ last;
+ }
+ }
+ }
+}
+
+Irssi::command_bind( 'layout_save', 'CmdLayoutSave' );
+Irssi::command_bind( 'layout_load', 'CmdLayoutLoad' );