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