--- rxvt-unicode/src/urxvt.pm 2006/01/06 05:37:59 1.40 +++ rxvt-unicode/src/urxvt.pm 2006/01/07 20:23:52 1.45 @@ -19,8 +19,8 @@ =head1 DESCRIPTION -Everytime a terminal object gets created, scripts specified via the -C resource are loaded and associated with it. +Everytime a terminal object gets created, extension scripts specified via +the C resource are loaded and associated with it. Scripts are compiled in a 'use strict' and 'use utf8' environment, and thus must be encoded as UTF-8. @@ -64,7 +64,13 @@ =item mark-urls -Uses per-line filtering (C) to underline urls. +Uses per-line display filtering (C) to underline urls. + +=item block-graphics-to-ascii + +A not very useful example of filtering all text output to the terminal, +by replacing all line-drawing characters (U+2500 .. U+259F) by a +similar-looking ascii character. =item example-refresh-hooks @@ -72,13 +78,6 @@ window. Illustrates overwriting the refresh callbacks to create your own overlays or changes. -=item example-filter-input - -A not very useful example of filtering all text output to the terminal, by -underlining all urls that matches a certain regex (i.e. some urls :). It -is not very useful because urls that are output in multiple steps (e.g. -when typing them) do not get marked. - =back =head2 General API Considerations @@ -119,14 +118,15 @@ =head2 Hooks -The following subroutines can be declared in loaded scripts, and will be +The following subroutines can be declared in extension files, and will be called whenever the relevant event happens. The first argument passed to them is an object private to each terminal -and extension package. You can call all C methods on it, but +and extension package. You can call all C methods on it, but its not a real C object. Instead, the real C object that is shared between all packages is stored in the C -member. +member. It is, however, blessed intot he package of the extension script, +so for all practical purposes you can treat an extension script as a class. All of them must return a boolean value. If it is true, then the event counts as being I, and the invocation of other hooks is skipped, @@ -181,16 +181,6 @@ See the F example extension. -=item on_focus_in $term - -Called whenever the window gets the keyboard focus, before urxvt does -focus in processing. - -=item on_focus_out $term - -Called wheneever the window loses keyboard focus, before urxvt does focus -out processing. - =item on_view_change $term, $offset Called whenever the view offset changes, i..e the user or program @@ -263,6 +253,16 @@ C action bound to it (see description of the B resource in the @@RXVT_NAME@@(1) manpage). +=item on_focus_in $term + +Called whenever the window gets the keyboard focus, before rxvt-unicode +does focus in processing. + +=item on_focus_out $term + +Called wheneever the window loses keyboard focus, before rxvt-unicode does +focus out processing. + =item on_key_press $term, $event, $octets =item on_key_release $term, $event @@ -273,6 +273,10 @@ =item on_motion_notify $term, $event +=item on_map_notify $term, $event + +=item on_unmap_notify $term, $event + Called whenever the corresponding X event is received for the terminal If the hook returns true, then the even will be ignored by rxvt-unicode. @@ -293,8 +297,8 @@ =item $urxvt::TERM -The current terminal. Whenever a callback/Hook is bein executed, this -variable stores the current C object. +The current terminal. This variable stores the current C +object, whenever a callback/hook is executing. =back @@ -431,22 +435,23 @@ } } -my $script_pkg = "script0000"; -my %script_pkg; +my $extension_pkg = "extension0000"; +my %extension_pkg; # load a single script into its own package, once only -sub script_package($) { +sub extension_package($) { my ($path) = @_; - $script_pkg{$path} ||= do { - my $pkg = "urxvt::" . ($script_pkg++); + $extension_pkg{$path} ||= do { + my $pkg = "urxvt::" . ($extension_pkg++); - verbose 3, "loading script '$path' into package '$pkg'"; + verbose 3, "loading extension '$path' into package '$pkg'"; open my $fh, "<:raw", $path or die "$path: $!"; my $source = "package $pkg; use strict; use utf8;\n" + . "use base urxvt::term::proxy::;\n" . "#line 1 \"$path\"\n{\n" . (do { local $/; <$fh> }) . "\n};\n1"; @@ -467,11 +472,11 @@ if ($htype == 0) { # INIT my @dirs = ((split /:/, $TERM->resource ("perl_lib")), "$LIBDIR/perl"); - for my $ext (map { split /:/, $TERM->resource ("perl_ext_$_") } 1, 2) { + for my $ext (map { split /,/, $TERM->resource ("perl_ext_$_") } 1, 2) { my @files = grep -f $_, map "$_/$ext", @dirs; if (@files) { - register_package script_package $files[0]; + register_package extension_package $files[0]; } else { warn "perl extension '$ext' not found in perl library search path\n"; } @@ -487,14 +492,17 @@ keys %$cb; while (my ($pkg, $cb) = each %$cb) { - $retval = $cb->( - $TERM->{_pkg}{$pkg} ||= do { - my $proxy = bless { }, urxvt::term::proxy::; - Scalar::Util::weaken ($proxy->{term} = $TERM); - $proxy - }, - @_, - ) and last; + eval { + $retval = $cb->( + $TERM->{_pkg}{$pkg} ||= do { + my $proxy = bless { }, $pkg; + Scalar::Util::weaken ($proxy->{term} = $TERM); + $proxy + }, + @_, + ) and last; + }; + warn $@ if $@;#d# } } @@ -532,6 +540,14 @@ goto &$urxvt::term::proxy::AUTOLOAD; } +sub urxvt::destroy_hook::DESTROY { + ${$_[0]}->(); +} + +sub urxvt::destroy_hook(&) { + bless \shift, urxvt::destroy_hook:: +} + =head2 The C Class =over 4 @@ -579,7 +595,7 @@ sub urxvt::term::resource($$;$) { my ($self, $name) = (shift, shift); unshift @_, $self, $name, ($name =~ s/\s*\+\s*(\d+)$// ? $1 : 0); - goto &urxvt::term::_resource; + &urxvt::term::_resource } =item $rend = $term->rstyle ([$new_rstyle]) @@ -665,6 +681,33 @@ =back +=item $popup = $term->popup ($event) + +Creates a new C object that implements a popup menu. The +C<$event> I be the event causing the menu to pop up (a button event, +currently). + +=cut + +sub urxvt::term::popup { + my ($self, $event) = @_; + + $self->grab ($event->{time}, 1) + or return; + + my $popup = bless { + term => $self, + event => $event, + }, urxvt::popup::; + + Scalar::Util::weaken $popup->{term}; + + $self->{_destroy}{$popup} = urxvt::destroy_hook { $popup->{popup}->destroy }; + Scalar::Util::weaken $self->{_destroy}{$popup}; + + $popup +} + =item $cellwidth = $term->strwidth ($string) Returns the number of screen-cells this string would need. Correctly @@ -925,6 +968,43 @@ =back +=head2 The C Class + +=over 4 + +=cut + +package urxvt::popup; + +sub add_item { + my ($self, $item) = @_; + + push @{ $self->{item} }, $item; +} + +sub add_button { + my ($self, $text, $cb) = @_; + + $self->add_item ({ type => "button", text => "[ $text ]", activate => $cb }); +} + +sub show { + my ($self) = @_; + + local $urxvt::popup::self = $self; + + urxvt->new ("--perl-lib" => "", "--perl-ext-common" => "", "-pty-fd" => -1, "-sl" => 0, "-b" => 0, + "--transient-for" => $self->{term}->parent, + "-pe" => "urxvt_popup") + or die "unable to create popup window\n"; +} + +sub DESTROY { + my ($self) = @_; + + $self->{term}->ungrab; +} + =head2 The C Class This class implements timer watchers/events. Time is represented as a