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