updated on Thu Jan 26 16:09:46 UTC 2012
[aur-mirror.git] / rxvt-unicode-cvs / tabbed.pl
blob109aa11f1511289ba73a7af771f3cb4c9dd13ace
1 #!perl
3 sub refresh {
4 my ($self) = @_;
6 my $ncol = $self->ncol;
8 my $text = " " x $ncol;
9 my $rend = [($self->{rs_tabbar}) x $ncol];
11 my @ofs;
13 substr $text, 0, 1, "|";
14 push @ofs, [0, 1, sub { $_[0]->new_tab }];
16 my $ofs = 1;
17 for my $tab (@{ $self->{tabs} }) {
18 my $act = $tab->{activity} && $tab != $self->{cur} ? "*" : " ";
19 my $txt = "$act$tab->{name}$act";
20 my $len = length $txt;
22 substr $text, $ofs, $len + 1, "$txt|";
23 @$rend[$ofs .. $ofs + $len - 1] = ($self->{rs_tab}) x $len
24 if $tab == $self->{cur};
25 push @ofs, [ $ofs, $ofs + $len, sub { $_[0]->make_current ($tab) } ];
27 $ofs += $len + 1;
30 $self->{tabofs} = \@ofs;
32 $self->ROW_t ($self->nrow - 1, $text, 0, 0, $ncol);
33 $self->ROW_r ($self->nrow - 1, $rend, 0, 0, $ncol);
35 $self->want_refresh;
38 sub new_tab {
39 my ($self, @argv) = @_;
41 my $offset = $self->fheight;
43 push @urxvt::TERM_INIT, sub {
44 my ($term) = @_;
45 $term->{parent} = $self;
47 for (0 .. urxvt::NUM_RESOURCES - 1) {
48 my $value = $self->{resource}[$_];
50 $term->resource ("+$_" => $value)
51 if defined $value;
54 $term->resource (perl_ext_2 => $term->resource ("perl_ext_2") . ",-tabbed");
58 push @urxvt::TERM_EXT, urxvt::ext::tabbed::tab::;
60 my $term = new urxvt::term
61 $self->env, $urxvt::RXVTNAME,
62 -embed => $self->parent,
63 @argv,
67 sub configure {
68 my ($self) = @_;
70 my $tab = $self->{cur};
72 $tab->XMoveResizeWindow (
73 $tab->parent,
74 0, 1,
75 $self->width, $self->height - (@{ $self->{tabs} } > 1 ? $self->{tabheight} : 0)
77 $tab->XMoveResizeWindow (
78 $tab->parent,
79 0, 0,
80 $self->width, $self->height - (@{ $self->{tabs} } > 1 ? $self->{tabheight} : 0)
84 sub on_resize_all_windows {
85 my ($self, $width, $height) = @_;
90 sub copy_properties {
91 my ($self) = @_;
92 my $tab = $self->{cur};
94 my $wm_normal_hints = $self->XInternAtom ("WM_NORMAL_HINTS");
96 my $current = delete $self->{current_properties};
98 for my $atom ($tab->XListProperties ($tab->parent)) {
99 my ($type, $format, $items) = $self->XGetWindowProperty ($tab->parent, $atom);
101 if ($atom == $wm_normal_hints && @{ $self->{tabs} } > 1) {
102 my (@hints) = unpack "l!*", $items;
104 $hints[$_] += $self->{tabheight} for (4, 6, 16);
106 $items = pack "l!*", @hints;
109 my $cur = delete $current->{$atom};
111 $self->XChangeProperty ($self->parent, $atom, $type, $format, $items)
112 if $cur->[0] != $type or $cur->[1] != $format or $cur->[2] ne $items;
114 $self->{current_properties}{$atom} = [$type, $format, $items];
117 $self->XDeleteProperty ($self->parent, $_) for keys %$current;
120 sub make_current {
121 my ($self, $tab) = @_;
123 if (my $cur = $self->{cur}) {
124 delete $cur->{activity};
125 $cur->XUnmapWindow ($cur->parent) if $cur->mapped;
126 $cur->focus_out;
129 $self->{cur} = $tab;
131 $self->configure;
132 $self->copy_properties;
134 $tab->focus_out;
135 $tab->focus_in if $self->focus;
137 $tab->XMapWindow ($tab->parent);
138 delete $tab->{activity};
139 $self->refresh;
144 sub on_focus_in {
145 my ($self, $event) = @_;
147 $self->{cur}->focus_in;
152 sub on_focus_out {
153 my ($self, $event) = @_;
155 $self->{cur}->focus_out;
160 sub on_key_press {
161 my ($self, $event) = @_;
163 $self->{cur}->key_press ($event->{state}, $event->{keycode}, $event->{time});
168 sub on_key_release {
169 my ($self, $event) = @_;
171 $self->{cur}->key_release ($event->{state}, $event->{keycode}, $event->{time});
176 sub on_button_press {
180 sub on_button_release {
181 my ($self, $event) = @_;
183 if ($event->{row} == $self->nrow - 1) {
184 for my $button (@{ $self->{tabofs} }) {
185 $button->[2]->($self, $event)
186 if $event->{col} >= $button->[0]
187 && $event->{col} < $button->[1];
194 sub on_motion_notify {
198 sub on_init {
199 my ($self) = @_;
201 $self->{resource} = [map $self->resource ("+$_"), 0 .. urxvt::NUM_RESOURCES - 1];
203 $self->resource (int_bwidth => 0);
204 $self->resource (name => "URxvt.tabbed");
205 $self->resource (pty_fd => -1);
207 $self->option ($urxvt::OPTION{scrollBar}, 0);
209 my $fg = $self->x_resource ("tabbar-fg");
210 my $bg = $self->x_resource ("tabbar-bg");
211 my $tabfg = $self->x_resource ("tab-fg");
212 my $tabbg = $self->x_resource ("tab-bg");
214 defined $fg or $fg = -2;
215 defined $bg or $bg = -1;
216 defined $tabfg or $tabfg = -1;
217 defined $tabbg or $tabbg = -2;
219 $self->{rs_tabbar} = urxvt::SET_COLOR (urxvt::DEFAULT_RSTYLE, $fg + 2, $bg + 2);
220 $self->{rs_tab} = urxvt::SET_COLOR (urxvt::DEFAULT_RSTYLE, $tabfg + 2, $tabbg + 2);
225 sub on_start {
226 my ($self) = @_;
228 $self->{tabheight} = $self->int_bwidth + $self->fheight + $self->lineSpace;
230 $self->cmd_parse ("\033[?25l");
232 my @argv = $self->argv;
234 do {
235 shift @argv;
236 } while @argv && $argv[0] ne "-e";
238 $self->new_tab (@argv);
243 sub on_configure_notify {
244 my ($self, $event) = @_;
246 $self->configure;
247 $self->refresh;
252 sub on_wm_delete_window {
253 my ($self) = @_;
255 $self->{destroy} = urxvt::iw->new->start->cb (sub { $self->destroy });
256 $_->destroy for @{ $self->{tabs} };
261 sub tab_start {
262 my ($self, $tab) = @_;
264 $tab->XChangeInput ($tab->parent, urxvt::PropertyChangeMask);
266 push @{ $self->{tabs} }, $tab;
268 $tab->{name} ||= @{ $self->{tabs} } - 1;
269 my @t = reverse sort { $a->{name} <=> $b->{name} } @{ $self->{tabs} };
270 $tab->{name} = $t[0]->{name} + 1;
271 $self->make_current ($tab);
276 sub tab_destroy {
277 my ($self, $tab) = @_;
279 my ($idx) = grep $self->{tabs}[$_] == $tab, 0 .. $#{ $self->{tabs} };
280 $idx = -1 if $idx == @{ $self->{tabs} } - 1;
281 $self->{tabs} = [ grep $_ != $tab, @{ $self->{tabs} } ];
283 if (@{ $self->{tabs} }) {
284 if ($self->{cur} == $tab) {
285 delete $self->{cur};
286 $self->make_current ($self->{tabs}[$idx]);
287 } else {
288 $self->refresh;
290 } else {
291 $self->new_tab if ! $self->{destroy};
297 sub tab_key_press {
298 my ($self, $tab, $event, $keysym, $str) = @_;
300 if ($event->{state} & urxvt::ShiftMask) {
301 if ($keysym == 0xff51 || $keysym == 0xff53) {
302 my ($idx) = grep $self->{tabs}[$_] == $tab, 0 .. $#{ $self->{tabs} };
304 --$idx if $keysym == 0xff51;
305 ++$idx if $keysym == 0xff53;
307 $self->make_current ($self->{tabs}[$idx % @{ $self->{tabs}}]);
309 return 1;
310 } elsif ($keysym == 0xff54) {
311 $self->new_tab;
313 return 1;
314 } elsif ($keysym == 0xff52 && @{ $self->{tabs} } == 1) {
315 $tab->{name} = 1;
317 return 1;
319 } elsif ($event->{state} & urxvt::ControlMask) {
320 if ($keysym == 0xff51 || $keysym == 0xff53) {
321 my ($idx1) = grep $self->{tabs}[$_] == $tab, 0 .. $#{ $self->{tabs} };
322 my $idx2 = ($idx1 + ($keysym == 0xff51 ? -1 : +1)) % @{ $self->{tabs} };
324 ($self->{tabs}[$idx1], $self->{tabs}[$idx2]) =
325 ($self->{tabs}[$idx2], $self->{tabs}[$idx1]);
327 $self->make_current ($self->{tabs}[$idx2]);
329 return 1;
336 sub tab_property_notify {
337 my ($self, $tab, $event) = @_;
339 $self->copy_properties
340 if $event->{window} == $tab->parent;
345 sub tab_activity {
346 my ($self, $tab) = @_;
348 $self->refresh;
351 package urxvt::ext::tabbed::tab;
354 for my $hook qw(start destroy key_press property_notify) {
355 eval qq{
356 sub on_$hook {
357 my \$parent = \$_[0]{term}{parent}
358 or return;
359 \$parent->tab_$hook (\@_)
362 die if $@;
366 sub on_add_lines {
367 $_[0]->{activity}++
368 or $_[0]{term}{parent}->tab_activity ($_[0]);