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