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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines