]> git.lyx.org Git - lyx.git/blobdiff - src/LaTeX.C
add comment
[lyx.git] / src / LaTeX.C
index 9a81f8fea5536d42c71a636394c0c52e8449d385..b8caf83b607aa2bf65056589af6a3ad41688edc2 100644 (file)
@@ -40,11 +40,14 @@ using support::absolutePath;
 using support::bformat;
 using support::changeExtension;
 using support::contains;
+using support::FileName;
 using support::findtexfile;
 using support::getcwd;
+using support::makeAbsPath;
 using support::onlyFilename;
 using support::prefixIs;
 using support::quoteName;
+using support::removeExtension;
 using support::rtrim;
 using support::split;
 using support::subst;
@@ -123,16 +126,16 @@ bool operator!=(Aux_Info const & a, Aux_Info const & o)
  */
 
 LaTeX::LaTeX(string const & latex, OutputParams const & rp,
-            string const & f, string const & p)
-       : cmd(latex), file(f), path(p), runparams(rp)
+            FileName const & f)
+       : cmd(latex), file(f), runparams(rp)
 {
        num_errors = 0;
-       depfile = file + ".dep";
        if (prefixIs(cmd, "pdf")) { // Do we use pdflatex ?
-               depfile += "-pdf";
-               output_file = changeExtension(file,".pdf");
+               depfile = FileName(file.absFilename() + ".dep-pdf");
+               output_file = FileName(changeExtension(file.absFilename(), ".pdf"));
        } else {
-               output_file = changeExtension(file,".dvi");
+               depfile = FileName(file.absFilename() + ".dep");
+               output_file = FileName(changeExtension(file.absFilename(), ".dvi"));
        }
 }
 
@@ -148,22 +151,20 @@ void LaTeX::deleteFilesOnError() const
 
        // but the reason for the error might be in a generated file...
 
-       string const ofname = onlyFilename(file);
-
        // bibtex file
-       string const bbl = changeExtension(ofname, ".bbl");
+       FileName const bbl(changeExtension(file.absFilename(), ".bbl"));
        unlink(bbl);
 
        // makeindex file
-       string const ind = changeExtension(ofname, ".ind");
+       FileName const ind(changeExtension(file.absFilename(), ".ind"));
        unlink(ind);
 
        // nomencl file
-       string const nls = changeExtension(ofname, ".nls");
+       FileName const nls(changeExtension(file.absFilename(), ".nls"));
        unlink(nls);
 
        // Also remove the aux file
-       string const aux = changeExtension(ofname, ".aux");
+       FileName const aux(changeExtension(file.absFilename(), ".aux"));
        unlink(aux);
 }
 
@@ -183,7 +184,7 @@ int LaTeX::run(TeXErrors & terr)
        bool rerun = false; // rerun requested
 
        // The class LaTeX does not know the temp path.
-       theBufferList().updateIncludedTeXfiles(getcwd(), runparams);
+       theBufferList().updateIncludedTeXfiles(getcwd().absFilename(), runparams);
 
        // Never write the depfile if an error was encountered.
 
@@ -203,9 +204,9 @@ int LaTeX::run(TeXErrors & terr)
        //             remake the dependency file.
        //
 
-       bool had_depfile = fs::exists(depfile);
+       bool had_depfile = fs::exists(depfile.toFilesystemEncoding());
        bool run_bibtex = false;
-       string aux_file = onlyFilename(changeExtension(file, "aux"));
+       FileName const aux_file(changeExtension(file.absFilename(), "aux"));
 
        if (had_depfile) {
                lyxerr[Debug::DEPEND] << "Dependency file exists" << endl;
@@ -219,7 +220,7 @@ int LaTeX::run(TeXErrors & terr)
                // Can't just check if anything has changed because it might have aborted
                // on error last time... in which cas we need to re-run latex
                // and collect the error messages (even if they are the same).
-               if (!fs::exists(output_file)) {
+               if (!fs::exists(output_file.toFilesystemEncoding())) {
                        lyxerr[Debug::DEPEND]
                                << "re-running LaTeX because output file doesn't exist." << endl;
                } else if (!head.sumchange()) {
@@ -277,21 +278,24 @@ int LaTeX::run(TeXErrors & terr)
 
        // memoir (at least) writes an empty *idx file in the first place.
        // A second latex run is needed.
-       rerun = fs::exists(changeExtension(file, ".idx"))
-               && fs::is_empty(changeExtension(file, ".idx"));
+       FileName const idxfile(changeExtension(file.absFilename(), ".idx"));
+       rerun = fs::exists(idxfile.toFilesystemEncoding()) &&
+               fs::is_empty(idxfile.toFilesystemEncoding());
 
        // run makeindex
-       if (head.haschanged(onlyFilename(changeExtension(file, ".idx")))) {
+       if (head.haschanged(idxfile)) {
                // no checks for now
                lyxerr[Debug::LATEX] << "Running MakeIndex." << endl;
                message(_("Running MakeIndex."));
-               rerun |= runMakeIndex(onlyFilename(changeExtension(file, ".idx")), runparams);
+               // onlyFilename() is needed for cygwin
+               rerun |= runMakeIndex(onlyFilename(idxfile.absFilename()), runparams);
        }
-       if (head.haschanged(onlyFilename(changeExtension(file, ".nlo")))) {
+       if (head.haschanged(FileName(changeExtension(file.absFilename(), ".nlo")))) {
                lyxerr[Debug::LATEX] << "Running MakeIndex for nomencl." << endl;
                message(_("Running MakeIndex for nomencl."));
-               string const nomenclstr = " -s nomencl.ist -o " + changeExtension(file, ".nls");
-               rerun |= runMakeIndex(onlyFilename(changeExtension(file, ".nlo")), runparams, nomenclstr);
+               // onlyFilename() is needed for cygwin
+               string const nomenclstr = " -s nomencl.ist -o " + onlyFilename(changeExtension(file.toFilesystemEncoding(), ".nls"));
+               rerun |= runMakeIndex(onlyFilename(changeExtension(file.absFilename(), ".nlo")), runparams, nomenclstr);
        }
 
        // run bibtex
@@ -355,19 +359,21 @@ int LaTeX::run(TeXErrors & terr)
        // more after this.
 
        // run makeindex if the <file>.idx has changed or was generated.
-       if (head.haschanged(onlyFilename(changeExtension(file, ".idx")))) {
+       if (head.haschanged(FileName(changeExtension(file.absFilename(), ".idx")))) {
                // no checks for now
                lyxerr[Debug::LATEX] << "Running MakeIndex." << endl;
                message(_("Running MakeIndex."));
-               rerun = runMakeIndex(onlyFilename(changeExtension(file, ".idx")), runparams);
+               // onlyFilename() is needed for cygwin
+               rerun = runMakeIndex(onlyFilename(changeExtension(file.absFilename(), ".idx")), runparams);
        }
 
        // I am not pretty sure if need this twice. 
-       if (head.haschanged(onlyFilename(changeExtension(file, ".nlo")))) {
+       if (head.haschanged(FileName(changeExtension(file.absFilename(), ".nlo")))) {
                lyxerr[Debug::LATEX] << "Running MakeIndex for nomencl." << endl;
                message(_("Running MakeIndex for nomencl."));
-               string nomenclstr = " -s nomencl.ist -o " + changeExtension(file, ".nls");
-               rerun |= runMakeIndex(onlyFilename(changeExtension(file, ".nlo")), runparams, nomenclstr);
+               // onlyFilename() is needed for cygwin
+               string nomenclstr = " -s nomencl.ist -o " + onlyFilename(changeExtension(file.toFilesystemEncoding(), ".nls"));
+               rerun |= runMakeIndex(onlyFilename(changeExtension(file.absFilename(), ".nlo")), runparams, nomenclstr);
        }
 
        // 2
@@ -408,7 +414,8 @@ int LaTeX::run(TeXErrors & terr)
 
 int LaTeX::startscript()
 {
-       string tmp = cmd + ' ' + quoteName(file) + " > " + os::nulldev();
+       // onlyFilename() is needed for cygwin
+       string tmp = cmd + ' ' + quoteName(onlyFilename(file.toFilesystemEncoding())) + " > " + os::nulldev();
        Systemcall one;
        return one.startscript(Systemcall::Wait, tmp);
 }
@@ -432,17 +439,18 @@ bool LaTeX::runMakeIndex(string const & f, OutputParams const & runparams,
 
 
 vector<Aux_Info> const
-LaTeX::scanAuxFiles(string const & file)
+LaTeX::scanAuxFiles(FileName const & file)
 {
        vector<Aux_Info> result;
 
        result.push_back(scanAuxFile(file));
 
+       string const basename = removeExtension(file.absFilename());
        for (int i = 1; i < 1000; ++i) {
-               string const file2 = changeExtension(file, "")
+               FileName const file2(basename
                        + '.' + convert<string>(i)
-                       + ".aux";
-               if (!fs::exists(file2))
+                       + ".aux");
+               if (!fs::exists(file2.toFilesystemEncoding()))
                        break;
                result.push_back(scanAuxFile(file2));
        }
@@ -450,7 +458,7 @@ LaTeX::scanAuxFiles(string const & file)
 }
 
 
-Aux_Info const LaTeX::scanAuxFile(string const & file)
+Aux_Info const LaTeX::scanAuxFile(FileName const & file)
 {
        Aux_Info result;
        result.aux_file = file;
@@ -459,11 +467,11 @@ Aux_Info const LaTeX::scanAuxFile(string const & file)
 }
 
 
-void LaTeX::scanAuxFile(string const & file, Aux_Info & aux_info)
+void LaTeX::scanAuxFile(FileName const & file, Aux_Info & aux_info)
 {
        lyxerr[Debug::LATEX] << "Scanning aux file: " << file << endl;
 
-       ifstream ifs(file.c_str());
+       ifstream ifs(file.toFilesystemEncoding().c_str());
        string token;
        static regex const reg1("\\\\citation\\{([^}]+)\\}");
        static regex const reg2("\\\\bibdata\\{([^}]+)\\}");
@@ -473,6 +481,9 @@ void LaTeX::scanAuxFile(string const & file, Aux_Info & aux_info)
        while (getline(ifs, token)) {
                token = rtrim(token, "\r");
                smatch sub;
+               // FIXME UNICODE: We assume that citation keys and filenames
+               // in the aux file are in the file system encoding.
+               token = to_utf8(from_filesystem8bit(token));
                if (regex_match(token, sub, reg1)) {
                        string data = sub.str(1);
                        while (!data.empty()) {
@@ -504,7 +515,7 @@ void LaTeX::scanAuxFile(string const & file, Aux_Info & aux_info)
                        aux_info.styles.insert(style);
                } else if (regex_match(token, sub, reg4)) {
                        string const file2 = sub.str(1);
-                       scanAuxFile(file2, aux_info);
+                       scanAuxFile(makeAbsPath(file2), aux_info);
                }
        }
 }
@@ -523,14 +534,14 @@ void LaTeX::updateBibtexDependencies(DepTable & dep,
             it != bibtex_info.end(); ++it) {
                for (set<string>::const_iterator it2 = it->databases.begin();
                     it2 != it->databases.end(); ++it2) {
-                       string file = findtexfile(*it2, "bib");
+                       FileName const file = findtexfile(*it2, "bib");
                        if (!file.empty())
                                dep.insert(file, true);
                }
 
                for (set<string>::const_iterator it2 = it->styles.begin();
                     it2 != it->styles.end(); ++it2) {
-                       string file = findtexfile(*it2, "bst");
+                       FileName const file = findtexfile(*it2, "bst");
                        if (!file.empty())
                                dep.insert(file, true);
                }
@@ -548,7 +559,8 @@ bool LaTeX::runBibTeX(vector<Aux_Info> const & bibtex_info)
                result = true;
 
                string tmp = lyxrc.bibtex_command + " ";
-               tmp += quoteName(onlyFilename(changeExtension(it->aux_file, string())));
+               // onlyFilename() is needed for cygwin
+               tmp += quoteName(onlyFilename(removeExtension(it->aux_file.absFilename())));
                Systemcall one;
                one.startscript(Systemcall::Wait, tmp);
        }
@@ -562,9 +574,10 @@ int LaTeX::scanLogFile(TeXErrors & terr)
        int last_line = -1;
        int line_count = 1;
        int retval = NO_ERRORS;
-       string tmp = onlyFilename(changeExtension(file, ".log"));
+       string tmp = onlyFilename(changeExtension(file.absFilename(), ".log"));
        lyxerr[Debug::LATEX] << "Log file: " << tmp << endl;
-       ifstream ifs(tmp.c_str());
+       FileName const fn = FileName(makeAbsPath(tmp));
+       ifstream ifs(fn.toFilesystemEncoding().c_str());
 
        string token;
        while (getline(ifs, token)) {
@@ -672,10 +685,14 @@ int LaTeX::scanLogFile(TeXErrors & terr)
                                }
                                if (line_count <= 5) {
                                        // FIXME UNICODE
-                                       // We have no idea what the encoding of the log file is
-                                       // (probably pure ascii, but maybe some localized
-                                       // latex compilers or packages exist)
-                                       terr.insertError(line, from_utf8(desc), from_utf8(errstr));
+                                       // We have no idea what the encoding of
+                                       // the log file is.
+                                       // It seems that the output from the
+                                       // latex compiler itself is pure ASCII,
+                                       // but it can include bits from the
+                                       // document, so whatever encoding we
+                                       // assume here it can be wrong.
+                                       terr.insertError(line, from_local8bit(desc), from_local8bit(errstr));
                                        ++num_errors;
                                }
                        }
@@ -727,17 +744,20 @@ void handleFoundFile(string const & ff, DepTable & head)
                // On initial insert we want to do the update at once
                // since this file cannot be a file generated by
                // the latex run.
-               if (fs::exists(foundfile) && !fs::is_directory(foundfile))
-                       head.insert(foundfile, true);
+               FileName const absname(foundfile);
+               if (fs::exists(absname.toFilesystemEncoding()) &&
+                   !fs::is_directory(absname.toFilesystemEncoding()))
+                       head.insert(absname, true);
 
                return;
        }
 
        string const onlyfile = onlyFilename(foundfile);
+       FileName const absname(makeAbsPath(onlyfile));
 
        // (2) foundfile is in the tmpdir
        //     insert it into head
-       if (fs::exists(onlyfile)) {
+       if (fs::exists(absname.toFilesystemEncoding())) {
                static regex unwanted("^.*\\.(aux|log|dvi|bbl|ind|glo)$");
                if (regex_match(onlyfile, unwanted)) {
                        lyxerr[Debug::DEPEND]
@@ -753,13 +773,13 @@ void handleFoundFile(string const & ff, DepTable & head)
                                << "Tmpdir TeX file: "
                                << onlyfile
                                << endl;
-                       head.insert(onlyfile, true);
+                       head.insert(absname, true);
                } else {
                        lyxerr[Debug::DEPEND]
                                << "In tmpdir file:"
                                << onlyfile
                                << endl;
-                       head.insert(onlyfile);
+                       head.insert(absname);
                }
        } else
                lyxerr[Debug::DEPEND]
@@ -776,7 +796,7 @@ void LaTeX::deplog(DepTable & head)
        // files used by the LaTeX run. The files are then entered into the
        // dependency file.
 
-       string const logfile = onlyFilename(changeExtension(file, ".log"));
+       string const logfile = onlyFilename(changeExtension(file.absFilename(), ".log"));
 
        static regex reg1(".*\\([^)]+.*");
        static regex reg2("File: ([^ ]+).*");
@@ -796,7 +816,8 @@ void LaTeX::deplog(DepTable & head)
        // This line is not present if no toc should be created.
        static regex miktexTocReg("\\\\tf@toc=\\\\write.*");
 
-       ifstream ifs(logfile.c_str());
+       FileName const fn(makeAbsPath(logfile));
+       ifstream ifs(fn.toFilesystemEncoding().c_str());
        while (ifs) {
                // Ok, the scanning of files here is not sufficient.
                // Sometimes files are named by "File: xxx" only
@@ -818,6 +839,10 @@ void LaTeX::deplog(DepTable & head)
 
                smatch sub;
 
+               // FIXME UNICODE: We assume that the file names in the log
+               // file are in the file system encoding.
+               token = to_utf8(from_filesystem8bit(token));
+
                if (regex_match(token, sub, reg1)) {
                        static regex reg1_1("\\(([^()]+)");
                        smatch what;
@@ -839,11 +864,11 @@ void LaTeX::deplog(DepTable & head)
                else if (regex_match(token, sub, regnomencl))
                        handleFoundFile(sub.str(1), head);
                else if (regex_match(token, sub, miktexTocReg))
-                       handleFoundFile(changeExtension(file, ".toc"), head);
+                       handleFoundFile(onlyFilename(changeExtension(file.absFilename(), ".toc")), head);
        }
 
        // Make sure that the main .tex file is in the dependancy file.
-       head.insert(onlyFilename(file), true);
+       head.insert(file, true);
 }