1 | #! perl |
1 | #! perl |
2 | |
2 | |
|
|
3 | #:META:RESOURCE:tabbar-fg:colour:tab bar foreground colour |
|
|
4 | #:META:RESOURCE:tabbar-bg:colour:tab bar background colour |
|
|
5 | #:META:RESOURCE:tab-fg:colour:tab foreground colour |
|
|
6 | #:META:RESOURCE:tab-bg:colour:tab background colour |
|
|
7 | |
|
|
8 | =head1 NAME |
|
|
9 | |
|
|
10 | tabbed - tabbed interface to urxvt |
|
|
11 | |
|
|
12 | =head1 DESCRIPTION |
|
|
13 | |
|
|
14 | This transforms the terminal into a tabbar with additional terminals, that |
|
|
15 | is, it implements what is commonly referred to as "tabbed terminal". The topmost line |
|
|
16 | displays a "[NEW]" button, which, when clicked, will add a new tab, followed by one |
|
|
17 | button per tab. |
|
|
18 | |
|
|
19 | Clicking a button will activate that tab. Pressing B<Shift-Left> and |
|
|
20 | B<Shift-Right> will switch to the tab left or right of the current one, |
|
|
21 | while B<Shift-Down> creates a new tab. Pressing B<Ctrl-Left> and |
|
|
22 | B<Ctrl-Right> will renumber the current tab by moving it to the left or |
|
|
23 | to the right. |
|
|
24 | |
|
|
25 | The tabbar itself can be configured similarly to a normal terminal, but |
|
|
26 | with a resource class of C<URxvt.tabbed>. In addition, it supports the |
|
|
27 | following four resources (shown with defaults): |
|
|
28 | |
|
|
29 | URxvt.tabbed.tabbar-fg: <colour-index, default 3> |
|
|
30 | URxvt.tabbed.tabbar-bg: <colour-index, default 0> |
|
|
31 | URxvt.tabbed.tab-fg: <colour-index, default 0> |
|
|
32 | URxvt.tabbed.tab-bg: <colour-index, default 1> |
|
|
33 | |
|
|
34 | See I<COLOR AND GRAPHICS> in the @@RXVT_NAME@@(1) manpage for valid |
|
|
35 | indices. |
|
|
36 | |
|
|
37 | =cut |
|
|
38 | |
3 | sub refresh { |
39 | sub refresh { |
4 | my ($self) = @_; |
40 | my ($self) = @_; |
5 | |
41 | |
6 | my $ncol = $self->ncol; |
42 | my $ncol = $self->ncol; |
7 | |
43 | |
8 | my $text = " " x $ncol; |
44 | my $text = " " x $ncol; |
9 | my $rend = [(urxvt::DEFAULT_RSTYLE | urxvt::RS_RVid) x $ncol]; |
45 | my $rend = [($self->{rs_tabbar}) x $ncol]; |
10 | |
46 | |
11 | my @ofs; |
47 | my @ofs; |
12 | |
48 | |
13 | substr $text, 0, 7, "[NEW] |"; |
49 | substr $text, 0, 7, "[NEW] |"; |
14 | @$rend[0 .. 5] = (urxvt::OVERLAY_RSTYLE) x 6; |
50 | @$rend[0 .. 5] = ($self->{rs_tab}) x 6; |
15 | push @ofs, [0, 6, sub { $_[0]->new_tab }]; |
51 | push @ofs, [0, 6, sub { $_[0]->new_tab }]; |
16 | |
52 | |
17 | my $ofs = 7; |
53 | my $ofs = 7; |
18 | my $idx = 0; |
54 | my $idx = 0; |
19 | |
55 | |
20 | for my $tab (@{ $self->{tabs} }) { |
56 | for my $tab (@{ $self->{tabs} }) { |
21 | $idx++; |
57 | $idx++; |
22 | |
58 | |
|
|
59 | my $act = $tab->{activity} && $tab != $self->{cur} |
|
|
60 | ? "*" : " "; |
|
|
61 | |
23 | my $txt = " $idx "; |
62 | my $txt = "$act$idx$act"; |
24 | my $len = length $txt; |
63 | my $len = length $txt; |
25 | |
64 | |
26 | substr $text, $ofs, $len + 1, "$txt|"; |
65 | substr $text, $ofs, $len + 1, "$txt|"; |
27 | @$rend[$ofs .. $ofs + $len - 1] = (urxvt::OVERLAY_RSTYLE) x $len |
66 | @$rend[$ofs .. $ofs + $len - 1] = ($self->{rs_tab}) x $len |
28 | if $tab == $self->{cur}; |
67 | if $tab == $self->{cur}; |
29 | |
68 | |
30 | push @ofs, [ $ofs, $ofs + $len, sub { $_[0]->make_current ($tab) } ]; |
69 | push @ofs, [ $ofs, $ofs + $len, sub { $_[0]->make_current ($tab) } ]; |
31 | |
70 | |
32 | $ofs += $len + 1; |
71 | $ofs += $len + 1; |
… | |
… | |
39 | |
78 | |
40 | $self->want_refresh; |
79 | $self->want_refresh; |
41 | } |
80 | } |
42 | |
81 | |
43 | sub new_tab { |
82 | sub new_tab { |
44 | my ($self) = @_; |
83 | my ($self, @argv) = @_; |
45 | |
|
|
46 | my $offset = $self->fheight; |
|
|
47 | |
84 | |
48 | # save a backlink to us, make sure tabbed is inactive |
85 | # save a backlink to us, make sure tabbed is inactive |
49 | push @urxvt::TERM_INIT, sub { |
86 | push @urxvt::TERM_INIT, sub { |
50 | my ($term) = @_; |
87 | my ($term) = @_; |
51 | $term->{parent} = $self; |
88 | $term->{parent} = $self; |
52 | |
89 | |
53 | $term->resource ($_->[0] => $_->[1]) |
90 | for (0 .. urxvt::NUM_RESOURCES - 1) { |
54 | for @{ $self->{resource} || [] }; |
91 | my $value = $self->{resource}[$_]; |
|
|
92 | |
|
|
93 | $term->resource ("+$_" => $value) |
|
|
94 | if defined $value; |
|
|
95 | } |
55 | |
96 | |
56 | $term->resource (perl_ext_2 => $term->resource ("perl_ext_2") . ",-tabbed"); |
97 | $term->resource (perl_ext_2 => $term->resource ("perl_ext_2") . ",-tabbed"); |
57 | |
|
|
58 | }; |
98 | }; |
59 | |
99 | |
60 | push @urxvt::TERM_EXT, urxvt::ext::tabbed::tab::; |
100 | push @urxvt::TERM_EXT, urxvt::ext::tabbed::tab::; |
61 | |
101 | |
62 | my $term = new urxvt::term |
102 | my $term = new urxvt::term |
63 | $self->env, $urxvt::RXVTNAME, |
103 | $self->env, $urxvt::RXVTNAME, |
64 | -embed => $self->parent, |
104 | -embed => $self->parent, |
|
|
105 | @argv, |
65 | ; |
106 | ; |
66 | } |
107 | } |
67 | |
108 | |
68 | sub configure { |
109 | sub configure { |
69 | my ($self) = @_; |
110 | my ($self) = @_; |
70 | |
111 | |
71 | my $tab = $self->{cur}; |
112 | my $tab = $self->{cur}; |
72 | |
113 | |
|
|
114 | # this is an extremely dirty way to force a configurenotify, but who cares |
|
|
115 | $tab->XMoveResizeWindow ( |
|
|
116 | $tab->parent, |
|
|
117 | 0, $self->{tabheight} + 1, |
|
|
118 | $self->width, $self->height - $self->{tabheight} |
|
|
119 | ); |
73 | $tab->XMoveResizeWindow ( |
120 | $tab->XMoveResizeWindow ( |
74 | $tab->parent, |
121 | $tab->parent, |
75 | 0, $self->{tabheight}, |
122 | 0, $self->{tabheight}, |
76 | $self->width, $self->height - $self->{tabheight} |
123 | $self->width, $self->height - $self->{tabheight} |
77 | ); |
124 | ); |
|
|
125 | } |
|
|
126 | |
|
|
127 | sub on_resize_all_windows { |
|
|
128 | my ($self, $width, $height) = @_; |
|
|
129 | |
|
|
130 | 1 |
|
|
131 | } |
|
|
132 | |
|
|
133 | sub copy_properties { |
|
|
134 | my ($self) = @_; |
|
|
135 | my $tab = $self->{cur}; |
78 | |
136 | |
79 | my $wm_normal_hints = $self->XInternAtom ("WM_NORMAL_HINTS"); |
137 | my $wm_normal_hints = $self->XInternAtom ("WM_NORMAL_HINTS"); |
80 | |
138 | |
|
|
139 | my $current = delete $self->{current_properties}; |
|
|
140 | |
|
|
141 | # pass 1: copy over properties different or nonexisting |
81 | for my $atom ($tab->XListProperties ($tab->parent)) { |
142 | for my $atom ($tab->XListProperties ($tab->parent)) { |
82 | my ($type, $format, $items) = $self->XGetWindowProperty ($tab->parent, $atom); |
143 | my ($type, $format, $items) = $self->XGetWindowProperty ($tab->parent, $atom); |
83 | |
144 | |
|
|
145 | # fix up size hints |
84 | if ($atom == $wm_normal_hints) { |
146 | if ($atom == $wm_normal_hints) { |
85 | my (@hints) = unpack "l!*", $items; |
147 | my (@hints) = unpack "l!*", $items; |
86 | |
148 | |
87 | $hints[$_] += $self->{tabheight} for (4, 6, 16); |
149 | $hints[$_] += $self->{tabheight} for (4, 6, 16); |
88 | |
150 | |
89 | $items = pack "l!*", @hints; |
151 | $items = pack "l!*", @hints; |
90 | } |
152 | } |
|
|
153 | |
|
|
154 | my $cur = delete $current->{$atom}; |
|
|
155 | |
|
|
156 | # update if changed, we assume empty items and zero type and format will not happen |
91 | $self->XChangeWindowProperty ($self->parent, $atom, $type, $format, $items); |
157 | $self->XChangeProperty ($self->parent, $atom, $type, $format, $items) |
92 | } |
158 | if $cur->[0] != $type or $cur->[1] != $format or $cur->[2] ne $items; |
93 | |
159 | |
94 | $self->refresh; |
160 | $self->{current_properties}{$atom} = [$type, $format, $items]; |
|
|
161 | } |
|
|
162 | |
|
|
163 | # pass 2, delete all extraneous properties |
|
|
164 | $self->XDeleteProperty ($self->parent, $_) for keys %$current; |
95 | } |
165 | } |
96 | |
166 | |
97 | sub make_current { |
167 | sub make_current { |
98 | my ($self, $tab) = @_; |
168 | my ($self, $tab) = @_; |
99 | |
169 | |
100 | if (my $cur = $self->{cur}) { |
170 | if (my $cur = $self->{cur}) { |
|
|
171 | delete $cur->{activity}; |
101 | $cur->XUnmapWindow ($cur->parent) if $cur->mapped; |
172 | $cur->XUnmapWindow ($cur->parent) if $cur->mapped; |
102 | $cur->focus_out; |
173 | $cur->focus_out; |
103 | } |
174 | } |
104 | |
175 | |
105 | $self->{cur} = $tab; |
176 | $self->{cur} = $tab; |
106 | |
177 | |
107 | $self->configure; |
178 | $self->configure; |
108 | $tab->focus_in; |
179 | $self->copy_properties; |
|
|
180 | |
|
|
181 | $tab->focus_out; # just in case, should be a nop |
|
|
182 | $tab->focus_in if $self->focus; |
|
|
183 | |
109 | $tab->XMapWindow ($tab->parent); |
184 | $tab->XMapWindow ($tab->parent); |
|
|
185 | delete $tab->{activity}; |
110 | $self->refresh; |
186 | $self->refresh; |
111 | |
187 | |
112 | () |
188 | () |
|
|
189 | } |
|
|
190 | |
|
|
191 | sub on_focus_in { |
|
|
192 | my ($self, $event) = @_; |
|
|
193 | |
|
|
194 | $self->{cur}->focus_in; |
|
|
195 | |
|
|
196 | () |
|
|
197 | } |
|
|
198 | |
|
|
199 | sub on_focus_out { |
|
|
200 | my ($self, $event) = @_; |
|
|
201 | |
|
|
202 | $self->{cur}->focus_out; |
|
|
203 | |
|
|
204 | () |
|
|
205 | } |
|
|
206 | |
|
|
207 | sub on_tt_write { |
|
|
208 | my ($self, $octets) = @_; |
|
|
209 | |
|
|
210 | $self->{cur}->tt_write ($octets); |
|
|
211 | |
|
|
212 | 1 |
|
|
213 | } |
|
|
214 | |
|
|
215 | sub on_key_press { |
|
|
216 | my ($self, $event) = @_; |
|
|
217 | |
|
|
218 | $self->{cur}->key_press ($event->{state}, $event->{keycode}, $event->{time}); |
|
|
219 | $self->{cur}->refresh_check; |
|
|
220 | |
|
|
221 | 1 |
|
|
222 | } |
|
|
223 | |
|
|
224 | sub on_key_release { |
|
|
225 | my ($self, $event) = @_; |
|
|
226 | |
|
|
227 | $self->{cur}->key_release ($event->{state}, $event->{keycode}, $event->{time}); |
|
|
228 | $self->{cur}->refresh_check; |
|
|
229 | |
|
|
230 | 1 |
113 | } |
231 | } |
114 | |
232 | |
115 | sub on_button_press { |
233 | sub on_button_press { |
116 | 1 |
234 | 1 |
117 | } |
235 | } |
… | |
… | |
135 | } |
253 | } |
136 | |
254 | |
137 | sub on_init { |
255 | sub on_init { |
138 | my ($self) = @_; |
256 | my ($self) = @_; |
139 | |
257 | |
140 | for (qw(name perl_ext_1 perl_ext_2)) { |
258 | $self->{resource} = [map $self->resource ("+$_"), 0 .. urxvt::NUM_RESOURCES - 1]; |
141 | my $val = $self->resource ($_); |
|
|
142 | |
|
|
143 | push @{ $self->{resource} }, [$_ => $val] |
|
|
144 | if defined $val; |
|
|
145 | } |
|
|
146 | |
259 | |
147 | $self->resource (int_bwidth => 0); |
260 | $self->resource (int_bwidth => 0); |
148 | $self->resource (name => "URxvt.tab"); |
261 | $self->resource (name => "URxvt.tabbed"); |
149 | $self->resource (pty_fd => -1); |
262 | $self->resource (pty_fd => -1); |
150 | |
263 | |
151 | $self->option ($urxvt::OPTION{scrollBar}, 0); |
264 | $self->option ($urxvt::OPTION{scrollBar}, 0); |
152 | |
265 | |
|
|
266 | my $fg = $self->x_resource ("tabbar-fg"); |
|
|
267 | my $bg = $self->x_resource ("tabbar-bg"); |
|
|
268 | my $tabfg = $self->x_resource ("tab-fg"); |
|
|
269 | my $tabbg = $self->x_resource ("tab-bg"); |
|
|
270 | |
|
|
271 | defined $fg or $fg = 3; |
|
|
272 | defined $bg or $bg = 0; |
|
|
273 | defined $tabfg or $tabfg = 0; |
|
|
274 | defined $tabbg or $tabbg = 1; |
|
|
275 | |
|
|
276 | $self->{rs_tabbar} = urxvt::SET_COLOR (urxvt::DEFAULT_RSTYLE, $fg + 2, $bg + 2); |
|
|
277 | $self->{rs_tab} = urxvt::SET_COLOR (urxvt::DEFAULT_RSTYLE, $tabfg + 2, $tabbg + 2); |
|
|
278 | |
153 | () |
279 | () |
154 | } |
280 | } |
155 | |
281 | |
156 | sub on_start { |
282 | sub on_start { |
157 | my ($self) = @_; |
283 | my ($self) = @_; |
158 | |
284 | |
159 | $self->{tabheight} = $self->int_bwidth + $self->fheight + $self->lineSpace; |
285 | $self->{tabheight} = $self->int_bwidth + $self->fheight + $self->lineSpace; |
160 | |
286 | |
|
|
287 | $self->cmd_parse ("\033[?25l"); |
|
|
288 | |
|
|
289 | my @argv = $self->argv; |
|
|
290 | |
|
|
291 | do { |
|
|
292 | shift @argv; |
|
|
293 | } while @argv && $argv[0] ne "-e"; |
|
|
294 | |
161 | $self->new_tab; |
295 | $self->new_tab (@argv); |
162 | |
296 | |
163 | () |
297 | () |
164 | } |
298 | } |
165 | |
299 | |
166 | sub on_configure_notify { |
300 | sub on_configure_notify { |
167 | my ($self, $event) = @_; |
301 | my ($self, $event) = @_; |
168 | |
302 | |
169 | $self->configure; |
303 | $self->configure; |
|
|
304 | $self->refresh; |
170 | |
305 | |
171 | () |
306 | () |
172 | } |
307 | } |
173 | |
308 | |
174 | sub on_wm_delete_window { |
309 | sub on_wm_delete_window { |
… | |
… | |
179 | 1 |
314 | 1 |
180 | } |
315 | } |
181 | |
316 | |
182 | sub tab_start { |
317 | sub tab_start { |
183 | my ($self, $tab) = @_; |
318 | my ($self, $tab) = @_; |
|
|
319 | |
|
|
320 | $tab->XChangeInput ($tab->parent, urxvt::PropertyChangeMask); |
184 | |
321 | |
185 | push @{ $self->{tabs} }, $tab; |
322 | push @{ $self->{tabs} }, $tab; |
186 | |
323 | |
187 | # $tab->{name} ||= scalar @{ $self->{tabs} }; |
324 | # $tab->{name} ||= scalar @{ $self->{tabs} }; |
188 | $self->make_current ($tab); |
325 | $self->make_current ($tab); |
… | |
… | |
211 | } |
348 | } |
212 | |
349 | |
213 | sub tab_key_press { |
350 | sub tab_key_press { |
214 | my ($self, $tab, $event, $keysym, $str) = @_; |
351 | my ($self, $tab, $event, $keysym, $str) = @_; |
215 | |
352 | |
216 | if ($event->{state} & urxvt::ShiftMask |
353 | if ($event->{state} & urxvt::ShiftMask) { |
217 | && ($keysym == 0xff51 || $keysym == 0xff53)) { |
354 | if ($keysym == 0xff51 || $keysym == 0xff53) { |
218 | my ($idx) = grep $self->{tabs}[$_] == $tab, 0 .. $#{ $self->{tabs} }; |
355 | my ($idx) = grep $self->{tabs}[$_] == $tab, 0 .. $#{ $self->{tabs} }; |
219 | |
356 | |
220 | --$idx if $keysym == 0xff51; |
357 | --$idx if $keysym == 0xff51; |
221 | ++$idx if $keysym == 0xff53; |
358 | ++$idx if $keysym == 0xff53; |
222 | |
359 | |
223 | $self->make_current ($self->{tabs}[$idx % @{ $self->{tabs}}]); |
360 | $self->make_current ($self->{tabs}[$idx % @{ $self->{tabs}}]); |
224 | $self->refresh; |
361 | |
|
|
362 | return 1; |
|
|
363 | } elsif ($keysym == 0xff54) { |
|
|
364 | $self->new_tab; |
|
|
365 | |
|
|
366 | return 1; |
225 | |
367 | } |
|
|
368 | } |
|
|
369 | elsif ($event->{state} & urxvt::ControlMask) { |
|
|
370 | if ($keysym == 0xff51 || $keysym == 0xff53) { |
|
|
371 | my ($idx1) = grep $self->{tabs}[$_] == $tab, 0 .. $#{ $self->{tabs} }; |
|
|
372 | my $idx2 = ($idx1 + ($keysym == 0xff51 ? -1 : +1)) % @{ $self->{tabs} }; |
|
|
373 | |
|
|
374 | ($self->{tabs}[$idx1], $self->{tabs}[$idx2]) = |
|
|
375 | ($self->{tabs}[$idx2], $self->{tabs}[$idx1]); |
|
|
376 | |
|
|
377 | $self->make_current ($self->{tabs}[$idx2]); |
|
|
378 | |
226 | return 1; |
379 | return 1; |
|
|
380 | } |
227 | } |
381 | } |
228 | |
382 | |
229 | () |
383 | () |
|
|
384 | } |
|
|
385 | |
|
|
386 | sub tab_property_notify { |
|
|
387 | my ($self, $tab, $event) = @_; |
|
|
388 | |
|
|
389 | $self->copy_properties |
|
|
390 | if $event->{window} == $tab->parent; |
|
|
391 | |
|
|
392 | () |
|
|
393 | } |
|
|
394 | |
|
|
395 | sub tab_activity { |
|
|
396 | my ($self, $tab) = @_; |
|
|
397 | |
|
|
398 | $self->refresh; |
230 | } |
399 | } |
231 | |
400 | |
232 | package urxvt::ext::tabbed::tab; |
401 | package urxvt::ext::tabbed::tab; |
233 | |
402 | |
234 | # helper extension implementing the subwindows of a tabbed terminal. |
403 | # helper extension implementing the subwindows of a tabbed terminal. |
235 | # simply proxies all interesting calls back to the tabbed class. |
404 | # simply proxies all interesting calls back to the tabbed class. |
236 | |
405 | |
237 | { |
406 | { |
238 | for my $hook qw(start destroy key_press) { |
407 | for my $hook (qw(start destroy key_press property_notify)) { |
239 | eval qq{ |
408 | eval qq{ |
240 | sub on_$hook { |
409 | sub on_$hook { |
241 | my \$parent = \$_[0]{term}{parent} |
410 | my \$parent = \$_[0]{term}{parent} |
242 | or return; |
411 | or return; |
243 | \$parent->tab_$hook (\@_) |
412 | \$parent->tab_$hook (\@_) |
… | |
… | |
245 | }; |
414 | }; |
246 | die if $@; |
415 | die if $@; |
247 | } |
416 | } |
248 | } |
417 | } |
249 | |
418 | |
|
|
419 | sub on_add_lines { |
|
|
420 | $_[0]->{activity}++ |
|
|
421 | or $_[0]{term}{parent}->tab_activity ($_[0]); |
|
|
422 | () |
|
|
423 | } |
250 | |
424 | |
251 | |
425 | |