| 1 | package DBIx::dbMan::Extension::CmdMacros; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use base 'DBIx::dbMan::Extension'; | ||
| 5 | |||
| 6 | our $VERSION = '0.03'; | ||
| 7 | |||
| 8 | 1; | ||
| 9 | |||
| 10 | sub IDENTIFICATION { return "000001-000085-000003"; } | ||
| 11 | |||
| 12 | sub preference { return 2000; } | ||
| 13 | |||
| 14 | sub known_actions { return [ qw/COMMAND/ ]; } | ||
| 15 | |||
| 16 | sub handle_action { | ||
| 17 | my ($obj,%action) = @_; | ||
| 18 | |||
| 19 | if ($action{action} eq 'COMMAND') { | ||
| 20 | if ($action{cmd} =~ /^show\s+macros?$/i) { | ||
| 21 | $action{action} = 'MACRO'; | ||
| 22 | $action{operation} = 'show'; | ||
| 23 | } elsif ($action{cmd} =~ /^(?:clear|erase)\s+macros?(\s+permanent(?:ly)?)?$/i) { | ||
| 24 | $action{action} = 'MACRO'; | ||
| 25 | $action{operation} = 'clear'; | ||
| 26 | $action{permanent} = $1?1:0; | ||
| 27 | } elsif ($action{cmd} =~ /^(re)?load\s+macros?$/i) { | ||
| 28 | $action{action} = 'MACRO'; | ||
| 29 | $action{operation} = 'reload'; | ||
| 30 | } elsif ($action{cmd} =~ /^def(?:ine)?(?:\s+macro)?\s+(.+)$/i) { | ||
| 31 | $action{action} = 'MACRO'; | ||
| 32 | $action{operation} = 'define'; | ||
| 33 | $action{macro} = $1; | ||
| 34 | } elsif ($action{cmd} =~ /^undef(?:ine)?(?:\s+macro)?\s+(.+)$/i) { | ||
| 35 | $action{action} = 'MACRO'; | ||
| 36 | $action{operation} = 'undefine'; | ||
| 37 | $action{macro} = $1; | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | $action{processed} = 1; | ||
| 42 | return %action; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub cmdhelp { | ||
| 46 | return [ | ||
| 47 | 'SHOW MACROS' => 'Show macros (substitutions)', | ||
| 48 | 'CLEAR MACROS [PERMAMENT]' => 'Clear macros (substitutions) - permanent or temporary', | ||
| 49 | 'RELOAD MACROS' => 'Reload macros (substitutions) from file', | ||
| 50 | 'DEFINE MACRO s/macro/substition/[ige]' => 'Define macro (substitution) as subscribed', | ||
| 51 | 'UNDEFINE MACRO <macro>' => 'Undefine macro (substitution) with <macro> in first part of substitution' | ||
| 52 | ]; | ||
| 53 | } | ||
| 54 | |||
| 55 | sub cmdcomplete { | ||
| 56 | my ($obj,$text,$line,$start) = @_; | ||
| 57 | return qw/PERMANENT/ if $line =~ /^\s*(CLEAR|ERASE)\s+MACROS\s+\S*$/i; | ||
| 58 | return qw/MACROS/ if $line =~ /^\s*(SHOW|CLEAR|ERASE|RELOAD)\s+\S*$/i; | ||
| 59 | return qw/MACRO/ if $line =~ /^\s*(UN)?DEF(INE)?\s+\S*$/i; | ||
| 60 | return qw/SHOW CLEAR RELOAD DEFINE UNDEFINE/ if $line =~ /^\s*[A-Z]*$/i; | ||
| 61 | |||
| 62 | if ($line =~ /^\s*(UN)?DEF(INE)?(\s+MACRO)?\s+.*$/i) { | ||
| 63 | my @macros = @{$obj->{-mempool}->get('macros')}; | ||
| 64 | return () unless @macros; | ||
| 65 | |||
| 66 | my @names = (); | ||
| 67 | for (@macros) { | ||
| 68 | s#^s/##; | ||
| 69 | s#/([ige])?$##; | ||
| 70 | s#^(.+)(?!\\)/.*$#$1#; | ||
| 71 | push @names,$_ if $_; | ||
| 72 | } | ||
| 73 | |||
| 74 | my @result = (); | ||
| 75 | for my $name (@names) { | ||
| 76 | $name =~ s/\\s[+*]?/ /g; | ||
| 77 | $name =~ s/^\^//; | ||
| 78 | $name =~ s/\$$//; | ||
| 79 | my @words = (); | ||
| 80 | for (split /\s+/,$name) { | ||
| 81 | if (/^[-a-z0-9_\\]+$/i) { | ||
| 82 | push @words,$_; | ||
| 83 | } else { | ||
| 84 | last; | ||
| 85 | } | ||
| 86 | } | ||
| 87 | if (@words) { | ||
| 88 | if ($line =~ /^\s*(UN)?DEF(INE)?(\s+MACRO)?\s+\S*$/i) { | ||
| 89 | push @result,$words[0]; | ||
| 90 | } else { | ||
| 91 | my $saved = pop @words; | ||
| 92 | while (@words) { | ||
| 93 | $name = '$line =~ /^\s*(UN)?DEF(INE)?(\s+MACRO)?\s+'.join('\\s+',@words).'\s+\S*$/i'; | ||
| 94 | push @result,$saved if eval $name; | ||
| 95 | $saved = pop @words; | ||
| 96 | } | ||
| 97 | } | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | return @result; | ||
| 102 | } | ||
| 103 | |||
| 104 | return (); | ||
| 105 | } |