]> git.lyx.org Git - lyx.git/blobdiff - lib/reLyX/BasicLyX.pm
cruft removal
[lyx.git] / lib / reLyX / BasicLyX.pm
index 728a0bc1424a47745bfce043233b625d7489200f..d704c34db07ddf31df12b3a4e62b7cc7674015a6 100644 (file)
@@ -158,12 +158,14 @@ my %TextTokenTransTable = (
     '\LaTeXe' => "LaTeX2e",
     '\TeX'    => "TeX",
     '\LyX'    => "LyX",
+    '\lyxarrow' => "\\SpecialChar \\menuseparator\n",
     '\hfill'  => "\n\\hfill \n",
     '\noindent'        => "\n\\noindent \n",
     '\textbackslash'   => "\n\\backslash \n",
     '\textgreater'     => ">",
     '\textless'        => "<",
     '\textbar'         => "|",
+    '\textasciitilde'  => "~",
 );
 
 # LyX translations of some plain LaTeX text (TeX parser won't recognize it
@@ -212,10 +214,36 @@ my %ExtraArgEnvironments = (
 );
 
 # Math environments are copied verbatim
-my $MathEnvironments = "(math|equation|displaymath|eqnarray(\\*)?)";
+my $MathEnvironments = "(math|displaymath|xxalignat|(equation|eqnarray|align|alignat|xalignat|multline|gather)(\\*)?)";
 # ListLayouts may have standard paragraphs nested inside them.
 my $ListLayouts = "Itemize|Enumerate|Description";
 
+# passed a string and an array
+# returns true if the string is an element of the array.
+sub foundIn {
+    my $name = shift;
+    return grep {$_ eq $name} @_;
+}
+
+my @NatbibCommands = map {"\\$_"} qw(citet citealt citep citealp citeauthor);
+
+# passed a string.
+# returns true if it is a valid natbib citation
+sub isNatbibCitation {
+    my $name = shift;
+
+    # These two have a single form
+    return 1 if ($name eq '\citeyear' or $name eq '\citeyearpar');
+
+    # Natbib citations can start with a 'C' or a 'c'
+    $name =~ s/^\\C/\\c/;
+    # The can end with a '*'
+    $name =~ s/\*$//;
+    # Is this doctored string found in the list of valid commands?
+    return foundIn($name, @NatbibCommands);
+    
+}
+
 #####################   PARSER INVOCATION   ##################################
 sub call_parser {
 # This subroutine calls the TeX parser & translator
@@ -288,6 +316,42 @@ sub call_parser {
     return;
 } # end subroutine call_parser
 
+# This is used as a toggle so that we know what to do when basic_lyx is
+# passed a '$' or '$$' token.
+my $inside_math=0;
+
+sub starting_math {
+    my $name = shift;
+
+    if ($name eq '\(' || $name eq '\[' ||
+       # These tokens bound both ends of a math environment so we must check
+       # $inside_math to know what action to take.
+       ($name eq '$' || $name eq '$$') && !$inside_math) {
+
+       $inside_math = 1;
+       return 1;
+    }
+
+    # All other tokens
+    return 0;
+}
+
+sub ending_math {
+    my $name = shift;
+
+    if ($name eq '\)' || $name eq '\]' ||
+       # These tokens bound both ends of a math environment so we must check
+       # $inside_math to know what action to take.
+       ($name eq '$' || $name eq '$$') && $inside_math) {
+
+       $inside_math = 0;
+       return 1;
+    }
+
+    # All other tokens
+    return 0;
+}
+
 ##########################   MAIN TRANSLATOR SUBROUTINE   #####################
 sub basic_lyx {
 # This subroutine is called by Text::TeX::process each time subroutine
@@ -379,14 +443,15 @@ sub basic_lyx {
                } # end special handling for \@
 
            # Handle tokens that LyX translates as a "LatexCommand" inset
-           } elsif (grep {$_ eq $name} @LatexCommands) {
+           } elsif (foundIn($name, @LatexCommands) ||
+                    isNatbibCitation($name)){
                &CheckForNewParagraph; #Start new paragraph if necessary
                print OUTFILE "$pre_space\n\\begin_inset LatexCommand ",
                               $name,
                              "\n\n\\end_inset \n\n";
 
            # Math -- copy verbatim until you're done
-           } elsif ($name eq '\(' || $name eq '\[') {
+           } elsif (starting_math($name)) {
                print "\nCopying math beginning with '$name'\n" if $debug_on;
                # copy everything until end text
                $dummy = &Verbatim::copy_verbatim($fileobject, $eaten);
@@ -397,7 +462,7 @@ sub basic_lyx {
                print $dummy if $debug_on;
                print OUTFILE $dummy;
 
-           } elsif ($name eq '\)' || $name eq '\]') {
+           } elsif (ending_math($name)) {
                # end math
                print OUTFILE "$name\n\\end_inset \n\n";
                print "\nDone copying math ending with '$name'" if $debug_on;
@@ -468,7 +533,7 @@ sub basic_lyx {
                    $thistable->nextcol;
                } else {warn "& is illegal outside a table!"}
 
-           } elsif ($name eq '\\\\' || $name eq '\\newline') {
+           } elsif ($name eq '\\\\' || $name eq '\\newline' || $name eq "\\tabularnewline") {
                &CheckForNewParagraph; # could be at beginning of par?
                 print OUTFILE "\n\\newline \n";
 
@@ -535,7 +600,7 @@ sub basic_lyx {
            print "$name" if $debug_on;
 
            # Handle things that LyX translates as a "LatexCommand" inset
-           if (grep {$_ eq $name} @LatexCommands) {
+           if (foundIn($name, @LatexCommands) || isNatbibCitation($name)){
                &CheckForNewParagraph; #Start new paragraph if necessary
 
                print OUTFILE "$pre_space\n\\begin_inset LatexCommand ";
@@ -576,7 +641,7 @@ sub basic_lyx {
            # This is to handle cases where _ is used, say, in a filename.
            # When _ is used in math mode, it'll be copied by the math mode
            # copying subs. Here we handle cases where it's used in non-math.
-           # Examples are filenames for & citation labels.
+           # Examples are filenames for \include & citation labels.
            # (It's illegal to use it in regular LaTeX text.)
            if ($name eq "_") {
               print OUTFILE $eaten->exact_print;
@@ -770,7 +835,8 @@ sub basic_lyx {
 
            # Handle things that LyX translates as a "LatexCommand" inset
            # or "Include" insets
-           if (grep {$_ eq $name} @LatexCommands, @IncludeCommands) {
+           if (foundIn($name, @LatexCommands, @IncludeCommands) ||
+               isNatbibCitation($name)){
                print OUTFILE "\}\n\n\\end_inset \n\n";
 
            } elsif (exists $ReadCommands::ToLayout->{$name}) {
@@ -909,13 +975,17 @@ sub basic_lyx {
 
            # Environments lyx translates to floats
            } elsif (exists $FloatEnvTransTable{$env}) {
+               # this really only matters if it's at the very
+               # beginning of the doc.
+               &CheckForNewParagraph;
+
                $tok = $fileobject->eatOptionalArgument;
                if ($tok && defined ($dummy = $tok->print) && $dummy) {
                    print "\nIgnoring float placement '$dummy'" if $debug_on;
                }
                my $command = $FloatEnvTransTable{$env};
 
-               # Open the footnote
+               # Open the table/figure
                print OUTFILE "$command";
 
            # table