-
Notifications
You must be signed in to change notification settings - Fork 0
/
usr##lib##urxvt##perl##tabbedex
847 lines (692 loc) · 24.3 KB
/
usr##lib##urxvt##perl##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
#! perl
# 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/>.
#
##
## Tabbed plugin for rxvt-unicode
## Modified by Michal Nazarewicz (mina86/AT/mina86.com), StephenB
## (mail4stb/AT/gmail.com) and Steven Merrill
## <steven dot merrill at gmail.com>
##
## The following has been added:
##
## 1. Depending on time of last activity, activity character differs.
## By default, after 4 seconds an asterisk becomes a plus sing,
## after next 4 it becomes a colon, and finally, after another 8
## seconds it becomes a dot. This can be configured via
## tabbar-timeouts resource. It's format is:
##
## ( <timeout> ":" <character> ":" )* <timeout> ":" <character> ":"
##
## where <timeout> is timeout in seconds and <character> is
## a single activity character.
##
## 2. The "[NEW]" button can be disabled (who on Earth uses mouse to
## create new tab anyways?) by setting new-button resource to yes.
##
## 3. If title resource is true, tab's title is displayed after last
## button. This is handy if you have terminal with no window
## decorations. Colours can be configured via title-fg and
## title-bg.
##
## 4. Incorporated Alexey Semenko <asemenko at gmail.com> patch adding
## autohide resource. If it's true tab bar is hidden if there is
## no more then one tab opened.
##
## 5. Tabs are indexed in starting with zero hex. :] If you're such
## a geek to use urxvt it shouldn't be a problem for you and it
## saves few character when many tabs are opened.
##
## 6. As a minor modification: Final pipe character is removed (unless
## title is displayed). This make tab bar look nicer.
##
## Added by StephenB:
##
## 7. Tabs can be named with Shift+Up (Enter to confirm, Escape to
## cancel).
##
## 8. "[NEW]" button disabled by default.
##
## Added by Steven Merrill <steven dot merrill at gmail.com>
##
## 9. Ability to start a new tab or cycle through tabs via user
## commands: tabbedex:(new|next|prev)_tab .
## e.g. (in .Xdefaults) URxvt.keysym.M-t: perl:tabbedex:new_tab
## (see the urxvt man file for more info about keysym)
##
## 10. Fix an issue whereby on_user_command would not properly get sent
## to other extension packages if the mouse was not over the urxvt
## window.
##
## Added by Thomas Jost:
##
## 11. Add several user commands: tabbedex:rename_tab,
## tabbedex:move_tab_(left|right).
## e.g. (see 9.) URxvt.keysym.C-S-Left: perl:tabbex:move_tab_left
##
## 12. Ability to disable the default keybindings using the
## no-tabbedex-keys resource.
##
## Added by Jim Diamond <Jim dot Diamond at acadiau dot ca>
##
## 13. Work around one or more bugs in urxvt V 9.12 which cause tabbed
## terminals to open up too small and to ignore X geometry resources.
## These changes work for me with these choices:
## fvwm2, Slackware64 13.37, my modified (narrowed) NeXT scrollbars.
## I've tried a few other fonts, but the testing wasn't extensive.
## For other scrollbar widths, you need to modify a paramter; see the
## comments at the beginning of on_resize_all_windows().
## Also, I used some mystery offsets that worked for me. YMMV.
## (See the comments and 5 and 27.)
## NOTE: these changes work in 9.14 with either of the patches suggested
## on 2011/01/05 on the rxvt-unicode mailing list.
## NOTE: these changes work in 9.15 with no patches applied.
##
## 14. The previous version of on_start() threw away all of @argv up
## to the "-e" (if it exists). This tossed away arguments that I
## want, most notably "-ls" and "-geometry <geometry>. The code below
## passes through everything except "-pe <arg>" and geometry. If
## other options are found to be problematic they will need to be
## tossed too. The other args are saved for all tabs, which may or
## may not be desired in the case of
## urxvt -pe tabbedex -e <some command>
##
## 15. The name of the current tab can be changed with an escape sequence.
## For example, the shell command
## printf "\033]777;tabbedex;set_tab_name;work stuff\007"
## would change the name of the current tab to "work stuff".
##
## 16. Previously, if you had a lot of tabs and/or tabs with long names,
## the current tab might not show up in the tab bar (if its position
## in the tab name string was more than $self->ncol chars from the
## beginning). Changes below ensure that the name of the current tab
## is always visible. Arguably it would be nice to have indications
## at each end showing whether there are more tabs in that direction.
##
## 17. Previous versions did not autohide the tab bar if the remaining
## tab had a name (as opposed to an index number). I think "autohide"
## means "autohide", so I changed that behaviour. See the comment in
## update_autohide().
##
## 18. When printing the tab bar, limit the length of the tab names so that
## one tab name doesn't occupy the whole tab bar.
##
sub update_autohide {
my ($self, $reconfigure) = @_;
my $oldh = $self->{tabheight};
# JD modification: replace this
# if ($self->{autohide} && @{ $self->{tabs} } <= 1 &&
# ! (@{ $self->{tabs} } == 1 && $self->{tabs}[-1]->{name})) {
# with the following line. The old code did not autohide tabs
# which had been named, but I believe autohide means autohide.
if ($self->{autohide} && @{ $self->{tabs} } <= 1) {
$self->{tabheight} = 0;
} else {
$self->{tabheight} = $self->{maxtabheight};
}
if ($reconfigure && $self->{tabheight} != $oldh) {
$self->{needs_fixing} = 1;
$self->configure;
$self->on_resize_all_windows;
$self->copy_properties;
}
}
sub tab_activity_mark ($$) {
my ($self, $tab) = @_;
return ' ' unless defined $tab->{lastActivity};
return ' ' if $tab == $self->{cur};
if (defined $self->{timeouts}) {
my $diff = int urxvt::NOW - $tab->{lastActivity};
for my $spec (@{ $self->{timeouts} }) {
return $spec->[1] if $diff > $spec->[0];
}
}
'*';
}
sub refresh {
my ($self) = @_;
# autohide makes it zero
return unless $self->{tabheight};
# Note that the tab bar uses space above the scroll bar.
my $ncol = $self->ncol;
my $text = " " x $ncol;
my $rend = [($self->{rs_tabbar}) x $ncol];
my ($ofs, $idx, @ofs) = (0, 0);
if ($self->{new_button}) {
substr $text, 0, 7, "[NEW] |";
@$rend[0 .. 5] = ($self->{rs_tab}) x 6;
push @ofs, [0, 6, -1 ];
$ofs = 7;
}
# Limit the length of tab names.
# Subtract 5 from the available length so that the beginning of a
# following tab should be visable and thus clickable.
# This algorithm is heavy-handed; it would be nicer to just chop the
# longest names only when necessary.
my $space_available = $ofs;
my $max_len = int(($ncol - $ofs - 5) / ($#{ $self->{tabs} } > 1 ? 3 : 2));
# See if the name of the current tab would be displayed;
# if not, compute the number of tabs to skip so that the
# name of the current tab will be displayed.
my $tabs_to_skip = 0;
my $len = $ofs;
my @ends;
for my $tab (@{ $self->{tabs} }) {
my $name = substr($tab->{name} ? $tab->{name} : $idx, -$max_len);
$idx++;
$len += length($name) + 3; # '| ' and ' '
if ($tab == $self->{cur}) {
if ($len > $ncol + 1) {
for my $end (@ends) {
$tabs_to_skip++;
if ($len - $end <= $ncol) {
last;
}
}
}
last;
}
push @ends, $len;
}
$idx = 0;
for my $tab (@{ $self->{tabs} }) {
if ($idx >= $tabs_to_skip) {
my $name = substr($tab->{name} ? $tab->{name} : $idx, -$max_len);
my $act = $self->tab_activity_mark($tab);
my $txt = sprintf "%s%s%s", $act, $name, $act;
my $len = length $txt;
substr $text, $ofs, $len + 1, "$txt|";
@$rend[$ofs .. $ofs + $len - 1] = ($self->{rs_tab}) x $len
if $tab == $self->{cur};
push @ofs, [ $ofs, $ofs + $len, $idx ];
$ofs += $len + 1;
}
++$idx;
}
substr $text, --$ofs, 1, ' '; # remove last '|'
if ($self->{tab_title} && $ofs + 3 < $ncol) {
my $term = $self->{term};
my @str = $term->XGetWindowProperty($term->parent, $self->{tab_title});
if (@str && $str[2]) {
my $str = '| ' . $str[2];
my $len = length $str;
$len = $ncol - $ofs if $ofs + $len > $ncol;
substr $text, $ofs, $len, substr $str, 0, $len;
@$rend[$ofs + 2 .. $ofs + $len - 1] = ($self->{rs_title}) x ($len - 2);
}
}
$self->{tabofs} = \@ofs;
$self->ROW_t (0, $text, 0, 0, $ncol);
$self->ROW_r (0, $rend, 0, 0, $ncol);
$self->want_refresh;
}
sub new_tab {
my ($self, @argv) = @_;
my $offset = $self->fheight;
my $oldtabheight = $self->{tabheight};
$self->{tabheight} = $self->{maxtabheight}
unless $self->{autohide} && !(defined $self->{tabs} && @{ $self->{tabs} });
if ($oldtabheight != $self->{tabheight}) {
# We are about to use up a line for the tab bar.
# Stretch the (outer) window.
# Since the "needs fixing" code adds 1 to desired_rows
# if we have a tab we don't need to change it here.
$self->{needs_fixing} = 1;
$self->on_resize_all_windows;
}
# save a backlink to us, make sure tabbedex is inactive
push @urxvt::TERM_INIT, sub {
my ($term) = @_;
$term->{parent} = $self;
for (0 .. urxvt::NUM_RESOURCES - 1) {
my $value = $self->{resource}[$_];
$term->resource ("+$_" => $value)
if defined $value;
}
$term->resource (perl_ext_2 => $term->resource ("perl_ext_2") . ",-tabbedex");
};
push @urxvt::TERM_EXT, urxvt::ext::tabbedex::tab::;
my $term = new urxvt::term
$self->env, $urxvt::RXVTNAME,
-embed => $self->parent,
@argv;
}
sub configure {
my ($self) = @_;
my $tab = $self->{cur};
# this is an extremely dirty way to force a configurenotify, but who cares
$tab->XMoveResizeWindow (
$tab->parent,
0, $self->{tabheight} + 1,
$self->width, $self->height - $self->{tabheight}
);
$tab->XMoveResizeWindow (
$tab->parent,
0, $self->{tabheight},
$self->width, $self->height - $self->{tabheight}
);
}
sub copy_properties {
my ($self) = @_;
my $tab = $self->{cur};
my $wm_normal_hints = $self->XInternAtom ("WM_NORMAL_HINTS");
my $current = delete $self->{current_properties};
# pass 1: copy over properties different or nonexisting
for my $atom ($tab->XListProperties ($tab->parent)) {
my ($type, $format, $items) = $self->XGetWindowProperty ($tab->parent, $atom);
# fix up size hints
if ($atom == $wm_normal_hints) {
my (@hints) = unpack "l!*", $items;
$hints[$_] += $self->{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
$self->XChangeProperty ($self->parent, $atom, $type, $format, $items)
if $cur->[0] != $type or $cur->[1] != $format or $cur->[2] ne $items;
$self->{current_properties}{$atom} = [$type, $format, $items];
}
# pass 2, delete all extraneous properties
$self->XDeleteProperty ($self->parent, $_) for keys %$current;
}
sub make_current {
my ($self, $tab) = @_;
if (my $cur = $self->{cur}) {
delete $cur->{lastActivity};
$cur->XUnmapWindow ($cur->parent) if $cur->mapped;
$cur->focus_out;
}
$self->{cur} = $tab;
$self->configure;
$self->copy_properties;
$tab->focus_out; # just in case, should be a nop
$tab->focus_in if $self->focus;
$tab->XMapWindow ($tab->parent);
delete $tab->{lastActivity};
$self->refresh;
();
}
sub on_focus_in {
my ($self, $event) = @_;
$self->{cur}->focus_in;
();
}
sub on_focus_out {
my ($self, $event) = @_;
$self->{cur}->focus_out;
();
}
sub on_key_press {
my ($self, $event) = @_;
$self->{cur}->key_press ($event->{state}, $event->{keycode}, $event->{time});
1;
}
sub on_key_release {
my ($self, $event) = @_;
$self->{cur}->key_release ($event->{state}, $event->{keycode}, $event->{time});
1;
}
sub on_button_release {
my ($self, $event) = @_;
if ($event->{row} == 0) {
my $col = $event->{col};
for my $button (@{ $self->{tabofs} }) {
last if $col < $button->[0];
next unless $col <= $button->[1];
if ($button->[2] == -1) {
$self->new_tab(@{$self->{argv}});
} else {
$self->make_current($self->{tabs}[$button->[2]]);
}
}
return 1;
}
();
}
sub on_init {
my ($self) = @_;
# This captures X resources but not a command-line geometry specification.
my $geom = $self->x_resource("geometry");
$self->{initial_geometry} = $geom;
$self->{initial_int_bwidth} = $self->int_bwidth;
$self->{resource} = [map $self->resource ("+$_"), 0 .. urxvt::NUM_RESOURCES - 1];
$self->resource (int_bwidth => 0);
$self->resource (name => "URxvt.tabbed");
$self->resource (pty_fd => -1);
$self->option ($urxvt::OPTION{scrollBar}, 0);
my $fg = $self->x_resource ("tabbar-fg");
my $bg = $self->x_resource ("tabbar-bg");
my $tabfg = $self->x_resource ("tab-fg");
my $tabbg = $self->x_resource ("tab-bg");
my $titfg = $self->x_resource ("title-fg");
my $titbg = $self->x_resource ("title-bg");
defined $fg or $fg = 3;
defined $bg or $bg = 0;
defined $tabfg or $tabfg = 0;
defined $tabbg or $tabbg = 1;
defined $titfg or $titfg = 2;
defined $titbg or $titbg = 0;
$self->{rs_tabbar} = urxvt::SET_COLOR (urxvt::DEFAULT_RSTYLE, $fg + 2, $bg + 2);
$self->{rs_tab} = urxvt::SET_COLOR (urxvt::DEFAULT_RSTYLE, $tabfg + 2, $tabbg + 2);
$self->{rs_title} = urxvt::SET_COLOR (urxvt::DEFAULT_RSTYLE, $titfg + 2, $titbg + 2);
my $timeouts = $self->x_resource ("tabbar-timeouts");
$timeouts = '16:.:8:::4:+' unless defined $timeouts;
if ($timeouts ne '') {
my @timeouts;
while ($timeouts =~ /^(\d+):(.)(?::(.*))?$/) {
push @timeouts, [ int $1, $2 ];
$timeouts = defined $3 ? $3 : '';
}
if (@timeouts) {
$self->{timeouts} = [ sort { $b->[0] <=> $a-> [0] } @timeouts ];
}
}
$self->{new_button} =
($self->x_resource ('new-button') or 'false') !~ /^(?:false|0|no)/i;
$self->{tab_title} =
($self->x_resource ('title') or 'true') !~ /^(?:false|0|no)/i;
$self->{autohide} =
($self->x_resource ('autohide') or 'false') !~ /^(?:false|0|no)/i;
$self->{no_default_keys} =
($self->x_resource ('no-tabbedex-keys') or 'false') !~ /^(?:false|0|no)/i;
();
}
sub on_start {
my ($self) = @_;
# Save these now, for use on the first call to on_resize_all_windows().
$self->{desired_rows} = $self->{term}->nrow;
$self->{desired_cols} = $self->{term}->ncol;
$self->{needs_fixing} = 1;
$self->{maxtabheight} = $self->int_bwidth + $self->fheight + $self->lineSpace;
$self->{tabheight} = $self->{autohide} ? 0 : $self->{maxtabheight};
$self->{running_user_command} = 0;
$self->cmd_parse ("\033[?25l");
# JD: don't throw away all arguments, as original code did.
# Instead, let most of them through.
# Q: What shouldn't we pass through aside from "-pe <extensions>"?
my @argv = ();
my @tmp = $self->argv;
shift @tmp; # toss away argv[0] (== "urxvt" or similar).
while (@tmp != ()) {
if ($tmp[0] eq "-pe") {
shift @tmp; # toss away -pe, we later toss its arg.
} elsif ($tmp[0] eq "-geometry") {
shift @tmp;
$self->{initial_geometry} = $tmp[0];
} else {
push(@argv, $tmp[0]);
}
shift @tmp;
}
$self->{argv} = [ @argv ];
if ($self->{tab_title}) {
$self->{tab_title} = $self->{term}->XInternAtom("WM_NAME", 1);
}
$self->new_tab (@argv);
if (defined $self->{timeouts}) {
my $interval = ($self->{timeouts}[@{ $self->{timeouts} } - 1]->[0]);
$interval = int($interval / 4);
$self->{timer} = urxvt::timer->new
->interval($interval < 1 ? 1 : $interval)
->cb ( sub { $self->refresh; } );
}
();
}
sub on_configure_notify {
my ($self, $event) = @_;
$self->configure;
$self->refresh;
();
}
sub on_user_command {
my ($self, $event) = @_;
$self->{cur}->{term}->{parent}->tab_user_command($self->{cur}, $event, 1);
();
}
sub on_wm_delete_window {
my ($self) = @_;
$_->destroy for @{ $self->{tabs} };
1;
}
sub tab_start {
my ($self, $tab) = @_;
$tab->XChangeInput ($tab->parent, urxvt::PropertyChangeMask);
push @{ $self->{tabs} }, $tab;
# $tab->{name} ||= scalar @{ $self->{tabs} };
$self->make_current ($tab);
();
}
sub tab_destroy {
my ($self, $tab) = @_;
$self->{tabs} = [ grep $_ != $tab, @{ $self->{tabs} } ];
$self->update_autohide (1);
if (@{ $self->{tabs} }) {
if ($self->{cur} == $tab) {
delete $self->{cur};
$self->make_current ($self->{tabs}[-1]);
} else {
$self->refresh;
}
} else {
# delay destruction a tiny bit
$self->{destroy} = urxvt::iw->new->start->cb (sub { $self->destroy });
}
();
}
sub tab_key_press {
my ($self, $tab, $event, $keysym, $str) = @_;
if ($tab->{is_inputting_name}) {
if ($keysym == 0xff0d || $keysym == 0xff8d) { # enter
$tab->{name} = $tab->{new_name};
$tab->{is_inputting_name} = 0;
$self->update_autohide (1);
} elsif ($keysym == 0xff1b) { # escape
$tab->{name} = $tab->{old_name};
$tab->{is_inputting_name} = 0;
$self->update_autohide (1);
} elsif ($keysym == 0xff08) { # backspace
substr $tab->{new_name}, -1, 1, "";
$tab->{name} = "$tab->{new_name}█";
} elsif ($str !~ /[\x00-\x1f\x80-\xaf]/) {
$tab->{new_name} .= $str;
$tab->{name} = "$tab->{new_name}█";
}
$self->refresh;
return 1;
}
return () if ($self->{no_default_keys});
if ($event->{state} & urxvt::ShiftMask) {
if ($keysym == 0xff51 || $keysym == 0xff53) {
if (@{ $self->{tabs} } > 1) {
$self->change_tab($tab, $keysym - 0xff52);
}
return 1;
} elsif ($keysym == 0xff54) {
$self->new_tab(@{$self->{argv}});
return 1;
} elsif ($keysym == 0xff52) {
$self->rename_tab($tab);
return 1;
}
} elsif ($event->{state} & urxvt::ControlMask) {
if ($keysym == 0xff51 || $keysym == 0xff53) {
$self->move_tab($tab, $keysym - 0xff52);
return 1;
}
}
();
}
sub tab_property_notify {
my ($self, $tab, $event) = @_;
$self->copy_properties
if $event->{window} == $tab->parent;
();
}
sub tab_add_lines {
my ($self, $tab) = @_;
my $mark = $self->tab_activity_mark($tab);
$tab->{lastActivity} = int urxvt::NOW;
$self->refresh if $mark ne $self->tab_activity_mark($tab);
();
}
sub tab_user_command {
my ($self, $tab, $cmd, $proxy_events) = @_;
if ($cmd eq 'tabbedex:new_tab') {
$self->new_tab(@{$self->{argv}});
}
elsif ($cmd eq 'tabbedex:next_tab') {
$self->change_tab($tab, 1);
}
elsif ($cmd eq 'tabbedex:prev_tab') {
$self->change_tab($tab, -1);
}
elsif ($cmd eq 'tabbedex:move_tab_left') {
$self->move_tab($tab, -1);
}
elsif ($cmd eq 'tabbedex:move_tab_right') {
$self->move_tab($tab, 1);
}
elsif ($cmd eq 'tabbedex:rename_tab') {
$self->rename_tab($tab);
}
else {
# Proxy the user command through to the tab's term, while taking care not
# to get caught in an infinite loop.
if ($proxy_events && $self->{running_user_command} == 0) {
$self->{running_user_command} = 1;
urxvt::invoke($tab->{term}, 20, $cmd);
$self->{running_user_command} = 0;
}
}
();
}
sub change_tab {
my ($self, $tab, $direction) = @_;
my $idx = 0;
++$idx while $self->{tabs}[$idx] != $tab;
$idx += $direction;
$self->make_current ($self->{tabs}[$idx % @{ $self->{tabs}}]);
();
}
sub move_tab {
my ($self, $tab, $direction) = @_;
if (@{ $self->{tabs} } > 1) {
my $idx1 = 0;
++$idx1 while $self->{tabs}[$idx1] != $tab;
my $idx2 = ($idx1 + $direction) % @{ $self->{tabs} };
($self->{tabs}[$idx1], $self->{tabs}[$idx2]) =
($self->{tabs}[$idx2], $self->{tabs}[$idx1]);
$self->make_current ($self->{tabs}[$idx2]);
}
();
}
sub rename_tab {
my ($self, $tab) = @_;
$tab->{is_inputting_name} = 1;
$tab->{old_name} = $tab->{name} ? $tab->{name} : "";
$tab->{new_name} = "";
$tab->{name} = "█"; # \u2588 in original code, that doesn't work for JD.
$self->update_autohide (1);
$self->refresh;
();
}
sub on_resize_all_windows {
my ($self, $width, $height) = @_;
# In urxvt-9.12, the size as reported by $self->width apparently
# includes the space used by the scroll bar and other padding.
# I need to figure out how to calculate that in a robust way.
# But for my personal version of the NeXT scrollbars, the following
# seems to properly calculate the number of columns of text that
# are missing.
# (12 is the magic number I need to calculate. This is apparently
# the width of the scroll bar. Maybe.)
# TODO: take lineSpace and letterSpace into account.
my $sb_width = 12;
my $border_pixels = 2 * $self->{initial_int_bwidth} + $self->ext_bwidth;
my $cols_eaten_by_sb = int(($sb_width + $border_pixels + $self->fwidth - 1)
/ $self->fwidth);
if ($self->{needs_fixing}) {
my $selfterm = $self->{term};
my $selfparent = $self->parent;
my $rootwindow = $selfterm->DefaultRootWindow;
my $x;
my $y;
$self->{needs_fixing} = 0;
# Compute the diffs between what it should be and what it is.
my $row_delta = $self->{desired_rows} - $self->{term}->nrow;
if ($self->{tabheight} > 0) {
$row_delta += 1;
}
my $col_delta = $self->{desired_cols} - $self->{term}->ncol
+ $cols_eaten_by_sb;
if ($row_delta != 0 || $col_delta != 0) {
my $wid_delta = $col_delta * $self->fwidth;
my $ht_delta = $row_delta * $self->fheight;
# This only works for geometry specs whose X offset is of the form
# +\d++\d+
if ($self->{initial_geometry} =~ m/.*\+(.*)\+(.*)/) {
$x = $1 + 5; # Why + 5? Dunno.
$y = $2 + 27; # Ditto.
} else {
($x, $y)
= $selfterm->XTranslateCoordinates($selfparent, $rootwindow,
0, 0);
}
# We will never want this again. Lose it.
$self->{initial_geometry} = "";
$self->XMoveResizeWindow($selfparent, $x, $y,
$self->width + $wid_delta,
$self->height + $ht_delta);
}
}
else {
# The window changed size, (manually or because the tab bar was added).
# Record the new "desired" size.
$self->{desired_cols} = $self->{term}->ncol - $cols_eaten_by_sb;
$self->{desired_rows} = $self->{term}->nrow;
if ($self->{tabheight} > 0) {
$self->{desired_rows} -= 1;
}
}
1;
}
# At least in urxvt 9.12:
# arg2 is /urxvt::ext::tabbedex::tab=HASH(0x20625b0)/ ( == $self->{tab} )
# arg3 is the actual OSC argument
# Some other modules (at least overlay_osc) get the OSC argument in arg2
# and nothing in arg3.
sub tab_osc_seq_perl {
my ($self, $tab, $osc) = @_;
return unless $osc =~ s/^tabbedex;//;
$osc =~ s/^([^;]+)+;//
or return;
if ($1 eq "set_tab_name") {
my ($new_name) = split /;/, $osc, 1;
$tab->{name} = $new_name;
$self->refresh;
}
1;
}
package urxvt::ext::tabbedex::tab;
# helper extension implementing the subwindows of a tabbed terminal.
# simply proxies all interesting calls back to the tabbedex class.
{
for my $hook (qw(osc_seq_perl start destroy user_command key_press property_notify add_lines)) {
eval qq{
sub on_$hook {
my \$parent = \$_[0]{term}{parent}
or return;
\$parent->tab_$hook (\@_)
}
};
die if $@;
}
}