ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/perl/tabbed
Revision: 1.34
Committed: Wed Aug 15 19:41:37 2012 UTC (11 years, 9 months ago) by sf-exg
Branch: MAIN
Changes since 1.33: +3 -1 lines
Log Message:
Document ctrl-left and ctrl-right tabbed bindings, patch by Ryan Kavanagh.

File Contents

# User Rev Content
1 root 1.1 #! perl
2    
3 root 1.32 #: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 root 1.31
8 root 1.33 =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 sf-exg 1.34 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 root 1.33
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    
39 root 1.1 sub refresh {
40     my ($self) = @_;
41    
42 root 1.6 my $ncol = $self->ncol;
43    
44     my $text = " " x $ncol;
45 root 1.15 my $rend = [($self->{rs_tabbar}) x $ncol];
46 root 1.6
47     my @ofs;
48 root 1.1
49 root 1.7 substr $text, 0, 7, "[NEW] |";
50 root 1.15 @$rend[0 .. 5] = ($self->{rs_tab}) x 6;
51 root 1.7 push @ofs, [0, 6, sub { $_[0]->new_tab }];
52    
53     my $ofs = 7;
54     my $idx = 0;
55    
56 root 1.1 for my $tab (@{ $self->{tabs} }) {
57 root 1.7 $idx++;
58    
59 root 1.9 my $act = $tab->{activity} && $tab != $self->{cur}
60     ? "*" : " ";
61    
62     my $txt = "$act$idx$act";
63 root 1.6 my $len = length $txt;
64    
65     substr $text, $ofs, $len + 1, "$txt|";
66 root 1.15 @$rend[$ofs .. $ofs + $len - 1] = ($self->{rs_tab}) x $len
67 root 1.7 if $tab == $self->{cur};
68 root 1.6
69 root 1.7 push @ofs, [ $ofs, $ofs + $len, sub { $_[0]->make_current ($tab) } ];
70 root 1.1
71 root 1.6 $ofs += $len + 1;
72 root 1.1 }
73    
74     $self->{tabofs} = \@ofs;
75    
76 root 1.7 $self->ROW_t (0, $text, 0, 0, $ncol);
77     $self->ROW_r (0, $rend, 0, 0, $ncol);
78    
79     $self->want_refresh;
80 root 1.1 }
81    
82     sub new_tab {
83 root 1.21 my ($self, @argv) = @_;
84 root 1.1
85     # save a backlink to us, make sure tabbed is inactive
86     push @urxvt::TERM_INIT, sub {
87     my ($term) = @_;
88     $term->{parent} = $self;
89    
90 root 1.25 for (0 .. urxvt::NUM_RESOURCES - 1) {
91     my $value = $self->{resource}[$_];
92    
93     $term->resource ("+$_" => $value)
94     if defined $value;
95     }
96 root 1.1
97     $term->resource (perl_ext_2 => $term->resource ("perl_ext_2") . ",-tabbed");
98     };
99    
100     push @urxvt::TERM_EXT, urxvt::ext::tabbed::tab::;
101    
102     my $term = new urxvt::term
103     $self->env, $urxvt::RXVTNAME,
104     -embed => $self->parent,
105 root 1.21 @argv,
106 root 1.1 ;
107     }
108    
109     sub configure {
110     my ($self) = @_;
111    
112 root 1.5 my $tab = $self->{cur};
113    
114 root 1.14 # this is an extremely dirty way to force a configurenotify, but who cares
115 root 1.5 $tab->XMoveResizeWindow (
116     $tab->parent,
117 root 1.14 0, $self->{tabheight} + 1,
118 mikachu 1.30 $self->width, $self->height - $self->{tabheight}
119 root 1.13 );
120     $tab->XMoveResizeWindow (
121     $tab->parent,
122 root 1.4 0, $self->{tabheight},
123 mikachu 1.30 $self->width, $self->height - $self->{tabheight}
124 root 1.1 );
125 root 1.11 }
126    
127 root 1.19 sub on_resize_all_windows {
128     my ($self, $width, $height) = @_;
129    
130     1
131     }
132    
133 root 1.11 sub copy_properties {
134     my ($self) = @_;
135     my $tab = $self->{cur};
136 root 1.5
137     my $wm_normal_hints = $self->XInternAtom ("WM_NORMAL_HINTS");
138    
139 root 1.24 my $current = delete $self->{current_properties};
140 root 1.16
141 root 1.24 # pass 1: copy over properties different or nonexisting
142 root 1.5 for my $atom ($tab->XListProperties ($tab->parent)) {
143     my ($type, $format, $items) = $self->XGetWindowProperty ($tab->parent, $atom);
144 sf-exg 1.27
145 root 1.24 # fix up size hints
146 root 1.5 if ($atom == $wm_normal_hints) {
147     my (@hints) = unpack "l!*", $items;
148 sf-exg 1.27
149 root 1.19 $hints[$_] += $self->{tabheight} for (4, 6, 16);
150 sf-exg 1.27
151 root 1.19 $items = pack "l!*", @hints;
152     }
153 root 1.5
154 root 1.24 my $cur = delete $current->{$atom};
155 root 1.6
156 root 1.24 # 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 root 1.5 }
162 root 1.6
163 root 1.24 # pass 2, delete all extraneous properties
164     $self->XDeleteProperty ($self->parent, $_) for keys %$current;
165 root 1.1 }
166    
167     sub make_current {
168     my ($self, $tab) = @_;
169    
170     if (my $cur = $self->{cur}) {
171 root 1.9 delete $cur->{activity};
172 root 1.7 $cur->XUnmapWindow ($cur->parent) if $cur->mapped;
173     $cur->focus_out;
174 root 1.1 }
175 sf-exg 1.27
176 root 1.1 $self->{cur} = $tab;
177    
178     $self->configure;
179 root 1.11 $self->copy_properties;
180 sf-exg 1.27
181 root 1.20 $tab->focus_out; # just in case, should be a nop
182     $tab->focus_in if $self->focus;
183 sf-exg 1.27
184 root 1.1 $tab->XMapWindow ($tab->parent);
185 root 1.10 delete $tab->{activity};
186 root 1.1 $self->refresh;
187    
188     ()
189     }
190    
191 root 1.15 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 sf-exg 1.26 sub on_tt_write {
208     my ($self, $octets) = @_;
209    
210     $self->{cur}->tt_write ($octets);
211    
212     1
213     }
214    
215 root 1.15 sub on_key_press {
216     my ($self, $event) = @_;
217    
218     $self->{cur}->key_press ($event->{state}, $event->{keycode}, $event->{time});
219    
220     1
221     }
222    
223     sub on_key_release {
224     my ($self, $event) = @_;
225    
226     $self->{cur}->key_release ($event->{state}, $event->{keycode}, $event->{time});
227    
228     1
229     }
230    
231 root 1.1 sub on_button_press {
232     1
233     }
234    
235     sub on_button_release {
236     my ($self, $event) = @_;
237    
238     if ($event->{row} == 0) {
239 root 1.6 for my $button (@{ $self->{tabofs} }) {
240     $button->[2]->($self, $event)
241     if $event->{col} >= $button->[0]
242     && $event->{col} < $button->[1];
243 root 1.1 }
244     }
245    
246     1
247     }
248    
249     sub on_motion_notify {
250     1
251     }
252    
253     sub on_init {
254     my ($self) = @_;
255    
256 root 1.25 $self->{resource} = [map $self->resource ("+$_"), 0 .. urxvt::NUM_RESOURCES - 1];
257 root 1.1
258     $self->resource (int_bwidth => 0);
259 root 1.15 $self->resource (name => "URxvt.tabbed");
260 root 1.1 $self->resource (pty_fd => -1);
261    
262     $self->option ($urxvt::OPTION{scrollBar}, 0);
263    
264 root 1.15 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    
277 root 1.1 ()
278     }
279    
280     sub on_start {
281     my ($self) = @_;
282    
283 root 1.4 $self->{tabheight} = $self->int_bwidth + $self->fheight + $self->lineSpace;
284    
285 root 1.15 $self->cmd_parse ("\033[?25l");
286    
287 root 1.21 my @argv = $self->argv;
288    
289     do {
290     shift @argv;
291     } while @argv && $argv[0] ne "-e";
292    
293     $self->new_tab (@argv);
294 root 1.1
295     ()
296     }
297    
298     sub on_configure_notify {
299     my ($self, $event) = @_;
300    
301     $self->configure;
302 root 1.12 $self->refresh;
303 root 1.1
304     ()
305     }
306    
307     sub on_wm_delete_window {
308     my ($self) = @_;
309    
310     $_->destroy for @{ $self->{tabs} };
311    
312     1
313     }
314    
315     sub tab_start {
316     my ($self, $tab) = @_;
317    
318 root 1.11 $tab->XChangeInput ($tab->parent, urxvt::PropertyChangeMask);
319    
320 root 1.1 push @{ $self->{tabs} }, $tab;
321    
322 root 1.7 # $tab->{name} ||= scalar @{ $self->{tabs} };
323 root 1.1 $self->make_current ($tab);
324    
325     ()
326     }
327    
328     sub tab_destroy {
329     my ($self, $tab) = @_;
330    
331     $self->{tabs} = [ grep $_ != $tab, @{ $self->{tabs} } ];
332    
333     if (@{ $self->{tabs} }) {
334     if ($self->{cur} == $tab) {
335     delete $self->{cur};
336     $self->make_current ($self->{tabs}[-1]);
337 root 1.7 } else {
338     $self->refresh;
339 root 1.1 }
340     } else {
341     # delay destruction a tiny bit
342     $self->{destroy} = urxvt::iw->new->start->cb (sub { $self->destroy });
343     }
344    
345     ()
346     }
347    
348 root 1.8 sub tab_key_press {
349     my ($self, $tab, $event, $keysym, $str) = @_;
350    
351 root 1.10 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 sf-exg 1.27
360 root 1.10 return 1;
361     } elsif ($keysym == 0xff54) {
362     $self->new_tab;
363    
364     return 1;
365     }
366 root 1.8 }
367 root 1.22 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 root 1.8
381     ()
382     }
383    
384 root 1.11 sub tab_property_notify {
385     my ($self, $tab, $event) = @_;
386    
387     $self->copy_properties
388     if $event->{window} == $tab->parent;
389    
390     ()
391     }
392    
393 root 1.9 sub tab_activity {
394     my ($self, $tab) = @_;
395    
396     $self->refresh;
397     }
398    
399 root 1.1 package urxvt::ext::tabbed::tab;
400    
401     # helper extension implementing the subwindows of a tabbed terminal.
402     # simply proxies all interesting calls back to the tabbed class.
403    
404     {
405 root 1.11 for my $hook qw(start destroy key_press property_notify) {
406 root 1.1 eval qq{
407     sub on_$hook {
408     my \$parent = \$_[0]{term}{parent}
409     or return;
410     \$parent->tab_$hook (\@_)
411     }
412     };
413     die if $@;
414     }
415     }
416    
417 root 1.9 sub on_add_lines {
418     $_[0]->{activity}++
419     or $_[0]{term}{parent}->tab_activity ($_[0]);
420     ()
421     }
422 root 1.1
423