--- rxvt-unicode/src/urxvt.pm 2015/06/17 11:59:37 1.255 +++ rxvt-unicode/src/urxvt.pm 2022/12/29 01:10:08 1.272 @@ -6,24 +6,24 @@ =head1 SYNOPSIS - # create a file grab_test in $HOME: + # create a file grab_test in some directory: sub on_sel_grab { warn "you selected ", $_[0]->selection; () } - # start a urxvt using it: + # start a urxvt instance using it: - urxvt --perl-lib $HOME -pe grab_test + urxvt --perl-lib path/to/somedirectory -pe grab_test =head1 DESCRIPTION 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 "vars"' and 'use utf8' environment, and -thus must be encoded as UTF-8. +Scripts are compiled in a 'use strict qw(vars subs)' and 'use utf8' +environment, and thus must be encoded as UTF-8. Each script will only ever be loaded once, even in urxvtd, where scripts will be shared (but not enabled) for all terminals. @@ -72,7 +72,7 @@ Argument names also often indicate the type of a parameter. Here are some hints on what they mean: -=over 4 +=over =item $text @@ -117,11 +117,15 @@ =head2 META comments Rxvt-unicode recognizes special meta comments in extensions that define -different types of metadata. +different types of metadata. These comments are scanned whenever a +terminal is created and are typically used to autoload extensions when +their resources or command line parameters are used. + +Currently, it recognises these comments below. Individual components are +separated by colons (C<:>), and should not contain colons themselves - +there is also currently no escaping mechanism provided for this. -Currently, it recognises only one such comment: - -=over 4 +=over =item #:META:RESOURCE:name:type:desc @@ -129,6 +133,54 @@ C is the resource name, C is the resource type, C or C, and C is the resource description. +The extension will be autoloaded when this resource is specified or used +as a command line parameter. + +Example: matcher provides the C resource by having this +comment: + + #:META:RESOURCE:%.launcher:string:default launcher command + +Example: load this extension when the C<-tr> command line option or +resource name is used. + + #:META:RESOURCE:tr:boolean:set root pixmap as background + +=item #:META:OSC:number:desc + +The OSC comment specifies an OSC sequence, where C is the +numerical OSC code and C is a short description that is currently +unused. + +This will cause the extension to be autoloaded when the OSC sequence is +used for the first time. + +Note that autoloading carries some extra responsibilities with it: +although the terminal cannot really protect itself against malicious +sources of command sequences, therefore relying on the programs running +I to sanitize data that they output, it is very common for +programs to emit command sequences from untrusted sources. + +While this means that extensions should, as a defense-in-depth mechanism, +always consider whether OSC sequences are safe, autoloading automatically +exposes any autoloaded extension in all terminal windows, so extra care +should be taken. + +Example: the background extension registers OSC C<20> like this: + + #:META:OSC:20:change/query background image + +=item #:META:OSC_PERL:prefix:desc + +The same as the OSC comment, but for the Perl OSC sequence (C<777>). The +C should be unique among extensions, of course, which is most +easily arranged by using the extension name, although this is not +required. + +Example: the overlay-osc extension registers its Perl OSC like this: + + #:META:OSC_PERL:overlay:man overlay-osc + =back =head2 Hooks @@ -145,7 +197,19 @@ I<< When in doubt, return a false value (preferably C<()>). >> -=over 4 +=over + +=item on_attach $term + +Called when an extension package is attached to a running terminal +instance. Must return true in all cases, and runs with the same +limitations as C. + +Unlike C or C, this is called when the extension is +attached to a terminal, regardless of whether the extension is loaded +before or after the terminal is started. Extensions that need to do +something before they work can do it in this callback, as opposed to e.g. +C, which might not be called. =item on_init $term @@ -237,12 +301,12 @@ =item on_osc_seq_perl $term, $args, $resp -Called whenever the B command sequence (OSC = -operating system command) is processed. Cursor position and other state -information is up-to-date when this happens. For interoperability, the -string should start with the extension name (sans -osc) and a semicolon, -to distinguish it from commands for other extensions, and this might be -enforced in the future. +Called whenever the B command sequence +(OSC = operating system command) is processed. Cursor position and other +state information is up-to-date when this happens. For interoperability, +the argument should start with the extension name (sans -osc) or some +other suitable prefix, and a semicolon, to distinguish it from commands +for other extensions. For example, C uses this: @@ -397,7 +461,7 @@ package urxvt; use utf8; -use strict 'vars'; +use strict qw(vars subs); use Carp (); use Scalar::Util (); use List::Util (); @@ -419,7 +483,7 @@ =head2 Variables in the C Package -=over 4 +=over =item $urxvt::LIBDIR @@ -459,7 +523,7 @@ =head2 Functions in the C Package -=over 4 +=over =item urxvt::fatal $errormessage @@ -514,6 +578,18 @@ Various constants for use in X calls and event processing. +=item urxvt::PrivMode_132, PrivMode_132OK, PrivMode_rVideo, PrivMode_relOrigin, +PrivMode_Screen, PrivMode_Autowrap, PrivMode_aplCUR, PrivMode_aplKP, +PrivMode_HaveBackSpace, PrivMode_BackSpace, PrivMode_ShiftKeys, +PrivMode_VisibleCursor, PrivMode_MouseX10, PrivMode_MouseX11, +PrivMode_scrollBar, PrivMode_TtyOutputInh, PrivMode_Keypress, +PrivMode_smoothScroll, PrivMode_vt52, PrivMode_LFNL, PrivMode_MouseBtnEvent, +PrivMode_MouseAnyEvent, PrivMode_BracketPaste, PrivMode_ExtMouseUTF8, +PrivMode_ExtMouseUrxvt, PrivMode_BlinkingCursor, PrivMode_mouse_report, +PrivMode_Default + +Constants for checking DEC private modes. + =back =head2 RENDITION @@ -526,7 +602,7 @@ as they contain important information required for correct operation of rxvt-unicode. -=over 4 +=over =item $rend = urxvt::DEFAULT_RSTYLE @@ -590,6 +666,8 @@ $term->scan_extensions; + # iterating over all resources has quadratic time overhead + # overall, maybe this could be optimised? my $r = $term->{meta}{resource}; keys %$r; # reset iterator while (my ($k, $v) = each %$r) { @@ -606,6 +684,8 @@ push @{ $term->{perl_ext_3} }, $v->[0]; + return 1 unless $isarg; + if ($v->[1] eq "boolean") { $term->put_option_db ($name, $flag ? "true" : "false"); return 1; @@ -647,7 +727,7 @@ } } -my $verbosity = $ENV{URXVT_PERL_VERBOSITY}; +my $verbosity = $ENV{URXVT_PERL_VERBOSITY} // 2; sub verbose { my ($level, $msg) = @_; @@ -674,7 +754,7 @@ or die "$path: $!"; my $source = - "package $pkg; use strict 'vars'; use utf8; no warnings 'utf8';\n" + "package $pkg; use strict qw(vars subs); use utf8; no warnings 'utf8';\n" . "#line 1 \"$path\"\n{\n" . (do { local $/; <$fh> }) . "\n};\n1"; @@ -694,8 +774,6 @@ my $htype = shift; if ($htype == HOOK_INIT) { - my @dirs = $TERM->perl_libdirs; - $TERM->scan_extensions; my %ext_arg; @@ -710,13 +788,13 @@ } for ( + @{ delete $TERM->{perl_ext_3} }, (grep $_, map { split /,/, $TERM->resource ("perl_ext_$_") } 1, 2), - @{ delete $TERM->{perl_ext_3} } ) { if ($_ eq "default") { $ext_arg{$_} = [] - for qw(selection option-popup selection-popup readline searchable-scrollback); + for qw(selection option-popup selection-popup readline searchable-scrollback confirm-paste); for ($TERM->_keysym_resources) { next if /^(?:string|command|builtin|builtin-string|perl)/; @@ -727,24 +805,31 @@ $ext_arg{$ext} = []; } - } elsif (/^-(.*)$/) { + } elsif (/^-(.*)$/) { # remove from set delete $ext_arg{$1}; - } elsif (/^([^<]+)<(.*)>$/) { - push @{ $ext_arg{$1} }, $2; + } elsif (/^\/(.*)$/) { # prohibit loading + undef $TERM->{ext_prohibit}{$1}; + + } elsif (/^([^<]+)(?:<(.*)>)?$/) { # add to set, clear prohibit status + delete $TERM->{ext_prohibit}{$1}; + push @{ $ext_arg{$1} }, defined $2 ? $2 : (); } else { - $ext_arg{$_} ||= []; + verbose 2, "cannot parse extension specification '$_', ignoring."; } } + $TERM->set_should_invoke (HOOK_OSC_SEQ , +1) if $TERM->{meta}{autoload_osc}; + $TERM->set_should_invoke (HOOK_OSC_SEQ_PERL, +1) if $TERM->{meta}{autoload_osc_perl}; + for my $ext (sort keys %ext_arg) { - my @files = grep -f $_, map "$_/$ext", @dirs; + my $path = $TERM->extension_path ($ext); - if (@files) { - $TERM->register_package (extension_package $files[0], $ext_arg{$ext}); + if (defined $path) { + $TERM->autoload_extension ($ext, $ext_arg{$ext}); } else { - warn "perl extension '$ext' not found in perl library search path\n"; + verbose 2, "perl extension '$ext' not found in perl library search path"; } } @@ -752,6 +837,16 @@ warn $@ if $@; } + if ($htype == HOOK_OSC_SEQ) { + if (my $exts = delete $TERM->{meta}{autoload_osc}{$_[0]}) { + $TERM->autoload_extension ($_->[0]) for @$exts; + } + } elsif ($htype == HOOK_OSC_SEQ_PERL) { + if ($_[0] =~ /^([^;]+)/ and (my $exts = delete $TERM->{meta}{autoload_osc_perl}{$1})) { + $TERM->autoload_extension ($_->[0]) for @$exts; + } + } + $retval = undef; if (my $cb = $TERM->{_hook}[$htype]) { @@ -839,7 +934,7 @@ objects, in addition to call methods documented for the class. -=over 4 +=over =item $urxvt_term = $self->{term} @@ -1090,7 +1185,7 @@ =head2 The C Class -=over 4 +=over =cut @@ -1099,6 +1194,8 @@ sub register_package { my ($self, $pkg, $argv) = @_; + return if $self->{_pkg}{$pkg}; + no strict 'refs'; urxvt::verbose 6, "register package $pkg to $self"; @@ -1119,6 +1216,35 @@ $proxy->enable ($name => $ref); } } + + if (my $attach_hook = $pkg->can ("on_attach")) { + $attach_hook->($proxy) + or urxvt::verbose 1, "$pkg->on_attach returned false, extension failed to attach"; + } +} + +# map extension name to filesystem path +sub extension_path { + (grep -f $_, map "$_/$_[1]", $_[0]->perl_libdirs)[0] +} + +# load an extension by name +sub load_extension_file { + my ($self, $path, $argv) = @_; + + $self->register_package (urxvt::extension_package $path, $argv); +} + +# autoload an extension unless loading it is prohibited +sub autoload_extension { + my ($self, $name, $argv) = @_; + + return if exists $self->{ext_prohibit}{$name}; + + my $path = $self->extension_path ($name) + // return urxvt::verbose 2, "perl extension '$name' not found in perl library search path (during autoload)"; + + $self->load_extension_file ($path, $argv); } sub perl_libdirs { @@ -1138,7 +1264,7 @@ my @urxvtdirs = perl_libdirs $self; # my @cpandirs = grep -d, map "$_/URxvt/Ext", @INC; - $self->{meta} = \my %meta; + $self->{meta} = \my %allmeta; # first gather extensions @@ -1161,7 +1287,7 @@ $ext =~ s/\.uext$// or $core or next; - my %ext = (dir => $dir); + my %meta = (dir => $dir); while (<$fh>) { if (/^#:META:(?:X_)?RESOURCE:(.*)/) { @@ -1170,16 +1296,24 @@ if ($pattern =~ /[^a-zA-Z0-9\-\.]/) { warn "$dir/$ext: meta resource '$pattern' contains illegal characters (not alphanumeric nor . nor *)\n"; } else { - $ext{resource}{$pattern} = [$ext, $type, $desc]; + $meta{resource}{$pattern} = [$ext, $type, $desc]; } + + } elsif (/^#:META:OSC:([0-9]+):(.*)/) { + push @{$allmeta{autoload_osc}{$1}}, [$ext, $2]; + + } elsif (/^#:META:OSC_PERL:([^:]+):(.*)/) { + push @{$allmeta{autoload_osc_perl}{$1}}, [$ext, $2]; + } elsif (/^\s*(?:#|$)/) { # skip other comments and empty lines + } else { last; # stop parsing on first non-empty non-comment line } } - $meta{ext}{$ext} = \%ext; + $allmeta{ext}{$ext} = \%meta; } }; @@ -1188,9 +1322,9 @@ # and now merge resources - $meta{resource} = \my %resource; + $allmeta{resource} = \my %resource; - while (my ($k, $v) = each %{ $meta{ext} }) { + while (my ($k, $v) = each %{ $allmeta{ext} }) { #TODO: should check for extensions overriding each other %resource = (%resource, %{ $v->{resource} }); } @@ -1232,6 +1366,10 @@ the user environment before exec'ing the command (e.g. C) and should be preferred over explicit calls to C or C. +It also sets the C environment variable to the window +ID of the terminal (C<< $self->parent >>), similar to the C +variable set for the process spawned inside the terminal. + Returns the pid of the subprocess or C on error. =cut @@ -1244,7 +1382,10 @@ return $pid if !defined $pid or $pid; - %ENV = %{ $self->env }; + %ENV = ( + %{ $self->env }, + URXVT_EXT_WINDOWID => $self->parent, + ); exec @_; urxvt::_exit 255; @@ -1265,7 +1406,7 @@ pointerBlank reverseVideo scrollBar scrollBar_floating scrollBar_right scrollTtyKeypress scrollTtyOutput scrollWithBuffer secondaryScreen secondaryScroll skipBuiltinGlyphs skipScroll transparent tripleclickwords - urgentOnBell utmpInhibit visualBell + urgentOnBell utmpInhibit visualBell disablePasteBrackets =item $value = $term->resource ($name[, $newval]) @@ -1301,7 +1442,7 @@ scrollTtyKeypress scrollTtyOutput scrollWithBuffer scrollstyle secondaryScreen secondaryScroll shade skipBuiltinGlyphs skipScroll term_name title transient_for transparent tripleclickwords urgentOnBell - utmpInhibit visualBell + utmpInhibit visualBell rewrapMode disablePasteBrackets =cut @@ -1458,7 +1599,7 @@ The methods currently supported on C objects are: -=over 4 +=over =item $overlay->set ($x, $y, $text[, $rend]) @@ -1512,11 +1653,13 @@ =item $octets = $term->locale_encode ($string) -Convert the given text string into the corresponding locale encoding. +Convert the given text string into the corresponding locale +encoding. Returns C if C<$string> is C. =item $string = $term->locale_decode ($octets) -Convert the given locale-encoded octets into a perl string. +Convert the given locale-encoded octets into a perl string. Returns +C if C<$octets> is C. =item $term->scr_xor_span ($beg_row, $beg_col, $end_row, $end_col[, $rstyle]) @@ -1691,11 +1834,22 @@ Returns whether the cursor is currently hidden or not. +=item $priv_modes = $term->priv_modes + +Returns a bitset with the state of DEC private modes. + +Example: + + if ($term->priv_modes & urxvt::PrivMode_mouse_report) { + # mouse reporting is turned on + } + =item $view_start = $term->view_start ([$newvalue]) -Returns the row number of the topmost displayed line. Maximum value is -C<0>, which displays the normal terminal contents. Lower values scroll -this many lines into the scrollback buffer. +Returns the row number of the topmost displayed line and changes it, +if an argument is given. Values greater than or equal to C<0> display +the terminal contents. Lower values scroll this many lines into the +scrollback buffer. =item $term->want_refresh @@ -1763,7 +1917,7 @@ about the logical line that row C<$row_number> is part of. It supports the following methods: -=over 4 +=over =item $text = $line->t ([$new_text]) @@ -1869,15 +2023,15 @@ ) } -=item $text = $term->special_encode $string +=item $text = $term->special_encode ($string) Converts a perl string into the special encoding used by rxvt-unicode, where one character corresponds to one screen cell. See C<< $term->ROW_t >> for details. -=item $string = $term->special_decode $text +=item $string = $term->special_decode ($text) -Converts rxvt-unicodes text representation into a perl string. See +Converts rxvt-unicode's text representation into a perl string. See C<< $term->ROW_t >> for details. =item $success = $term->grab_button ($button, $modifiermask[, $window = $term->vt]) @@ -1953,7 +2107,7 @@ =head2 The C Class -=over 4 +=over =cut @@ -2099,7 +2253,7 @@ sprintf "%2d:%02d:%02d", (localtime urxvt::NOW)[2,1,0]); }); -=over 4 +=over =item $timer = new urxvt::timer @@ -2158,7 +2312,7 @@ }); -=over 4 +=over =item $iow = new urxvt::iow @@ -2195,7 +2349,7 @@ the process is idle. They should return as fast as possible, after doing some useful work. -=over 4 +=over =item $iw = new urxvt::iw @@ -2230,7 +2384,7 @@ ... }); -=over 4 +=over =item $pw = new urxvt::pw @@ -2257,9 +2411,11 @@ This variable controls the verbosity level of the perl extension. Higher numbers indicate more verbose output. -=over 4 +=over + +=item == 0 - fatal messages only -=item == 0 - fatal messages +=item >= 2 - general warnings (default level) =item >= 3 - script loading and management