ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/perl/searchable-scrollback
(Generate patch)

Comparing rxvt-unicode/src/perl/searchable-scrollback (file contents):
Revision 1.22 by root, Thu Jul 6 18:56:10 2006 UTC vs.
Revision 1.44 by sf-exg, Tue Oct 24 20:12:08 2017 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines