1 |
#! perl |
2 |
|
3 |
# this extension implements scrollback buffer search |
4 |
|
5 |
#:META:RESOURCE:%:string:activation hotkey keysym |
6 |
#:META:BINDING:M-s:start |
7 |
|
8 |
=head1 NAME |
9 |
|
10 |
searchable-scrollback - incremental scrollback search (enabled by default) |
11 |
|
12 |
=head1 DESCRIPTION |
13 |
|
14 |
Adds regex search functionality to the scrollback buffer, triggered by |
15 |
the C<searchable-scrollback:start> action (bound to C<M-s> by |
16 |
default). While in search mode, normal terminal input/output is |
17 |
suspended and a regex is displayed at the bottom of the screen. |
18 |
|
19 |
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> |
21 |
search upwards/downwards in the scrollback buffer, C<End> jumps to the |
22 |
bottom. C<Escape> leaves search mode and returns to the point where search |
23 |
was started, while C<Enter> or C<Return> stay at the current position and |
24 |
additionally stores the first match in the current line into the primary |
25 |
selection if the C<Shift> modifier is active. |
26 |
|
27 |
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> |
29 |
or simply use an uppercase character which removes the "(?i)" prefix. |
30 |
|
31 |
See L<perlre> for more info about perl regular expression syntax. |
32 |
|
33 |
=cut |
34 |
|
35 |
sub on_init { |
36 |
my ($self) = @_; |
37 |
|
38 |
# only for backwards compatibility |
39 |
my $hotkey = $self->{argv}[0] |
40 |
|| $self->x_resource ("%") |
41 |
|| return; |
42 |
|
43 |
$self->bind_action ($hotkey, "searchable-scrollback:start") # ugh |
44 |
or warn "unable to register '$hotkey' as scrollback search start hotkey\n"; |
45 |
|
46 |
() |
47 |
} |
48 |
|
49 |
sub on_user_command { |
50 |
my ($self, $cmd) = @_; |
51 |
|
52 |
$cmd eq "searchable-scrollback:start" |
53 |
and $self->enter; |
54 |
|
55 |
() |
56 |
} |
57 |
|
58 |
sub on_action { |
59 |
my ($self, $action) = @_; |
60 |
|
61 |
$action eq "start" |
62 |
and $self->enter; |
63 |
|
64 |
() |
65 |
} |
66 |
|
67 |
sub msg { |
68 |
my ($self, $msg) = @_; |
69 |
|
70 |
$self->{overlay} = $self->overlay (0, -1, $self->ncol, 1, urxvt::OVERLAY_RSTYLE, 0); |
71 |
$self->{overlay}->set (0, 0, $self->special_encode ($msg)); |
72 |
} |
73 |
|
74 |
sub enter { |
75 |
my ($self) = @_; |
76 |
|
77 |
return if $self->{overlay}; |
78 |
|
79 |
$self->{view_start} = $self->view_start; |
80 |
$self->{pty_ev_events} = $self->pty_ev_events (urxvt::EV_NONE); |
81 |
$self->{row} = $self->nrow - 1; |
82 |
$self->{search} = "(?i)"; |
83 |
|
84 |
$self->enable ( |
85 |
key_press => \&key_press, |
86 |
tt_write => \&tt_write, |
87 |
refresh_begin => \&refresh, |
88 |
refresh_end => \&refresh, |
89 |
); |
90 |
|
91 |
$self->{manpage_overlay} = $self->overlay (0, -2, $self->ncol, 1, urxvt::OVERLAY_RSTYLE, 0); |
92 |
$self->{manpage_overlay}->set (0, 0, "scrollback search, see the ${urxvt::RXVTNAME}perl manpage for details"); |
93 |
|
94 |
$self->idle; |
95 |
} |
96 |
|
97 |
sub leave { |
98 |
my ($self) = @_; |
99 |
|
100 |
$self->disable ("key_press", "tt_write", "refresh_begin", "refresh_end"); |
101 |
$self->pty_ev_events ($self->{pty_ev_events}); |
102 |
|
103 |
delete $self->{manpage_overlay}; |
104 |
delete $self->{overlay}; |
105 |
delete $self->{search}; |
106 |
delete $self->{found}; |
107 |
} |
108 |
|
109 |
sub idle { |
110 |
my ($self) = @_; |
111 |
|
112 |
$self->msg ("(escape cancels) /$self->{search}█"); |
113 |
} |
114 |
|
115 |
sub search { |
116 |
my ($self, $dir, $row) = @_; |
117 |
|
118 |
my $search = $self->special_encode ($self->{search}); |
119 |
|
120 |
no re 'eval'; # just to be sure |
121 |
if (my $re = eval { qr/$search/ }) { |
122 |
while ($self->nrow > $row && $row >= $self->top_row) { |
123 |
my $line = $self->line ($row) |
124 |
or last; |
125 |
|
126 |
my $text = $line->t; |
127 |
if ($text =~ /$re/g) { |
128 |
delete $self->{found}; |
129 |
|
130 |
do { |
131 |
push @{ $self->{found} }, [$line->coord_of ($-[0]), $line->coord_of ($+[0])]; |
132 |
} while $text =~ /$re/g; |
133 |
|
134 |
$self->{row} = $row; |
135 |
$self->view_start (List::Util::min 0, $row - ($self->nrow >> 1)); |
136 |
$self->want_refresh; |
137 |
return; |
138 |
} |
139 |
|
140 |
$row = $dir < 0 ? $line->beg - 1 : $line->end + 1; |
141 |
} |
142 |
} |
143 |
|
144 |
$self->scr_bell; |
145 |
} |
146 |
|
147 |
sub refresh { |
148 |
my ($self) = @_; |
149 |
|
150 |
return unless $self->{found}; |
151 |
|
152 |
my $xor = urxvt::RS_RVid | urxvt::RS_Blink; |
153 |
for (@{ $self->{found} }) { |
154 |
$self->scr_xor_span (@$_, $xor); |
155 |
$xor = urxvt::RS_RVid; |
156 |
} |
157 |
|
158 |
() |
159 |
} |
160 |
|
161 |
sub key_press { |
162 |
my ($self, $event, $keysym, $string) = @_; |
163 |
|
164 |
delete $self->{manpage_overlay}; |
165 |
|
166 |
if ($keysym == 0xff0d || $keysym == 0xff8d) { # enter |
167 |
if ($self->{found} && $event->{state} & urxvt::ShiftMask) { |
168 |
my ($br, $bc, $er, $ec) = @{ $self->{found}[0] }; |
169 |
$self->selection_beg ($br, $bc); |
170 |
$self->selection_end ($er, $ec); |
171 |
$self->selection_make ($event->{time}); |
172 |
} |
173 |
$self->leave; |
174 |
} elsif ($keysym == 0xff1b) { # escape |
175 |
$self->view_start ($self->{view_start}); |
176 |
$self->leave; |
177 |
} elsif ($keysym == 0xff57) { # end |
178 |
$self->{row} = $self->nrow - 1; |
179 |
$self->view_start (0); |
180 |
} elsif ($keysym == 0xff52) { # up |
181 |
my $line = $self->line ($self->{row}); |
182 |
$self->search (-1, $line->beg - 1) |
183 |
if $line->beg > $self->top_row; |
184 |
} elsif ($keysym == 0xff54) { # down |
185 |
my $line = $self->line ($self->{row}); |
186 |
$self->search (+1, $line->end + 1) |
187 |
if $line->end < $self->nrow; |
188 |
} elsif ($keysym == 0xff08) { # backspace |
189 |
substr $self->{search}, -1, 1, ""; |
190 |
$self->search (+1, $self->{row}); |
191 |
$self->idle; |
192 |
} elsif ($string !~ /[\x00-\x1f\x80-\xaf]/) { |
193 |
return; # pass to tt_write |
194 |
} |
195 |
|
196 |
1 |
197 |
} |
198 |
|
199 |
sub tt_write { |
200 |
my ($self, $data) = @_; |
201 |
|
202 |
$self->{search} .= $self->locale_decode ($data); |
203 |
|
204 |
$self->{search} =~ s/^\(\?i\)// |
205 |
if $self->{search} =~ /^\(.*[[:upper:]]/; |
206 |
|
207 |
delete $self->{found}; |
208 |
$self->search (-1, $self->{row}); |
209 |
$self->idle; |
210 |
|
211 |
1 |
212 |
} |
213 |
|
214 |
|