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

In reply to Text/Code expansion plugin for Padre by gsiems

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.