-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathadv_windowlist.pl
2988 lines (2811 loc) · 89.1 KB
/
adv_windowlist.pl
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
use strict;
use warnings;
our $VERSION = '1.11'; # 28b8dcf69e0355e
our %IRSSI = (
authors => 'Nei',
contact => 'Nei @ [email protected]',
url => "http://anti.teamidiot.de/",
name => 'adv_windowlist',
description => 'Adds a permanent advanced window list on the right or in a status bar.',
sbitems => 'awl_shared',
license => 'GNU GPLv2 or later',
);
# UPGRADE NOTE
# ============
# for users of 0.7 or earlier series, please note that appearance
# settings have moved to /format, i.e. inside your theme!
# the fifo (screen) has been replaced by an external viewer script
# Usage
# =====
# copy the script to ~/.irssi/scripts/
#
# In irssi:
#
# /run adv_windowlist
#
# In your shell (for example a tmux split):
#
# perl ~/.irssi/scripts/adv_windowlist.pl
#
# To use sbar mode instead:
#
# /toggle awl_viewer
#
# Hint: to get rid of the old [Act:] display
# /statusbar window remove act
#
# to get it back:
# /statusbar window add -after lag -priority 10 act
# Options
# =======
# formats can be cleared with /format -delete
#
# /format awl_display_(no)key(_active|_visible) <string>
# * string : Format String for one window. The following $'s are expanded:
# $C : Name
# $N : Number of the Window
# $Q : meta-Keymap
# $H : Start hilighting
# $S : Stop hilighting
# /+++++++++++++++++++++++++++++++++,
# | **** I M P O R T A N T : **** |
# | |
# | don't forget to use $S if you |
# | used $H before! |
# | |
# '+++++++++++++++++++++++++++++++++/
# key : a key binding that goes to this window could be detected in /bind
# nokey : no such key binding was detected
# active : window would receive the input you are currently typing
# visible : window is also visible on screen but not active (a split window)
#
# /format awl_name_display <string>
# * string : Format String for window names
# $0 : name as formatted by the settings
#
# /format awl_display_header <string>
# * string : Format String for this header line. The following $'s are expanded:
# $C : network tag
#
# /format awl_separator(2) <string>
# * string : Character to use between the channel entries
# variant 2 can be used for alternating separators (only in status bar
# without block display)
#
# /format awl_abbrev_chars <string>
# * string : Character to use when shortening long names. The second character
# will be used if two blocks need to be filled.
#
# /format awl_title <string>
# * string : Text to display in the title string or title bar
#
# /format awl_viewer_item_bg <string>
# * string : Format String specifying the viewer's item background colour
#
# /set awl_prefer_name <ON|OFF>
# * this setting decides whether awl will use the active_name (OFF) or the
# window name as the name/caption in awl_display_*.
# That way you can rename windows using /window name myownname.
#
# /set awl_hide_empty <num>
# * if visible windows without items should be hidden from the window list
# set it to 0 to show all windows
# 1 to hide visible windows without items (negative exempt
# active window)
#
# /set awl_custom_key_re <regex>
# * regex : which symbolic key names to show in $Q (for example F-keys)
#
# /set awl_detach <list>
# * list of windows that should be hidden from the window list. you
# can also use /awl detach and /awl attach to manage this
# setting. an optional data_level can be specified with ",num"
#
# /set awl_detach_data <num>
# * num : hide the detached window if its data_level is below num
#
# /set awl_detach_aht <ON|OFF>
# * if enabled, also detach all windows listed in the
# activity_hide_targets setting
#
# /set awl_hide_data <num>
# * num : hide the window if its data_level is below num
# set it to 0 to basically disable this feature,
# 1 if you don't want windows without activity to be shown
# 2 to show only those windows with channel text or hilight
# 3 to show only windows with hilight (negative exempt active window)
#
# /set awl_hide_name_data <num>
# * num : hide the name of the window if its data_level is below num
# (only works in status bar without block display)
# you will want to change your formats to add $H...$S around $Q or $N
# if you plan to use this
#
# /set awl_maxlines <num>
# * num : number of lines to use for the window list (0 to disable, negative
# lock)
#
# /set awl_maxcolumns <num>
# * num : number of columns to use for the window list when using the
# tmux integration (0 to disable)
#
# /set awl_block <num>
# * num : width of a column in viewer mode (negative values = block
# display in status bar mode)
# /+++++++++++++++++++++++++++++++++,
# | ****** W A R N I N G ! ****** |
# | |
# | If your block display looks |
# | DISTORTED, you need to add the |
# | following line to your .theme |
# | file under |
# | abstracts = { : |
# | |
# | sb_act_none = "%K$*"; |
# | |
# '+++++++++++++++++++++++++++++++++/
#
# /set awl_sbar_maxlength <ON|OFF>
# * if you enable the maxlength setting, the block width will be used as a
# maximum length for the non-block status bar mode too.
#
# /set awl_height_adjust <num>
# * num : how many lines to leave empty in viewer mode
#
# /set awl_sort <-data_level|-last_line|refnum>
# * you can change the window sort order with this variable
# -data_level : sort windows with hilight first
# -last_line : sort windows in order of activity
# refnum : sort windows by window number
# active/server/tag : sort by server name
# lru : sort windows with the last recently used last
# "-" reverses the sort order
# typechecks are supported via ::, e.g. active::Query or active::Irc::Query
# undefinedness can be checked with ~, e.g. ~active
# string comparison can be done with =, e.g. name=(status)
# to make sort case insensitive, use #i, e.g. name#i
# any key in the window hash can be tested, e.g. active/chat_type=XMPP
# multiple criteria can be separated with , or +, e.g. -data_level+-last_line
#
# /set awl_placement <top|bottom>
# /set awl_position <num>
# * these settings correspond to /statusbar because awl will create
# status bars for you
# (see /help statusbar to learn more)
#
# /set awl_all_disable <ON|OFF>
# * if you set awl_all_disable to ON, awl will also remove the
# last status bar it created if it is empty.
# As you might guess, this only makes sense with awl_hide_data > 0 ;)
#
# /set awl_viewer <ON|OFF>
# * enable the external viewer script
#
# /set awl_viewer_launch <ON|OFF>
# * try to auto-launch the viewer under tmux or with a shell command
# /awl restart is required all auto-launch related settings to take
# effect
#
# /set awl_viewer_tmux_position <left|top|right|bottom|custom>
# * try to split in this direction when using tmux for the viewer
# custom : use custom_command setting
#
# /set awl_viewer_xwin_command <shell command>
# * custom command to run in order to start the viewer when irssi is
# running under X
# %A - gets replaced by the command to run the viewer
# %qA - additionally quote the command
#
# /set awl_viewer_custom_command <shell command>
# * custom command to run in order to start the viewer
#
# /set awl_viewer_launch_env <string>
# * specific environment settings for use on viewer auto-launch,
# without the AWL_ prefix
#
# /set awl_shared_sbar <left<right|OFF>
# * share a status bar for the first awl item, you will need to manually
# /statusbar window add -after lag -priority 10 awl_shared
# left : space in cells occupied on the left of status bar
# right : space occupied on the right
# Note: you need to replace "left" AND "right" with the appropriate numbers!
#
# /set awl_path <path>
# * path to the file which the viewer script reads
#
# /set fancy_abbrev <no|head|strict|fancy>
# * how to shorten too long names
# no : shorten in the middle
# head : always cut off the ends
# strict : shorten repeating substrings
# fancy : combination of no+strict
#
# /set awl_custom_xform <perl code>
# * specify a custom routine to transform window names
# example: s/^#// remove the #-mark of IRC channels
# the special flags $CHANNEL / $TAG / $QUERY / $NAME can be
# tested in conditionals
#
# /set awl_last_line_shade <timeout>
# * set timeout to shade activity base colours, to enable
# you also need to add +-last_line to awl_sort
# (requires 256 colour support)
#
# /set awl_no_mode_hint <ON|OFF>
# * whether to show the hint of running the viewer script in the
# status bar
#
# /set awl_mouse <ON|OFF>
# * enable the terminal mouse in irssi
# (use the awl-patched mouse.pl for gestures and commands if you need
# them and disable mouse_escape)
#
# /set awl_mouse_offset <num>
# * specifies where on the screen is the awl status bar
# (0 = on top/bottom, 1 = one additional line in between,
# e.g. prompt)
# you MUST set this correctly otherwise the mouse coordinates will
# be off
#
# /set mouse_scroll <num>
# * how many lines the mouse wheel scrolls
#
# /set mouse_escape <num>
# * seconds to disable the mouse, when not clicked on the windowlist
#
# Commands
# ========
# /awl detach <num>
# * hide the current window from the window list. num specifies the
# data_level (optional)
#
# /awl attach
# * unhide the current window from the window list
#
# /awl ack
# * change to the next window with activity, ignoring detached windows
#
# /awl redraw
# * redraws the windowlist. There may be occasions where the
# windowlist can get destroyed so you can use this command to
# force a redraw.
#
# /awl restart
# * restart the connection to the viewer script.
# Viewer script
# =============
# When run from the command line, adv_windowlist acts as the viewer
# script to be used together with the irssi script to display the
# window list in a sidebar/terminal of its own.
#
# One optional parameter is accepted, the awl_path
#
# The viewer can be configured by three environment variables:
#
# AWL_HI9=1
# * interpret %9 as high-intensity toggle instead of bold. This had
# been the default prior to version 0.9b8
#
# AWL_AUTOFOCUS=0
# * disable auto-focus behaviour when activating a window
#
# AWL_NOTITLE=1
# * disable the title bar
# Nei =^.^= ( [email protected] )
no warnings 'redefine';
use constant IN_IRSSI => __PACKAGE__ ne 'main' || $ENV{IRSSI_MOCK};
use constant SCRIPT_FILE => __FILE__;
no if !IN_IRSSI, strict => (qw(subs refs));
use if IN_IRSSI, Irssi => ();
use if IN_IRSSI, 'Irssi::TextUI' => ();
use v5.10;
use Encode;
use Storable ();
use IO::Socket::UNIX;
use List::Util qw(min max reduce);
use Hash::Util qw(lock_keys);
use Text::ParseWords qw(shellwords);
BEGIN {
if ($] < 5.012) {
*CORE::GLOBAL::length = *CORE::GLOBAL::length = sub (_) {
defined $_[0] ? CORE::length($_[0]) : undef
};
}
*Irssi::active_win = {}; # hide incorrect warning
}
unless (IN_IRSSI) {
local *_ = \@ARGV;
&AwlViewer::main;
exit;
}
use constant GLOB_QUEUE_TIMER => 100;
our $BLOCK_ALL; # localized blocker
my @actString; # status bar texts
my @win_items;
my $currentLines = 0;
my %awins;
my $globTime; # timer to limit remake calls
my %CHANGED;
my $VIEWER_MODE;
my $MOUSE_ON;
my %mouse_coords;
my %statusbars;
my %S; # settings
my $settings_str = '1';
my $window_sort_func;
my $custom_xform;
my $custom_key_re = qr/(?!)/;
my ($sb_base_width, $sb_base_width_pre, $sb_base_width_post);
my $print_text_activity;
my $shade_line_timer;
my ($screenHeight, $screenWidth);
my %viewer;
my (%keymap, %nummap, %wnmap, %specialmap, %wnmap_exp, %custom_key_map);
my %banned_channels;
my %detach_map;
my %abbrev_cache;
use constant setc => 'awl';
sub set ($) {
setc . '_' . $_[0]
}
sub add_statusbar {
for (@_) {
# add subs
my $l = set $_;
{
my $close = $_;
no strict 'refs';
*{$l} = sub { awl($close, @_) };
}
Irssi::command("^statusbar $l reset");
Irssi::command("statusbar $l enable");
if (lc $S{placement} eq 'top') {
Irssi::command("statusbar $l placement top");
}
if (my $x = $S{position}) {
Irssi::command("statusbar $l position $x");
}
Irssi::command("statusbar $l add -priority 100 -alignment left barstart");
Irssi::command("statusbar $l add $l");
Irssi::command("statusbar $l add -priority 100 -alignment right barend");
Irssi::command("statusbar $l disable");
Irssi::statusbar_item_register($l, '$0', $l);
$statusbars{$_} = 1;
Irssi::command("statusbar $l enable");
}
}
sub remove_statusbar {
for (@_) {
my $l = set $_;
Irssi::command("statusbar $l disable");
Irssi::command("statusbar $l reset");
Irssi::statusbar_item_unregister($l);
{
no strict 'refs';
undef &{$l};
}
delete $statusbars{$_};
}
}
my $awl_shared_empty = sub {
return if $BLOCK_ALL;
my ($item, $get_size_only) = @_;
$item->default_handler($get_size_only, '', '', 0);
};
sub syncLines {
my $maxLines = $S{maxlines};
my $newLines = ($maxLines > 0 and @actString > $maxLines) ?
$maxLines :
($maxLines < 0) ?
-$maxLines :
@actString;
$currentLines = 1 if !$currentLines && $S{shared_sbar};
if ($S{shared_sbar} && !$statusbars{shared}) {
my $l = set 'shared';
{
no strict 'refs';
*{$l} = sub {
return if $BLOCK_ALL;
my ($item, $get_size_only) = @_;
my $text = $actString[0];
my $title = _get_format(set 'title');
if (length $title) {
$title =~ s{\\(.)|(.)}{
defined $2 ? quotemeta $2
: $1 eq 'V' ? '\u'
: $1 eq ':' ? quotemeta ':%n'
: $1 =~ /^[uUFQE]$/ ? "\\$1"
: quotemeta "\\$1"
}sge;
$title = eval qq{"$title"};
$title .= ' ';
}
my $pat = defined $text ? "{sb $title\$*}" : '{sb }';
$text //= '';
$item->default_handler($get_size_only, $pat, $text, 0);
};
}
$statusbars{shared} = 1;
remove_statusbar (0) if $statusbars{0};
}
elsif ($statusbars{shared} && !$S{shared_sbar}) {
add_statusbar (0) if $currentLines && $newLines;
delete $statusbars{shared};
my $l = set 'shared';
{
no strict 'refs';
*{$l} = $awl_shared_empty;
}
}
if ($currentLines == $newLines) { return; }
elsif ($newLines > $currentLines) {
add_statusbar ($currentLines .. ($newLines - 1));
}
else {
remove_statusbar (reverse ($newLines .. ($currentLines - 1)));
}
$currentLines = $newLines;
}
sub awl {
return if $BLOCK_ALL;
my ($line, $item, $get_size_only) = @_;
my $text = $actString[$line];
my $pat = defined $text ? '{sb $*}' : '{sb }';
$text //= '';
$item->default_handler($get_size_only, $pat, $text, 0);
}
# remove old statusbars
{ my %killBar;
sub get_old_status {
my ($textDest, $cont, $cont_stripped) = @_;
if ($textDest->{level} == MSGLEVEL_CLIENTCRAP and $textDest->{target} eq '' and !defined $textDest->{server}) {
my $name = quotemeta(set '');
if ($cont_stripped =~ m/^$name(\d+)\s/) { $killBar{$1} = 1; }
Irssi::signal_stop;
}
}
sub killOldStatus {
%killBar = ();
Irssi::signal_add_first('print text' => 'get_old_status');
Irssi::command('statusbar');
Irssi::signal_remove('print text' => 'get_old_status');
remove_statusbar(keys %killBar);
}
}
sub _add_map {
my ($type, $target, $map) = @_;
($type->{$target}) = sort { length $a <=> length $b || $a cmp $b }
$map, exists $type->{$target} ? $type->{$target} : ();
}
sub get_keymap {
my ($textDest, undef, $cont_stripped) = @_;
if ($textDest->{level} == MSGLEVEL_CLIENTCRAP and $textDest->{target} eq '' and !defined $textDest->{server}) {
my $one_meta_or_ctrl_key = qr/((?:meta-)*?)(?:(meta-|\^)(\S)|(\w+))/;
$cont_stripped = as_uni($cont_stripped);
if ($cont_stripped =~ m/((?:$one_meta_or_ctrl_key-)*$one_meta_or_ctrl_key)\s+(.*)$/) {
my ($combo, $command) = ($1, $10);
my $map = '';
while ($combo =~ s/(?:-|^)$one_meta_or_ctrl_key$//) {
my ($level, $ctl, $key, $nkey) = ($1, $2, $3, $4);
my $numlevel = ($level =~ y/-//);
if (not defined $key and $nkey =~ /^($custom_key_re)$/) {
$key = $nkey;
}
$ctl = '' if !$ctl || $ctl ne '^';
$map = ('-' x ($numlevel%2)) . ('+' x ($numlevel/2)) .
$ctl . (defined $key ? $key : "\01$nkey\01") . $map;
}
for ($command) {
last unless length $map;
if (/^change_window (\d+)/i) {
_add_map(\%nummap, $1, $map);
}
elsif (/^(?:command window goto|change_window) (\S+)/i) {
my $window = $1;
if ($window !~ /\D/) {
_add_map(\%nummap, $window, $map);
}
elsif (lc $window eq 'active') {
_add_map(\%specialmap, '_active', $map);
}
else {
_add_map(\%wnmap, $window, $map);
}
}
elsif (/^(?:active_window|command ((awl )?ack))/i) {
_add_map(\%specialmap, '_active', $map);
$viewer{use_ack} = $1;
}
elsif (/^command window last/i) {
_add_map(\%specialmap, '_last', $map);
}
elsif (/^(?:upper_window|command window up)/i) {
_add_map(\%specialmap, '_up', $map);
}
elsif (/^(?:lower_window|command window down)/i) {
_add_map(\%specialmap, '_down', $map);
}
elsif (/^key\s+(\w+)/i) {
$custom_key_map{$1} = $map;
}
}
}
Irssi::signal_stop;
}
}
sub update_keymap {
%nummap = %wnmap = %specialmap = %custom_key_map = ();
Irssi::signal_remove('command bind' => 'watch_keymap');
Irssi::signal_add_first('print text' => 'get_keymap');
Irssi::command('bind');
Irssi::signal_remove('print text' => 'get_keymap');
for (keys %custom_key_map) {
if (exists $custom_key_map{$_} &&
$custom_key_map{$_} =~ s/\01(\w+)\01/exists $custom_key_map{$1} ? $custom_key_map{$1} : "\02"/ge) {
if ($custom_key_map{$_} =~ /\02/) {
delete $custom_key_map{$_};
}
else {
redo;
}
}
}
for my $keymap (\(%specialmap, %wnmap, %nummap)) {
for (keys %$keymap) {
if ($keymap->{$_} =~ s/\01(\w+)\01/exists $custom_key_map{$1} ? $custom_key_map{$1} : "\02"/ge) {
if ($keymap->{$_} =~ /\02/) {
delete $keymap->{$_};
}
}
}
}
Irssi::signal_add('command bind' => 'watch_keymap');
delete $viewer{client_keymap};
&wl_changed;
}
# watch keymap changes
sub watch_keymap {
Irssi::timeout_add_once(1000, 'update_keymap', undef);
}
{ my %strip_table = (
# fe-common::core::formats.c:format_expand_styles
# delete format_backs format_fores bold_fores other stuff
(map { $_ => '' } (split //, '04261537' . 'kbgcrmyw' . 'KBGCRMYW' . 'U9_8I:|FnN>#[' . 'pP')),
# escape
(map { $_ => $_ } (split //, '{}%')),
);
sub ir_strip_codes { # strip %codes
my $o = shift;
$o =~ s/(%(%|Z.{6}|z.{6}|X..|x..|.))/exists $strip_table{$2} ? $strip_table{$2} :
$2 =~ m{x(?:0[a-f]|[1-6][0-9a-z]|7[a-x])|z[0-9a-f]{6}}i ? '' : $1/gex;
$o
}
}
## ir_parse_special -- wrapper around parse_special
## $i - input format
## $args - array ref of arguments to format
## $win - different target window (default current window)
## $flags - different kind of escape flags (default 4|8)
## returns formatted str
sub ir_parse_special {
my $o;
my $i = shift;
my $args = shift // [];
y/ /\177/ for @$args; # hack to escape spaces
my $win = shift || Irssi::active_win;
my $flags = shift // 0x4|0x8;
my @cmd_args = ($i, (join ' ', @$args), $flags);
my $server = Irssi::active_server();
if (ref $win and ref $win->{active}) {
$o = $win->{active}->parse_special(@cmd_args);
}
elsif (ref $win and ref $win->{active_server}) {
$o = $win->{active_server}->parse_special(@cmd_args);
}
elsif (ref $server) {
$o = $server->parse_special(@cmd_args);
}
else {
$o = &Irssi::parse_special(@cmd_args);
}
$o =~ y/\177/ /;
$o
}
sub sb_format_expand { # Irssi::current_theme->format_expand wrapper
Irssi::current_theme->format_expand(
$_[0],
(
Irssi::EXPAND_FLAG_IGNORE_REPLACES
|
($_[1] ? 0 : Irssi::EXPAND_FLAG_IGNORE_EMPTY)
)
)
}
{ my $term_type = Irssi::version > 20040819 ? 'term_charset' : 'term_type';
if (Irssi->can('string_width')) {
*screen_length = sub { Irssi::string_width($_[0]) };
}
else {
local $@;
eval { require Text::CharWidth; };
unless ($@) {
*screen_length = sub { Text::CharWidth::mbswidth($_[0]) };
}
else {
my $err = $@; chomp $err; $err =~ s/\sat .* line \d+\.$//;
#Irssi::print("%_$IRSSI{name}: warning:%_ Text::CharWidth module failed to load. Length calculation may be off! Error was:");
print "%_$IRSSI{name}:%_ $err";
*screen_length = sub {
my $temp = shift;
if (lc Irssi::settings_get_str($term_type) eq 'utf-8') {
Encode::_utf8_on($temp);
}
length($temp)
};
}
}
sub as_uni {
no warnings 'utf8';
Encode::decode(Irssi::settings_get_str($term_type), $_[0], 0)
}
sub as_tc {
Encode::encode(Irssi::settings_get_str($term_type), $_[0], 0)
}
}
sub sb_length {
screen_length(ir_strip_codes($_[0]))
}
sub run_custom_xform {
local $@;
eval {
$custom_xform->()
};
if ($@) {
$@ =~ /^(.*)/;
print '%_'.(set 'custom_xform').'%_ died (disabling): '.$1;
$custom_xform = undef;
}
}
sub remove_uniform {
my $o = shift;
$o =~ s/^xmpp:(.*?[%@]).+\.[^.]+$/$1/ or
$o =~ s#^psyc://.+\.[^.]+/([@~].*)$#$1#;
if ($custom_xform) {
run_custom_xform() for $o;
}
$o
}
sub remove_uniform_vars {
my $win = shift;
my $name = __PACKAGE__ . '::custom_xform::' . $win->{active}{type}
if ref $win->{active} && $win->{active}{type};
no strict 'refs';
local ${$name} = 1 if $name;
remove_uniform(+shift);
}
sub lc1459 {
my $x = shift;
$x =~ y/][\\^/}{|~/;
lc $x
}
sub window_list {
my $i = 0;
map { $_->[1] } sort $window_sort_func map { [ $i++, $_ ] } Irssi::windows;
}
sub _calculate_abbrev {
my ($wins, $abbrevList) = @_;
if ($S{fancy_abbrev} !~ /^(no|off|head)/i) {
my @nameList = map { ref $_ ? remove_uniform_vars($_, as_uni($_->get_active_name) // '') : '' } @$wins;
for (my $i = 0; $i < @nameList - 1; ++$i) {
my ($x, $y) = ($nameList[$i], $nameList[$i + 1]);
s/^[+#!=]// for $x, $y;
my $res = exists $abbrev_cache{$x}{$y} ? $abbrev_cache{$x}{$y}
: $abbrev_cache{$x}{$y} = string_LCSS($x, $y);
if (defined $res) {
for ($nameList[$i], $nameList[$i + 1]) {
$abbrevList->{$_} //= int((index $_, $res) + (length $res) / 2);
}
}
}
}
}
my %act_last_line_shades = (
r => [qw[ 50 40 30 20 ]],
g => [qw[ 1O 1I 1C 16 ]],
y => [qw[ 5O 4I 3C 26 ]],
b => [qw[ 15 14 13 12 ]],
m => [qw[ 54 43 32 21 ]],
c => [qw[ 1S 1L 1E 17 ]],
w => [qw[ 7W 7T 7Q 3E ]],
K => [qw[ 7M 7K 27 7H ]],
R => [qw[ 60 50 40 30 ]],
G => [qw[ 1U 1O 1I 1C ]],
Y => [qw[ 6U 5O 4I 3C ]],
B => [qw[ 2B 2A 29 28 ]],
M => [qw[ 65 54 43 32 ]],
C => [qw[ 1Z 1S 1L 1E ]],
W => [qw[ 6Z 5S 7R 7O ]],
);
sub _format_display {
my (undef, $format, $cformat, $hilight, $name, $number, $key, $win) = @_;
if ($print_text_activity && $S{line_shade}) {
my @hilight_code = split /\177/, sb_format_expand("{$hilight \177}"), 2;
my $max_time = max(1, log($S{line_shade}) - log(1000));
my $time_delta = min(3, min($max_time, log(max(1, time - $win->{last_line}))) / $max_time * 3);
if ($hilight_code[0] =~ /%(.)/ && exists $act_last_line_shades{$1}) {
$hilight = 'sb_act_hilight_color %X'.$act_last_line_shades{$1}[$time_delta];
}
}
$cformat = '$0' unless length $cformat;
my %map = ('$C' => $cformat, '$N' => '$1', '$Q' => '$2');
$format =~ s<(\$.)><$map{$1}//$1>ge;
$format =~ s<\$H((?:\$.|[^\$])*?)\$S><{$hilight $1%n}>g;
my @ret = ir_parse_special(sb_format_expand($format), [$name, $number, $key], $win);
@ret
}
sub _get_format {
Irssi::current_theme->get_format(__PACKAGE__, @_)
}
sub _is_detached {
my ($win, $active_number) = @_;
my $level = $win->{data_level} // 0;
my $number = $win->{refnum};
my $name = lc1459( as_uni($win->{name}) );
my $active = lc1459( as_uni($win->get_active_name) // '' );
my $tag = $win->{active} && $win->{active}{server} ? lc1459( as_uni($win->{active}{server}{tag}) // '' ) : '';
my @cond = ($number);
push @cond, "$name" if length $name;
push @cond, "$tag/$active" if length $tag && length $active;
push @cond, "$active" if length $active;
push @cond, "$tag/*", "$tag/::all" if length $tag;
push @cond, "*", "::all";
for my $cond (@cond) {
if (exists $detach_map{ $cond }) {
my $dd = $detach_map{ $cond } // $S{detach_data};
return $win->{data_level} < abs $dd
&& ($number != $active_number || 0 <= $dd);
}
}
return;
}
sub _calculate_items {
my ($wins, $abbrevList) = @_;
my $display_header = _get_format(set 'display_header');
my $name_format = _get_format(set 'name_display');
my $abbrev_chars = as_uni(_get_format(set 'abbrev_chars'));
my %displays;
my $active = Irssi::active_win;
@win_items = ();
%keymap = (%nummap, %wnmap_exp);
my ($numPad, $keyPad) = (0, 0);
if ($VIEWER_MODE or $S{block} < 0) {
$numPad = length((sort { length $b <=> length $a } keys %keymap)[0]) // 0;
$keyPad = length((sort { length $b <=> length $a } values %keymap)[0]) // 0;
}
my $last_net;
my ($abbrev1, $abbrev2) = $abbrev_chars =~ /(\X)(.*)/;
my @abbrev_chars = ('~', "\x{301c}");
unless (defined $abbrev1 && screen_length(as_tc($abbrev1)) == 1) { $abbrev1 = $abbrev_chars[0] }
unless (length $abbrev2) {
$abbrev2 = $abbrev1;
if ($abbrev1 eq $abbrev_chars[0]) {
$abbrev2 = $abbrev_chars[1];
}
else {
$abbrev2 = $abbrev1;
}
}
if (screen_length(as_tc($abbrev2)) == 1) {
$abbrev2 x= 2;
}
while (screen_length(as_tc($abbrev2)) > 2) {
chop $abbrev2;
}
unless (screen_length(as_tc($abbrev2)) == 2) {
$abbrev2 = $abbrev_chars[1];
}
for my $win (@$wins) {
my $global_tag_header_mode;
next unless ref $win;
my $backup_win = Storable::dclone($win);
delete $backup_win->{active} unless ref $backup_win->{active};
$global_tag_header_mode =
$display_header && ($last_net // '') ne ($backup_win->{active}{server}{tag} // '');
if ($win->{data_level} < abs $S{hide_data}
&& ($win->{refnum} != $active->{refnum} || 0 <= $S{hide_data})) {
next; }
elsif (exists $awins{$win->{refnum}} && $S{hide_empty} && !$win->items
&& ($win->{refnum} != $active->{refnum} || 0 <= $S{hide_empty})) {
next; }
elsif (_is_detached($win, $active->{refnum})) {
next; }
my $colour = $win->{hilight_color} // '';
my $hilight = do {
if ($win->{data_level} == 0) { 'sb_act_none'; }
elsif ($win->{data_level} == 1) { 'sb_act_text'; }
elsif ($win->{data_level} == 2) { 'sb_act_msg'; }
elsif ($colour ne '') { "sb_act_hilight_color $colour"; }
elsif ($win->{data_level} == 3) { 'sb_act_hilight'; }
else { 'sb_act_special'; }
};
my $number = $win->{refnum};
my ($name, $display, $cdisplay);
if ($global_tag_header_mode) {
$display = $display_header;
$name = as_uni($backup_win->{active}{server}{tag}) // '';
if ($custom_xform) {
no strict 'refs';
local ${ __PACKAGE__ . '::custom_xform::TAG' } = 1;
run_custom_xform() for $name;
}
}
else {
my @display = ('display_nokey');
if (defined $keymap{$number} and $keymap{$number} ne '') {
unshift @display, map { (my $cpy = $_) =~ s/_no/_/; $cpy } @display;
}
if (exists $awins{$number}) {
unshift @display, map { my $cpy = $_; $cpy .= '_visible'; $cpy } @display;
}
if ($active->{refnum} == $number) {
unshift @display, map { my $cpy = $_; $cpy .= '_active'; $cpy }
grep { !/_visible$/ } @display;
}
$display = (grep { length $_ }
map { $displays{$_} //= _get_format(set $_) }
@display)[0];
$cdisplay = $name_format;
$name = as_uni($win->get_active_name) // '';
$name = '*' if $S{banned_on} and exists $banned_channels{lc1459($name)};
$name = remove_uniform_vars($win, $name) if $name ne '*';
if ($name ne '*' and $win->{name} ne '' and $S{prefer_name}) {
$name = as_uni($win->{name});
if ($custom_xform) {
no strict 'refs';
local ${ __PACKAGE__ . '::custom_xform::NAME' } = 1;
run_custom_xform() for $name;
}
}
if (!$VIEWER_MODE && $S{block} >= 0 && $S{hide_name}
&& $win->{data_level} < abs $S{hide_name}
&& ($win->{refnum} != $active->{refnum} || 0 <= $S{hide_name})) {
$name = '';
$cdisplay = '';
}
}
$display = "$display%n";
my $num_ent = (' 'x max(0,$numPad - length $number)) . $number;
my $key_ent = exists $keymap{$number} ? ((' 'x max(0,$keyPad - length $keymap{$number})) . $keymap{$number}) : ' 'x$keyPad;
if ($VIEWER_MODE or $S{sbar_maxlen} or $S{block} < 0) {
my $baseLength = sb_length(_format_display(
'', $display, $cdisplay, $hilight,
'x', # placeholder
$num_ent,
$key_ent,
$win)) - 1;
my $diff = (abs $S{block}) - (screen_length(as_tc($name)) + $baseLength);
if ($diff < 0) { # too long
my $screen_length = screen_length(as_tc($name));
if ((abs $diff) >= $screen_length) { $name = '' } # forget it
elsif ((abs $diff) + screen_length(as_tc(substr($name, 0, 1))) >= $screen_length) { $name = substr($name, 0, 1); }
else {
my $ulen = length $name;
my $middle2 = exists $abbrevList->{$name} ?
($S{fancy_strict}) ?
2* $abbrevList->{$name} :
(2*($abbrevList->{$name} + $ulen) / 3) :
($S{fancy_head}) ?
2*$ulen :
$ulen;
my $first = 1;
while (length $name > 1) {
my $cp = $middle2 >= 0 ? $middle2/2 : -1; # clearing position
my $rm = 2;
# if character at end is wider than 1 cell -> replace it with ~
if (screen_length(as_tc(substr $name, $cp, 1)) > 1) {
if ($first || $cp < 0) {
$rm = 1;
$first = undef;
}
}
elsif ($cp < 0) { # elsif at end -> replace last 2 characters
--$cp;
}
(substr $name, $cp, $rm) = $abbrev1;
if ($cp > -1 && $rm > 1) {
--$middle2;
}
my $sl = screen_length(as_tc($name));
if ($sl + $baseLength < abs $S{block}) {
(substr $name, ($middle2+1)/2, 1) = $abbrev2;
last;
}
elsif ($sl + $baseLength == abs $S{block}) {
last;
}
}
}
}
elsif ($VIEWER_MODE or $S{block} < 0) {
$name .= (' ' x $diff);
}
}
push @win_items, _format_display(
'', $display, $cdisplay, $hilight,
as_tc($name),
$num_ent,
as_tc($key_ent),
$win);
if ($global_tag_header_mode) {
$last_net = $backup_win->{active}{server}{tag};
redo;
}