print "${BGreen}Loading sub.pl\n";    

%Subs = ();

# Do subs.
sub substitution {
    foreach $p (keys %Subs) {
        s/($p)/$Subs{$p}/g;
    }
}

# Add/Display subs.
sub cmd_sub {
  my $Original = "";
  my $Subbed = "";
  my $count = 0;

  if (/^\"(.*)\"\s+(.*)\n/) {
    $Original = $1; $Subbed = $2;
    if (length($Subbed)) {
      $Subs{$Original} = $Subbed;
      print "Substituting: \"${Original}\" with ${Subbed}\n" unless $Loading;
    } else {
      print "No text to substitute, use gag to gag text.\n";
    }
  } elsif (/^(\d+)/) {
    # Remove a certain highlight
    foreach (keys %Subs) {
      if (++$count == $1) {
	print "Deleted Sub: $_\n";
	delete $Subs{$_};
	return;
      }
    }
    print "No such sub (there are only $count!) - type sub to see a list.\n";
    
  } else {
    print "Following substutions are active:\n";
    foreach (keys %Subs) {
      printf "%2d) \"$_\" = $Subs{$_}\n", ++$count;
    }
    print "\nUse sub  \"<text>\" <replacement> to do subtitution.\n";
    print "Use sub <number> to remove.\n";
  }
}

sub save_subs {
    local (*FH) = $_[0];
    foreach (keys %Subs) {
        print FH "sub \"$_\" Subs{$_}";
    }
}

save_add(\&save_subs);
load_add("sub", \&cmd_sub);
output_add(\&substitution);


1;
