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

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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
package Padre::Wx::Ack;

=pod

=head1 NAME

Padre::Wx::Ack - Find in files, using L<Ack>

=head1 DESCRIPTION

C<Padre::Wx::Ack> implements a search dialog used to find recursively in
files. It is using C<Ack> underneath, for lots of nifty features.

=cut

use 5.008;
use strict;
use warnings;
use File::Basename    ();
use Padre::Current    ();
use Padre::DB         ();
use Padre::Wx         ();
use Padre::Wx::Dialog ();

our $VERSION = '0.63';

my $iter;
my %opts;
my %stats;
my $panel_string_index = 9999999;

my $DONE_EVENT : shared = Wx::NewEventType;

my $loaded = 0;

sub load {
	my $minver = 1.86;
	my $error  = "App::Ack $minver required for this feature";

	# try to load app::ack - we don't require $minver in the eval to
	# provide a meaningful error message if needed.
	eval "use App::Ack";
	if ($@) {
		return "$error (module not installed)";
	}
	if ( $App::Ack::VERSION < $minver ) {
		return "$error (you have $App::Ack::VERSION installed)";
	}

	# redefine some app::ack subs to display results in padre's output
	SCOPE: {
		no warnings 'redefine', 'once';
		*{App::Ack::print_first_filename} = sub { print_results("$_[0]\n"); };
		*{App::Ack::print_separator}      = sub { print_results("--\n"); };
		*{App::Ack::print}                = sub { print_results( $_[0] ); };
		*{App::Ack::print_filename}       = sub { print_results("$_[0]$_[1]"); };
		*{App::Ack::print_line_no}        = sub { print_results("$_[0]$_[1]"); };
	}

	return;
}

sub on_ack {
	my $main    = shift;
	my $current = $main->current;

	# delay App::Ack loading till first use, to reduce memory
	# usage and init time.
	unless ($loaded) {
		my $error = load();
		if ($error) {
			$main->error($error);
			return;
		}
		$loaded = 1;
	}

	my $project = $current->project;
	my $dialog  = dialog(
		$main,
		$current->text,
		$project ? $project->root : '',
	);
	$dialog->Show(1);

	return;
}

################################
# Dialog related

sub get_layout {
	my $term   = shift;
	my $dir    = shift;
	my $search = Padre::DB::History->recent('search');
	my $in_dir = Padre::DB::History->recent('find in');
	my $types  = Padre::DB::History->recent('find types');

	# default value is 1 for ignore_hidden_subdirs
	my $config = Padre->ide->config;

	my @layout = (
		[   [ 'Wx::StaticText', undef, Wx::gettext('Term:') ],
			[ 'Wx::ComboBox', '_ack_term_', $term, $search ],
			[ 'Wx::Button',   '_find_',     Wx::wxID_FIND ],
		],
		[   [ 'Wx::StaticText', undef, Wx::gettext('Dir:') ],
			[ 'Wx::ComboBox', '_ack_dir_',  $dir, $in_dir ],
			[ 'Wx::Button',   '_pick_dir_', Wx::gettext('Pick &directory') ],
		],
		[   [ 'Wx::StaticText', undef, Wx::gettext('In Files/Types:') ],
			[ 'Wx::ComboBox', '_file_types_', '', $types ],
			[ 'Wx::Button',   '_cancel_',     Wx::wxID_CANCEL ],
		],
		[   [   'Wx::CheckBox',
				'case_insensitive',
				Wx::gettext('Case &Insensitive'),
				( $config->find_case ? 0 : 1 )
			],
		],
		[   [   'Wx::CheckBox',
				'ignore_hidden_subdirs',
				Wx::gettext('I&gnore hidden Subdirectories'),
				$config->find_nohidden,
			],
		],
		[   [   'Wx::CheckBox',
				'files_without_match',
				Wx::gettext("Show only files that don't match"),
				$config->find_nomatch,
			],
		],

	);

	return \@layout;
}

sub dialog {
	my ( $main, $term, $directory ) = @_;

	my $layout = get_layout( $term, $directory );
	my $dialog = Padre::Wx::Dialog->new(
		parent => $main,
		title  => Wx::gettext("Find in Files"),
		layout => $layout,
		width  => [ 190, 210 ],
		size   => Wx::wxDefaultSize,
		pos    => Wx::wxDefaultPosition,
	);

	$dialog->{_widgets_}->{_find_}->SetDefault;

	Wx::Event::EVT_BUTTON( $dialog, $dialog->{_widgets_}->{_find_},     \&find_clicked );
	Wx::Event::EVT_BUTTON( $dialog, $dialog->{_widgets_}->{_pick_dir_}, \&on_pick_dir );
	Wx::Event::EVT_BUTTON( $dialog, $dialog->{_widgets_}->{_cancel_},   \&cancel_clicked );

	Wx::Event::EVT_IDLE(
		$dialog,
		sub {
			$dialog->{_widgets_}->{_ack_term_}->SetFocus;
			Wx::Event::EVT_IDLE( $dialog, undef );
		}
	);

	return $dialog;
}

sub on_pick_dir {
	my ( $dialog, $event ) = @_;

	my $main = Padre->ide->wx->main;

	my $default_dir = $dialog->{_widgets_}->{_ack_dir_}->GetValue;
	unless ($default_dir) { # we use currect editor
		my $filename = $main->current->filename;
		if ($filename) {
			$default_dir = File::Basename::dirname($filename);
		}
	}

	my $dir_dialog = Wx::DirDialog->new(
		$main,
		Wx::gettext("Select directory"),
		$default_dir
	);
	if ( $dir_dialog->ShowModal == Wx::wxID_CANCEL ) {
		return;
	}
	$dialog->{_widgets_}->{_ack_dir_}->SetValue( $dir_dialog->GetPath );

	return;
}

sub cancel_clicked {
	$_[0]->Destroy;
}

sub find_clicked {
	my ( $dialog, $event ) = @_;

	my $search = _get_data_from($dialog);

	$search->{dir} ||= '.';
	return if not $search->{term};

	my $main = Padre->ide->wx->main;

	@_ = (); # cargo cult or bug? see Wx::Thread / Creating new threads

	# TO DO kill the thread before closing the application

	# prepare \%opts
	%opts = ();
	$opts{regex} = $search->{term};

	# ignore_hidden_subdirs
	if ( $search->{ignore_hidden_subdirs} ) {
		$opts{all} = 1;
	} else {
		$opts{u} = 1; # unrestricted
	}

	# file_type
	fill_type_wanted();
	if ( my $file_types = $search->{file_types} ) {
		my $is_regex = 1;
		my $wanted = ( $file_types =~ s/^no// ) ? 0 : 1;
		foreach my $i ( App::Ack::filetypes_supported() ) {
			if ( $i eq $file_types ) {
				$App::Ack::type_wanted{$i} = $wanted;
				$is_regex = 0;
				last;
			}
		}
		$opts{G} = quotemeta $file_types if ($is_regex);
	}

	# case_insensitive
	$opts{i} = $search->{case_insensitive} if $search->{case_insensitive};

	# find only files that do not match
	if ( $search->{files_without_match} ) {
		$opts{l} = $opts{v} = 1;
	}

	# karl: borrowed this from ack hoping that will fix the ignore-case bug
	my $file_matching = $opts{f} || $opts{lines};
	if ( !$file_matching ) {
		$opts{regex} = App::Ack::build_regex( $opts{regex}, \%opts );
	}

	# check that all regexes do compile fine
	eval { App::Ack::check_regex( $opts{regex} ) };
	if ($@) {
		$main->error( "Find in Files: error in regex " . $opts{regex} );
		return;
	}


	my $what = App::Ack::get_starting_points( [ $search->{dir} ], \%opts );
	$iter = App::Ack::get_iterator( $what, \%opts );
	App::Ack::filetype_setup();

	unless ( $main->{ack} ) {
		create_ack_pane($main);
	}
	$main->show_output(1);
	show_ack_output( $main, 1 );
	$main->{ack}->DeleteAllItems;

	Wx::Event::EVT_COMMAND( $main, -1, $DONE_EVENT, \&ack_done );

	my $worker = threads->create( \&on_ack_thread );

	return;
}

sub _get_data_from {
	my ($dialog)              = @_;
	my $data                  = $dialog->get_data;
	my $term                  = $data->{_ack_term_};
	my $dir                   = $data->{_ack_dir_};
	my $file_types            = $data->{_file_types_};
	my $case_insensitive      = $data->{case_insensitive};
	my $ignore_hidden_subdirs = $data->{ignore_hidden_subdirs};
	my $files_without_match   = $data->{files_without_match};

	$dialog->Destroy;

	# Save our preferences
	my $config = Padre->ide->config;

	TRANSACTION: {
		my $lock = Padre::Current->main->lock('DB');
		Padre::DB::History->create(
			type => 'search',
			name => $term,
		) if $term;
		Padre::DB::History->create(
			type => 'find in',
			name => $dir,
		) if $dir;
		Padre::DB::History->create(
			type => 'find type',
			name => $file_types,
		) if $file_types;
	}

	$config->set( find_case => $case_insensitive ? 0 : 1 );
	$config->set( find_nohidden => $ignore_hidden_subdirs );
	$config->set( find_nomatch  => $files_without_match );

	return {
		term                  => $term,
		dir                   => $dir,
		file_types            => $file_types,
		case_insensitive      => $case_insensitive,
		ignore_hidden_subdirs => $ignore_hidden_subdirs,
		files_without_match   => $files_without_match,
	};
}

################################
# Ack pane related

sub create_ack_pane {
	my ($main) = @_;

	$main->{ack} = Wx::ListCtrl->new(
		$main->bottom,
		-1,
		Wx::wxDefaultPosition,
		Wx::wxDefaultSize,
		Wx::wxLC_SINGLE_SEL | Wx::wxLC_NO_HEADER | Wx::wxLC_REPORT
	);

	$main->{ack}->InsertColumn( 0, Wx::gettext('Find in Files') );
	$main->{ack}->SetColumnWidth( 0, Wx::wxLIST_AUTOSIZE );

	Wx::Event::EVT_LIST_ITEM_ACTIVATED(
		$main,
		$main->{ack},
		\&on_ack_result_selected,
	);
}

sub show_ack_output {
	my $main   = shift;
	my $on     = @_ ? $_[0] ? 1 : 0 : 1;
	my $bottom = $main->bottom;
	my $bp     = \$bottom;
	my $op     = \$main->{ack};
	my $idx    = ${$bp}->GetPageIndex( ${$op} );

	if ( $idx >= 0 ) {
		${$bp}->SetSelection($idx);
	} else {
		${$bp}->InsertPage(
			0,
			${$op},
			Wx::gettext('Find in Files'),
			1,
		);
		${$op}->Show;
	}
	$main->aui->GetPane('bottom')->Show;
	$main->aui->Update;

	return;
}

sub on_ack_result_selected {
	my ( $self, $event ) = @_;

	my $text = $event->GetItem->GetText;
	return if not defined $text;

	my ( $file, $line );

	# TODO: those appear to be very fragile regexps
	# specially with i18n of the message
	if ( $opts{l} ) {
		($file) = ( $text =~ /'.+'[^']+'(.+)'$/ );
		$line = 1 if $file;
	} else {
		( $file, $line ) = ( $text =~ /^(.*?)\((\d+)\)\:/ );
	}
	return unless $line;

	my $main = Padre->ide->wx->main;
	my $id   = $main->setup_editor($file);
	$main->on_nth_pane($id) if $id;

	my $page = $main->current->editor;
	$line--;
	$page->goto_line_centerize($line);
}

######################################
# Ack related

sub ack_done {
	my ( $main, $event ) = @_;

	my $data = $event->GetData;

	$main = Padre->ide->wx->main;
	$main->{ack}->InsertStringItem( $panel_string_index--, $data );
	$main->{ack}->SetColumnWidth( 0, Wx::wxLIST_AUTOSIZE );

	return;
}

sub on_ack_thread {

	# clear %stats; for every request
	%stats = (
		cnt_files   => 0,
		cnt_matches => 0,
	);

	if ( $opts{l} ) {
		App::Ack::print_files_with_matches( $iter, \%opts );

		# summary
		_send_text( '-' x 39 . "\n" ) if ( $stats{cnt_files} );
		_send_text( sprintf( Wx::gettext("Found %d files\n"), $stats{cnt_files} ) );
	} else {
		App::Ack::print_matches( $iter, \%opts );

		# summary
		_send_text( '-' x 39 . "\n" ) if ( $stats{cnt_files} );
		_send_text(
			sprintf(
				Wx::gettext("Found %d files and %d matches\n"),
				$stats{cnt_files},
				$stats{cnt_matches}
			)
		);
	}

}

sub print_results {
	my ($text) = @_;

	#print "$text\n";
	# TODO: for some reason App::Ack::print_files_with_matches()
	# is returning empty 1-length text. Any way to fix this will be
	# greatly appreciated (and we'll be able to remove the line below)
	return unless defined $text and $text !~ m{^\s*$}o;

	# the first is filename, the second is line number, the third is matched line text
	# while 'Binary file' and the 'files without matches' mode, there is ONLY filename
	$stats{printed_lines}++;

	# don't print filename again if it's just printed
	return
		if ($stats{printed_lines} % 3 == 1
		and $stats{last_matched_filename}
		and $stats{last_matched_filename} eq $text );
	if ( $stats{printed_lines} % 3 == 1 ) {
		if ( $text =~ /^Binary file/ or $opts{l} ) {
			$stats{printed_lines} = $stats{printed_lines} + 2;
		}

		$stats{last_matched_filename} = $text;
		$stats{cnt_files}++;

		# list only file names
		if ( $opts{l} ) {
			$text = sprintf(
				Wx::gettext("'%s' missing in file '%s'\n"),
				$opts{regex},
				$text
			);
		}

		# list file names and context
		else {

			# chop last ':', add \n after $filename
			chop($text);
			$text = sprintf(
				Wx::gettext("Found '%s' in '%s':\n"),
				$opts{regex},
				$text,
			);
		}

		# new line between different files
		_send_text( '-' x 39 . "\n" );
	} elsif ( $stats{printed_lines} % 3 == 2 ) {
		$stats{cnt_matches}++;

		# use () to wrap the number, an extra space for line number
		$text =~ s/(\d+)/\($1\)/;
		$text .= ' ';
	}

	#my $end = $result->get_end_iter;
	#$result->insert($end, $text);

	# just print it when we have \n
	if ( $text =~ /[\r\n]/ ) {
		my $filename = $stats{last_matched_filename};
		chop($filename);
		$text = $filename . $stats{last_text} . $text if $stats{last_text};
		delete $stats{last_text};
	} else {
		$stats{last_text} .= $text;
		return;
	}

	_send_text($text);

	return;
}

sub _send_text {
	my $text = shift;

	# Reformat the text to remove non-printable characters
	$text =~ s/\n\z//g;
	$text =~ s/\t/        /g;

	my $frame = Padre->ide->wx->main;
	my $threvent = Wx::PlThreadEvent->new( -1, $DONE_EVENT, $text );
	Wx::PostEvent( $frame, $threvent );
}

# see t/module.t in ack distro
sub fill_type_wanted {
	foreach my $i ( App::Ack::filetypes_supported() ) {
		$App::Ack::type_wanted{$i} = undef;
	}
}

1;

=pod

=head1 COPYRIGHT & LICENSE

Copyright 2008-2010 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 itself.

The full text of the license can be found in the
LICENSE file included with this module.

=cut

# Copyright 2008-2010 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.