~ubuntu-branches/ubuntu/saucy/padre/saucy-proposed

« back to all changes in this revision

Viewing changes to lib/Padre/Search.pm

  • Committer: Package Import Robot
  • Author(s): Dominique Dumont, gregor herrmann, Dominique Dumont
  • Date: 2012-01-04 12:04:20 UTC
  • mfrom: (1.3.3)
  • Revision ID: package-import@ubuntu.com-20120104120420-i5oybqwf91m1d3il
Tags: 0.92.ds1-1
[ gregor herrmann ]
* Remove debian/source/local-options; abort-on-upstream-changes
  and unapply-patches are default in dpkg-source since 1.16.1.
* Swap order of alternative (build) dependencies after the perl
  5.14 transition.

[ Dominique Dumont ]
* Imported Upstream version 0.92.ds1
* removed fix-spelling patch (applied upstream)
* lintian-override: use wildcard to avoid listing a gazillion files
* updated size of some 'not-real-man-page' entries
* rules: remove dekstop cruft (replaced by a file provided in debian
  directory)
* control: removed Breaks statement. Add /me to uploaders. Updated
  dependencies
* rules: make sure that non-DFSG file (i.e. the cute butterfly, sigh)
  is not distributed

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
use List::Util   ();
33
33
use Params::Util ();
34
34
 
35
 
our $VERSION = '0.90';
 
35
our $VERSION    = '0.92';
 
36
our $COMPATIBLE = '0.64';
36
37
 
37
38
sub new {
38
39
        my $class = shift;
191
192
        my $self = shift;
192
193
        if ( Params::Util::_INSTANCE( $_[0], 'Padre::Wx::Editor' ) ) {
193
194
                return $self->editor_replace_all(@_);
 
195
        } elsif ( Params::Util::_SCALAR0( $_[0] ) ) {
 
196
                return $self->scalar_replace_all(@_);
194
197
        }
195
198
        die "Missing or invalid content object to search in";
196
199
}
199
202
        my $self = shift;
200
203
        if ( Params::Util::_INSTANCE( $_[0], 'Padre::Wx::Editor' ) ) {
201
204
                return $self->editor_count_all(@_);
 
205
        } elsif ( Params::Util::_SCALAR0( $_[0] ) ) {
 
206
                return $self->scalar_count_all(@_);
202
207
        }
203
208
        die "Missing or invalid content object to search in";
204
209
}
293
298
        $self->search_next($editor);
294
299
}
295
300
 
 
301
sub editor_count_all {
 
302
        my $self   = shift;
 
303
        my $editor = shift;
 
304
        unless ( Params::Util::_INSTANCE( $editor, 'Padre::Wx::Editor' ) ) {
 
305
                die "Failed to provide editor object to count in";
 
306
        }
 
307
 
 
308
        # Execute the regex search for all matches
 
309
        $self->match_count(
 
310
                $editor->GetTextRange( 0, $editor->GetLength ),
 
311
                $self->search_regex,
 
312
        );
 
313
}
 
314
 
296
315
sub editor_replace_all {
297
316
        my $self   = shift;
298
317
        my $editor = shift;
323
342
        return scalar @matches;
324
343
}
325
344
 
326
 
sub editor_count_all {
327
 
        my $self   = shift;
328
 
        my $editor = shift;
329
 
        unless ( Params::Util::_INSTANCE( $editor, 'Padre::Wx::Editor' ) ) {
330
 
                die "Failed to provide editor object to replace in";
331
 
        }
332
 
 
333
 
        # Execute the search for all matches
334
 
        my ( undef, undef, @matches ) = $self->matches(
335
 
                $editor->GetTextRange( 0, $editor->GetLength ),
 
345
 
 
346
 
 
347
 
 
348
 
 
349
#####################################################################
 
350
# Scalar Interaction
 
351
 
 
352
sub scalar_replace_all {
 
353
        my $self   = shift;
 
354
        my $scalar = shift;
 
355
        unless ( Params::Util::_SCALAR0($scalar) ) {
 
356
                die "Failed to provide SCALAR to count in";
 
357
        }
 
358
 
 
359
        # Prepare the search and replace
 
360
        my $search  = $self->search_regex;
 
361
        my $replace = $self->replace_term;
 
362
 
 
363
        # Do the replacement
 
364
        my $count = $$scalar =~ s/$search/$replace/g;
 
365
 
 
366
        # Return the replace count
 
367
        return $count;
 
368
}
 
369
 
 
370
sub scalar_count_all {
 
371
        my $self   = shift;
 
372
        my $scalar = shift;
 
373
        unless ( Params::Util::_SCALAR0($scalar) ) {
 
374
                die "Failed to provide SCALAR to count in";
 
375
        }
 
376
 
 
377
        # Execute the regex search for all matches
 
378
        $self->match_count(
 
379
                $$scalar,
336
380
                $self->search_regex,
337
 
                $editor->GetSelection,
338
381
        );
339
 
 
340
 
        return scalar @matches;
341
382
}
342
383
 
343
384
 
345
386
 
346
387
 
347
388
#####################################################################
348
 
# Core Search
 
389
# Core Search Methods
349
390
 
350
391
=pod
351
392
 
407
448
        return ( @$pair, @matches );
408
449
}
409
450
 
410
 
# NOTE: This current fails to work with multi-line searche expressions
 
451
# NOTE: This current fails to work with multi-line search expressions
411
452
sub match_lines {
412
 
        my ( $self, $selected_text, $regex ) = @_;
413
 
 
414
 
        # Searches run in unicode
415
 
        my $text = Encode::encode( 'utf-8', $selected_text );
416
 
        my @lines = split( /\n/, $text );
417
 
 
418
 
        my @matches = ();
419
 
        foreach my $i ( 0 .. $#lines ) {
420
 
                next unless $lines[$i] =~ /$regex/;
421
 
                push @matches, [ $i + 1, $lines[$i] ];
422
 
        }
423
 
        return @matches;
 
453
        my $self  = shift;
 
454
        my @lines = split /\n/, Encode::encode( 'utf-8', shift );
 
455
        my $regex = shift;
 
456
 
 
457
        # Apply the search regex as a filter
 
458
        return map { [ $_ + 1, $lines[$_] ] } grep { $lines[$_] =~ /$regex/ } ( 0 .. $#lines );
 
459
}
 
460
 
 
461
sub match_count {
 
462
        my $self  = shift;
 
463
        my $text  = Encode::encode( 'utf-8', shift );
 
464
        my $regex = shift;
 
465
        my $count = () = $text =~ /$regex/g;
 
466
        return $count;
424
467
}
425
468
 
426
469
1;