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.5 by root, Fri Jan 20 14:02:41 2006 UTC vs.
Revision 1.37 by root, Sat May 17 13:38:23 2014 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 @@RXVT_NAME@@(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 $tab = $self->{cur}; 112 my $tab = $self->{cur};
55 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 );
56 $tab->XMoveResizeWindow ( 120 $tab->XMoveResizeWindow (
57 $tab->parent, 121 $tab->parent,
58 0, $self->{tabheight}, 122 0, $self->{tabheight},
59 $self->width, $self->height - $self->{tabheight} 123 $self->width, $self->height - $self->{tabheight}
60 ); 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};
61 136
62 my $wm_normal_hints = $self->XInternAtom ("WM_NORMAL_HINTS"); 137 my $wm_normal_hints = $self->XInternAtom ("WM_NORMAL_HINTS");
63 138
139 my $current = delete $self->{current_properties};
140
141 # pass 1: copy over properties different or nonexisting
64 for my $atom ($tab->XListProperties ($tab->parent)) { 142 for my $atom ($tab->XListProperties ($tab->parent)) {
65 my ($type, $format, $items) = $self->XGetWindowProperty ($tab->parent, $atom); 143 my ($type, $format, $items) = $self->XGetWindowProperty ($tab->parent, $atom);
66 144
145 # fix up size hints
67 if ($atom == $wm_normal_hints) { 146 if ($atom == $wm_normal_hints) {
68 my (@hints) = unpack "l!*", $items; 147 my (@hints) = unpack "l!*", $items;
69 148
70 $hints[ 4] += $self->{tabheight}; 149 $hints[$_] += $self->{tabheight} for (4, 6, 16);
71 $hints[16] += $self->{tabheight}; 150
151 $items = pack "l!*", @hints;
72 } 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
73 $self->XChangeWindowProperty ($self->parent, $atom, $type, $format, $items); 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];
74 } 161 }
162
163 # pass 2, delete all extraneous properties
164 $self->XDeleteProperty ($self->parent, $_) for keys %$current;
75} 165}
76 166
77sub make_current { 167sub make_current {
78 my ($self, $tab) = @_; 168 my ($self, $tab) = @_;
79 169
80 if (my $cur = $self->{cur}) { 170 if (my $cur = $self->{cur}) {
171 delete $cur->{activity};
81 $cur->XUnmapWindow ($cur->parent) 172 $cur->XUnmapWindow ($cur->parent) if $cur->mapped;
82 if $cur->mapped; 173 $cur->focus_out;
83 }
84 174 }
175
85 $self->{cur} = $tab; 176 $self->{cur} = $tab;
86 177
87 $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;
88 183
89 $tab->XMapWindow ($tab->parent); 184 $tab->XMapWindow ($tab->parent);
90 185 delete $tab->{activity};
91 $self->refresh; 186 $self->refresh;
92 187
93 () 188 ()
94} 189}
95 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
220 1
221}
222
223sub on_key_release {
224 my ($self, $event) = @_;
225
226 $self->{cur}->key_release ($event->{state}, $event->{keycode}, $event->{time});
227
228 1
229}
230
96sub on_button_press { 231sub on_button_press {
97 1 232 1
98} 233}
99 234
100sub on_button_release { 235sub on_button_release {
101 my ($self, $event) = @_; 236 my ($self, $event) = @_;
102 237
103 my $ofs = $self->{tabofs};
104
105 if ($event->{row} == 0) { 238 if ($event->{row} == 0) {
106 for my $i (0 .. @$ofs - 2) { 239 for my $button (@{ $self->{tabofs} }) {
240 $button->[2]->($self, $event)
107 if ($event->{col} >= $ofs->[$i] 241 if $event->{col} >= $button->[0]
108 && $event->{col} < $ofs->[$i+1]) { 242 && $event->{col} < $button->[1];
109 $self->make_current ($self->{tabs}[$i]);
110 }
111 } 243 }
112 } 244 }
113 245
114 1 246 1
115} 247}
119} 251}
120 252
121sub on_init { 253sub on_init {
122 my ($self) = @_; 254 my ($self) = @_;
123 255
124 for (qw(name perl_ext_1 perl_ext_2)) { 256 $self->{resource} = [map $self->resource ("+$_"), 0 .. urxvt::NUM_RESOURCES - 1];
125 my $val = $self->resource ($_);
126
127 push @{ $self->{resource} }, [$_ => $val]
128 if defined $val;
129 }
130 257
131 $self->resource (int_bwidth => 0); 258 $self->resource (int_bwidth => 0);
132 $self->resource (name => "URxvt.tab"); 259 $self->resource (name => "URxvt.tabbed");
133 $self->resource (pty_fd => -1); 260 $self->resource (pty_fd => -1);
134 261
135 $self->option ($urxvt::OPTION{scrollBar}, 0); 262 $self->option ($urxvt::OPTION{scrollBar}, 0);
136 263
264 my $fg = $self->x_resource ("tabbar-fg");
265 my $bg = $self->x_resource ("tabbar-bg");
266 my $tabfg = $self->x_resource ("tab-fg");
267 my $tabbg = $self->x_resource ("tab-bg");
268
269 defined $fg or $fg = 3;
270 defined $bg or $bg = 0;
271 defined $tabfg or $tabfg = 0;
272 defined $tabbg or $tabbg = 1;
273
274 $self->{rs_tabbar} = urxvt::SET_COLOR (urxvt::DEFAULT_RSTYLE, $fg + 2, $bg + 2);
275 $self->{rs_tab} = urxvt::SET_COLOR (urxvt::DEFAULT_RSTYLE, $tabfg + 2, $tabbg + 2);
276
137 () 277 ()
138} 278}
139 279
140sub on_start { 280sub on_start {
141 my ($self) = @_; 281 my ($self) = @_;
142 282
143 $self->{tabheight} = $self->int_bwidth + $self->fheight + $self->lineSpace; 283 $self->{tabheight} = $self->int_bwidth + $self->fheight + $self->lineSpace;
144 284
145 $self->cmd_parse ("\x1b[?25l\x1b[?7l"); 285 $self->cmd_parse ("\033[?25l");
286
287 my @argv = $self->argv;
288
289 do {
290 shift @argv;
291 } while @argv && $argv[0] ne "-e";
292
146 $self->new_tab; 293 $self->new_tab (@argv);
147 $self->new_tab;
148 $self->new_tab;
149 294
150 () 295 ()
151} 296}
152 297
153sub on_configure_notify { 298sub on_configure_notify {
154 my ($self, $event) = @_; 299 my ($self, $event) = @_;
155 300
156 $self->configure; 301 $self->configure;
302 $self->refresh;
157 303
158 () 304 ()
159} 305}
160 306
161sub on_wm_delete_window { 307sub on_wm_delete_window {
167} 313}
168 314
169sub tab_start { 315sub tab_start {
170 my ($self, $tab) = @_; 316 my ($self, $tab) = @_;
171 317
318 $tab->XChangeInput ($tab->parent, urxvt::PropertyChangeMask);
319
172 push @{ $self->{tabs} }, $tab; 320 push @{ $self->{tabs} }, $tab;
173 321
174 $tab->{name} ||= scalar @{ $self->{tabs} }; 322# $tab->{name} ||= scalar @{ $self->{tabs} };
175 $self->make_current ($tab); 323 $self->make_current ($tab);
176 324
177 () 325 ()
178} 326}
179 327
184 332
185 if (@{ $self->{tabs} }) { 333 if (@{ $self->{tabs} }) {
186 if ($self->{cur} == $tab) { 334 if ($self->{cur} == $tab) {
187 delete $self->{cur}; 335 delete $self->{cur};
188 $self->make_current ($self->{tabs}[-1]); 336 $self->make_current ($self->{tabs}[-1]);
337 } else {
338 $self->refresh;
189 } 339 }
190 } else { 340 } else {
191 # delay destruction a tiny bit 341 # delay destruction a tiny bit
192 $self->{destroy} = urxvt::iw->new->start->cb (sub { $self->destroy }); 342 $self->{destroy} = urxvt::iw->new->start->cb (sub { $self->destroy });
193 } 343 }
194 344
195 () 345 ()
196} 346}
197 347
348sub tab_key_press {
349 my ($self, $tab, $event, $keysym, $str) = @_;
350
351 if ($event->{state} & urxvt::ShiftMask) {
352 if ($keysym == 0xff51 || $keysym == 0xff53) {
353 my ($idx) = grep $self->{tabs}[$_] == $tab, 0 .. $#{ $self->{tabs} };
354
355 --$idx if $keysym == 0xff51;
356 ++$idx if $keysym == 0xff53;
357
358 $self->make_current ($self->{tabs}[$idx % @{ $self->{tabs}}]);
359
360 return 1;
361 } elsif ($keysym == 0xff54) {
362 $self->new_tab;
363
364 return 1;
365 }
366 }
367 elsif ($event->{state} & urxvt::ControlMask) {
368 if ($keysym == 0xff51 || $keysym == 0xff53) {
369 my ($idx1) = grep $self->{tabs}[$_] == $tab, 0 .. $#{ $self->{tabs} };
370 my $idx2 = ($idx1 + ($keysym == 0xff51 ? -1 : +1)) % @{ $self->{tabs} };
371
372 ($self->{tabs}[$idx1], $self->{tabs}[$idx2]) =
373 ($self->{tabs}[$idx2], $self->{tabs}[$idx1]);
374
375 $self->make_current ($self->{tabs}[$idx2]);
376
377 return 1;
378 }
379 }
380
381 ()
382}
383
384sub tab_property_notify {
385 my ($self, $tab, $event) = @_;
386
387 $self->copy_properties
388 if $event->{window} == $tab->parent;
389
390 ()
391}
392
393sub tab_activity {
394 my ($self, $tab) = @_;
395
396 $self->refresh;
397}
398
198package urxvt::ext::tabbed::tab; 399package urxvt::ext::tabbed::tab;
199 400
200# helper extension implementing the subwindows of a tabbed terminal. 401# helper extension implementing the subwindows of a tabbed terminal.
201# simply proxies all interesting calls back to the tabbed class. 402# simply proxies all interesting calls back to the tabbed class.
202 403
203{ 404{
204 for my $hook qw(start destroy) { 405 for my $hook (qw(start destroy key_press property_notify)) {
205 eval qq{ 406 eval qq{
206 sub on_$hook { 407 sub on_$hook {
207 my \$parent = \$_[0]{term}{parent} 408 my \$parent = \$_[0]{term}{parent}
208 or return; 409 or return;
209 \$parent->tab_$hook (\@_) 410 \$parent->tab_$hook (\@_)
211 }; 412 };
212 die if $@; 413 die if $@;
213 } 414 }
214} 415}
215 416
417sub on_add_lines {
418 $_[0]->{activity}++
419 or $_[0]{term}{parent}->tab_activity ($_[0]);
420 ()
421}
216 422
217 423

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines