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} - 1, |
79 |
$self->width, $self->height - $self->{tabheight} |
80 |
); |
81 |
$tab->XMoveResizeWindow ( |
82 |
$tab->parent, |
83 |
0, $self->{tabheight}, |
84 |
$self->width, $self->height - $self->{tabheight} |
85 |
); |
86 |
} |
87 |
|
88 |
sub copy_properties { |
89 |
my ($self) = @_; |
90 |
my $tab = $self->{cur}; |
91 |
|
92 |
my $wm_normal_hints = $self->XInternAtom ("WM_NORMAL_HINTS"); |
93 |
|
94 |
my %our_props = map +($_ => undef), $self->XListProperties ($self->parent); |
95 |
|
96 |
for my $atom ($tab->XListProperties ($tab->parent)) { |
97 |
my ($type, $format, $items) = $self->XGetWindowProperty ($tab->parent, $atom); |
98 |
|
99 |
delete $our_props{$atom}; |
100 |
|
101 |
if ($atom == $wm_normal_hints) { |
102 |
my (@hints) = unpack "l!*", $items; |
103 |
|
104 |
$hints[$_] += $self->{tabheight} for (4, 6, 16); |
105 |
|
106 |
$items = pack "l!*", @hints; |
107 |
} |
108 |
$self->XChangeWindowProperty ($self->parent, $atom, $type, $format, $items); |
109 |
} |
110 |
|
111 |
$self->XDeleteProperty ($self->parent, $_) for keys %our_props; |
112 |
} |
113 |
|
114 |
sub make_current { |
115 |
my ($self, $tab) = @_; |
116 |
|
117 |
if (my $cur = $self->{cur}) { |
118 |
delete $cur->{activity}; |
119 |
$cur->XUnmapWindow ($cur->parent) if $cur->mapped; |
120 |
$cur->focus_out; |
121 |
} |
122 |
|
123 |
$self->{cur} = $tab; |
124 |
|
125 |
$self->configure; |
126 |
$self->copy_properties; |
127 |
$tab->focus_in; |
128 |
$tab->XMapWindow ($tab->parent); |
129 |
delete $tab->{activity}; |
130 |
$self->refresh; |
131 |
|
132 |
() |
133 |
} |
134 |
|
135 |
sub on_button_press { |
136 |
1 |
137 |
} |
138 |
|
139 |
sub on_button_release { |
140 |
my ($self, $event) = @_; |
141 |
|
142 |
if ($event->{row} == 0) { |
143 |
for my $button (@{ $self->{tabofs} }) { |
144 |
$button->[2]->($self, $event) |
145 |
if $event->{col} >= $button->[0] |
146 |
&& $event->{col} < $button->[1]; |
147 |
} |
148 |
} |
149 |
|
150 |
1 |
151 |
} |
152 |
|
153 |
sub on_motion_notify { |
154 |
1 |
155 |
} |
156 |
|
157 |
sub on_init { |
158 |
my ($self) = @_; |
159 |
|
160 |
for (qw(name perl_ext_1 perl_ext_2)) { |
161 |
my $val = $self->resource ($_); |
162 |
|
163 |
push @{ $self->{resource} }, [$_ => $val] |
164 |
if defined $val; |
165 |
} |
166 |
|
167 |
$self->resource (int_bwidth => 0); |
168 |
$self->resource (name => "URxvt.tab"); |
169 |
$self->resource (pty_fd => -1); |
170 |
|
171 |
$self->option ($urxvt::OPTION{scrollBar}, 0); |
172 |
|
173 |
() |
174 |
} |
175 |
|
176 |
sub on_start { |
177 |
my ($self) = @_; |
178 |
|
179 |
$self->{tabheight} = $self->int_bwidth + $self->fheight + $self->lineSpace; |
180 |
|
181 |
$self->new_tab; |
182 |
|
183 |
() |
184 |
} |
185 |
|
186 |
sub on_configure_notify { |
187 |
my ($self, $event) = @_; |
188 |
|
189 |
$self->configure; |
190 |
$self->refresh; |
191 |
|
192 |
() |
193 |
} |
194 |
|
195 |
sub on_wm_delete_window { |
196 |
my ($self) = @_; |
197 |
|
198 |
$_->destroy for @{ $self->{tabs} }; |
199 |
|
200 |
1 |
201 |
} |
202 |
|
203 |
sub tab_start { |
204 |
my ($self, $tab) = @_; |
205 |
|
206 |
$tab->XChangeInput ($tab->parent, urxvt::PropertyChangeMask); |
207 |
|
208 |
push @{ $self->{tabs} }, $tab; |
209 |
|
210 |
# $tab->{name} ||= scalar @{ $self->{tabs} }; |
211 |
$self->make_current ($tab); |
212 |
|
213 |
() |
214 |
} |
215 |
|
216 |
sub tab_destroy { |
217 |
my ($self, $tab) = @_; |
218 |
|
219 |
$self->{tabs} = [ grep $_ != $tab, @{ $self->{tabs} } ]; |
220 |
|
221 |
if (@{ $self->{tabs} }) { |
222 |
if ($self->{cur} == $tab) { |
223 |
delete $self->{cur}; |
224 |
$self->make_current ($self->{tabs}[-1]); |
225 |
} else { |
226 |
$self->refresh; |
227 |
} |
228 |
} else { |
229 |
# delay destruction a tiny bit |
230 |
$self->{destroy} = urxvt::iw->new->start->cb (sub { $self->destroy }); |
231 |
} |
232 |
|
233 |
() |
234 |
} |
235 |
|
236 |
sub tab_key_press { |
237 |
my ($self, $tab, $event, $keysym, $str) = @_; |
238 |
|
239 |
if ($event->{state} & urxvt::ShiftMask) { |
240 |
if ($keysym == 0xff51 || $keysym == 0xff53) { |
241 |
my ($idx) = grep $self->{tabs}[$_] == $tab, 0 .. $#{ $self->{tabs} }; |
242 |
|
243 |
--$idx if $keysym == 0xff51; |
244 |
++$idx if $keysym == 0xff53; |
245 |
|
246 |
$self->make_current ($self->{tabs}[$idx % @{ $self->{tabs}}]); |
247 |
|
248 |
return 1; |
249 |
} elsif ($keysym == 0xff54) { |
250 |
$self->new_tab; |
251 |
|
252 |
return 1; |
253 |
} |
254 |
} |
255 |
|
256 |
() |
257 |
} |
258 |
|
259 |
sub tab_property_notify { |
260 |
my ($self, $tab, $event) = @_; |
261 |
|
262 |
$self->copy_properties |
263 |
if $event->{window} == $tab->parent; |
264 |
|
265 |
() |
266 |
} |
267 |
|
268 |
sub tab_activity { |
269 |
my ($self, $tab) = @_; |
270 |
|
271 |
$self->refresh; |
272 |
} |
273 |
|
274 |
package urxvt::ext::tabbed::tab; |
275 |
|
276 |
# helper extension implementing the subwindows of a tabbed terminal. |
277 |
# simply proxies all interesting calls back to the tabbed class. |
278 |
|
279 |
{ |
280 |
for my $hook qw(start destroy key_press property_notify) { |
281 |
eval qq{ |
282 |
sub on_$hook { |
283 |
my \$parent = \$_[0]{term}{parent} |
284 |
or return; |
285 |
\$parent->tab_$hook (\@_) |
286 |
} |
287 |
}; |
288 |
die if $@; |
289 |
} |
290 |
} |
291 |
|
292 |
sub on_add_lines { |
293 |
$_[0]->{activity}++ |
294 |
or $_[0]{term}{parent}->tab_activity ($_[0]); |
295 |
() |
296 |
} |
297 |
|
298 |
|