]> git.lyx.org Git - lyx.git/blob - src/LaTeX.C
- Add support for ae package
[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(head);
222         }
223         
224         // 1
225         // we know on this point that latex has been run once (or we just
226         // returned) and the question now is to decide if we need to run
227         // it any more. This is done by asking if any of the files in the
228         // dependency file has changed. (remember that the checksum for
229         // a given file is reported to have changed if it just was created)
230         //     -> if changed or rerun == true:
231         //             run latex once more and
232         //             update the dependency structure
233         //     -> if not changed:
234         //             we does nothing at this point
235         //
236         if (rerun || head.sumchange()) {
237                 rerun = false;
238                 ++count;
239                 lyxerr[Debug::DEPEND]
240                         << "Dep. file has changed or rerun requested" << endl;
241                 lyxerr[Debug::LATEX]
242                         << "Run #" << count << endl;
243                 WriteStatus(minib,
244                             string(_("LaTeX run number ")) + tostr(count));
245                 this->operator()();
246                 scanres = scanLogFile(terr);
247                 if (scanres & LaTeX::ERRORS) {
248                         deleteFilesOnError();
249                         return scanres; // return on error
250                 }
251                 
252                 // update the depedencies
253                 deplog(head); // reads the latex log
254                 head.update();
255         } else {
256                 lyxerr[Debug::DEPEND] << "Dep. file has NOT changed" << endl;
257         }
258
259         // 1.5
260         // The inclusion of files generated by external programs like
261         // makeindex or bibtex might have done changes to pagenumbereing,
262         // etc. And because of this we must run the external programs
263         // again to make sure everything is redone correctly.
264         // Also there should be no need to run the external programs any
265         // more after this.
266         
267         // run makeindex if the <file>.idx has changed or was generated.
268         if (head.haschanged(OnlyFilename(ChangeExtension(file, ".idx")))) {
269                 // no checks for now
270                 lyxerr[Debug::LATEX] << "Running MakeIndex." << endl;
271                 WriteStatus(minib, _("Running MakeIndex."));
272                 rerun = runMakeIndex(OnlyFilename(ChangeExtension(file, ".idx")));
273         }
274         
275         // 2
276         // we will only run latex more if the log file asks for it.
277         // or if the sumchange() is true.
278         //     -> rerun asked for:
279         //             run latex and
280         //             remake the dependency file
281         //             goto 2 or return if max runs are reached.
282         //     -> rerun not asked for:
283         //             just return (fall out of bottom of func)
284         //
285         while ((head.sumchange() || rerun || (scanres & LaTeX::RERUN)) 
286                && count < MAX_RUN) {
287                 // Yes rerun until message goes away, or until
288                 // MAX_RUNS are reached.
289                 rerun = false;
290                 ++count;
291                 lyxerr[Debug::LATEX] << "Run #" << count << endl;
292                 WriteStatus(minib, string(_("LaTeX run number ")) + tostr(count));
293                 this->operator()();
294                 scanres = scanLogFile(terr);
295                 if (scanres & LaTeX::ERRORS) {
296                         deleteFilesOnError();
297                         return scanres; // return on error
298                 }
299                 
300                 // keep this updated
301                 head.update();
302         }
303
304         // Write the dependencies to file.
305         head.write(depfile);
306         lyxerr[Debug::LATEX] << "Done." << endl;
307         return scanres;
308 }
309
310
311 int LaTeX::operator()()
312 {
313 #ifndef __EMX__
314         string tmp = cmd + ' ' + QuoteName(file) + " > /dev/null";
315 #else // cmd.exe (OS/2) causes SYS0003 error at "/dev/null"
316         string tmp = cmd + ' ' + file + " > nul";
317 #endif
318         Systemcalls one;
319         return one.startscript(Systemcalls::System, tmp);
320 }
321
322
323 bool LaTeX::runMakeIndex(string const & f)
324 {
325         lyxerr[Debug::LATEX] << "idx file has been made,"
326                 " running makeindex on file "
327                              <<  f << endl;
328
329         // It should be possible to set the switches for makeindex
330         // sorting style and such. It would also be very convenient
331         // to be able to make style files from within LyX. This has
332         // to come for a later time. (0.13 perhaps?)
333         string tmp = "makeindex -c -q ";
334         tmp += f;
335         Systemcalls one;
336         one.startscript(Systemcalls::System, tmp);
337         return true;
338 }
339
340
341 bool LaTeX::scanAux(DepTable & dep)
342 {
343         // if any of the bib file has changed we don't have to
344         // check the .aux file.
345         if (dep.extchanged(".bib")
346             || dep.extchanged(".bst")) return true;
347         
348         string aux = OnlyFilename(ChangeExtension(file, ".aux"));
349         return scanAux(aux, dep, false);
350 }
351
352
353 bool LaTeX::scanAux(string const & file, DepTable & dep, bool insert)
354 {
355         lyxerr[Debug::LATEX] << "Scanning aux file: " << file << endl;
356
357         ifstream ifs(file.c_str());
358         string token;
359         LRegex reg1("\\\\bibdata\\{([^}]+)\\}");
360         LRegex reg2("\\\\bibstyle\\{([^}]+)\\}");
361         LRegex reg3("\\\\@input\\{([^}]+)\\}");
362         bool using_bibtex = false;
363         while (getline(ifs, token)) {
364                 if (reg1.exact_match(token)) {
365                         using_bibtex = true;
366                         LRegex::SubMatches const & sub = reg1.exec(token);
367                         string data = LSubstring(token, sub[1].first,
368                                                  sub[1].second);
369                         // data is now all the bib files separated by ','
370                         // get them one by one and pass them to the helper
371                         while (!data.empty()) {
372                                 string l;
373                                 data = split(data, l, ',');
374                                 string full_l = 
375                                         findtexfile(ChangeExtension(l, "bib"),
376                                                     "bib");
377                                 lyxerr[Debug::LATEX] << "Bibtex database: `"
378                                                      << full_l << "'" << endl;
379                                 if (!full_l.empty()) {
380                                         if (insert)
381                                                 // add full_l to the dep file.
382                                                 dep.insert(full_l, true);
383                                         else
384                                                 if (!dep.exist(full_l))
385                                                         return true;
386                                 }
387                         }
388                 } else if (reg2.exact_match(token)) {
389                         using_bibtex = true;
390                         LRegex::SubMatches const & sub = reg2.exec(token);
391                         string style = LSubstring(token, sub[1].first,
392                                                   sub[1].second);
393                         // token is now the style file
394                         // pass it to the helper
395                         string full_l = 
396                                 findtexfile(ChangeExtension(style, "bst"),
397                                             "bst");
398                         lyxerr[Debug::LATEX] << "Bibtex style: `"
399                                              << full_l << "'" << endl;
400                         if (!full_l.empty()) {
401                                 if (insert)
402                                         // add full_l to the dep file.
403                                         dep.insert(full_l, true);
404                                 else
405                                         if (!dep.exist(full_l))
406                                                 return true;    
407                         }
408                 } else if (reg3.exact_match(token)) {
409                         LRegex::SubMatches const & sub = reg3.exec(token);
410                         string file2 = LSubstring(token, sub[1].first,
411                                                   sub[1].second);
412                         if (scanAux(file2, dep, insert)) {
413                                 using_bibtex = true;
414                                 if (!insert)
415                                         return true;
416                         }
417                 }
418         }
419         if (insert)
420                 return using_bibtex;
421         else
422                 return false;
423 }
424
425
426 bool LaTeX::runBibTeX(DepTable & dep)
427 {
428         // Since a run of Bibtex mandates more latex runs it is ok to
429         // remove all ".bib" and ".bst" files, it is also required to
430         // discover style and database changes.
431         dep.remove_files_with_extension(".bib");
432         dep.remove_files_with_extension(".bst");
433         string aux = OnlyFilename(ChangeExtension(file, ".aux"));
434         if (scanAux(aux, dep, true)) {
435                 // run bibtex and
436                 string tmp = "bibtex ";
437                 tmp += OnlyFilename(ChangeExtension(file, string()));
438                 Systemcalls one;
439                 one.startscript(Systemcalls::System, tmp);
440                 return true;
441         }
442         // bibtex was not run.
443         return false;
444 }
445
446
447 int LaTeX::scanLogFile(TeXErrors & terr)
448 {
449         int last_line = -1;
450         int line_count = 1;
451         int retval = NO_ERRORS;
452         string tmp = OnlyFilename(ChangeExtension(file, ".log"));
453         lyxerr[Debug::LATEX] << "Log file: " << tmp << endl;
454         ifstream ifs(tmp.c_str());
455
456         string token;
457         while (getline(ifs, token)) {
458                 lyxerr[Debug::LATEX] << "Log line: " << token << endl;
459                 
460                 if (token.empty())
461                         continue;
462
463                 if (prefixIs(token, "LaTeX Warning:")) {
464                         // Here shall we handle different
465                         // types of warnings
466                         retval |= LATEX_WARNING;
467                         lyxerr[Debug::LATEX] << "LaTeX Warning." << endl;
468                         if (contains(token, "Rerun to get cross-references")) {
469                                 retval |= RERUN;
470                                 lyxerr[Debug::LATEX]
471                                         << "We should rerun." << endl;
472                         } else if (contains(token, "Citation")
473                                    && contains(token, "on page")
474                                    && contains(token, "undefined")) {
475                                 retval |= UNDEF_CIT;
476                         }
477                 } else if (prefixIs(token, "Package")) {
478                         // Package warnings
479                         retval |= PACKAGE_WARNING;
480                         if (contains(token, "natbib Warning:")) {
481                                 // Natbib warnings
482                                 if (contains(token, "Citation")
483                                     && contains(token, "on page")
484                                     && contains(token, "undefined")) {
485                                         retval |= UNDEF_CIT;
486                                 }
487                         } else if (contains(token, "Rerun LaTeX.")) {
488                                 // at least longtable.sty might use this.
489                                 retval |= RERUN;
490                         }
491                 } else if (prefixIs(token, "! ")) {
492                         // Ok, we have something that looks like a TeX Error
493                         // but what do we really have.
494
495                         // Just get the error description:
496                         string desc(token, 2);
497                         if (contains(token, "LaTeX Error:"))
498                                 retval |= LATEX_ERROR;
499                         // get the next line
500                         string tmp;
501                         int count = 0;
502                         do {
503                                 if (!getline(ifs, tmp))
504                                         break;
505                                 if (++count > 10)
506                                         break;
507                         } while (!prefixIs(tmp, "l."));
508                         if (prefixIs(tmp, "l.")) {
509                                 // we have a latex error
510                                 retval |=  TEX_ERROR;
511                                 // get the line number:
512                                 int line = 0;
513                                 sscanf(tmp.c_str(), "l.%d", &line);
514                                 // get the rest of the message:
515                                 string errstr(tmp, tmp.find(' '));
516                                 errstr += '\n';
517                                 getline(ifs, tmp);
518                                 while (!contains(errstr, "l.")
519                                        && !tmp.empty()
520                                        && !prefixIs(tmp, "! ")
521                                        && !contains(tmp, "(job aborted")) {
522                                         errstr += tmp;
523                                         errstr += "\n";
524                                         getline(ifs, tmp);
525                                 }
526                                 lyxerr[Debug::LATEX]
527                                         << "line: " << line << '\n'
528                                         << "Desc: " << desc << '\n'
529                                         << "Text: " << errstr << endl;
530                                 if (line == last_line)
531                                         ++line_count;
532                                 else {
533                                         line_count = 1;
534                                         last_line = line;
535                                 }
536                                 if (line_count <= 5) {
537                                         terr.insertError(line, desc, errstr);
538                                         ++num_errors;
539                                 }
540                         }
541                 } else {
542                         // information messages, TeX warnings and other
543                         // warnings we have not caught earlier.
544                         if (prefixIs(token, "Overfull ")) {
545                                 retval |= TEX_WARNING;
546                         } else if (prefixIs(token, "Underfull ")) {
547                                 retval |= TEX_WARNING;
548                         } else if (contains(token, "Rerun to get citations")) {
549                                 // Natbib seems to use this.
550                                 retval |= RERUN;
551                         } else if (contains(token, "No pages of output")) {
552                                 // A dvi file was not created
553                                 retval |= NO_OUTPUT;
554                         } else if (contains(token, "That makes 100 errors")) {
555                                 // More than 100 errors were reprted
556                                 retval |= TOO_MANY_ERRORS;
557                         }
558                 }
559         }
560         lyxerr[Debug::LATEX] << "Log line: " << token << endl;
561         return retval;
562 }
563
564
565 void LaTeX::deplog(DepTable & head)
566 {
567         // This function reads the LaTeX log file end extracts all the external
568         // files used by the LaTeX run. The files are then entered into the
569         // dependency file.
570
571         string logfile = OnlyFilename(ChangeExtension(file, ".log"));
572
573         LRegex reg1(")* *\\(([^ \\)]+).*");
574         LRegex reg2("File: ([^ ]+).*");
575         LRegex reg3("No file ([^ ]+)\\..*");
576         LRegex reg4("\\\\openout[0-9]+.*=.*`([^ ]+)'\\..*");
577         LRegex unwanted("^.*\\.(aux|log|dvi|bbl|ind|glo)$");
578         
579         ifstream ifs(logfile.c_str());
580         while (ifs) {
581                 // Ok, the scanning of files here is not sufficient.
582                 // Sometimes files are named by "File: xxx" only
583                 // So I think we should use some regexps to find files instead.
584                 // "(\([^ ]+\)"   should match the "(file " variant
585                 // "File: \([^ ]+\)" should match the "File: file" variant
586                 string foundfile;
587                 string token;
588                 getline(ifs, token);
589                 if (token.empty()) continue;
590                 
591                 if (reg1.exact_match(token)) {
592                         LRegex::SubMatches const & sub = reg1.exec(token);
593                         foundfile = LSubstring(token, sub[1].first,
594                                                sub[1].second);
595                 } else if (reg2.exact_match(token)) {
596                         LRegex::SubMatches const & sub = reg2.exec(token);
597                         foundfile = LSubstring(token, sub[1].first,
598                                                sub[1].second);
599                 } else if (reg3.exact_match(token)) {
600                         LRegex::SubMatches const & sub = reg3.exec(token);
601                         foundfile = LSubstring(token, sub[1].first,
602                                                sub[1].second);
603                 } else if (reg4.exact_match(token)) {
604                         LRegex::SubMatches const & sub = reg4.exec(token);
605                         foundfile = LSubstring(token, sub[1].first,
606                                                sub[1].second);
607                 } else {
608                         continue;
609                 }
610
611                 lyxerr[Debug::DEPEND] << "Found file: " 
612                                       << foundfile << endl;
613                 
614                 // Ok now we found a file.
615                 // Now we should make sure that this is a file that we can
616                 // access through the normal paths.
617                 // We will not try any fancy search methods to
618                 // find the file.
619                 
620                 // (1) foundfile is an
621                 //     absolute path and should
622                 //     be inserted.
623                 if (AbsolutePath(foundfile)) {
624                         lyxerr[Debug::DEPEND] << "AbsolutePath file: " 
625                                               << foundfile << endl;
626                         // On initial insert we want to do the update at once
627                         // since this file can not be a file generated by
628                         // the latex run.
629                         head.insert(foundfile, true);
630                         continue;
631                 }
632
633                 // (2) foundfile is in the tmpdir
634                 //     insert it into head
635                 if (FileInfo(OnlyFilename(foundfile)).exist()) {
636                         if (unwanted.exact_match(foundfile)) {
637                                 lyxerr[Debug::DEPEND]
638                                         << "We don't want "
639                                         << OnlyFilename(foundfile)
640                                         << " in the dep file"
641                                         << endl;
642                         } else if (suffixIs(foundfile, ".tex")) {
643                                 // This is a tex file generated by LyX
644                                 // and latex is not likely to change this
645                                 // during its runs.
646                                 lyxerr[Debug::DEPEND]
647                                         << "Tmpdir TeX file: "
648                                         << OnlyFilename(foundfile)
649                                         << endl;
650                                 head.insert(foundfile, true);
651                         } else {
652                                 lyxerr[Debug::DEPEND]
653                                         << "In tmpdir file:"
654                                         << OnlyFilename(foundfile)
655                                         << endl;
656                                 head.insert(OnlyFilename(foundfile));
657                         }
658                         continue;
659                 }
660                 lyxerr[Debug::DEPEND]
661                         << "Not a file or we are unable to find it."
662                         << endl;
663         }
664 }