~ubuntu-branches/ubuntu/maverick/padre/maverick

« back to all changes in this revision

Viewing changes to lib/Padre/Search.pm

  • Committer: Bazaar Package Importer
  • Author(s): Damyan Ivanov
  • Date: 2009-10-29 17:40:10 UTC
  • mto: (10.1.1 sid) (1.2.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 8.
  • Revision ID: james.westby@ubuntu.com-20091029174010-v9ryrnscjm4gg0x2
Tags: upstream-0.48.ds2
ImportĀ upstreamĀ versionĀ 0.48.ds2

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
      find_term => 'foo',
14
14
  );  
15
15
  
16
 
  # Execute the search on an editor object
17
 
  $search->find_next(Padre::Current->editor);
 
16
  # Execute the search on the current editor
 
17
  $search->search_next(Padre::Current->editor);
18
18
 
19
19
=head2 DESCRIPTION
20
20
 
25
25
 
26
26
=cut
27
27
 
 
28
use 5.008;
28
29
use strict;
29
30
use warnings;
30
31
use Encode     ();
31
32
use List::Util ();
32
33
use Params::Util '_INSTANCE';
33
34
 
34
 
our $VERSION = '0.42';
 
35
our $VERSION = '0.48';
35
36
 
36
37
use Class::XSAccessor getters => {
37
38
        find_term    => 'find_term',
48
49
        unless ( defined $self->find_term ) {
49
50
                die("Did not provide 'find_term' search term");
50
51
        }
 
52
        unless ( length $self->find_term ) {
 
53
 
 
54
                # Pointless zero-length search
 
55
                return;
 
56
        }
51
57
        unless ( defined $self->find_case ) {
52
58
                $self->{find_case} = $self->config->find_case;
53
59
        }
72
78
 
73
79
        # Compile the regex
74
80
        $self->{search_regex} = eval { $self->find_case ? qr/$term/m : qr/$term/mi };
75
 
        if ($@) {
76
 
 
77
 
                # The regex doesn't compile
78
 
                return;
79
 
        }
 
81
        return undef if $@;
80
82
 
81
83
        return $self;
82
84
}
89
91
        return $self->{config};
90
92
}
91
93
 
 
94
 
 
95
 
 
96
 
 
97
 
92
98
#####################################################################
93
99
# Command Abstraction
94
100
 
95
101
sub search_next {
96
102
        my $self = shift;
97
 
        if ( $self->config->find_reverse ) {
 
103
        if ( $self->find_reverse ) {
 
104
                return $self->search_up(@_);
 
105
        } else {
98
106
                return $self->search_down(@_);
99
 
        } else {
100
 
                return $self->search_up(@_);
101
107
        }
102
108
}
103
109
 
104
110
sub search_previous {
105
111
        my $self = shift;
106
 
        if ( $self->config->find_reverse ) {
 
112
        if ( $self->find_reverse ) {
 
113
                return $self->search_down(@_);
 
114
        } else {
107
115
                return $self->search_up(@_);
108
 
        } else {
109
 
                return $self->search_down(@_);
110
116
        }
111
117
}
112
118
 
117
123
        $self->replace(@_);
118
124
 
119
125
        # Select and move to the next match
120
 
        if ( $self->config->find_reverse ) {
 
126
        if ( $self->find_reverse ) {
121
127
                return $self->search_down(@_);
122
128
        } else {
123
129
                return $self->search_up(@_);
131
137
        $self->replace(@_);
132
138
 
133
139
        # Select and move to the next match
134
 
        if ( $self->config->find_reverse ) {
 
140
        if ( $self->find_reverse ) {
135
141
                return $self->search_up(@_);
136
142
        } else {
137
143
                return $self->search_down(@_);
138
144
        }
139
145
}
140
146
 
 
147
 
 
148
 
 
149
 
 
150
 
141
151
#####################################################################
142
152
# Content Abstraction
143
153
 
173
183
        die("Missing or invalid content object to search in");
174
184
}
175
185
 
 
186
sub count_all {
 
187
        my $self = shift;
 
188
        if ( _INSTANCE( $_[0], 'Padre::Wx::Editor' ) ) {
 
189
                return $self->editor_count_all(@_);
 
190
        }
 
191
        die("Missing or invalid ccontent object to search in");
 
192
}
 
193
 
 
194
 
 
195
 
 
196
 
 
197
 
176
198
#####################################################################
177
199
# Editor Interaction
178
200
 
224
246
                die("Failed to provide editor object to replace in");
225
247
        }
226
248
 
227
 
        # Execute the search and move to the resulting location
 
249
        # Execute the search
228
250
        my ( $start, $end, @matches ) = $self->matches(
229
251
                $editor->GetTextRange( 0, $editor->GetLength ),
230
252
                $self->search_regex,
231
253
                $editor->GetSelection,
232
254
        );
233
255
 
234
 
        # If they match replace it
235
 
        if ( defined $start and $start == 0 and $end == $editor->GetLength ) {
236
 
                $editor->ReplaceSelection( $self->replace_text );
237
 
                return 1;
238
 
        } else {
239
 
                return 0;
 
256
        # Are they perfectly selecting a match already?
 
257
        my $selection = [ $editor->GetSelection ];
 
258
        if ( $selection->[0] != $selection->[1] ) {
 
259
                if ( grep { $selection->[0] == $_->[0] and $selection->[1] == $_->[1] } @matches ) {
 
260
 
 
261
                        # Yes, replace it
 
262
                        $editor->ReplaceSelection( $self->replace_term );
 
263
 
 
264
                        # Move our selection to a point just before/after the replace,
 
265
                        # so that it doesn't double-match
 
266
                        if ( $self->find_reverse ) {
 
267
                                $editor->SetSelection( $start, $start );
 
268
                        } else {
 
269
 
 
270
                                # TODO: There might be unicode bugs in this.
 
271
                                # TODO: Someone that understands needs to check.
 
272
                                $start = $start + length( $self->replace_term );
 
273
                                $editor->SetSelection( $start, $start );
 
274
                        }
 
275
                }
240
276
        }
 
277
 
 
278
        # Move to the next match
 
279
        $self->search_next($editor);
241
280
}
242
281
 
243
282
sub editor_replace_all {
251
290
        my ( undef, undef, @matches ) = $self->matches(
252
291
                $editor->GetTextRange( 0, $editor->GetLength ),
253
292
                $self->search_regex,
 
293
                $editor->GetSelection
254
294
        );
255
295
 
256
296
        # Replace all matches as a single undo
269
309
        return scalar @matches;
270
310
}
271
311
 
 
312
sub editor_count_all {
 
313
        my $self   = shift;
 
314
        my $editor = shift;
 
315
        unless ( _INSTANCE( $editor, 'Padre::Wx::Editor' ) ) {
 
316
                die("Failed to provide editor object to replace in");
 
317
        }
 
318
 
 
319
        # Execute the search for all matches
 
320
        my ( undef, undef, @matches ) = $self->matches(
 
321
                $editor->GetTextRange( 0, $editor->GetLength ),
 
322
                $self->search_regex,
 
323
                $editor->GetSelection,
 
324
        );
 
325
 
 
326
        return scalar @matches;
 
327
}
 
328
 
 
329
 
 
330
 
 
331
 
 
332
 
272
333
#####################################################################
273
334
# Core Search
274
335
 
309
370
                return ( undef, undef );
310
371
        }
311
372
 
312
 
        my $pair = ();
 
373
        my $pair = [];
313
374
        my $from = shift || 0;
314
375
        my $to   = shift || 0;
315
376
        if ( $_[0] ) {
316
377
 
317
378
                # Search backwards
318
 
                my $pair = List::Util::first { $to > $_->[1] } reverse @matches;
 
379
                $pair = List::Util::first { $to > $_->[1] } reverse @matches;
319
380
                $pair = $matches[-1] unless $pair;
320
381
        } else {
321
382
 
322
383
                # Search forwards
323
 
                my $pair = List::Util::first { $from < $_->[0] } @matches;
 
384
                $pair = List::Util::first { $from < $_->[0] } @matches;
324
385
                $pair = $matches[0] unless $pair;
325
386
        }
326
387