#! /usr/bin/perl -CSD
use strict;

my ($Prog, $Version) = ("w0-l-latex", "2.0.3");
my $LaTeX_id = "\\\\([A-Za-z@]+|.)";
my ($Bcode, $Icode) = ("code", "code");
my ($Bcode, $Icode) = (0, 0);
PARAM:
while (@ARGV && $ARGV[0] =~ /^-/) { $_ = shift;
    /^-e$/ and do { $Bcode = "bcode";  $Icode = "icode";  
        next PARAM; };
    /^-v$/ and do {
        print STDERR "This is $Prog (version $Version)\n";
        next PARAM; };
}
while (<>) { 
    unless (/^code:(.*);$/) { print;  next; };
    chomp($_ = $1);
    while (/[%\\]/) {
    print "code:$`;\n" if $`;
    $_ = $&.$';
    /^%/ and do { print "$Icode:$_;\n";  $_ = "";  last; };
    s/^(\\(re)?newcommand\s*\*?\s*\{)(.+?)(\})// and do {
    print "code:$1;\n$Bcode:$3;\ncode:$4;\n";
    print "fdef:$3;\n";
    next; };
s/^(\\def\s*)($LaTeX_id)//o and do {
    print "code:$1;\n$Bcode:$2;\n";
    print "fdef:$2;\n";  next; };
s/^(\\(re)?newenvironment\s*\{)(.+?)(\})// and do {
    print "code:$1;\n$Bcode:$3;\ncode:$4;\n";
    print "vdef:$3;\n";  next; };
    s/^\\begin\s*\{(.+?)\}// and do {
    print "code:$&;\n";
    print "vuse:$1;\n";  next; };
s/^\\end\s*\{(.+?)\}// and do {
    print "code:$&;\n";   next; };
s/^$LaTeX_id//o and do {
    print "code:$&;\n";
    print "fuse:$&;\n";  next; };
print "code:\\;\n";  s/^.//;  next;
}
print "code:$_;\n" if $_;
}
exit 0;

sub Error {
    &Message(@_);  
    &Tidy_up if defined &Tidy_up;
    exit 1;
}
sub Message {
    print STDERR "$Prog: ", shift, "\n";
    while (@_) {
        print STDERR " "x(2+length $Prog), shift, "\n";
    }
}
