ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/perl/searchable-scrollback
Revision: 1.12
Committed: Wed Jan 11 02:13:56 2006 UTC (18 years, 4 months ago) by root
Branch: MAIN
Changes since 1.11: +3 -1 lines
Log Message:
*** empty log message ***

File Contents

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