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 $ncol = $self->ncol; |
11 | my $ncol = $self->ncol; |
7 | |
12 | |
8 | my $text = " " x $ncol; |
13 | my $text = " " x $ncol; |
9 | my $rend = [(urxvt::DEFAULT_RSTYLE | urxvt::RS_RVid) x $ncol]; |
14 | my $rend = [($self->{rs_tabbar}) x $ncol]; |
10 | |
15 | |
11 | my @ofs; |
16 | my @ofs; |
12 | |
17 | |
13 | substr $text, 0, 7, "[NEW] |"; |
18 | substr $text, 0, 7, "[NEW] |"; |
14 | @$rend[0 .. 5] = (urxvt::OVERLAY_RSTYLE) x 6; |
19 | @$rend[0 .. 5] = ($self->{rs_tab}) x 6; |
15 | push @ofs, [0, 6, sub { $_[0]->new_tab }]; |
20 | push @ofs, [0, 6, sub { $_[0]->new_tab }]; |
16 | |
21 | |
17 | my $ofs = 7; |
22 | my $ofs = 7; |
18 | my $idx = 0; |
23 | my $idx = 0; |
19 | |
24 | |
… | |
… | |
25 | |
30 | |
26 | my $txt = "$act$idx$act"; |
31 | my $txt = "$act$idx$act"; |
27 | my $len = length $txt; |
32 | my $len = length $txt; |
28 | |
33 | |
29 | substr $text, $ofs, $len + 1, "$txt|"; |
34 | substr $text, $ofs, $len + 1, "$txt|"; |
30 | @$rend[$ofs .. $ofs + $len - 1] = (urxvt::OVERLAY_RSTYLE) x $len |
35 | @$rend[$ofs .. $ofs + $len - 1] = ($self->{rs_tab}) x $len |
31 | if $tab == $self->{cur}; |
36 | if $tab == $self->{cur}; |
32 | |
37 | |
33 | push @ofs, [ $ofs, $ofs + $len, sub { $_[0]->make_current ($tab) } ]; |
38 | push @ofs, [ $ofs, $ofs + $len, sub { $_[0]->make_current ($tab) } ]; |
34 | |
39 | |
35 | $ofs += $len + 1; |
40 | $ofs += $len + 1; |
… | |
… | |
42 | |
47 | |
43 | $self->want_refresh; |
48 | $self->want_refresh; |
44 | } |
49 | } |
45 | |
50 | |
46 | sub new_tab { |
51 | sub new_tab { |
47 | my ($self) = @_; |
52 | my ($self, @argv) = @_; |
48 | |
|
|
49 | my $offset = $self->fheight; |
|
|
50 | |
53 | |
51 | # save a backlink to us, make sure tabbed is inactive |
54 | # save a backlink to us, make sure tabbed is inactive |
52 | push @urxvt::TERM_INIT, sub { |
55 | push @urxvt::TERM_INIT, sub { |
53 | my ($term) = @_; |
56 | my ($term) = @_; |
54 | $term->{parent} = $self; |
57 | $term->{parent} = $self; |
55 | |
58 | |
56 | $term->resource ($_->[0] => $_->[1]) |
59 | for (0 .. urxvt::NUM_RESOURCES - 1) { |
57 | for @{ $self->{resource} || [] }; |
60 | my $value = $self->{resource}[$_]; |
|
|
61 | |
|
|
62 | $term->resource ("+$_" => $value) |
|
|
63 | if defined $value; |
|
|
64 | } |
58 | |
65 | |
59 | $term->resource (perl_ext_2 => $term->resource ("perl_ext_2") . ",-tabbed"); |
66 | $term->resource (perl_ext_2 => $term->resource ("perl_ext_2") . ",-tabbed"); |
60 | |
|
|
61 | }; |
67 | }; |
62 | |
68 | |
63 | push @urxvt::TERM_EXT, urxvt::ext::tabbed::tab::; |
69 | push @urxvt::TERM_EXT, urxvt::ext::tabbed::tab::; |
64 | |
70 | |
65 | my $term = new urxvt::term |
71 | my $term = new urxvt::term |
66 | $self->env, $urxvt::RXVTNAME, |
72 | $self->env, $urxvt::RXVTNAME, |
67 | -embed => $self->parent, |
73 | -embed => $self->parent, |
|
|
74 | @argv, |
68 | ; |
75 | ; |
69 | } |
76 | } |
70 | |
77 | |
71 | sub configure { |
78 | sub configure { |
72 | my ($self) = @_; |
79 | my ($self) = @_; |
… | |
… | |
84 | 0, $self->{tabheight}, |
91 | 0, $self->{tabheight}, |
85 | $self->width, $self->height - $self->{tabheight} |
92 | $self->width, $self->height - $self->{tabheight} |
86 | ); |
93 | ); |
87 | } |
94 | } |
88 | |
95 | |
|
|
96 | sub on_resize_all_windows { |
|
|
97 | my ($self, $width, $height) = @_; |
|
|
98 | |
|
|
99 | 1 |
|
|
100 | } |
|
|
101 | |
89 | sub copy_properties { |
102 | sub copy_properties { |
90 | my ($self) = @_; |
103 | my ($self) = @_; |
91 | my $tab = $self->{cur}; |
104 | my $tab = $self->{cur}; |
92 | |
105 | |
93 | my $wm_normal_hints = $self->XInternAtom ("WM_NORMAL_HINTS"); |
106 | my $wm_normal_hints = $self->XInternAtom ("WM_NORMAL_HINTS"); |
94 | |
107 | |
95 | my %our_props = map +($_ => undef), $self->XListProperties ($self->parent); |
108 | my $current = delete $self->{current_properties}; |
96 | |
109 | |
|
|
110 | # pass 1: copy over properties different or nonexisting |
97 | for my $atom ($tab->XListProperties ($tab->parent)) { |
111 | for my $atom ($tab->XListProperties ($tab->parent)) { |
98 | my ($type, $format, $items) = $self->XGetWindowProperty ($tab->parent, $atom); |
112 | my ($type, $format, $items) = $self->XGetWindowProperty ($tab->parent, $atom); |
99 | |
|
|
100 | delete $our_props{$atom}; |
|
|
101 | |
113 | |
|
|
114 | # fix up size hints |
102 | if ($atom == $wm_normal_hints) { |
115 | if ($atom == $wm_normal_hints) { |
103 | my (@hints) = unpack "l!*", $items; |
116 | my (@hints) = unpack "l!*", $items; |
104 | |
117 | |
105 | $hints[$_] += $self->{tabheight} for (4, 6, 16); |
118 | $hints[$_] += $self->{tabheight} for (4, 6, 16); |
106 | |
119 | |
107 | $items = pack "l!*", @hints; |
120 | $items = pack "l!*", @hints; |
108 | } |
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 |
109 | $self->XChangeWindowProperty ($self->parent, $atom, $type, $format, $items); |
126 | $self->XChangeProperty ($self->parent, $atom, $type, $format, $items) |
110 | } |
127 | if $cur->[0] != $type or $cur->[1] != $format or $cur->[2] ne $items; |
111 | |
128 | |
|
|
129 | $self->{current_properties}{$atom} = [$type, $format, $items]; |
|
|
130 | } |
|
|
131 | |
|
|
132 | # pass 2, delete all extraneous properties |
112 | $self->XDeleteProperty ($self->parent, $_) for keys %our_props; |
133 | $self->XDeleteProperty ($self->parent, $_) for keys %$current; |
113 | } |
134 | } |
114 | |
135 | |
115 | sub make_current { |
136 | sub make_current { |
116 | my ($self, $tab) = @_; |
137 | my ($self, $tab) = @_; |
117 | |
138 | |
118 | if (my $cur = $self->{cur}) { |
139 | if (my $cur = $self->{cur}) { |
119 | delete $cur->{activity}; |
140 | delete $cur->{activity}; |
120 | $cur->XUnmapWindow ($cur->parent) if $cur->mapped; |
141 | $cur->XUnmapWindow ($cur->parent) if $cur->mapped; |
121 | $cur->focus_out; |
142 | $cur->focus_out; |
122 | } |
143 | } |
123 | |
144 | |
124 | $self->{cur} = $tab; |
145 | $self->{cur} = $tab; |
125 | |
146 | |
126 | $self->configure; |
147 | $self->configure; |
127 | $self->copy_properties; |
148 | $self->copy_properties; |
128 | $tab->focus_in; |
149 | |
|
|
150 | $tab->focus_out; # just in case, should be a nop |
|
|
151 | $tab->focus_in if $self->focus; |
|
|
152 | |
129 | $tab->XMapWindow ($tab->parent); |
153 | $tab->XMapWindow ($tab->parent); |
130 | delete $tab->{activity}; |
154 | delete $tab->{activity}; |
131 | $self->refresh; |
155 | $self->refresh; |
132 | |
156 | |
133 | () |
157 | () |
|
|
158 | } |
|
|
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 |
134 | } |
198 | } |
135 | |
199 | |
136 | sub on_button_press { |
200 | sub on_button_press { |
137 | 1 |
201 | 1 |
138 | } |
202 | } |
… | |
… | |
156 | } |
220 | } |
157 | |
221 | |
158 | sub on_init { |
222 | sub on_init { |
159 | my ($self) = @_; |
223 | my ($self) = @_; |
160 | |
224 | |
161 | for (qw(name perl_ext_1 perl_ext_2)) { |
225 | $self->{resource} = [map $self->resource ("+$_"), 0 .. urxvt::NUM_RESOURCES - 1]; |
162 | my $val = $self->resource ($_); |
|
|
163 | |
|
|
164 | push @{ $self->{resource} }, [$_ => $val] |
|
|
165 | if defined $val; |
|
|
166 | } |
|
|
167 | |
226 | |
168 | $self->resource (int_bwidth => 0); |
227 | $self->resource (int_bwidth => 0); |
169 | $self->resource (name => "URxvt.tab"); |
228 | $self->resource (name => "URxvt.tabbed"); |
170 | $self->resource (pty_fd => -1); |
229 | $self->resource (pty_fd => -1); |
171 | |
230 | |
172 | $self->option ($urxvt::OPTION{scrollBar}, 0); |
231 | $self->option ($urxvt::OPTION{scrollBar}, 0); |
173 | |
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 | |
174 | () |
246 | () |
175 | } |
247 | } |
176 | |
248 | |
177 | sub on_start { |
249 | sub on_start { |
178 | my ($self) = @_; |
250 | my ($self) = @_; |
179 | |
251 | |
180 | $self->{tabheight} = $self->int_bwidth + $self->fheight + $self->lineSpace; |
252 | $self->{tabheight} = $self->int_bwidth + $self->fheight + $self->lineSpace; |
181 | |
253 | |
|
|
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 | |
182 | $self->new_tab; |
262 | $self->new_tab (@argv); |
183 | |
263 | |
184 | () |
264 | () |
185 | } |
265 | } |
186 | |
266 | |
187 | sub on_configure_notify { |
267 | sub on_configure_notify { |
… | |
… | |
243 | |
323 | |
244 | --$idx if $keysym == 0xff51; |
324 | --$idx if $keysym == 0xff51; |
245 | ++$idx if $keysym == 0xff53; |
325 | ++$idx if $keysym == 0xff53; |
246 | |
326 | |
247 | $self->make_current ($self->{tabs}[$idx % @{ $self->{tabs}}]); |
327 | $self->make_current ($self->{tabs}[$idx % @{ $self->{tabs}}]); |
248 | |
328 | |
249 | return 1; |
329 | return 1; |
250 | } elsif ($keysym == 0xff54) { |
330 | } elsif ($keysym == 0xff54) { |
251 | $self->new_tab; |
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]); |
252 | |
345 | |
253 | return 1; |
346 | return 1; |
254 | } |
347 | } |
255 | } |
348 | } |
256 | |
349 | |