Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Text/Code expansion plugin for Padre

by gsiems (Chaplain)
on Oct 08, 2009 at 11:40 UTC ( #799935=CUFP: print w/ replies, xml ) Need Help??

In responding to RFC: Padre::Plugin::MyTemplates, LanX pointed me to yasnippet (which loooks seriously cool). This got me wondering just how hard it would be to create a rudementary text/code expansion plugin for Padre.

A couple of hours later (I'm slow) and Ta-da.

--gsiems

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_ex +pansions() }, ]; } ##################################################################### # Custom Methods sub expansions_file { my ($self) = @_; return File::Spec->catfile( Padre::Constant::CONFIG_DIR, 'text_exp +ansions' ); } 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 rea +ding. !$\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 w +ith the expanded text. Expansions are stored in a plain text file in the user's ~/.padre dire +ctory. 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 i +gnored. Tabs can be stored in the expansion as a '\\t' character sequence, ret +urns and new lines can be stored as '\\n'. =head1 AUTHOR Gregory Siems E<lt>gsiems@gmail.comE<gt> =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

Comment on Text/Code expansion plugin for Padre
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://799935]
Approved by ww
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2015-07-07 05:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (87 votes), past polls