]> git.lyx.org Git - lyx.git/blobdiff - development/autotests/lyxStatus.pm
Adding binary path for Homebrew on MacOS-arm64 (bug 12619).
[lyx.git] / development / autotests / lyxStatus.pm
index fd2e3829ff8d212873cf36d8714a822cee8fd420..699626746f333cd361569d8000aa361cb4354799 100644 (file)
@@ -13,9 +13,31 @@ BEGIN {
   @EXPORT    = qw(initLyxStack checkLyxLine closeLyxStack diestack);
 }
 
+# Prototypes
+sub initLyxStack($$$);
+sub diestack($);
+sub closeLyxStack();
+sub setMatching($);
+sub getMatching();
+sub checkForEndBlock($);
+sub newMatch(%);
+sub getSearch($);
+sub getFileType($);
+sub getFileIdx($);
+sub getExt($);
+sub getResult($);
+sub checkForHeader($$);
+sub checkForPreamble($);
+sub checkForLayoutStart($);
+sub checkForInsetStart($);
+sub checkForLatexCommand($);
+sub checkLyxLine($$);
+
 my @stack = ();                        # list of HASH-Arrays
 my $rFont = {};
 my $useNonTexFont = "true";
+my $inputEncoding = undef;
+my $sysdir = undef;
 
 # The elements are:
 # type (layout, inset, header, preamble, ...)
@@ -29,16 +51,27 @@ my $useNonTexFont = "true";
 #              but first set the modified value into $result->[$fileidx]
 #              numerical value will be replaced with appropriate matching group value
 
-sub initLyxStack($$)
+sub initLyxStack($$$)
 {
+  use Cwd 'abs_path';
+
   $rFont = $_[0];
   if ($_[1] eq "systemF") {
     $useNonTexFont = "true";
   }
+  elsif ($_[1] eq "dontChange") {
+    $useNonTexFont = "dontChange";
+  }
   else {
     $useNonTexFont = "false";
+    $inputEncoding = $_[2];
   }
   $stack[0] = { type => "Starting"};
+  my $p = abs_path( __FILE__ );
+  $p =~ s/\/development\/autotests\/.*$/\/lib/;
+  # Save the value to be used as replacement for systemlyxdir in \origin statements
+  $sysdir = $p;
+  # print "Sysdir set to $sysdir\n";
 }
 
 sub diestack($)
@@ -77,7 +110,7 @@ sub diestack($)
 
 sub closeLyxStack()
 {
-  &diestack("Stack not OK") if ($stack[0]->{type} ne "Starting");
+  diestack("Stack not OK") if ($stack[0]->{type} ne "Starting");
 }
 
 sub setMatching($)
@@ -100,7 +133,7 @@ sub checkForEndBlock($)
 
   for my $et ( qw( layout inset preamble header)) {
     if ($l =~ /^\\end_$et$/) {
-      &diestack("Not in $et") if ($stack[0]->{type} ne "$et");
+      diestack("Not in $et") if ($stack[0]->{type} ne "$et");
       #print "End $et\n";
       shift(@stack);
       return(1);
@@ -109,7 +142,7 @@ sub checkForEndBlock($)
   return(0);
 }
 
-sub newMatch($$)
+sub newMatch(%)
 {
   my %elem = @_;
 
@@ -122,7 +155,14 @@ sub newMatch($$)
   if (! defined($elem{"fileidx"})) {
     $elem{"fileidx"} = 1;
   }
-  &diestack("No result defined") if (! defined($elem{"result"}));
+  if (exists($elem{"search"})) {
+    my $ref = ref($elem{"search"});
+    diestack("Wrong or invalid regex (ref == $ref) specified") if ($ref ne "Regexp");
+  }
+  else {
+    diestack("No search defined");
+  }
+  diestack("No result defined") if (! defined($elem{"result"}));
   return(\%elem);
 }
 
@@ -161,9 +201,9 @@ sub getResult($)
   return($m->{"result"});
 }
 
-sub checkForHeader($)
+sub checkForHeader($$)
 {
-  my ($l) = @_;
+  my ($l, $sourcedir) = @_;
 
   if ($l =~ /^\\begin_header\s*$/) {
     my %selem = ();
@@ -171,22 +211,47 @@ sub checkForHeader($)
     $selem{name} = $1;
     unshift(@stack, \%selem);
     my @rElems = ();
-    $rElems[0] = &newMatch("search" => '^\\\\master\s+(.*\.lyx)',
+    $rElems[0] = newMatch("search" => qr/^\\master\s+(.*\.lyx)/,
                           "filetype" => "prefix_only",
                           "result" => ["\\master ", ""]);
     if (keys %{$rFont}) {
       for my $ff ( keys %{$rFont}) {
-       my $elem = &newMatch("search" => '^\\\\font_' . $ff . '\s+',
+       # fontentry of type '\font_roman default'
+       my $elem = newMatch("search" => qr/^\\font_$ff\s+[^\"]*\s*$/,
                             "filetype" => "replace_only",
                             "result" => ["\\font_$ff ", $rFont->{$ff}]);
-       push(@rElems, $elem);
+       # fontentry of type '\font_roman "default"'
+       my $elem1 = newMatch("search" => qr/^\\font_$ff\s+\"[^\"]*\"\s*$/,
+                            "filetype" => "replace_only",
+                            "result" => ["\\font_$ff \"", $rFont->{$ff}, '"']);
+       # fontentry of type '\font_roman "default" "default"'
+       my $elem2 = newMatch("search" => qr/^\\font_$ff\s+\"(.*)\"\s+\"default\"\s*$/,
+                            "filetype" => "replace_only",
+                            "result" => ["\\font_$ff ", '"', "1", '" "', $rFont->{$ff}, '"']);
+       push(@rElems, $elem, $elem1, $elem2);
       }
     }
-    my $elemntf = &newMatch("search" => '^\\\\use_non_tex_fonts\s+(false|true)',
-                           "filetype" => "replace_only",
-                           "result" => ["\\use_non_tex_fonts $useNonTexFont"]);
-    push(@rElems, $elemntf);
-    &setMatching(\@rElems);
+    if ($useNonTexFont ne "dontChange") {
+      my $elemntf = newMatch("search" => qr/^\\use_non_tex_fonts\s+(false|true)/,
+                             "filetype" => "replace_only",
+                             "result" => ["\\use_non_tex_fonts $useNonTexFont"]);
+      push(@rElems, $elemntf);
+    }
+    if (defined($inputEncoding)) {
+      my $inputenc = newMatch("search" =>  qr/^\\inputencoding\s+($inputEncoding->{search})/,
+                             "filetype" => "replace_only",
+                             "result" => ["\\inputencoding " . $inputEncoding->{out}]);
+      push(@rElems, $inputenc);
+    }
+    my $origin = newMatch("search" => qr/^\\origin\s+(\/systemlyxdir)(.*)$/,
+                          "filetype" => "replace_only",
+                          "result" => ["\\origin $sysdir", "2"]);
+    push(@rElems, $origin);
+    my $originu = newMatch("search" => qr/^\\origin\s+unavailable/,
+                          "filetype" => "replace_only",
+                          "result" => ["\\origin $sourcedir"]);
+    push(@rElems, $originu);
+    setMatching(\@rElems);
     return(1);
   }
   return(0);
@@ -201,11 +266,16 @@ sub checkForPreamble($)
     $selem{type} = "preamble";
     $selem{name} = $1;
     unshift(@stack, \%selem);
-    my $rElem = &newMatch("ext" => [".eps", ".png"],
-                         "search" => '^\\\\(photo|ecvpicture)(.*\{)(.*)\}',
+    my $rElem = newMatch("ext" => [".eps", ".png"],
+                         "search" => qr/^\\(photo|ecvpicture)(.*\{)(.*)\}/,
                          "fileidx" => 3,
                          "result" => ["\\", "1", "2", "3", "}"]);
-    &setMatching([$rElem]);
+    #
+    # Remove comments from preamble
+    my $comments = newMatch("search" => qr/^([^%]*)([%]+)([^%]*)$/,
+                           "filetype" => "replace_only",
+                           "result" => ["1", "2"]);
+    setMatching([$rElem, $comments]);
     return(1);
   }
   return(0);
@@ -215,17 +285,18 @@ sub checkForLayoutStart($)
 {
   my ($l) = @_;
 
-  if ($l =~ /^\\begin_layout\s+(.+)$/) {
+  if ($l =~ /^\\begin_layout\s+(.*)$/) {
     #print "started layout\n";
     my %selem = ();
     $selem{type} = "layout";
     $selem{name} = $1;
     unshift(@stack, \%selem);
     if ($selem{name} =~ /^(Picture|Photo)$/ ) {
-      my $rElem = &newMatch("ext" => [".eps", ".png"],
-                           "search" => '^(.+)',
+      my $rElem = newMatch("ext" => [".eps", ".png", ""],
+                            "filetype" => "copy_only",
+                           "search" => qr/^(.+)/,
                            "result" => ["", "", ""]);
-      &setMatching([$rElem]);
+      setMatching([$rElem]);
     }
     return(1);
   }
@@ -243,10 +314,10 @@ sub checkForInsetStart($)
     $selem{name} = $1;
     unshift(@stack, \%selem);
     if ($selem{name} =~ /^(Graphics|External)$/) {
-      my $rElem = &newMatch("search" => '^\s+filename\s+(.+)$',
+      my $rElem = newMatch("search" => qr/^\s+filename\s+(.+)$/,
                            "filetype" => "copy_only",
                            "result" => ["\tfilename ", "", ""]);
-      &setMatching([$rElem]);
+      setMatching([$rElem]);
     }
     return(1);
   }
@@ -262,29 +333,29 @@ sub checkForLatexCommand($)
       my $param = $1;
       if ($stack[0]->{name} =~ /^CommandInset\s+bibtex$/) {
        if ($param eq "bibtex") {
-         my $rElem1 = &newMatch("ext" => ".bib",
+         my $rElem1 = newMatch("ext" => ".bib",
                                 "filetype" => "prefix_for_list",
-                                "search" => '^bibfiles\s+\"(.+)\"',
+                                "search" => qr/^bibfiles\s+\"([^\"]+)\"/,
                                 "result" => ["bibfiles \"", "1", "\""]);
-         my $rElem2 = &newMatch("ext" => ".bst",
+         my $rElem2 = newMatch("ext" => ".bst",
                                 "filetype" => "prefix_for_list",
-                                "search" => '^options\s+\"(.+)\"',
+                                "search" => qr/^options\s+\"(.+)\"/,
                                 "result" => ["options \"", "1", "\""]);
-         &setMatching([$rElem1, $rElem2]);
+         setMatching([$rElem1, $rElem2]);
        }
       }
       elsif ($stack[0]->{name} =~ /^CommandInset\s+include$/) {
-       if ($param =~ /^(verbatiminput\*?|lstinputlisting)$/) {
-         my $rElem = &newMatch("search" => '^filename\s+\"(.+)\"',
+       if ($param =~ /^(verbatiminput\*?|lstinputlisting|inputminted)$/) {
+         my $rElem = newMatch("search" => qr/^filename\s+\"(.+)\"/,
                                "filetype" => "copy_only",
                                "result" => ["filename \"", "", "\""]);
-         &setMatching([$rElem]);
+         setMatching([$rElem]);
        }
        elsif ($param =~ /^(include|input)$/) {
-         my $rElem = &newMatch("search" => '^filename\s+\"(.+)\"',
+         my $rElem = newMatch("search" => qr/^filename\s+\"(.+)\"/,
                                "filetype" => "interpret",
                                "result" => ["filename \"", "", "\""]);
-         &setMatching([$rElem]);
+         setMatching([$rElem]);
        }
       }
     }
@@ -302,24 +373,24 @@ sub checkForLatexCommand($)
 #    separator: to be used while concatenating the filenames
 #    filetype: prefix_only,replace_only,copy_only,interpret
 #              same as before, but without 'prefix_for_list'
-sub checkLyxLine($)
+sub checkLyxLine($$)
 {
-  my ($l) = @_;
-
-  return({"found" => 0}) if (&checkForHeader($l));
-  return({"found" => 0}) if (&checkForPreamble($l));
-  return({"found" => 0}) if (&checkForEndBlock($l));
-  return({"found" => 0}) if (&checkForLayoutStart($l));
-  return({"found" => 0}) if (&checkForInsetStart($l));
-  return({"found" => 0}) if (&checkForLatexCommand($l));
+  my ($l, $sourcedir) = @_;
+
+  return({"found" => 0}) if (checkForHeader($l, $sourcedir));
+  return({"found" => 0}) if (checkForPreamble($l));
+  return({"found" => 0}) if (checkForEndBlock($l));
+  return({"found" => 0}) if (checkForLayoutStart($l));
+  return({"found" => 0}) if (checkForInsetStart($l));
+  return({"found" => 0}) if (checkForLatexCommand($l));
   if (defined($stack[0])) {
-    my $rMatch = &getMatching();
+    my $rMatch = getMatching();
     for my $m ( @{$rMatch}) {
-      my $search = &getSearch($m);
-      if ($l =~ /$search/) {
+      my $search = getSearch($m);
+      if ($l =~ $search) {
        my @matches = ($1, $2, $3, $4);
-       my $filetype = &getFileType($m);
-       my @result2 = @{&getResult($m)};
+       my $filetype = getFileType($m);
+       my @result2 = @{getResult($m)};
 
        for my $r (@result2) {
          if ($r =~ /^\d$/) {
@@ -334,12 +405,12 @@ sub checkLyxLine($)
          return(\%result);
        }
        else {
-         my $fileidx = &getFileIdx($m);
+         my $fileidx = getFileIdx($m);
          my $filename = $matches[$fileidx-1];
          if ($filename !~ /^\.*$/) {
            my %result = ("found" => 1,
                          "fileidx" => $fileidx,
-                         "ext" => &getExt($m),
+                         "ext" => getExt($m),
                          "result" => \@result2);
            if ($filetype eq "prefix_for_list") {
              # bibfiles|options in CommandInset bibtex