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:BINDING:M-s:start |
|
|
7 | |
6 | |
8 | =head1 NAME |
7 | =head1 NAME |
9 | |
8 | |
10 | searchable-scrollback - incremental scrollback search (enabled by default) |
9 | searchable-scrollback - incremental scrollback search (enabled by default) |
11 | |
10 | |
… | |
… | |
36 | my ($self) = @_; |
35 | my ($self) = @_; |
37 | |
36 | |
38 | # only for backwards compatibility |
37 | # only for backwards compatibility |
39 | my $hotkey = $self->{argv}[0] |
38 | my $hotkey = $self->{argv}[0] |
40 | || $self->x_resource ("%") |
39 | || $self->x_resource ("%") |
41 | || return; |
40 | || "M-s"; |
42 | |
41 | |
43 | $self->bind_action ($hotkey, "searchable-scrollback:start") # ugh |
42 | $self->bind_action ($hotkey, "%:start") |
44 | or warn "unable to register '$hotkey' as scrollback search start hotkey\n"; |
43 | or warn "unable to register '$hotkey' as scrollback search start hotkey\n"; |
|
|
44 | |
|
|
45 | () |
|
|
46 | } |
|
|
47 | |
|
|
48 | sub on_user_command { |
|
|
49 | my ($self, $cmd) = @_; |
|
|
50 | |
|
|
51 | $cmd eq "searchable-scrollback:start" |
|
|
52 | and $self->enter; |
45 | |
53 | |
46 | () |
54 | () |
47 | } |
55 | } |
48 | |
56 | |
49 | sub on_action { |
57 | sub on_action { |
… | |
… | |
92 | $self->pty_ev_events ($self->{pty_ev_events}); |
100 | $self->pty_ev_events ($self->{pty_ev_events}); |
93 | |
101 | |
94 | delete $self->{manpage_overlay}; |
102 | delete $self->{manpage_overlay}; |
95 | delete $self->{overlay}; |
103 | delete $self->{overlay}; |
96 | delete $self->{search}; |
104 | delete $self->{search}; |
|
|
105 | delete $self->{found}; |
97 | } |
106 | } |
98 | |
107 | |
99 | sub idle { |
108 | sub idle { |
100 | my ($self) = @_; |
109 | my ($self) = @_; |
101 | |
110 | |
102 | $self->msg ("(escape cancels) /$self->{search}█"); |
111 | $self->msg ("(escape cancels) /$self->{search}█"); |
103 | } |
112 | } |
104 | |
113 | |
105 | sub search { |
114 | sub search { |
106 | my ($self, $dir, $row) = @_; |
115 | my ($self, $dir, $row) = @_; |
107 | |
|
|
108 | delete $self->{found}; |
|
|
109 | |
116 | |
110 | my $search = $self->special_encode ($self->{search}); |
117 | my $search = $self->special_encode ($self->{search}); |
111 | |
118 | |
112 | no re 'eval'; # just to be sure |
119 | no re 'eval'; # just to be sure |
113 | if (my $re = eval { qr/$search/ }) { |
120 | if (my $re = eval { qr/$search/ }) { |
… | |
… | |
115 | my $line = $self->line ($row) |
122 | my $line = $self->line ($row) |
116 | or last; |
123 | or last; |
117 | |
124 | |
118 | my $text = $line->t; |
125 | my $text = $line->t; |
119 | if ($text =~ /$re/g) { |
126 | if ($text =~ /$re/g) { |
|
|
127 | delete $self->{found}; |
|
|
128 | |
120 | do { |
129 | do { |
121 | push @{ $self->{found} }, [$line->coord_of ($-[0]), $line->coord_of ($+[0])]; |
130 | push @{ $self->{found} }, [$line->coord_of ($-[0]), $line->coord_of ($+[0])]; |
122 | } while $text =~ /$re/g; |
131 | } while $text =~ /$re/g; |
123 | |
132 | |
124 | $self->{row} = $row; |
133 | $self->{row} = $row; |
125 | $self->view_start (List::Util::min 0, $row - ($self->nrow >> 1)); |
134 | $self->view_start (List::Util::min 0, $row - ($self->nrow >> 1)); |
126 | $self->want_refresh; |
135 | $self->want_refresh; |
127 | last; |
136 | return; |
128 | } |
137 | } |
129 | |
138 | |
130 | $row = $dir < 0 ? $line->beg - 1 : $line->end + 1; |
139 | $row = $dir < 0 ? $line->beg - 1 : $line->end + 1; |
131 | } |
140 | } |
132 | } |
141 | } |
133 | |
142 | |
134 | $self->scr_bell unless $self->{found}; |
143 | $self->scr_bell; |
135 | } |
144 | } |
136 | |
145 | |
137 | sub refresh { |
146 | sub refresh { |
138 | my ($self) = @_; |
147 | my ($self) = @_; |
139 | |
148 | |
… | |
… | |
192 | $self->{search} .= $self->locale_decode ($data); |
201 | $self->{search} .= $self->locale_decode ($data); |
193 | |
202 | |
194 | $self->{search} =~ s/^\(\?i\)// |
203 | $self->{search} =~ s/^\(\?i\)// |
195 | if $self->{search} =~ /^\(.*[[:upper:]]/; |
204 | if $self->{search} =~ /^\(.*[[:upper:]]/; |
196 | |
205 | |
|
|
206 | delete $self->{found}; |
197 | $self->search (-1, $self->{row}); |
207 | $self->search (-1, $self->{row}); |
198 | $self->idle; |
208 | $self->idle; |
199 | |
209 | |
200 | 1 |
210 | 1 |
201 | } |
211 | } |