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