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.5 by root, Mon Jan 9 23:18:49 2006 UTC vs.
Revision 1.17 by root, Thu Jan 12 10:02:20 2006 UTC

3# this extension implements scrollback buffer search 3# this extension implements scrollback buffer search
4 4
5sub on_init { 5sub on_init {
6 my ($self) = @_; 6 my ($self) = @_;
7 7
8 my $hotkey = $self->{argv}[0] || "M-s"; 8 my $hotkey = $self->{argv}[0]
9 || $self->x_resource ("searchable-scrollback")
10 || "M-s";
9 11
10 $self->parse_keysym ($hotkey, "perl:searchable-scrollback:start") 12 $self->parse_keysym ($hotkey, "perl:searchable-scrollback:start")
11 or warn "unable to register '$hotkey' as scrollback search start hotkey\n"; 13 or warn "unable to register '$hotkey' as scrollback search start hotkey\n";
12 14
13 () 15 ()
14} 16}
15 17
16sub on_keyboard_command { 18sub on_keyboard_command {
17 my ($self, $cmd) = @_; 19 my ($self, $cmd) = @_;
18 20
19 if ($cmd eq "searchable-scrollback:start") { 21 $cmd eq "searchable-scrollback:start"
20 $self->enter; 22 and $self->enter;
21 }
22 23
23 () 24 ()
24} 25}
25 26
26sub msg { 27sub msg {
33sub enter { 34sub enter {
34 my ($self) = @_; 35 my ($self) = @_;
35 36
36 return if $self->{overlay}; 37 return if $self->{overlay};
37 38
39 $self->{view_start} = $self->view_start;
38 $self->{pty_ev_events} = $self->pty_ev_events (urxvt::EVENT_NONE); 40 $self->{pty_ev_events} = $self->pty_ev_events (urxvt::EVENT_NONE);
39 $self->{view_start} = $self->view_start; 41 $self->{row} = $self->nrow - 1;
40 42
41 $self->enable ( 43 $self->enable (
42 key_press => \&key_press, 44 key_press => \&key_press,
45 tt_write => \&tt_write,
43 refresh_begin => \&refresh, 46 refresh_begin => \&refresh,
44 refresh_end => \&refresh, 47 refresh_end => \&refresh,
45 ); 48 );
49
50 $self->{manpage_overlay} = $self->overlay (0, -2, $self->ncol, 1, urxvt::OVERLAY_RSTYLE, 0);
51 $self->{manpage_overlay}->set (0, 0, "scrollback search, see the ${urxvt::RXVTNAME}perl manpage for details");
46 52
47 $self->idle; 53 $self->idle;
48} 54}
49 55
50sub leave { 56sub leave {
51 my ($self) = @_; 57 my ($self) = @_;
52 58
59 $self->disable ("key_press", "tt_write", "refresh_begin", "refresh_end");
60 $self->pty_ev_events ($self->{pty_ev_events});
61
62 delete $self->{manpage_overlay};
53 delete $self->{overlay}; 63 delete $self->{overlay};
54 64 delete $self->{history};
55 $self->disable ("key_press", "refresh_begin", "refresh_end"); 65 delete $self->{search};
56
57 $self->pty_ev_events ($self->{pty_ev_events});
58 $self->want_refresh;
59} 66}
60 67
61sub idle { 68sub idle {
62 my ($self) = @_; 69 my ($self) = @_;
63 70
64 $self->msg ("scrollback search, escape=exit, enter=accept, /=start search, n=next, p=previous, G=bottom"); 71 $self->msg ("(escape cancels) /$self->{search}█");
65
66 delete $self->{in_search};
67} 72}
68 73
69sub search { 74sub search {
70 my ($self, $offset) = @_; 75 my ($self, $dir) = @_;
71
72 my $row = -$self->view_start + ($self->nrow >> 1) + $offset;
73 76
74 delete $self->{found}; 77 delete $self->{found};
78 my $row = $self->{row};
79
80 my $search = $self->special_encode ($self->{search});
75 81
76 no re 'eval'; # just to be sure 82 no re 'eval'; # just to be sure
77 my $re = qr/$self->{search}/; 83 if (my $re = eval { qr/$search/ }) {
84 while ($self->nrow > $row && $row > -$self->nsaved) {
85 my $line = $self->line ($row)
86 or last;
78 87
79 while ($row > -$self->nsaved) { 88 my $text = $line->t;
80 my $line = $self->line ($row) 89 if ($text =~ /$re/g) {
81 or last; 90 do {
91 push @{ $self->{found} }, [$line->coord_of ($-[0]), $line->coord_of ($+[0])];
92 } while $text =~ /$re/g;
82 93
83 my $text = $line->t; 94 $self->{row} = $row;
84 if ($text =~ /$re/g) { 95 $self->view_start (List::Util::max 0, -$row + ($self->nrow >> 1));
96 $self->want_refresh;
97 last;
85 do { 98 }
86 push @{ $self->{found} }, [$line->coord_of ($-[0]), $line->coord_of ($+[0])];
87 } while $text =~ /$re/g;
88 99
89 $self->view_start (-$row + ($self->nrow >> 1)); 100 $row = $dir < 0 ? $line->beg - 1 : $line->end + 1;
90 last;
91 } 101 }
92
93 $row = $line->beg - 1;
94 } 102 }
95 103
96 $self->msg ("enter/type/backspace: /$self->{search}_"
97 . ($self->{found} ? "" : " (not found)"));
98 $self->scr_bell unless $self->{found}; 104 $self->scr_bell unless $self->{found};
99} 105}
100 106
101sub refresh { 107sub refresh {
102 my ($self) = @_; 108 my ($self) = @_;
103 109
104 return unless $self->{found}; 110 my $xor = urxvt::RS_RVid | urxvt::RS_Blink;
105
106 $self->scr_xor_span (@$_, urxvt::SET_FGCOLOR (urxvt::RS_RVid, 15))
107 for @{ $self->{found} }; 111 for (@{ $self->{found} }) {
112 $self->scr_xor_span (@$_, $xor);
113 $xor = urxvt::RS_RVid;
114 }
108 115
109 () 116 ()
110} 117}
111 118
112sub key_press { 119sub key_press {
113 my ($self, $event, $keysym, $string) = @_; 120 my ($self, $event, $keysym, $string) = @_;
114 121
115 if (exists $self->{in_search}) { 122 delete $self->{manpage_overlay};
123
116 if ($keysym == 0xff0d || $keysym == 0xff8d) { 124 if ($keysym == 0xff0d || $keysym == 0xff8d) { # enter
117 $self->idle; 125 if ($self->{found}) {
118 } elsif ($keysym == 0xff1b) { 126 my ($br, $bc, $er, $ec) = @{ $self->{found}[0] };
119 $self->view_start ($self->{in_search}); 127 $self->selection_beg ($br, $bc);
120 $self->idle; 128 $self->selection_end ($er, $ec);
121 } elsif ($keysym == 0xff08) { 129 $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 } 130 }
128 } else {
129 if ($keysym == 0xff0d || $keysym == 0xff8d) {
130 # OK
131 $self->leave; 131 $self->leave;
132 } elsif ($keysym == 0xff1b) { 132 } elsif ($keysym == 0xff1b) { # escape
133 $self->view_start ($self->{view_start}); 133 $self->view_start ($self->{view_start});
134 $self->leave; 134 $self->leave;
135 } elsif ($keysym == 0xff52) { 135 } elsif ($keysym == 0xff57) { # end
136 $self->view_start ($self->view_start + 1); 136 $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); 137 $self->view_start (0);
144 } elsif ($string eq "n") { 138 } elsif ($keysym == 0xff52) { # up
139 $self->{row}-- if $self->{row} > -$self->nsaved;
145 $self->search (-1); 140 $self->search (-1);
141 } elsif ($keysym == 0xff54) { # down
142 $self->{row}++ if $self->{row} < $self->nrow;
143 $self->search (+1);
144 } elsif ($keysym == 0xff08) { # backspace
145 substr $self->{search}, -1, 1, "";
146 $self->search;
146 $self->idle; 147 $self->idle;
147 } elsif ($string eq "p") { 148 } elsif ($string =~ /^[^\x00-\x1f\x80-\xaf]+$/) {
148 # TODO 149 return; # pass to tt_write
149 } elsif ($string ne "") {
150 $self->scr_bell;
151 }
152 } 150 }
153 151
154 1 152 1
155} 153}
156 154
155sub tt_write {
156 my ($self, $data) = @_;
157 157
158 $self->{search} .= $self->locale_decode ($data);
159 $self->search (-1);
160 $self->idle;
161
162 1
163}
164
165

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines