ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/perl/tabbed
Revision: 1.33
Committed: Sun Jun 10 17:31:53 2012 UTC (11 years, 11 months ago) by root
Branch: MAIN
Changes since 1.32: +29 -0 lines
Log Message:
move perl docs to extensions

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