| 1 | package Padre::Plugin::Shell::Filter; | ||
| 2 | 1 | ▶ | use base 'Padre::Plugin::Shell::Base'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | |||
| 4 | use 5.008; | ||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | use Padre::Constant (); | ||
| 8 | use Padre::Current (); | ||
| 9 | use Padre::Wx (); | ||
| 10 | |||
| 11 | our $VERSION = '0.1'; | ||
| 12 | |||
| 13 | ######################################################################## | ||
| 14 | # | ||
| 15 | sub plugin_menu { | ||
| 16 | my ($self) = @_; | ||
| 17 | my @menu = (); | ||
| 18 | my %config = $self->get_config(); | ||
| 19 | my @accel_keys = ( 0 .. 9, 'A' .. 'Z' ); | ||
| 20 | foreach my $filter ( sort keys %config ) { | ||
| 21 | my $accel = ''; | ||
| 22 | if (@accel_keys) { | ||
| 23 | my $a = shift @accel_keys; | ||
| 24 | $accel = '&' . $a . ' - '; | ||
| 25 | } | ||
| 26 | push @menu, "$accel$filter" => sub { $self->run_filter($filter) }; | ||
| 27 | } | ||
| 28 | push @menu, '---' => undef; | ||
| 29 | push @menu, Wx::gettext("&Configure Filters") => sub { $self->edit_config_file() },; | ||
| 30 | |||
| 31 | return @menu; | ||
| 32 | } | ||
| 33 | |||
| 34 | sub example_config { | ||
| 35 | my ($self) = @_; | ||
| 36 | my $config = <<'EOT'; | ||
| 37 | --- | ||
| 38 | Sort: | ||
| 39 | command: 'sort [% IN %] > [% OUT %]' | ||
| 40 | description: Sort the selected text | ||
| 41 | input: either | ||
| 42 | output: replace | ||
| 43 | Sort Numeric: | ||
| 44 | command: 'sort -n [% IN %] > [% OUT %]' | ||
| 45 | description: Numerically sort the selected text | ||
| 46 | input: either | ||
| 47 | output: replace | ||
| 48 | Sort Unique: | ||
| 49 | command: 'sort -u [% IN %] > [% OUT %]' | ||
| 50 | description: Uniquely sort the selected text | ||
| 51 | input: either | ||
| 52 | output: replace | ||
| 53 | |||
| 54 | EOT | ||
| 55 | return $config; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub run_filter { | ||
| 59 | my ( $self, $filter ) = @_; | ||
| 60 | my %config = $self->get_config(); | ||
| 61 | if ( exists $config{$filter} ) { | ||
| 62 | |||
| 63 | my $editor = Padre::Current->editor or return; | ||
| 64 | |||
| 65 | # TODO complain if no editor? | ||
| 66 | |||
| 67 | ################################################################ | ||
| 68 | # Setup the environment | ||
| 69 | $self->update_environment_vars(); | ||
| 70 | |||
| 71 | ################################################################ | ||
| 72 | # Resolve/obtain the input text | ||
| 73 | my $input = $config{$filter}{input} || 'either'; | ||
| 74 | my $input_text = ''; | ||
| 75 | my $selected_text = $editor->GetSelectedText() || ''; | ||
| 76 | if ( $input =~ m/selection/i && length($selected_text) > 0 ) { | ||
| 77 | $input_text = $selected_text; | ||
| 78 | } | ||
| 79 | elsif ( $input =~ m/document/i ) { | ||
| 80 | $editor->SelectAll(); | ||
| 81 | $input_text = $editor->GetText(); | ||
| 82 | } | ||
| 83 | elsif ( $input =~ m/line/i ) { | ||
| 84 | my $pos = $editor->GetCurrentPos(); | ||
| 85 | my $line = $editor->LineFromPosition($pos); | ||
| 86 | $editor->SetSelection( $editor->PositionFromLine($line), $editor->GetLineEndPosition($line) ); | ||
| 87 | $input_text = $editor->GetSelectedText(); | ||
| 88 | } | ||
| 89 | elsif ( $input =~ m/none/i ) { | ||
| 90 | $input_text = ''; | ||
| 91 | } | ||
| 92 | else { | ||
| 93 | $self->notify_of_error( Wx::gettext("Unknown input specified for filter.") ); | ||
| 94 | return; | ||
| 95 | } | ||
| 96 | |||
| 97 | ################################################################ | ||
| 98 | # Run the filter | ||
| 99 | # Note that we could use STDIN and STDOUT, except... | ||
| 100 | # - long/large inputs | ||
| 101 | # - not all commands support STDIN/STDOUT? | ||
| 102 | # - Concerns regarding MS Windows | ||
| 103 | my $command = $config{$filter}{command}; | ||
| 104 | |||
| 105 | my $in_filename = $self->get_temp_file(); | ||
| 106 | my $IN; | ||
| 107 | if ( open $IN, '>', $in_filename ) { | ||
| 108 | print $IN $input_text; | ||
| 109 | close $IN; | ||
| 110 | } | ||
| 111 | |||
| 112 | my $out_filename = $self->get_temp_file(); | ||
| 113 | |||
| 114 | $command =~ s/\[\% IN \%\]/$in_filename/; | ||
| 115 | $command =~ s/\[\% OUT \%\]/$out_filename/; | ||
| 116 | |||
| 117 | my $rc = system($command); | ||
| 118 | if ( $rc == -1 ) { | ||
| 119 | $self->notify_of_error( Wx::gettext("Failed to run filter.\n") . $! ); | ||
| 120 | return; | ||
| 121 | } | ||
| 122 | |||
| 123 | ################################################################ | ||
| 124 | # | ||
| 125 | my $output = $config{$filter}{output} || 'append'; | ||
| 126 | |||
| 127 | if ( $output =~ m/new/i ) { | ||
| 128 | $self->new_document_from_file( $out_filename, 'text/plain' ); | ||
| 129 | } | ||
| 130 | elsif ( $output =~ m/replace/i ) { | ||
| 131 | $self->replace_selection_from_file($out_filename); | ||
| 132 | } | ||
| 133 | elsif ( $output =~ m/append/i ) { | ||
| 134 | $self->append_selection_from_file($out_filename); | ||
| 135 | } | ||
| 136 | else { | ||
| 137 | $self->notify_of_error( Wx::gettext("Unknown output specified for filter.") ); | ||
| 138 | return; | ||
| 139 | } | ||
| 140 | |||
| 141 | $self->delete_temp_file($in_filename); | ||
| 142 | $self->delete_temp_file($out_filename); | ||
| 143 | } | ||
| 144 | } | ||
| 145 | |||
| 146 | 1; | ||
| 147 | |||
| 148 | =pod | ||
| 149 | |||
| 150 | =head1 NAME | ||
| 151 | |||
| 152 | Padre::Plugin::Shell::Filter - Unix-like external filters in Padre. | ||
| 153 | |||
| 154 | =head1 DESCRIPTION | ||
| 155 | |||
| 156 | This plug-in enables the use of Unix-like external filtering | ||
| 157 | commands/scripts to transform part or all of the current document. | ||
| 158 | |||
| 159 | The output of the filter can either replace the input, be appended to | ||
| 160 | the input, or be inserted into a new document. | ||
| 161 | |||
| 162 | Unlike Unix filters, the filter mechanism in this plug-in | ||
| 163 | is designed to use input and output files rather than STDIN and STDOUT. | ||
| 164 | |||
| 165 | =head1 CONFIGURATION | ||
| 166 | |||
| 167 | Filter definitions are stored in a YAML formatted configuration file | ||
| 168 | in the user's Padre configuration directory (C<~/.padre>). | ||
| 169 | Each filter is labeled with the name that is to be displayed in the | ||
| 170 | filter menu. In addition to the name there are four attributes for | ||
| 171 | each filter definition: | ||
| 172 | |||
| 173 | =over | ||
| 174 | |||
| 175 | =item B<command> -- The command to run to perform the filtering. There | ||
| 176 | are two placeholders in the command string C<[% IN %]> and C<[% OUT %]> | ||
| 177 | for the input and output filenames used in running the filter. | ||
| 178 | |||
| 179 | =item B<description> A description of what the filter does (optional). | ||
| 180 | |||
| 181 | =item B<input> The source of the text to filter. Valid values are | ||
| 182 | I<selection>, I<document>, I<line>, and I<none>. I<Selection> may be | ||
| 183 | combined with either I<document>, I<line>, or I<none>. | ||
| 184 | The default value is I<selection,document>. | ||
| 185 | |||
| 186 | =over | ||
| 187 | |||
| 188 | =item B<selection> -- There needs to be a text selection for the filter | ||
| 189 | to run, and the filter is run using the selected text as input. | ||
| 190 | |||
| 191 | =item B<document> -- The filter uses the entire document as input | ||
| 192 | whether there is selected text or not. | ||
| 193 | |||
| 194 | =item B<line> -- The filter uses the current line as input | ||
| 195 | whether there is selected text or not. | ||
| 196 | |||
| 197 | =item B<none> -- Do not use any text as the input to the filter. | ||
| 198 | |||
| 199 | =item B<selection,(document|line|none)> -- If there is a text selection | ||
| 200 | then the filter uses the selected text as input. | ||
| 201 | If there is no selected text then the filter falls back to using the other | ||
| 202 | specified source (document, line, or none) as input. | ||
| 203 | |||
| 204 | =back | ||
| 205 | |||
| 206 | =item B<output> The action to take with the output. Valid values are | ||
| 207 | I<replace>, I<append>, and I<new>. The default value is I<append>. | ||
| 208 | |||
| 209 | =over | ||
| 210 | |||
| 211 | =item B<append> -- Appends the filter results after the input. | ||
| 212 | |||
| 213 | =item B<replace> -- Replaces the input with the filter results. | ||
| 214 | |||
| 215 | =item B<new> -- Creates a new document with the filter results. | ||
| 216 | |||
| 217 | =back | ||
| 218 | |||
| 219 | =back | ||
| 220 | |||
| 221 | =head1 ENVIRONMENT VARIABLES | ||
| 222 | |||
| 223 | To provide additional information for the filters, various | ||
| 224 | environment variables are set prior to running the filter. | ||
| 225 | These environment variables are covered in the | ||
| 226 | L<Padre::Plugin::Shell::Base> documentation. | ||
| 227 | |||
| 228 | =head1 METHODS | ||
| 229 | |||
| 230 | =head2 plugin_menu | ||
| 231 | |||
| 232 | Generates and returns a menu of the defined filters. | ||
| 233 | |||
| 234 | =head2 example_config | ||
| 235 | |||
| 236 | Returns an example configuration. This is the initial configuration | ||
| 237 | that gets created the first time that this plugin is used. | ||
| 238 | |||
| 239 | =head2 run_filter($filter_name) | ||
| 240 | |||
| 241 | Searches the filter configuration for a filter definition that | ||
| 242 | matches the supplied $filter_name. If a matching filter is found | ||
| 243 | then that filter is run. | ||
| 244 | |||
| 245 | =head1 ISSUES | ||
| 246 | |||
| 247 | When adding entries to the configuration file it appears to be necessary | ||
| 248 | to have an empty line at the end of the file in order for the configuration | ||
| 249 | to properly load. No properly loaded configuration results in no menu | ||
| 250 | for the plug-in. | ||
| 251 | |||
| 252 | =head1 AUTHOR | ||
| 253 | |||
| 254 | Gregory Siems E<lt>gsiems@gmail.comE<gt> | ||
| 255 | |||
| 256 | =head1 COPYRIGHT AND LICENSE | ||
| 257 | |||
| 258 | Copyright (C) 2009 by Gregory Siems | ||
| 259 | |||
| 260 | This library is free software; you can redistribute it and/or modify | ||
| 261 | it under the same terms as Perl itself, either Perl version 5.8.8 or, | ||
| 262 | at your option, any later version of Perl 5 you may have available. | ||
| 263 | |||
| 264 | =cut | ||