1use strict;
2use vars qw($VERSION %IRSSI);
3use Irssi;
4use Irssi::Irc;
5
6# Usage:
7# /script load go.pl
8# If you are in #irssi you can type /go #irssi or /go irssi or even /go ir ...
9# also try /go ir<tab> and /go  <tab> (that's two spaces)
10#
11# The following settings exist:
12#
13#   /SET go_match_case_sensitive [ON|OFF]
14#     Match window/item names sensitively (the default). Turning this off
15#     means e.g. "/go foo" would jump to a window named "Foobar", too.
16#
17#   /SET go_match_anchored [ON|OFF]
18#     Match window/names only at the start of the word (the default). Turning
19#     this off will mean that strings can match anywhere in the window/names.
20#     The leading '#' of channel names is optional either way.
21#
22#   /SET go_complete_case_sensitive [ON|OFF]
23#     When using tab-completion, match case-insensitively (the default).
24#     Turning this on means that "/go foo<tab>" will *not* suggest "Foobar".
25#
26#   /SET go_complete_anchored [ON|OFF]
27#     Match window/names only at the start of the word. The default is 'off',
28#     which causes completion to match anywhere in the window/names during
29#     completion. The leading '#' of channel names is optional either way.
30#
31
32$VERSION = '1.1';
33
34%IRSSI = (
35    authors     => 'nohar',
36    contact     => 'nohar@freenode',
37    name        => 'go to window',
38    description => 'Implements /go command that activates a window given a name/partial name. It features a nice completion.',
39    license     => 'GPLv2 or later',
40    changed     => '2017-02-02'
41);
42
43sub _make_regexp {
44	my ($name, $ci, $aw) = @_;
45	my $re = "\Q${name}\E";
46	$re = "(?i:$re)" unless $ci;
47	$re = "^#?$re" if $aw;
48	return $re;
49}
50
51sub signal_complete_go {
52	my ($complist, $window, $word, $linestart, $want_space) = @_;
53	my $channel = $window->get_active_name();
54	my $k = Irssi::parse_special('$k');
55
56        return unless ($linestart =~ /^\Q${k}\Ego\b/i);
57
58	my $re = _make_regexp($word,
59		Irssi::settings_get_bool('go_complete_case_sensitive'),
60		Irssi::settings_get_bool('go_complete_anchored'));
61	@$complist = ();
62	foreach my $w (Irssi::windows) {
63		my $name = $w->get_active_name();
64		if ($word ne "") {
65			if ($name =~ $re) {
66				push(@$complist, $name)
67			}
68		} else {
69			push(@$complist, $name);
70		}
71	}
72	Irssi::signal_stop();
73};
74
75sub cmd_go
76{
77	my($chan,$server,$witem) = @_;
78
79	$chan =~ s/ *//g;
80	my $re = _make_regexp($chan,
81		Irssi::settings_get_bool('go_match_case_sensitive'),
82		Irssi::settings_get_bool('go_match_anchored'));
83
84	foreach my $w (Irssi::windows) {
85		my $name = $w->get_active_name();
86		if ($name =~ $re) {
87			$w->set_active();
88			return;
89		}
90	}
91}
92
93Irssi::command_bind("go", "cmd_go");
94Irssi::signal_add_first('complete word', 'signal_complete_go');
95Irssi::settings_add_bool('go', 'go_match_case_sensitive', 1);
96Irssi::settings_add_bool('go', 'go_complete_case_sensitive', 0);
97Irssi::settings_add_bool('go', 'go_match_anchored', 1);
98Irssi::settings_add_bool('go', 'go_complete_anchored', 0);
99
100# Changelog
101#
102# 2017-02-02  1.1  martin f. krafft <madduck@madduck.net>
103#   - made case-sensitivity of match configurable
104#   - made anchoring of search strings configurable
105#
106