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