1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
|
=pod
=head1 NAME
ido_switcher.pl
=head1 DESCRIPTION
Search and select windows similar to ido-mode for emacs
=head1 INSTALLATION
This script requires that you have first installed and loaded F<uberprompt.pl>
Uberprompt can be downloaded from:
L<https://github.com/shabble/irssi-scripts/raw/master/prompt_info/uberprompt.pl>
and follow the instructions at the top of that file or its README for installation.
If uberprompt.pl is available, but not loaded, this script will make one
attempt to load it before giving up. This eliminates the need to precisely
arrange the startup order of your scripts.
=head2 SETUP
C</bind ^G /ido_switch_start [options]>
Where C<^G> is a key of your choice.
=head2 USAGE
C<C-g> (or whatever you've set the above bind to), enters IDO window switching mode.
You can then type either a search string, or use one of the additional key-bindings
to change the behaviour of the search. C<C-h> provides online help regarding
the possible interactive options.
=head3 EXTENDED USAGE:
It is possible to pass arguments to the C</ido_switch_start> command, which
correspond to some of the interactively settable parameters listed below.
The following options are available:
=over 4
=item C<-channels>
Search through only channels.
=item C<-queries>
Search through only queries.
=item C<-all>
search both queries and channels (Default).
=item C<-active>
Lmit search to only window items with activity.
=item C<-exact>
Enable exact-substring matching
=item C<-flex>
Enable flex-string matching
=back
I<If neither of C<-exact> or C<-flex> are given, the default is the value of
C</set ido_use_flex>>
=head4 EXAMPLE
=over 2
=item C</bind ^G /ido_switch_start -channels>
=item C</bind ^F /ido_switch_start -queries -active>
=back
B<NOTE:> When entering window switching mode, the contents of your input line will
be saved and cleared, to avoid visual clutter whilst using the switching
interface. It will be restored once you exit the mode using either C<C-g>, C<Esc>,
or C<RET>.
=head3 INTERACTIVE COMMANDS
The following key-bindings are available only once the mode has been
activated:
=over 4
=item C<C-g>
Exit the mode without changing windows.
=item C<Esc>
Exit, as above.
=item C<C-s>
Rotate the list of window candidates forward by one item
=item C<C-r>
Rotate the list of window candidates backward by one item
=item C<C-e>
Toggle 'Active windows only' filter
=item C<C-f>
Switch between 'Flex' and 'Exact' matching.
=item C<C-d>
Select a network or server to filter candidates by
=item C<C-u>
Clear the current search string
=item C<C-q>
Cycle between showing only queries, channels, or all.
=item C<C-SPC>
Filter candidates by current search string, and then reset
the search string
=item C<RET>
Select the current head of the candidate list (the green one)
=item C<SPC>
Select the current head of the list, without exiting the
switching mode. The head is then moved one place to the right,
allowing one to cycle through channels by repeatedly pressing space.
=item C<TAB>
B<[currently in development]> displays all possible completions
at the bottom of the current window.
=item I<All other keys> (C<a-z, A-Z>, etc)
Add that character to the current search string.
=back
=head3 USAGE NOTES
=over 4
=item *
Using C-e (show actives), followed by repeatedly pressing space will cycle
through all your currently active windows.
=item *
If you enter a search string fragment, and realise that more than one candidate
is still presented, rather than delete the whole string and modify it, you
can use C-SPC to 'lock' the current matching candidates, but allow you to
search through those matches alone.
=back
=head1 AUTHORS
Based originally on L<window_switcher.pl|http://scripts.irssi.org/scripts/window_switcher.pl> script Copyright 2007 Wouter Coekaerts
C<E<lt>coekie@irssi.orgE<gt>>.
Primary functionality Copyright 2010-2011 Tom Feist
C<E<lt>shabble+irssi@metavore.orgE<gt>>.
=head1 LICENCE
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.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
=head1 BUGS:
=over 4
=item B<FIXED> Sometimes selecting a channel with the same name on a different
network will take you to the wrong channel.
=back
=head1 TODO
=over 4
=item B<DONE> C-g - cancel
=item B<DONE> C-spc - narrow
=item B<DONE> flex matching (on by default, but optional)
=item TODO server/network narrowing
=item B<DONE> colourised output (via uberprompt)
=item B<DONE> C-r / C-s rotate matches
=item B<DONE> toggle queries/channels
=item B<DONE> remove inputline content, restore it afterwards.
=item TODO tab - display all possibilities in window (clean up afterwards)
how exactly will this work?
=item B<DONE> sort by recent activity/recently used windows (separate commands?)
=item B<TODO> need to be able to switch ordering of active ones (numerical, or most
recently active, priority to PMs/hilights, etc?)
=item B<DONE> should space auto-move forward to next window for easy stepping
through sequential/active windows?
=back
=cut
use strict;
use warnings;
use Irssi;
use Irssi::TextUI;
use Data::Dumper;
our $VERSION = '2.1';
our %IRSSI =
(
authors => 'Tom Feist, Wouter Coekaerts',
contact => 'shabble+irssi@metavore.org, shabble@#irssi/freenode',
name => 'ido_switcher',
description => 'Select window[-items] using an ido-mode like search interface',
license => 'GPLv2 or later',
url => 'http://github.com/shabble/irssi-scripts/tree/master/ido-mode/',
changed => '24/7/2010'
);
my $CMD_NAME = 'ido_switch_start';
my $CMD_OPTS = '-channels -queries -all -active -exact -flex';
my $input_copy = '';
my $input_pos_copy = 0;
my $ido_switch_active = 0; # for intercepting keystrokes
my @window_cache = ();
my @search_matches = ();
my $match_index = 0;
my $search_str = '';
my $active_only = 0;
my $mode_type = 'ALL';
my @mode_cache;
my $showing_help = 0;
my $need_clear = 0;
my $sort_ordering = "start-asc";
my $sort_active_first = 0;
# /set configurable settings
my $ido_show_count;
my $ido_use_flex;
my $DEBUG_ENABLED = 0;
sub DEBUG () { $DEBUG_ENABLED }
sub MODE_WIN () { 0 } # windows
sub MODE_NET () { 1 } # chatnets
#sub MODE_C () { 2 } # channels
#sub MODE_S () { 3 } # select server
#sub MODE_W () { 4 } # select window
my $MODE = MODE_WIN;
# check we have uberprompt loaded.
sub _print {
my $win = Irssi::active_win;
my $str = join('', @_);
$need_clear = 1;
$win->print($str, MSGLEVEL_NEVER);
}
sub _debug_print {
return unless DEBUG;
my $win = Irssi::active_win;
my $str = join('', @_);
$win->print($str, MSGLEVEL_CLIENTCRAP);
}
sub _print_clear {
return unless $need_clear;
my $win = Irssi::active_win();
$win->command('/^scrollback levelclear -level NEVER');
}
# TODO: use the code from rl_history_search to put this into a disposable
# split win.
# TODO: create formats for this.
sub display_help {
my @message =
('%_IDO Window Switching Help:%_',
'',
'%_Ctrl-g%_ - cancel out of the mode without changing windows.',
'%_Esc%_ - cancel out, as above.',
'%_Ctrl-s%_ - rotate the list of window candidates forward by 1',
'%_Ctrl-r%_ - rotate the list of window candidates backward by 1',
'%_Ctrl-e%_ - Toggle \'Active windows only\' filter',
'%_Ctrl-f%_ - Switch between \'Flex\' and \'Exact\' matching.',
'%_Ctrl-d%_ - Select a network or server to filter candidates by',
'%_Ctrl-u%_ - Clear the current search string',
'%_Ctrl-q%_ - Cycle between showing only queries, channels, or all.',
'%_Ctrl-SPC%_ - Filter candidates by current search string, and then ',
' reset the search string',
'%_RET%_ - Select the current head of the candidate list (the %_green%n one)',
'%_SPC%_ - Select the current head of the list, without exiting switching',
' mode. The head is then moved one place to the right,',
' allowing one to cycle through channels by repeatedly pressing space.',
'%_TAB%_ - [%_currently non-functional%_] displays all possible completions',
' at the bottom of the current window.',
'',
' %_All other keys (a-z, A-Z, etc) - Add that character to the',
' %_current search string.',
'',
'%_Press Any Key to return%_',
);
_print($_) for @message;
$showing_help = 1;
}
sub print_all_matches {
my $msg = join(", ", map { $_->{name} } @search_matches);
my $message_header = "Windows:";
my $win = Irssi::active_win();
my $win_width = $win->{width} || 80;
# TODO: needs to prefix ambig things with chatnet, or maybe order in groups
# by chatnet with newlines.
# Also, colourise the channel list.
my $col_width;
for (@search_matches) {
my $len = length($_->{name});
$col_width = $len if $len > $col_width;
}
my $cols = int($win_width / $col_width);
my @lines;
my $i = 0;
my @line;
for my $item (@search_matches) {
my $name = $item->{name};
push @line, sprintf('%.*s', $col_width, $name);
if ($i == $cols) {
push @lines, join ' ', @line;
@line = ();
$i = 0;
}
}
# flush rest out.
push @lines, join ' ', @line;
_print($message_header);
_print($_) for (@lines);
#_print("Longtest name: $longest_name");
}
sub script_is_loaded {
return exists($Irssi::Script::{$_[0] . '::'});
}
unless (script_is_loaded('uberprompt')) {
_print "This script requires '\%_uberprompt.pl\%_' in order to work. "
. "Attempting to load it now...";
Irssi::signal_add('script error', 'load_uberprompt_failed');
Irssi::command("script load uberprompt.pl");
unless(script_is_loaded('uberprompt')) {
load_uberprompt_failed("File does not exist");
}
ido_switch_init();
}
sub load_uberprompt_failed {
Irssi::signal_remove('script error', 'load_uberprompt_failed');
_print "Script could not be loaded. Script cannot continue. "
. "Check you have uberprompt.pl installed in your path and "
. "try again.";
die "Script Load Failed: " . join(" ", @_);
}
sub ido_switch_init {
Irssi::settings_add_bool('ido_switch', 'ido_switch_debug', 0);
Irssi::settings_add_bool('ido_switch', 'ido_use_flex', 1);
Irssi::settings_add_bool('ido_switch', 'ido_show_active_first', 1);
Irssi::settings_add_int ('ido_switch', 'ido_show_count', 5);
Irssi::command_bind($CMD_NAME, \&ido_switch_start);
Irssi::command_set_options($CMD_NAME, $CMD_OPTS);
Irssi::signal_add ('setup changed' => \&setup_changed);
Irssi::signal_add_first('gui key pressed' => \&handle_keypress);
setup_changed();
}
sub setup_changed {
$DEBUG_ENABLED = Irssi::settings_get_bool('ido_switch_debug');
$ido_show_count = Irssi::settings_get_int ('ido_show_count');
$ido_use_flex = Irssi::settings_get_bool('ido_use_flex');
$sort_active_first = Irssi::settings_get_bool('ido_show_active_first');
}
sub ido_switch_start {
my ($args, $server, $witem) = @_;
# store copy of input line to restore later.
$input_copy = Irssi::parse_special('$L');
$input_pos_copy = Irssi::gui_input_get_pos();
Irssi::gui_input_set('');
my $options = {};
my @opts = Irssi::command_parse_options($CMD_NAME, $args);
if (@opts and ref($opts[0]) eq 'HASH') {
$options = $opts[0];
print "Options: " . Dumper($options);
}
# clear / initialise match variables.
$ido_switch_active = 1;
$search_str = '';
$match_index = 0;
# configure settings from provided arguments.
# use provided options first, or fall back to /setting.
$ido_use_flex = exists $options->{exact}
? 0
: exists $options->{flex}
? 1
: Irssi::settings_get_bool('ido_use_flex');
# only select active items
$active_only = exists $options->{active};
# what type of items to search.
$mode_type = exists $options->{queries}
? 'QUERY'
: exists $options->{channels}
? 'CHANNEL'
: 'ALL';
_debug_print "Win cache: " . join(", ", map { $_->{name} } @window_cache);
_update_cache();
update_matches();
update_window_select_prompt();
}
sub _update_cache {
@window_cache = get_all_windows();
}
sub _build_win_obj {
my ($win, $win_item) = @_;
my @base = (
b_pos => -1,
e_pos => -1,
hilight_field => 'name',
active => $win->{data_level} > 0,
num => $win->{refnum},
server => $win->{active_server},
);
if (defined($win_item)) {
return (
@base,
name => $win_item->{visible_name},
type => $win_item->{type},
itemname => $win_item->{name},
active => $win_item->{data_level} > 0,
)
} else {
return (
@base,
name => $win->{name},
type => 'WIN',
);
}
}
sub get_all_windows {
my @ret;
foreach my $win (Irssi::windows()) {
my @items = $win->items();
if ($win->{name} ne '') {
_debug_print "Adding window: " . $win->{name};
push @ret, { _build_win_obj($win, undef) };
}
if (scalar @items) {
foreach my $item (@items) {
_debug_print "Adding windowitem: " . $item->{visible_name};
push @ret, { _build_win_obj($win, $item) };
}
} else {
if (not grep { $_->{num} == $win->{refnum} } @ret) {
my $item = { _build_win_obj($win, undef) };
$item->{name} = "Unknown";
push @ret, $item;
}
#_debug_print "Error occurred reading info from window: $win";
#_debug_print Dumper($win);
}
}
@ret = _sort_windows(\@ret);
return @ret;
}
sub _sort_windows {
my $list_ref = shift;
my @ret = @$list_ref;
@ret = sort { $a->{num} <=> $b->{num} } @ret;
if ($sort_active_first) {
my @active = grep { $_->{active} } @ret;
my @inactive = grep { not $_->{active} } @ret;
return (@active, @inactive);
} else {
return @ret;
}
}
sub ido_switch_select {
my ($selected, $tag) = @_;
_debug_print sprintf("Selecting window: %s (%d)",
$selected->{name}, $selected->{num});
Irssi::command("WINDOW GOTO " . $selected->{num});
if ($selected->{type} ne 'WIN') {
_debug_print "Selecting window item: " . $selected->{itemname};
Irssi::command("WINDOW ITEM GOTO " . $selected->{itemname});
}
update_matches();
}
sub ido_switch_exit {
$ido_switch_active = 0;
_print_clear();
Irssi::gui_input_set($input_copy);
Irssi::gui_input_set_pos($input_pos_copy);
Irssi::signal_emit('change prompt', '', 'UP_INNER');
}
sub _order_matches {
return @_[$match_index .. $#_,
0 .. $match_index - 1]
}
sub update_window_select_prompt {
# take the top $ido_show_count entries and display them.
my $match_count = scalar @search_matches;
my $show_count = $ido_show_count;
my $match_string = '[No matches]';
$show_count = $match_count if $match_count < $show_count;
if ($show_count > 0) { # otherwise, default message above.
_debug_print "Showing: $show_count matches";
my @ordered_matches = _order_matches(@search_matches);
my @display = @ordered_matches[0..$show_count - 1];
# determine which items are non-unique, if any.
my %uniq;
foreach my $res (@display) {
my $name = $res->{name};
if (exists $uniq{$name}) {
push @{$uniq{$name}}, $res;
} else {
$uniq{$name} = [];
push @{$uniq{$name}}, $res;
}
}
# and set a flag to ensure they have their network tag applied
# to them when drawn.
foreach my $name (keys %uniq) {
my @values = @{$uniq{$name}};
if (@values > 1) {
$_->{display_net} = 1 for @values;
}
}
# show the first entry in green
my $first = shift @display;
my $formatted_first = _format_display_entry($first, '%g');
unshift @display, $formatted_first;
# and array-slice-map the rest to be red.
# or yellow, if they have unviewed activity
@display[1..$#display]
= map
{
_format_display_entry($_, $_->{active}?'%y':'%r')
} @display[1..$#display];
# join em all up
$match_string = join ', ', @display;
}
my @indicators;
# indicator if flex mode is being used (C-f to toggle)
push @indicators, $ido_use_flex ? 'Flex' : 'Exact';
push @indicators, 'Active' if $active_only;
push @indicators, ucfirst(lc($mode_type));
my $flex = sprintf(' %%b[%%n%s%%b]%%n ', join ', ', @indicators);
my $search = '';
$search = (sprintf '`%s\': ', $search_str) if length $search_str;
Irssi::signal_emit('change prompt', $flex . $search . $match_string,
'UP_INNER');
}
sub _format_display_entry {
my ($obj, $colour) = @_;
my $field = $obj->{hilight_field};
my $hilighted = { name => $obj->{name}, num => $obj->{num} };
my $show_tag = $obj->{display_net} || 0;
if ($obj->{b_pos} >= 0 && $obj->{e_pos} > $obj->{b_pos}) {
substr($hilighted->{$field}, $obj->{e_pos}, 0) = '%_';
substr($hilighted->{$field}, $obj->{b_pos}, 0) = '%_';
_debug_print "Showing $field as: " . $hilighted->{$field}
}
return sprintf('%s%s:%s%s%%n',
$colour,
$hilighted->{num},
$show_tag ? _format_display_tag($obj) : '',
$hilighted->{name});
}
sub _format_display_tag {
my $obj = shift;
if (defined $obj->{server}) {
my $server = $obj->{server};
my $tag = $server->{tag};
return $tag . '/' if length $tag;
}
return '';
}
sub _check_active {
my ($obj) = @_;
return 1 unless $active_only;
return $obj->{active};
}
sub update_matches {
_update_cache() unless $search_str;
if ($mode_type ne 'ALL') {
@mode_cache = @window_cache;
@window_cache = grep { $_->{type} eq $mode_type } @window_cache;
} else {
@window_cache = @mode_cache if @mode_cache;
}
if ($search_str =~ m/^\d+$/) {
@search_matches =
grep {
_check_active($_) and regex_match($_, 'num')
} @window_cache;
} elsif ($ido_use_flex) {
@search_matches =
grep {
_check_active($_) and flex_match($_) >= 0
} @window_cache;
} else {
@search_matches =
grep {
_check_active($_) and regex_match($_, 'name')
} @window_cache;
}
}
sub regex_match {
my ($obj, $field) = @_;
if ($obj->{$field} =~ m/^(.*?)\Q$search_str\E(.*?)$/i) {
$obj->{hilight_field} = $field;
$obj->{b_pos} = length $1;
$obj->{e_pos} = $obj->{b_pos} + length($search_str);
return 1;
}
return 0;
}
sub flex_match {
my ($obj) = @_;
my $pattern = $search_str;
my $source = $obj->{name};
_debug_print "Flex match: $pattern / $source";
# default to matching everything if we don't have a pattern to compare
# against.
return 0 unless $pattern;
my @chars = split '', lc($pattern);
my $ret = -1;
my $first = 0;
my $lc_source = lc($source);
$obj->{hilight_field} = 'name';
foreach my $char (@chars) {
my $pos = index($lc_source, $char, $ret);
if ($pos > -1) {
# store the beginning of the match
$obj->{b_pos} = $pos unless $first;
$first = 1;
_debug_print("matched: $char at $pos in $source");
$ret = $pos + 1;
} else {
$obj->{b_pos} = $obj->{e_pos} = -1;
_debug_print "Flex returning: -1";
return -1;
}
}
_debug_print "Flex returning: $ret";
#store the end of the match.
$obj->{e_pos} = $ret;
return $ret;
}
sub prev_match {
$match_index++;
if ($match_index > $#search_matches) {
$match_index = 0;
}
_debug_print "index now: $match_index";
}
sub next_match {
$match_index--;
if ($match_index < 0) {
$match_index = $#search_matches;
}
_debug_print "index now: $match_index";
}
sub get_window_match {
return $search_matches[$match_index];
}
sub handle_keypress {
my ($key) = @_;
return unless $ido_switch_active;
if ($showing_help) {
_print_clear();
$showing_help = 0;
Irssi::signal_stop();
}
if ($key == 0) { # C-SPC?
_debug_print "\%_Ctrl-space\%_";
$search_str = '';
@window_cache = @search_matches;
update_window_select_prompt();
Irssi::signal_stop();
return;
}
if ($key == 3) { # C-c
_print_clear();
Irssi::signal_stop();
return;
}
if ($key == 4) { # C-d
update_network_select_prompt();
Irssi::signal_stop();
return;
}
if ($key == 5) { # C-e
$active_only = not $active_only;
Irssi::signal_stop();
update_matches();
update_window_select_prompt();
return;
}
if ($key == 6) { # C-f
$ido_use_flex = not $ido_use_flex;
_update_cache();
update_matches();
update_window_select_prompt();
Irssi::signal_stop();
return;
}
if ($key == 9) { # TAB
_debug_print "Tab complete";
print_all_matches();
Irssi::signal_stop();
}
if ($key == 10) { # enter
_debug_print "selecting history and quitting";
my $selected_win = get_window_match();
ido_switch_select($selected_win);
ido_switch_exit();
Irssi::signal_stop();
return;
}
if ($key == 11) { # Ctrl-K
my $sel = get_window_match();
_debug_print("deleting entry: " . $sel->{num});
Irssi::command("window close " . $sel->{num});
_update_cache();
update_matches();
update_window_select_prompt();
Irssi::signal_stop();
}
if ($key == 18) { # Ctrl-R
_debug_print "skipping to prev match";
#update_matches();
next_match();
update_window_select_prompt();
Irssi::signal_stop(); # prevent the bind from being re-triggered.
return;
}
if ($key == 17) { # Ctrl-q
if ($mode_type eq 'CHANNEL') {
$mode_type = 'QUERY';
} elsif ($mode_type eq 'QUERY') {
$mode_type = 'ALL';
} else { # ALL
$mode_type = 'CHANNEL';
}
update_matches();
update_window_select_prompt();
Irssi::signal_stop();
}
if ($key == 19) { # Ctrl-s
_debug_print "skipping to next match";
prev_match();
#update_matches();
update_window_select_prompt();
Irssi::signal_stop();
return;
}
if ($key == 7) { # Ctrl-g
_debug_print "aborting search";
ido_switch_exit();
Irssi::signal_stop();
return;
}
if ($key == 8) { # Ctrl-h
display_help();
Irssi::signal_stop();
return;
}
if ($key == 21) { # Ctrl-u
$search_str = '';
update_matches();
update_window_select_prompt();
Irssi::signal_stop();
return;
}
if ($key == 127) { # DEL
if (length $search_str) {
$search_str = substr($search_str, 0, -1);
_debug_print "Deleting char, now: $search_str";
}
update_matches();
update_window_select_prompt();
Irssi::signal_stop();
return;
}
# TODO: handle esc- sequences and arrow-keys?
if ($key == 27) { # Esc
ido_switch_exit();
return;
}
if ($key == 32) { # space
my $selected_win = get_window_match();
ido_switch_select($selected_win);
prev_match();
update_window_select_prompt();
Irssi::signal_stop();
return;
}
if ($key > 32) { # printable
$search_str .= chr($key);
update_matches();
update_window_select_prompt();
Irssi::signal_stop();
return;
}
# ignore all other keys.
Irssi::signal_stop();
}
ido_switch_init();
sub update_network_select_prompt {
my @servers = map
{
{
name => $_->{tag},
type => 'SERVER',
active => 0,
e_pos => -1,
b_pos => -1,
hilight_field => 'name',
}
} Irssi::servers();
my $match_count = scalar @servers;
my $show_count = $ido_show_count;
my $match_string = '(no matches) ';
$show_count = $match_count if $match_count < $show_count;
if ($show_count > 0) {
_debug_print "Showing: $show_count matches";
my @ordered_matches = _order_matches(@servers);
my @display = @ordered_matches[0..$show_count - 1];
# show the first entry in green
unshift(@display, _format_display_entry(shift(@display), '%g'));
# and array-slice-map the rest to be red (or yellow for active)
@display[1..$#display]
= map
{
_format_display_entry($_, $_->{active}?'%y':'%r')
} @display[1..$#display];
# join em all up
$match_string = join ', ', @display;
}
my @indicators;
# indicator if flex mode is being used (C-f to toggle)
push @indicators, $ido_use_flex ? 'Flex' : 'Exact';
push @indicators, 'Active' if $active_only;
my $flex = sprintf(' %%k[%%n%s%%k]%%n ', join ',', @indicators);
my $search = '';
$search = (sprintf '`%s\': ', $search_str) if length $search_str;
Irssi::signal_emit('change prompt', $flex . $search . $match_string,
'UP_INNER');
}
|