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