]> git.lyx.org Git - lyx.git/blob - src/LaTeX.cpp
prepare Qt 5.6 builds
[lyx.git] / src / LaTeX.cpp
1 /**
2  * \file LaTeX.cpp
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  * \author Jürgen Spitzmüller
12  *
13  * Full author contact details are available in file CREDITS.
14  */
15
16 #include <config.h>
17
18 #include "BufferList.h"
19 #include "LaTeX.h"
20 #include "LyXRC.h"
21 #include "DepTable.h"
22
23 #include "support/debug.h"
24 #include "support/convert.h"
25 #include "support/FileName.h"
26 #include "support/filetools.h"
27 #include "support/gettext.h"
28 #include "support/lstrings.h"
29 #include "support/Systemcall.h"
30 #include "support/os.h"
31
32 #include "support/regex.h"
33
34 #include <fstream>
35 #include <stack>
36
37
38 using namespace std;
39 using namespace lyx::support;
40
41 namespace lyx {
42
43 namespace os = support::os;
44
45 // TODO: in no particular order
46 // - get rid of the call to
47 //   BufferList::updateIncludedTeXfiles, this should either
48 //   be done before calling LaTeX::funcs or in a completely
49 //   different way.
50 // - the makeindex style files should be taken care of with
51 //   the dependency mechanism.
52
53 namespace {
54
55 docstring runMessage(unsigned int count)
56 {
57         return bformat(_("Waiting for LaTeX run number %1$d"), count);
58 }
59
60 } // anon namespace
61
62 /*
63  * CLASS TEXERRORS
64  */
65
66 void TeXErrors::insertError(int line, docstring const & error_desc,
67                             docstring const & error_text,
68                             string const & child_name)
69 {
70         Error newerr(line, error_desc, error_text, child_name);
71         errors.push_back(newerr);
72 }
73
74
75 bool operator==(AuxInfo const & a, AuxInfo const & o)
76 {
77         return a.aux_file == o.aux_file
78                 && a.citations == o.citations
79                 && a.databases == o.databases
80                 && a.styles == o.styles;
81 }
82
83
84 bool operator!=(AuxInfo const & a, AuxInfo const & o)
85 {
86         return !(a == o);
87 }
88
89
90 /*
91  * CLASS LaTeX
92  */
93
94 LaTeX::LaTeX(string const & latex, OutputParams const & rp,
95              FileName const & f, string const & p, string const & lp,
96              bool const clean_start)
97         : cmd(latex), file(f), path(p), lpath(lp), runparams(rp), biber(false)
98 {
99         num_errors = 0;
100         if (prefixIs(cmd, "pdf")) { // Do we use pdflatex ?
101                 depfile = FileName(file.absFileName() + ".dep-pdf");
102                 output_file =
103                         FileName(changeExtension(file.absFileName(), ".pdf"));
104         } else {
105                 depfile = FileName(file.absFileName() + ".dep");
106                 output_file =
107                         FileName(changeExtension(file.absFileName(), ".dvi"));
108         }
109         if (clean_start)
110                 removeAuxiliaryFiles();
111 }
112
113
114 void LaTeX::removeAuxiliaryFiles() const
115 {
116         // Note that we do not always call this function when there is an error.
117         // For example, if there is an error but an output file is produced we
118         // still would like to output (export/view) the file.
119
120         // What files do we have to delete?
121
122         // This will at least make latex do all the runs
123         depfile.removeFile();
124
125         // but the reason for the error might be in a generated file...
126
127         // bibtex file
128         FileName const bbl(changeExtension(file.absFileName(), ".bbl"));
129         bbl.removeFile();
130
131         // biber file
132         FileName const bcf(changeExtension(file.absFileName(), ".bcf"));
133         bcf.removeFile();
134
135         // makeindex file
136         FileName const ind(changeExtension(file.absFileName(), ".ind"));
137         ind.removeFile();
138
139         // nomencl file
140         FileName const nls(changeExtension(file.absFileName(), ".nls"));
141         nls.removeFile();
142
143         // nomencl file (old version of the package)
144         FileName const gls(changeExtension(file.absFileName(), ".gls"));
145         gls.removeFile();
146
147         // Also remove the aux file
148         FileName const aux(changeExtension(file.absFileName(), ".aux"));
149         aux.removeFile();
150
151         // Remove the output file, which is often generated even if error
152         output_file.removeFile();
153 }
154
155
156 int LaTeX::run(TeXErrors & terr)
157         // We know that this function will only be run if the lyx buffer
158         // has been changed. We also know that a newly written .tex file
159         // is always different from the previous one because of the date
160         // in it. However it seems safe to run latex (at least) one time
161         // each time the .tex file changes.
162 {
163         int scanres = NO_ERRORS;
164         int bscanres = NO_ERRORS;
165         unsigned int count = 0; // number of times run
166         num_errors = 0; // just to make sure.
167         unsigned int const MAX_RUN = 6;
168         DepTable head; // empty head
169         bool rerun = false; // rerun requested
170
171         // The class LaTeX does not know the temp path.
172         theBufferList().updateIncludedTeXfiles(FileName::getcwd().absFileName(),
173                 runparams);
174
175         // 0
176         // first check if the file dependencies exist:
177         //     ->If it does exist
178         //             check if any of the files mentioned in it have
179         //             changed (done using a checksum).
180         //                 -> if changed:
181         //                        run latex once and
182         //                        remake the dependency file
183         //                 -> if not changed:
184         //                        just return there is nothing to do for us.
185         //     ->if it doesn't exist
186         //             make it and
187         //             run latex once (we need to run latex once anyway) and
188         //             remake the dependency file.
189         //
190
191         bool had_depfile = depfile.exists();
192         bool run_bibtex = false;
193         FileName const aux_file(changeExtension(file.absFileName(), ".aux"));
194
195         if (had_depfile) {
196                 LYXERR(Debug::DEPEND, "Dependency file exists");
197                 // Read the dep file:
198                 had_depfile = head.read(depfile);
199         }
200
201         if (had_depfile) {
202                 // Update the checksums
203                 head.update();
204                 // Can't just check if anything has changed because it might
205                 // have aborted on error last time... in which cas we need
206                 // to re-run latex and collect the error messages
207                 // (even if they are the same).
208                 if (!output_file.exists()) {
209                         LYXERR(Debug::DEPEND,
210                                 "re-running LaTeX because output file doesn't exist.");
211                 } else if (!head.sumchange()) {
212                         LYXERR(Debug::DEPEND, "return no_change");
213                         return NO_CHANGE;
214                 } else {
215                         LYXERR(Debug::DEPEND, "Dependency file has changed");
216                 }
217
218                 if (head.extchanged(".bib") || head.extchanged(".bst"))
219                         run_bibtex = true;
220         } else
221                 LYXERR(Debug::DEPEND,
222                         "Dependency file does not exist, or has wrong format");
223
224         /// We scan the aux file even when had_depfile = false,
225         /// because we can run pdflatex on the file after running latex on it,
226         /// in which case we will not need to run bibtex again.
227         vector<AuxInfo> bibtex_info_old;
228         if (!run_bibtex)
229                 bibtex_info_old = scanAuxFiles(aux_file);
230
231         ++count;
232         LYXERR(Debug::LATEX, "Run #" << count);
233         message(runMessage(count));
234
235         int exit_code = startscript();
236
237         scanres = scanLogFile(terr);
238         if (scanres & ERROR_RERUN) {
239                 LYXERR(Debug::LATEX, "Rerunning LaTeX");
240                 terr.clearErrors();
241                 exit_code = startscript();
242                 scanres = scanLogFile(terr);
243         }
244
245         vector<AuxInfo> const bibtex_info = scanAuxFiles(aux_file);
246         if (!run_bibtex && bibtex_info_old != bibtex_info)
247                 run_bibtex = true;
248
249         // update the dependencies.
250         deplog(head); // reads the latex log
251         head.update();
252
253         // 1
254         // At this point we must run external programs if needed.
255         // makeindex will be run if a .idx file changed or was generated.
256         // And if there were undefined citations or changes in references
257         // the .aux file is checked for signs of bibtex. Bibtex is then run
258         // if needed.
259
260         // memoir (at least) writes an empty *idx file in the first place.
261         // A second latex run is needed.
262         FileName const idxfile(changeExtension(file.absFileName(), ".idx"));
263         rerun = idxfile.exists() && idxfile.isFileEmpty();
264
265         // run makeindex
266         if (head.haschanged(idxfile)) {
267                 // no checks for now
268                 LYXERR(Debug::LATEX, "Running MakeIndex.");
269                 message(_("Running Index Processor."));
270                 // onlyFileName() is needed for cygwin
271                 rerun |= runMakeIndex(onlyFileName(idxfile.absFileName()),
272                                 runparams);
273         }
274         FileName const nlofile(changeExtension(file.absFileName(), ".nlo"));
275         // If all nomencl entries are removed, nomencl writes an empty nlo file.
276         // DepTable::hasChanged() returns false in this case, since it does not
277         // distinguish empty files from non-existing files. This is why we need
278         // the extra checks here (to trigger a rerun). Cf. discussions in #8905.
279         // FIXME: Sort out the real problem in DepTable.
280         if (head.haschanged(nlofile) || (nlofile.exists() && nlofile.isFileEmpty()))
281                 rerun |= runMakeIndexNomencl(file, ".nlo", ".nls");
282         FileName const glofile(changeExtension(file.absFileName(), ".glo"));
283         if (head.haschanged(glofile))
284                 rerun |= runMakeIndexNomencl(file, ".glo", ".gls");
285
286         // check if we're using biber instead of bibtex
287         // biber writes no info to the aux file, so we just check
288         // if a bcf file exists (and if it was updated)
289         FileName const bcffile(changeExtension(file.absFileName(), ".bcf"));
290         biber |= head.exist(bcffile);
291
292         // run bibtex
293         // if (scanres & UNDEF_CIT || scanres & RERUN || run_bibtex)
294         if (scanres & UNDEF_CIT || run_bibtex) {
295                 // Here we must scan the .aux file and look for
296                 // "\bibdata" and/or "\bibstyle". If one of those
297                 // tags is found -> run bibtex and set rerun = true;
298                 // no checks for now
299                 LYXERR(Debug::LATEX, "Running BibTeX.");
300                 message(_("Running BibTeX."));
301                 updateBibtexDependencies(head, bibtex_info);
302                 rerun |= runBibTeX(bibtex_info, runparams);
303                 FileName const blgfile(changeExtension(file.absFileName(), ".blg"));
304                 if (blgfile.exists())
305                         bscanres = scanBlgFile(head, terr);
306         } else if (!had_depfile) {
307                 /// If we run pdflatex on the file after running latex on it,
308                 /// then we do not need to run bibtex, but we do need to
309                 /// insert the .bib and .bst files into the .dep-pdf file.
310                 updateBibtexDependencies(head, bibtex_info);
311         }
312
313         // 2
314         // we know on this point that latex has been run once (or we just
315         // returned) and the question now is to decide if we need to run
316         // it any more. This is done by asking if any of the files in the
317         // dependency file has changed. (remember that the checksum for
318         // a given file is reported to have changed if it just was created)
319         //     -> if changed or rerun == true:
320         //             run latex once more and
321         //             update the dependency structure
322         //     -> if not changed:
323         //             we do nothing at this point
324         //
325         if (rerun || head.sumchange()) {
326                 rerun = false;
327                 ++count;
328                 LYXERR(Debug::DEPEND, "Dep. file has changed or rerun requested");
329                 LYXERR(Debug::LATEX, "Run #" << count);
330                 message(runMessage(count));
331                 startscript();
332                 scanres = scanLogFile(terr);
333
334                 // update the depedencies
335                 deplog(head); // reads the latex log
336                 head.update();
337         } else {
338                 LYXERR(Debug::DEPEND, "Dep. file has NOT changed");
339         }
340         
341         // 3
342         // rerun bibtex?
343         // Complex bibliography packages such as Biblatex require
344         // an additional bibtex cycle sometimes.
345         if (scanres & UNDEF_CIT) {
346                 // Here we must scan the .aux file and look for
347                 // "\bibdata" and/or "\bibstyle". If one of those
348                 // tags is found -> run bibtex and set rerun = true;
349                 // no checks for now
350                 LYXERR(Debug::LATEX, "Running BibTeX.");
351                 message(_("Running BibTeX."));
352                 updateBibtexDependencies(head, bibtex_info);
353                 rerun |= runBibTeX(bibtex_info, runparams);
354                 FileName const blgfile(changeExtension(file.absFileName(), ".blg"));
355                 if (blgfile.exists())
356                         bscanres = scanBlgFile(head, terr);
357         }
358
359         // 4
360         // The inclusion of files generated by external programs such as
361         // makeindex or bibtex might have done changes to pagenumbering,
362         // etc. And because of this we must run the external programs
363         // again to make sure everything is redone correctly.
364         // Also there should be no need to run the external programs any
365         // more after this.
366
367         // run makeindex if the <file>.idx has changed or was generated.
368         if (head.haschanged(idxfile)) {
369                 // no checks for now
370                 LYXERR(Debug::LATEX, "Running MakeIndex.");
371                 message(_("Running Index Processor."));
372                 // onlyFileName() is needed for cygwin
373                 rerun = runMakeIndex(onlyFileName(changeExtension(
374                                 file.absFileName(), ".idx")), runparams);
375         }
376
377         // I am not pretty sure if need this twice.
378         if (head.haschanged(nlofile))
379                 rerun |= runMakeIndexNomencl(file, ".nlo", ".nls");
380         if (head.haschanged(glofile))
381                 rerun |= runMakeIndexNomencl(file, ".glo", ".gls");
382
383         // 5
384         // we will only run latex more if the log file asks for it.
385         // or if the sumchange() is true.
386         //     -> rerun asked for:
387         //             run latex and
388         //             remake the dependency file
389         //             goto 2 or return if max runs are reached.
390         //     -> rerun not asked for:
391         //             just return (fall out of bottom of func)
392         //
393         while ((head.sumchange() || rerun || (scanres & RERUN))
394                && count < MAX_RUN) {
395                 // Yes rerun until message goes away, or until
396                 // MAX_RUNS are reached.
397                 rerun = false;
398                 ++count;
399                 LYXERR(Debug::LATEX, "Run #" << count);
400                 message(runMessage(count));
401                 startscript();
402                 scanres = scanLogFile(terr);
403
404                 // keep this updated
405                 head.update();
406         }
407
408         // Write the dependencies to file.
409         head.write(depfile);
410
411         if (exit_code) {
412                 // add flag here, just before return, instead of when exit_code
413                 // is defined because scanres is sometimes overwritten above
414                 // (e.g. rerun)
415                 scanres |= NONZERO_ERROR;
416         }
417
418         LYXERR(Debug::LATEX, "Done.");
419
420         if (bscanres & ERRORS)
421                 return bscanres; // return on error
422
423         return scanres;
424 }
425
426
427 int LaTeX::startscript()
428 {
429         // onlyFileName() is needed for cygwin
430         string tmp = cmd + ' '
431                      + quoteName(onlyFileName(file.toFilesystemEncoding()))
432                      + " > " + os::nulldev();
433         Systemcall one;
434         return one.startscript(Systemcall::Wait, tmp, path, lpath);
435 }
436
437
438 bool LaTeX::runMakeIndex(string const & f, OutputParams const & runparams,
439                          string const & params)
440 {
441         string tmp = runparams.use_japanese ?
442                 lyxrc.jindex_command : lyxrc.index_command;
443         
444         if (!runparams.index_command.empty())
445                 tmp = runparams.index_command;
446
447         LYXERR(Debug::LATEX,
448                 "idx file has been made, running index processor ("
449                 << tmp << ") on file " << f);
450
451         tmp = subst(tmp, "$$lang", runparams.document_language);
452         if (runparams.use_indices) {
453                 tmp = lyxrc.splitindex_command + " -m " + quoteName(tmp);
454                 LYXERR(Debug::LATEX,
455                 "Multiple indices. Using splitindex command: " << tmp);
456         }
457         tmp += ' ';
458         tmp += quoteName(f);
459         tmp += params;
460         Systemcall one;
461         one.startscript(Systemcall::Wait, tmp, path, lpath);
462         return true;
463 }
464
465
466 bool LaTeX::runMakeIndexNomencl(FileName const & file,
467                 string const & nlo, string const & nls)
468 {
469         LYXERR(Debug::LATEX, "Running MakeIndex for nomencl.");
470         message(_("Running MakeIndex for nomencl."));
471         string tmp = lyxrc.nomencl_command + ' ';
472         // onlyFileName() is needed for cygwin
473         tmp += quoteName(onlyFileName(changeExtension(file.absFileName(), nlo)));
474         tmp += " -o "
475                 + onlyFileName(changeExtension(file.toFilesystemEncoding(), nls));
476         Systemcall one;
477         one.startscript(Systemcall::Wait, tmp, path, lpath);
478         return true;
479 }
480
481
482 vector<AuxInfo> const
483 LaTeX::scanAuxFiles(FileName const & file)
484 {
485         vector<AuxInfo> result;
486
487         result.push_back(scanAuxFile(file));
488
489         string const basename = removeExtension(file.absFileName());
490         for (int i = 1; i < 1000; ++i) {
491                 FileName const file2(basename
492                         + '.' + convert<string>(i)
493                         + ".aux");
494                 if (!file2.exists())
495                         break;
496                 result.push_back(scanAuxFile(file2));
497         }
498         return result;
499 }
500
501
502 AuxInfo const LaTeX::scanAuxFile(FileName const & file)
503 {
504         AuxInfo result;
505         result.aux_file = file;
506         scanAuxFile(file, result);
507         return result;
508 }
509
510
511 void LaTeX::scanAuxFile(FileName const & file, AuxInfo & aux_info)
512 {
513         LYXERR(Debug::LATEX, "Scanning aux file: " << file);
514
515         ifstream ifs(file.toFilesystemEncoding().c_str());
516         string token;
517         static regex const reg1("\\\\citation\\{([^}]+)\\}");
518         static regex const reg2("\\\\bibdata\\{([^}]+)\\}");
519         static regex const reg3("\\\\bibstyle\\{([^}]+)\\}");
520         static regex const reg4("\\\\@input\\{([^}]+)\\}");
521
522         while (getline(ifs, token)) {
523                 token = rtrim(token, "\r");
524                 smatch sub;
525                 // FIXME UNICODE: We assume that citation keys and filenames
526                 // in the aux file are in the file system encoding.
527                 token = to_utf8(from_filesystem8bit(token));
528                 if (regex_match(token, sub, reg1)) {
529                         string data = sub.str(1);
530                         while (!data.empty()) {
531                                 string citation;
532                                 data = split(data, citation, ',');
533                                 LYXERR(Debug::LATEX, "Citation: " << citation);
534                                 aux_info.citations.insert(citation);
535                         }
536                 } else if (regex_match(token, sub, reg2)) {
537                         string data = sub.str(1);
538                         // data is now all the bib files separated by ','
539                         // get them one by one and pass them to the helper
540                         while (!data.empty()) {
541                                 string database;
542                                 data = split(data, database, ',');
543                                 database = changeExtension(database, "bib");
544                                 LYXERR(Debug::LATEX, "BibTeX database: `" << database << '\'');
545                                 aux_info.databases.insert(database);
546                         }
547                 } else if (regex_match(token, sub, reg3)) {
548                         string style = sub.str(1);
549                         // token is now the style file
550                         // pass it to the helper
551                         style = changeExtension(style, "bst");
552                         LYXERR(Debug::LATEX, "BibTeX style: `" << style << '\'');
553                         aux_info.styles.insert(style);
554                 } else if (regex_match(token, sub, reg4)) {
555                         string const file2 = sub.str(1);
556                         scanAuxFile(makeAbsPath(file2), aux_info);
557                 }
558         }
559 }
560
561
562 void LaTeX::updateBibtexDependencies(DepTable & dep,
563                                      vector<AuxInfo> const & bibtex_info)
564 {
565         // Since a run of Bibtex mandates more latex runs it is ok to
566         // remove all ".bib" and ".bst" files.
567         dep.remove_files_with_extension(".bib");
568         dep.remove_files_with_extension(".bst");
569         //string aux = OnlyFileName(ChangeExtension(file, ".aux"));
570
571         for (vector<AuxInfo>::const_iterator it = bibtex_info.begin();
572              it != bibtex_info.end(); ++it) {
573                 for (set<string>::const_iterator it2 = it->databases.begin();
574                      it2 != it->databases.end(); ++it2) {
575                         FileName const file = findtexfile(*it2, "bib");
576                         if (!file.empty())
577                                 dep.insert(file, true);
578                 }
579
580                 for (set<string>::const_iterator it2 = it->styles.begin();
581                      it2 != it->styles.end(); ++it2) {
582                         FileName const file = findtexfile(*it2, "bst");
583                         if (!file.empty())
584                                 dep.insert(file, true);
585                 }
586         }
587
588         // biber writes nothing into the aux file.
589         // Instead, we have to scan the blg file
590         if (biber) {
591                 TeXErrors terr;
592                 scanBlgFile(dep, terr);
593         }
594 }
595
596
597 bool LaTeX::runBibTeX(vector<AuxInfo> const & bibtex_info,
598                       OutputParams const & runparams)
599 {
600         bool result = false;
601         for (vector<AuxInfo>::const_iterator it = bibtex_info.begin();
602              it != bibtex_info.end(); ++it) {
603                 if (!biber && it->databases.empty())
604                         continue;
605                 result = true;
606
607                 string tmp = runparams.use_japanese ?
608                         lyxrc.jbibtex_command : lyxrc.bibtex_command;
609
610                 if (!runparams.bibtex_command.empty())
611                         tmp = runparams.bibtex_command;
612                 tmp += " ";
613                 // onlyFileName() is needed for cygwin
614                 tmp += quoteName(onlyFileName(removeExtension(
615                                 it->aux_file.absFileName())));
616                 Systemcall one;
617                 one.startscript(Systemcall::Wait, tmp, path, lpath);
618         }
619         // Return whether bibtex was run
620         return result;
621 }
622
623
624 int LaTeX::scanLogFile(TeXErrors & terr)
625 {
626         int last_line = -1;
627         int line_count = 1;
628         int retval = NO_ERRORS;
629         string tmp =
630                 onlyFileName(changeExtension(file.absFileName(), ".log"));
631         LYXERR(Debug::LATEX, "Log file: " << tmp);
632         FileName const fn = FileName(makeAbsPath(tmp));
633         ifstream ifs(fn.toFilesystemEncoding().c_str());
634         bool fle_style = false;
635         static regex const file_line_error(".+\\.\\D+:[0-9]+: (.+)");
636         static regex const child_file(".*([0-9]+[A-Za-z]*_.+\\.tex).*");
637         // Flag for 'File ended while scanning' message.
638         // We need to wait for subsequent processing.
639         string wait_for_error;
640         string child_name;
641         int pnest = 0;
642         stack <pair<string, int> > child;
643
644         string token;
645         while (getline(ifs, token)) {
646                 // MikTeX sometimes inserts \0 in the log file. They can't be
647                 // removed directly with the existing string utility
648                 // functions, so convert them first to \r, and remove all
649                 // \r's afterwards, since we need to remove them anyway.
650                 token = subst(token, '\0', '\r');
651                 token = subst(token, "\r", "");
652                 smatch sub;
653
654                 LYXERR(Debug::LATEX, "Log line: " << token);
655
656                 if (token.empty())
657                         continue;
658
659                 // Track child documents
660                 for (size_t i = 0; i < token.length(); ++i) {
661                         if (token[i] == '(') {
662                                 ++pnest;
663                                 size_t j = token.find('(', i + 1);
664                                 size_t len = j == string::npos
665                                                 ? token.substr(i + 1).length()
666                                                 : j - i - 1;
667                                 string const substr = token.substr(i + 1, len);
668                                 if (regex_match(substr, sub, child_file)) {
669                                         string const name = sub.str(1);
670                                         child.push(make_pair(name, pnest));
671                                         i += len;
672                                 }
673                         } else if (token[i] == ')') {
674                                 if (!child.empty()
675                                     && child.top().second == pnest)
676                                         child.pop();
677                                 --pnest;
678                         }
679                 }
680                 child_name = child.empty() ? empty_string() : child.top().first;
681
682                 if (contains(token, "file:line:error style messages enabled"))
683                         fle_style = true;
684
685                 if (prefixIs(token, "LaTeX Warning:") ||
686                     prefixIs(token, "! pdfTeX warning")) {
687                         // Here shall we handle different
688                         // types of warnings
689                         retval |= LATEX_WARNING;
690                         LYXERR(Debug::LATEX, "LaTeX Warning.");
691                         if (contains(token, "Rerun to get cross-references")) {
692                                 retval |= RERUN;
693                                 LYXERR(Debug::LATEX, "We should rerun.");
694                         // package clefval needs 2 latex runs before bibtex
695                         } else if (contains(token, "Value of")
696                                    && contains(token, "on page")
697                                    && contains(token, "undefined")) {
698                                 retval |= ERROR_RERUN;
699                                 LYXERR(Debug::LATEX, "Force rerun.");
700                         // package etaremune
701                         } else if (contains(token, "Etaremune labels have changed")) {
702                                 retval |= ERROR_RERUN;
703                                 LYXERR(Debug::LATEX, "Force rerun.");
704                         } else if (contains(token, "Citation")
705                                    && contains(token, "on page")
706                                    && contains(token, "undefined")) {
707                                 retval |= UNDEF_CIT;
708                         } else if (contains(token, "Citation")
709                                    && contains(token, "on input line")
710                                    && contains(token, "undefined")) {
711                                 retval |= UNDEF_CIT;
712                         }
713                 } else if (prefixIs(token, "Package")) {
714                         // Package warnings
715                         retval |= PACKAGE_WARNING;
716                         if (contains(token, "natbib Warning:")) {
717                                 // Natbib warnings
718                                 if (contains(token, "Citation")
719                                     && contains(token, "on page")
720                                     && contains(token, "undefined")) {
721                                         retval |= UNDEF_CIT;
722                                 }
723                         } else if (contains(token, "run BibTeX")) {
724                                 retval |= UNDEF_CIT;
725                         } else if (contains(token, "run Biber")) {
726                                 retval |= UNDEF_CIT;
727                                 biber = true;
728                         } else if (contains(token, "Rerun LaTeX") ||
729                                    contains(token, "Please rerun LaTeX") ||
730                                    contains(token, "Rerun to get")) {
731                                 // at least longtable.sty and bibtopic.sty
732                                 // might use this.
733                                 LYXERR(Debug::LATEX, "We should rerun.");
734                                 retval |= RERUN;
735                         }
736                 } else if (prefixIs(token, "LETTRE WARNING:")) {
737                         if (contains(token, "veuillez recompiler")) {
738                                 // lettre.cls
739                                 LYXERR(Debug::LATEX, "We should rerun.");
740                                 retval |= RERUN;
741                         }
742                 } else if (token[0] == '(') {
743                         if (contains(token, "Rerun LaTeX") ||
744                             contains(token, "Rerun to get")) {
745                                 // Used by natbib
746                                 LYXERR(Debug::LATEX, "We should rerun.");
747                                 retval |= RERUN;
748                         }
749                 } else if (prefixIs(token, "! ")
750                             || (fle_style
751                                 && regex_match(token, sub, file_line_error)
752                                 && !contains(token, "pdfTeX warning"))) {
753                            // Ok, we have something that looks like a TeX Error
754                            // but what do we really have.
755
756                         // Just get the error description:
757                         string desc;
758                         if (prefixIs(token, "! "))
759                                 desc = string(token, 2);
760                         else if (fle_style)
761                                 desc = sub.str();
762                         if (contains(token, "LaTeX Error:"))
763                                 retval |= LATEX_ERROR;
764
765                         if (prefixIs(token, "! File ended while scanning")){
766                                 if (prefixIs(token, "! File ended while scanning use of \\Hy@setref@link.")){
767                                         // bug 7344. We must rerun LaTeX if hyperref has been toggled.
768                                         retval |= ERROR_RERUN;
769                                         LYXERR(Debug::LATEX, "Force rerun.");
770                                 } else {
771                                         // bug 6445. At this point its not clear we finish with error.
772                                         wait_for_error = desc;
773                                         continue;
774                                 }
775                         }
776
777                         if (prefixIs(token, "! Paragraph ended before \\Hy@setref@link was complete.")){
778                                         // bug 7344. We must rerun LaTeX if hyperref has been toggled.
779                                         retval |= ERROR_RERUN;
780                                         LYXERR(Debug::LATEX, "Force rerun.");
781                         }
782
783                         if (!wait_for_error.empty() && prefixIs(token, "! Emergency stop.")){
784                                 retval |= LATEX_ERROR;
785                                 string errstr;
786                                 int count = 0;
787                                 errstr = wait_for_error;
788                                 do {
789                                         if (!getline(ifs, tmp))
790                                                 break;
791                                         tmp = rtrim(tmp, "\r");
792                                         errstr += "\n" + tmp;
793                                         if (++count > 5)
794                                                 break;
795                                 } while (!contains(tmp, "(job aborted"));
796
797                                 terr.insertError(0,
798                                                  from_local8bit("Emergency stop"),
799                                                  from_local8bit(errstr),
800                                                  child_name);
801                         }
802
803                         // get the next line
804                         string tmp;
805                         int count = 0;
806                         do {
807                                 if (!getline(ifs, tmp))
808                                         break;
809                                 tmp = rtrim(tmp, "\r");
810                                 // 15 is somewhat arbitrarily chosen, based on practice.
811                                 // We used 10 for 14 years and increased it to 15 when we
812                                 // saw one case.
813                                 if (++count > 15)
814                                         break;
815                         } while (!prefixIs(tmp, "l."));
816                         if (prefixIs(tmp, "l.")) {
817                                 // we have a latex error
818                                 retval |=  TEX_ERROR;
819                                 if (contains(desc,
820                                         "Package babel Error: You haven't defined the language")
821                                     || contains(desc,
822                                         "Package babel Error: You haven't loaded the option")
823                                     || contains(desc,
824                                         "Package babel Error: Unknown language"))
825                                         retval |= ERROR_RERUN;
826                                 // get the line number:
827                                 int line = 0;
828                                 sscanf(tmp.c_str(), "l.%d", &line);
829                                 // get the rest of the message:
830                                 string errstr(tmp, tmp.find(' '));
831                                 errstr += '\n';
832                                 getline(ifs, tmp);
833                                 tmp = rtrim(tmp, "\r");
834                                 while (!contains(errstr, "l.")
835                                        && !tmp.empty()
836                                        && !prefixIs(tmp, "! ")
837                                        && !contains(tmp, "(job aborted")) {
838                                         errstr += tmp;
839                                         errstr += "\n";
840                                         getline(ifs, tmp);
841                                         tmp = rtrim(tmp, "\r");
842                                 }
843                                 LYXERR(Debug::LATEX, "line: " << line << '\n'
844                                         << "Desc: " << desc << '\n' << "Text: " << errstr);
845                                 if (line == last_line)
846                                         ++line_count;
847                                 else {
848                                         line_count = 1;
849                                         last_line = line;
850                                 }
851                                 if (line_count <= 5) {
852                                         // FIXME UNICODE
853                                         // We have no idea what the encoding of
854                                         // the log file is.
855                                         // It seems that the output from the
856                                         // latex compiler itself is pure ASCII,
857                                         // but it can include bits from the
858                                         // document, so whatever encoding we
859                                         // assume here it can be wrong.
860                                         terr.insertError(line,
861                                                          from_local8bit(desc),
862                                                          from_local8bit(errstr),
863                                                          child_name);
864                                         ++num_errors;
865                                 }
866                         }
867                 } else {
868                         // information messages, TeX warnings and other
869                         // warnings we have not caught earlier.
870                         if (prefixIs(token, "Overfull ")) {
871                                 retval |= TEX_WARNING;
872                         } else if (prefixIs(token, "Underfull ")) {
873                                 retval |= TEX_WARNING;
874                         } else if (contains(token, "Rerun to get citations")) {
875                                 // Natbib seems to use this.
876                                 retval |= UNDEF_CIT;
877                         } else if (contains(token, "No pages of output")) {
878                                 // A dvi file was not created
879                                 retval |= NO_OUTPUT;
880                         } else if (contains(token, "That makes 100 errors")) {
881                                 // More than 100 errors were reprted
882                                 retval |= TOO_MANY_ERRORS;
883                         } else if (prefixIs(token, "!pdfTeX error:")) {
884                                 // otherwise we dont catch e.g.:
885                                 // !pdfTeX error: pdflatex (file feyn10): Font feyn10 at 600 not found
886                                 retval |= ERRORS;
887                                 terr.insertError(0,
888                                                  from_local8bit("pdfTeX Error"),
889                                                  from_local8bit(token),
890                                                  child_name);
891                         } else if (prefixIs(token, "Missing character: There is no ")) {
892                                 // XeTeX/LuaTeX error about missing glyph in selected font
893                                 // (bug 9610)
894                                 retval |= LATEX_ERROR;
895                                 terr.insertError(0,
896                                                  from_local8bit("Missing glyphs!"),
897                                                  from_local8bit(token),
898                                                  child_name);
899                         }
900                 }
901         }
902         LYXERR(Debug::LATEX, "Log line: " << token);
903         return retval;
904 }
905
906
907 namespace {
908
909 bool insertIfExists(FileName const & absname, DepTable & head)
910 {
911         if (absname.exists() && !absname.isDirectory()) {
912                 head.insert(absname, true);
913                 return true;
914         }
915         return false;
916 }
917
918
919 bool handleFoundFile(string const & ff, DepTable & head)
920 {
921         // convert from native os path to unix path
922         string foundfile = os::internal_path(trim(ff));
923
924         LYXERR(Debug::DEPEND, "Found file: " << foundfile);
925
926         // Ok now we found a file.
927         // Now we should make sure that this is a file that we can
928         // access through the normal paths.
929         // We will not try any fancy search methods to
930         // find the file.
931
932         // (1) foundfile is an
933         //     absolute path and should
934         //     be inserted.
935         FileName absname;
936         if (FileName::isAbsolute(foundfile)) {
937                 LYXERR(Debug::DEPEND, "AbsolutePath file: " << foundfile);
938                 // On initial insert we want to do the update at once
939                 // since this file cannot be a file generated by
940                 // the latex run.
941                 absname.set(foundfile);
942                 if (!insertIfExists(absname, head)) {
943                         // check for spaces
944                         string strippedfile = foundfile;
945                         while (contains(strippedfile, " ")) {
946                                 // files with spaces are often enclosed in quotation
947                                 // marks; those have to be removed
948                                 string unquoted = subst(strippedfile, "\"", "");
949                                 absname.set(unquoted);
950                                 if (insertIfExists(absname, head))
951                                         return true;
952                                 // strip off part after last space and try again
953                                 string tmp = strippedfile;
954                                 string const stripoff =
955                                         rsplit(tmp, strippedfile, ' ');
956                                 absname.set(strippedfile);
957                                 if (insertIfExists(absname, head))
958                                         return true;
959                         }
960                 }
961         }
962
963         string onlyfile = onlyFileName(foundfile);
964         absname = makeAbsPath(onlyfile);
965
966         // check for spaces
967         while (contains(foundfile, ' ')) {
968                 if (absname.exists())
969                         // everything o.k.
970                         break;
971                 else {
972                         // files with spaces are often enclosed in quotation
973                         // marks; those have to be removed
974                         string unquoted = subst(foundfile, "\"", "");
975                         absname = makeAbsPath(unquoted);
976                         if (absname.exists())
977                                 break;
978                         // strip off part after last space and try again
979                         string strippedfile;
980                         string const stripoff =
981                                 rsplit(foundfile, strippedfile, ' ');
982                         foundfile = strippedfile;
983                         onlyfile = onlyFileName(strippedfile);
984                         absname = makeAbsPath(onlyfile);
985                 }
986         }
987
988         // (2) foundfile is in the tmpdir
989         //     insert it into head
990         if (absname.exists() && !absname.isDirectory()) {
991                 // FIXME: This regex contained glo, but glo is used by the old
992                 // version of nomencl.sty. Do we need to put it back?
993                 static regex const unwanted("^.*\\.(aux|log|dvi|bbl|ind)$");
994                 if (regex_match(onlyfile, unwanted)) {
995                         LYXERR(Debug::DEPEND, "We don't want " << onlyfile
996                                 << " in the dep file");
997                 } else if (suffixIs(onlyfile, ".tex")) {
998                         // This is a tex file generated by LyX
999                         // and latex is not likely to change this
1000                         // during its runs.
1001                         LYXERR(Debug::DEPEND, "Tmpdir TeX file: " << onlyfile);
1002                         head.insert(absname, true);
1003                 } else {
1004                         LYXERR(Debug::DEPEND, "In tmpdir file:" << onlyfile);
1005                         head.insert(absname);
1006                 }
1007                 return true;
1008         } else {
1009                 LYXERR(Debug::DEPEND, "Not a file or we are unable to find it.");
1010                 return false;
1011         }
1012 }
1013
1014
1015 bool completeFilename(string const & ff, DepTable & head)
1016 {
1017         // If we do not find a dot, we suspect
1018         // a fragmental file name
1019         if (!contains(ff, '.'))
1020                 return false;
1021
1022         // if we have a dot, we let handleFoundFile decide
1023         return handleFoundFile(ff, head);
1024 }
1025
1026
1027 int iterateLine(string const & token, regex const & reg, string const & closing,
1028                 int fragment_pos, DepTable & head)
1029 {
1030         smatch what;
1031         string::const_iterator first = token.begin();
1032         string::const_iterator end = token.end();
1033         bool fragment = false;
1034         string last_match;
1035
1036         while (regex_search(first, end, what, reg)) {
1037                 // if we have a dot, try to handle as file
1038                 if (contains(what.str(1), '.')) {
1039                         first = what[0].second;
1040                         if (what.str(2) == closing) {
1041                                 handleFoundFile(what.str(1), head);
1042                                 // since we had a closing bracket,
1043                                 // do not investigate further
1044                                 fragment = false;
1045                         } else
1046                                 // if we have no closing bracket,
1047                                 // try to handle as file nevertheless
1048                                 fragment = !handleFoundFile(
1049                                         what.str(1) + what.str(2), head);
1050                 }
1051                 // if we do not have a dot, check if the line has
1052                 // a closing bracket (else, we suspect a line break)
1053                 else if (what.str(2) != closing) {
1054                         first = what[0].second;
1055                         fragment = true;
1056                 } else {
1057                         // we have a closing bracket, so the content
1058                         // is not a file name.
1059                         // no need to investigate further
1060                         first = what[0].second;
1061                         fragment = false;
1062                 }
1063                 last_match = what.str(1);
1064         }
1065
1066         // We need to consider the result from previous line iterations:
1067         // We might not find a fragment here, but another one might follow
1068         // E.g.: (filename.ext) <filenam
1069         // Vice versa, we consider the search completed if a real match
1070         // follows a potential fragment from a previous iteration.
1071         // E.g. <some text we considered a fragment (filename.ext)
1072         // result = -1 means we did not find a fragment!
1073         int result = -1;
1074         int last_match_pos = -1;
1075         if (!last_match.empty() && token.find(last_match) != string::npos)
1076                 last_match_pos = int(token.find(last_match));
1077         if (fragment) {
1078                 if (last_match_pos > fragment_pos)
1079                         result = last_match_pos;
1080                 else
1081                         result = fragment_pos;
1082         } else
1083                 if (last_match_pos < fragment_pos)
1084                         result = fragment_pos;
1085
1086         return result;
1087 }
1088
1089 } // anon namespace
1090
1091
1092 void LaTeX::deplog(DepTable & head)
1093 {
1094         // This function reads the LaTeX log file end extracts all the
1095         // external files used by the LaTeX run. The files are then
1096         // entered into the dependency file.
1097
1098         string const logfile =
1099                 onlyFileName(changeExtension(file.absFileName(), ".log"));
1100
1101         static regex const reg1("File: (.+).*");
1102         static regex const reg2("No file (.+)(.).*");
1103         static regex const reg3("\\\\openout[0-9]+.*=.*`(.+)(..).*");
1104         // If an index should be created, MikTex does not write a line like
1105         //    \openout# = 'sample.idx'.
1106         // but instead only a line like this into the log:
1107         //   Writing index file sample.idx
1108         static regex const reg4("Writing index file (.+).*");
1109         static regex const regoldnomencl("Writing glossary file (.+).*");
1110         static regex const regnomencl("Writing nomenclature file (.+).*");
1111         // If a toc should be created, MikTex does not write a line like
1112         //    \openout# = `sample.toc'.
1113         // but only a line like this into the log:
1114         //    \tf@toc=\write#
1115         // This line is also written by tetex.
1116         // This line is not present if no toc should be created.
1117         static regex const miktexTocReg("\\\\tf@toc=\\\\write.*");
1118         // file names can be enclosed in <...> (anywhere on the line)
1119         static regex const reg5(".*<[^>]+.*");
1120         // and also (...) anywhere on the line
1121         static regex const reg6(".*\\([^)]+.*");
1122
1123         FileName const fn = makeAbsPath(logfile);
1124         ifstream ifs(fn.toFilesystemEncoding().c_str());
1125         string lastline;
1126         while (ifs) {
1127                 // Ok, the scanning of files here is not sufficient.
1128                 // Sometimes files are named by "File: xxx" only
1129                 // Therefore we use some regexps to find files instead.
1130                 // Note: all file names and paths might contains spaces.
1131                 // Also, file names might be broken across lines. Therefore
1132                 // we mark (potential) fragments and merge those lines.
1133                 bool fragment = false;
1134                 string token;
1135                 getline(ifs, token);
1136                 // MikTeX sometimes inserts \0 in the log file. They can't be
1137                 // removed directly with the existing string utility
1138                 // functions, so convert them first to \r, and remove all
1139                 // \r's afterwards, since we need to remove them anyway.
1140                 token = subst(token, '\0', '\r');
1141                 token = subst(token, "\r", "");
1142                 if (token.empty() || token == ")") {
1143                         lastline = string();
1144                         continue;
1145                 }
1146
1147                 // FIXME UNICODE: We assume that the file names in the log
1148                 // file are in the file system encoding.
1149                 token = to_utf8(from_filesystem8bit(token));
1150
1151                 // Sometimes, filenames are broken across lines.
1152                 // We care for that and save suspicious lines.
1153                 // Here we exclude some cases where we are sure
1154                 // that there is no continued filename
1155                 if (!lastline.empty()) {
1156                         static regex const package_info("Package \\w+ Info: .*");
1157                         static regex const package_warning("Package \\w+ Warning: .*");
1158                         if (prefixIs(token, "File:") || prefixIs(token, "(Font)")
1159                             || prefixIs(token, "Package:")
1160                             || prefixIs(token, "Language:")
1161                             || prefixIs(token, "LaTeX Info:")
1162                             || prefixIs(token, "LaTeX Font Info:")
1163                             || prefixIs(token, "\\openout[")
1164                             || prefixIs(token, "))")
1165                             || regex_match(token, package_info)
1166                             || regex_match(token, package_warning))
1167                                 lastline = string();
1168                 }
1169
1170                 if (!lastline.empty())
1171                         // probably a continued filename from last line
1172                         token = lastline + token;
1173                 if (token.length() > 255) {
1174                         // string too long. Cut off.
1175                         token.erase(0, token.length() - 251);
1176                 }
1177
1178                 smatch sub;
1179
1180                 // (1) "File: file.ext"
1181                 if (regex_match(token, sub, reg1)) {
1182                         // is this a fragmental file name?
1183                         fragment = !completeFilename(sub.str(1), head);
1184                         // However, ...
1185                         if (suffixIs(token, ")"))
1186                                 // no fragment for sure
1187                                 fragment = false;
1188                 // (2) "No file file.ext"
1189                 } else if (regex_match(token, sub, reg2)) {
1190                         // file names must contains a dot, line ends with dot
1191                         if (contains(sub.str(1), '.') && sub.str(2) == ".")
1192                                 fragment = !handleFoundFile(sub.str(1), head);
1193                         else
1194                                 // we suspect a line break
1195                                 fragment = true;
1196                 // (3) "\openout<nr> = `file.ext'."
1197                 } else if (regex_match(token, sub, reg3)) {
1198                         // search for closing '. at the end of the line
1199                         if (sub.str(2) == "\'.")
1200                                 fragment = !handleFoundFile(sub.str(1), head);
1201                         else
1202                                 // potential fragment
1203                                 fragment = true;
1204                 // (4) "Writing index file file.ext"
1205                 } else if (regex_match(token, sub, reg4))
1206                         // fragmential file name?
1207                         fragment = !completeFilename(sub.str(1), head);
1208                 // (5) "Writing nomenclature file file.ext"
1209                 else if (regex_match(token, sub, regnomencl) ||
1210                            regex_match(token, sub, regoldnomencl))
1211                         // fragmental file name?
1212                         fragment= !completeFilename(sub.str(1), head);
1213                 // (6) "\tf@toc=\write<nr>" (for MikTeX)
1214                 else if (regex_match(token, sub, miktexTocReg))
1215                         fragment = !handleFoundFile(onlyFileName(changeExtension(
1216                                                 file.absFileName(), ".toc")), head);
1217                 else
1218                         // not found, but we won't check further
1219                         fragment = false;
1220
1221                 int fragment_pos = -1;
1222                 // (7) "<file.ext>"
1223                 // We can have several of these on one line
1224                 // (and in addition to those above)
1225                 if (regex_match(token, sub, reg5)) {
1226                         // search for strings in <...>
1227                         static regex const reg5_1("<([^>]+)(.)");
1228                         fragment_pos = iterateLine(token, reg5_1, ">",
1229                                                    fragment_pos, head);
1230                         fragment = (fragment_pos != -1);
1231                 }
1232
1233                 // (8) "(file.ext)"
1234                 // We can have several of these on one line
1235                 // this must be queried separated, because of
1236                 // cases such as "File: file.ext (type eps)"
1237                 // where "File: file.ext" would be skipped
1238                 if (regex_match(token, sub, reg6)) {
1239                         // search for strings in (...)
1240                         static regex const reg6_1("\\(([^()]+)(.)");
1241                         fragment_pos = iterateLine(token, reg6_1, ")",
1242                                                    fragment_pos, head);
1243                         fragment = (fragment_pos != -1);
1244                 }
1245
1246                 if (fragment)
1247                         // probable linebreak within file name:
1248                         // save this line
1249                         lastline = token;
1250                 else
1251                         // no linebreak: reset
1252                         lastline = string();
1253         }
1254
1255         // Make sure that the main .tex file is in the dependency file.
1256         head.insert(file, true);
1257 }
1258
1259
1260 int LaTeX::scanBlgFile(DepTable & dep, TeXErrors & terr)
1261 {
1262         FileName const blg_file(changeExtension(file.absFileName(), "blg"));
1263         LYXERR(Debug::LATEX, "Scanning blg file: " << blg_file);
1264
1265         ifstream ifs(blg_file.toFilesystemEncoding().c_str());
1266         string token;
1267         static regex const reg1(".*Found (bibtex|BibTeX) data (file|source) '([^']+).*");
1268         static regex const bibtexError("^(.*---line [0-9]+ of file).*$");
1269         static regex const bibtexError2("^(.*---while reading file).*$");
1270         static regex const bibtexError3("(A bad cross reference---).*");
1271         static regex const bibtexError4("(Sorry---you've exceeded BibTeX's).*");
1272         static regex const bibtexError5("\\*Please notify the BibTeX maintainer\\*");
1273         static regex const biberError("^.*> (FATAL|ERROR) - (.*)$");
1274         int retval = NO_ERRORS;
1275
1276         string prevtoken;
1277         while (getline(ifs, token)) {
1278                 token = rtrim(token, "\r");
1279                 smatch sub;
1280                 // FIXME UNICODE: We assume that citation keys and filenames
1281                 // in the aux file are in the file system encoding.
1282                 token = to_utf8(from_filesystem8bit(token));
1283                 if (regex_match(token, sub, reg1)) {
1284                         string data = sub.str(3);
1285                         if (!data.empty()) {
1286                                 LYXERR(Debug::LATEX, "Found bib file: " << data);
1287                                 handleFoundFile(data, dep);
1288                         }
1289                 }
1290                 else if (regex_match(token, sub, bibtexError)
1291                          || regex_match(token, sub, bibtexError2)
1292                          || regex_match(token, sub, bibtexError4)
1293                          || regex_match(token, sub, bibtexError5)) {
1294                         retval |= BIBTEX_ERROR;
1295                         string errstr = N_("BibTeX error: ") + token;
1296                         string message;
1297                         if ((prefixIs(token, "while executing---line")
1298                              || prefixIs(token, "---line ")
1299                              || prefixIs(token, "*Please notify the BibTeX"))
1300                             && !prevtoken.empty()) {
1301                                 errstr = N_("BibTeX error: ") + prevtoken;
1302                                 message = prevtoken + '\n';
1303                         }
1304                         message += token;
1305                         terr.insertError(0,
1306                                          from_local8bit(errstr),
1307                                          from_local8bit(message));
1308                 } else if (regex_match(prevtoken, sub, bibtexError3)) {
1309                         retval |= BIBTEX_ERROR;
1310                         string errstr = N_("BibTeX error: ") + prevtoken;
1311                         string message = prevtoken + '\n' + token;
1312                         terr.insertError(0,
1313                                          from_local8bit(errstr),
1314                                          from_local8bit(message));
1315                 } else if (regex_match(token, sub, biberError)) {
1316                         retval |= BIBTEX_ERROR;
1317                         string errstr = N_("Biber error: ") + sub.str(2);
1318                         string message = token;
1319                         terr.insertError(0,
1320                                          from_local8bit(errstr),
1321                                          from_local8bit(message));
1322                 }
1323                 prevtoken = token;
1324         }
1325         return retval;
1326 }
1327
1328
1329 } // namespace lyx