]> git.lyx.org Git - lyx.git/blob - src/converter.C
More fixes to insettabular/text (and some missing features added).
[lyx.git] / src / converter.C
1 /* This file is part of
2  * ====================================================== 
3  * 
4  *           LyX, The Document Processor
5  *        
6  *           Copyright 1995 Matthias Ettrich
7  *           Copyright 1995-2000 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include <cctype>
18
19 #include "converter.h"
20 #include "lyxrc.h"
21 #include "support/syscall.h"
22 #include "support/path.h"
23 #include "support/filetools.h"
24 #include "buffer.h"
25 #include "bufferview_funcs.h"
26 #include "LaTeX.h"
27 #include "LyXView.h"
28 #include "minibuffer.h"
29 #include "lyx_gui_misc.h"
30 #include "lyx_cb.h" // ShowMessage()
31 #include "support/lyxfunctional.h"
32
33 using std::vector;
34 using std::queue;
35 using std::endl;
36 using std::fill;
37 using std::find_if;
38 using std::reverse;
39 using std::sort;
40
41 static string const token_from("$$i");
42 static string const token_base("$$b");
43 static string const token_to("$$o");
44
45 //////////////////////////////////////////////////////////////////////////////
46
47 static inline
48 string const add_options(string const & command, string const & options)
49 {
50         string head;
51         string const tail = split(command, head, ' ');
52         return head + ' ' + options + ' ' + tail;
53 }
54
55 //////////////////////////////////////////////////////////////////////////////
56
57 bool Format::dummy() const
58 {
59         return extension().empty();
60 }
61
62
63 bool Format::IsChildFormat() const
64 {
65         if (name_.empty())
66                 return false;
67         return isdigit(name_[name_.length() - 1]);
68 }
69
70
71 string const Format::ParentFormat() const
72 {
73         return name_.substr(0, name_.length() - 1);
74 }
75
76 //////////////////////////////////////////////////////////////////////////////
77
78 // This method should return a reference, and throw an exception
79 // if the format named name cannot be found (Lgb)
80 Format const * Formats::GetFormat(string const & name) const
81 {
82         FormatList::const_iterator cit =
83                 find_if(formatlist.begin(), formatlist.end(),
84                         compare_memfun(&Format::name, name));
85         if (cit != formatlist.end())
86                 return &(*cit);
87         else
88                 return 0;
89 }
90
91
92 int Formats::GetNumber(string const & name) const
93 {
94         FormatList::const_iterator cit =
95                 find_if(formatlist.begin(), formatlist.end(),
96                         compare_memfun(&Format::name, name));
97         if (cit != formatlist.end())
98                 return cit - formatlist.begin();
99         else
100                 return -1;
101 }
102
103
104 void Formats::Add(string const & name)
105 {
106         if (!GetFormat(name))
107                 Add(name, name, name, string());
108 }
109
110
111 void Formats::Add(string const & name, string const & extension, 
112                   string const & prettyname, string const & shortcut)
113 {
114         FormatList::iterator it = 
115                 find_if(formatlist.begin(), formatlist.end(),
116                         compare_memfun(&Format::name, name));
117         if (it == formatlist.end())
118                 formatlist.push_back(Format(name, extension, prettyname,
119                                             shortcut, ""));
120         else {
121                 string viewer = it->viewer();
122                 *it = Format(name, extension, prettyname, shortcut, viewer);
123         }
124 }
125
126
127 void Formats::Delete(string const & name)
128 {
129         FormatList::iterator it = 
130                 find_if(formatlist.begin(), formatlist.end(),
131                         compare_memfun(&Format::name, name));
132         if (it != formatlist.end())
133                 formatlist.erase(it);
134 }
135
136
137 void Formats::Sort()
138 {
139         sort(formatlist.begin(), formatlist.end());
140 }
141
142
143 void Formats::SetViewer(string const & name, string const & command)
144 {
145         Add(name);
146         FormatList::iterator it =
147                 find_if(formatlist.begin(), formatlist.end(),
148                         compare_memfun(&Format::name, name));
149         if (it != formatlist.end())
150                 it->setViewer(command);
151 }
152
153
154 bool Formats::View(Buffer const * buffer, string const & filename,
155                    string const & format_name) const
156 {
157         if (filename.empty())
158                 return false;
159
160         Format const * format = GetFormat(format_name);
161         if (format && format->viewer().empty() &&
162             format->IsChildFormat())
163                 format = GetFormat(format->ParentFormat());
164         if (!format || format->viewer().empty()) {
165                 WriteAlert(_("Can not view file"),
166                            _("No information for viewing ")
167                            + PrettyName(format_name));
168                            return false;
169         }
170
171         string command = format->viewer();
172
173         if (format_name == "dvi" &&
174             !lyxrc.view_dvi_paper_option.empty()) {
175                 command += " " + lyxrc.view_dvi_paper_option;
176                 command += " " + converters.dvi_papersize(buffer);
177                 if (buffer->params.orientation 
178                     == BufferParams::ORIENTATION_LANDSCAPE)
179                         command += 'r';
180         }
181
182         command += " " + QuoteName(OnlyFilename((filename)));
183
184         lyxerr << "Executing command: " << command << endl;
185         ShowMessage(buffer, _("Executing command:"), command);
186
187         Path p(OnlyPath(filename));
188         Systemcalls one;
189         int const res = one.startscript(Systemcalls::SystemDontWait, command);
190
191         if (res) {
192                 WriteAlert(_("Can not view file"),
193                            _("Error while executing"),
194                            command.substr(0, 50));
195                 return false;
196         }
197         return true;
198 }
199
200
201 string const Formats::PrettyName(string const & name) const
202 {
203         Format const * format = GetFormat(name);
204         if (format)
205                 return format->prettyname();
206         else
207                 return name;
208 }
209
210
211 string const Formats::Extension(string const & name) const
212 {
213         Format const * format = GetFormat(name);
214         if (format)
215                 return format->extension();
216         else
217                 return name;
218 }
219
220 //////////////////////////////////////////////////////////////////////////////
221
222 void Converter::ReadFlags()
223 {
224         string flag_list(flags);
225         while (!flag_list.empty()) {
226                 string flag_name, flag_value;
227                 flag_list = split(flag_list, flag_value, ',');
228                 flag_value = split(flag_value, flag_name, '=');
229                 if (flag_name == "latex")
230                         latex = true;
231                 else if (flag_name == "originaldir")
232                         original_dir = true;
233                 else if (flag_name == "needaux")
234                         need_aux = true;
235                 else if (flag_name == "resultdir")
236                         result_dir = (flag_value.empty())
237                                 ? token_base : flag_value;
238                 else if (flag_name == "resultfile")
239                         result_file = flag_value;
240                 else if (flag_name == "parselog")
241                         parselog = flag_value;
242         }
243         if (!result_dir.empty() && result_file.empty())
244                 result_file = "index." + formats.Extension(to);
245         //if (!contains(command, token_from))
246         //      latex = true;
247 }
248
249
250 bool operator<(Converter const & a, Converter const & b)
251 {
252         int const i = compare_no_case(a.From->prettyname(),
253                                       b.From->prettyname());
254         if (i == 0)
255                 return compare_no_case(a.To->prettyname(), b.To->prettyname())
256                         < 0;
257         else
258                 return i < 0;
259 }
260
261 //////////////////////////////////////////////////////////////////////////////
262
263 class compare_Converter {
264 public:
265         compare_Converter(string const & f, string const & t)
266                 : from(f), to(t) {}
267         bool operator()(Converter const & c) {
268                 return c.from == from && c.to == to;
269         }
270 private:
271         string const & from;
272         string const & to;
273 };
274
275
276 Converter const * Converters::GetConverter(string const & from,
277                                             string const & to)
278 {
279         ConverterList::const_iterator cit =
280                 find_if(converterlist.begin(), converterlist.end(),
281                         compare_Converter(from, to));
282         if (cit != converterlist.end())
283                 return &(*cit);
284         else
285                 return 0;
286 }
287
288
289 int Converters::GetNumber(string const & from, string const & to)
290 {
291         ConverterList::const_iterator cit =
292                 find_if(converterlist.begin(), converterlist.end(),
293                         compare_Converter(from, to));
294         if (cit != converterlist.end())
295                 return cit - converterlist.begin();
296         else
297                 return -1;
298 }
299
300
301 void Converters::Add(string const & from, string const & to,
302                      string const & command, string const & flags)
303 {
304         formats.Add(from);
305         formats.Add(to);
306         ConverterList::iterator it = find_if(converterlist.begin(),
307                                              converterlist.end(),
308                                              compare_Converter(from, to));
309
310         Converter converter(from, to, command, flags);
311         if (it != converterlist.end() && !flags.empty() && flags[0] == '*') {
312                 converter = *it;
313                 converter.command = command;
314                 converter.flags = flags;
315         }
316         converter.ReadFlags();
317         
318         if (converter.latex && (latex_command.empty() || to == "dvi"))
319                 latex_command = subst(command, token_from, "");
320         // If we have both latex & pdflatex, we set latex_command to latex.
321         // The latex_command is used to update the .aux file when running
322         // a converter that uses it.
323
324         if (it == converterlist.end())
325                 converterlist.push_back(converter);
326         else {
327                 converter.From = it->From;
328                 converter.To = it->To;
329                 *it = converter;
330         }
331 }
332
333
334 void Converters::Delete(string const & from, string const & to)
335 {
336         ConverterList::iterator it = find_if(converterlist.begin(),
337                                              converterlist.end(),
338                                              compare_Converter(from, to));
339         if (it != converterlist.end())
340                 converterlist.erase(it);
341 }
342
343
344 // This method updates the pointers From and To in all the converters.
345 // The code is not very efficient, but it doesn't matter as the number
346 // of formats and converters is small.
347 // Furthermore, this method is called only on startup, or after 
348 // adding/deleting a format in FormPreferences (the latter calls can be
349 // eliminated if the formats in the Formats class are stored using a map or
350 // a list (instead of a vector), but this will cause other problems). 
351 void Converters::Update(Formats const & formats)
352 {
353         for (ConverterList::iterator it = converterlist.begin();
354              it != converterlist.end(); ++it) {
355                 it->From = formats.GetFormat(it->from);
356                 it->To = formats.GetFormat(it->to);
357         }
358 }
359
360
361 // This method updates the pointers From and To in the last converter.
362 // It is called when adding a new converter in FormPreferences
363 void Converters::UpdateLast(Formats const & formats)
364 {
365         if (converterlist.begin() != converterlist.end()) {
366                 ConverterList::iterator it = converterlist.end()-1;
367                 it->From = formats.GetFormat(it->from);
368                 it->To = formats.GetFormat(it->to);
369         }
370 }
371
372
373 void Converters::Sort()
374 {
375         sort(converterlist.begin(), converterlist.end());
376 }
377
378
379 int Converters::BFS_init(string const & start, bool clear_visited)
380 {
381         int const s = formats.GetNumber(start);
382         if (s < 0)
383                 return s;
384
385         Q = queue<int>();
386         if (clear_visited)
387                 fill(visited.begin(), visited.end(), false);
388         if (visited[s] == false) {
389                 Q.push(s);
390                 visited[s] = true;
391         }
392         return s;
393 }
394
395
396 vector<Format const *> const
397 Converters::GetReachableTo(string const & target, bool clear_visited)
398 {
399         vector<Format const *> result;
400         int const s = BFS_init(target, clear_visited);
401         if (s < 0)
402                 return result;
403
404         while (!Q.empty()) {
405                 int const i = Q.front();
406                 Q.pop();
407                 if (i != s || target != "lyx")
408                         result.push_back(&formats.Get(i));
409                 for (vector<int>::iterator it = vertices[i].in_vertices.begin();
410                      it != vertices[i].in_vertices.end(); ++it)
411                         if (!visited[*it]) {
412                                 visited[*it] = true;
413                                 Q.push(*it);
414                         }
415         }
416
417         return result;
418 }
419
420
421 vector<Format const *> const
422 Converters::GetReachable(string const & from, bool only_viewable,
423                          bool clear_visited)
424 {
425         vector<Format const *> result;
426
427         if (BFS_init(from, clear_visited) < 0)
428                 return result;
429
430         while (!Q.empty()) {
431                 int const i = Q.front();
432                 Q.pop();
433                 Format const & format = formats.Get(i);
434                 if (format.name() == "lyx")
435                         continue;
436                 if (!only_viewable || !format.viewer().empty() ||
437                     format.IsChildFormat())
438                         result.push_back(&format);
439                 for (vector<int>::iterator it = vertices[i].out_vertices.begin();
440                      it != vertices[i].out_vertices.end(); ++it)
441                         if (!visited[*it]) {
442                                 visited[*it] = true;
443                                 Q.push(*it);
444                         }
445         }
446
447         return result;
448 }
449
450
451 bool Converters::IsReachable(string const & from, string const & to)
452 {
453         if (from == to)
454                 return true;
455
456         int const s = BFS_init(from);
457         int const t = formats.GetNumber(to);
458         if (s < 0 || t < 0)
459                 return false;
460
461         while (!Q.empty()) {
462                 int const i = Q.front();
463                 Q.pop();
464                 if (i == t)
465                         return true;
466                 for (vector<int>::iterator it = vertices[i].out_vertices.begin();
467                      it != vertices[i].out_vertices.end(); ++it)
468                         if (!visited[*it]) {
469                                 visited[*it] = true;
470                                 Q.push(*it);
471                         }
472         }
473
474         return false;
475 }
476
477
478 Converters::EdgePath const
479 Converters::GetPath(string const & from, string const & to)
480 {
481         EdgePath path;
482         if (from == to)
483                 return path;
484
485         int const s = BFS_init(from);
486         int t = formats.GetNumber(to);
487         if (s < 0 || t < 0)
488                 return path;
489
490         vector<int> prev_edge(formats.size());
491         vector<int> prev_vertex(formats.size());
492
493         bool found = false;
494         while (!Q.empty()) {
495                 int const i = Q.front();
496                 Q.pop();
497                 if (i == t) {
498                         found = true;
499                         break;
500                 }
501                 for (vector<int>::iterator it = vertices[i].out_vertices.begin();
502                      it != vertices[i].out_vertices.end(); ++it)
503                         if (!visited[*it]) {
504                                 int const j = *it;
505                                 visited[j] = true;
506                                 Q.push(j);
507                                 int const k = it - vertices[i].out_vertices.begin();
508                                 prev_edge[j] = vertices[i].out_edges[k];
509                                 prev_vertex[j] = i;
510                         }
511         }
512         if (!found)
513                 return path;
514
515         while (t != s) {
516                 path.push_back(prev_edge[t]);
517                 t = prev_vertex[t];
518         }
519         reverse(path.begin(), path.end());
520         return path;
521 }
522
523
524 bool Converters::UsePdflatex(EdgePath const & path)
525 {
526         for (EdgePath::const_iterator cit = path.begin();
527              cit != path.end(); ++cit) {
528                 Converter const & conv = converterlist[*cit];
529                 if (conv.latex)
530                         return contains(conv.to, "pdf");
531         }
532         return false;
533 }
534
535
536 bool Converters::Convert(Buffer const * buffer,
537                          string const & from_file, string const & to_file_base,
538                          string const & from_format, string const & to_format,
539                          string & to_file)
540 {
541         to_file = ChangeExtension(to_file_base,
542                                   formats.Extension(to_format));
543
544         if (from_format == to_format)
545                 return Move(from_file, to_file, false);
546
547         EdgePath edgepath = GetPath(from_format, to_format);
548         if (edgepath.empty()) {
549                 WriteAlert(_("Can not convert file"),
550                            _("No information for converting from ")
551                            + formats.PrettyName(from_format) + _(" to ")
552                            + formats.PrettyName(to_format));
553                 return false;
554         }
555
556         string path = OnlyPath(from_file);
557         Path p(path);
558
559         bool run_latex = false;
560         string from_base = ChangeExtension(from_file, "");
561         string to_base = ChangeExtension(to_file, "");
562         string infile;
563         string outfile = from_file;
564         for (EdgePath::const_iterator cit = edgepath.begin();
565              cit != edgepath.end(); ++cit) {
566                 Converter const & conv = converterlist[*cit];
567                 bool dummy = conv.To->dummy() && conv.to != "program";
568                 if (!dummy)
569                         lyxerr << "Converting from  "
570                                << conv.from << " to " << conv.to << endl;
571                 infile = outfile;
572                 outfile = conv.result_dir.empty()
573                         ? ChangeExtension(from_file, conv.To->extension())
574                         : AddName(subst(conv.result_dir,
575                                         token_base, from_base),
576                                   subst(conv.result_file,
577                                         token_base, OnlyFilename(from_base)));
578
579                 if (conv.latex) {
580                         run_latex = true;
581                         string command = subst(conv.command, token_from, "");
582                         lyxerr << "Running " << command << endl;
583                         if (!runLaTeX(buffer, command))
584                                 return false;
585                 } else {
586                         if (conv.need_aux && !run_latex
587                             && !latex_command.empty()) {
588                                 lyxerr << "Running " << latex_command 
589                                        << " to update aux file"<<  endl;
590                                 runLaTeX(buffer, latex_command);
591                         }
592
593                         string infile2 = (conv.original_dir)
594                                 ? infile : MakeRelPath(infile, path);
595                         string outfile2 = (conv.original_dir)
596                                 ? outfile : MakeRelPath(outfile, path);
597
598                         string command = conv.command;
599                         command = subst(command, token_from, QuoteName(infile2));
600                         command = subst(command, token_base, QuoteName(from_base));
601                         command = subst(command, token_to, QuoteName(outfile2));
602
603                         if (!conv.parselog.empty())
604                                 command += " 2> " + QuoteName(infile2 + ".out");
605
606                         if (conv.from == "dvi" && conv.to == "ps")
607                                 command = add_options(command,
608                                                       dvips_options(buffer));
609
610                         lyxerr << "Calling " << command << endl;
611                         if (buffer)
612                                 ShowMessage(buffer, _("Executing command:"), command);
613
614                         Systemcalls::Starttype type = (dummy)
615                                 ? Systemcalls::SystemDontWait : Systemcalls::System;
616                         Systemcalls one;
617                         int res;
618                         if (conv.original_dir && buffer) {
619                                 Path p(buffer->filepath);
620                                 res = one.startscript(type, command);
621                         } else
622                                 res = one.startscript(type, command);
623
624                         if (!conv.parselog.empty()) {
625                                 string const logfile =  infile2 + ".log";
626                                 string const command2 = conv.parselog +
627                                         " < " + QuoteName(infile2 + ".out") +
628                                         " > " + QuoteName(logfile);
629                                 one.startscript(Systemcalls::System, command2);
630                                 if (!scanLog(buffer, command, logfile))
631                                         return false;
632                         }
633
634                         if (res) {
635                                 if (conv.to == "program")
636                                         WriteAlert(_("There were errors during the Build process."),
637                                                    _("You should try to fix them."));
638                                 else
639                                         WriteAlert(_("Can not convert file"),
640                                                    "Error while executing",
641                                                    command.substr(0, 50));
642                                 return false;
643                         }
644                 }
645         }
646
647         Converter const & conv = converterlist[edgepath.back()];
648         if (conv.To->dummy())
649                 return true;
650
651
652         if (!conv.result_dir.empty()) {
653                 to_file = AddName(subst(conv.result_dir, token_base, to_base),
654                                   subst(conv.result_file,
655                                         token_base, OnlyFilename(to_base)));
656                 if (from_base != to_base) {
657                         string from = subst(conv.result_dir,
658                                             token_base, from_base);
659                         string to = subst(conv.result_dir,
660                                           token_base, to_base);
661                         if (!lyx::rename(from, to)) {
662                                 WriteAlert(_("Error while trying to move directory:"),
663                                            from, ("to ") + to);
664                                 return false;
665                         }
666                 }
667                 return true;
668         } else 
669                 return Move(outfile, to_file, conv.latex);
670 }
671
672 // If from = /path/file.ext and to = /path2/file2.ext2 then this method 
673 // moves each /path/file*.ext file to /path2/file2*.ext2'
674 bool Converters::Move(string const & from, string const & to, bool copy)
675 {
676         if (from == to)
677                 return true;
678
679         bool no_errors = true;
680         string const path = OnlyPath(from);
681         string const base = OnlyFilename(ChangeExtension(from, ""));
682         string const to_base = ChangeExtension(to, "");
683         string const to_extension = GetExtension(to);
684
685         vector<string> files = DirList(OnlyPath(from), GetExtension(from));
686         for (vector<string>::const_iterator it = files.begin();
687              it != files.end(); ++it)
688                 if (prefixIs(*it, base)) {
689                         string from2 = path + *it;
690                         string to2 = to_base + (*it).substr(base.length());
691                         to2 = ChangeExtension(to2, to_extension);
692                         lyxerr << "moving " << from2 << " to " << to2 << endl;
693                         bool moved = (copy)
694                                 ? lyx::copy(from2, to2)
695                                 : lyx::rename(from2, to2);
696                         if (!moved && no_errors) {
697                                 WriteAlert(_("Error while trying to move file:"),
698                                            from2, _("to ") + to2);
699                                 no_errors = false;
700                         }
701                 }
702         return no_errors;
703 }
704
705 bool Converters::Convert(Buffer const * buffer,
706                         string const & from_file, string const & to_file_base,
707                         string const & from_format, string const & to_format)
708 {
709         string to_file;
710         return Convert(buffer, from_file, to_file_base, from_format, to_format,
711                        to_file);
712 }
713
714
715
716 void Converters::BuildGraph()
717 {
718         vertices = vector<Vertex>(formats.size());
719         visited.resize(formats.size());
720
721         for (ConverterList::iterator it = converterlist.begin();
722              it != converterlist.end(); ++it) {
723                 int const s = formats.GetNumber(it->from);
724                 int const t = formats.GetNumber(it->to);
725                 vertices[t].in_vertices.push_back(s);
726                 vertices[s].out_vertices.push_back(t);
727                 vertices[s].out_edges.push_back(it - converterlist.begin());
728         }
729 }
730
731
732 bool Converters::FormatIsUsed(string const & format)
733 {
734         for (ConverterList::const_iterator cit = converterlist.begin();
735             cit != converterlist.end(); ++cit)
736                 if (cit->from == format || cit->to == format)
737                         return true;
738         return false;
739 }
740
741
742 bool Converters::scanLog(Buffer const * buffer, string const & command,
743                         string const & filename)
744 {
745         if (!buffer)
746                 return false;
747
748         BufferView * bv = buffer->getUser();
749         bool need_redraw = false;
750         if (bv) {
751                 ProhibitInput(bv);
752                 // Remove all error insets
753                 need_redraw = bv->removeAutoInsets();
754         }
755
756         LaTeX latex("", filename, "");
757         TeXErrors terr;
758         int result = latex.scanLogFile(terr);
759         if (bv) {
760                 if ((result & LaTeX::ERRORS)) {
761                         // Insert all errors as errors boxes
762                         bv->insertErrors(terr);
763                         need_redraw = true;
764                 }
765                 if (need_redraw) {
766                         bv->redraw();
767                         bv->fitCursor(bv->text);
768                 }
769                 AllowInput(bv);
770         }
771
772         if ((result & LaTeX::ERRORS)) {
773                 int num_errors = latex.getNumErrors();
774                 string s;
775                 string t;
776                 if (num_errors == 1) {
777                         s = _("One error detected");
778                         t = _("You should try to fix it.");
779                 } else {
780                         s = tostr(num_errors);
781                         s += _(" errors detected.");
782                         t = _("You should try to fix them.");
783                 }
784                 string head;
785                 split(command, head, ' ');
786                 WriteAlert(_("There were errors during running of ") + head,
787                            s, t);
788                 return false;
789         } else if (result & LaTeX::NO_OUTPUT) {
790                 string const s = _("The operation resulted in");
791                 string const t = _("an empty file.");
792                 WriteAlert(_("Resulting file is empty"), s, t);
793                 return false;
794         }
795         return true;
796 }
797
798
799 bool Converters::runLaTeX(Buffer const * buffer, string const & command)
800 {
801         if (!buffer)
802                 return false;
803
804         BufferView * bv = buffer->getUser();
805         string name = buffer->getLatexName();
806         bool need_redraw = false;
807
808         if (bv) {
809                 ProhibitInput(bv);
810                 bv->owner()->getMiniBuffer()->Set(_("Running LaTeX..."));
811                 // Remove all error insets
812                 need_redraw = bv->removeAutoInsets();
813         }
814
815
816         // do the LaTex run(s)
817         TeXErrors terr;
818         LaTeX latex(command, name, buffer->filepath);
819         int result = latex.run(terr,
820                                bv ? bv->owner()->getMiniBuffer() : 0);
821         
822
823         if (bv) {
824                 if ((result & LaTeX::ERRORS)) {
825                         // Insert all errors as errors boxes
826                         bv->insertErrors(terr);
827                         need_redraw = true;
828                 }
829
830                 // if we removed error insets before we ran LaTeX or if we inserted
831                 // error insets after we ran LaTeX this must be run:
832                 if (need_redraw) {
833                         bv->redraw();
834                         bv->fitCursor(bv->text);
835                 }
836         }
837
838         // check return value from latex.run().
839         if ((result & LaTeX::NO_LOGFILE)) {
840                 WriteAlert(_("LaTeX did not work!"),
841                            _("Missing log file:"), name);
842         } else if ((result & LaTeX::ERRORS)) {
843                 int num_errors = latex.getNumErrors();
844                 string s;
845                 string t;
846                 if (num_errors == 1) {
847                         s = _("One error detected");
848                         t = _("You should try to fix it.");
849                 } else {
850                         s = tostr(num_errors);
851                         s += _(" errors detected.");
852                         t = _("You should try to fix them.");
853                 }
854                 WriteAlert(_("There were errors during the LaTeX run."),
855                            s, t);
856         }  else if (result & LaTeX::NO_OUTPUT) {
857                 string const s = _("The operation resulted in");
858                 string const t = _("an empty file.");
859                 WriteAlert(_("Resulting file is empty"), s, t);
860         }
861
862         if (bv)
863                 AllowInput(bv);
864  
865         int const ERROR_MASK = 
866                         LaTeX::NO_LOGFILE |
867                         LaTeX::ERRORS |
868                         LaTeX::NO_OUTPUT;
869         
870         return (result & ERROR_MASK) == 0;
871
872 }
873
874
875 string const Converters::dvi_papersize(Buffer const * buffer)
876 {
877         char real_papersize = buffer->params.papersize;
878         if (real_papersize == BufferParams::PAPER_DEFAULT)
879                 real_papersize = lyxrc.default_papersize;
880
881         switch (real_papersize) {
882         case BufferParams::PAPER_A3PAPER:
883                 return "a3";
884         case BufferParams::PAPER_A4PAPER:
885                 return "a4";
886         case BufferParams::PAPER_A5PAPER:
887                 return "a5";
888         case BufferParams::PAPER_B5PAPER:
889                 return "b5";
890         case BufferParams::PAPER_EXECUTIVEPAPER:
891                 return "foolscap";
892         case BufferParams::PAPER_LEGALPAPER:
893                 return "legal";
894         case BufferParams::PAPER_USLETTER:
895         default:
896                 return "us";
897         }
898 }
899
900
901 string const Converters::dvips_options(Buffer const * buffer)
902 {
903         string result;
904         if (!buffer)
905                 return result;
906
907         if (buffer->params.use_geometry
908             && buffer->params.papersize2 == BufferParams::VM_PAPER_CUSTOM
909             && !lyxrc.print_paper_dimension_flag.empty()
910             && !buffer->params.paperwidth.empty()
911             && !buffer->params.paperheight.empty()) {
912                 // using a custom papersize
913                 result = lyxrc.print_paper_dimension_flag;
914                 result += ' ' + buffer->params.paperwidth;
915                 result += ',' + buffer->params.paperheight;
916         } else {
917                 string paper_option = dvi_papersize(buffer);
918                 if (paper_option == "us")
919                         paper_option = "letter";
920                 if (paper_option != "letter" ||
921                     buffer->params.orientation != BufferParams::ORIENTATION_LANDSCAPE) {
922                         // dvips won't accept -t letter -t landscape.  In all other
923                         // cases, include the paper size explicitly.
924                         result = lyxrc.print_paper_flag;
925                         result += ' ' + paper_option;
926                 }
927         }
928         if (buffer->params.orientation == BufferParams::ORIENTATION_LANDSCAPE)
929                 result += ' ' + lyxrc.print_landscape_flag;
930         return result;
931 }
932
933 vector<Converters::Vertex> Converters::vertices;
934
935
936 /// The global instance
937 Formats formats;
938 Converters converters;
939
940 // The global copy after reading lyxrc.defaults
941 Formats system_formats;
942 Converters system_converters;