1 | #! perl |
1 | #! perl |
2 | |
2 | |
3 | # this extension implements scrollback buffer search |
3 | # this extension implements scrollback buffer search |
4 | |
4 | |
5 | #:META:X_RESOURCE:%:string:activation hotkey keysym |
5 | #:META:RESOURCE:%:string:activation hotkey keysym |
|
|
6 | |
|
|
7 | =head1 NAME |
|
|
8 | |
|
|
9 | searchable-scrollback - incremental scrollback search (enabled by default) |
|
|
10 | |
|
|
11 | =head1 DESCRIPTION |
|
|
12 | |
|
|
13 | Adds regex search functionality to the scrollback buffer, triggered by |
|
|
14 | the C<searchable-scrollback:start> action (bound to C<M-s> by |
|
|
15 | default). While in search mode, normal terminal input/output is |
|
|
16 | suspended and a regex is displayed at the bottom of the screen. |
|
|
17 | |
|
|
18 | Inputting characters appends them to the regex and continues incremental |
|
|
19 | search. In addition, the following bindings are recognized: |
|
|
20 | |
|
|
21 | =over 4 |
|
|
22 | |
|
|
23 | =item C<BackSpace> |
|
|
24 | |
|
|
25 | Remove a character from the regex. |
|
|
26 | |
|
|
27 | =item C<Insert> |
|
|
28 | |
|
|
29 | Append the value of the PRIMARY selection to the regex. |
|
|
30 | |
|
|
31 | =item C<Up> |
|
|
32 | |
|
|
33 | Search for a match upwards. |
|
|
34 | |
|
|
35 | =item C<Down> |
|
|
36 | |
|
|
37 | Search for a match downwards. |
|
|
38 | |
|
|
39 | =item C<Prior> |
|
|
40 | |
|
|
41 | Scroll up. |
|
|
42 | |
|
|
43 | =item C<Next> |
|
|
44 | |
|
|
45 | Scroll down. |
|
|
46 | |
|
|
47 | =item C<End> |
|
|
48 | |
|
|
49 | Scroll to the bottom. |
|
|
50 | |
|
|
51 | =item C<Escape> |
|
|
52 | |
|
|
53 | Leave the mode and return to the point where search was started. |
|
|
54 | |
|
|
55 | =item C<Enter> or C<Return> |
|
|
56 | |
|
|
57 | Leave the mode maintaining the current position in the scrollback. |
|
|
58 | Additionally, if the C<Shift> modifier is active, store the first |
|
|
59 | match in the current line into the primary selection. |
|
|
60 | |
|
|
61 | =back |
|
|
62 | |
|
|
63 | The regex defaults to "(?i)", resulting in a case-insensitive search. To |
|
|
64 | get a case-sensitive search you can delete this prefix using C<BackSpace> |
|
|
65 | or simply use an uppercase character which removes the "(?i)" prefix. |
|
|
66 | |
|
|
67 | See L<perlre> for more info about perl regular expression syntax. |
|
|
68 | |
|
|
69 | =cut |
6 | |
70 | |
7 | sub on_init { |
71 | sub on_init { |
8 | my ($self) = @_; |
72 | my ($self) = @_; |
9 | |
73 | |
|
|
74 | # only for backwards compatibility |
10 | my $hotkey = $self->{argv}[0] |
75 | my $hotkey = $self->{argv}[0] |
11 | || $self->x_resource ("%") |
76 | || $self->x_resource ("%") |
12 | || "M-s"; |
77 | || "M-s"; |
13 | |
78 | |
14 | $self->parse_keysym ($hotkey, "perl:searchable-scrollback:start") |
79 | $self->bind_action ($hotkey, "%:start") |
15 | or warn "unable to register '$hotkey' as scrollback search start hotkey\n"; |
80 | or warn "unable to register '$hotkey' as scrollback search start hotkey\n"; |
16 | |
81 | |
17 | () |
82 | () |
18 | } |
83 | } |
19 | |
84 | |
20 | sub on_user_command { |
85 | sub on_user_command { |
21 | my ($self, $cmd) = @_; |
86 | my ($self, $cmd) = @_; |
22 | |
87 | |
23 | $cmd eq "searchable-scrollback:start" |
88 | $cmd eq "searchable-scrollback:start" |
|
|
89 | and $self->enter; |
|
|
90 | |
|
|
91 | () |
|
|
92 | } |
|
|
93 | |
|
|
94 | sub on_action { |
|
|
95 | my ($self, $action) = @_; |
|
|
96 | |
|
|
97 | $action eq "start" |
24 | and $self->enter; |
98 | and $self->enter; |
25 | |
99 | |
26 | () |
100 | () |
27 | } |
101 | } |
28 | |
102 | |
… | |
… | |
62 | $self->disable ("key_press", "tt_write", "refresh_begin", "refresh_end"); |
136 | $self->disable ("key_press", "tt_write", "refresh_begin", "refresh_end"); |
63 | $self->pty_ev_events ($self->{pty_ev_events}); |
137 | $self->pty_ev_events ($self->{pty_ev_events}); |
64 | |
138 | |
65 | delete $self->{manpage_overlay}; |
139 | delete $self->{manpage_overlay}; |
66 | delete $self->{overlay}; |
140 | delete $self->{overlay}; |
67 | delete $self->{history}; |
|
|
68 | delete $self->{search}; |
141 | delete $self->{search}; |
|
|
142 | delete $self->{found}; |
69 | } |
143 | } |
70 | |
144 | |
71 | sub idle { |
145 | sub idle { |
72 | my ($self) = @_; |
146 | my ($self) = @_; |
73 | |
147 | |
74 | $self->msg ("(escape cancels) /$self->{search}█"); |
148 | $self->msg ("(escape cancels) /$self->{search}█"); |
75 | } |
149 | } |
76 | |
150 | |
77 | sub search { |
151 | sub search { |
78 | my ($self, $dir) = @_; |
152 | my ($self, $dir, $row) = @_; |
79 | |
|
|
80 | delete $self->{found}; |
|
|
81 | my $row = $self->{row}; |
|
|
82 | |
153 | |
83 | my $search = $self->special_encode ($self->{search}); |
154 | my $search = $self->special_encode ($self->{search}); |
84 | |
155 | |
85 | no re 'eval'; # just to be sure |
156 | no re 'eval'; # just to be sure |
86 | if (my $re = eval { qr/$search/ }) { |
157 | if (my $re = eval { qr/$search/ }) { |
… | |
… | |
88 | my $line = $self->line ($row) |
159 | my $line = $self->line ($row) |
89 | or last; |
160 | or last; |
90 | |
161 | |
91 | my $text = $line->t; |
162 | my $text = $line->t; |
92 | if ($text =~ /$re/g) { |
163 | if ($text =~ /$re/g) { |
|
|
164 | delete $self->{found}; |
|
|
165 | |
93 | do { |
166 | do { |
94 | push @{ $self->{found} }, [$line->coord_of ($-[0]), $line->coord_of ($+[0])]; |
167 | push @{ $self->{found} }, [$line->coord_of ($-[0]), $line->coord_of ($+[0])]; |
95 | } while $text =~ /$re/g; |
168 | } while $text =~ /$re/g; |
96 | |
169 | |
97 | $self->{row} = $row; |
170 | $self->{row} = $row; |
98 | $self->view_start (List::Util::min 0, $row - ($self->nrow >> 1)); |
171 | $self->view_start (List::Util::min 0, $row - ($self->nrow >> 1)); |
99 | $self->want_refresh; |
172 | $self->want_refresh; |
100 | last; |
173 | return; |
101 | } |
174 | } |
102 | |
175 | |
103 | $row = $dir < 0 ? $line->beg - 1 : $line->end + 1; |
176 | $row = $dir < 0 ? $line->beg - 1 : $line->end + 1; |
104 | } |
177 | } |
105 | } |
178 | } |
106 | |
179 | |
107 | $self->scr_bell unless $self->{found}; |
180 | $self->scr_bell; |
108 | } |
181 | } |
109 | |
182 | |
110 | sub refresh { |
183 | sub refresh { |
111 | my ($self) = @_; |
184 | my ($self) = @_; |
112 | |
185 | |
… | |
… | |
139 | $self->leave; |
212 | $self->leave; |
140 | } elsif ($keysym == 0xff57) { # end |
213 | } elsif ($keysym == 0xff57) { # end |
141 | $self->{row} = $self->nrow - 1; |
214 | $self->{row} = $self->nrow - 1; |
142 | $self->view_start (0); |
215 | $self->view_start (0); |
143 | } elsif ($keysym == 0xff52) { # up |
216 | } elsif ($keysym == 0xff52) { # up |
144 | $self->{row}-- if $self->{row} > $self->top_row; |
217 | my $line = $self->line ($self->{row}); |
145 | $self->search (-1); |
218 | $self->search (-1, $line->beg - 1) |
|
|
219 | if $line->beg > $self->top_row; |
146 | } elsif ($keysym == 0xff54) { # down |
220 | } elsif ($keysym == 0xff54) { # down |
147 | $self->{row}++ if $self->{row} < $self->nrow; |
221 | my $line = $self->line ($self->{row}); |
148 | $self->search (+1); |
222 | $self->search (+1, $line->end + 1) |
|
|
223 | if $line->end < $self->nrow; |
|
|
224 | } elsif ($keysym == 0xff55) { # prior |
|
|
225 | my $row = $self->view_start - ($self->nrow - 1); |
|
|
226 | $self->view_start (List::Util::min 0, $row); |
|
|
227 | } elsif ($keysym == 0xff56) { # next |
|
|
228 | my $row = $self->view_start + ($self->nrow - 1); |
|
|
229 | $self->view_start (List::Util::min 0, $row); |
|
|
230 | } elsif ($keysym == 0xff63) { # insert |
|
|
231 | $self->selection_request (urxvt::CurrentTime, 1); |
149 | } elsif ($keysym == 0xff08) { # backspace |
232 | } elsif ($keysym == 0xff08) { # backspace |
150 | substr $self->{search}, -1, 1, ""; |
233 | substr $self->{search}, -1, 1, ""; |
151 | $self->search; |
234 | $self->search (+1, $self->{row}); |
152 | $self->idle; |
235 | $self->idle; |
153 | } elsif ($string !~ /[\x00-\x1f\x80-\xaf]/) { |
236 | } elsif ($string !~ /[\x00-\x1f\x80-\xaf]/) { |
154 | return; # pass to tt_write |
237 | return; # pass to tt_write |
155 | } |
238 | } |
156 | |
239 | |
… | |
… | |
159 | |
242 | |
160 | sub tt_write { |
243 | sub tt_write { |
161 | my ($self, $data) = @_; |
244 | my ($self, $data) = @_; |
162 | |
245 | |
163 | $self->{search} .= $self->locale_decode ($data); |
246 | $self->{search} .= $self->locale_decode ($data); |
|
|
247 | $self->{search} =~ s/\e\[20[01]~//g; |
164 | |
248 | |
165 | $self->{search} =~ s/^\(\?i\)// |
249 | $self->{search} =~ s/^\(\?i\)// |
166 | if $self->{search} =~ /^\(.*[[:upper:]]/; |
250 | if $self->{search} =~ /^\(.*[[:upper:]]/; |
167 | |
251 | |
|
|
252 | delete $self->{found}; |
168 | $self->search (-1); |
253 | $self->search (-1, $self->{row}); |
169 | $self->idle; |
254 | $self->idle; |
170 | |
255 | |
171 | 1 |
256 | 1 |
172 | } |
257 | } |
173 | |
258 | |