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 |
|
|
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 |
|
|
70 | |
5 | sub on_init { |
71 | sub on_init { |
6 | my ($self) = @_; |
72 | my ($self) = @_; |
7 | |
73 | |
|
|
74 | # only for backwards compatibility |
8 | my $hotkey = $self->{argv}[0] || "M-s"; |
75 | my $hotkey = $self->{argv}[0] |
|
|
76 | || $self->x_resource ("%") |
|
|
77 | || "M-s"; |
9 | |
78 | |
10 | $self->parse_keysym ($hotkey, "perl:searchable-scrollback:start") |
79 | $self->bind_action ($hotkey, "%:start") |
11 | 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"; |
12 | |
81 | |
13 | () |
82 | () |
14 | } |
83 | } |
15 | |
84 | |
16 | sub on_keyboard_command { |
85 | sub on_user_command { |
17 | my ($self, $cmd) = @_; |
86 | my ($self, $cmd) = @_; |
18 | |
87 | |
19 | if ($cmd eq "searchable-scrollback:start") { |
88 | $cmd eq "searchable-scrollback:start" |
20 | $self->enter; |
89 | and $self->enter; |
21 | } |
90 | |
|
|
91 | () |
|
|
92 | } |
|
|
93 | |
|
|
94 | sub on_action { |
|
|
95 | my ($self, $action) = @_; |
|
|
96 | |
|
|
97 | $action eq "start" |
|
|
98 | and $self->enter; |
22 | |
99 | |
23 | () |
100 | () |
24 | } |
101 | } |
25 | |
102 | |
26 | sub msg { |
103 | sub msg { |
… | |
… | |
33 | sub enter { |
110 | sub enter { |
34 | my ($self) = @_; |
111 | my ($self) = @_; |
35 | |
112 | |
36 | return if $self->{overlay}; |
113 | return if $self->{overlay}; |
37 | |
114 | |
38 | $self->{pty_ev_events} = $self->pty_ev_events (urxvt::EVENT_NONE); |
|
|
39 | $self->{view_start} = $self->view_start; |
115 | $self->{view_start} = $self->view_start; |
|
|
116 | $self->{pty_ev_events} = $self->pty_ev_events (urxvt::EV_NONE); |
|
|
117 | $self->{row} = $self->nrow - 1; |
|
|
118 | $self->{search} = "(?i)"; |
40 | |
119 | |
41 | $self->enable ( |
120 | $self->enable ( |
42 | key_press => \&key_press, |
121 | key_press => \&key_press, |
|
|
122 | tt_write => \&tt_write, |
43 | refresh_begin => \&refresh, |
123 | refresh_begin => \&refresh, |
44 | refresh_end => \&refresh, |
124 | refresh_end => \&refresh, |
45 | ); |
125 | ); |
46 | |
126 | |
|
|
127 | $self->{manpage_overlay} = $self->overlay (0, -2, $self->ncol, 1, urxvt::OVERLAY_RSTYLE, 0); |
|
|
128 | $self->{manpage_overlay}->set (0, 0, "scrollback search, see the ${urxvt::RXVTNAME}perl manpage for details"); |
|
|
129 | |
47 | $self->idle; |
130 | $self->idle; |
48 | } |
131 | } |
49 | |
132 | |
50 | sub leave { |
133 | sub leave { |
51 | my ($self) = @_; |
134 | my ($self) = @_; |
52 | |
135 | |
|
|
136 | $self->disable ("key_press", "tt_write", "refresh_begin", "refresh_end"); |
|
|
137 | $self->pty_ev_events ($self->{pty_ev_events}); |
|
|
138 | |
|
|
139 | delete $self->{manpage_overlay}; |
53 | delete $self->{overlay}; |
140 | delete $self->{overlay}; |
54 | |
141 | delete $self->{search}; |
55 | $self->disable ("key_press", "refresh_begin", "refresh_end"); |
142 | delete $self->{found}; |
56 | |
|
|
57 | $self->pty_ev_events ($self->{pty_ev_events}); |
|
|
58 | $self->want_refresh; |
|
|
59 | } |
143 | } |
60 | |
144 | |
61 | sub idle { |
145 | sub idle { |
62 | my ($self) = @_; |
146 | my ($self) = @_; |
63 | |
147 | |
64 | $self->msg ("scrollback search, escape=exit, enter=accept, /=start search, n=next, p=previous, G=bottom"); |
148 | $self->msg ("(escape cancels) /$self->{search}█"); |
65 | |
|
|
66 | delete $self->{in_search}; |
|
|
67 | } |
149 | } |
68 | |
150 | |
69 | sub search { |
151 | sub search { |
70 | my ($self, $offset) = @_; |
152 | my ($self, $dir, $row) = @_; |
71 | |
153 | |
72 | my $row = -$self->view_start + ($self->nrow >> 1) + $offset; |
154 | my $search = $self->special_encode ($self->{search}); |
73 | |
|
|
74 | delete $self->{found}; |
|
|
75 | |
155 | |
76 | no re 'eval'; # just to be sure |
156 | no re 'eval'; # just to be sure |
77 | my $re = qr/$self->{search}/; |
157 | if (my $re = eval { qr/$search/ }) { |
78 | |
158 | while ($self->nrow > $row && $row >= $self->top_row) { |
79 | while ($row > -$self->nsaved) { |
|
|
80 | my $line = $self->line ($row) |
159 | my $line = $self->line ($row) |
81 | or last; |
160 | or last; |
82 | |
161 | |
83 | my $text = $line->t; |
162 | my $text = $line->t; |
84 | if ($text =~ /$re/g) { |
163 | if ($text =~ /$re/g) { |
|
|
164 | delete $self->{found}; |
|
|
165 | |
85 | do { |
166 | do { |
86 | push @{ $self->{found} }, [$line->coord_of ($-[0]), $line->coord_of ($+[0])]; |
167 | push @{ $self->{found} }, [$line->coord_of ($-[0]), $line->coord_of ($+[0])]; |
87 | } while $text =~ /$re/g; |
168 | } while $text =~ /$re/g; |
88 | |
169 | |
|
|
170 | $self->{row} = $row; |
89 | $self->view_start (-$row + ($self->nrow >> 1)); |
171 | $self->view_start (List::Util::min 0, $row - ($self->nrow >> 1)); |
|
|
172 | $self->want_refresh; |
|
|
173 | return; |
90 | last; |
174 | } |
|
|
175 | |
|
|
176 | $row = $dir < 0 ? $line->beg - 1 : $line->end + 1; |
91 | } |
177 | } |
92 | |
|
|
93 | $row = $line->beg - 1; |
|
|
94 | } |
178 | } |
95 | |
179 | |
96 | $self->msg ("enter/type/backspace: /$self->{search}_" |
180 | $self->scr_bell; |
97 | . ($self->{found} ? "" : " (not found)")); |
|
|
98 | $self->scr_bell unless $self->{found}; |
|
|
99 | } |
181 | } |
100 | |
182 | |
101 | sub refresh { |
183 | sub refresh { |
102 | my ($self) = @_; |
184 | my ($self) = @_; |
103 | |
185 | |
104 | return unless $self->{found}; |
186 | return unless $self->{found}; |
105 | |
187 | |
106 | $self->scr_xor_span (@$_, urxvt::SET_FGCOLOR (urxvt::RS_RVid, 15)) |
188 | my $xor = urxvt::RS_RVid | urxvt::RS_Blink; |
107 | for @{ $self->{found} }; |
189 | for (@{ $self->{found} }) { |
|
|
190 | $self->scr_xor_span (@$_, $xor); |
|
|
191 | $xor = urxvt::RS_RVid; |
|
|
192 | } |
108 | |
193 | |
109 | () |
194 | () |
110 | } |
195 | } |
111 | |
196 | |
112 | sub key_press { |
197 | sub key_press { |
113 | my ($self, $event, $keysym, $string) = @_; |
198 | my ($self, $event, $keysym, $string) = @_; |
114 | |
199 | |
115 | if (exists $self->{in_search}) { |
200 | delete $self->{manpage_overlay}; |
|
|
201 | |
116 | if ($keysym == 0xff0d || $keysym == 0xff8d) { |
202 | if ($keysym == 0xff0d || $keysym == 0xff8d) { # enter |
117 | $self->idle; |
203 | if ($self->{found} && $event->{state} & urxvt::ShiftMask) { |
118 | } elsif ($keysym == 0xff1b) { |
204 | my ($br, $bc, $er, $ec) = @{ $self->{found}[0] }; |
119 | $self->view_start ($self->{in_search}); |
205 | $self->selection_beg ($br, $bc); |
120 | $self->idle; |
206 | $self->selection_end ($er, $ec); |
121 | } elsif ($keysym == 0xff08) { |
207 | $self->selection_make ($event->{time}); |
122 | substr $self->{search}, -1, 1, ""; |
|
|
123 | $self->search; |
|
|
124 | } elsif ($string ne "") { |
|
|
125 | $self->{search} .= $string; |
|
|
126 | $self->search; |
|
|
127 | } |
208 | } |
128 | } else { |
|
|
129 | if ($keysym == 0xff0d || $keysym == 0xff8d) { |
|
|
130 | # OK |
|
|
131 | $self->leave; |
209 | $self->leave; |
132 | } elsif ($keysym == 0xff1b) { |
210 | } elsif ($keysym == 0xff1b) { # escape |
133 | $self->view_start ($self->{view_start}); |
211 | $self->view_start ($self->{view_start}); |
134 | $self->leave; |
212 | $self->leave; |
135 | } elsif ($keysym == 0xff52) { |
213 | } elsif ($keysym == 0xff57) { # end |
136 | $self->view_start ($self->view_start + 1); |
214 | $self->{row} = $self->nrow - 1; |
137 | } elsif ($keysym == 0xff54) { |
|
|
138 | $self->view_start ($self->view_start - 1); |
|
|
139 | } elsif ($string eq "/") { |
|
|
140 | $self->{in_search} = $self->view_start; |
|
|
141 | $self->search; |
|
|
142 | } elsif ($string eq "G") { |
|
|
143 | $self->view_start (0); |
215 | $self->view_start (0); |
144 | } elsif ($string eq "n") { |
216 | } elsif ($keysym == 0xff52) { # up |
145 | $self->search (-1); |
217 | my $line = $self->line ($self->{row}); |
|
|
218 | $self->search (-1, $line->beg - 1) |
|
|
219 | if $line->beg > $self->top_row; |
|
|
220 | } elsif ($keysym == 0xff54) { # down |
|
|
221 | my $line = $self->line ($self->{row}); |
|
|
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); |
|
|
232 | } elsif ($keysym == 0xff08) { # backspace |
|
|
233 | substr $self->{search}, -1, 1, ""; |
|
|
234 | $self->search (+1, $self->{row}); |
146 | $self->idle; |
235 | $self->idle; |
147 | } elsif ($string eq "p" or $string eq "N") { |
236 | } elsif ($string !~ /[\x00-\x1f\x80-\xaf]/) { |
148 | # TODO |
237 | return; # pass to tt_write |
149 | } elsif ($string ne "") { |
|
|
150 | $self->scr_bell; |
|
|
151 | } |
|
|
152 | } |
238 | } |
153 | |
239 | |
154 | 1 |
240 | 1 |
155 | } |
241 | } |
156 | |
242 | |
|
|
243 | sub tt_write { |
|
|
244 | my ($self, $data) = @_; |
157 | |
245 | |
|
|
246 | $self->{search} .= $self->locale_decode ($data); |
|
|
247 | |
|
|
248 | $self->{search} =~ s/^\(\?i\)// |
|
|
249 | if $self->{search} =~ /^\(.*[[:upper:]]/; |
|
|
250 | |
|
|
251 | delete $self->{found}; |
|
|
252 | $self->search (-1, $self->{row}); |
|
|
253 | $self->idle; |
|
|
254 | |
|
|
255 | 1 |
|
|
256 | } |
|
|
257 | |
|
|
258 | |