--- rxvt-unicode/src/urxvt.pm 2006/01/02 15:35:43 1.1 +++ rxvt-unicode/src/urxvt.pm 2006/01/02 20:40:20 1.9 @@ -1,25 +1,42 @@ =head1 NAME -urxvt - rxvt-unicode's embedded perl interpreter +rxvtperl - rxvt-unicode's embedded perl interpreter =head1 SYNOPSIS -Put your scripts into $LIBDIR/perl-init/, they will be loaded automatically. +* Put your scripts into F<@@RXVT_LIBDIR@@/urxvt/perl/>, they will be loaded automatically. -Each script will only be loaded once, even in urxvtd, and will be valid -globally. - -Scripts are evaluated in a 'use strict' and 'use utf8' environment, and -thus must be written in utf-8. +* Scripts are evaluated in a 'use strict' and 'use utf8' environment, and +thus must be encoded as UTF-8. sub on_sel_grab { warn "you selected ", $_[0]->selection; + () } 1 =head1 DESCRIPTION +Everytime a terminal object gets created, scripts specified via the +C resource are associated with it. + +Each script will only ever be loaded once, even in @@RXVT_NAME@@d, where +scripts will be shared (But not enabled) for all terminals. + +=head2 General API Considerations + +All objects (such as terminals, time watchers etc.) are typical +reference-to-hash objects. The hash can be used to store anything you +like. All members starting with an underscore (such as C<_ptr> or +C<_hook>) are reserved for internal uses and must not be accessed or +modified). + +When objects are destroyed on the C++ side, the perl object hashes are +emptied, so its best to store related objects such as time watchers and +the like inside the terminal object so they get destroyed as soon as the +terminal is destroyed. + =head2 Hooks The following subroutines can be declared in loaded scripts, and will be called @@ -121,18 +138,13 @@ =item urxvt::warn $string -Calls C witht eh given string which should not include a +Calls C with the given string which should not include a newline. The module also overwrites the C builtin with a function that calls this function. Using this function has the advantage that its output ends up in the correct place, e.g. on stderr of the connecting urxvtc client. -=item $cellwidth = urxvt::wcswidth $string - -Returns the number of screen-cells this string would need. Correctly -accounts for wide and combining characters. - =item $time = urxvt::NOW Returns the "current time" (as per the event loop). @@ -159,30 +171,12 @@ }; } -my $verbosity = $ENV{URXVT_PERL_VERBOSITY} || 10; +my @hook_count; +my $verbosity = $ENV{URXVT_PERL_VERBOSITY}; sub verbose { my ($level, $msg) = @_; - warn "$msg\n"; #d# -} - -my @invoke_cb; - -# called by the rxvt core -sub invoke { - local $term = shift; - my $htype = shift; - - my $cb = $invoke_cb[$htype]; - - verbose 10, "$HOOKNAME[$htype] (" . (join ", ", $term, @_) . ")" - if $verbosity >= 10; - - while (my ($k, $v) = each %$cb) { - return 1 if $v->($term, @_); - } - - 0 + warn "$msg\n" if $level <= $verbosity; } # find on_xxx subs in the package and register them @@ -190,14 +184,15 @@ sub register_package($) { my ($pkg) = @_; - for my $hook (0.. $#HOOKNAME) { - my $name = $HOOKNAME[$hook]; + for my $htype (0.. $#HOOKNAME) { + my $name = $HOOKNAME[$htype]; my $ref = $pkg->can ("on_" . lc $name) or next; - $invoke_cb[$hook]{$ref*1} = $ref; - set_should_invoke $hook, 1; + $term->{_hook}[$htype]{$ref*1} = $ref; + $hook_count[$htype]++ + or set_should_invoke $htype, 1; } } @@ -205,29 +200,67 @@ my %script_pkg; # load a single script into its own package, once only -sub load_script($) { +sub script_package($) { my ($path) = @_; $script_pkg{$path} ||= do { - my $pkg = $script_pkg++; + my $pkg = "urxvt::" . ($script_pkg++); + verbose 3, "loading script '$path' into package '$pkg'"; open my $fh, "<:raw", $path or die "$path: $!"; - eval "package $pkg; use strict; use utf8;\n" - . "#line 1 \"$path\"\n" - . do { local $/; <$fh> } - or die "$path: $@"; + my $source = "package $pkg; use strict; use utf8;\n" + . "#line 1 \"$path\"\n{\n" + . (do { local $/; <$fh> }) + . "\n};\n1"; - register_package $pkg; + eval $source or die "$path: $@"; $pkg - }; + } } -load_script $_ for grep -f $_, <$LIBDIR/perl-init/*>; +# called by the rxvt core +sub invoke { + local $term = shift; + my $htype = shift; + + if ($htype == 0) { # INIT + my @dirs = ((split /:/, $term->resource ("perl_lib")), "$LIBDIR/perl"); + + for my $ext (split /:/, $term->resource ("perl_ext")) { + my @files = grep -f $_, map "$_/$ext", @dirs; + + if (@files) { + register_package script_package $files[0]; + } else { + warn "perl extension '$ext' not found in perl library search path\n"; + } + } + + } elsif ($htype == 1) { # DESTROY + if (my $hook = $term->{_hook}) { + for my $htype (0..$#$hook) { + $hook_count[$htype] -= scalar keys %{ $hook->[$htype] || {} } + or set_should_invoke $htype, 0; + } + } + } + my $cb = $term->{_hook}[$htype] + or return; + + verbose 10, "$HOOKNAME[$htype] (" . (join ", ", $term, @_) . ")" + if $verbosity >= 10; + + while (my ($k, $v) = each %$cb) { + return 1 if $v->($term, @_); + } + + 0 +} =back @@ -235,6 +268,48 @@ =over 4 +=item $value = $term->resource ($name[, $newval]) + +Returns the current resource value associated with a given name and +optionally sets a new value. Setting values is most useful in the C +hook. Unset resources are returned and accepted as C. + +The new value must be properly encoded to a suitable character encoding +before passing it to this method. Similarly, the returned value may need +to be converted from the used encoding to text. + +Resource names are as defined in F. Colours can be specified +as resource names of the form C<< color+ >>, e.g. C. (will +likely change). + +Please note that resource strings will currently only be freed when the +terminal is destroyed, so changing options frequently will eat memory. + +Here is a a likely non-exhaustive list of resource names, not all of which +are supported in every build, please see the source to see the actual +list: + + answerbackstring backgroundPixmap backspace_key boldFont boldItalicFont + borderLess color cursorBlink cursorUnderline cutchars delete_key + display_name embed ext_bwidth fade font geometry hold iconName + imFont imLocale inputMethod insecure int_bwidth intensityStyles + italicFont jumpScroll lineSpace loginShell mapAlert menu meta8 modifier + mouseWheelScrollPage name pastableTabs path perl_eval perl_ext + perl_lib pointerBlank pointerBlankDelay preeditType print_pipe pty_fd + reverseVideo saveLines scrollBar scrollBar_align scrollBar_floating + scrollBar_right scrollBar_thickness scrollTtyKeypress scrollTtyOutput + scrollWithBuffer scrollstyle secondaryScreen secondaryScroll selectstyle + shade term_name title transparent transparent_all tripleclickwords + utmpInhibit visualBell + +=cut + +sub urxvt::term::resource($$;$) { + my ($self, $name) = (shift, shift); + unshift @_, $self, $name, ($name =~ s/\s*\+\s*(\d+)$// ? $1 : 0); + goto &urxvt::term::_resource; +} + =item ($row, $col) = $term->selection_mark ([$row, $col]) =item ($row, $col) = $term->selection_beg ([$row, $col]) @@ -265,7 +340,7 @@ my @lines = split /\n/, $text; my $w = 0; - for (map urxvt::wcswidth $_, @lines) { + for (map $self->strwidth ($_), @lines) { $w = $_ if $w < $_; } @@ -293,6 +368,25 @@ Write a string at the given position into the overlay. +=item $cellwidth = $term->strwidth $string + +Returns the number of screen-cells this string would need. Correctly +accounts for wide and combining characters. + +=item $octets = $term->locale_encode $string + +Convert the given text string into the corresponding locale encoding. + +=item $string = $term->locale_decode $octets + +Convert the given locale-encoded octets into a perl string. + +=item $term->tt_write ($octets) + +Write the octets given in C<$data> to the tty (i.e. as program input). To +pass characters instead of octets, you should convetr you strings first to +the locale-specific encoding using C<< $term->locale_encode >>. + =back =head2 The C Class @@ -392,6 +486,23 @@ =back +=head1 ENVIRONMENT + +=head2 URXVT_PERL_VERBOSITY + +This variable controls the verbosity level of the perl extension. Higher +numbers indicate more verbose output. + +=over 4 + +=item 0 - only fatal messages + +=item 3 - script loading and management + +=item 10 - all events received + +=back + =head1 AUTHOR Marc Lehmann