1 |
#! perl |
2 |
|
3 |
sub refresh { |
4 |
my ($self) = @_; |
5 |
|
6 |
my $ncol = $self->ncol; |
7 |
|
8 |
my $text = " " x $ncol; |
9 |
my $rend = [(urxvt::DEFAULT_RSTYLE | urxvt::RS_RVid) x $ncol]; |
10 |
|
11 |
my @ofs; |
12 |
|
13 |
substr $text, 0, 7, "[NEW] |"; |
14 |
@$rend[0 .. 5] = (urxvt::OVERLAY_RSTYLE) x 6; |
15 |
push @ofs, [0, 6, sub { $_[0]->new_tab }]; |
16 |
|
17 |
my $ofs = 7; |
18 |
my $idx = 0; |
19 |
|
20 |
for my $tab (@{ $self->{tabs} }) { |
21 |
$idx++; |
22 |
|
23 |
my $act = $tab->{activity} && $tab != $self->{cur} |
24 |
? "*" : " "; |
25 |
|
26 |
my $txt = "$act$idx$act"; |
27 |
my $len = length $txt; |
28 |
|
29 |
substr $text, $ofs, $len + 1, "$txt|"; |
30 |
@$rend[$ofs .. $ofs + $len - 1] = (urxvt::OVERLAY_RSTYLE) x $len |
31 |
if $tab == $self->{cur}; |
32 |
|
33 |
push @ofs, [ $ofs, $ofs + $len, sub { $_[0]->make_current ($tab) } ]; |
34 |
|
35 |
$ofs += $len + 1; |
36 |
} |
37 |
|
38 |
$self->{tabofs} = \@ofs; |
39 |
|
40 |
$self->ROW_t (0, $text, 0, 0, $ncol); |
41 |
$self->ROW_r (0, $rend, 0, 0, $ncol); |
42 |
|
43 |
$self->want_refresh; |
44 |
} |
45 |
|
46 |
sub new_tab { |
47 |
my ($self) = @_; |
48 |
|
49 |
my $offset = $self->fheight; |
50 |
|
51 |
# save a backlink to us, make sure tabbed is inactive |
52 |
push @urxvt::TERM_INIT, sub { |
53 |
my ($term) = @_; |
54 |
$term->{parent} = $self; |
55 |
|
56 |
$term->resource ($_->[0] => $_->[1]) |
57 |
for @{ $self->{resource} || [] }; |
58 |
|
59 |
$term->resource (perl_ext_2 => $term->resource ("perl_ext_2") . ",-tabbed"); |
60 |
|
61 |
}; |
62 |
|
63 |
push @urxvt::TERM_EXT, urxvt::ext::tabbed::tab::; |
64 |
|
65 |
my $term = new urxvt::term |
66 |
$self->env, $urxvt::RXVTNAME, |
67 |
-embed => $self->parent, |
68 |
; |
69 |
} |
70 |
|
71 |
sub configure { |
72 |
my ($self) = @_; |
73 |
|
74 |
my $tab = $self->{cur}; |
75 |
|
76 |
$tab->XMoveResizeWindow ( |
77 |
$tab->parent, |
78 |
0, $self->{tabheight}, |
79 |
$self->width, $self->height - $self->{tabheight} |
80 |
); |
81 |
|
82 |
my $wm_normal_hints = $self->XInternAtom ("WM_NORMAL_HINTS"); |
83 |
|
84 |
for my $atom ($tab->XListProperties ($tab->parent)) { |
85 |
my ($type, $format, $items) = $self->XGetWindowProperty ($tab->parent, $atom); |
86 |
|
87 |
if ($atom == $wm_normal_hints) { |
88 |
my (@hints) = unpack "l!*", $items; |
89 |
|
90 |
$hints[$_] += $self->{tabheight} for (4, 6, 16); |
91 |
|
92 |
$items = pack "l!*", @hints; |
93 |
} |
94 |
$self->XChangeWindowProperty ($self->parent, $atom, $type, $format, $items); |
95 |
} |
96 |
|
97 |
$self->refresh; |
98 |
} |
99 |
|
100 |
sub make_current { |
101 |
my ($self, $tab) = @_; |
102 |
|
103 |
if (my $cur = $self->{cur}) { |
104 |
delete $cur->{activity}; |
105 |
$cur->XUnmapWindow ($cur->parent) if $cur->mapped; |
106 |
$cur->focus_out; |
107 |
} |
108 |
|
109 |
$self->{cur} = $tab; |
110 |
|
111 |
$self->configure; |
112 |
$tab->focus_in; |
113 |
$tab->XMapWindow ($tab->parent); |
114 |
$self->refresh; |
115 |
delete $tab->{activity}; |
116 |
|
117 |
() |
118 |
} |
119 |
|
120 |
sub on_button_press { |
121 |
1 |
122 |
} |
123 |
|
124 |
sub on_button_release { |
125 |
my ($self, $event) = @_; |
126 |
|
127 |
if ($event->{row} == 0) { |
128 |
for my $button (@{ $self->{tabofs} }) { |
129 |
$button->[2]->($self, $event) |
130 |
if $event->{col} >= $button->[0] |
131 |
&& $event->{col} < $button->[1]; |
132 |
} |
133 |
} |
134 |
|
135 |
1 |
136 |
} |
137 |
|
138 |
sub on_motion_notify { |
139 |
1 |
140 |
} |
141 |
|
142 |
sub on_init { |
143 |
my ($self) = @_; |
144 |
|
145 |
for (qw(name perl_ext_1 perl_ext_2)) { |
146 |
my $val = $self->resource ($_); |
147 |
|
148 |
push @{ $self->{resource} }, [$_ => $val] |
149 |
if defined $val; |
150 |
} |
151 |
|
152 |
$self->resource (int_bwidth => 0); |
153 |
$self->resource (name => "URxvt.tab"); |
154 |
$self->resource (pty_fd => -1); |
155 |
|
156 |
$self->option ($urxvt::OPTION{scrollBar}, 0); |
157 |
|
158 |
() |
159 |
} |
160 |
|
161 |
sub on_start { |
162 |
my ($self) = @_; |
163 |
|
164 |
$self->{tabheight} = $self->int_bwidth + $self->fheight + $self->lineSpace; |
165 |
|
166 |
$self->new_tab; |
167 |
|
168 |
() |
169 |
} |
170 |
|
171 |
sub on_configure_notify { |
172 |
my ($self, $event) = @_; |
173 |
|
174 |
$self->configure; |
175 |
|
176 |
() |
177 |
} |
178 |
|
179 |
sub on_wm_delete_window { |
180 |
my ($self) = @_; |
181 |
|
182 |
$_->destroy for @{ $self->{tabs} }; |
183 |
|
184 |
1 |
185 |
} |
186 |
|
187 |
sub tab_start { |
188 |
my ($self, $tab) = @_; |
189 |
|
190 |
push @{ $self->{tabs} }, $tab; |
191 |
|
192 |
# $tab->{name} ||= scalar @{ $self->{tabs} }; |
193 |
$self->make_current ($tab); |
194 |
|
195 |
() |
196 |
} |
197 |
|
198 |
sub tab_destroy { |
199 |
my ($self, $tab) = @_; |
200 |
|
201 |
$self->{tabs} = [ grep $_ != $tab, @{ $self->{tabs} } ]; |
202 |
|
203 |
if (@{ $self->{tabs} }) { |
204 |
if ($self->{cur} == $tab) { |
205 |
delete $self->{cur}; |
206 |
$self->make_current ($self->{tabs}[-1]); |
207 |
} else { |
208 |
$self->refresh; |
209 |
} |
210 |
} else { |
211 |
# delay destruction a tiny bit |
212 |
$self->{destroy} = urxvt::iw->new->start->cb (sub { $self->destroy }); |
213 |
} |
214 |
|
215 |
() |
216 |
} |
217 |
|
218 |
sub tab_key_press { |
219 |
my ($self, $tab, $event, $keysym, $str) = @_; |
220 |
|
221 |
if ($event->{state} & urxvt::ShiftMask |
222 |
&& ($keysym == 0xff51 || $keysym == 0xff53)) { |
223 |
my ($idx) = grep $self->{tabs}[$_] == $tab, 0 .. $#{ $self->{tabs} }; |
224 |
|
225 |
--$idx if $keysym == 0xff51; |
226 |
++$idx if $keysym == 0xff53; |
227 |
|
228 |
$self->make_current ($self->{tabs}[$idx % @{ $self->{tabs}}]); |
229 |
$self->refresh; |
230 |
|
231 |
return 1; |
232 |
} |
233 |
|
234 |
() |
235 |
} |
236 |
|
237 |
sub tab_activity { |
238 |
my ($self, $tab) = @_; |
239 |
|
240 |
$self->refresh; |
241 |
} |
242 |
|
243 |
package urxvt::ext::tabbed::tab; |
244 |
|
245 |
# helper extension implementing the subwindows of a tabbed terminal. |
246 |
# simply proxies all interesting calls back to the tabbed class. |
247 |
|
248 |
{ |
249 |
for my $hook qw(start destroy key_press) { |
250 |
eval qq{ |
251 |
sub on_$hook { |
252 |
my \$parent = \$_[0]{term}{parent} |
253 |
or return; |
254 |
\$parent->tab_$hook (\@_) |
255 |
} |
256 |
}; |
257 |
die if $@; |
258 |
} |
259 |
} |
260 |
|
261 |
sub on_add_lines { |
262 |
$_[0]->{activity}++ |
263 |
or $_[0]{term}{parent}->tab_activity ($_[0]); |
264 |
() |
265 |
} |
266 |
|
267 |
|