This file is indexed.

/usr/share/perl5/Padre/Search.pm is in padre 1.00+dfsg-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
package Padre::Search;

=pod

=head1 NAME

Padre::Search - The Padre Search API

=head2 SYNOPSIS

  # Create the search object
  my $search = Padre::Search->new(
      find_term => 'foo',
  );
  
  # Execute the search on the current editor
  $search->search_next(Padre::Current->editor);

=head2 DESCRIPTION

This is the Padre Search API. It allows the creation of abstract search
object that can independently search and/or replace in an editor object.

=head2 METHODS

=cut

use 5.008;
use strict;
use warnings;
use Carp         ();
use Encode       ();
use List::Util   ();
use Scalar::Util ();
use Params::Util ();

our $VERSION    = '1.00';
our $COMPATIBLE = '0.93';

sub new {
	my $class = shift;
	my $self = bless {@_}, $class;

	# Check params
	unless ( defined $self->find_term ) {
		die "Did not provide 'find_term' search term";
	}
	unless ( length $self->find_term ) {

		# Pointless zero-length search
		return;
	}

	# Apply defaults
	$self->{find_case}    ||= 0;
	$self->{find_regex}   ||= 0;
	$self->{find_reverse} ||= 0;

	# Pre-compile the search
	unless ( defined $self->search_regex ) {
		return;
	}

	return $self;
}

sub find_term {
	$_[0]->{find_term};
}

sub find_case {
	$_[0]->{find_case};
}

sub find_regex {
	$_[0]->{find_regex};
}

sub find_reverse {
	$_[0]->{find_reverse};
}

sub replace_term {
	$_[0]->{replace_term};
}

sub search_regex {
	my $self = shift;

	# Escape the raw search term
	my $term = $self->find_term;
	if ( $self->find_regex ) {

		# Escape non-trailing $ so they won't interpolate
		$term =~ s/\$(?!\z)/\\\$/g;
	} else {

		# Escape everything
		$term = quotemeta $term;
	}

	# Compile the regex
	my $search_regex = eval { $self->find_case ? qr/$term/m : qr/$term/mi };
	return if $@;
	return $search_regex;
}

sub equals {
	my $self = shift;
	my $search = Params::Util::_INSTANCE( shift, 'Padre::Search' ) or return;
	return Scalar::Util::refaddr($self) == Scalar::Util::refaddr($search);
}





#####################################################################
# Command Abstraction

sub search_next {
	my $self = shift;
	if ( $self->find_reverse ) {
		return $self->search_up(@_);
	} else {
		return $self->search_down(@_);
	}
}

sub search_previous {
	my $self = shift;
	if ( $self->find_reverse ) {
		return $self->search_down(@_);
	} else {
		return $self->search_up(@_);
	}
}

sub replace_next {
	my $self = shift;
	if ( $self->find_reverse ) {
		return $self->replace_up(@_);
	} else {
		return $self->replace_down(@_);
	}
}

sub replace_previous {
	my $self = shift;
	if ( $self->find_reverse ) {
		return $self->replace_down(@_);
	} else {
		return $self->replace_up(@_);
	}
}





#####################################################################
# Content Abstraction

sub search_down {
	my $self   = shift;
	my $editor = _EDITOR(shift);
	$self->editor_search_down( $editor, @_ );
}

sub search_up {
	my $self   = shift;
	my $editor = _EDITOR(shift);
	$self->editor_search_up( $editor, @_ );
}

sub search_count {
	my $self = shift;
	if ( Params::Util::_INSTANCE( $_[0], 'Padre::Wx::Editor' ) ) {
		return $self->editor_search_count(@_);
	} elsif ( Params::Util::_SCALAR0( $_[0] ) ) {
		return $self->scalar_search_count(@_);
	}
	die "Missing or invalid content object";
}

sub replace {
	my $self   = shift;
	my $editor = _EDITOR(shift);
	$self->editor_replace_down( $editor, @_ );
}

sub replace_down {
	my $self   = shift;
	my $editor = _EDITOR(shift);
	$self->editor_replace_down( $editor, @_ );
}

sub replace_up {
	my $self   = shift;
	my $editor = _EDITOR(shift);
	$self->editor_replace_up( $editor, @_ );
}

sub replace_all {
	my $self = shift;
	if ( Params::Util::_INSTANCE( $_[0], 'Padre::Wx::Editor' ) ) {
		return $self->editor_replace_all(@_);
	} elsif ( Params::Util::_SCALAR0( $_[0] ) ) {
		return $self->scalar_replace_all(@_);
	}
	die "Missing or invalid content object";
}





#####################################################################
# Editor Interaction

sub editor_search_down {
	my $self   = shift;
	my $editor = _EDITOR(shift);

	# Execute the search and move to the resulting location
	my ( $start, $end, @matches ) = $self->matches(
		text  => $editor->GetText,
		regex => $self->search_regex,
		from  => $editor->GetSelectionStart,
		to    => $editor->GetSelectionEnd,
	);
	return unless defined $start;

	# Highlight the found item
	$editor->match( $self, $start, $end );
}

sub editor_search_up {
	my $self   = shift;
	my $editor = _EDITOR(shift);

	# Execute the search and move to the resulting location
	my ( $start, $end, @matches ) = $self->matches(
		text      => $editor->GetText,
		regex     => $self->search_regex,
		from      => $editor->GetSelectionStart,
		to        => $editor->GetSelectionEnd,
		backwards => 1,
	);
	return unless defined $start;

	# Highlight the found item
	$editor->match( $self, $start, $end );
}

sub editor_search_count {
	my $self   = shift;
	my $editor = _EDITOR(shift);

	# Execute the regex search for all matches
	$self->match_count(
		$editor->GetText,
		$self->search_regex,
	);
}

sub editor_replace_down {
	my $self   = shift;
	my $editor = _EDITOR(shift);
	my $from   = $editor->GetSelectionStart;
	my $to     = $editor->GetSelectionEnd;

	# Execute the search so we can establish if we have already
	# selected a match.
	my ( $start, $end, @matches ) = $self->matches(
		text  => $editor->GetText,
		regex => $self->search_regex,
		from  => $from,
		to    => $from,
	);
	return unless @matches;

	# Are we perfectly selecting a match already
	unless ( $from == $to ) {
		foreach my $i ( 0 .. $#matches ) {
			my $match = $matches[$i];
			next unless $match->[0] == $from;
			next unless $match->[1] == $to;

			# The selection matches, replace it
			$editor->ReplaceSelection( $self->replace_term );

			# Shortcut if there are no more matches
			return unless $#matches;

			# Move to the next match
			if ( $i == $#matches ) {

				# Wrap to the beginning of the document
				$start = $matches[0]->[0];
				$end   = $matches[0]->[1];
			} else {
				my $delta = $editor->GetSelectionEnd - $to;
				my $down  = $matches[ $i + 1 ];
				$start = $down->[0] + $delta;
				$end   = $down->[1] + $delta;
			}

			last;
		}
	}

	$editor->match( $self, $start, $end );
}

sub editor_replace_up {
	my $self   = shift;
	my $editor = _EDITOR(shift);
	my $from   = $editor->GetSelectionStart;
	my $to     = $editor->GetSelectionEnd;

	# Execute the search so we can establish if we have already
	# selected a match.
	my ( $start, $end, @matches ) = $self->matches(
		text  => $editor->GetTextRange( 0, $editor->GetLength ),
		regex => $self->search_regex,
		from  => $from,
		to    => $from,
	);
	return unless @matches;

	# Are we perfectly selecting a match already
	unless ( $from == $to ) {
		foreach my $i ( 0 .. $#matches ) {
			my $match = $matches[$i];
			next unless $match->[0] == $from;
			next unless $match->[1] == $to;

			# The selection matches, replace it
			$editor->ReplaceSelection( $self->replace_term );

			# Shortcut if there are no more matches
			return unless $#matches;

			# Move to the next match
			if ( $i == 0 ) {

				# Wrap to the end of the document
				my $delta = $editor->GetSelectionEnd - $to;
				$start = $matches[-1]->[0] + $delta;
				$end   = $matches[-1]->[1] + $delta;
			} else {
				my $up = $matches[ $i - 1 ];
				$start = $up->[0];
				$end   = $up->[1];
			}

			last;
		}
	}

	$editor->match( $self, $start, $end );
}

sub editor_replace_all {
	my $self   = shift;
	my $editor = _EDITOR(shift);

	# Execute the search for all matches
	my ( undef, undef, @matches ) = $self->matches(
		text  => $editor->GetTextRange( 0, $editor->GetLength ),
		regex => $self->search_regex,
		from  => $editor->GetSelectionStart,
		to    => $editor->GetSelectionEnd,
	);
	return 0 unless @matches;

	# Replace all matches, returning the number we replaced
	my $replace = $self->replace_term;
	@matches = map { [ @$_, $replace ] } reverse @matches;
	require Padre::Delta;
	Padre::Delta->new( position => @matches )->to_editor($editor);
}





#####################################################################
# Scalar Interaction

sub scalar_search_count {
	my $self   = shift;
	my $scalar = shift;
	unless ( Params::Util::_SCALAR0($scalar) ) {
		die "Failed to provide SCALAR to count in";
	}

	# Execute the regex search for all matches
	$self->match_count(
		$$scalar,
		$self->search_regex,
	);
}

sub scalar_replace_all {
	my $self   = shift;
	my $scalar = shift;
	unless ( Params::Util::_SCALAR0($scalar) ) {
		die "Failed to provide SCALAR to count in";
	}

	# Prepare the search and replace
	my $search  = $self->search_regex;
	my $replace = $self->replace_term;

	# Do the replacement
	my $count = $$scalar =~ s/$search/$replace/g;

	# Return the replace count
	return $count;
}





#####################################################################
# Core Search Methods

=pod

=head2 matches

  my ($first_char, $last_char, @all) = $search->matches(
      text      => $search_text,
      regex     => $search_regexp,
      from      => $from,
      to        => $to,
      backwards => $reverse,
  );

Parameters:

* The text in which we need to search

* The regular expression

* The offset within the text where we the last match started so the next
  forward match must start after this.

* The offset within the text where we the last match ended so the next
  backward match must end before this.

* backward bit (1 = search backward, 0 = search forward)

=cut

sub matches {
	my $self  = shift;
	my %param = @_;

	# Searches run in unicode
	my $text  = Encode::encode( 'utf-8', delete $param{text} );
	my $regex = Encode::encode( 'utf-8', delete $param{regex} );

	# Find all matches for the regex
	my @matches = ();
	my $submatch = $param{submatch} || 0;
	while ( $text =~ /$regex/g ) {
		push @matches, [ $-[$submatch], $+[$submatch] ];
	}
	unless (@matches) {
		return ( undef, undef );
	}

	my $pair = [];
	my $from = $param{from} || 0;
	my $to   = $param{to} || 0;

	if ( $param{backwards} ) {

		# Search backwards
		$pair = List::Util::first { $from >= $_->[1] } reverse @matches;
		$pair = $matches[-1] unless $pair;
	} else {

		# Search forwards
		$pair = List::Util::first { $to <= $_->[0] } @matches;
		$pair = $matches[0] unless $pair;
	}

	return ( @$pair, @matches );
}

# NOTE: This current fails to work with multi-line search expressions
sub match_lines {
	my $self  = shift;
	my @lines = split /\n/, Encode::encode( 'utf-8', shift );
	my $regex = shift;

	# Apply the search regex as a filter
	return map { [ $_ + 1, $lines[$_] ] } grep { $lines[$_] =~ /$regex/ } ( 0 .. $#lines );
}

sub match_count {
	my $self  = shift;
	my $text  = Encode::encode( 'utf-8', shift );
	my $regex = shift;
	my $count = () = $text =~ /$regex/g;
	return $count;
}





######################################################################
# Support Functions

sub _EDITOR {
	unless ( Params::Util::_INSTANCE( $_[0], 'Padre::Wx::Editor' ) ) {
		Carp::croak("Missing or invalid Padre::Ex::Editor param");
	}
	return $_[0];
}

1;

=pod

=head1 COPYRIGHT & LICENSE

Copyright 2008-2013 The Padre development team as listed in Padre.pm.

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl 5 itself.

=cut

# Copyright 2008-2013 The Padre development team as listed in Padre.pm.
# LICENSE
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl 5 itself.