--- rxvt-unicode/src/perl/matcher 2011/08/30 20:02:32 1.7 +++ rxvt-unicode/src/perl/matcher 2014/05/17 13:34:35 1.14 @@ -1,15 +1,61 @@ #! perl # Author: Tim Pope -# Bob Farrell +# Bob Farrell + +#:META:X_RESOURCE:%.launcher:string:default launcher command +#:META:X_RESOURCE:%.button:string:the button, yeah +#:META:X_RESOURCE:%.pattern.:string:extra pattern to match +#:META:X_RESOURCE:%.launcher.:string:custom launcher for pattern +#:META:X_RESOURCE:%.rend.:string:custom rednition for pattern + +=head1 NAME + +matcher - match strings in terminal output and change their rendition + +=head1 DESCRIPTION + +Uses per-line display filtering (C) to underline text +matching a certain pattern and make it clickable. When clicked with the +mouse button specified in the C resource (default 2, or +middle), the program specified in the C resource +(default, the C resource, C) will be started +with the matched text as first argument. The default configuration is +suitable for matching URLs and launching a web browser, like the +former "mark-urls" extension. + +The default pattern to match URLs can be overridden with the +C resource, and additional patterns can be specified +with numbered patterns, in a manner similar to the "selection" extension. +The launcher can also be overridden on a per-pattern basis. + +It is possible to activate the most recently seen match or a list of matches +from the keyboard. Simply bind a keysym to "perl:matcher:last" or +"perl:matcher:list" as seen in the example below. + +Example: load and use the matcher extension with defaults. + + URxvt.perl-ext: default,matcher + +Example: use a custom configuration. + + URxvt.url-launcher: sensible-browser + URxvt.keysym.C-Delete: perl:matcher:last + URxvt.keysym.M-Delete: perl:matcher:list + URxvt.matcher.button: 1 + URxvt.matcher.pattern.1: \\bwww\\.[\\w-]+\\.[\\w./?&@#-]*[\\w/-] + URxvt.matcher.pattern.2: \\B(/\\S+?):(\\d+)(?=:|$) + URxvt.matcher.launcher.2: gvim +$2 $1 + +=cut my $url = qr{ (?:https?://|ftp://|news://|mailto:|file://|\bwww\.) - [a-zA-Z0-9\-\@;\/?:&=%\$_.+!*\x27,~#]* + [\w\-\@;\/?:&=%\$.+!*\x27,~#]* ( - \([a-zA-Z0-9\-\@;\/?:&=%\$_.+!*\x27,~#]*\)| # Allow a pair of matched parentheses - [a-zA-Z0-9\-\@;\/?:&=%\$_+*~] # exclude some trailing characters (heuristic) + \([\w\-\@;\/?:&=%\$.+!*\x27,~#]*\)| # Allow a pair of matched parentheses + [\w\-\@;\/?:&=%\$+*~] # exclude some trailing characters (heuristic) )+ }x; @@ -32,29 +78,28 @@ $self->exec_async( $self->{launcher}, @args ); } +# backwards compat sub on_user_command { my ($self, $cmd) = @_; - if($cmd =~ s/^matcher:list\b//) { - $self->matchlist(); + if ($cmd =~ s/^matcher:list\b//) { + $self->matchlist; } else { - if($cmd =~ s/^matcher:last\b//) { + if ($cmd =~ s/^matcher:last\b//) { $self->most_recent; - } - # For backward compatibility - else { - if($cmd =~ s/^matcher\b//) { + } elsif ($cmd =~ s/^matcher\b//) { + # for backward compatibility $self->most_recent; } } - } + () } sub matchlist { my ($self) = @_; if ( $self->{showing} ) { - $self->{url_overlay}->hide(); + $self->{url_overlay}->hide; $self->{showing} = 0; return; } @@ -124,8 +169,7 @@ } sub my_resource { - my $self = shift; - $self->x_resource ("$self->{name}.$_[0]"); + $_[0]->x_resource ("%.$_[1]") } # turn a rendition spec in the resource into a sub that implements it on $_ @@ -146,18 +190,14 @@ sub on_start { my ($self) = @_; - ($self->{name} = __PACKAGE__) =~ s/.*:://; - $self->{name} =~ tr/_/-/; - $self->{launcher} = $self->my_resource("launcher") || - $self->x_resource("urlLauncher") || - "sensible-browser"; + $self->{launcher} = $self->my_resource ("launcher") || $self->x_resource("url-launcher") || "sensible-browser"; $self->{urls} = []; $self->{showing} = 0; $self->{button} = 2; $self->{state} = 0; - if($self->{argv}[0] || $self->my_resource("button")) { - my @mods = split('', $self->{argv}[0] || $self->my_resource("button")); + if($self->{argv}[0] || $self->my_resource ("button")) { + my @mods = split '', $self->{argv}[0] || $self->my_resource ("button"); for my $mod (@mods) { if($mod =~ /^\d+$/) { $self->{button} = $mod; @@ -168,19 +208,19 @@ } elsif($mod eq "M") { $self->{state} |= $self->ModMetaMask; } elsif($mod ne "-" && $mod ne " ") { - warn("$mod is invalid in $self->{name}<$self->{argv}[0]>\n"); + warn("$mod is invalid in $self->{_name}<$self->{argv}[0]>\n"); } } } my @defaults = ($url); my @matchers; - for (my $idx = 0; defined (my $res = $self->my_resource("pattern.$idx") || $defaults[$idx]); $idx++) { + for (my $idx = 0; defined (my $res = $self->my_resource ("pattern.$idx") || $defaults[$idx]); $idx++) { $res = $self->locale_decode ($res); utf8::encode $res; - my $launcher = $self->my_resource("launcher.$idx"); - $launcher =~ s/\$&|\$\{&\}/\${0}/g if ($launcher); - my $rend = $self->parse_rend($self->my_resource("rend.$idx")); + my $launcher = $self->my_resource ("launcher.$idx"); + $launcher =~ s/\$&|\$\{&\}/\${0}/g if $launcher; + my $rend = $self->parse_rend($self->my_resource ("rend.$idx")); unshift @matchers, [qr($res)x,$launcher,$rend]; } $self->{matchers} = \@matchers;