]> git.lyx.org Git - lyx.git/blob - src/LaTeX.C
Enable convertDefault.sh to run even if its executable bit is not set.
[lyx.git] / src / LaTeX.C
1 /**
2  * \file LaTeX.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alfredo Braunstein
7  * \author Lars Gullik Bjønnes
8  * \author Jean-Marc Lasgouttes
9  * \author Angus Leeming
10  * \author Dekel Tsur
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #include <config.h>
16
17 #include "LaTeX.h"
18 #include "bufferlist.h"
19 #include "gettext.h"
20 #include "debug.h"
21 #include "support/filetools.h"
22 #include "support/FileInfo.h"
23 #include "support/tostr.h"
24 #include "support/lstrings.h"
25 #include "support/lyxlib.h"
26 #include "support/systemcall.h"
27 #include "support/os.h"
28 #include "support/path.h"
29
30 #include <boost/regex.hpp>
31
32 #include <fstream>
33 #include <cstdio>  // sscanf
34
35 using namespace lyx::support;
36
37 #ifndef CXX_GLOBAL_CSTD
38 using std::sscanf;
39 #endif
40
41 using std::ifstream;
42 using std::getline;
43 using std::endl;
44 using std::vector;
45 using std::set;
46 using boost::regex;
47 using boost::regex_match;
48
49 #ifndef USE_INCLUDED_STRING
50 using boost::smatch;
51 #else
52 using boost::cmatch;
53 #endif
54
55 // TODO: in no particular order
56 // - get rid of the extern BufferList and the call to
57 //   BufferList::updateIncludedTeXfiles, this should either
58 //   be done before calling LaTeX::funcs or in a completely
59 //   different way.
60 // - the bibtex command options should be supported.
61 // - the makeindex style files should be taken care of with
62 //   the dependency mechanism.
63 // - makeindex commandline options should be supported
64 // - somewhere support viewing of bibtex and makeindex log files.
65 // - we should perhaps also scan the bibtex log file
66 // - we should perhaps also scan the bibtex log file
67
68 extern BufferList bufferlist;
69
70 namespace {
71
72 string runMessage(unsigned int count)
73 {
74         return bformat(_("Waiting for LaTeX run number %1$s"), tostr(count));
75 }
76
77 };
78
79 /*
80  * CLASS TEXERRORS
81  */
82
83 void TeXErrors::insertError(int line, string const & error_desc,
84                             string const & error_text)
85 {
86         Error newerr(line, error_desc, error_text);
87         errors.push_back(newerr);
88 }
89
90
91 bool operator==(Aux_Info const & a, Aux_Info const & o)
92 {
93         return a.aux_file == o.aux_file &&
94                 a.citations == o.citations &&
95                 a.databases == o.databases &&
96                 a.styles == o.styles;
97 }
98
99
100 bool operator!=(Aux_Info const & a, Aux_Info const & o)
101 {
102         return !(a == o);
103 }
104
105
106 /*
107  * CLASS LaTeX
108  */
109
110 LaTeX::LaTeX(string const & latex, LatexRunParams const & rp,
111              string const & f, string const & p)
112         : cmd(latex), file(f), path(p), runparams(rp)
113 {
114         num_errors = 0;
115         depfile = file + ".dep";
116         if (prefixIs(cmd, "pdf")) { // Do we use pdflatex ?
117                 depfile += "-pdf";
118                 output_file = ChangeExtension(file,".pdf");
119         } else {
120                 output_file = ChangeExtension(file,".dvi");
121         }
122 }
123
124
125 void LaTeX::deleteFilesOnError() const
126 {
127         // currently just a dummy function.
128
129         // What files do we have to delete?
130
131         // This will at least make latex do all the runs
132         unlink(depfile);
133
134         // but the reason for the error might be in a generated file...
135
136         string const ofname = OnlyFilename(file);
137
138         // bibtex file
139         string const bbl = ChangeExtension(ofname, ".bbl");
140         unlink(bbl);
141
142         // makeindex file
143         string const ind = ChangeExtension(ofname, ".ind");
144         unlink(ind);
145
146         // Also remove the aux file
147         string const aux = ChangeExtension(ofname, ".aux");
148         unlink(aux);
149 }
150
151
152 int LaTeX::run(TeXErrors & terr)
153         // We know that this function will only be run if the lyx buffer
154         // has been changed. We also know that a newly written .tex file
155         // is always different from the previous one because of the date
156         // in it. However it seems safe to run latex (at least) on time each
157         // time the .tex file changes.
158 {
159         int scanres = NO_ERRORS;
160         unsigned int count = 0; // number of times run
161         num_errors = 0; // just to make sure.
162         unsigned int const MAX_RUN = 6;
163         DepTable head; // empty head
164         bool rerun = false; // rerun requested
165
166         // The class LaTeX does not know the temp path.
167         bufferlist.updateIncludedTeXfiles(getcwd(), runparams);
168
169         // Never write the depfile if an error was encountered.
170
171         // 0
172         // first check if the file dependencies exist:
173         //     ->If it does exist
174         //             check if any of the files mentioned in it have
175         //             changed (done using a checksum).
176         //                 -> if changed:
177         //                        run latex once and
178         //                        remake the dependency file
179         //                 -> if not changed:
180         //                        just return there is nothing to do for us.
181         //     ->if it doesn't exist
182         //             make it and
183         //             run latex once (we need to run latex once anyway) and
184         //             remake the dependency file.
185         //
186
187         FileInfo fi(depfile);
188         bool had_depfile = fi.exist();
189         bool run_bibtex = false;
190         string aux_file = OnlyFilename(ChangeExtension(file, "aux"));
191
192         if (had_depfile) {
193                 lyxerr[Debug::DEPEND] << "Dependency file exists" << endl;
194                 // Read the dep file:
195                 had_depfile = head.read(depfile);
196         }
197
198         if (had_depfile) {
199                 // Update the checksums
200                 head.update();
201                 // Can't just check if anything has changed because it might have aborted
202                 // on error last time... in which cas we need to re-run latex
203                 // and collect the error messages (even if they are the same).
204                 if (!FileInfo(output_file).exist()) {
205                         lyxerr[Debug::DEPEND]
206                                 << "re-running LaTeX because output file doesn't exist." << endl;
207                 } else if (!head.sumchange()) {
208                         lyxerr[Debug::DEPEND] << "return no_change" << endl;
209                         return NO_CHANGE;
210                 } else {
211                         lyxerr[Debug::DEPEND]
212                                 << "Dependency file has changed" << endl;
213                 }
214
215                 if (head.extchanged(".bib") || head.extchanged(".bst"))
216                         run_bibtex = true;
217         } else
218                 lyxerr[Debug::DEPEND]
219                         << "Dependency file does not exist, or has wrong format" << endl;
220
221         /// We scan the aux file even when had_depfile = false,
222         /// because we can run pdflatex on the file after running latex on it,
223         /// in which case we will not need to run bibtex again.
224         vector<Aux_Info> bibtex_info_old;
225         if (!run_bibtex)
226                 bibtex_info_old = scanAuxFiles(aux_file);
227
228         ++count;
229         lyxerr[Debug::LATEX] << "Run #" << count << endl;
230         message(runMessage(count));
231         
232         startscript();
233         scanres = scanLogFile(terr);
234         if (scanres & ERROR_RERUN) {
235                 lyxerr[Debug::LATEX] << "Rerunning LaTeX" << endl;
236                 startscript();
237                 scanres = scanLogFile(terr);
238         }
239
240         if (scanres & ERRORS) {
241                 deleteFilesOnError();
242                 return scanres; // return on error
243         }
244
245         vector<Aux_Info> const bibtex_info = scanAuxFiles(aux_file);
246         if (!run_bibtex && bibtex_info_old != bibtex_info)
247                 run_bibtex = true;
248
249         // update the dependencies.
250         deplog(head); // reads the latex log
251         head.update();
252
253         // 0.5
254         // At this point we must run external programs if needed.
255         // makeindex will be run if a .idx file changed or was generated.
256         // And if there were undefined citations or changes in references
257         // the .aux file is checked for signs of bibtex. Bibtex is then run
258         // if needed.
259
260         // run makeindex
261         if (head.haschanged(OnlyFilename(ChangeExtension(file, ".idx")))) {
262                 // no checks for now
263                 lyxerr[Debug::LATEX] << "Running MakeIndex." << endl;
264                 message(_("Running MakeIndex."));
265                 rerun = runMakeIndex(OnlyFilename(ChangeExtension(file, ".idx")));
266         }
267
268         // run bibtex
269         // if (scanres & UNDEF_CIT || scanres & RERUN || run_bibtex)
270         if (scanres & UNDEF_CIT || run_bibtex) {
271                 // Here we must scan the .aux file and look for
272                 // "\bibdata" and/or "\bibstyle". If one of those
273                 // tags is found -> run bibtex and set rerun = true;
274                 // no checks for now
275                 lyxerr[Debug::LATEX] << "Running BibTeX." << endl;
276                 message(_("Running BibTeX."));
277                 updateBibtexDependencies(head, bibtex_info);
278                 rerun |= runBibTeX(bibtex_info);
279         } else if (!had_depfile) {
280                 /// If we run pdflatex on the file after running latex on it,
281                 /// then we do not need to run bibtex, but we do need to
282                 /// insert the .bib and .bst files into the .dep-pdf file.
283                 updateBibtexDependencies(head, bibtex_info);
284         }
285
286         // 1
287         // we know on this point that latex has been run once (or we just
288         // returned) and the question now is to decide if we need to run
289         // it any more. This is done by asking if any of the files in the
290         // dependency file has changed. (remember that the checksum for
291         // a given file is reported to have changed if it just was created)
292         //     -> if changed or rerun == true:
293         //             run latex once more and
294         //             update the dependency structure
295         //     -> if not changed:
296         //             we does nothing at this point
297         //
298         if (rerun || head.sumchange()) {
299                 rerun = false;
300                 ++count;
301                 lyxerr[Debug::DEPEND]
302                         << "Dep. file has changed or rerun requested" << endl;
303                 lyxerr[Debug::LATEX]
304                         << "Run #" << count << endl;
305                 message(runMessage(count));
306                 startscript();
307                 scanres = scanLogFile(terr);
308                 if (scanres & ERRORS) {
309                         deleteFilesOnError();
310                         return scanres; // return on error
311                 }
312
313                 // update the depedencies
314                 deplog(head); // reads the latex log
315                 head.update();
316         } else {
317                 lyxerr[Debug::DEPEND] << "Dep. file has NOT changed" << endl;
318         }
319
320         // 1.5
321         // The inclusion of files generated by external programs like
322         // makeindex or bibtex might have done changes to pagenumbereing,
323         // etc. And because of this we must run the external programs
324         // again to make sure everything is redone correctly.
325         // Also there should be no need to run the external programs any
326         // more after this.
327
328         // run makeindex if the <file>.idx has changed or was generated.
329         if (head.haschanged(OnlyFilename(ChangeExtension(file, ".idx")))) {
330                 // no checks for now
331                 lyxerr[Debug::LATEX] << "Running MakeIndex." << endl;
332                 message(_("Running MakeIndex."));
333                 rerun = runMakeIndex(OnlyFilename(ChangeExtension(file, ".idx")));
334         }
335
336         // 2
337         // we will only run latex more if the log file asks for it.
338         // or if the sumchange() is true.
339         //     -> rerun asked for:
340         //             run latex and
341         //             remake the dependency file
342         //             goto 2 or return if max runs are reached.
343         //     -> rerun not asked for:
344         //             just return (fall out of bottom of func)
345         //
346         while ((head.sumchange() || rerun || (scanres & RERUN))
347                && count < MAX_RUN) {
348                 // Yes rerun until message goes away, or until
349                 // MAX_RUNS are reached.
350                 rerun = false;
351                 ++count;
352                 lyxerr[Debug::LATEX] << "Run #" << count << endl;
353                 message(runMessage(count));
354                 startscript();
355                 scanres = scanLogFile(terr);
356                 if (scanres & ERRORS) {
357                         deleteFilesOnError();
358                         return scanres; // return on error
359                 }
360
361                 // keep this updated
362                 head.update();
363         }
364
365         // Write the dependencies to file.
366         head.write(depfile);
367         lyxerr[Debug::LATEX] << "Done." << endl;
368         return scanres;
369 }
370
371
372 int LaTeX::startscript()
373 {
374 #ifndef __EMX__
375         string tmp = cmd + ' ' + QuoteName(file) + " > /dev/null";
376 #else // cmd.exe (OS/2) causes SYS0003 error at "/dev/null"
377         string tmp = cmd + ' ' + file + " > nul";
378 #endif
379         Systemcall one;
380         return one.startscript(Systemcall::Wait, tmp);
381 }
382
383
384 bool LaTeX::runMakeIndex(string const & f)
385 {
386         lyxerr[Debug::LATEX] << "idx file has been made,"
387                 " running makeindex on file "
388                              <<  f << endl;
389
390         // It should be possible to set the switches for makeindex
391         // sorting style and such. It would also be very convenient
392         // to be able to make style files from within LyX. This has
393         // to come for a later time.
394         string tmp = "makeindex -c -q ";
395         tmp += f;
396         Systemcall one;
397         one.startscript(Systemcall::Wait, tmp);
398         return true;
399 }
400
401
402 vector<Aux_Info> const
403 LaTeX::scanAuxFiles(string const & file)
404 {
405         vector<Aux_Info> result;
406
407         result.push_back(scanAuxFile(file));
408
409         for (int i = 1; i < 1000; ++i) {
410                 string file2 = ChangeExtension(file, "") + '.' + tostr(i)
411                         + ".aux";
412                 FileInfo fi(file2);
413                 if (!fi.exist())
414                         break;
415                 result.push_back(scanAuxFile(file2));
416         }
417         return result;
418 }
419
420
421 Aux_Info const LaTeX::scanAuxFile(string const & file)
422 {
423         Aux_Info result;
424         result.aux_file = file;
425         scanAuxFile(file, result);
426         return result;
427 }
428
429
430 void LaTeX::scanAuxFile(string const & file, Aux_Info & aux_info)
431 {
432         lyxerr[Debug::LATEX] << "Scanning aux file: " << file << endl;
433
434         ifstream ifs(file.c_str());
435         string token;
436         regex reg1("\\\\citation\\{([^}]+)\\}");
437         regex reg2("\\\\bibdata\\{([^}]+)\\}");
438         regex reg3("\\\\bibstyle\\{([^}]+)\\}");
439         regex reg4("\\\\@input\\{([^}]+)\\}");
440
441         while (getline(ifs, token)) {
442                 token = rtrim(token, "\r");
443 #ifndef USE_INCLUDED_STRING
444                 smatch sub;
445 #else
446                 cmatch sub;
447 #endif
448                 if (regex_match(STRCONV(token), sub, reg1)) {
449                         string data = STRCONV(sub.str(1));
450                         while (!data.empty()) {
451                                 string citation;
452                                 data = split(data, citation, ',');
453                                 lyxerr[Debug::LATEX] << "Citation: "
454                                                      << citation << endl;
455                                 aux_info.citations.insert(citation);
456                         }
457                 } else if (regex_match(STRCONV(token), sub, reg2)) {
458                         string data = sub.STRCONV(str(1));
459                         // data is now all the bib files separated by ','
460                         // get them one by one and pass them to the helper
461                         while (!data.empty()) {
462                                 string database;
463                                 data = split(data, database, ',');
464                                 database = ChangeExtension(database, "bib");
465                                 lyxerr[Debug::LATEX] << "BibTeX database: `"
466                                                      << database << '\'' << endl;
467                                 aux_info.databases.insert(database);
468                         }
469                 } else if (regex_match(STRCONV(token), sub, reg3)) {
470                         string style = STRCONV(sub.str(1));
471                         // token is now the style file
472                         // pass it to the helper
473                         style = ChangeExtension(style, "bst");
474                         lyxerr[Debug::LATEX] << "BibTeX style: `"
475                                              << style << '\'' << endl;
476                         aux_info.styles.insert(style);
477                 } else if (regex_match(STRCONV(token), sub, reg4)) {
478                         string const file2 = STRCONV(sub.str(1));
479                         scanAuxFile(file2, aux_info);
480                 }
481         }
482 }
483
484
485 void LaTeX::updateBibtexDependencies(DepTable & dep,
486                                      vector<Aux_Info> const & bibtex_info)
487 {
488         // Since a run of Bibtex mandates more latex runs it is ok to
489         // remove all ".bib" and ".bst" files.
490         dep.remove_files_with_extension(".bib");
491         dep.remove_files_with_extension(".bst");
492         //string aux = OnlyFilename(ChangeExtension(file, ".aux"));
493
494         for (vector<Aux_Info>::const_iterator it = bibtex_info.begin();
495              it != bibtex_info.end(); ++it) {
496                 for (set<string>::const_iterator it2 = it->databases.begin();
497                      it2 != it->databases.end(); ++it2) {
498                         string file = findtexfile(*it2, "bib");
499                         if (!file.empty())
500                                 dep.insert(file, true);
501                 }
502
503                 for (set<string>::const_iterator it2 = it->styles.begin();
504                      it2 != it->styles.end(); ++it2) {
505                         string file = findtexfile(*it2, "bst");
506                         if (!file.empty())
507                                 dep.insert(file, true);
508                 }
509         }
510 }
511
512
513 bool LaTeX::runBibTeX(vector<Aux_Info> const & bibtex_info)
514 {
515         bool result = false;
516         for (vector<Aux_Info>::const_iterator it = bibtex_info.begin();
517              it != bibtex_info.end(); ++it) {
518                 if (it->databases.empty())
519                         continue;
520                 result = true;
521
522                 string tmp = "bibtex ";
523                 tmp += OnlyFilename(ChangeExtension(it->aux_file, string()));
524                 Systemcall one;
525                 one.startscript(Systemcall::Wait, tmp);
526         }
527         // Return whether bibtex was run
528         return result;
529 }
530
531
532 int LaTeX::scanLogFile(TeXErrors & terr)
533 {
534         int last_line = -1;
535         int line_count = 1;
536         int retval = NO_ERRORS;
537         string tmp = OnlyFilename(ChangeExtension(file, ".log"));
538         lyxerr[Debug::LATEX] << "Log file: " << tmp << endl;
539         ifstream ifs(tmp.c_str());
540
541         string token;
542         while (getline(ifs, token)) {
543                 lyxerr[Debug::LATEX] << "Log line: " << token << endl;
544
545                 if (token.empty())
546                         continue;
547
548                 if (prefixIs(token, "LaTeX Warning:")) {
549                         // Here shall we handle different
550                         // types of warnings
551                         retval |= LATEX_WARNING;
552                         lyxerr[Debug::LATEX] << "LaTeX Warning." << endl;
553                         if (contains(token, "Rerun to get cross-references")) {
554                                 retval |= RERUN;
555                                 lyxerr[Debug::LATEX]
556                                         << "We should rerun." << endl;
557                         } else if (contains(token, "Citation")
558                                    && contains(token, "on page")
559                                    && contains(token, "undefined")) {
560                                 retval |= UNDEF_CIT;
561                         }
562                 } else if (prefixIs(token, "Package")) {
563                         // Package warnings
564                         retval |= PACKAGE_WARNING;
565                         if (contains(token, "natbib Warning:")) {
566                                 // Natbib warnings
567                                 if (contains(token, "Citation")
568                                     && contains(token, "on page")
569                                     && contains(token, "undefined")) {
570                                         retval |= UNDEF_CIT;
571                                 }
572                         } else if (contains(token, "run BibTeX")) {
573                                 retval |= UNDEF_CIT;
574                         } else if (contains(token, "Rerun LaTeX") ||
575                                    contains(token, "Rerun to get")) {
576                                 // at least longtable.sty and bibtopic.sty
577                                 // might use this.
578                                 lyxerr[Debug::LATEX]
579                                         << "We should rerun." << endl;
580                                 retval |= RERUN;
581                         }
582                 } else if (token[0] == '(') {
583                         if (contains(token, "Rerun LaTeX") ||
584                             contains(token, "Rerun to get")) {
585                                 // Used by natbib
586                                 lyxerr[Debug::LATEX]
587                                         << "We should rerun." << endl;
588                                 retval |= RERUN;
589                         }
590                 } else if (prefixIs(token, "! ")) {
591                         // Ok, we have something that looks like a TeX Error
592                         // but what do we really have.
593
594                         // Just get the error description:
595                         string desc(token, 2);
596                         if (contains(token, "LaTeX Error:"))
597                                 retval |= LATEX_ERROR;
598                         // get the next line
599                         string tmp;
600                         int count = 0;
601                         do {
602                                 if (!getline(ifs, tmp))
603                                         break;
604                                 if (++count > 10)
605                                         break;
606                         } while (!prefixIs(tmp, "l."));
607                         if (prefixIs(tmp, "l.")) {
608                                 // we have a latex error
609                                 retval |=  TEX_ERROR;
610                                 if (contains(desc, "Package babel Error: You haven't defined the language"))
611                                         retval |= ERROR_RERUN;
612                                 // get the line number:
613                                 int line = 0;
614                                 sscanf(tmp.c_str(), "l.%d", &line);
615                                 // get the rest of the message:
616                                 string errstr(tmp, tmp.find(' '));
617                                 errstr += '\n';
618                                 getline(ifs, tmp);
619                                 while (!contains(errstr, "l.")
620                                        && !tmp.empty()
621                                        && !prefixIs(tmp, "! ")
622                                        && !contains(tmp, "(job aborted")) {
623                                         errstr += tmp;
624                                         errstr += "\n";
625                                         getline(ifs, tmp);
626                                 }
627                                 lyxerr[Debug::LATEX]
628                                         << "line: " << line << '\n'
629                                         << "Desc: " << desc << '\n'
630                                         << "Text: " << errstr << endl;
631                                 if (line == last_line)
632                                         ++line_count;
633                                 else {
634                                         line_count = 1;
635                                         last_line = line;
636                                 }
637                                 if (line_count <= 5) {
638                                         terr.insertError(line, desc, errstr);
639                                         ++num_errors;
640                                 }
641                         }
642                 } else {
643                         // information messages, TeX warnings and other
644                         // warnings we have not caught earlier.
645                         if (prefixIs(token, "Overfull ")) {
646                                 retval |= TEX_WARNING;
647                         } else if (prefixIs(token, "Underfull ")) {
648                                 retval |= TEX_WARNING;
649                         } else if (contains(token, "Rerun to get citations")) {
650                                 // Natbib seems to use this.
651                                 retval |= UNDEF_CIT;
652                         } else if (contains(token, "No pages of output")) {
653                                 // A dvi file was not created
654                                 retval |= NO_OUTPUT;
655                         } else if (contains(token, "That makes 100 errors")) {
656                                 // More than 100 errors were reprted
657                                 retval |= TOO_MANY_ERRORS;
658                         }
659                 }
660         }
661         lyxerr[Debug::LATEX] << "Log line: " << token << endl;
662         return retval;
663 }
664
665
666 void LaTeX::deplog(DepTable & head)
667 {
668         // This function reads the LaTeX log file end extracts all the external
669         // files used by the LaTeX run. The files are then entered into the
670         // dependency file.
671
672         string const logfile = OnlyFilename(ChangeExtension(file, ".log"));
673
674         regex reg1("\\)* *\\(([^ )]+).*");
675         regex reg2("File: ([^ ]+).*");
676         regex reg3("No file ([^ ]+)\\..*");
677         regex reg4("\\\\openout[0-9]+.*=.*`([^ ]+)'\\..*");
678         // If an index should be created, MikTex does not write a line like
679         //    \openout# = 'sample,idx'.
680         // but intstead only a line like this into the log:
681         //   Writing index file sample.idx
682         regex reg5("Writing index file ([^ ]+).*");
683         regex unwanted("^.*\\.(aux|log|dvi|bbl|ind|glo)$");
684
685         ifstream ifs(logfile.c_str());
686         while (ifs) {
687                 // Ok, the scanning of files here is not sufficient.
688                 // Sometimes files are named by "File: xxx" only
689                 // So I think we should use some regexps to find files instead.
690                 // "(\([^ ]+\)"   should match the "(file " variant
691                 // "File: \([^ ]+\)" should match the "File: file" variant
692                 string foundfile;
693                 string token;
694                 getline(ifs, token);
695                 token = rtrim(token, "\r");
696                 if (token.empty()) continue;
697
698 #ifndef USE_INCLUDED_STRING
699                 smatch sub;
700 #else
701                 cmatch sub;
702 #endif
703                 if (regex_match(STRCONV(token), sub, reg1)) {
704                         foundfile = STRCONV(sub.str(1));
705                 } else if (regex_match(STRCONV(token), sub, reg2)) {
706                         foundfile = STRCONV(sub.str(1));
707                 } else if (regex_match(STRCONV(token), sub, reg3)) {
708                         foundfile = STRCONV(sub.str(1));
709                 } else if (regex_match(STRCONV(token), sub, reg4)) {
710                         foundfile = STRCONV(sub.str(1));
711                 } else if (regex_match(STRCONV(token), sub, reg5)) {
712                         foundfile = STRCONV(sub.str(1));
713                 } else {
714                         continue;
715                 }
716
717                 // convert from native os path to unix path
718                 foundfile = os::internal_path(foundfile);
719
720                 lyxerr[Debug::DEPEND] << "Found file: "
721                                       << foundfile << endl;
722
723                 // Ok now we found a file.
724                 // Now we should make sure that this is a file that we can
725                 // access through the normal paths.
726                 // We will not try any fancy search methods to
727                 // find the file.
728
729                 // (1) foundfile is an
730                 //     absolute path and should
731                 //     be inserted.
732                 if (AbsolutePath(foundfile)) {
733                         lyxerr[Debug::DEPEND] << "AbsolutePath file: "
734                                               << foundfile << endl;
735                         // On initial insert we want to do the update at once
736                         // since this file can not be a file generated by
737                         // the latex run.
738                         if (FileInfo(foundfile).exist())
739                                 head.insert(foundfile, true);
740                 }
741
742                 // (2) foundfile is in the tmpdir
743                 //     insert it into head
744                 else if (FileInfo(OnlyFilename(foundfile)).exist()) {
745                         if (regex_match(STRCONV(foundfile), unwanted)) {
746                                 lyxerr[Debug::DEPEND]
747                                         << "We don't want "
748                                         << OnlyFilename(foundfile)
749                                         << " in the dep file"
750                                         << endl;
751                         } else if (suffixIs(foundfile, ".tex")) {
752                                 // This is a tex file generated by LyX
753                                 // and latex is not likely to change this
754                                 // during its runs.
755                                 lyxerr[Debug::DEPEND]
756                                         << "Tmpdir TeX file: "
757                                         << OnlyFilename(foundfile)
758                                         << endl;
759                                 head.insert(foundfile, true);
760                         } else {
761                                 lyxerr[Debug::DEPEND]
762                                         << "In tmpdir file:"
763                                         << OnlyFilename(foundfile)
764                                         << endl;
765                                 head.insert(OnlyFilename(foundfile));
766                         }
767                 } else
768                         lyxerr[Debug::DEPEND]
769                                 << "Not a file or we are unable to find it."
770                                 << endl;
771         }
772
773         // Make sure that the main .tex file is in the dependancy file.
774         head.insert(OnlyFilename(file), true);
775 }