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