App-Codit
view release on metacpan or search on metacpan
lib/App/Codit/Plugins/SearchReplace.pm view on Meta::CPAN
package App::Codit::Plugins::SearchReplace;
=head1 NAME
App::Codit::Plugins::SearchReplace - plugin for App::Codit
=cut
use strict;
use warnings;
use vars qw( $VERSION );
$VERSION = '0.19';
use base qw( Tk::AppWindow::BaseClasses::Plugin );
use Tk;
require Tk::LabFrame;
require Tk::ITree;
my $srchcur = 'Search in current document';
my $srchall = 'Search in all documents';
my $srchprj = 'Search in project files';
my $srchres = 'Search in results';
=head1 DESCRIPTION
Search and replace across multiple files.
=head1 DETAILS
This plugin allows you to do a search and replace across multiple or just one file.
After filling out the search and replace fields you first click Find.
The list box will fill with the search results.
When you click Replace the first item in the list is replaced and then removed from the list.
You can skip replaces by pressing Skip.
When searching with a regular expression you can include the captures you make with () in
the replace string with â$1â, â$2â, etcetera.
Clear removes all search results.
=cut
sub new {
my $class = shift;
my $self = $class->SUPER::new(@_);
return undef unless defined $self;
# my $tp = $self->extGet('ToolPanel');
# my $page = $tp->addPage('SearchReplace', 'edit-find-replace', undef, 'Search and replace');
my $page = $self->ToolRightPageAdd('SearchReplace', 'edit-find-replace', undef, 'Search and replace', 350);
my $searchterm = '';
my $replaceterm = '';
my $casesensitive = '-case';
my $useregex = '-exact';
my $searchmode = 'Search in current document';
$self->{CASE} = \$casesensitive;
$self->{CURRENT} = undef;
$self->{LASTRESULTS} = [];
$self->{MODE} = \$searchmode;
$self->{OFFSET} = {};
$self->{REPLACE} = \$replaceterm;
$self->{REPLACED} = 0;
$self->{REGEX} = \$useregex;
$self->{SEARCH} = \$searchterm;
$self->{SKIPPED} = 0;
my @padding = (-padx => 2, -pady => 2);
my $sa = $page->Frame(
-relief => 'groove',
-borderwidth => 2,
)->pack(@padding, -fill => 'x');
my $sf = $sa->Frame->pack(-expand => 1, -fill => 'x');
$sf->Label(
-text => 'Search',
-width => 7,
-anchor => 'e',
)->pack(@padding, -side => 'left');
my $se = $sf->Entry(
-textvariable => \$searchterm,
)->pack(@padding, -side => 'left', -expand => 1, -fill => 'x');
$se->bind('<Return>', [$self, 'Find']);
my $rf = $sa->Frame->pack(-expand => 1, -fill => 'x');
$rf->Label(
-text => 'Replace',
-width => 7,
-anchor => 'e',
)->pack(@padding, -side => 'left');
$rf->Entry(
-textvariable => \$replaceterm,
)->pack(@padding, -side => 'left', -expand => 1, -fill => 'x');
my $sb = $page->LabFrame(
-labelside => 'acrosstop',
-relief => 'groove',
-label => 'Search options',
)->pack(@padding, -fill => 'x');
$sb->Checkbutton(
-variable => \$useregex,
-text => 'Regular expression',
lib/App/Codit/Plugins/SearchReplace.pm view on Meta::CPAN
my $self = shift;
my $first = $self->GetFirst;
$self->Select($first) if defined $first
}
sub SelectLast {
my $self = shift;
my $last = $self->GetLast;
$self->Select($last) if defined $last
}
sub ShowResults {
my ($self, $log) = @_;
$log = 0 unless defined $log;
my $mdi = $self->mdi;
my $name = $mdi->docSelected;
return @_ unless defined $name;
my $widg = $mdi->docWidget;
my $list = $self->list;
my @hits = $self->GetList($name);
$widg->FindClear;
for (@hits) {
my $data = $list->infoData($_);
my ($begin, $end) = @$data;
$widg->tagAdd('Find', $begin, $end);
}
if ($log) {
my $nhits = @hits;
my @e = $list->infoChildren('');
my $ndocs = @e;
$self->log("$nhits hits in $ndocs documents");
}
}
sub Skip {
my $self = shift;
my $cur = $self->GetCurrent;
unless (defined $cur) {
$self->logWarning('Nothing to skip');
return
}
my ($name, $index) = split(/@/, $cur);
if (defined $self->Current) {
$self->OffsetInc($name);
$self->skipped($self->skipped + 1);
$self->Report;
}
$self->GoCurrent unless $self->FinishedCheck;
}
sub skipped {
my $self = shift;
$self->{SKIPPED} = shift if @_;
return $self->{SKIPPED}
}
sub Unload {
my $self = shift;
$self->ToolRightPageRemove('SearchReplace');
$self->cmdUnhookAfter('doc_select', 'ShowResults', $self);
return $self->SUPER::Unload
}
=head1 LICENSE
Same as Perl.
=head1 AUTHOR
Hans Jeuken (hanje at cpan dot org)
=head1 TODO
=over 4
=back
=head1 BUGS AND CAVEATS
If you find any bugs, please report them here L<https://github.com/haje61/App-Codit/issues>.
=head1 SEE ALSO
=over 4
=back
=cut
1;
( run in 0.731 second using v1.01-cache-2.11-cpan-99c4e6809bf )