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