| 1 | package Padre::Plugin::Experimento; | ||
| 2 | |||
| 3 | use Modern::Perl; | ||
| 4 | use Padre::Plugin (); | ||
| 5 | |||
| 6 | our $VERSION = '0.02'; | ||
| 7 | our @ISA = 'Padre::Plugin'; | ||
| 8 | |||
| 9 | # Child modules we need to unload when disabled | ||
| 10 | use constant CHILDREN => qw{ | ||
| 11 | Padre::Plugin::Experimento | ||
| 12 | }; | ||
| 13 | |||
| 14 | # Called when Padre wants to check what package versions this | ||
| 15 | # plugin needs | ||
| 16 | sub padre_interfaces { | ||
| 17 | 'Padre::Plugin' => 0.94,; | ||
| 18 | } | ||
| 19 | |||
| 20 | # Called when Padre wants a name for the plugin | ||
| 21 | sub plugin_name { | ||
| 22 | Wx::gettext('Experimento'); | ||
| 23 | } | ||
| 24 | |||
| 25 | ####### | ||
| 26 | # Called by padre to build the menu in a simple way | ||
| 27 | ####### | ||
| 28 | sub menu_plugins_simple { | ||
| 29 | my $self = shift; | ||
| 30 | return $self->plugin_name => [ | ||
| 31 | Wx::gettext('Move Selected Lines Up') . "\tCtrl-Shift-Up" => sub { $self->move_selected_lines_up }, | ||
| 32 | Wx::gettext('Move Selected Lines Down') . "\tCtrl-Shift-Down" => sub { $self->move_selected_lines_down }, | ||
| 33 | '---' => undef, | ||
| 34 | Wx::gettext('Check POD') => sub { $self->check_pod }, | ||
| 35 | '---' => undef, | ||
| 36 | Wx::gettext('About') => sub { $self->show_about }, | ||
| 37 | ]; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub show_about { | ||
| 41 | my $self = shift; | ||
| 42 | |||
| 43 | # Generate the About dialog | ||
| 44 | my $about = Wx::AboutDialogInfo->new; | ||
| 45 | $about->SetName( Wx::gettext('Experimento Plug-in') ); | ||
| 46 | my $authors = 'Ahmad M. Zawawi'; | ||
| 47 | my $description = Wx::gettext( <<'END' ); | ||
| 48 | Experimental features for Padre | ||
| 49 | |||
| 50 | Copyright 2012 %s | ||
| 51 | This plug-in is free software; you can redistribute it and/or modify it under the same terms as Padre. | ||
| 52 | END | ||
| 53 | $about->SetDescription( sprintf( $description, $authors ) ); | ||
| 54 | |||
| 55 | # Show the About dialog | ||
| 56 | Wx::AboutBox($about); | ||
| 57 | |||
| 58 | return; | ||
| 59 | } | ||
| 60 | |||
| 61 | sub move_selected_lines_up { | ||
| 62 | my $self = shift; | ||
| 63 | |||
| 64 | my $editor = $self->current->editor or return; | ||
| 65 | |||
| 66 | if ( $editor->can('MoveSelectedLinesUp') ) { | ||
| 67 | $editor->MoveSelectedLinesUp; | ||
| 68 | } else { | ||
| 69 | $self->main->error( Wx::gettext('Error: Wx::Scintilla should be 0.3801 or later') ); | ||
| 70 | } | ||
| 71 | |||
| 72 | return; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub move_selected_lines_down { | ||
| 76 | my $self = shift; | ||
| 77 | |||
| 78 | my $editor = $self->current->editor or return; | ||
| 79 | if ( $editor->can('MoveSelectedLinesDown') ) { | ||
| 80 | $editor->MoveSelectedLinesDown; | ||
| 81 | } else { | ||
| 82 | $self->main->error( Wx::gettext('Error: Wx::Scintilla should be 0.3801 or later') ); | ||
| 83 | } | ||
| 84 | |||
| 85 | return; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub check_pod { | ||
| 89 | my $self = shift; | ||
| 90 | my $editor = $self->current->editor or return; | ||
| 91 | |||
| 92 | require Pod::Checker; | ||
| 93 | require IO::String; | ||
| 94 | |||
| 95 | my $checker = Pod::Checker->new; | ||
| 96 | my $output = ''; | ||
| 97 | my $out = IO::String->new($output); | ||
| 98 | $checker->parse_from_file( IO::String->new( $editor->GetText ), $out ); | ||
| 99 | |||
| 100 | my $num_errors = $checker->num_errors; | ||
| 101 | my $num_warnings = $checker->num_warnings; | ||
| 102 | my $results; | ||
| 103 | if ( $num_errors == -1 ) { | ||
| 104 | $results = Wx::gettext('No POD in current document'); | ||
| 105 | } elsif ( $num_errors == 0 and $num_warnings == 0 ) { | ||
| 106 | $results = Wx::gettext('POD check OK'); | ||
| 107 | } else { | ||
| 108 | $results = sprintf( | ||
| 109 | Wx::gettext("Found %s errors and %s warnings"), | ||
| 110 | $num_errors, $num_warnings | ||
| 111 | ); | ||
| 112 | for ( split /^/, $output ) { | ||
| 113 | if (/^(.+?) at line (\d+) in file \S+$/) { | ||
| 114 | my ( $message, $line ) = ( $1, $2 ); | ||
| 115 | $results .= "\nAt line $line, $message"; | ||
| 116 | } | ||
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 120 | my $main = $self->main; | ||
| 121 | $main->output->SetValue($results); | ||
| 122 | $main->output->SetSelection( 0, 0 ); | ||
| 123 | $main->show_output(1); | ||
| 124 | |||
| 125 | return; | ||
| 126 | } | ||
| 127 | |||
| 128 | # Called when the plugin is enabled by Padre | ||
| 129 | sub plugin_enable { | ||
| 130 | my $self = shift; | ||
| 131 | |||
| 132 | # Read the plugin configuration, and | ||
| 133 | my $config = $self->config_read; | ||
| 134 | unless ( defined $config ) { | ||
| 135 | |||
| 136 | # No configuration, let us create it | ||
| 137 | $config = {}; | ||
| 138 | } | ||
| 139 | |||
| 140 | #TODO some configuration defaults | ||
| 141 | |||
| 142 | # Write the plugin's configuration | ||
| 143 | $self->config_write($config); | ||
| 144 | |||
| 145 | # Update configuration attribute | ||
| 146 | $self->{config} = $config; | ||
| 147 | |||
| 148 | return 1; | ||
| 149 | } | ||
| 150 | |||
| 151 | # Called when the plugin is disabled by Padre | ||
| 152 | sub plugin_disable { | ||
| 153 | my $self = shift; | ||
| 154 | |||
| 155 | # TODO: Switch to Padre::Unload once Padre 0.96 is released | ||
| 156 | for my $package (CHILDREN) { | ||
| 157 | require Padre::Unload; | ||
| 158 | Padre::Unload->unload($package); | ||
| 159 | } | ||
| 160 | |||
| 161 | return; | ||
| 162 | } | ||
| 163 | |||
| 164 | 1; | ||
| 165 | |||
| 166 | __END__ |