]> git.lyx.org Git - features.git/blobdiff - src/LaTeX.C
clear()->erase() ; lots of using directives for cxx
[features.git] / src / LaTeX.C
index 411519627e95491480c560e88ee241dd5a60f9e9..951e5c91e0e6dba997e6533d3bf5491842408235 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
  *
  * ====================================================== 
@@ -33,6 +33,8 @@
 #include "gettext.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
@@ -51,7 +53,7 @@ extern BufferList bufferlist;
 
 struct texfile_struct {
        LaTeX::TEX_FILES file;
-       char const *extension;
+       char const * extension;
 };
 
 static
@@ -97,6 +99,31 @@ LaTeX::LaTeX(string const & latex, string const & f, string const & p)
 }
 
 
+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
+       ::unlink(depfile.c_str());
+
+       // but the reason for the error might be in a generated file...
+
+       // bibtex file
+       string bbl = ChangeExtension(file, ".bbl", true);
+       ::unlink(bbl.c_str());
+
+       // makeindex file
+       string ind = ChangeExtension(file, ".ind", true);
+       ::unlink(ind.c_str());
+       
+       // Also remove the aux file
+       string aux = ChangeExtension(file, ".aux", true);
+       ::unlink(aux.c_str());
+}
+
+
 int LaTeX::run(TeXErrors & terr, MiniBuffer * minib)
        // We know that this function will only be run if the lyx buffer
        // has been changed. We also know that a newly written .tex file
@@ -139,34 +166,47 @@ int LaTeX::run(TeXErrors & terr, MiniBuffer * minib)
                // Update the checksums
                head.update();
                
-               lyxerr[Debug::LATEX] << "Dependency file exists" << endl;
+               lyxerr[Debug::DEPEND] << "Dependency file exists" << endl;
                if (head.sumchange()) {
                        ++count;
+                       lyxerr[Debug::DEPEND]
+                               << "Dependency file has changed" << endl;
                        lyxerr[Debug::LATEX]
-                               << "Dependency file has changed\n"
                                << "Run #" << count << endl; 
                        minib->Set(string(_("LaTeX run number ")) + tostr(count));
                        minib->Store();
                        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 << "Bibtex demands rerun" << endl;
+                               lyxerr[Debug::DEPEND]
+                                       << "Bibtex demands rerun" << endl;
                } else {
-                       lyxerr[Debug::LATEX] << "return no_change" << endl;
+                       lyxerr[Debug::DEPEND] << "return no_change" << endl;
                        return LaTeX::NO_CHANGE;
                }
        } else {
                ++count;
-               lyxerr[Debug::LATEX] << "Dependency file does not exist\n"
-                                    << "Run #" << count << endl;
+               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();
                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.
@@ -219,19 +259,24 @@ int LaTeX::run(TeXErrors & terr, MiniBuffer * minib)
        if (rerun || head.sumchange()) {
                rerun = false;
                ++count;
+               lyxerr[Debug::DEPEND]
+                       << "Dep. file has changed or rerun requested" << endl;
                lyxerr[Debug::LATEX]
-                       << "Dep. file has changed or rerun requested\n"
                        << "Run #" << count << endl;
                minib->Set(string(_("LaTeX run number ")) + tostr(count));
                minib->Store();
                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();
        } else {
-               lyxerr[Debug::LATEX] << "Dep. file has NOT changed" << endl;
+               lyxerr[Debug::DEPEND] << "Dep. file has NOT changed" << endl;
        }
 
        // 1.5
@@ -272,7 +317,11 @@ int LaTeX::run(TeXErrors & terr, MiniBuffer * minib)
                minib->Store();
                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();
        }
@@ -287,7 +336,7 @@ int LaTeX::run(TeXErrors & terr, MiniBuffer * minib)
 int LaTeX::operator()()
 {
 #ifndef __EMX__
-       string tmp = cmd + ' ' + file + " > /dev/null";
+       string tmp = cmd + ' ' + QuoteName(file) + " > /dev/null";
 #else // cmd.exe (OS/2) causes SYS0003 error at "/dev/null"
        string tmp = cmd + ' ' + file + " > nul";
 #endif
@@ -444,6 +493,8 @@ 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);
        lyxerr[Debug::LATEX] << "Log file: " << tmp << endl;
@@ -494,7 +545,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;
@@ -517,8 +574,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
@@ -570,7 +635,7 @@ void LaTeX::deplog(DepTable & head)
                                break;
                        foundfile += c;
                }
-               lyxerr[Debug::LATEX] << "Found file: " 
+               lyxerr[Debug::DEPEND] << "Found file: " 
                                     << foundfile << endl;
                
                // Ok now we found a file.
@@ -582,7 +647,7 @@ void LaTeX::deplog(DepTable & head)
                //     absolute path and should
                //     be inserted.
                if (AbsolutePath(foundfile)) {
-                       lyxerr[Debug::LATEX] << "AbsolutePath file: " 
+                       lyxerr[Debug::DEPEND] << "AbsolutePath file: " 
                                             << foundfile << endl;
                        // On inital insert we want to do the update at once
                        // since this file can not be a file generated by
@@ -595,7 +660,7 @@ void LaTeX::deplog(DepTable & head)
                //     insert it into head
                if (FileInfo(OnlyFilename(foundfile)).exist()) {
                        if (suffixIs(foundfile, ".aux")) {
-                               lyxerr[Debug::LATEX] << "We don't want "
+                               lyxerr[Debug::DEPEND] << "We don't want "
                                                     << OnlyFilename(foundfile)
                                                     << " in the dep file"
                                                     << endl;
@@ -603,19 +668,19 @@ void LaTeX::deplog(DepTable & head)
                                // This is a tex file generated by LyX
                                // and latex is not likely to change this
                                // during its runs.
-                               lyxerr[Debug::LATEX] << "Tmpdir TeX file: "
+                               lyxerr[Debug::DEPEND] << "Tmpdir TeX file: "
                                                     << OnlyFilename(foundfile)
                                                     << endl;
                                head.insert(foundfile, true);
                        } else {
-                               lyxerr[Debug::LATEX] << "In tmpdir file:"
+                               lyxerr[Debug::DEPEND] << "In tmpdir file:"
                                                     << OnlyFilename(foundfile)
                                                     << endl;
                                head.insert(OnlyFilename(foundfile));
                        }
                        continue;
                }
-               lyxerr[Debug::LATEX]
+               lyxerr[Debug::DEPEND]
                        << "Not a file or we are unable to find it."
                        << endl;
        }
@@ -632,10 +697,9 @@ void LaTeX::deptex(DepTable & head)
                        tmp = ChangeExtension(file,
                                              all_files[i].extension,
                                              true);
-                       lyxerr[Debug::LATEX] << "deptex: " << tmp << endl;
+                       lyxerr[Debug::DEPEND] << "deptex: " << tmp << endl;
                        if (fi.newFile(tmp).exist())
                                head.insert(tmp);
                }
        }
 }
-