]> git.lyx.org Git - lyx.git/blobdiff - lib/reLyX/BasicLyX.pm
cruft removal
[lyx.git] / lib / reLyX / BasicLyX.pm
index 521e7ee056533200e73d4efabfe366eee8ee89c6..d704c34db07ddf31df12b3a4e62b7cc7674015a6 100644 (file)
@@ -214,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
@@ -290,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
@@ -381,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);
@@ -399,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;
@@ -470,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";
 
@@ -537,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 ";
@@ -772,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}) {