summaryrefslogtreecommitdiffstats
path: root/src/perl/searchable-scrollback
blob: 7c944900870e44990ca784c4a7870fbea9d4029c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#! perl

# 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<searchable-scrollback:start> action (bound to C<M-s> 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<BackSpace>

Remove a character from the regex.

=item C<Insert>

Append the value of the PRIMARY selection to the regex.

=item C<Up>

Search for a match upwards.

=item C<Down>

Search for a match downwards.

=item C<Prior>

Scroll up.

=item C<Next>

Scroll down.

=item C<End>

Scroll to the bottom.

=item C<Escape>

Leave the mode and return to the point where search was started.

=item C<Enter> or C<Return>

Leave the mode maintaining the current position in the scrollback.
Additionally, if the C<Shift> 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<BackSpace>
or simply use an uppercase character which removes the "(?i)" prefix.

See L<perlre> for more info about perl regular expression syntax.

=cut

sub on_init {
   my ($self) = @_;

   # only for backwards compatibility
   my $hotkey = $self->{argv}[0]
                || $self->x_resource ("%")
                || "M-s";

   $self->bind_action ($hotkey, "%:start")
      or warn "unable to register '$hotkey' as scrollback search start hotkey\n";

   ()
}

sub on_user_command {
   my ($self, $cmd) = @_;

   $cmd eq "searchable-scrollback:start"
      and $self->enter;

   ()
}

sub on_action {
   my ($self, $action) = @_;

   $action eq "start"
      and $self->enter;

   ()
}

sub msg {
   my ($self, $msg) = @_;

   $self->{overlay} = $self->overlay (0, -1, $self->ncol, 1, urxvt::OVERLAY_RSTYLE, 0);
   $self->{overlay}->set (0, 0, $self->special_encode ($msg));
}

sub enter {
   my ($self) = @_;

   return if $self->{overlay};

   $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 (
      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) = @_;

   $self->disable ("key_press", "tt_write", "refresh_begin", "refresh_end");
   $self->pty_ev_events ($self->{pty_ev_events});

   delete $self->{manpage_overlay};
   delete $self->{overlay};
   delete $self->{search};
   delete $self->{found};
}

sub idle {
   my ($self) = @_;

   $self->msg ("(escape cancels) /$self->{search}█");
}

sub search {
   my ($self, $dir, $row) = @_;

   my $search = $self->special_encode ($self->{search});

   no re 'eval'; # just to be sure
   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;
         }

         $row = $dir < 0 ? $line->beg - 1 : $line->end + 1;
      }
   }

   $self->scr_bell;
}

sub refresh {
   my ($self) = @_;

   return unless $self->{found};

   my $xor = urxvt::RS_RVid | urxvt::RS_Blink;
   for (@{ $self->{found} }) {
      $self->scr_xor_span (@$_, $xor);
      $xor = urxvt::RS_RVid;
   }

   ()
}

sub key_press {
   my ($self, $event, $keysym, $string) =  @_;

   delete $self->{manpage_overlay};

   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
}