| 1 | package DBIx::dbMan::Extension::MacroWorks; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use base 'DBIx::dbMan::Extension'; | ||
| 5 | use Text::FormatTable; | ||
| 6 | use File::Spec; | ||
| 7 | |||
| 8 | our $VERSION = '0.05'; | ||
| 9 | |||
| 10 | 1; | ||
| 11 | |||
| 12 | sub IDENTIFICATION { return "000001-000086-000005"; } | ||
| 13 | |||
| 14 | sub preference { return 0; } | ||
| 15 | |||
| 16 | sub known_actions { return [ qw/MACRO/ ]; } | ||
| 17 | |||
| 18 | sub menu { | ||
| 19 | my $obj = shift; | ||
| 20 | |||
| 21 | return ( { label => 'Input', submenu => [ | ||
| 22 | { label => 'Macros', submenu => [ | ||
| 23 | { label => 'Show', | ||
| 24 | action => { action => 'MACRO', operation => 'show' } }, | ||
| 25 | { label => 'Clear', | ||
| 26 | action => { action => 'MACRO', operation => 'clear' } }, | ||
| 27 | { label => 'Reload', | ||
| 28 | action => { action => 'KEYS', operation => 'reload' } } | ||
| 29 | ] } ] } ); | ||
| 30 | } | ||
| 31 | |||
| 32 | sub macrofile { | ||
| 33 | my $obj = shift; | ||
| 34 | |||
| 35 | return $ENV{DBMAN_MACROFILE} if $ENV{DBMAN_MACROFILE}; | ||
| 36 | return $obj->{-config}->macro_file if $obj->{-config}->macro_file; | ||
| 37 | return $ENV{HOME}.'/.dbman/macros'; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub load_macros { | ||
| 41 | my $obj = shift; | ||
| 42 | |||
| 43 | my @macros = (); | ||
| 44 | |||
| 45 | my @files = (); | ||
| 46 | |||
| 47 | if ( -d $obj->macrofile ) { | ||
| 48 | if (opendir D, $obj->macrofile) { | ||
| 49 | while (my $file = readdir D) { | ||
| 50 | next if $file=~ /^\./; | ||
| 51 | push @files, File::Spec->catfile( $obj->macrofile, $file ); | ||
| 52 | } | ||
| 53 | closedir D; | ||
| 54 | } | ||
| 55 | } else { | ||
| 56 | push @files, $obj->macrofile; | ||
| 57 | } | ||
| 58 | |||
| 59 | for my $file (@files) { | ||
| 60 | if (open F, $file) { | ||
| 61 | if ( $obj->{ -interface }->is_utf8 ) { | ||
| 62 | binmode F, ':utf8'; | ||
| 63 | } | ||
| 64 | while (<F>) { | ||
| 65 | chomp; | ||
| 66 | push @macros,$_ if m#^s/(.+)/(.*)/[gei]?#; | ||
| 67 | } | ||
| 68 | close F; | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | $obj->{-mempool}->set(macros => \@macros); | ||
| 73 | } | ||
| 74 | |||
| 75 | sub clear_macros { | ||
| 76 | my $obj = shift; | ||
| 77 | |||
| 78 | $obj->{-mempool}->set(macros => []); | ||
| 79 | } | ||
| 80 | |||
| 81 | sub init { | ||
| 82 | my $obj = shift; | ||
| 83 | |||
| 84 | $obj->load_macros; | ||
| 85 | } | ||
| 86 | |||
| 87 | sub done { | ||
| 88 | my $obj = shift; | ||
| 89 | |||
| 90 | $obj->clear_macros; | ||
| 91 | } | ||
| 92 | |||
| 93 | sub handle_action { | ||
| 94 | my ($obj,%action) = @_; | ||
| 95 | |||
| 96 | if ($action{action} eq 'MACRO') { | ||
| 97 | if ($action{operation} eq 'clear') { | ||
| 98 | $obj->clear_macros; | ||
| 99 | my $add = ''; | ||
| 100 | $add = 'permanently ' if ($action{permanent} and unlink $obj->macrofile); | ||
| 101 | $action{action} = 'OUTPUT'; | ||
| 102 | $action{output} = "Macro definitions $add"."cleared.\n"; | ||
| 103 | } elsif ($action{operation} eq 'reload') { | ||
| 104 | $obj->load_macros; | ||
| 105 | my $count = @{$obj->{-mempool}->get('macros')}; | ||
| 106 | $action{action} = 'OUTPUT'; | ||
| 107 | $action{output} = "Macro definitions reloaded ($count substitution".($count == 1?'':'s').").\n"; | ||
| 108 | } elsif ($action{operation} eq 'show') { | ||
| 109 | my @list = @{$obj->{-mempool}->get('macros')}; | ||
| 110 | $action{action} = 'OUTPUT'; | ||
| 111 | unless (@list) { | ||
| 112 | $action{output} = "No macro definition yet.\n"; | ||
| 113 | } else { | ||
| 114 | my $table = new Text::FormatTable '| l | l | l |'; | ||
| 115 | $table->rule; | ||
| 116 | $table->head('MACRO','SUBSTITUTION','FLAGS'); | ||
| 117 | $table->rule; | ||
| 118 | for my $macro (sort @list) { | ||
| 119 | $macro =~ s/^s\///; | ||
| 120 | my $flags = ''; | ||
| 121 | $flags = $1 if $macro =~ s#/([ige])?$##; | ||
| 122 | my $name = ''; | ||
| 123 | $name = $1 if $macro =~ s#^(.+?)\$?(?!\\)/##; | ||
| 124 | $name =~ s/^\^//; | ||
| 125 | |||
| 126 | $table->row($name,$macro,$flags); | ||
| 127 | } | ||
| 128 | $table->rule; | ||
| 129 | $action{output} = $table->render($obj->{-interface}->render_size); | ||
| 130 | } | ||
| 131 | } elsif ($action{operation} eq 'define') { | ||
| 132 | my $def = $action{macro}; | ||
| 133 | $action{action} = 'OUTPUT'; | ||
| 134 | if ($def !~ m#^s/(.+)/(.*)/[gei]?#) { | ||
| 135 | $action{output} = "Invalid substitution definition.\n"; | ||
| 136 | } else { | ||
| 137 | my @macros = @{$obj->{-mempool}->get('macros')}; | ||
| 138 | push @macros,$def; | ||
| 139 | $obj->{-mempool}->set(macros => \@macros); | ||
| 140 | my $addenum = ''; | ||
| 141 | if (open F,">>".$obj->macrofile) { | ||
| 142 | if ( $obj->{ -interface }->is_utf8 ) { | ||
| 143 | binmode F, ':utf8'; | ||
| 144 | } | ||
| 145 | print F "$def\n"; | ||
| 146 | close F; | ||
| 147 | } else { | ||
| 148 | $addenum = ", but not permanently"; | ||
| 149 | } | ||
| 150 | $action{output} = "New macro substitution defined$addenum.\n"; | ||
| 151 | } | ||
| 152 | } elsif ($action{operation} eq 'undefine') { | ||
| 153 | my $def = $action{macro}; | ||
| 154 | my @macros = @{$obj->{-mempool}->get('macros')}; | ||
| 155 | my $i = 0; | ||
| 156 | my @clearlist = (); | ||
| 157 | for (@macros) { | ||
| 158 | my $name = ''; | ||
| 159 | s#/([ige])?$##; | ||
| 160 | $name = $1 if m#^s/\^?(.+?)\$?(?!\\)/#; | ||
| 161 | push @clearlist,$i if ($name and $name eq $def); | ||
| 162 | ++$i; | ||
| 163 | } | ||
| 164 | $action{action} = 'OUTPUT'; | ||
| 165 | if (@clearlist) { | ||
| 166 | delete $macros[$_] for reverse sort @clearlist; | ||
| 167 | $obj->{-mempool}->set(macros => \@macros); | ||
| 168 | |||
| 169 | if (open F,$obj->macrofile) { | ||
| 170 | if ( $obj->{ -interface }->is_utf8 ) { | ||
| 171 | binmode F, ':utf8'; | ||
| 172 | } | ||
| 173 | my @all = <F>; | ||
| 174 | close F; | ||
| 175 | if (open F,">".$obj->macrofile) { | ||
| 176 | if ( $obj->{ -interface }->is_utf8 ) { | ||
| 177 | binmode F, ':utf8'; | ||
| 178 | } | ||
| 179 | for my $line (@all) { | ||
| 180 | chomp $line; | ||
| 181 | my $test = $line; | ||
| 182 | my $name = ''; | ||
| 183 | $test =~ s#/([ige])?$##; | ||
| 184 | $name = $1 if $test =~ m#^s/(.+)(?!\\)/#; | ||
| 185 | print F "$line\n" if ($name and $name ne $def); | ||
| 186 | } | ||
| 187 | close F; | ||
| 188 | } | ||
| 189 | } | ||
| 190 | |||
| 191 | $action{output} = "Macro named $def undefined.\n"; | ||
| 192 | } else { | ||
| 193 | $action{output} = "No macro named $def found.\n"; | ||
| 194 | } | ||
| 195 | } | ||
| 196 | } | ||
| 197 | |||
| 198 | $action{processed} = 1; | ||
| 199 | return %action; | ||
| 200 | } | ||
| 201 | |||
| 202 | sub cmdcomplete { | ||
| 203 | my ($obj,$text,$line,$start) = @_; | ||
| 204 | |||
| 205 | my @macros = @{$obj->{-mempool}->get('macros')}; | ||
| 206 | return () unless @macros; | ||
| 207 | |||
| 208 | my @names = (); | ||
| 209 | for (@macros) { | ||
| 210 | s#^s/##; | ||
| 211 | s#/([ige])?$##; | ||
| 212 | s#^(.+)(?!\\)/.*$#$1#; | ||
| 213 | push @names,$_ if $_; | ||
| 214 | } | ||
| 215 | |||
| 216 | my @result = (); | ||
| 217 | for my $name (@names) { | ||
| 218 | $name =~ s/\\s[+*]?/ /g; | ||
| 219 | $name =~ s/^\^//; | ||
| 220 | $name =~ s/\$$//; | ||
| 221 | my @words = (); | ||
| 222 | for (split /\s+/,$name) { | ||
| 223 | if (/^[-a-z0-9_\\]+$/i) { | ||
| 224 | push @words,$_; | ||
| 225 | } else { | ||
| 226 | last; | ||
| 227 | } | ||
| 228 | } | ||
| 229 | if (@words) { | ||
| 230 | if ($line =~ /^\s*[A-Z]*$/i) { | ||
| 231 | push @result,$words[0]; | ||
| 232 | } else { | ||
| 233 | my $saved = pop @words; | ||
| 234 | while (@words) { | ||
| 235 | $name = '$line =~ /^\s*'.join('\\s+',@words).'\s+\S*$/i'; | ||
| 236 | push @result,$saved if eval $name; | ||
| 237 | $saved = pop @words; | ||
| 238 | } | ||
| 239 | } | ||
| 240 | } | ||
| 241 | } | ||
| 242 | |||
| 243 | return @result; | ||
| 244 | } |