--- rxvt-unicode/src/perl/searchable-scrollback 2006/01/09 22:41:41 1.1 +++ rxvt-unicode/src/perl/searchable-scrollback 2017/10/24 20:12:08 1.44 @@ -2,23 +2,100 @@ # this extension implements scrollback buffer search +#:META:RESOURCE:%:string:activation hotkey keysym + +=head1 NAME + +searchable-scrollback - incremental scrollback search (enabled by default) + +=head1 DESCRIPTION + +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. 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 +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 ("%") + || "M-s"; - $self->parse_keysym ($hotkey, "perl:searchable-scrollback:enter") - or warn "unable to register '$hotkey' as scrollback search enter hotkey\n"; + $self->bind_action ($hotkey, "%:start") + or warn "unable to register '$hotkey' as scrollback search start hotkey\n"; () } -sub on_keyboard_command { +sub on_user_command { my ($self, $cmd) = @_; - if ($cmd eq "searchable-scrollback:enter") { - $self->enter; - } + $cmd eq "searchable-scrollback:start" + and $self->enter; + + () +} + +sub on_action { + my ($self, $action) = @_; + + $action eq "start" + and $self->enter; () } @@ -35,112 +112,147 @@ return if $self->{overlay}; - $self->{pty_ev_events} = $self->pty_ev_events (urxvt::EVENT_NONE); $self->{view_start} = $self->view_start; + $self->{pty_ev_events} = $self->pty_ev_events (urxvt::EV_NONE); + $self->{row} = $self->nrow - 1; + $self->{search} = "(?i)"; $self->enable ( - refresh_begin => sub { - warn "beg(@_)\n"; - }, - refresh_end => sub { - warn "end(@_)\n"; - }, + key_press => \&key_press, + tt_write => \&tt_write, + refresh_begin => \&refresh, + refresh_end => \&refresh, ); + $self->{manpage_overlay} = $self->overlay (0, -2, $self->ncol, 1, urxvt::OVERLAY_RSTYLE, 0); + $self->{manpage_overlay}->set (0, 0, "scrollback search, see the ${urxvt::RXVTNAME}perl manpage for details"); + $self->idle; } sub leave { my ($self) = @_; - delete $self->{overlay}; - - $self->disable ("refresh_begin", "refresh_end"); - + $self->disable ("key_press", "tt_write", "refresh_begin", "refresh_end"); $self->pty_ev_events ($self->{pty_ev_events}); - $self->want_refresh; + + delete $self->{manpage_overlay}; + delete $self->{overlay}; + delete $self->{search}; + delete $self->{found}; } sub idle { my ($self) = @_; - $self->msg ("scrollback search, escape=exit, enter=accept, /=start search, n=next, p=previous"); - - delete $self->{in_search}; + $self->msg ("(escape cancels) /$self->{search}█"); } sub search { - my ($self) = @_; - - my $row = -$self->view_start; + my ($self, $dir, $row) = @_; - delete $self->{found}; + my $search = $self->special_encode ($self->{search}); no re 'eval'; # just to be sure - my $re = qr/$self->{search}/; - - 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 ($+[1])]; - } while $text =~ /$re/g; + 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) { + delete $self->{found}; + + 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; + return; + } - $self->view_start (-$row + ($self->nrow >> 1)); - last; + $row = $dir < 0 ? $line->beg - 1 : $line->end + 1; } + } + + $self->scr_bell; +} + +sub refresh { + my ($self) = @_; + + return unless $self->{found}; - $row = $line->beg - 1; + my $xor = urxvt::RS_RVid | urxvt::RS_Blink; + for (@{ $self->{found} }) { + $self->scr_xor_span (@$_, $xor); + $xor = urxvt::RS_RVid; } - $self->msg ("enter/type/backspace: /$self->{search}_" - . ($self->{found} ? " (not found)" : "")); - $self->scr_bell unless $self->{found}; + () } -sub on_key_press { +sub key_press { my ($self, $event, $keysym, $string) = @_; - $self->{overlay} - or return; - - if (exists $self->{in_search}) { - if ($keysym == 0xff0d || $keysym == 0xff8d) { - delete $self->{in_search}; - } elsif ($keysym == 0xff1b) { - $self->view_start (delete $self->{in_search}); - } elsif ($keysym == 0xff08) { - substr $self->{search}, -1, 1, ""; - } elsif ($string ne "") { - $self->{search} .= $string; - } + delete $self->{manpage_overlay}; - $self->search; - } else { - if ($keysym == 0xff0d || $keysym == 0xff8d) { - # OK - $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 "/") { - $self->{in_search} = $self->view_start; - $self->search; - } elsif ($string eq "n") { - } elsif ($string eq "p") { - } 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 + my $line = $self->line ($self->{row}); + $self->search (-1, $line->beg - 1) + if $line->beg > $self->top_row; + } elsif ($keysym == 0xff54) { # down + 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 (+1, $self->{row}); + $self->idle; + } elsif ($string !~ /[\x00-\x1f\x80-\xaf]/) { + return; # pass to tt_write } 1 } + +sub tt_write { + my ($self, $data) = @_; + + $self->{search} .= $self->locale_decode ($data); + + $self->{search} =~ s/^\(\?i\)// + if $self->{search} =~ /^\(.*[[:upper:]]/; + + delete $self->{found}; + $self->search (-1, $self->{row}); + $self->idle; + + 1 +}