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