forked from stepb/urxvt-tabbedex
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtabbedex
1721 lines (1381 loc) · 48.4 KB
/
tabbedex
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
#! perl
# TabbedEx plugin for rxvt-unicode; based on original tabbed plugin.
# https://github.com/mina86/urxvt-tabbedex
# Copyright 2006-2020 tabbed and tabbedex authors
#
# 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 3 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, see <http://www.gnu.org/licenses/>.
=head1 NAME
tabbedex - tabbed interface to urxvt; extended
=head1 DESCRIPTION
This extension implements a tabbed terminal. Once rxvt-unicode starts, slave
terminals can be started in their own tabs and switched between. At each point
only one terminal is visible. rxvt-unicode exits once all tabs are closed.
Once at least two tabs are created a tab bar is displayed at the top of the
window which lists all the slave terminals. Clicking on a number/name of a tab
on that bar switches to it.
tabbedex is an extended version of the tabbed plugin distributed with urxvt.
=head2 Key bindings
Creating new tabs, switching between them and moving them around can be
performed through the following key bindings:
=over
=item B<Shift-Down>
Creates a new tab.
=item B<Shift-Left> and B<Shift-Right>
Switches to the tab on the left or on the right of the current tab. Movement
wraps around once at the first or last tab.
=item B<Meta-F1> through B<Meta-F12>
Switches to the first through twelfth tab.
=item B<Control-Left> and B<Control-right>
Move current tab to the left or right. Wraps around on the first and last
position.
=item B<Shift-Up>
Allows current tab to be renamed. Tab bar is shown if it was hidden, current
tab's name (if any) is cleared and cursor is displayed in its place. Typing
text renames the tab, backspace or Ctrl+H delete the last character, Escape or
Ctrl+C aborts the rename operation and Enter accepts the new name.
=back
Default key bindings can be disabled using B<no-tabbedex-keys> resource (see
below) and replaced with custom ones using urxvt's B<keysym> resource.
=head1 CONFIGURATION
The extension can be configured with X resources just like urxvt itself, for
example:
URxvt.tabbedex.tabbar-fg: 4
URxvt.tabbedex.tabbar-bg: 0
Tabbedex recognises the following resources:
=over
=item B<autohide>: I<boolean>
If set (the default), the tab bar will be hidden unless there are at least two
tabs open. This is irrespective of whether C<NEW> button or tab's title is
displayed.
=item B<new-button>: I<boolean>
If set, a C<NEW> button will be displayed on the left of the tab bar. Clicking
the button creates a new tab. It is not displayed by default.
=item B<title>: I<boolean>
If set (the default), when tab bar is visible and there is enough space left,
current tab's title will be displayed after the last tab.
=item B<tabbar-fg>: [I<style>] I<colour> and B<tabbar-bg>: I<colour>
=item B<tab-fg>: [I<style>] I<colour> and B<tab-bg>: I<colour>
=item B<bell-fg>: [I<style>] I<colour> and B<bell-bg>: I<colour>
=item B<bell-tab-fg>: [I<style>] I<colour> and B<bell-tab-bg>: I<colour>
=item B<title-fg>: [I<style>] I<colour> and B<title-bg>: I<colour>
Foreground and background styles of elements of the tab bar, respectively: 1)
the tab bar including background tabs, 2) the current tab and new button, 3)
a background tab on which bell has rung, 4) the current tab if bell has rung and
5) tab title.
Colours can be specified as: I<-2> meaning default foreground, I<-1> meaning
default background, an integer between B<0> and B<255> specifying index in
terminal' colour palette or any other string which X11 recognises as a colour
(e.g. B<#1155CC>).
Furthermore, the B<-fg> resource can be prefixed by combination of B<bold>,
B<italic>, B<blink>, B<reverse-video> and B<underline> which specify the
font-style of the text.
The default values for the settings are as follows:
Component | *-fg | *-bg | Description
----------+---------------+------+----------------------
tabbar | 3 | 0 | brown text on black
tab | bold 0 | 1 | black text on red
bell | italic 0 | 3 | black text on brown
bell-tab | bold italic 5 | 4 | magenta text on blue
title | italic 2 | 0 | green text on black
=item B<bell-timeout>: I<number>
Time in seconds, one second by default, a bell is said to be ringing on current
tab for after it was rung. Setting this to zero essentially disables the
B<bell-tab-(fg|bg)> styling of tabs.
=item B<tab-arguments>: I<string>
Arguments passed when starting a slave tabs. If not specified, I<-e B<command>>
switch used when starting urxvt command is used (if any). If specified, the
value is first expanded (see below), then split into words using shell quoting
rules and those are passed as arguments to slave terminals when they are
started.
This resource can be used to pass any arguments however doing so may have
unintended consequences. Options such as I<-bw> or I<-geometry> are especially
problematic but others may cause issues as well. Because of this, only I<-e
B>command arguments>> is officially supported.
The value is expanded by replacing two-character sequences starting with
a percent sign as follows:
=over
=item I<%%>
Replaced by a literal percent sign.
=item I<%e>
Replaced by properly quoted I<-e B<command>> arguments if they were given when
urxvt was started or an empty string otherwise. See also I<%E> which may be
easier for some uses.
Remember that if neither I<%e> nor I<%E> is used, I<-e> switch passed when
starting urxvt will be completely ignored.
=item I<%E>
Similar to I<%e> but I<-e> is not included in the expansion and if I<%e> would
expand to an empty string I<%E> expands to value of SHELL environment variable
or I</bin/sh> if that's empty. This means that I<%E> is always a valid command.
For example, here's how the sequence can be used to set TABBEDEX_NUM environment
variables when running a command:
URxvt.tabbedex.tab-arguments: \
-e /usr/bin/env TABBEDEX_NUM=%n %E
=item I<%n>
Replaced by the total number of tabs opened during the lifetime of the
extension. Not to be confused with number of tabs currently open. The counter
starts at zero and always increments for each subsequent tab.
=item I<%p>
Replaced by ID of a process group which is running in foreground of the pseudo
terminal in the current tab (as in, not the new tab being created but the tab
where the new tab request originated from) or -1 if this piece of information
could not be determined.
This can be used to guess the current working directory of current tab.
=item I<%~>
Replaced by a properly quoted value of HOME environment variable or I</> if it's
not set.
=back
Unrecognised sequences are replaced with an empty string.
See B<command-runner.sample> file distributed with this extension for some ideas
how this resource can be used to start different commands in different tabs or
how to start new tabs in the same working directory as the current tab is using.
=item B<no-tabbedex-keys>: I<boolean>
If set, the default key bindings (described at the beginning of this document)
are not set up. The mappings can be recreated using urxvt's B<keysym.*>
resources:
URxvt.tabbedex.no-tabbedex-keys: yes
URxvt.keysym.Shift-Left: tabbedex:prev_tab
URxvt.keysym.Shift-Right: tabbedex:next_tab
URxvt.keysym.Shift-Down: tabbedex:new_tab
URxvt.keysym.Shift-Up: tabbedex:rename_tab
URxvt.keysym.Control-Left: tabbedex:move_tab_left
URxvt.keysym.Control-Right: tabbedex:move_tab_right
URxvt.keysym.Meta-F1: tabbedex:goto_tab_1
URxvt.keysym.Meta-F2: tabbedex:goto_tab_2
URxvt.keysym.Meta-F3: tabbedex:goto_tab_3
URxvt.keysym.Meta-F4: tabbedex:goto_tab_4
URxvt.keysym.Meta-F5: tabbedex:goto_tab_5
URxvt.keysym.Meta-F6: tabbedex:goto_tab_6
URxvt.keysym.Meta-F7: tabbedex:goto_tab_7
URxvt.keysym.Meta-F8: tabbedex:goto_tab_8
URxvt.keysym.Meta-F9: tabbedex:goto_tab_9
URxvt.keysym.Meta-F10: tabbedex:goto_tab_10
URxvt.keysym.Meta-F11: tabbedex:goto_tab_11
URxvt.keysym.Meta-F12: tabbedex:goto_tab_12
See I<ACTIONS AND USER COMMANDS> below for list of available actions.
=item B<perl-ext-blacklist>: I<string>
A comma-separated list of extensions that must not be loaded into the slave
terminals (tabs). tabbedex plugin is implicitly added onto the list.
=item B<reopen-on-close>: I<boolean>
If set, whenever last tab is destroyed a new one will be created.
=item B<tabbar-timeouts>: I<string>
When new text is written to a background tab, activity marks are displayed
around its number (or name if it has one) on the tab bar. By default Unicode
characters are used to display a block which grows with time the longer it was
since last time there was any activity in the tab.
This resource allows for this to be customised. It's format is
( <timeout> ":" <char> <char>? ":" )* <timeout> ":" <char> <char>? ":"
where <timeout> is timeout in seconds and <char> is an activity character. If
two characters are given, they specify left and right activity marks. If one
character is given, it specifies both. For example:
URxvt.tabbedex.tabbar-timeouts: 0:|:3:():6:[]:9:{}:12:<>
See B<tabbedex;ignore_line_activity> OSC sequence below to see how the activity
detection can be further affected.
=back
Extension's behaviour is also influenced by some of URxvt's configuration
options as well. (See I<RESOURCES> in the L<urxvt(1)> manpage for more
information about them). The options include:
=over
=item B<mapAlert>
If set, when bell rings in a background tab, the tab is selected as current.
=item B<urgentOnBell>
If set, when bell rings in a background tab, the master terminal's urgency hint
is set.
=back
=head1 ACTIONS AND USER COMMANDS
tabbedex supports actions which can be bound to keystrokes using
B<URxvt.keysym>.I<keysym> resource. For example:
URxvt.keysym.Control-t: tabbedex:new_tab
URxvt.keysym.Control-Tab: tabbedex:next_tab
URxvt.keysym.Control-Shift-Tab: tabbedex:prev_tab
! Shift-Tab is often bound to ISO_Left_Tab in X keyboard
! layouts. In those layouts Control-Shift-Tab won't
! work and Control-ISO_Left_Tab is needed instead:
URxvt.keysym.Control-ISO_Left_Tab: tabbedex:prev_tab
makes I<Ctrl+T> create a new tab and I<Ctrl+Tab> and I<Ctrl+Shift+Tab> switch
between existing tabs. Using the name of the extension as the prefix of the
keysym binding is preferred but when running an ancient urxvt (i.e. anything
prior to 9.21) an alternative B<perl:tabbedex> must be used instead.
Supported actions are:
=over
=item B<tabbedex:new_tab> and B<tabbedex:new_tab:>I<arguments>
Creates a new tab, puts it at the end of the tab list and switches to it. In
the second form, I<arguments> temporarily override value of the B<tab-arguments>
resource. This makes it possible to create bindings which start particular
commands, for example:
URxvt.keysym.F1: tabbedex:new_tab:-e info
makes it such that pressing F1 will open a new tab running B<info> command.
(Remember that I<arguments> are passed as arguments to urxvt so to run a command
B<-e> is required as in example above).
=item B<tabbedex:new_tab_>I<side>B<_this> and B<tabbedex:new_tab_>I<side>B<_this:>I<arguments>
Like B<tabbedex:new_tab> but puts the new tab before (if I<side> is B<before>)
or after (if I<side> is B<after>) the current tab on the tab list. For example,
URxvt.keysym.C-t: tabbedex:new_tab_after_this
makes I<Ctrl+T> start a new tab next to the current one.
=item B<tabbedex:next_tab> and B<tabbedex:prev_tab>
Switches to the tab on the right or left of the current tab. Wraps around when
switching right from the last tab or left from the first one.
=item B<tabbedex:next_tab:nowrap> and B<tabbedex:prev_tab:nowrap>
Switches tabs like B<tabbedex:next_tab> and B<tabbedex:prev_tab> action but does
not wrap around. I.e. does nothing if trying to switch to the last tab's next
tab or first tab's previous tab.
=item B<tabbedex:move_tab_left> and B<tabbedex:move_tab_right>
Moves the current tab left or right. Wraps around when moving last tab right or
first tab left.
=item B<tabbedex:move_tab_left:nowrap> and B<tabbedex:move_tab_right:nowrap>
Moves the current tab left or right like B<tabbedex:move_tab_left> and
B<tabbedex:move_tab_right> but does not wrap around. I.e. moving first tab left
or last tab right does nothing.
=item B<tabbedex:goto_tab_>I<N>
If I<N> is a positive integer, switches to the I<N>th tab. If I<N> is negative,
switches to the -I<N>th tab counting from the last one.
=item B<tabbedex:kill_tab>
Kills/destroys current tab.
=item B<tabbedex:rename_tab>
Start renaming the tab.
=back
=head1 OSC SEQUENCES
tabbedex supports two OSC sequences. They can be invoked by programs running in
the terminal by writing an OSC sequence I<ESC ] 777 ; string ST> where I<string>
is the command to execute. For example:
printf '\033]777;tabbedex;set_tab_name;%s\007' "foo"
=over
=item B<tabbedex;set_tab_name;>I<name>
Sets name of the current tab to I<name>.
=item B<tabbedex;ignore_line_activity;>I<n>
Prevents new text on given line affecting activity marks displayed in the tab
bar (see B<tabbar-timeouts> resource). Positive values specify I<n>th row from
the top, negative specify -I<n>th row from the bottom and zero disables the
filtering (thus making all updates affect activity marks).
This is useful if an application has a periodically-updating status line whose
refresh does not indicate activity in the tab. For example, L<screen(1)> can
display current time in its hardstatus. To prevent updates to the clock
registering as activity in the screen window, tabbedex can be instructed to
ignore the bottom row of the display:
printf '\033]777;tabbedex;ignore_line_activity;%d\007' -1
To make this automatic a custom script for starting L<screen(1)> could be used:
#!/bin/sh
ignore_line_activity() {
printf '\033]777;tabbedex;ignore_line_activity;%d\007' "$1"
}
ignore_line_activity -1
screen "$@"
ignore_line_activity 0
Note that if text wraps multiple lines, only the topmost row counts as changed.
=back
=head1 SEE ALSO
L<urxvt(1)>, L<urxvt-tabbed(1)>, L<tabbedex-command-runner(1)> and
L<tabbedex-pgid-cd(1)>
=head1 COPYRIGHT
Copyright 2006-2020 tabbed and tabbedex authors
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 3 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, see <http://www.gnu.org/licenses/>.
=head1 AUTHOR
tabbedex has been created (as a fork of L<urxvt-tabbed(1)>) and is maintained by
Michal Nazarewicz. For list of authors see B<AUTHORS> file distributed with the
extension. Send bug reports and comments to [email protected] or file them at
L<https://github.com/mina86/urxvt-tabbedex>.
=cut
use Scalar::Util;
use Text::ParseWords;
# An initialisation hook which is called in root/master and slave tabs. It’s
# purpose is to determine which one the object is and call that type’s
# initialisation code.
#
# The reason we do it this way rather than directly pointing urxvt at correct
# package is that we want urxvt to think that master and slave tabs both use
# ‘tabbedex’ extension. Previously, the code would point tabs to
# a ‘urxvt::ext::tabbedex::tab’ extension but that resulted in terminal thinking
# the extension is ‘tabbedex::tab’ (actually it was even more complicated than
# that) and actions weren’t dispatched correctly.
#
# Now, no matter if it’s root or not, from urxvt’s point of view it’s all
# ‘tebbedex’ plugin.
sub on_init {
my ($self) = @_;
# If we are root tab, then terminal’s 'tabbedex-root-tab' property is
# not set. Otherwise it is set and it points to the root tab.
my $root = delete $self->{term}{'tabbedex-root-tab'};
my $type = 'root';
if (defined $root) {
$type = 'tab';
$self->{root} = $root;
}
# Copy ISA so things behave the same way.
my $pkg = Scalar::Util::blessed($self);
@{"urxvt::ext::tabbedex::$type\::ISA"} = @{"$pkg\::ISA"};
# Change type of the object so methods in proper package are used.
bless $self, "urxvt::ext::tabbedex::$type";
# And continue initialisation.
my $supports_action = enable_action_hooks($self, !defined $root);
$self->enable_hooks;
$self->init;
if ($root->{register_keysyms}) {
register_keysyms($self, $supports_action);
}
()
}
sub register_keysyms {
my ($self, $supports_action) = @_;
my $prefix = ($supports_action && $self->{_name}) || 'perl:tabbedex';
$self->parse_keysym('Shift-Left', $prefix . ':prev_tab');
$self->parse_keysym('Shift-Right', $prefix . ':next_tab');
$self->parse_keysym('Shift-Down', $prefix . ':new_tab');
$self->parse_keysym('Shift-Up', $prefix . ':rename_tab');
$self->parse_keysym('Control-Left', $prefix . ':move_tab_left');
$self->parse_keysym('Control-Right', $prefix . ':move_tab_right');
for my $num (1..12) {
$self->parse_keysym('Meta-F' . $num,
$prefix . ':goto_tab_' . $num);
}
}
# Install handlers for ‘action’ and ‘user_command’ hooks. This subroutine means
# that root and tab packages should not touch those hooks themselves. This is
# here mostly because ancient urxvt errors out when trying to define ‘action’
# hook. As I didn’t want to duplicate handling of that, I moved it here where
# both packages are handled. And since ‘user_command’ is closely related, why
# not handle it here as well? A µoptimisation is that whatever way an action
# reaches the plugin, there’s just one hop till ‘tab_action’ is called.
sub enable_action_hooks {
my ($self, $isroot) = @_;
$self->enable(user_command => $isroot ? sub {
if ($_[1] =~ s/^tabbedex://) {
splice @_, 1, 0, $_[0]{cur};
goto \&urxvt::ext::tabbedex::root::tab_action;
}
} : sub {
if ($_[1] =~ s/^tabbedex://) {
unshift @_, $_[0]{root};
goto \&urxvt::ext::tabbedex::root::tab_action;
}
});
# Guard against ancient urxvts which do not support ‘action’ hook. We
# probably shouldn’t bother, but oh well…
eval {
$self->enable(action => $isroot ? sub {
splice @_, 1, 0, $_[0]{cur};
goto \&urxvt::ext::tabbedex::root::tab_action;
} : sub {
unshift @_, $_[0]{root};
goto \&urxvt::ext::tabbedex::root::tab_action;
});
1
} // 0
}
package urxvt::ext::tabbedex::root;
use Encode qw(decode);
{
my %hooks;
sub _on($&) {
my ($hook, $sub) = @_;
$hooks{$hook} = $sub
}
sub enable_hooks {
my ($root) = @_;
$root->enable(%hooks);
}
}
sub warn {
my $root = shift;
urxvt::warn(join '', $root->{_name} // 'tabbedex', ': ', @_, "\n");
}
sub tab_activity_marks {
my ($root, $tab, $now) = @_;
if (!defined($tab->{last_activity})) {
return (' ', ' ');
}
my $diff = $now - $tab->{last_activity};
my $prev;
for my $spec (@{ $root->{timeouts} }) {
my ($time, $left, $right) = @$spec;
if ($diff >= $time) {
my $next = defined $prev ? $prev - $diff : undef;
return ($left, $right, $next);
}
$prev = $time;
}
('!', '!') # we should never be here
}
# Returns a ($first_idx, $last_idx) list indicating index of first and last
# (inclusive) tab to be shown in the tabbar.
sub _tabbar_range {
my ($root, $ncol, $new_button, $names, $first_idx) = @_;
my $last_idx = $first_idx;
my $max = int(($ncol - 1) / 3) - 3;
$max = $max < 2 ? 1 : $max;
my $width = length $names->[$first_idx];
$ncol -= (($width < $max ? $width : $max) + 2 + # name and marks
($first_idx > 0 && !$new_button) + # left arrow
($last_idx < $#$names)); # right arrow
my $try_add = sub {
my $idx = shift;
if ($idx >= 0 && $idx <= $#$names) {
my $length = length $names->[$idx];
my $n = $ncol - (($length < $max ? $length : $max) + 2);
$n -= ($idx > 0) && ($idx < $#$names);
if ($n > 0) {
$ncol = $n;
return 1;
}
}
0
};
if ($try_add->($last_idx + 1)) {
++$last_idx;
}
while ($try_add->($first_idx - 1)) {
--$first_idx;
}
while ($try_add->($last_idx + 1)) {
++$last_idx;
}
($first_idx, $last_idx)
}
# Truncates names of the tabs so that all tabs visible in the tabbar (specified
# by the [$first_idx..$last_idx] range) can all fit. Tries to keep as much of
# the names of all tabs as possible.
sub _tabbar_truncate_names {
my ($root, $ncol, $new_button, $names, $first_idx, $last_idx) = @_;
$ncol -= (3 * ($last_idx - $first_idx) + 2 + # pipes and marks
($first_idx > 0 && !$new_button) + # left arrow
($last_idx < $#$names)); # right arrow
my $max = int($ncol / ($last_idx - $first_idx + 1));
my $cur_max = $max;
if ($max > 0 && $first_idx < $last_idx) {
my @lengths = sort { $b <=> $a }
grep { $_ <= $max ? (($ncol -= $_), 0) : 1 }
map length, @{ $names }[$first_idx .. $last_idx];
while (@lengths &&
$lengths[$#lengths] <= ($max = int($ncol / @lengths))) {
do {
$ncol -= pop @lengths;
} while (@lengths && $lengths[$#lengths] <= $max);
}
$cur_max = $max + $ncol - $max * @lengths;
}
for my $idx ($first_idx .. $last_idx) {
my $width = $root->{cur} == $root->{tabs}[$idx]
? $cur_max : $max;
if (length(my $name = $names->[$idx]) <= $width) {
# nop
} elsif ($width >= 2) {
$names->[$idx] = '…' . substr $name, -($width - 1);
} elsif ($width >= 1) {
$names->[$idx] = substr $name, -1;
} else { # extremely narrow window
$names->[$idx] = '';
}
}
}
# Determines layout of the tabbar. Returns a ($new_button, $first_idx,
# $last_idx, \@names) list where:
# - $new_button specifies text of the NEW button or is an empty string,
# - $first_idx is an index of the first visible tab,
# - $last_idx is an index of the last visible tab and
# - \@names is an array of (possibly truncated) names of the tabs.
sub _tabbar_layout {
my ($root) = @_;
my $ncol = $root->ncol;
my $new_button = '';
if ($root->{new_button}) {
if ($ncol >= 60) {
$new_button = '[NEW]';
} elsif ($ncol >= 30) {
$new_button = 'NEW';
} elsif ($ncol >= 13) {
$new_button = '+';
}
}
$ncol -= !!$new_button + length $new_button;
my $max = int(($ncol - 1) / 3) - 3;
$max = $max < 2 ? 1 : $max;
my ($total_truncated, $total_full, $cur_idx) = (-1, -1);
my @names = map {
my $tab = $root->{tabs}[$_];
if ($tab == $root->{cur}) {
$cur_idx = $_;
}
my $name = $tab->{name} || '' . ($_ + 1);
my $length = length $name;
$total_full += 3 + $length;
$total_truncated += 3 + ($length < $max ? $length : $max);
$name
} 0..$#{ $root->{tabs} };
my ($first_idx, $last_idx) = (0, $#names);
if ($ncol < $total_full) {
if ($ncol < $total_truncated) {
($first_idx, $last_idx) = $root->_tabbar_range(
$ncol, !!$new_button, \@names, $cur_idx);
}
$root->_tabbar_truncate_names(
$ncol, !!$new_button, \@names, $first_idx, $last_idx);
}
($new_button, $first_idx, $last_idx, \@names)
}
sub min {
@_ = grep { defined && $_ > 0 } @_;
my $best = shift;
for my $n (@_) {
if ($n < $best) {
$best = $n;
}
}
$best;
}
sub refresh {
my ($root, $now) = @_;
my $visible = (!$root->{autohide} ||
@{ $root->{tabs} } > 1 ||
$root->{cur}->is_being_renamed);
if ($visible != !!$root->{tabheight}) {
$root->{tabheight} = $visible * $root->{maxtabheight};
$root->configure;
$root->copy_properties;
}
if (!$visible) {
return;
}
my ($new_button, $first_idx, $last_idx, $names) = $root->_tabbar_layout;
my $bar = new urxvt::ext::tabbedex::tabbar($root);
if ($new_button) {
$bar->add_button($new_button, 1, -1);
}
if ($first_idx) {
$bar->add_arrow(-1);
}
if (!defined $now) {
$now = urxvt::NOW;
}
my $min_delay;
for my $idx ($first_idx .. $last_idx) {
my $tab = $root->{tabs}[$idx];
my ($left, $right, $delay) =
$root->tab_activity_marks($tab, $now);
$bar->add_button(
$left . $names->[$idx] . $right,
($tab == $root->{cur}) + 2 * ($now < $tab->{bell_ends}),
$idx);
if ($tab->is_being_renamed) {
$bar->put_cursor(-2);
}
$min_delay = min $min_delay, $delay, $tab->{bell_ends} - $now;
}
if ($last_idx < $#$names) {
$bar->add_arrow(1);
}
if ($root->{tab_title} && $bar->space_left > 0) {
my @str = $root->XGetWindowProperty(
$root->parent, $root->{tab_title});
if (@str && $str[2]) {
my $str = decode('utf8', $str[2]);
$bar->add_title($root->special_encode($str));
}
}
$bar->apply;
$root->want_refresh;
if (defined $min_delay) {
$root->{timer}->start($now + $min_delay);
}
}
sub tab_term_init {
my ($root, $term, $index) = @_;
$term->{'tabbedex-root-tab'} = $root;
$term->{'tabbedex-tab-index'} = $index;
for (0 .. urxvt::NUM_RESOURCES - 1) {
if (defined(my $value = $root->{resource}[$_])) {
$term->resource("+$_" => $value);
}
}
foreach my $key (values %urxvt::OPTION) {
my $val = exists $root->{option}{$key}
? $root->{option}{$key} : $root->option($key);
$term->option($key, $val);
}
if (defined(my $blacklist = $root->{perl_ext_blacklist})) {
$term->resource(perl_ext_2 =>
$term->resource('perl_ext_2') . $blacklist);
}
}
sub quote_words {
join ' ', map {
my $v = $_;
$v =~ s/'/'\\''/g;
"'$v'";
} @_
}
sub args_percent_substitution {
my ($root, $ch, $tab) = @_;
if ($ch eq '%') {
$ch
} elsif ($ch eq 'e') {
quote_words @{ $root->{argv} }
} elsif ($ch eq 'E') {
quote_words @{ $root->{argv} }
? @{ $root->{argv} }[1..$#{ $root->{argv} }]
: ($ENV{SHELL} || '/bin/sh')
} elsif ($ch eq 'p') {
$tab ? $tab->foreground_pgid : -1
} elsif ($ch eq 'n') {
$root->{total_tabs_open} // 0
} elsif ($ch eq '~') {
exists $ENV{'HOME'} ? quote_words $ENV{'HOME'} : '/'
} else {
$root->warn('unrecognised substitution in ‘tab-arguments’: ',
'‘%', $ch, '’; will substitute with empty string');
''
}
}
sub new_tab {
my ($root, $tab, $args, $index) = @_;
if (!defined $args) {
$args = $root->{tab_arguments};
}
$args =~ s/^\s+|\s+$//g;
if ($args eq '' || $args eq '%e') {
$args = $root->{argv};
} else {
$args =~ s/%(.)/
$root->args_percent_substitution($1, $tab)
/ge;
if (defined Text::ParseWords::shellwords($args)) {
$args = [Text::ParseWords::shellwords($args)];
} else {
$root->warn('error parsing ‘tab-arguments’ value: ‘',
$args, '’; ignoring');
$args = $root->{argv};
}
}
push @urxvt::TERM_INIT, sub { $root->tab_term_init($_[0], $index) };
push @urxvt::TERM_EXT, urxvt::ext::tabbedex::;
new urxvt::term
$root->env, $urxvt::RXVTNAME,
-embed => $root->parent,
@{ $args }
}
sub configure {
my ($root) = @_;
my $tab = $root->{cur};
# this is an extremely dirty way to force a configurenotify, but who cares
$tab->XMoveResizeWindow (
$tab->parent,
0, $root->{tabheight} + 1,
$root->width, $root->height - $root->{tabheight}
);
$tab->XMoveResizeWindow (
$tab->parent,
0, $root->{tabheight},
$root->width, $root->height - $root->{tabheight}
);
}
sub copy_properties {
my ($root) = @_;
my $tab = $root->{cur};
my $wm_normal_hints = $root->XInternAtom ("WM_NORMAL_HINTS");
my $current = delete $root->{current_properties};
# pass 1: copy over properties different or nonexisting
for my $atom ($tab->XListProperties ($tab->parent)) {
my ($type, $format, $items) = $root->XGetWindowProperty ($tab->parent,
$atom);
# fix up size hints
if ($atom == $wm_normal_hints && $root->{tabheight}) {
# typedef struct {
# long flags; /* 0 */
# int x, y; /* 1, 2 */ /* Obsolete */
# int width, height; /* 3, 4 */ /* Obsolete */
# int min_width, min_height; /* 5, 6 */
# int max_width, max_height; /* 7, 8 */
# int width_inc, height_inc; /* 9, 10 */
# struct {
# int x; /* numerator */
# int y; /* denominator */
# } min_aspect, max_aspect; /* 11, 12, 13, 14 */
# int base_width, base_height; /* 15, 16 */
# int win_gravity; /* 17 */
# } XSizeHints;
#
# (Don’t ask me how this works with long and int types. Somehow it
# does – mina86).
my (@hints) = unpack "l!*", $items;
$hints[$_] += $root->{tabheight} for (4, 6, 16);
$items = pack "l!*", @hints;
}
my $cur = delete $current->{$atom};
# update if changed, we assume empty items and zero type and
# format will not happen
$root->XChangeProperty ($root->parent, $atom, $type, $format, $items)
if $cur->[0] != $type or $cur->[1] != $format or $cur->[2] ne $items;
$root->{current_properties}{$atom} = [$type, $format, $items];
}
# pass 2, delete all extraneous properties
$root->XDeleteProperty ($root->parent, $_) for keys %$current;
}
sub make_current {
my ($root, $tab, $bell_ends) = @_;
if (!ref $tab) {
$tab = $root->{tabs}[$tab];
}
my $cur = $root->{cur};
if ($cur == $tab) {
return;
}
if ($cur) {
if ($cur->is_being_renamed) {
return;
}
$cur->enable_activity_hook(1);
delete $cur->{last_activity};
$cur->{bell_ends} = 0;
$cur->XUnmapWindow ($cur->parent) if $cur->mapped;
$cur->focus_out;
}
$root->{cur} = $tab;
# Disable the add_lines hook so that we don’t waste time processing that
# hook for the current tab. We’re not interested in that hook on the
# current tab.
$tab->enable_activity_hook(0);
$root->configure;
$root->copy_properties;
$tab->focus_out; # just in case, should be a nop
$tab->focus_in if $root->focus;
$tab->XMapWindow ($tab->parent);
delete $tab->{last_activity};
$tab->{bell_ends} = $bell_ends // 0;
$root->refresh;
}
_on focus_in => sub {
my ($root, $event) = @_;
$root->{cur}->focus_in;
();
};
_on focus_out => sub {
my ($root, $event) = @_;
$root->{cur}->focus_out;
();
};
_on tt_write => sub {
my ($root, $octets) = @_;
$root->{cur}->tt_write ($octets);
1