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