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