1 | #! perl |
1 | #! perl |
2 | |
2 | |
3 | # this extension implements scrollback buffer search |
3 | # this extension implements scrollback buffer search |
4 | |
4 | |
5 | #:META:RESOURCE:%:string:activation hotkey keysym |
5 | #:META:RESOURCE:%:string:activation hotkey keysym |
6 | #:META:KEYSYM:M-s:searchable-scrollback:start |
|
|
7 | |
6 | |
8 | =head1 NAME |
7 | =head1 NAME |
9 | |
8 | |
10 | searchable-scrollback<hotkey> - incremental scrollback search (enabled by default) |
9 | searchable-scrollback - incremental scrollback search (enabled by default) |
11 | |
10 | |
12 | =head1 DESCRIPTION |
11 | =head1 DESCRIPTION |
13 | |
12 | |
14 | Adds regex search functionality to the scrollback buffer, triggered |
13 | Adds regex search functionality to the scrollback buffer, triggered by |
15 | by a hotkey (default: C<M-s>). While in search mode, normal terminal |
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 | input/output is suspended and a regex is displayed at the bottom of the |
16 | suspended and a regex is displayed at the bottom of the screen. |
17 | screen. |
|
|
18 | |
17 | |
19 | Inputting characters appends them to the regex and continues incremental |
18 | Inputting characters appends them to the regex and continues incremental |
20 | search. C<BackSpace> removes a character from the regex, C<Up> and C<Down> |
19 | search. In addition, the following bindings are recognized: |
21 | search upwards/downwards in the scrollback buffer, C<End> jumps to the |
20 | |
22 | bottom. C<Escape> leaves search mode and returns to the point where search |
21 | =over 4 |
23 | was started, while C<Enter> or C<Return> stay at the current position and |
22 | |
24 | additionally stores the first match in the current line into the primary |
23 | =item C<BackSpace> |
25 | selection if the C<Shift> modifier is active. |
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 |
26 | |
62 | |
27 | The regex defaults to "(?i)", resulting in a case-insensitive search. To |
63 | The regex defaults to "(?i)", resulting in a case-insensitive search. To |
28 | get a case-sensitive search you can delete this prefix using C<BackSpace> |
64 | get a case-sensitive search you can delete this prefix using C<BackSpace> |
29 | or simply use an uppercase character which removes the "(?i)" prefix. |
65 | or simply use an uppercase character which removes the "(?i)" prefix. |
30 | |
66 | |
… | |
… | |
33 | =cut |
69 | =cut |
34 | |
70 | |
35 | sub on_init { |
71 | sub on_init { |
36 | my ($self) = @_; |
72 | my ($self) = @_; |
37 | |
73 | |
|
|
74 | # only for backwards compatibility |
38 | my $hotkey = $self->{argv}[0] |
75 | my $hotkey = $self->{argv}[0] |
39 | || $self->x_resource ("%") |
76 | || $self->x_resource ("%") |
40 | || "M-s"; |
77 | || "M-s"; |
41 | |
78 | |
42 | $self->parse_keysym ($hotkey, "perl:searchable-scrollback:start") |
79 | $self->bind_action ($hotkey, "%:start") |
43 | 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"; |
44 | |
81 | |
45 | () |
82 | () |
46 | } |
83 | } |
47 | |
84 | |
48 | sub on_user_command { |
85 | sub on_user_command { |
49 | my ($self, $cmd) = @_; |
86 | my ($self, $cmd) = @_; |
50 | |
87 | |
51 | $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" |
52 | and $self->enter; |
98 | and $self->enter; |
53 | |
99 | |
54 | () |
100 | () |
55 | } |
101 | } |
56 | |
102 | |
… | |
… | |
90 | $self->disable ("key_press", "tt_write", "refresh_begin", "refresh_end"); |
136 | $self->disable ("key_press", "tt_write", "refresh_begin", "refresh_end"); |
91 | $self->pty_ev_events ($self->{pty_ev_events}); |
137 | $self->pty_ev_events ($self->{pty_ev_events}); |
92 | |
138 | |
93 | delete $self->{manpage_overlay}; |
139 | delete $self->{manpage_overlay}; |
94 | delete $self->{overlay}; |
140 | delete $self->{overlay}; |
95 | delete $self->{history}; |
|
|
96 | delete $self->{search}; |
141 | delete $self->{search}; |
|
|
142 | delete $self->{found}; |
97 | } |
143 | } |
98 | |
144 | |
99 | sub idle { |
145 | sub idle { |
100 | my ($self) = @_; |
146 | my ($self) = @_; |
101 | |
147 | |
102 | $self->msg ("(escape cancels) /$self->{search}█"); |
148 | $self->msg ("(escape cancels) /$self->{search}█"); |
103 | } |
149 | } |
104 | |
150 | |
105 | sub search { |
151 | sub search { |
106 | my ($self, $dir) = @_; |
152 | my ($self, $dir, $row) = @_; |
107 | |
|
|
108 | delete $self->{found}; |
|
|
109 | my $row = $self->{row}; |
|
|
110 | |
153 | |
111 | my $search = $self->special_encode ($self->{search}); |
154 | my $search = $self->special_encode ($self->{search}); |
112 | |
155 | |
113 | no re 'eval'; # just to be sure |
156 | no re 'eval'; # just to be sure |
114 | if (my $re = eval { qr/$search/ }) { |
157 | if (my $re = eval { qr/$search/ }) { |
… | |
… | |
116 | my $line = $self->line ($row) |
159 | my $line = $self->line ($row) |
117 | or last; |
160 | or last; |
118 | |
161 | |
119 | my $text = $line->t; |
162 | my $text = $line->t; |
120 | if ($text =~ /$re/g) { |
163 | if ($text =~ /$re/g) { |
|
|
164 | delete $self->{found}; |
|
|
165 | |
121 | do { |
166 | do { |
122 | push @{ $self->{found} }, [$line->coord_of ($-[0]), $line->coord_of ($+[0])]; |
167 | push @{ $self->{found} }, [$line->coord_of ($-[0]), $line->coord_of ($+[0])]; |
123 | } while $text =~ /$re/g; |
168 | } while $text =~ /$re/g; |
124 | |
169 | |
125 | $self->{row} = $row; |
170 | $self->{row} = $row; |
126 | $self->view_start (List::Util::min 0, $row - ($self->nrow >> 1)); |
171 | $self->view_start (List::Util::min 0, $row - ($self->nrow >> 1)); |
127 | $self->want_refresh; |
172 | $self->want_refresh; |
128 | last; |
173 | return; |
129 | } |
174 | } |
130 | |
175 | |
131 | $row = $dir < 0 ? $line->beg - 1 : $line->end + 1; |
176 | $row = $dir < 0 ? $line->beg - 1 : $line->end + 1; |
132 | } |
177 | } |
133 | } |
178 | } |
134 | |
179 | |
135 | $self->scr_bell unless $self->{found}; |
180 | $self->scr_bell; |
136 | } |
181 | } |
137 | |
182 | |
138 | sub refresh { |
183 | sub refresh { |
139 | my ($self) = @_; |
184 | my ($self) = @_; |
140 | |
185 | |
… | |
… | |
167 | $self->leave; |
212 | $self->leave; |
168 | } elsif ($keysym == 0xff57) { # end |
213 | } elsif ($keysym == 0xff57) { # end |
169 | $self->{row} = $self->nrow - 1; |
214 | $self->{row} = $self->nrow - 1; |
170 | $self->view_start (0); |
215 | $self->view_start (0); |
171 | } elsif ($keysym == 0xff52) { # up |
216 | } elsif ($keysym == 0xff52) { # up |
172 | $self->{row}-- if $self->{row} > $self->top_row; |
217 | my $line = $self->line ($self->{row}); |
173 | $self->search (-1); |
218 | $self->search (-1, $line->beg - 1) |
|
|
219 | if $line->beg > $self->top_row; |
174 | } elsif ($keysym == 0xff54) { # down |
220 | } elsif ($keysym == 0xff54) { # down |
175 | $self->{row}++ if $self->{row} < $self->nrow; |
221 | my $line = $self->line ($self->{row}); |
176 | $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); |
177 | } elsif ($keysym == 0xff08) { # backspace |
232 | } elsif ($keysym == 0xff08) { # backspace |
178 | substr $self->{search}, -1, 1, ""; |
233 | substr $self->{search}, -1, 1, ""; |
179 | $self->search; |
234 | $self->search (+1, $self->{row}); |
180 | $self->idle; |
235 | $self->idle; |
181 | } elsif ($string !~ /[\x00-\x1f\x80-\xaf]/) { |
236 | } elsif ($string !~ /[\x00-\x1f\x80-\xaf]/) { |
182 | return; # pass to tt_write |
237 | return; # pass to tt_write |
183 | } |
238 | } |
184 | |
239 | |
… | |
… | |
187 | |
242 | |
188 | sub tt_write { |
243 | sub tt_write { |
189 | my ($self, $data) = @_; |
244 | my ($self, $data) = @_; |
190 | |
245 | |
191 | $self->{search} .= $self->locale_decode ($data); |
246 | $self->{search} .= $self->locale_decode ($data); |
|
|
247 | $self->{search} =~ s/\e\[20[01]~//g; |
192 | |
248 | |
193 | $self->{search} =~ s/^\(\?i\)// |
249 | $self->{search} =~ s/^\(\?i\)// |
194 | if $self->{search} =~ /^\(.*[[:upper:]]/; |
250 | if $self->{search} =~ /^\(.*[[:upper:]]/; |
195 | |
251 | |
|
|
252 | delete $self->{found}; |
196 | $self->search (-1); |
253 | $self->search (-1, $self->{row}); |
197 | $self->idle; |
254 | $self->idle; |
198 | |
255 | |
199 | 1 |
256 | 1 |
200 | } |
257 | } |
201 | |
258 | |