| 1 | |||
| 2 | package Text::Editor::Vip::Actions; | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings ; | ||
| 6 | |||
| 7 | use File::Slurp ; | ||
| 8 | use Data::TreeDumper ; | ||
| 9 | |||
| 10 | use Text::Editor::Vip::Buffer ; | ||
| 11 | |||
| 12 | BEGIN | ||
| 13 | { | ||
| 14 | use Exporter (); | ||
| 15 | |||
| 16 | use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); | ||
| 17 | $VERSION = 0.01; | ||
| 18 | @ISA = qw (Exporter); | ||
| 19 | @EXPORT = qw (DoAction LoadActions); | ||
| 20 | @EXPORT_OK = qw (); | ||
| 21 | %EXPORT_TAGS = (); | ||
| 22 | } | ||
| 23 | |||
| 24 | #------------------------------------------------------------------------------- | ||
| 25 | |||
| 26 | =head1 NAME | ||
| 27 | |||
| 28 | Text::Editor::Vip::Actions::Actions - loads actions from files | ||
| 29 | |||
| 30 | =head1 SYNOPSIS | ||
| 31 | |||
| 32 | =head1 DESCRIPTION | ||
| 33 | |||
| 34 | =head1 MEMBER FUNCTIONS | ||
| 35 | |||
| 36 | =cut | ||
| 37 | |||
| 38 | #------------------------------------------------------------------------------------------------------ | ||
| 39 | |||
| 40 | sub new | ||
| 41 | { | ||
| 42 | my ($class, $key_lookup, @file_names) = @_ ; | ||
| 43 | |||
| 44 | my $actions = bless({}, $class) ; | ||
| 45 | |||
| 46 | $actions->Load($key_lookup, @file_names) ; | ||
| 47 | |||
| 48 | return($actions) ; | ||
| 49 | } | ||
| 50 | |||
| 51 | #------------------------------------------------------------------------------------------------------ | ||
| 52 | |||
| 53 | sub Run | ||
| 54 | { | ||
| 55 | my ($actions, $action_name, @args) = @_ ; | ||
| 56 | |||
| 57 | if(exists $actions->{PREACTION}) | ||
| 58 | { | ||
| 59 | print "PREACTION: $actions->{PREACTION}{COMMENT}.\n" ; | ||
| 60 | ($action_name, @args) = $actions->{PREACTION}{SUB}($actions, $action_name,@args) ; | ||
| 61 | } | ||
| 62 | |||
| 63 | if(exists $actions->{$action_name}) | ||
| 64 | { | ||
| 65 | print "Action: $actions->{$action_name}{COMMENT}.\n" ; | ||
| 66 | $actions->{$action_name}{SUB}->($actions, @args) ; | ||
| 67 | } | ||
| 68 | else | ||
| 69 | { | ||
| 70 | print "No Action for '$action_name'.\n" ; | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | #------------------------------------------------------------------------------------------------------ | ||
| 75 | |||
| 76 | sub Load | ||
| 77 | { | ||
| 78 | my ($actions, $key_lookup, $view, @file_names) = @_; | ||
| 79 | |||
| 80 | my $exceptions ; | ||
| 81 | |||
| 82 | for my $file (@file_names) | ||
| 83 | { | ||
| 84 | eval | ||
| 85 | { | ||
| 86 | $actions->DoActionFile($key_lookup, $view, $file) ; | ||
| 87 | } ; | ||
| 88 | |||
| 89 | $exceptions .= "$@ \n" if $@ ; | ||
| 90 | } | ||
| 91 | |||
| 92 | die $exceptions if defined $exceptions ; | ||
| 93 | } | ||
| 94 | |||
| 95 | #------------------------------------------------------------------------------------------------------ | ||
| 96 | |||
| 97 | { # private block | ||
| 98 | |||
| 99 | my $load = 0 ; | ||
| 100 | sub DoActionFile | ||
| 101 | { | ||
| 102 | #~ print DumpTree \@_, "DoActionFile args:", MAX_DEPTH => 2 ; | ||
| 103 | |||
| 104 | my ($actions, $key_lookup, $view, $file) = @_ ; | ||
| 105 | |||
| 106 | $load++ ; | ||
| 107 | |||
| 108 | my $pre_code = <<EOC ; | ||
| 109 | |||
| 110 | use strict ; | ||
| 111 | use warnings ; | ||
| 112 | |||
| 113 | EOC | ||
| 114 | |||
| 115 | my $package = "View::LoadActions::${file}_$load" ; | ||
| 116 | $package =~ s/[^a-zA-Z:0-9]/_/g ; | ||
| 117 | |||
| 118 | LoadFileInPackage | ||
| 119 | ( | ||
| 120 | $file | ||
| 121 | , $package | ||
| 122 | , $pre_code | ||
| 123 | ) ; | ||
| 124 | |||
| 125 | my $init_function ; | ||
| 126 | eval "\$init_function= *${package}::Init{CODE} ;" ; | ||
| 127 | |||
| 128 | if($init_function) | ||
| 129 | { | ||
| 130 | $actions->{CURRENT_KEY_LOOKUP} = $key_lookup ; | ||
| 131 | $init_function->($view) ; | ||
| 132 | delete $actions->{CURRENT_KEY_LOOKUP} ; | ||
| 133 | } | ||
| 134 | else | ||
| 135 | { | ||
| 136 | die "No 'Init' function to call in actions file '$file'.\n" ; | ||
| 137 | } | ||
| 138 | } | ||
| 139 | |||
| 140 | #------------------------------------------------------------------------------------------------------ | ||
| 141 | |||
| 142 | sub RegisterActions | ||
| 143 | { | ||
| 144 | my (undef, $filename, $line) = caller() ; | ||
| 145 | |||
| 146 | my ($actions, @actions_to_register) = @_ ; | ||
| 147 | |||
| 148 | for my $action (@actions_to_register) | ||
| 149 | { | ||
| 150 | my ($comment, $key, $modifier, $sub, $modifier2) = @$action ; | ||
| 151 | |||
| 152 | if(defined $actions->{CURRENT_KEY_LOOKUP} && exists $actions->{CURRENT_KEY_LOOKUP}{$key}) | ||
| 153 | { | ||
| 154 | $key = $actions->{CURRENT_KEY_LOOKUP}{$key} ; | ||
| 155 | } | ||
| 156 | |||
| 157 | #~ print "RegisterActions '$comment' $key\n" ; | ||
| 158 | |||
| 159 | $comment = defined $comment ? $comment : "$key-$modifier" ; | ||
| 160 | |||
| 161 | my $new_action ; | ||
| 162 | |||
| 163 | if('CODE' eq ref $sub) | ||
| 164 | { | ||
| 165 | $new_action = {COMMENT => "'$comment' at $filename:$line" , SUB => $sub} ; | ||
| 166 | } | ||
| 167 | elsif('' eq ref $sub && '' eq ref $modifier2) | ||
| 168 | { | ||
| 169 | my $alias_to = "$sub-$modifier2" ; | ||
| 170 | |||
| 171 | if(exists $actions->{$alias_to}) | ||
| 172 | { | ||
| 173 | $new_action = {ALIAS => $alias_to, COMMENT => "'$comment' at $filename:$line" , SUB => $actions->{$alias_to}{SUB}} ; | ||
| 174 | } | ||
| 175 | else | ||
| 176 | { | ||
| 177 | $new_action = {ALIAS => $alias_to, COMMENT => "'$comment' at $filename:$line" , SUB => sub{print "!! Print aliased '$comment' to unexisting '$alias_to'\n"}} ; | ||
| 178 | } | ||
| 179 | } | ||
| 180 | else | ||
| 181 | { | ||
| 182 | die "Invalid RegisterActions '$comment' $key @ $filename:$line\n" ; | ||
| 183 | } | ||
| 184 | |||
| 185 | if(exists $actions->{"$key-$modifier"}) | ||
| 186 | { | ||
| 187 | my $old_definition = $actions->{"$key-$modifier"}{COMMENT} ; | ||
| 188 | my $new_definition = $new_action->{COMMENT} ; | ||
| 189 | |||
| 190 | print "Overriding '$key-$modifier' defined as: $old_definition with new definition: $new_definition\n" ; | ||
| 191 | } | ||
| 192 | |||
| 193 | $actions->{"$key-$modifier"} = $new_action ; | ||
| 194 | } | ||
| 195 | } | ||
| 196 | |||
| 197 | } # private block | ||
| 198 | |||
| 199 | #------------------------------------------------------------------------------------------------------ | ||
| 200 | |||
| 201 | sub LoadFileInPackage | ||
| 202 | { | ||
| 203 | my ($file, $package, $pre_code) = @_ ; | ||
| 204 | |||
| 205 | my $file_body = read_file($file) or die "LoadFileInPackage: Error opening $file: $!\n" ; | ||
| 206 | |||
| 207 | my $source = <<EOS ; | ||
| 208 | #>>>>> start of file '$file' | ||
| 209 | |||
| 210 | #line 0 '$file' | ||
| 211 | package $package ; | ||
| 212 | $pre_code | ||
| 213 | |||
| 214 | #line 1 '$file' | ||
| 215 | $file_body | ||
| 216 | #<<<<< end of file '$file' | ||
| 217 | |||
| 218 | EOS | ||
| 219 | |||
| 220 | my $result = eval $source ; | ||
| 221 | |||
| 222 | #~confess "$@ ." if $@ ; | ||
| 223 | #~ PrintError $@ if $@ ; | ||
| 224 | |||
| 225 | die "" if $@ ; | ||
| 226 | |||
| 227 | unless(defined $result && $result == 1) | ||
| 228 | { | ||
| 229 | $result ||= 'undef' ; | ||
| 230 | die "$file didn't return OK [$result] (did you forget '1 ;' at the last line?)\n" ; | ||
| 231 | } | ||
| 232 | } | ||
| 233 | |||
| 234 | #------------------------------------------------------------------------------------------------------ | ||
| 235 | |||
| 236 | =head1 BUGS | ||
| 237 | |||
| 238 | =head1 AUTHOR | ||
| 239 | |||
| 240 | Khemir Nadim ibn Hamouda | ||
| 241 | CPAN ID: NKH | ||
| 242 | mailto:nadim@khemir.net | ||
| 243 | http:// no web site | ||
| 244 | |||
| 245 | =head1 COPYRIGHT | ||
| 246 | |||
| 247 | This program is free software; you can redistribute | ||
| 248 | it and/or modify it under the same terms as Perl itself. | ||
| 249 | |||
| 250 | The full text of the license can be found in the | ||
| 251 | LICENSE file included with this module. | ||
| 252 | |||
| 253 | =cut | ||
| 254 | |||
| 255 | #------------------------------------------------------------------------------------------------------ | ||
| 256 | 1 ; |