package Padre::Plugin::TextExpansions; use 5.008; use strict; use warnings; use Padre::Constant (); use Padre::Current (); use Padre::Plugin (); use Padre::Wx (); our $VERSION = '0.01'; our @ISA = 'Padre::Plugin'; ##################################################################### # Padre::Plugin Methods sub plugin_name { 'TextExpansions'; } sub padre_interfaces { 'Padre::Plugin' => 0.43; } sub menu_plugins_simple { my ($self) = @_; TextExpansions => [ Wx::gettext("Expand Tag\tAlt+Shift+P") => sub { $self->expand_tag() }, Wx::gettext("Edit Expansions List") => sub { $self->edit_expansions() }, ]; } ##################################################################### # Custom Methods sub expansions_file { my ($self) = @_; return File::Spec->catfile( Padre::Constant::CONFIG_DIR, 'text_expansions' ); } sub expand_tag { my ($self) = @_; my $editor = $self->current->editor; my $document = $editor->{Document}; $editor->Freeze; my $pos = $editor->GetCurrentPos; my $line = $editor->LineFromPosition($pos); my $first = $editor->PositionFromLine($line); # characters from beginning of line to current position my $chars = $editor->GetTextRange( $first, $pos ); my $tag = ( split /\s/, $chars )[-1] || ''; my $tag_len = length($tag); # Does the tag correspond to a known expansion my %expansions = $self->load_expansions(); if ( exists $expansions{$tag} ) { # Select the tag and replace it with the expansion $editor->SetSelection( $pos - $tag_len, $pos ); $editor->ReplaceSelection( $expansions{$tag} ); } $editor->Thaw; } sub load_expansions { my ($self) = @_; my %tags; my $exp_file = $self->expansions_file(); my $IN; if ( open( $IN, '<', $exp_file ) ) { my @entries = grep {m/^\s*[^#]/} (<$IN>); chomp @entries; close $IN; foreach my $i (@entries) { my ( $tag, $expansion ) = split /\s+\|\s+/, $i, 2; next unless ( $tag && $expansion ); $tag =~ s/\s+$//; $expansion =~ s|\\\\n|\n|g; $expansion =~ s|\\\\t|\t|g; $tags{$tag} = $expansion; } } return %tags; } sub edit_expansions { my ($self) = @_; my $exp_file = $self->expansions_file(); unless ( -f $exp_file ) { # Create a skeleton file my $OUT; if ( open( $OUT, '>', $exp_file ) ) { print $OUT <<'EOT'; # Code expansions list file # # This file should contain one line for each code expansion. # # Entries are a pipe separated list of the form: # tag | expanded code # # Comment lines start with a '#' # orin | open ($IN, '<', $source) || die "Could not open $source for reading. !$\n";\\n\\nclose $IN; EOT close $OUT; } } $self->current->main->setup_editors($exp_file); } 1; __END__ =pod =head1 NAME Padre::Plugin::TextExpansions - Text expansion plugin =head1 DESCRIPTION Mostly as a proof of concept (although it does appear useful as is), this plugin allows a user to maintain/use a list of tags and the text that those tags should be expanded to. By typing the tag and invoking the plugin the tag then gets replaced with the expanded text. Expansions are stored in a plain text file in the user's ~/.padre directory. Expansion entries are listed in the file, one expansion per line, as a pipe separated list of the form: tag | expanded text Comment lines start with a '#' and are ignored. Blank lines are also ignored. Tabs can be stored in the expansion as a '\\t' character sequence, returns and new lines can be stored as '\\n'. =head1 AUTHOR Gregory Siems Egsiems@gmail.comE =head1 COPYRIGHT AND LICENSE Copyright (C) 2009 by Gregory Siems This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available. =cut