| 1 | # -*- coding: utf-8 -*- | ||
| 2 | # Copyright (C) 2011-2014 Rocky Bernstein <rocky@cpan.org> | ||
| 3 | |||
| 4 | use warnings; | ||
| 5 | use rlib '../../../../..'; | ||
| 6 | |||
| 7 | # For highight_string | ||
| 8 | use Devel::Trepan::DB::LineCache; | ||
| 9 | |||
| 10 | package Devel::Trepan::CmdProcessor::Command::Info::Macros; | ||
| 11 | use Devel::Trepan::CmdProcessor::Command::Subcmd::Core; | ||
| 12 | |||
| 13 | @ISA = qw(Devel::Trepan::CmdProcessor::Command::Subcmd); | ||
| 14 | # Values inherited from parent | ||
| 15 | use vars @Devel::Trepan::CmdProcessor::Command::Subcmd::SUBCMD_VARS; | ||
| 16 | |||
| 17 | unless (@ISA) { | ||
| 18 | eval <<"EOE"; | ||
| 19 | use constant MAX_ARGS => undef; # unlimited. | ||
| 20 | EOE | ||
| 21 | } | ||
| 22 | |||
| 23 | our $CMD = "info macros"; | ||
| 24 | =pod | ||
| 25 | |||
| 26 | =head2 Synopsis: | ||
| 27 | |||
| 28 | =cut | ||
| 29 | our $HELP = <<'HELP'; | ||
| 30 | =pod | ||
| 31 | |||
| 32 | info macros | ||
| 33 | |||
| 34 | B<info macros *> | ||
| 35 | B<info macros> I<macro1> [I<macro2> ..] | ||
| 36 | |||
| 37 | In the first form a list of the existing macro names are shown | ||
| 38 | in column format. | ||
| 39 | |||
| 40 | In the second form, all macro names and their definitions are shown. | ||
| 41 | |||
| 42 | In the last form the only definitions of the given macro names is shown. | ||
| 43 | show macro [I<name1> I<name2> ...] | ||
| 44 | |||
| 45 | If macros names are given, show their definition. If left blank, show | ||
| 46 | all macro names. | ||
| 47 | =cut | ||
| 48 | HELP | ||
| 49 | |||
| 50 | our $MIN_ABBREV = length('ma'); | ||
| 51 | our $SHORT_HELP = "Show defined macros"; | ||
| 52 | |||
| 53 | # sub complete($$) { | ||
| 54 | # { | ||
| 55 | # my ($self, $prefix) = @_; | ||
| 56 | # my @cmds = sort keys %{$proc->{macros}}; | ||
| 57 | # Trepan::Complete.complete_token(@cmds, $prefix); | ||
| 58 | # } | ||
| 59 | |||
| 60 | no warnings 'redefine'; | ||
| 61 | sub run($$) { | ||
| 62 | my ($self, $args) = @_; | ||
| 63 | my $proc = $self->{proc}; | ||
| 64 | my @args = @$args; | ||
| 65 | if (scalar(@args) > 2) { | ||
| 66 | my @macro_names; | ||
| 67 | if ((scalar(@args)) == 3 && '*' eq $args[2]) { | ||
| 68 | @macro_names = sort keys %{$proc->{macros}}; | ||
| 69 | if (scalar @macro_names == 0) { | ||
| 70 | $proc->msg("No macros defined."); | ||
| 71 | return; | ||
| 72 | } | ||
| 73 | } else { | ||
| 74 | @macro_names = @args[2..$#args]; | ||
| 75 | } | ||
| 76 | for my $macro_name (@macro_names) { | ||
| 77 | if (exists $proc->{macros}{$macro_name}) { | ||
| 78 | my $line = $proc->{macros}{$macro_name}->[1]; | ||
| 79 | if ($proc->{settings}{highlight}) { | ||
| 80 | $line = Devel::Trepan::DB::LineCache::highlight_string($line); | ||
| 81 | } | ||
| 82 | my $msg = sprintf("%s: %s", $macro_name, $line); | ||
| 83 | $proc->msg($msg); | ||
| 84 | } else { | ||
| 85 | $proc->errmsg("$macro_name is not a defined macro"); | ||
| 86 | } | ||
| 87 | } | ||
| 88 | } else { | ||
| 89 | my @macros = sort keys %{$proc->{macros}}; | ||
| 90 | if (scalar @macros == 0) { | ||
| 91 | $proc->msg("No macros defined."); | ||
| 92 | } else { | ||
| 93 | $proc->section("List of macro names currently defined:"); | ||
| 94 | my @cmds = sort @macros; | ||
| 95 | $proc->msg($self->{cmd}->columnize_commands(\@cmds)); | ||
| 96 | } | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | unless(caller) { | ||
| 101 | # Demo it. | ||
| 102 | # require_relative '../../mock'; | ||
| 103 | # my $cmd = MockDebugger::sub_setup(__PACKAGE__); | ||
| 104 | # my $cmd->run($cmd->{prefix} + %w(u foo)); | ||
| 105 | } | ||
| 106 | |||
| 107 | 1; |