1 |
#! perl |
2 |
|
3 |
sub refresh { |
4 |
my ($self) = @_; |
5 |
|
6 |
my $ncol = $self->ncol; |
7 |
|
8 |
my $text = " " x $ncol; |
9 |
my $rend = [($self->{rs_tabbar}) x $ncol]; |
10 |
|
11 |
my @ofs; |
12 |
|
13 |
substr $text, 0, 7, "[NEW] |"; |
14 |
@$rend[0 .. 5] = ($self->{rs_tab}) 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] = ($self->{rs_tab}) 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, @argv) = @_; |
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 |
for (0 .. urxvt::NUM_RESOURCES - 1) { |
57 |
my $value = $self->{resource}[$_]; |
58 |
|
59 |
$term->resource ("+$_" => $value) |
60 |
if defined $value; |
61 |
} |
62 |
|
63 |
$term->resource (perl_ext_2 => $term->resource ("perl_ext_2") . ",-tabbed"); |
64 |
}; |
65 |
|
66 |
push @urxvt::TERM_EXT, urxvt::ext::tabbed::tab::; |
67 |
|
68 |
my $term = new urxvt::term |
69 |
$self->env, $urxvt::RXVTNAME, |
70 |
-embed => $self->parent, |
71 |
@argv, |
72 |
; |
73 |
} |
74 |
|
75 |
sub configure { |
76 |
my ($self) = @_; |
77 |
|
78 |
my $tab = $self->{cur}; |
79 |
|
80 |
# this is an extremely dirty way to force a configurenotify, but who cares |
81 |
$tab->XMoveResizeWindow ( |
82 |
$tab->parent, |
83 |
0, $self->{tabheight} + 1, |
84 |
$self->width, $self->height - $self->{tabheight} |
85 |
); |
86 |
$tab->XMoveResizeWindow ( |
87 |
$tab->parent, |
88 |
0, $self->{tabheight}, |
89 |
$self->width, $self->height - $self->{tabheight} |
90 |
); |
91 |
} |
92 |
|
93 |
sub on_resize_all_windows { |
94 |
my ($self, $width, $height) = @_; |
95 |
|
96 |
1 |
97 |
} |
98 |
|
99 |
sub copy_properties { |
100 |
my ($self) = @_; |
101 |
my $tab = $self->{cur}; |
102 |
|
103 |
my $wm_normal_hints = $self->XInternAtom ("WM_NORMAL_HINTS"); |
104 |
|
105 |
my $current = delete $self->{current_properties}; |
106 |
|
107 |
# pass 1: copy over properties different or nonexisting |
108 |
for my $atom ($tab->XListProperties ($tab->parent)) { |
109 |
my ($type, $format, $items) = $self->XGetWindowProperty ($tab->parent, $atom); |
110 |
|
111 |
# fix up size hints |
112 |
if ($atom == $wm_normal_hints) { |
113 |
my (@hints) = unpack "l!*", $items; |
114 |
|
115 |
$hints[$_] += $self->{tabheight} for (4, 6, 16); |
116 |
|
117 |
$items = pack "l!*", @hints; |
118 |
} |
119 |
|
120 |
my $cur = delete $current->{$atom}; |
121 |
|
122 |
# update if changed, we assume empty items and zero type and format will not happen |
123 |
$self->XChangeProperty ($self->parent, $atom, $type, $format, $items) |
124 |
if $cur->[0] != $type or $cur->[1] != $format or $cur->[2] ne $items; |
125 |
|
126 |
$self->{current_properties}{$atom} = [$type, $format, $items]; |
127 |
} |
128 |
|
129 |
# pass 2, delete all extraneous properties |
130 |
$self->XDeleteProperty ($self->parent, $_) for keys %$current; |
131 |
} |
132 |
|
133 |
sub make_current { |
134 |
my ($self, $tab) = @_; |
135 |
|
136 |
if (my $cur = $self->{cur}) { |
137 |
delete $cur->{activity}; |
138 |
$cur->XUnmapWindow ($cur->parent) if $cur->mapped; |
139 |
$cur->focus_out; |
140 |
} |
141 |
|
142 |
$self->{cur} = $tab; |
143 |
|
144 |
$self->configure; |
145 |
$self->copy_properties; |
146 |
|
147 |
$tab->focus_out; # just in case, should be a nop |
148 |
$tab->focus_in if $self->focus; |
149 |
|
150 |
$tab->XMapWindow ($tab->parent); |
151 |
delete $tab->{activity}; |
152 |
$self->refresh; |
153 |
|
154 |
() |
155 |
} |
156 |
|
157 |
sub on_focus_in { |
158 |
my ($self, $event) = @_; |
159 |
|
160 |
$self->{cur}->focus_in; |
161 |
|
162 |
() |
163 |
} |
164 |
|
165 |
sub on_focus_out { |
166 |
my ($self, $event) = @_; |
167 |
|
168 |
$self->{cur}->focus_out; |
169 |
|
170 |
() |
171 |
} |
172 |
|
173 |
sub on_key_press { |
174 |
my ($self, $event) = @_; |
175 |
|
176 |
$self->{cur}->key_press ($event->{state}, $event->{keycode}, $event->{time}); |
177 |
|
178 |
1 |
179 |
} |
180 |
|
181 |
sub on_key_release { |
182 |
my ($self, $event) = @_; |
183 |
|
184 |
$self->{cur}->key_release ($event->{state}, $event->{keycode}, $event->{time}); |
185 |
|
186 |
1 |
187 |
} |
188 |
|
189 |
sub on_button_press { |
190 |
1 |
191 |
} |
192 |
|
193 |
sub on_button_release { |
194 |
my ($self, $event) = @_; |
195 |
|
196 |
if ($event->{row} == 0) { |
197 |
for my $button (@{ $self->{tabofs} }) { |
198 |
$button->[2]->($self, $event) |
199 |
if $event->{col} >= $button->[0] |
200 |
&& $event->{col} < $button->[1]; |
201 |
} |
202 |
} |
203 |
|
204 |
1 |
205 |
} |
206 |
|
207 |
sub on_motion_notify { |
208 |
1 |
209 |
} |
210 |
|
211 |
sub on_init { |
212 |
my ($self) = @_; |
213 |
|
214 |
$self->{resource} = [map $self->resource ("+$_"), 0 .. urxvt::NUM_RESOURCES - 1]; |
215 |
|
216 |
$self->resource (int_bwidth => 0); |
217 |
$self->resource (name => "URxvt.tabbed"); |
218 |
$self->resource (pty_fd => -1); |
219 |
|
220 |
$self->option ($urxvt::OPTION{scrollBar}, 0); |
221 |
|
222 |
my $fg = $self->x_resource ("tabbar-fg"); |
223 |
my $bg = $self->x_resource ("tabbar-bg"); |
224 |
my $tabfg = $self->x_resource ("tab-fg"); |
225 |
my $tabbg = $self->x_resource ("tab-bg"); |
226 |
|
227 |
defined $fg or $fg = 3; |
228 |
defined $bg or $bg = 0; |
229 |
defined $tabfg or $tabfg = 0; |
230 |
defined $tabbg or $tabbg = 1; |
231 |
|
232 |
$self->{rs_tabbar} = urxvt::SET_COLOR (urxvt::DEFAULT_RSTYLE, $fg + 2, $bg + 2); |
233 |
$self->{rs_tab} = urxvt::SET_COLOR (urxvt::DEFAULT_RSTYLE, $tabfg + 2, $tabbg + 2); |
234 |
|
235 |
() |
236 |
} |
237 |
|
238 |
sub on_start { |
239 |
my ($self) = @_; |
240 |
|
241 |
$self->{tabheight} = $self->int_bwidth + $self->fheight + $self->lineSpace; |
242 |
|
243 |
$self->cmd_parse ("\033[?25l"); |
244 |
|
245 |
my @argv = $self->argv; |
246 |
|
247 |
do { |
248 |
shift @argv; |
249 |
} while @argv && $argv[0] ne "-e"; |
250 |
|
251 |
$self->new_tab (@argv); |
252 |
|
253 |
() |
254 |
} |
255 |
|
256 |
sub on_configure_notify { |
257 |
my ($self, $event) = @_; |
258 |
|
259 |
$self->configure; |
260 |
$self->refresh; |
261 |
|
262 |
() |
263 |
} |
264 |
|
265 |
sub on_wm_delete_window { |
266 |
my ($self) = @_; |
267 |
|
268 |
$_->destroy for @{ $self->{tabs} }; |
269 |
|
270 |
1 |
271 |
} |
272 |
|
273 |
sub tab_start { |
274 |
my ($self, $tab) = @_; |
275 |
|
276 |
$tab->XChangeInput ($tab->parent, urxvt::PropertyChangeMask); |
277 |
|
278 |
push @{ $self->{tabs} }, $tab; |
279 |
|
280 |
# $tab->{name} ||= scalar @{ $self->{tabs} }; |
281 |
$self->make_current ($tab); |
282 |
|
283 |
() |
284 |
} |
285 |
|
286 |
sub tab_destroy { |
287 |
my ($self, $tab) = @_; |
288 |
|
289 |
$self->{tabs} = [ grep $_ != $tab, @{ $self->{tabs} } ]; |
290 |
|
291 |
if (@{ $self->{tabs} }) { |
292 |
if ($self->{cur} == $tab) { |
293 |
delete $self->{cur}; |
294 |
$self->make_current ($self->{tabs}[-1]); |
295 |
} else { |
296 |
$self->refresh; |
297 |
} |
298 |
} else { |
299 |
# delay destruction a tiny bit |
300 |
$self->{destroy} = urxvt::iw->new->start->cb (sub { $self->destroy }); |
301 |
} |
302 |
|
303 |
() |
304 |
} |
305 |
|
306 |
sub tab_key_press { |
307 |
my ($self, $tab, $event, $keysym, $str) = @_; |
308 |
|
309 |
if ($event->{state} & urxvt::ShiftMask) { |
310 |
if ($keysym == 0xff51 || $keysym == 0xff53) { |
311 |
my ($idx) = grep $self->{tabs}[$_] == $tab, 0 .. $#{ $self->{tabs} }; |
312 |
|
313 |
--$idx if $keysym == 0xff51; |
314 |
++$idx if $keysym == 0xff53; |
315 |
|
316 |
$self->make_current ($self->{tabs}[$idx % @{ $self->{tabs}}]); |
317 |
|
318 |
return 1; |
319 |
} elsif ($keysym == 0xff54) { |
320 |
$self->new_tab; |
321 |
|
322 |
return 1; |
323 |
} |
324 |
} |
325 |
elsif ($event->{state} & urxvt::ControlMask) { |
326 |
if ($keysym == 0xff51 || $keysym == 0xff53) { |
327 |
my ($idx1) = grep $self->{tabs}[$_] == $tab, 0 .. $#{ $self->{tabs} }; |
328 |
my $idx2 = ($idx1 + ($keysym == 0xff51 ? -1 : +1)) % @{ $self->{tabs} }; |
329 |
|
330 |
($self->{tabs}[$idx1], $self->{tabs}[$idx2]) = |
331 |
($self->{tabs}[$idx2], $self->{tabs}[$idx1]); |
332 |
|
333 |
$self->make_current ($self->{tabs}[$idx2]); |
334 |
|
335 |
return 1; |
336 |
} |
337 |
} |
338 |
|
339 |
() |
340 |
} |
341 |
|
342 |
sub tab_property_notify { |
343 |
my ($self, $tab, $event) = @_; |
344 |
|
345 |
$self->copy_properties |
346 |
if $event->{window} == $tab->parent; |
347 |
|
348 |
() |
349 |
} |
350 |
|
351 |
sub tab_activity { |
352 |
my ($self, $tab) = @_; |
353 |
|
354 |
$self->refresh; |
355 |
} |
356 |
|
357 |
package urxvt::ext::tabbed::tab; |
358 |
|
359 |
# helper extension implementing the subwindows of a tabbed terminal. |
360 |
# simply proxies all interesting calls back to the tabbed class. |
361 |
|
362 |
{ |
363 |
for my $hook qw(start destroy key_press property_notify) { |
364 |
eval qq{ |
365 |
sub on_$hook { |
366 |
my \$parent = \$_[0]{term}{parent} |
367 |
or return; |
368 |
\$parent->tab_$hook (\@_) |
369 |
} |
370 |
}; |
371 |
die if $@; |
372 |
} |
373 |
} |
374 |
|
375 |
sub on_add_lines { |
376 |
$_[0]->{activity}++ |
377 |
or $_[0]{term}{parent}->tab_activity ($_[0]); |
378 |
() |
379 |
} |
380 |
|
381 |
|