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