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