Skip to content

Instantly share code, notes, and snippets.

@jhedden
Forked from r4um/hlight.pl
Last active August 29, 2015 14:27
Show Gist options
  • Save jhedden/36df5e0d9725ac8be848 to your computer and use it in GitHub Desktop.
Save jhedden/36df5e0d9725ac8be848 to your computer and use it in GitHub Desktop.
rxvt-unicode highlighter module
#! perl
#
# rxvt-unicode highlighter module
# Highlight text on the console given regex and rendition string
# Specified via hlight.X and hlight.rend.X resources respectively
# Where X is any number for ex 0, 1 etc. for multiple entries.
#
# For example to hilight text "warning", put the following in .Xdefaults
#
# URxvt*hlight.0: warning
#
# By default the matched text is higlighted as bold white on red backgroud
# To specify a different coloring/attributes specify the rendition string in
# URxvt*hlight.rend.X resource corresponding to URxvt*hlight.X resource, For
# example to highlight the "warning" example above as italic, bold white on
# blue
#
# URxvt*hlight.rend.0: fg:9 bg:6 Italic
#
# A rendition string looks like "fg:17 bg:3 Bold" the numbers are color indexes
# plus 2, for ex. if you wish green color for foregroud the number would be 4
# ( 2 is the index for green + 2)
#
# The urxvt::RS_Bold, RS_Italic, RS_Blink, RS_RVid, RS_Uline bits can be turned on
# by putting it in the rendition string. For example to make the text italic put
# Italic in the rendition string which is basically the bit name excluding the RS_.
#
# To install put this file in /usr/lib/urxvt/perl and add hlight to
# URxvt*perl-ext-common resource in .Xdefaults
#
sub my_resource {
my $self = shift;
$self->x_resource ("$self->{name}.$_[0]");
}
# turn a rendition spec in the resource into a sub that implements it on $_
sub parse_rend {
my ($self, $str) = @_;
my ($mask, $fg, $bg, $failed) = $str ? urxvt::rend2mask($str)
: urxvt::rend2mask("fg:17 bg:3 Bold");
warn "Failed to parse rendition string: " . join(',', @$failed) if @$failed;
my @rend;
push @rend, sub { $_ |= $mask } if $mask;
push @rend, sub { $_ = urxvt::SET_FGCOLOR($_, $fg) } if defined $fg;
push @rend, sub { $_ = urxvt::SET_BGCOLOR($_, $bg) } if defined $bg;
sub {
for my $s ( @rend ) { &$s };
}
}
sub on_start {
my ($self) = @_;
($self->{name} = __PACKAGE__) =~ s/.*:://;
$self->{name} =~ tr/_/-/;
my @hlights;
for (my $idx = 0; defined (my $res = $self->my_resource("hlight.$idx"));
$idx++) {
$res = $self->locale_decode ($res);
utf8::encode $res;
my $rend = $self->parse_rend($self->my_resource("hlight.rend.$idx"));
unshift @hlights, [qr($res)x,$rend];
}
$self->{hlights} = \@hlights;
()
}
sub on_line_update {
my ($self, $row) = @_;
# fetch the line that has changed
my $line = $self->line ($row);
my $text = $line->t;
my $i = 0;
for my $hlight (@{$self->{hlights}}) {
while ($text =~ /$hlight->[0]/g) {
my $rend = $line->r;
&{$hlight->[1]}
for @{$rend}[ $-[0] .. $+[0] - 1];
$line->r ($rend);
}
}
()
}
#vim: set et ts=4 sw=3 tw=79:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment