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