]> git.lyx.org Git - lyx.git/blobdiff - src/LaTeX.C
Fixed inset-text border redraw problem and hopefully recalc boostup.
[lyx.git] / src / LaTeX.C
index 380c14a9fdd8d6e23c3e9896469ba2984c6f99d8..deaffae9ce38a8d3797dc60db20e55966f1d7e65 100644 (file)
@@ -3,9 +3,9 @@
  * 
  *           LyX, The Document Processor        
  *          Copyright 1995 Matthias Ettrich
- *           Copyright 1995-1999 The LyX Team.
+ *           Copyright 1995-2000 The LyX Team.
  *
- *           This file is Copyright 1996-1999
+ *           This file is Copyright 1996-2000
  *           Lars Gullik Bjønnes
  *
  * ====================================================== 
 #include "bufferlist.h"
 #include "minibuffer.h"
 #include "gettext.h"
+#include "lyx_gui_misc.h"
 
 using std::ifstream;
+using std::getline;
+using std::endl;
 
 // TODO: in no particular order
 // - get rid of the extern BufferList and the call to
@@ -49,29 +52,6 @@ using std::ifstream;
 
 extern BufferList bufferlist;
 
-struct texfile_struct {
-       LaTeX::TEX_FILES file;
-       char const * extension;
-};
-
-static
-const texfile_struct all_files[] = {
-       { LaTeX::AUX, ".aux"},
-       { LaTeX::BBL, ".bbl"},
-       { LaTeX::DVI, ".dvi"},
-       { LaTeX::GLO, ".glo"},
-       { LaTeX::IDX, ".idx"},
-       { LaTeX::IND, ".ind"},
-       { LaTeX::LOF, ".lof"},
-       { LaTeX::LOA, ".loa"},
-       { LaTeX::LOG, ".log"},
-       { LaTeX::LOT, ".lot"},
-       { LaTeX::TOC, ".toc"},
-       { LaTeX::LTX, ".ltx"},
-       { LaTeX::TEX, ".tex"}
-};
-
-
 /*
  * CLASS TEXERRORS
  */
@@ -90,10 +70,37 @@ void TeXErrors::insertError(int line, string const & error_desc,
 LaTeX::LaTeX(string const & latex, string const & f, string const & p)
                : cmd(latex), file(f), path(p)
 {
-       tex_files = NO_FILES;
-       file_count = sizeof(all_files) / sizeof(texfile_struct);
        num_errors = 0;
        depfile = file + ".dep";
+       if (prefixIs(cmd, "pdf")) // Do we use pdflatex ?
+               depfile += "-pdf";
+}
+
+
+void LaTeX::deleteFilesOnError() const
+{
+       // currently just a dummy function.
+
+       // What files do we have to delete?
+
+       // This will at least make latex do all the runs
+       lyx::unlink(depfile);
+
+       // but the reason for the error might be in a generated file...
+
+       string ofname = OnlyFilename(file);
+
+       // bibtex file
+       string bbl = ChangeExtension(ofname, ".bbl");
+       lyx::unlink(bbl);
+
+       // makeindex file
+       string ind = ChangeExtension(ofname, ".ind");
+       lyx::unlink(ind);
+       
+       // Also remove the aux file
+       string aux = ChangeExtension(ofname, ".aux");
+       lyx::unlink(aux);
 }
 
 
@@ -112,7 +119,7 @@ int LaTeX::run(TeXErrors & terr, MiniBuffer * minib)
        bool rerun = false; // rerun requested
        
        // The class LaTeX does not know the temp path.
-       bufferlist.updateIncludedTeXfiles(GetCWD());
+       bufferlist.updateIncludedTeXfiles(lyx::getcwd()); //GetCWD());
        
        // Never write the depfile if an error was encountered.
        
@@ -146,11 +153,15 @@ int LaTeX::run(TeXErrors & terr, MiniBuffer * minib)
                                << "Dependency file has changed" << endl;
                        lyxerr[Debug::LATEX]
                                << "Run #" << count << endl; 
-                       minib->Set(string(_("LaTeX run number ")) + tostr(count));
-                       minib->Store();
+                       WriteStatus(minib,
+                                   string(_("LaTeX run number ")) + tostr(count));
                        this->operator()();
                        scanres = scanLogFile(terr);
-                       if (scanres & LaTeX::ERRORS) return scanres; // return on error
+                       if (scanres & LaTeX::ERRORS) {
+                               deleteFilesOnError();
+                               return scanres; // return on error
+                       }
+                       
                        run_bibtex = scanAux(head);
                        if (run_bibtex)
                                lyxerr[Debug::DEPEND]
@@ -163,19 +174,23 @@ int LaTeX::run(TeXErrors & terr, MiniBuffer * minib)
                ++count;
                lyxerr[Debug::DEPEND]
                        << "Dependency file does not exist" << endl;
+               
                lyxerr[Debug::LATEX]
                        << "Run #" << count << endl;
                head.insert(file, true);
-               minib->Set(string(_("LaTeX run number ")) + tostr(count));
-               minib->Store();
+               WriteStatus(minib,
+                           string(_("LaTeX run number ")) + tostr(count));
                this->operator()();
                scanres = scanLogFile(terr);
-               if (scanres & LaTeX::ERRORS) return scanres; // return on error
+               if (scanres & LaTeX::ERRORS) {
+                       deleteFilesOnError();
+                       return scanres; // return on error
+               }
+               
        }
 
        // update the dependencies.
        deplog(head); // reads the latex log
-       deptex(head); // checks for latex files
        head.update();
 
        // 0.5
@@ -186,12 +201,11 @@ int LaTeX::run(TeXErrors & terr, MiniBuffer * minib)
        // if needed.
        
        // run makeindex
-       if (head.haschanged(ChangeExtension(file, ".idx", true))) {
+       if (head.haschanged(OnlyFilename(ChangeExtension(file, ".idx")))) {
                // no checks for now
                lyxerr[Debug::LATEX] << "Running MakeIndex." << endl;
-               minib->Set(_("Running MakeIndex."));
-               minib->Store();
-               rerun = runMakeIndex(ChangeExtension(file, ".idx", true));
+               WriteStatus(minib, _("Running MakeIndex."));
+               rerun = runMakeIndex(OnlyFilename(ChangeExtension(file, ".idx")));
        }
 
        // run bibtex
@@ -203,9 +217,9 @@ int LaTeX::run(TeXErrors & terr, MiniBuffer * minib)
                // tags is found -> run bibtex and set rerun = true;
                // no checks for now
                lyxerr[Debug::LATEX] << "Running BibTeX." << endl;
-               minib->Set(_("Running BibTeX."));
-               minib->Store();
-               rerun = runBibTeX(ChangeExtension(file, ".aux", true), head);
+               WriteStatus(minib, _("Running BibTeX."));
+               rerun = runBibTeX(OnlyFilename(ChangeExtension(file, ".aux")), 
+                                 head);
        }
        
        // 1
@@ -227,11 +241,15 @@ int LaTeX::run(TeXErrors & terr, MiniBuffer * minib)
                        << "Dep. file has changed or rerun requested" << endl;
                lyxerr[Debug::LATEX]
                        << "Run #" << count << endl;
-               minib->Set(string(_("LaTeX run number ")) + tostr(count));
-               minib->Store();
+               WriteStatus(minib,
+                           string(_("LaTeX run number ")) + tostr(count));
                this->operator()();
                scanres = scanLogFile(terr);
-               if (scanres & LaTeX::ERRORS) return scanres; // return on error
+               if (scanres & LaTeX::ERRORS) {
+                       deleteFilesOnError();
+                       return scanres; // return on error
+               }
+               
                // update the depedencies
                deplog(head); // reads the latex log
                head.update();
@@ -248,12 +266,11 @@ int LaTeX::run(TeXErrors & terr, MiniBuffer * minib)
        // more after this.
        
        // run makeindex if the <file>.idx has changed or was generated.
-       if (head.haschanged(ChangeExtension(file, ".idx", true))) {
+       if (head.haschanged(OnlyFilename(ChangeExtension(file, ".idx")))) {
                // no checks for now
                lyxerr[Debug::LATEX] << "Running MakeIndex." << endl;
-               minib->Set(_("Running MakeIndex."));
-               minib->Store();
-               rerun = runMakeIndex(ChangeExtension(file, ".idx", true));
+               WriteStatus(minib, _("Running MakeIndex."));
+               rerun = runMakeIndex(OnlyFilename(ChangeExtension(file, ".idx")));
        }
        
        // 2
@@ -273,11 +290,14 @@ int LaTeX::run(TeXErrors & terr, MiniBuffer * minib)
                rerun = false;
                ++count;
                lyxerr[Debug::LATEX] << "Run #" << count << endl;
-               minib->Set(string(_("LaTeX run number ")) + tostr(count));
-               minib->Store();
+               WriteStatus(minib, string(_("LaTeX run number ")) + tostr(count));
                this->operator()();
                scanres = scanLogFile(terr);
-               if (scanres & LaTeX::ERRORS) return scanres; // return on error
+               if (scanres & LaTeX::ERRORS) {
+                       deleteFilesOnError();
+                       return scanres; // return on error
+               }
+               
                // keep this updated
                head.update();
        }
@@ -326,7 +346,7 @@ bool LaTeX::scanAux(DepTable & dep)
        if (dep.extchanged(".bib")
            || dep.extchanged(".bst")) return true;
        
-       string aux = ChangeExtension(file, ".aux", true);
+       string aux = OnlyFilename(ChangeExtension(file, ".aux"));
        ifstream ifs(aux.c_str());
        string token;
        LRegex reg1("\\\\bibdata\\{([^}]+)\\}");
@@ -348,7 +368,7 @@ bool LaTeX::scanAux(DepTable & dep)
                                }
                                string full_l =
                                        findtexfile(
-                                               ChangeExtension(l, "bib", false), "bib");
+                                               ChangeExtension(l, "bib"), "bib");
                                if (!full_l.empty()) {
                                        if (!dep.exist(full_l))
                                                return true;
@@ -362,7 +382,7 @@ bool LaTeX::scanAux(DepTable & dep)
                        // pass it to the helper
                        string full_l =
                                findtexfile(
-                                       ChangeExtension(style, "bst", false),
+                                       ChangeExtension(style, "bst"),
                                        "bst");
                        if (!full_l.empty()) {
                                if (!dep.exist(full_l))
@@ -406,7 +426,7 @@ bool LaTeX::runBibTeX(string const & f, DepTable & dep)
                                }
                                string full_l = 
                                        findtexfile(
-                                               ChangeExtension(l, "bib", false),
+                                               ChangeExtension(l, "bib"),
                                                "bib");
                                lyxerr[Debug::LATEX] << "Bibtex database: `"
                                                     << full_l << "'" << endl;
@@ -424,7 +444,7 @@ bool LaTeX::runBibTeX(string const & f, DepTable & dep)
                        // pass it to the helper
                        string full_l = 
                                findtexfile(
-                                       ChangeExtension(style, "bst", false),
+                                       ChangeExtension(style, "bst"),
                                        "bst");
                        lyxerr[Debug::LATEX] << "Bibtex style: `"
                                             << full_l << "'" << endl;
@@ -437,7 +457,7 @@ bool LaTeX::runBibTeX(string const & f, DepTable & dep)
        if (using_bibtex) {
                // run bibtex and
                string tmp = "bibtex ";
-               tmp += ChangeExtension(file, string(), true);
+               tmp += OnlyFilename(ChangeExtension(file, string()));
                Systemcalls one;
                one.startscript(Systemcalls::System, tmp);
                return true;
@@ -449,8 +469,10 @@ bool LaTeX::runBibTeX(string const & f, DepTable & dep)
 
 int LaTeX::scanLogFile(TeXErrors & terr)
 {
+       int last_line = -1;
+       int line_count = 1;
        int retval = NO_ERRORS;
-       string tmp = ChangeExtension(file, ".log", true);
+       string tmp = OnlyFilename(ChangeExtension(file, ".log"));
        lyxerr[Debug::LATEX] << "Log file: " << tmp << endl;
        ifstream ifs(tmp.c_str());
 
@@ -499,7 +521,13 @@ int LaTeX::scanLogFile(TeXErrors & terr)
                                retval |= LATEX_ERROR;
                        // get the next line
                        string tmp;
-                       getline(ifs, tmp);
+                       int count = 0;
+                       do {
+                               if (!getline(ifs, tmp))
+                                       break;
+                               if (++count > 10)
+                                       break;
+                       } while (!prefixIs(tmp, "l."));
                        if (prefixIs(tmp, "l.")) {
                                // we have a latex error
                                retval |=  TEX_ERROR;
@@ -522,8 +550,16 @@ int LaTeX::scanLogFile(TeXErrors & terr)
                                        << "line: " << line << '\n'
                                        << "Desc: " << desc << '\n'
                                        << "Text: " << errstr << endl;
-                               terr.insertError(line, desc, errstr);
-                               ++num_errors;
+                               if (line == last_line)
+                                       ++line_count;
+                               else {
+                                       line_count = 1;
+                                       last_line = line;
+                               }
+                               if (line_count <= 5) {
+                                       terr.insertError(line, desc, errstr);
+                                       ++num_errors;
+                               }
                        }
                } else {
                        // information messages, TeX warnings and other
@@ -555,40 +591,61 @@ void LaTeX::deplog(DepTable & head)
        // files used by the LaTeX run. The files are then entered into the
        // dependency file.
 
-       string logfile = ChangeExtension(file, ".log", true);
+       string logfile = OnlyFilename(ChangeExtension(file, ".log"));
+
+       LRegex reg1(")* *\\(([^ \\)]+).*");
+       LRegex reg2("File: ([^ ]+).*");
+       LRegex reg3("No file ([^ ]+)\\..*");
+       LRegex reg4("\\\\openout[0-9]+.*=.*`([^ ]+)'\\..*");
+       LRegex unwanted("^.*\\.(aux|log|dvi|bbl|ind|glo)$");
        
        ifstream ifs(logfile.c_str());
        while (ifs) {
-               // Now we read chars until we find a '('
-               char c = 0;
-               while(ifs.get(c)) {
-                       if (c == '(') break;
-               };
-               if (!ifs) break;
-               
-               // We now have c == '(', we now read the the sequence of
-               // chars until reaching EOL, ' ' or ')' and put that
-               // into a string.
+               // Ok, the scanning of files here is not sufficient.
+               // Sometimes files are named by "File: xxx" only
+               // So I think we should use some regexps to find files instead.
+               // "(\([^ ]+\)"   should match the "(file " variant
+               // "File: \([^ ]+\)" should match the "File: file" variant
                string foundfile;
-               while (ifs.get(c)) {
-                       if (c == '\n' || c == ' ' || c == ')')
-                               break;
-                       foundfile += c;
+               string token;
+               getline(ifs, token);
+               if (token.empty()) continue;
+               
+               if (reg1.exact_match(token)) {
+                       LRegex::SubMatches const & sub = reg1.exec(token);
+                       foundfile = LSubstring(token, sub[1].first,
+                                              sub[1].second);
+               } else if (reg2.exact_match(token)) {
+                       LRegex::SubMatches const & sub = reg2.exec(token);
+                       foundfile = LSubstring(token, sub[1].first,
+                                              sub[1].second);
+               } else if (reg3.exact_match(token)) {
+                       LRegex::SubMatches const & sub = reg3.exec(token);
+                       foundfile = LSubstring(token, sub[1].first,
+                                              sub[1].second);
+               } else if (reg4.exact_match(token)) {
+                       LRegex::SubMatches const & sub = reg4.exec(token);
+                       foundfile = LSubstring(token, sub[1].first,
+                                              sub[1].second);
+               } else {
+                       continue;
                }
+
                lyxerr[Debug::DEPEND] << "Found file: " 
-                                    << foundfile << endl;
+                                     << foundfile << endl;
                
                // Ok now we found a file.
-               // Now we should make sure that
-               // this is a file that we can
-               // access through the normal
-               // paths:
+               // Now we should make sure that this is a file that we can
+               // access through the normal paths.
+               // We will not try any fance search methods to
+               // find the file.
+               
                // (1) foundfile is an
                //     absolute path and should
                //     be inserted.
                if (AbsolutePath(foundfile)) {
                        lyxerr[Debug::DEPEND] << "AbsolutePath file: " 
-                                            << foundfile << endl;
+                                             << foundfile << endl;
                        // On inital insert we want to do the update at once
                        // since this file can not be a file generated by
                        // the latex run.
@@ -599,23 +656,26 @@ void LaTeX::deplog(DepTable & head)
                // (2) foundfile is in the tmpdir
                //     insert it into head
                if (FileInfo(OnlyFilename(foundfile)).exist()) {
-                       if (suffixIs(foundfile, ".aux")) {
-                               lyxerr[Debug::DEPEND] << "We don't want "
-                                                    << OnlyFilename(foundfile)
-                                                    << " in the dep file"
-                                                    << endl;
+                       if (unwanted.exact_match(foundfile)) {
+                               lyxerr[Debug::DEPEND]
+                                       << "We don't want "
+                                       << OnlyFilename(foundfile)
+                                       << " in the dep file"
+                                       << endl;
                        } else if (suffixIs(foundfile, ".tex")) {
                                // This is a tex file generated by LyX
                                // and latex is not likely to change this
                                // during its runs.
-                               lyxerr[Debug::DEPEND] << "Tmpdir TeX file: "
-                                                    << OnlyFilename(foundfile)
-                                                    << endl;
+                               lyxerr[Debug::DEPEND]
+                                       << "Tmpdir TeX file: "
+                                       << OnlyFilename(foundfile)
+                                       << endl;
                                head.insert(foundfile, true);
                        } else {
-                               lyxerr[Debug::DEPEND] << "In tmpdir file:"
-                                                    << OnlyFilename(foundfile)
-                                                    << endl;
+                               lyxerr[Debug::DEPEND]
+                                       << "In tmpdir file:"
+                                       << OnlyFilename(foundfile)
+                                       << endl;
                                head.insert(OnlyFilename(foundfile));
                        }
                        continue;
@@ -625,21 +685,3 @@ void LaTeX::deplog(DepTable & head)
                        << endl;
        }
 }
-
-
-void LaTeX::deptex(DepTable & head)
-{
-       int except = AUX|LOG|DVI|BBL|IND|GLO; 
-       string tmp;
-       FileInfo fi;
-       for (int i = 0; i < file_count; ++i) {
-               if (!(all_files[i].file & except)) {
-                       tmp = ChangeExtension(file,
-                                             all_files[i].extension,
-                                             true);
-                       lyxerr[Debug::DEPEND] << "deptex: " << tmp << endl;
-                       if (fi.newFile(tmp).exist())
-                               head.insert(tmp);
-               }
-       }
-}