--- rxvt-unicode/src/urxvt.pm 2012/06/05 21:29:52 1.207 +++ rxvt-unicode/src/urxvt.pm 2012/06/10 13:32:55 1.216 @@ -22,7 +22,7 @@ Every time 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 +Scripts are compiled in a 'use strict "vars"' and 'use utf8' environment, and thus must be encoded as UTF-8. Each script will only ever be loaded once, even in @@RXVT_NAME@@d, where @@ -757,15 +757,15 @@ package urxvt; use utf8; -use strict; +use strict 'vars'; use Carp (); use Scalar::Util (); use List::Util (); our $VERSION = 1; our $TERM; -our @TERM_INIT; -our @TERM_EXT; +our @TERM_INIT; # should go, prevents async I/O etc. +our @TERM_EXT; # should go, prevents async I/O etc. our @HOOKNAME; our %HOOKTYPE = map +($HOOKNAME[$_] => $_), 0..$#HOOKNAME; our %OPTION; @@ -945,28 +945,66 @@ no warnings 'utf8'; -sub perl_libdirs { - map { split /:/ } - $_[0]->resource ("perl_lib"), - $ENV{URXVT_PERL_LIB}, - "$ENV{HOME}/.urxvt/ext", - "$LIBDIR/perl" -} +sub parse_resource { + my ($term, $name, $isarg, $longopt, $flag, $value) = @_; -our %META; # meta header information from scripts -our %SCAN; # which dirs already scanned + $name =~ y/-/./ if $isarg; -sub resource { - my ($term, $name, $isarg, $flag, $value) = @_; + $term->scan_meta; - for my $dir (perl_libdirs $term) { + my $r = $term->{meta}{resource}; + keys %$r; # reste iterator + while (my ($pattern, $v) = each %$r) { + if ( + $pattern =~ /\.$/ + ? $pattern eq substr $name, 0, length $pattern + : $pattern eq $name + ) { + $name = "$urxvt::RESCLASS.$name"; + + push @{ $term->{perl_ext_3} }, $v->[0]; + + if ($v->[1] eq "boolean") { + $term->put_option_db ($name, $flag ? "true" : "false"); + return 1; + } else { + $term->put_option_db ($name, $value); + return 1 + 2; + } + } } - warn "resourece<@_>\n";#d# - 0 } +sub usage { + my ($term, $usage_type) = @_; + + $term->scan_meta; + + my $r = $term->{meta}{resource}; + + for my $pattern (sort keys %$r) { + my ($ext, $type, $desc) = @{ $r->{$pattern} }; + + $desc .= " (-pe $ext)"; + + if ($usage_type == 1) { + $pattern =~ y/./-/; + $pattern =~ s/-$/-.../g; + + if ($type eq "boolean") { + urxvt::log sprintf " -%-30s %s\n", "/+$pattern", $desc; + } else { + urxvt::log sprintf " -%-30s %s\n", "$pattern $type", $desc; + } + } else { + $pattern =~ s/\.$/.*/g; + urxvt::log sprintf " %-31s %s\n", "$pattern:", $type; + } + } +} + my $verbosity = $ENV{URXVT_PERL_VERBOSITY}; sub verbose { @@ -992,7 +1030,7 @@ or die "$path: $!"; my $source = - "package $pkg; use strict; use utf8; no warnings 'utf8';\n" + "package $pkg; use strict 'vars'; use utf8; no warnings 'utf8';\n" . "#line 1 \"$path\"\n{\n" . (do { local $/; <$fh> }) . "\n};\n1"; @@ -1012,7 +1050,7 @@ my $htype = shift; if ($htype == 0) { # INIT - my @dirs = perl_libdirs $TERM; + my @dirs = $TERM->perl_libdirs; my %ext_arg; @@ -1025,7 +1063,10 @@ $TERM->register_package ($_) for @pkg; } - for (grep $_, map { split /,/, $TERM->resource ("perl_ext_$_") } 1, 2) { + for ( + @{ delete $TERM->{perl_ext_3} }, + grep $_, map { split /,/, $TERM->resource ("perl_ext_$_") } 1, 2 + ) { if ($_ eq "default") { $ext_arg{$_} ||= [] for qw(selection option-popup selection-popup searchable-scrollback readline); } elsif (/^-(.*)$/) { @@ -1183,9 +1224,10 @@ The sole purpose of this class is to deliver an interface to the C module - any module using it will work inside urxvt without further programming. The only exception is that you cannot wait on -condition variables, but non-blocking condvar use is ok. What this means -is that you cannot use blocking APIs, but the non-blocking variant should -work. +condition variables, but non-blocking condvar use is ok. + +In practical terms this means is that you cannot use blocking APIs, but +the non-blocking variant should work. =cut @@ -1293,6 +1335,53 @@ } } +sub perl_libdirs { + map { split /:/ } + $_[0]->resource ("perl_lib"), + $ENV{URXVT_PERL_LIB}, + "$ENV{HOME}/.urxvt/ext", + "$LIBDIR/perl" +} + +sub scan_meta { + my ($self) = @_; + my @libdirs = perl_libdirs $self; + + return if $self->{meta_libdirs} eq join "\x00", @libdirs; + + my %meta; + + $self->{meta_libdirs} = join "\x00", @libdirs; + $self->{meta} = \%meta; + + for my $dir (reverse @libdirs) { + opendir my $fh, $dir + or next; + for my $ext (readdir $fh) { + $ext ne "." + and $ext ne ".." + and open my $fh, "<", "$dir/$ext" + or next; + + while (<$fh>) { + if (/^#:META:X_RESOURCE:(.*)/) { + my ($pattern, $type, $desc) = split /:/, $1; + $pattern =~ s/^%(\.|$)/$ext$1/g; # % in pattern == extension name + if ($pattern =~ /[^a-zA-Z0-9\-\.]/) { + warn "$dir/$ext: meta resource '$pattern' contains illegal characters (not alphanumeric nor . nor *)\n"; + } else { + $meta{resource}{$pattern} = [$ext, $type, $desc]; + } + } elsif (/^\s*(?:#|$)/) { + # skip other comments and empty lines + } else { + last; # stop parsing on first non-empty non-comment line + } + } + } + } +} + =item $term = new urxvt::term $envhashref, $rxvtname, [arg...] Creates a new terminal, very similar as if you had started it with system @@ -1419,6 +1508,22 @@ only one resource database per display, and later invocations might return the wrong resources. +=item $value = $term->x_resource_boolean ($pattern) + +Like C, above, but interprets the string value as a boolean +and returns C<1> for true values, C<0> for false values and C if +the resource or option isn't specified. + +You should always use this method to parse boolean resources. + +=cut + +sub x_resource_boolean { + my $res = &x_resource; + + $res =~ /^\s*(?:true|yes|on|1)\s*$/i ? 1 : defined $res && 0 +} + =item $success = $term->parse_keysym ($key, $octets) Adds a key binding exactly as specified via a resource. See the