--- rxvt-unicode/src/perl/searchable-scrollback 2014/05/17 13:34:43 1.31 +++ rxvt-unicode/src/perl/searchable-scrollback 2017/10/24 20:12:08 1.44 @@ -2,27 +2,63 @@ # this extension implements scrollback buffer search -#:META:X_RESOURCE:%:string:activation hotkey keysym -#:META:KEYSYM:M-s:searchable-scrollback:start +#:META:RESOURCE:%:string:activation hotkey keysym =head1 NAME -searchable-scrollback - incremental scrollback search (enabled by default) +searchable-scrollback - incremental scrollback search (enabled by default) =head1 DESCRIPTION -Adds regex search functionality to the scrollback buffer, triggered -by a hotkey (default: C). While in search mode, normal terminal -input/output is suspended and a regex is displayed at the bottom of the -screen. +Adds regex search functionality to the scrollback buffer, triggered by +the C action (bound to C by +default). While in search mode, normal terminal input/output is +suspended and a regex is displayed at the bottom of the screen. Inputting characters appends them to the regex and continues incremental -search. C removes a character from the regex, C and C -search upwards/downwards in the scrollback buffer, C jumps to the -bottom. C leaves search mode and returns to the point where search -was started, while C or C stay at the current position and -additionally stores the first match in the current line into the primary -selection if the C modifier is active. +search. In addition, the following bindings are recognized: + +=over 4 + +=item C + +Remove a character from the regex. + +=item C + +Append the value of the PRIMARY selection to the regex. + +=item C + +Search for a match upwards. + +=item C + +Search for a match downwards. + +=item C + +Scroll up. + +=item C + +Scroll down. + +=item C + +Scroll to the bottom. + +=item C + +Leave the mode and return to the point where search was started. + +=item C or C + +Leave the mode maintaining the current position in the scrollback. +Additionally, if the C modifier is active, store the first +match in the current line into the primary selection. + +=back The regex defaults to "(?i)", resulting in a case-insensitive search. To get a case-sensitive search you can delete this prefix using C @@ -35,11 +71,12 @@ sub on_init { my ($self) = @_; + # only for backwards compatibility my $hotkey = $self->{argv}[0] || $self->x_resource ("%") || "M-s"; - $self->parse_keysym ($hotkey, "perl:searchable-scrollback:start") + $self->bind_action ($hotkey, "%:start") or warn "unable to register '$hotkey' as scrollback search start hotkey\n"; () @@ -54,6 +91,15 @@ () } +sub on_action { + my ($self, $action) = @_; + + $action eq "start" + and $self->enter; + + () +} + sub msg { my ($self, $msg) = @_; @@ -92,8 +138,8 @@ delete $self->{manpage_overlay}; delete $self->{overlay}; - delete $self->{history}; delete $self->{search}; + delete $self->{found}; } sub idle { @@ -103,10 +149,7 @@ } sub search { - my ($self, $dir) = @_; - - delete $self->{found}; - my $row = $self->{row}; + my ($self, $dir, $row) = @_; my $search = $self->special_encode ($self->{search}); @@ -118,6 +161,8 @@ my $text = $line->t; if ($text =~ /$re/g) { + delete $self->{found}; + do { push @{ $self->{found} }, [$line->coord_of ($-[0]), $line->coord_of ($+[0])]; } while $text =~ /$re/g; @@ -125,14 +170,14 @@ $self->{row} = $row; $self->view_start (List::Util::min 0, $row - ($self->nrow >> 1)); $self->want_refresh; - last; + return; } $row = $dir < 0 ? $line->beg - 1 : $line->end + 1; } } - $self->scr_bell unless $self->{found}; + $self->scr_bell; } sub refresh { @@ -169,14 +214,24 @@ $self->{row} = $self->nrow - 1; $self->view_start (0); } elsif ($keysym == 0xff52) { # up - $self->{row}-- if $self->{row} > $self->top_row; - $self->search (-1); + my $line = $self->line ($self->{row}); + $self->search (-1, $line->beg - 1) + if $line->beg > $self->top_row; } elsif ($keysym == 0xff54) { # down - $self->{row}++ if $self->{row} < $self->nrow; - $self->search (+1); + my $line = $self->line ($self->{row}); + $self->search (+1, $line->end + 1) + if $line->end < $self->nrow; + } elsif ($keysym == 0xff55) { # prior + my $row = $self->view_start - ($self->nrow - 1); + $self->view_start (List::Util::min 0, $row); + } elsif ($keysym == 0xff56) { # next + my $row = $self->view_start + ($self->nrow - 1); + $self->view_start (List::Util::min 0, $row); + } elsif ($keysym == 0xff63) { # insert + $self->selection_request (urxvt::CurrentTime, 1); } elsif ($keysym == 0xff08) { # backspace substr $self->{search}, -1, 1, ""; - $self->search; + $self->search (+1, $self->{row}); $self->idle; } elsif ($string !~ /[\x00-\x1f\x80-\xaf]/) { return; # pass to tt_write @@ -193,7 +248,8 @@ $self->{search} =~ s/^\(\?i\)// if $self->{search} =~ /^\(.*[[:upper:]]/; - $self->search (-1); + delete $self->{found}; + $self->search (-1, $self->{row}); $self->idle; 1