ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/perl/tabbed
(Generate patch)

Comparing rxvt-unicode/src/perl/tabbed (file contents):
Revision 1.1 by root, Fri Jan 20 12:16:28 2006 UTC vs.
Revision 1.39 by sf-exg, Sun Feb 26 06:36:46 2017 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines