From 288da6a76b7d55684f93db3a2e49b54462f32513 Mon Sep 17 00:00:00 2001 From: Simon Ruderich Date: Tue, 28 Sep 2010 02:28:30 +0200 Subject: vim_mode: Implement :b# and :b window-name. --- vim-mode/vim_mode.pl | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'vim-mode') diff --git a/vim-mode/vim_mode.pl b/vim-mode/vim_mode.pl index 26f950b..46ae7cc 100644 --- a/vim-mode/vim_mode.pl +++ b/vim-mode/vim_mode.pl @@ -686,14 +686,53 @@ sub cmd_ex_command { } elsif ($arg_str =~ m|b(?:uffer)?\s*(.+)$|) { my $window; + my $item; my $buffer = $1; + # Go to window number. if ($buffer =~ /^[0-9]+$/) { $window = Irssi::window_find_refnum($buffer); + # Go to previous window. + } elsif ($buffer eq '#') { + Irssi::command('window last'); + # Go to best regex matching window. + } else { + my $regex = qr/\Q$buffer\E/; + + my @matches; + foreach my $window (Irssi::windows()) { + # Matching window names. + if ($window->{name} =~ /$regex/) { + my $ratio = ($+[0] - $-[0]) / length($window->{name}); + push @matches, { window => $window, + item => undef, + ratio => $ratio }; + } + # Matching Window item names (= channels). + foreach my $item ($window->items()) { + if ($item->{name} =~ /$regex/) { + my $length = length($item->{name}); + $length-- if index($item->{name}, '#') == 0; + push @matches, { window => $window, + item => $item, + ratio => ($+[0] - $-[0]) / $length }; + } + } + } + + if (scalar @matches > 0) { + @matches = sort {$b->{ratio} <=> $a->{ratio}} @matches; + + $window = $matches[0]->{window}; + $item = $matches[0]->{item}; + } } if ($window) { $window->set_active(); + if ($item) { + $item->set_active(); + } } } } -- cgit v1.2.3