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