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