--- rxvt-unicode/src/perl/searchable-scrollback 2006/01/10 04:30:03 1.11 +++ rxvt-unicode/src/perl/searchable-scrollback 2014/05/17 17:12:29 1.34 @@ -2,21 +2,54 @@ # this extension implements scrollback buffer search +#:META:RESOURCE:%:string:activation hotkey keysym +#:META:BINDING:M-s:start + +=head1 NAME + +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. + +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. + +The regex defaults to "(?i)", resulting in a case-insensitive search. To +get a case-sensitive search you can delete this prefix using C +or simply use an uppercase character which removes the "(?i)" prefix. + +See L for more info about perl regular expression syntax. + +=cut + sub on_init { my ($self) = @_; - my $hotkey = $self->{argv}[0] || "M-s"; + # only for backwards compatibility + my $hotkey = $self->{argv}[0] + || $self->x_resource ("%") + || return; - $self->parse_keysym ($hotkey, "perl:searchable-scrollback:start") + $self->bind_action ($hotkey, "searchable-scrollback:start") # ugh or warn "unable to register '$hotkey' as scrollback search start hotkey\n"; () } -sub on_keyboard_command { - my ($self, $cmd) = @_; +sub on_action { + my ($self, $action) = @_; - $cmd eq "searchable-scrollback:start" + $action eq "start" and $self->enter; () @@ -34,9 +67,10 @@ return if $self->{overlay}; - $self->{history} = []; $self->{view_start} = $self->view_start; - $self->{pty_ev_events} = $self->pty_ev_events (urxvt::EVENT_NONE); + $self->{pty_ev_events} = $self->pty_ev_events (urxvt::EV_NONE); + $self->{row} = $self->nrow - 1; + $self->{search} = "(?i)"; $self->enable ( key_press => \&key_press, @@ -60,47 +94,45 @@ delete $self->{manpage_overlay}; delete $self->{overlay}; delete $self->{history}; + delete $self->{search}; } sub idle { my ($self) = @_; - $self->msg ("escape=exit, enter=accept, /=start search, n=next, p=previous, G=bottom"); - - delete $self->{in_search}; + $self->msg ("(escape cancels) /$self->{search}█"); } sub search { - my ($self, $offset) = @_; - - my $row = -$self->view_start + ($self->nrow >> 1) + $offset; + my ($self, $dir) = @_; delete $self->{found}; + my $row = $self->{row}; my $search = $self->special_encode ($self->{search}); no re 'eval'; # just to be sure - my $re = qr/$search/; + if (my $re = eval { qr/$search/ }) { + while ($self->nrow > $row && $row >= $self->top_row) { + my $line = $self->line ($row) + or last; + + my $text = $line->t; + if ($text =~ /$re/g) { + do { + push @{ $self->{found} }, [$line->coord_of ($-[0]), $line->coord_of ($+[0])]; + } while $text =~ /$re/g; - while ($row > -$self->nsaved) { - my $line = $self->line ($row) - or last; - - my $text = $line->t; - if ($text =~ /$re/g) { - do { - push @{ $self->{found} }, [$line->coord_of ($-[0]), $line->coord_of ($+[0])]; - } while $text =~ /$re/g; + $self->{row} = $row; + $self->view_start (List::Util::min 0, $row - ($self->nrow >> 1)); + $self->want_refresh; + last; + } - $self->view_start (-$row + ($self->nrow >> 1)); - last; + $row = $dir < 0 ? $line->beg - 1 : $line->end + 1; } - - $row = $line->beg - 1; } - $self->msg ("enter/type/backspace: /$self->{search}_" - . ($self->{found} ? "" : " (not found)")); $self->scr_bell unless $self->{found}; } @@ -109,8 +141,11 @@ return unless $self->{found}; - $self->scr_xor_span (@$_, urxvt::SET_FGCOLOR (urxvt::RS_RVid, 15)) - for @{ $self->{found} }; + my $xor = urxvt::RS_RVid | urxvt::RS_Blink; + for (@{ $self->{found} }) { + $self->scr_xor_span (@$_, $xor); + $xor = urxvt::RS_RVid; + } () } @@ -118,52 +153,34 @@ sub key_press { my ($self, $event, $keysym, $string) = @_; - if (exists $self->{in_search}) { - if ($keysym == 0xff0d || $keysym == 0xff8d) { - $self->idle; - } elsif ($keysym == 0xff1b) { - $self->view_start ($self->{in_search}); - $self->idle; - } elsif ($keysym == 0xff08) { - substr $self->{search}, -1, 1, ""; - $self->search; - } elsif ($string ne "") { - return; - } + delete $self->{manpage_overlay}; - } else { - if ($keysym == 0xff0d || $keysym == 0xff8d) { - # TODO: set selection - $self->leave; - } elsif ($keysym == 0xff1b) { - $self->view_start ($self->{view_start}); - $self->leave; - } elsif ($keysym == 0xff52) { - $self->view_start ($self->view_start + 1); - } elsif ($keysym == 0xff54) { - $self->view_start ($self->view_start - 1); - } elsif ($string eq "/") { - delete $self->{manpage_overlay}; - $self->{in_search} = $self->view_start; - $self->search; - } elsif ($string eq "G") { - $self->{history} = []; - $self->view_start (0); - } elsif ($string eq "n") { - if ($self->{found} && @{ $self->{found} }) { - push @{ $self->{history} }, [$self->view_start, $self->{found}]; - $self->search (-1); - $self->idle; - } - } elsif ($string eq "p" or $string eq "N") { - if (my $prev = pop @{ $self->{history} }) { - $self->view_start ($prev->[0]); - $self->{found} = $prev->[1]; - $self->want_refresh; - } - } elsif ($string ne "") { - $self->scr_bell; + if ($keysym == 0xff0d || $keysym == 0xff8d) { # enter + if ($self->{found} && $event->{state} & urxvt::ShiftMask) { + my ($br, $bc, $er, $ec) = @{ $self->{found}[0] }; + $self->selection_beg ($br, $bc); + $self->selection_end ($er, $ec); + $self->selection_make ($event->{time}); } + $self->leave; + } elsif ($keysym == 0xff1b) { # escape + $self->view_start ($self->{view_start}); + $self->leave; + } elsif ($keysym == 0xff57) { # end + $self->{row} = $self->nrow - 1; + $self->view_start (0); + } elsif ($keysym == 0xff52) { # up + $self->{row}-- if $self->{row} > $self->top_row; + $self->search (-1); + } elsif ($keysym == 0xff54) { # down + $self->{row}++ if $self->{row} < $self->nrow; + $self->search (+1); + } elsif ($keysym == 0xff08) { # backspace + substr $self->{search}, -1, 1, ""; + $self->search; + $self->idle; + } elsif ($string !~ /[\x00-\x1f\x80-\xaf]/) { + return; # pass to tt_write } 1 @@ -173,7 +190,12 @@ my ($self, $data) = @_; $self->{search} .= $self->locale_decode ($data); - $self->search; + + $self->{search} =~ s/^\(\?i\)// + if $self->{search} =~ /^\(.*[[:upper:]]/; + + $self->search (-1); + $self->idle; 1 }